c48a40f96ccb52fbc0845d5c991061c56541f9b3
[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"
30         "nanomsg.org/go/mangos/v2/protocol/pub"
31         _ "nanomsg.org/go/mangos/v2/transport/all"
32         "rtmgr"
33         "strconv"
34 )
35
36 var socket mangos.Socket
37
38 func createNngPubEndpointSocket(ep *rtmgr.Endpoint) error {
39         return nil
40 }
41
42 func destroyNngPubEndpointSocket(ep *rtmgr.Endpoint) error {
43         return nil
44 }
45
46 /*
47 Creates the NNG publication channel
48 */
49 func openNngPub(ip string) error {
50         var err error
51         if socket, err = pub.NewSocket(); err != nil {
52                 return errors.New("can't get new pub socket due to:" + err.Error())
53         }
54         uri := DEFAULT_NNG_PUBSUB_SOCKET_PREFIX + ip + ":" + strconv.Itoa(DEFAULT_NNG_PUBSUB_SOCKET_NUMBER)
55         rtmgr.Logger.Info("publishing on: " + uri)
56         if err = socket.Listen(uri); err != nil {
57                 return errors.New("can't publish on socket " + uri + " due to:" + err.Error())
58         }
59         return nil
60 }
61
62 func closeNngPub() error {
63         if err := socket.Close(); err != nil {
64                 return errors.New("can't close socket due to:" + err.Error())
65         }
66         return nil
67 }
68
69 func publishAll(policies *[]string) error {
70         for _, pe := range *policies {
71                 if err := socket.Send([]byte(pe)); err != nil {
72                         return errors.New("Unable to send policy entry due to: " + err.Error())
73                 }
74         }
75         rtmgr.Logger.Info("NNG PUB: OK (# of Entries:" + strconv.Itoa(len((*policies))) + ")")
76         return nil
77 }