02e4ebfd2fda352cc364b9978dfad7644a7a8ad4
[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 sock mangos.Socket
37
38 /*
39 Creates the NNG publication channel
40 */
41 func openNngPub(url string) error {
42         var err error
43         if sock, err = pub.NewSocket(); err != nil {
44                 return errors.New("can't get new pub socket due to:" + err.Error())
45         }
46         rtmgr.Logger.Info("publishing on: " + url)
47         if err = sock.Listen(url); err != nil {
48                 return errors.New("can't publish on socket " + url + " due to:" + err.Error())
49         }
50         return nil
51 }
52
53 func closeNngPub() error {
54         if err := sock.Close(); err != nil {
55                 return errors.New("can't close socket due to:" + err.Error())
56         }
57         return nil
58 }
59
60 func publishAll(policies *[]string) error {
61         for _, pe := range *policies {
62                 if err := sock.Send([]byte(pe)); err != nil {
63                         return errors.New("Unable to send policy entry due to: " + err.Error())
64                 }
65         }
66         rtmgr.Logger.Info("NNG PUB: OK (# of Entries:" + strconv.Itoa(len((*policies))) + ")")
67         return nil
68 }