From 36411bb35f177749152806b45211d158a802d047 Mon Sep 17 00:00:00 2001 From: Chris Hung Date: Tue, 10 Jan 2023 14:19:30 +0800 Subject: [PATCH] feat: remove device callback calls device service is subscribing device system events and removing REST device callbacks in edgexfoundry/device-sdk-go#1259 The calls to the REST callback can be removed closes #4235 Signed-off-by: Chris Hung --- internal/core/metadata/application/device.go | 15 +-- internal/core/metadata/application/notify.go | 106 +------------------ 2 files changed, 6 insertions(+), 115 deletions(-) diff --git a/internal/core/metadata/application/device.go b/internal/core/metadata/application/device.go index 780d76078c..51957022e6 100644 --- a/internal/core/metadata/application/device.go +++ b/internal/core/metadata/application/device.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020-2022 IOTech Ltd +// Copyright (C) 2020-2023 IOTech Ltd // Copyright (C) 2022 Intel // // SPDX-License-Identifier: Apache-2.0 @@ -12,9 +12,6 @@ import ( goErrors "errors" "fmt" - "github.com/edgexfoundry/edgex-go/internal/core/metadata/container" - "github.com/edgexfoundry/edgex-go/internal/core/metadata/infrastructure/interfaces" - "github.com/edgexfoundry/edgex-go/internal/pkg/correlation" bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container" "github.com/edgexfoundry/go-mod-bootstrap/v3/di" "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" @@ -24,6 +21,10 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/v3/errors" "github.com/edgexfoundry/go-mod-core-contracts/v3/models" "github.com/edgexfoundry/go-mod-messaging/v3/pkg/types" + + "github.com/edgexfoundry/edgex-go/internal/core/metadata/container" + "github.com/edgexfoundry/edgex-go/internal/core/metadata/infrastructure/interfaces" + "github.com/edgexfoundry/edgex-go/internal/pkg/correlation" ) // The AddDevice function accepts the new device model from the controller function @@ -48,9 +49,6 @@ func AddDevice(d models.Device, ctx context.Context, dic *di.Container) (id stri correlation.FromContext(ctx), ) - device := dtos.FromDeviceModelToDTO(d) - go addDeviceCallback(ctx, dic, device) - go publishDeviceSystemEvent(common.DeviceSystemEventActionAdd, d.ServiceName, d, ctx, lc, dic) return addedDevice.Id, nil @@ -72,7 +70,6 @@ func DeleteDeviceByName(name string, ctx context.Context, dic *di.Container) err if err != nil { return errors.NewCommonEdgeXWrapper(err) } - go deleteDeviceCallback(ctx, dic, device) go publishDeviceSystemEvent(common.DeviceSystemEventActionDelete, device.ServiceName, device, ctx, lc, dic) @@ -146,11 +143,9 @@ func PatchDevice(dto dtos.UpdateDevice, ctx context.Context, dic *di.Container) ) if oldServiceName != "" { - go updateDeviceCallback(ctx, dic, oldServiceName, device) go publishDeviceSystemEvent(common.DeviceSystemEventActionUpdate, oldServiceName, device, ctx, lc, dic) } - go updateDeviceCallback(ctx, dic, device.ServiceName, device) go publishDeviceSystemEvent(common.DeviceSystemEventActionUpdate, device.ServiceName, device, ctx, lc, dic) return nil diff --git a/internal/core/metadata/application/notify.go b/internal/core/metadata/application/notify.go index 08e41d4fcc..27119dcad9 100644 --- a/internal/core/metadata/application/notify.go +++ b/internal/core/metadata/application/notify.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021-2022 IOTech Ltd +// Copyright (C) 2021-2023 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -7,29 +7,18 @@ package application import ( "context" - "fmt" "net/http" - metadataContainer "github.com/edgexfoundry/edgex-go/internal/core/metadata/container" - "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container" "github.com/edgexfoundry/go-mod-bootstrap/v3/di" - clients "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http" "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces" - "github.com/edgexfoundry/go-mod-core-contracts/v3/common" "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos" "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests" "github.com/edgexfoundry/go-mod-core-contracts/v3/errors" "github.com/edgexfoundry/go-mod-core-contracts/v3/models" ) -const ( - deviceCreateAction = "Device creation" - deviceUpdateAction = "Device update" - deviceRemoveAction = "Device removal" -) - func newDeviceServiceCallbackClient(ctx context.Context, dic *di.Container, deviceServiceName string) (interfaces.DeviceServiceCallbackClient, errors.EdgeX) { ds, err := DeviceServiceByName(deviceServiceName, ctx, dic) if err != nil { @@ -38,28 +27,6 @@ func newDeviceServiceCallbackClient(ctx context.Context, dic *di.Container, devi return clients.NewDeviceServiceCallbackClient(ds.BaseAddress), nil } -// addDeviceCallback invoke device service's callback function for adding new device -func addDeviceCallback(ctx context.Context, dic *di.Container, device dtos.Device) { - lc := container.LoggingClientFrom(dic.Get) - deviceServiceCallbackClient, err := newDeviceServiceCallbackClient(ctx, dic, device.ServiceName) - if err != nil { - lc.Errorf("fail to new a device service callback client by serviceName %s, err: %v", device.ServiceName, err) - return - } - - request := requests.NewAddDeviceRequest(device) - response, err := deviceServiceCallbackClient.AddDeviceCallback(ctx, request) - if err != nil { - lc.Errorf("fail to invoke device service callback for adding device %s, err: %v", device.Name, err) - return - } - if response.StatusCode != http.StatusOK { - lc.Errorf("fail to invoke device service callback for adding device %s, err: %s", device.Name, response.Message) - } - - go sendNotification(ctx, dic, device.Name, deviceCreateAction) -} - // validateDeviceCallback invoke device service's validation function for validating new or updated device func validateDeviceCallback(ctx context.Context, dic *di.Container, device dtos.Device) errors.EdgeX { lc := container.LoggingClientFrom(dic.Get) @@ -88,48 +55,6 @@ func validateDeviceCallback(ctx context.Context, dic *di.Container, device dtos. return nil } -// updateDeviceCallback invoke device service's callback function for updating device -func updateDeviceCallback(ctx context.Context, dic *di.Container, serviceName string, device models.Device) { - lc := container.LoggingClientFrom(dic.Get) - deviceServiceCallbackClient, err := newDeviceServiceCallbackClient(ctx, dic, serviceName) - if err != nil { - lc.Errorf("fail to new a device service callback client by serviceName %s, err: %v", serviceName, err) - return - } - - request := requests.NewUpdateDeviceRequest(dtos.FromDeviceModelToUpdateDTO(device)) - response, err := deviceServiceCallbackClient.UpdateDeviceCallback(ctx, request) - if err != nil { - lc.Errorf("fail to invoke device service callback for updating device %s, err: %v", device.Name, err) - return - } - if response.StatusCode != http.StatusOK { - lc.Errorf("fail to invoke device service callback for updating device %s, err: %s", device.Name, response.Message) - } - - go sendNotification(ctx, dic, device.Name, deviceUpdateAction) -} - -// deleteDeviceCallback invoke device service's callback function for deleting device -func deleteDeviceCallback(ctx context.Context, dic *di.Container, device models.Device) { - lc := container.LoggingClientFrom(dic.Get) - deviceServiceCallbackClient, err := newDeviceServiceCallbackClient(ctx, dic, device.ServiceName) - if err != nil { - lc.Errorf("fail to new a device service callback client by serviceName %s, err: %v", device.ServiceName, err) - return - } - response, err := deviceServiceCallbackClient.DeleteDeviceCallback(ctx, device.Name) - if err != nil { - lc.Errorf("fail to invoke device service callback for deleting device %s, err: %v", device.Name, err) - return - } - if response.StatusCode != http.StatusOK { - lc.Errorf("fail to invoke device service callback for deleting device %s, err: %s", device.Name, response.Message) - } - - go sendNotification(ctx, dic, device.Name, deviceRemoveAction) -} - // updateDeviceProfileCallback invoke device service's callback function for updating device profile func updateDeviceProfileCallback(ctx context.Context, dic *di.Container, deviceProfile dtos.DeviceProfile) { lc := container.LoggingClientFrom(dic.Get) @@ -242,32 +167,3 @@ func updateDeviceServiceCallback(ctx context.Context, dic *di.Container, ds mode lc.Errorf("fail to invoke device service callback for updating device service %s, err: %s", ds.Name, response.Message) } } - -// sendNotification sends a notification after adding or updating the metadata -func sendNotification(ctx context.Context, dic *di.Container, name string, action string) { - config := metadataContainer.ConfigurationFrom(dic.Get) - if !config.Notifications.PostDeviceChanges { - return - } - lc := container.LoggingClientFrom(dic.Get) - client := container.NotificationClientFrom(dic.Get) - - dto := dtos.Notification{ - Content: fmt.Sprintf("%s %s %s", config.Notifications.Content, name, action), - ContentType: common.ContentTypeText, - Description: config.Notifications.Description, - Labels: []string{config.Notifications.Label}, - Sender: config.Notifications.Sender, - Severity: models.Normal, - } - - req := requests.NewAddNotificationRequest(dto) - res, err := client.SendNotification(ctx, []requests.AddNotificationRequest{req}) - if err != nil { - lc.Warnf("fail to send the notification for %s, err: %v", name, err) - return - } - if len(res) > 0 && res[0].StatusCode > http.StatusMultiStatus { - lc.Errorf("fail to send the notification for %s, err: %v", name, res[0].Message) - } -}