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: Implement Device Profile System Events #4336

Merged
merged 1 commit into from
Feb 8, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require (
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.17
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.2
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.9
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.10
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.7
github.com/edgexfoundry/go-mod-secrets/v3 v3.0.0-dev.5
github.com/fxamacker/cbor/v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.17 h1:WAbuyikabO+RGh+4HMm
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.17/go.mod h1:saCEXtGUBKvNfMHrnrZyqZYMzdrETOjo5mq0TxCtCRY=
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.2 h1:xp5MsP+qf/fuJxy8fT7k1N+c4j4C6w04qMCBXm6id7o=
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.2/go.mod h1:1Vv4uWAo6r7k6jUlqVJW8JOL6YKVBc6sRL8Al3DrMck=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.9 h1:ejafyDHaVCdfKW4IQZeg4n1mBI2JkC1Y1XXelyyj+z8=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.9/go.mod h1:4lpZUM54ZareGU/yuAJvLEw0BoJ43SvCj1LO+gsKm9c=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.10 h1:o5yenvmLn8+0AOz0d5GIek011Tt5ZRbvPlgE4VhozEU=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.10/go.mod h1:4lpZUM54ZareGU/yuAJvLEw0BoJ43SvCj1LO+gsKm9c=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.7 h1:jgDQA/7SENURXQkIX11pNgA/pX9IK9ZULenj/vF17Vw=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.7/go.mod h1:r6Klfz+QBDx1Z5UV0z70MKdK2/cgHwhtqTm2HFXoWug=
github.com/edgexfoundry/go-mod-registry/v3 v3.0.0-dev.3 h1:QgZF9f70Cwpvkjw3tP1aiVGHc+yNFJNzW6hO8pDs3fg=
Expand Down
55 changes: 8 additions & 47 deletions internal/core/metadata/application/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ package application

import (
"context"
"encoding/json"
goErrors "errors"
"fmt"

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"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"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"
"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"
Expand Down Expand Up @@ -50,15 +46,14 @@ func AddDevice(d models.Device, ctx context.Context, dic *di.Container) (id stri
correlation.FromContext(ctx),
)

go publishDeviceSystemEvent(common.DeviceSystemEventActionAdd, d.ServiceName, d, ctx, lc, dic)
deviceDTO := dtos.FromDeviceModelToDTO(addedDevice)
go publishSystemEvent(common.DeviceSystemEventType, common.SystemEventActionAdd, d.ServiceName, deviceDTO, ctx, dic)

return addedDevice.Id, nil
}

// DeleteDeviceByName deletes the device by name
func DeleteDeviceByName(name string, ctx context.Context, dic *di.Container) errors.EdgeX {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)

if name == "" {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "name is empty", nil)
}
Expand All @@ -72,7 +67,8 @@ func DeleteDeviceByName(name string, ctx context.Context, dic *di.Container) err
return errors.NewCommonEdgeXWrapper(err)
}

go publishDeviceSystemEvent(common.DeviceSystemEventActionDelete, device.ServiceName, device, ctx, lc, dic)
deviceDTO := dtos.FromDeviceModelToDTO(device)
go publishSystemEvent(common.DeviceSystemEventType, common.SystemEventActionDelete, device.ServiceName, deviceDTO, ctx, dic)

return nil
}
Expand Down Expand Up @@ -128,7 +124,8 @@ func PatchDevice(dto dtos.UpdateDevice, ctx context.Context, dic *di.Container)

requests.ReplaceDeviceModelFieldsWithDTO(&device, dto)

err = validateDeviceCallback(ctx, dic, dtos.FromDeviceModelToDTO(device))
deviceDTO := dtos.FromDeviceModelToDTO(device)
err = validateDeviceCallback(ctx, dic, deviceDTO)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
Expand All @@ -144,10 +141,10 @@ func PatchDevice(dto dtos.UpdateDevice, ctx context.Context, dic *di.Container)
)

if oldServiceName != "" {
go publishDeviceSystemEvent(common.DeviceSystemEventActionUpdate, oldServiceName, device, ctx, lc, dic)
go publishSystemEvent(common.DeviceSystemEventType, common.SystemEventActionUpdate, oldServiceName, deviceDTO, ctx, dic)
}

go publishDeviceSystemEvent(common.DeviceSystemEventActionUpdate, device.ServiceName, device, ctx, lc, dic)
go publishSystemEvent(common.DeviceSystemEventType, common.SystemEventActionUpdate, device.ServiceName, deviceDTO, ctx, dic)

return nil
}
Expand Down Expand Up @@ -223,39 +220,3 @@ func DevicesByProfileName(offset int, limit int, profileName string, dic *di.Con
}

var noMessagingClientError = goErrors.New("MessageBus Client not available. Please update RequireMessageBus and MessageBus configuration to enable sending System Events via the EdgeX MessageBus")

func publishDeviceSystemEvent(action string, owner string, d models.Device, ctx context.Context, lc logger.LoggingClient, dic *di.Container) {
device := dtos.FromDeviceModelToDTO(d)
systemEvent := dtos.NewSystemEvent(common.DeviceSystemEventType, action, common.CoreMetaDataServiceKey, owner, nil, device)

messagingClient := bootstrapContainer.MessagingClientFrom(dic.Get)
if messagingClient == nil {
lc.Errorf("unable to publish Device System Event: %v", noMessagingClientError)
return
}

config := container.ConfigurationFrom(dic.Get)

prefix := config.MessageBus.Topics[config2.MessageBusPublishTopicPrefix]
publishTopic := fmt.Sprintf("%s/%s/%s/%s/%s/%s",
prefix,
systemEvent.Source,
systemEvent.Type,
systemEvent.Action,
systemEvent.Owner,
device.ProfileName)

payload, _ := json.Marshal(systemEvent)
envelope := types.NewMessageEnvelope(payload, ctx)
// Correlation ID and Content type are set by the above factory function from the context of the request that
// triggered this System Event. We'll keep that Correlation ID, but need to make sure the Content Type is set appropriate
// for how the payload was encoded above.
envelope.ContentType = common.ContentTypeJSON

if err := messagingClient.Publish(envelope, publishTopic); err != nil {
lc.Errorf("unable to publish '%s' Device System Event for device '%s' to topic '%s': %v", action, device.Name, publishTopic, err)
return
}

lc.Debugf("Published the '%s' Device System Event for device '%s' to topic '%s'", action, device.Name, publishTopic)
}
7 changes: 6 additions & 1 deletion internal/core/metadata/application/devicecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package application

import (
"context"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"

"github.com/edgexfoundry/edgex-go/internal/core/metadata/container"
"github.com/edgexfoundry/edgex-go/internal/pkg/correlation"
Expand Down Expand Up @@ -43,6 +44,7 @@ func AddDeviceProfileDeviceCommand(profileName string, deviceCommand models.Devi
}

lc.Debugf("DeviceProfile deviceCommands added on DB successfully. Correlation-id: %s ", correlation.FromContext(ctx))
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}
Expand Down Expand Up @@ -76,11 +78,13 @@ func PatchDeviceProfileDeviceCommand(profileName string, dto dtos.UpdateDeviceCo
}

lc.Debugf("DeviceProfile deviceCommands patched on DB successfully. Correlation-id: %s ", correlation.FromContext(ctx))
profileDTO := dtos.FromDeviceProfileModelToDTO(profile)
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}

func DeleteDeviceCommandByName(profileName string, commandName string, dic *di.Container) errors.EdgeX {
func DeleteDeviceCommandByName(profileName string, commandName string, ctx context.Context, dic *di.Container) errors.EdgeX {
if profileName == "" {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "profile name is empty", nil)
}
Expand Down Expand Up @@ -130,5 +134,6 @@ func DeleteDeviceCommandByName(profileName string, commandName string, dic *di.C
return errors.NewCommonEdgeXWrapper(err)
}

go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)
return nil
}
30 changes: 26 additions & 4 deletions internal/core/metadata/application/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"context"
"fmt"

"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests"

"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/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"
)
Expand Down Expand Up @@ -46,6 +46,9 @@ func AddDeviceProfile(d models.DeviceProfile, ctx context.Context, dic *di.Conta
correlationId,
)

profileDTO := dtos.FromDeviceProfileModelToDTO(addedDeviceProfile)
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionAdd, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return addedDeviceProfile.Id, nil
}

Expand All @@ -69,7 +72,15 @@ func UpdateDeviceProfile(d models.DeviceProfile, ctx context.Context, dic *di.Co
"DeviceProfile updated on DB successfully. Correlation-id: %s ",
correlation.FromContext(ctx),
)
go updateDeviceProfileCallback(ctx, dic, dtos.FromDeviceProfileModelToDTO(d))

profile, err := dbClient.DeviceProfileByName(d.Name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}

profileDTO := dtos.FromDeviceProfileModelToDTO(profile)
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}

Expand Down Expand Up @@ -97,10 +108,18 @@ func DeleteDeviceProfileByName(name string, ctx context.Context, dic *di.Contain
return errors.NewCommonEdgeX(errors.KindContractInvalid, "name is empty", nil)
}
dbClient := container.DBClientFrom(dic.Get)
err := dbClient.DeleteDeviceProfileByName(name)
profile, err := dbClient.DeviceProfileByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
err = dbClient.DeleteDeviceProfileByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}

profileDTO := dtos.FromDeviceProfileModelToDTO(profile)
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionDelete, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}

Expand Down Expand Up @@ -201,6 +220,9 @@ func PatchDeviceProfileBasicInfo(ctx context.Context, dto dtos.UpdateDeviceProfi
correlation.FromContext(ctx),
)

profileDTO := dtos.FromDeviceProfileModelToDTO(deviceProfile)
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}

Expand Down
7 changes: 6 additions & 1 deletion internal/core/metadata/application/deviceresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

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/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"
Expand Down Expand Up @@ -79,6 +80,7 @@ func AddDeviceProfileResource(profileName string, resource models.DeviceResource
}

lc.Debugf("DeviceProfile deviceResources added on DB successfully. Correlation-id: %s ", correlation.FromContext(ctx))
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}
Expand Down Expand Up @@ -112,11 +114,13 @@ func PatchDeviceProfileResource(profileName string, dto dtos.UpdateDeviceResourc
}

lc.Debugf("DeviceProfile deviceResources patched on DB successfully. Correlation-id: %s ", correlation.FromContext(ctx))
profileDTO := dtos.FromDeviceProfileModelToDTO(profile)
go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)

return nil
}

func DeleteDeviceResourceByName(profileName string, resourceName string, dic *di.Container) errors.EdgeX {
func DeleteDeviceResourceByName(profileName string, resourceName string, ctx context.Context, dic *di.Container) errors.EdgeX {
if profileName == "" {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "profile name is empty", nil)
}
Expand Down Expand Up @@ -166,6 +170,7 @@ func DeleteDeviceResourceByName(profileName string, resourceName string, dic *di
return errors.NewCommonEdgeXWrapper(err)
}

go publishSystemEvent(common.DeviceProfileSystemEventType, common.SystemEventActionUpdate, common.CoreMetaDataServiceKey, profileDTO, ctx, dic)
return nil
}

Expand Down
Loading