-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): Enable Custom Trigger Registration
accept registration of factory functions for making custom trigger types available to the SDK Signed-off-by: Alex Ullrich <alexullrich@technotects.com>
- Loading branch information
Alex Ullrich
committed
Dec 12, 2020
1 parent
c9ed7d5
commit f81c328
Showing
4 changed files
with
319 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
// limitations under the License. | ||
// | ||
|
||
package trigger | ||
package appsdk | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// | ||
// Copyright (c) 2020 Technotects | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package appsdk | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/edgexfoundry/app-functions-sdk-go/appcontext" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/common" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/runtime" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/security" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/trigger/http" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/trigger/messagebus" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/trigger/mqtt" | ||
"github.com/edgexfoundry/go-mod-core-contracts/clients/command" | ||
"github.com/edgexfoundry/go-mod-core-contracts/clients/coredata" | ||
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger" | ||
"github.com/edgexfoundry/go-mod-core-contracts/clients/notifications" | ||
"github.com/edgexfoundry/go-mod-messaging/pkg/types" | ||
"strings" | ||
) | ||
|
||
// ProcessMessageFunc provides an interface that can be used by custom triggers to invoke the runtime | ||
type ProcessMessageFunc func(ctx *appcontext.Context, envelope types.MessageEnvelope) error | ||
|
||
// TriggerContextSeed provides information needed from the SDK to establish message context for pipeline | ||
type TriggerContextSeed struct { | ||
Configuration *common.ConfigurationStruct | ||
LoggingClient logger.LoggingClient | ||
EventClient coredata.EventClient | ||
ValueDescriptorClient coredata.ValueDescriptorClient | ||
CommandClient command.CommandClient | ||
NotificationsClient notifications.NotificationsClient | ||
SecretsProvider security.SecretProvider | ||
} | ||
|
||
// RegisterCustomTrigger allows users to register builders for custom trigger types | ||
func (s *AppFunctionsSDK) RegisterCustomTrigger(name string, | ||
builder func(TriggerContextSeed, ProcessMessageFunc) (Trigger, error)) error { | ||
nu := strings.ToUpper(name) | ||
|
||
if nu == bindingTypeEdgeXMessageBus || | ||
nu == bindingTypeMessageBus || | ||
nu == bindingTypeHTTP || | ||
nu == bindingTypeMQTT { | ||
return errors.New(fmt.Sprintf("cannot register custom trigger for builtin type (%s)", name)) | ||
} | ||
|
||
if s.customTriggerBuilders == nil { | ||
s.customTriggerBuilders = make(map[string]func(sdk *AppFunctionsSDK) (Trigger, error), 1) | ||
} | ||
|
||
s.customTriggerBuilders[nu] = func(sdk *AppFunctionsSDK) (Trigger, error) { | ||
proc := func(edgexcontext *appcontext.Context, envelope types.MessageEnvelope) error { | ||
messageError := sdk.runtime.ProcessMessage(edgexcontext, envelope) | ||
|
||
if messageError != nil { | ||
// ProcessMessage logs the error, so no need to log it here. | ||
return messageError.Err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return builder(TriggerContextSeed{ | ||
Configuration: sdk.config, | ||
LoggingClient: sdk.EdgexClients.LoggingClient, | ||
EventClient: sdk.EdgexClients.EventClient, | ||
ValueDescriptorClient: sdk.EdgexClients.ValueDescriptorClient, | ||
CommandClient: sdk.EdgexClients.CommandClient, | ||
NotificationsClient: sdk.EdgexClients.NotificationsClient, | ||
SecretsProvider: sdk.secretProvider, | ||
}, proc) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// setupTrigger configures the appropriate trigger as specified by configuration. | ||
func (sdk *AppFunctionsSDK) setupTrigger(configuration *common.ConfigurationStruct, runtime *runtime.GolangRuntime) Trigger { | ||
var t Trigger | ||
// Need to make dynamic, search for the binding that is input | ||
|
||
switch triggerType := strings.ToUpper(configuration.Binding.Type); triggerType { | ||
case bindingTypeHTTP: | ||
sdk.LoggingClient.Info("HTTP trigger selected") | ||
t = &http.Trigger{Configuration: configuration, Runtime: runtime, Webserver: sdk.webserver, EdgeXClients: sdk.EdgexClients} | ||
|
||
case bindingTypeMessageBus, | ||
bindingTypeEdgeXMessageBus: // Allows for more explicit name now that we have plain MQTT option also | ||
sdk.LoggingClient.Info("EdgeX MessageBus trigger selected") | ||
t = &messagebus.Trigger{Configuration: configuration, Runtime: runtime, EdgeXClients: sdk.EdgexClients} | ||
|
||
case bindingTypeMQTT: | ||
sdk.LoggingClient.Info("External MQTT trigger selected") | ||
t = mqtt.NewTrigger(configuration, runtime, sdk.EdgexClients, sdk.secretProvider) | ||
|
||
default: | ||
if builder, found := sdk.customTriggerBuilders[triggerType]; found { | ||
var err error | ||
t, err = builder(sdk) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to initialize custom trigger [%s]", triggerType)) | ||
} | ||
} else { | ||
sdk.LoggingClient.Error(fmt.Sprintf("Invalid Trigger type of '%s' specified", configuration.Binding.Type)) | ||
} | ||
} | ||
|
||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// | ||
// Copyright (c) 2020 Technotects | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package appsdk | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/common" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/trigger/http" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/trigger/messagebus" | ||
"github.com/edgexfoundry/app-functions-sdk-go/internal/trigger/mqtt" | ||
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap" | ||
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger" | ||
"github.com/edgexfoundry/go-mod-messaging/pkg/types" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"strings" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestRegisterCustomTrigger_HTTP(t *testing.T) { | ||
name := strings.ToTitle(bindingTypeHTTP) | ||
|
||
sdk := AppFunctionsSDK{} | ||
err := sdk.RegisterCustomTrigger(name, nil) | ||
|
||
require.NotNil(t, err, "should throw error") | ||
require.NotNil(t, err.Error(), fmt.Sprintf("cannot register custom trigger for builtin type (%s)", name)) | ||
require.Zero(t, len(sdk.customTriggerBuilders), "nothing should be registered") | ||
} | ||
|
||
func TestRegisterCustomTrigger_MessageBus(t *testing.T) { | ||
name := strings.ToTitle(bindingTypeMessageBus) | ||
|
||
sdk := AppFunctionsSDK{} | ||
err := sdk.RegisterCustomTrigger(name, nil) | ||
|
||
require.NotNil(t, err, "should throw error") | ||
require.NotNil(t, err.Error(), fmt.Sprintf("cannot register custom trigger for builtin type (%s)", name)) | ||
require.Zero(t, len(sdk.customTriggerBuilders), "nothing should be registered") | ||
} | ||
|
||
func TestRegisterCustomTrigger_EdgeXMessageBus(t *testing.T) { | ||
name := strings.ToTitle(bindingTypeEdgeXMessageBus) | ||
|
||
sdk := AppFunctionsSDK{} | ||
err := sdk.RegisterCustomTrigger(name, nil) | ||
|
||
require.NotNil(t, err, "should throw error") | ||
require.NotNil(t, err.Error(), fmt.Sprintf("cannot register custom trigger for builtin type (%s)", name)) | ||
require.Zero(t, len(sdk.customTriggerBuilders), "nothing should be registered") | ||
} | ||
|
||
func TestRegisterCustomTrigger_MQTT(t *testing.T) { | ||
name := strings.ToTitle(bindingTypeMQTT) | ||
|
||
sdk := AppFunctionsSDK{} | ||
err := sdk.RegisterCustomTrigger(name, nil) | ||
|
||
require.NotNil(t, err, "should throw error") | ||
require.NotNil(t, err.Error(), fmt.Sprintf("cannot register custom trigger for builtin type (%s)", name)) | ||
require.Zero(t, len(sdk.customTriggerBuilders), "nothing should be registered") | ||
} | ||
|
||
func TestRegisterCustomTrigger(t *testing.T) { | ||
name := "cUsToM tRiGgEr" | ||
trig := mockCustomTrigger{} | ||
|
||
builder := func(s TriggerContextSeed, f ProcessMessageFunc) (Trigger, error) { | ||
return &trig, nil | ||
} | ||
sdk := AppFunctionsSDK{} | ||
err := sdk.RegisterCustomTrigger(name, builder) | ||
|
||
require.Nil(t, err, "should not throw error") | ||
require.Equal(t, len(sdk.customTriggerBuilders), 1, "provided function should be registered") | ||
|
||
registeredBuilder := sdk.customTriggerBuilders[strings.ToUpper(name)] | ||
require.NotNil(t, registeredBuilder, "provided function should be registered with uppercase name") | ||
|
||
res, err := registeredBuilder(&sdk) | ||
require.NoError(t, err) | ||
require.Equal(t, res, &trig, "will be wrapped but should ultimately return result of builder") | ||
} | ||
|
||
func TestSetupTrigger_HTTP(t *testing.T) { | ||
sdk := AppFunctionsSDK{ | ||
config: &common.ConfigurationStruct{ | ||
Binding: common.BindingInfo{ | ||
Type: "http", | ||
}, | ||
}, | ||
LoggingClient: logger.MockLogger{}, | ||
} | ||
|
||
trigger := sdk.setupTrigger(sdk.config, sdk.runtime) | ||
|
||
require.NotNil(t, trigger, "should be defined") | ||
require.IsType(t, &http.Trigger{}, trigger, "should be an http trigger") | ||
} | ||
|
||
func TestSetupTrigger_MessageBus(t *testing.T) { | ||
sdk := AppFunctionsSDK{ | ||
config: &common.ConfigurationStruct{ | ||
Binding: common.BindingInfo{ | ||
Type: "messagebus", | ||
}, | ||
}, | ||
LoggingClient: logger.MockLogger{}, | ||
} | ||
|
||
trigger := sdk.setupTrigger(sdk.config, sdk.runtime) | ||
|
||
require.NotNil(t, trigger, "should be defined") | ||
require.IsType(t, &messagebus.Trigger{}, trigger, "should be a messagebus trigger") | ||
} | ||
|
||
func TestSetupTrigger_EdgeXMessageBus(t *testing.T) { | ||
sdk := AppFunctionsSDK{ | ||
config: &common.ConfigurationStruct{ | ||
Binding: common.BindingInfo{ | ||
Type: "edgex-messagebus", | ||
}, | ||
}, | ||
LoggingClient: logger.MockLogger{}, | ||
} | ||
|
||
trigger := sdk.setupTrigger(sdk.config, sdk.runtime) | ||
|
||
require.NotNil(t, trigger, "should be defined") | ||
require.IsType(t, &messagebus.Trigger{}, trigger, "should be a messagebus trigger") | ||
} | ||
|
||
func TestSetupTrigger_MQTT(t *testing.T) { | ||
sdk := AppFunctionsSDK{ | ||
config: &common.ConfigurationStruct{ | ||
Binding: common.BindingInfo{ | ||
Type: "external-mqtt", | ||
}, | ||
}, | ||
LoggingClient: logger.MockLogger{}, | ||
} | ||
|
||
trigger := sdk.setupTrigger(sdk.config, sdk.runtime) | ||
|
||
require.NotNil(t, trigger, "should be defined") | ||
require.IsType(t, &mqtt.Trigger{}, trigger, "should be an MQTT trigger") | ||
} | ||
|
||
type mockCustomTrigger struct { | ||
} | ||
|
||
func (*mockCustomTrigger) Initialize(wg *sync.WaitGroup, ctx context.Context, background <-chan types.MessageEnvelope) (bootstrap.Deferred, error) { | ||
return nil, nil | ||
} | ||
|
||
func TestSetupTrigger_CustomType(t *testing.T) { | ||
triggerName := uuid.New().String() | ||
|
||
sdk := AppFunctionsSDK{ | ||
config: &common.ConfigurationStruct{ | ||
Binding: common.BindingInfo{ | ||
Type: triggerName, | ||
}, | ||
}, | ||
LoggingClient: logger.MockLogger{}, | ||
} | ||
|
||
sdk.RegisterCustomTrigger(triggerName, func(s TriggerContextSeed, f ProcessMessageFunc) (Trigger, error) { | ||
return &mockCustomTrigger{}, nil | ||
}) | ||
|
||
trigger := sdk.setupTrigger(sdk.config, sdk.runtime) | ||
|
||
require.NotNil(t, trigger, "should be defined") | ||
require.IsType(t, &mockCustomTrigger{}, trigger, "should be a custom trigger") | ||
} |