Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove device callback calls #4269

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
"github.com/edgexfoundry/go-mod-bootstrap/v3/di"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

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