2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
21 Abstract: mangos (NNG) Pipeline SBI implementation
29 "nanomsg.org/go/mangos/v2"
30 "nanomsg.org/go/mangos/v2/protocol/push"
31 _ "nanomsg.org/go/mangos/v2/transport/all"
36 func openNngPush(ip string) error {
40 func closeNngPush() error {
44 func createNngPushEndpointSocket(ep *rtmgr.Endpoint) error {
45 rtmgr.Logger.Debug("Invoked sbi.createNngPushEndpointSocket")
46 rtmgr.Logger.Debug("args: %v", (*ep))
47 s, err := push.NewSocket()
49 return errors.New("can't open push socket for endpoint: " + ep.Name +" due to:" + err.Error())
51 s.SetPipeEventHook(pipeEventHandler)
57 func destroyNngPushEndpointSocket(ep *rtmgr.Endpoint) error {
58 rtmgr.Logger.Debug("Invoked sbi.destroyNngPushEndpointSocket")
59 rtmgr.Logger.Debug("args: %v", (*ep))
60 if err:= ep.Socket.(mangos.Socket).Close(); err != nil {
61 return errors.New("can't close push socket of endpoint:" + ep.Uuid + " due to:" + err.Error())
66 func pipeEventHandler(event mangos.PipeEvent, pipe mangos.Pipe) {
67 for _, ep := range rtmgr.Eps {
68 uri := DEFAULT_NNG_PIPELINE_SOCKET_PREFIX + ep.Ip + ":" + strconv.Itoa(DEFAULT_NNG_PIPELINE_SOCKET_NUMBER)
69 if uri == pipe.Address() {
73 rtmgr.Logger.Debug("Endpoint " + uri + " successfully registered")
76 rtmgr.Logger.Debug("Endpoint " + uri + " has been deregistered")
83 NOTE: Asynchronous dial starts a goroutine which keep maintains the connection to the given endpoint
85 func dial(ep *rtmgr.Endpoint) {
86 rtmgr.Logger.Debug("Dialing to endpoint: " + ep.Uuid)
87 uri := DEFAULT_NNG_PIPELINE_SOCKET_PREFIX + ep.Ip + ":" + strconv.Itoa(DEFAULT_NNG_PIPELINE_SOCKET_NUMBER)
88 options := make(map[string]interface{})
89 options[mangos.OptionDialAsynch] = true
90 if err := ep.Socket.(mangos.Socket).DialOptions(uri, options); err != nil {
91 rtmgr.Logger.Error("can't dial on push socket to " + uri + " due to:" + err.Error())
95 func pushAll(policies *[]string) error {
96 rtmgr.Logger.Debug("Invoked: sbi.pushAll")
97 rtmgr.Logger.Debug("args: %v", (*policies))
98 for _, ep := range rtmgr.Eps {
100 go send(ep, policies)
102 rtmgr.Logger.Warn("Endpoint " + ep.Uuid + "is not ready")
108 func send(ep *rtmgr.Endpoint, policies *[]string) {
109 rtmgr.Logger.Debug("Invoked: sbi.pushAll")
110 rtmgr.Logger.Debug("Push policy to endpoint: "+ ep.Uuid)
111 for _, pe := range *policies {
112 if err := ep.Socket.(mangos.Socket).Send([]byte(pe)); err != nil {
113 rtmgr.Logger.Error("Unable to send policy entry due to: " + err.Error())
116 rtmgr.Logger.Info("NNG PUSH to ednpoint " + ep.Uuid + ": OK (# of Entries:" + strconv.Itoa(len((*policies))) + ")")