Routing manager v0.3.3
[ric-plt/rtmgr.git] / pkg / sbi / nngpub.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:     NngPub.go
21   Abstract:     mangos (NNG) Pub/Sub SBI implementation
22   Date:         12 March 2019
23 */
24
25 package sbi
26
27 import (
28         "errors"
29         "nanomsg.org/go/mangos/v2/protocol/pub"
30         _ "nanomsg.org/go/mangos/v2/transport/all"
31         "routing-manager/pkg/rtmgr"
32         "strconv"
33 )
34
35 type NngPub struct {
36         Sbi
37         socket NngSocket
38         NewSocket CreateNewNngSocketHandler
39 }
40
41 func NewNngPub() *NngPub {
42         instance := new(NngPub)
43         instance.NewSocket = createNewPubSocket
44         return instance
45 }
46
47 func createNewPubSocket() (NngSocket, error) {
48         rtmgr.Logger.Debug("Invoked createNewPubSocket()")
49         s, err := pub.NewSocket()
50         if err != nil {
51                 return nil, errors.New("can't create new pub socket due to: " + err.Error())
52         }
53         return s, nil
54 }
55
56 func (c *NngPub) Initialize(ip string) error {
57         rtmgr.Logger.Debug("Invoked sbi.Initialize("+ ip +")")
58         var err error
59         c.socket, err = c.NewSocket()
60         if err != nil {
61                 return errors.New("create socket error due to: " + err.Error())
62         }
63         if err = c.listen(ip); err != nil {
64                 return errors.New("can't listen on socket due to: " + err.Error())
65         }
66         return nil
67 }
68
69 func (c *NngPub) Terminate() error {
70         rtmgr.Logger.Debug("Invoked sbi.Terminate()")
71         return c.closeSocket()
72 }
73
74 func (c *NngPub) AddEndpoint(ep *rtmgr.Endpoint) error {
75         return nil
76 }
77
78 func (c *NngPub) DeleteEndpoint(ep *rtmgr.Endpoint) error {
79         return nil
80 }
81
82 func (c *NngPub) UpdateEndpoints(rcs *rtmgr.RicComponents) {
83         c.updateEndpoints(rcs, c)
84 }
85
86 func (c *NngPub) listen(ip string) error {
87         rtmgr.Logger.Debug("Start listening on: " + ip)
88         uri := DEFAULT_NNG_PUBSUB_SOCKET_PREFIX + ip + ":" + strconv.Itoa(DEFAULT_NNG_PUBSUB_SOCKET_NUMBER)
89         rtmgr.Logger.Info("publishing on: " + uri)
90         if err := c.socket.(NngSocket).Listen(uri); err != nil {
91                 return errors.New("can't publish on socket " + uri + " due to: " + err.Error())
92         }
93         return nil
94 }
95
96 func (c *NngPub) closeSocket() error {
97         rtmgr.Logger.Debug("Close NngPub Socket")
98         if err := c.socket.(NngSocket).Close(); err != nil {
99                 return errors.New("can't close socket due to: " + err.Error())
100         }
101         return nil
102 }
103
104 func (c *NngPub) DistributeAll(policies *[]string) error {
105         rtmgr.Logger.Debug("Invoked: sbi.DistributeAll(), args: %v",(*policies))
106         for _, pe := range *policies {
107                 if err := c.socket.(NngSocket).Send([]byte(pe)); err != nil {
108                         return errors.New("Unable to send policy entry due to: " + err.Error())
109                 }
110         }
111         rtmgr.Logger.Info("NNG PUB: OK (# of Entries: " + strconv.Itoa(len((*policies))) + ")")
112         return nil
113 }