Skip to content

Commit

Permalink
http-gateway: Implement Web of Things Interface
Browse files Browse the repository at this point in the history
## New Features
- **/things Endpoint**: Introduced a new endpoint, `/things`, to retrieve resource links for devices. This endpoint provides a standardized way to access and interact with connected devices.
  
## Tests
- **Endpoint Testing**: Added comprehensive tests for the new `/things` endpoint. These tests ensure that device resources are properly retrieved and formatted, maintaining robustness and reliability in various scenarios.

## Refactor
- **JSON Unmarshalling**: Enhanced the JSON unmarshalling process in HTTP gateway tests. This improvement focuses on better error handling and more efficient processing of JSON data, contributing to overall test stability and readability.

## Documentation
- **Updated Documentation**: Revised the documentation to reflect changes in the usage of constants and parameters during virtual device creation. The updates include support for conditional resource addition based on the availability of device descriptions, making the documentation more accurate and helpful.

## New Endpoints
- **/api/v1/things**: Implemented the `/api/v1/things` endpoint to list all device resources.
- **/api/v1/things/{deviceID}**: Implemented the `/api/v1/things/{deviceID}` endpoint to access resources specific to a device identified by `deviceID`.

## Miscellaneous
- **Golang Upgrade**: Upgraded the Golang version to 1.22, ensuring compatibility with the latest features and improvements in the Go ecosystem.

---------

Co-authored-by: Jozef Kralik <[email protected]>
  • Loading branch information
Danielius1922 and jkralik authored May 15, 2024
1 parent 331c4db commit 7cfb6e5
Show file tree
Hide file tree
Showing 41 changed files with 1,528 additions and 116 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
matrix:
include:
# check support for oldest supported golang version
- name: go1.20
go-version: "~1.20"
- name: go1.22
go-version: "~1.22"
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -22,7 +22,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version || '^1.20' }}
go-version: ${{ matrix.go-version || '^1.22' }}
check-latest: true

- run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/checkFormat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: "^1.20" # The Go version to download (if necessary) and use.
go-version: "^1.22" # The Go version to download (if necessary) and use.
check-latest: true

- name: Check formatting
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "^1.20" # The Go version to download (if necessary) and use.
go-version: "^1.22" # The Go version to download (if necessary) and use.
check-latest: true
cache: false

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/staticAnalysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "^1.20" # The Go version to download (if necessary) and use.
go-version: "^1.22" # The Go version to download (if necessary) and use.
check-latest: true
- run: go version

Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ jobs:
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

# login to ghcr.io so we can download device/bridge-device package
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Run a test
run: |
make ${{ matrix.cmd }} TEST_CHECK_RACE=${{ matrix.checkRace }} \
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ issues:
# fix: true

run:
go: "1.20"
go: "1.22"
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"TEST_IDENTITY_STORE_LOG_LEVEL": "info",
"TEST_IDENTITY_STORE_LOG_DUMP_BODY": "false",
"TEST_DATABASE": "mongoDB",
// "TEST_DEVICE_NAME": "bridged-device",
"TEST_BRIDGE_DEVICE_CONFIG": "${workspaceFolder}/.tmp/bridge/config-test.yaml",
// "TEST_DEVICE_NAME": "bridged-device-0",
// "TEST_DEVICE_TYPE": "bridged",
// "GODEBUG": "scavtrace=1",
// "TEST_COAP_GATEWAY_UDP_ENABLED": "true",
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apt-get update \
# apt: ca-certificates git make sudo
RUN git clone https://github.com/udhos/update-golang.git \
&& cd update-golang \
&& sudo RELEASE=1.20.14 ./update-golang.sh \
&& sudo RELEASE=1.22.3 ./update-golang.sh \
&& ln -s /usr/local/go/bin/go /usr/bin/go
WORKDIR $GOPATH/src/github.com/plgd-dev/hub
COPY go.mod go.sum ./
Expand Down
62 changes: 61 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST_DATABASE ?= mongodb
CERT_TOOL_SIGN_ALG ?= ECDSA-SHA256
# supported values: P256, P384, P521
CERT_TOOL_ELLIPTIC_CURVE ?= P256
CERT_TOOL_IMAGE=ghcr.io/plgd-dev/hub/cert-tool:vnext
CERT_TOOL_IMAGE = ghcr.io/plgd-dev/hub/cert-tool:vnext

SUBDIRS := bundle certificate-authority cloud2cloud-connector cloud2cloud-gateway coap-gateway grpc-gateway resource-aggregate resource-directory http-gateway identity-store test/oauth-server tools/cert-tool
.PHONY: $(SUBDIRS) push proto/generate clean build test env mongo nats certificates hub-build http-gateway-www simulators
Expand Down Expand Up @@ -225,6 +225,64 @@ simulators: simulators/clean
$(call RUN-DOCKER-DEVICE,$(DEVICE_SIMULATOR_RES_OBSERVABLE_NAME),$(DEVICE_SIMULATOR_RES_OBSERVABLE_IMG))
.PHONY: simulators

BRIDGE_DEVICE_SRC_DIR = $(WORKING_DIRECTORY)/test/bridge-device
BRIDGE_DEVICE_IMAGE = ghcr.io/plgd-dev/device/bridge-device:vnext
BRIDGE_DEVICE_NAME = bridgedev
BRIDGE_DEVICE_ID ?= 8f596b43-29c0-4147-8b40-e99268ab30f7
BRIDGE_DEVICE_RESOURCES_PER_DEVICE ?= 3
BRIDGE_DEVICES_COUNT ?= 3

define SET-BRIDGE-DEVICE-CONFIG
yq -i '.apis.coap.id = "$(BRIDGE_DEVICE_ID)"' $(1)
yq -i '.apis.coap.externalAddresses=["127.0.0.1:15683","[::1]:15683"]' $(1)
yq -i '.cloud.enabled=true' $(1)
yq -i '.cloud.cloudID="$(CLOUD_SID)"' $(1)
yq -i '.cloud.tls.caPoolPath="$(2)/certs/root_ca.crt"' $(1)
yq -i '.cloud.tls.keyPath="$(2)/certs/coap.key"' $(1)
yq -i '.cloud.tls.certPath="$(2)/certs/coap.crt"' $(1)
yq -i '.numGeneratedBridgedDevices=$(BRIDGE_DEVICES_COUNT)' $(1)
yq -i '.numResourcesPerDevice=$(BRIDGE_DEVICE_RESOURCES_PER_DEVICE)' $(1)
yq -i '.thingDescription.enabled=true' $(1)
yq -i '.thingDescription.file="$(2)/bridge/bridge-device.jsonld"' $(1)
endef

# config-docker.yaml -> copy of configuration with paths valid inside docker container
# config-test.yaml -> copy of configuration with paths valid on host machine
simulators/bridge/env: simulators/bridge/clean certificates
mkdir -p $(WORKING_DIRECTORY)/.tmp/bridge
cp $(BRIDGE_DEVICE_SRC_DIR)/bridge-device.jsonld $(WORKING_DIRECTORY)/.tmp/bridge/
cp $(BRIDGE_DEVICE_SRC_DIR)/config.yaml $(WORKING_DIRECTORY)/.tmp/bridge/config-docker.yaml
$(call SET-BRIDGE-DEVICE-CONFIG,$(WORKING_DIRECTORY)/.tmp/bridge/config-docker.yaml,)
cp $(BRIDGE_DEVICE_SRC_DIR)/config.yaml $(WORKING_DIRECTORY)/.tmp/bridge/config-test.yaml
$(call SET-BRIDGE-DEVICE-CONFIG,$(WORKING_DIRECTORY)/.tmp/bridge/config-test.yaml,$(WORKING_DIRECTORY)/.tmp)

.PHONY: simulators/bridge/env

define RUN-BRIDGE-DOCKER-DEVICE
docker pull $(BRIDGE_DEVICE_IMAGE) ; \
docker run \
-d \
--name=$(BRIDGE_DEVICE_NAME) \
--network=host \
-v $(WORKING_DIRECTORY)/.tmp/certs:/certs \
-v $(WORKING_DIRECTORY)/.tmp/bridge:/bridge \
$(BRIDGE_DEVICE_IMAGE) -config /bridge/config-docker.yaml
endef

simulators/bridge: simulators/bridge/env
$(call RUN-BRIDGE-DOCKER-DEVICE)

.PHONY: simulators/bridge

simulators/bridge/clean:
rm -rf $(WORKING_DIRECTORY)/.tmp/bridge || :
$(call REMOVE-DOCKER-DEVICE,$(BRIDGE_DEVICE_NAME))

.PHONY: simulators/bridge/clean

simulators: simulators/bridge
simulators/clean: simulators/bridge/clean

env/test/mem: clean certificates nats mongo privateKeys scylla
.PHONY: env/test/mem

Expand All @@ -235,6 +293,7 @@ define RUN-DOCKER
docker run \
--rm \
--network=host \
-v $(WORKING_DIRECTORY)/.tmp/bridge:/bridge \
-v $(WORKING_DIRECTORY)/.tmp/certs:/certs \
-v $(WORKING_DIRECTORY)/.tmp/coverage:/coverage \
-v $(WORKING_DIRECTORY)/.tmp/report:/report \
Expand All @@ -253,6 +312,7 @@ define RUN-DOCKER
-e TEST_OAUTH_SERVER_ID_TOKEN_PRIVATE_KEY=/privKeys/idTokenKey.pem \
-e TEST_OAUTH_SERVER_ACCESS_TOKEN_PRIVATE_KEY=/privKeys/accessTokenKey.pem \
-e TEST_HTTP_GW_WWW_ROOT=/usr/local/www \
-e TEST_BRIDGE_DEVICE_CONFIG=/bridge/config-docker.yaml \
hub-test \
$(1) ;
endef
Expand Down
2 changes: 1 addition & 1 deletion bundle/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1
FROM golang:1.20.14-alpine AS build
FROM golang:1.22.3-alpine AS build
RUN apk add --no-cache curl git build-base
WORKDIR $GOPATH/src/github.com/plgd-dev/hub
COPY go.mod go.sum ./
Expand Down
7 changes: 4 additions & 3 deletions coap-gateway/service/observation/deviceObserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
coapgwTestService "github.com/plgd-dev/hub/v2/test/coap-gateway/service"
coapgwTest "github.com/plgd-dev/hub/v2/test/coap-gateway/test"
"github.com/plgd-dev/hub/v2/test/config"
"github.com/plgd-dev/hub/v2/test/device/ocf"
oauthTest "github.com/plgd-dev/hub/v2/test/oauth-server/test"
pbTest "github.com/plgd-dev/hub/v2/test/pb"
"github.com/plgd-dev/hub/v2/test/service"
Expand Down Expand Up @@ -299,13 +300,13 @@ func testPreregisterVirtualDevice(ctx context.Context, t *testing.T, deviceID st
require.NoError(t, err)
require.NotEmpty(t, ev.GetOperationProcessed())
require.Equal(t, pb.Event_OperationProcessed_ErrorStatus_OK, ev.GetOperationProcessed().GetErrorStatus().GetCode())
virtualdevice.CreateDevice(ctx, t, "name-"+deviceID, deviceID, numResources, test.StringToApplicationProtocol(config.ACTIVE_COAP_SCHEME), isClient, raClient)
resources := virtualdevice.CreateDeviceResourceLinks(deviceID, numResources)
virtualdevice.CreateDevice(ctx, t, "name-"+deviceID, deviceID, numResources, false, test.StringToApplicationProtocol(config.ACTIVE_COAP_SCHEME), isClient, raClient)
resources := virtualdevice.CreateDeviceResourceLinks(deviceID, numResources, false)
links := make([]schema.ResourceLink, 0, len(resources))
for _, r := range resources {
links = append(links, r.ToSchema())
}
test.WaitForDevice(t, client, deviceID, ev.GetSubscriptionId(), ev.GetCorrelationId(), links)
test.WaitForDevice(t, client, ocf.NewDevice(deviceID, test.TestDeviceName), ev.GetSubscriptionId(), ev.GetCorrelationId(), links)
}

func testValidateResourceLinks(ctx context.Context, t *testing.T, deviceID string, grpcClient pb.GrpcGatewayClient, _ raPb.ResourceAggregateClient) {
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/plgd-dev/hub/v2

go 1.20
go 1.22

toolchain go1.22.0

require (
github.com/favadi/protoc-go-inject-tag v1.4.0
Expand Down Expand Up @@ -28,7 +30,7 @@ require (
github.com/panjf2000/ants/v2 v2.9.1
github.com/pion/dtls/v2 v2.2.8-0.20240501061905-2c36d63320a0
github.com/pion/logging v0.2.2
github.com/plgd-dev/device/v2 v2.5.1-0.20240502072920-6021006ea86b
github.com/plgd-dev/device/v2 v2.5.1-0.20240513064831-b553d1a87e1c
github.com/plgd-dev/go-coap/v3 v3.3.4
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90
github.com/pseudomuto/protoc-gen-doc v1.5.1
Expand All @@ -38,6 +40,7 @@ require (
github.com/tidwall/sjson v1.2.5
github.com/ugorji/go/codec v1.2.12
github.com/vincent-petithory/dataurl v1.0.0
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510130416-741fef736e1e
go.mongodb.org/mongo-driver v1.15.0
go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.49.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0
Expand Down Expand Up @@ -71,6 +74,8 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dsnet/golib/memfile v1.0.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/fredbi/uri v1.1.0 // indirect
github.com/go-json-experiment/json v0.0.0-20240418180308-af2d5061e6c2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
Expand Down
Loading

0 comments on commit 7cfb6e5

Please sign in to comment.