Skip to content

Commit

Permalink
Merge pull request #4269 from hahattan/issue-4235
Browse files Browse the repository at this point in the history
feat: remove device callback calls
  • Loading branch information
cloudxxx8 authored Jan 30, 2023
2 parents a37026e + 36411bb commit 5b3503b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 115 deletions.
15 changes: 5 additions & 10 deletions internal/core/metadata/application/device.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
config2 "github.com/edgexfoundry/go-mod-bootstrap/v3/config"
"github.com/edgexfoundry/go-mod-bootstrap/v3/di"
Expand All @@ -25,6 +22,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
Expand All @@ -49,9 +50,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
Expand All @@ -73,7 +71,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)

Expand Down Expand Up @@ -147,11 +144,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
Expand Down
106 changes: 1 addition & 105 deletions internal/core/metadata/application/notify.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
//
// Copyright (C) 2021-2022 IOTech Ltd
// Copyright (C) 2021-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 5b3503b

Please sign in to comment.