01bde0ce1ff99302693e7a1d673349f4d16b09f6
[ric-plt/rtmgr.git] / pkg / sbi / nngpush.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19 /*
20   Mnemonic:     nngpipe.go
21   Abstract: mangos (NNG) Pipeline SBI implementation
22   Date:         12 March 2019
23 */
24
25 package sbi
26
27 import (
28         "errors"
29         "nanomsg.org/go/mangos/v2"
30         "nanomsg.org/go/mangos/v2/protocol/push"
31         _ "nanomsg.org/go/mangos/v2/transport/all"
32         "rtmgr"
33         "strconv"
34 )
35
36 func openNngPush(ip string) error {
37         return nil
38 }
39
40 func closeNngPush() error {
41         return nil
42 }
43
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()
48         if err != nil {
49                 return errors.New("can't open push socket for endpoint: " + ep.Name +" due to:" + err.Error())
50         }
51         s.SetPipeEventHook(pipeEventHandler)
52         ep.Socket = s
53         dial(ep)
54         return nil
55 }
56
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())
62                 }
63         return nil
64 }
65
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() {
70                         switch event {
71                         case 1:
72                                 ep.IsReady = true
73                                 rtmgr.Logger.Debug("Endpoint " + uri + " successfully registered")
74                         default:
75                                 ep.IsReady = false
76                                 rtmgr.Logger.Debug("Endpoint " + uri + " has been deregistered")
77                         }
78                 }       
79         }
80 }
81
82 /*
83 NOTE: Asynchronous dial starts a goroutine which keep maintains the connection to the given endpoint
84 */
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())
92         }
93 }
94
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 {
99                 if ep.IsReady {
100                         go send(ep, policies)
101                 } else {
102                         rtmgr.Logger.Warn("Endpoint " + ep.Uuid + "is not ready")
103                 }
104         }
105         return nil
106 }
107
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())
114                 }
115         }
116         rtmgr.Logger.Info("NNG PUSH to ednpoint " + ep.Uuid + ": OK (# of Entries:" + strconv.Itoa(len((*policies))) + ")")
117 }