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) Pub/Sub SBI implementation
29 "nanomsg.org/go/mangos/v2/protocol/pub"
30 _ "nanomsg.org/go/mangos/v2/transport/all"
31 "routing-manager/pkg/rtmgr"
38 NewSocket CreateNewNngSocketHandler
41 func NewNngPub() *NngPub {
42 instance := new(NngPub)
43 instance.NewSocket = createNewPubSocket
47 func createNewPubSocket() (NngSocket, error) {
48 rtmgr.Logger.Debug("Invoked createNewPubSocket()")
49 s, err := pub.NewSocket()
51 return nil, errors.New("can't create new pub socket due to: " + err.Error())
56 func (c *NngPub) Initialize(ip string) error {
57 rtmgr.Logger.Debug("Invoked sbi.Initialize("+ ip +")")
59 c.socket, err = c.NewSocket()
61 return errors.New("create socket error due to: " + err.Error())
63 if err = c.listen(ip); err != nil {
64 return errors.New("can't listen on socket due to: " + err.Error())
69 func (c *NngPub) Terminate() error {
70 rtmgr.Logger.Debug("Invoked sbi.Terminate()")
71 return c.closeSocket()
74 func (c *NngPub) AddEndpoint(ep *rtmgr.Endpoint) error {
78 func (c *NngPub) DeleteEndpoint(ep *rtmgr.Endpoint) error {
82 func (c *NngPub) UpdateEndpoints(rcs *rtmgr.RicComponents) {
83 c.updateEndpoints(rcs, c)
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())
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())
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())
111 rtmgr.Logger.Info("NNG PUB: OK (# of Entries: " + strconv.Itoa(len((*policies))) + ")")