Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 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. |
wahidw | 761934a | 2019-11-27 06:07:26 +0000 | [diff] [blame] | 17 | |
| 18 | This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | platform project (RICP). |
| 20 | |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 21 | ================================================================================== |
| 22 | */ |
| 23 | /* |
| 24 | Mnemonic: nngpipe.go |
| 25 | Abstract: mangos (NNG) Pipeline SBI implementation |
| 26 | Date: 12 March 2019 |
| 27 | */ |
| 28 | |
| 29 | package sbi |
| 30 | |
| 31 | import ( |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 32 | "bytes" |
| 33 | "crypto/md5" |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 34 | "errors" |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 35 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
prabhukaliswamy | b47d12d | 2019-12-03 15:06:30 +0000 | [diff] [blame] | 36 | "routing-manager/pkg/rtmgr" |
| 37 | "strconv" |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 38 | //"time" |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 39 | "fmt" |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 40 | ) |
| 41 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 42 | type NngPush struct { |
| 43 | Sbi |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 44 | rcChan chan *xapp.RMRParams |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 45 | } |
| 46 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 47 | type RMRParams struct { |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 48 | *xapp.RMRParams |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 49 | } |
| 50 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 51 | func (params *RMRParams) String() string { |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 52 | var b bytes.Buffer |
| 53 | sum := md5.Sum(params.Payload) |
| 54 | fmt.Fprintf(&b, "params(Src=%s Mtype=%d SubId=%d Xid=%s Meid=%s Paylens=%d/%d Payhash=%x)", params.Src, params.Mtype, params.SubId, params.Xid, params.Meid.RanName, params.PayloadLen, len(params.Payload), sum) |
| 55 | return b.String() |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 56 | } |
| 57 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 58 | func NewNngPush() *NngPush { |
| 59 | instance := new(NngPush) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 60 | return instance |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 61 | } |
| 62 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 63 | func (c *NngPush) Initialize(ip string) error { |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func (c *NngPush) Terminate() error { |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | func (c *NngPush) AddEndpoint(ep *rtmgr.Endpoint) error { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 72 | |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 73 | xapp.Logger.Debug("Invoked sbi.AddEndpoint") |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 74 | endpoint := ep.Ip + ":" + strconv.Itoa(DefaultNngPipelineSocketNumber) |
| 75 | ep.Whid = int(xapp.Rmr.Openwh(endpoint)) |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 76 | if ep.Whid < 0 { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 77 | return errors.New("can't open warmhole connection for endpoint:" + ep.Uuid + " due to invalid Wormhole ID: " + string(ep.Whid)) |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 78 | } else { |
| 79 | xapp.Logger.Debug("Wormhole ID is %v and EP is %v", ep.Whid, endpoint) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 80 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 81 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func (c *NngPush) DeleteEndpoint(ep *rtmgr.Endpoint) error { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 86 | xapp.Logger.Debug("Invoked sbi. DeleteEndpoint") |
| 87 | xapp.Logger.Debug("args: %v", *ep) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 88 | |
| 89 | xapp.Rmr.Closewh(ep.Whid) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 90 | return nil |
| 91 | } |
| 92 | |
| 93 | func (c *NngPush) UpdateEndpoints(rcs *rtmgr.RicComponents) { |
| 94 | c.updateEndpoints(rcs, c) |
| 95 | } |
| 96 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 97 | func (c *NngPush) DistributeAll(policies *[]string) error { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 98 | xapp.Logger.Debug("Invoked: sbi.DistributeAll") |
| 99 | xapp.Logger.Debug("args: %v", *policies) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 100 | |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 101 | for _, ep := range rtmgr.Eps { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 102 | go c.send(ep, policies) |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 103 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 104 | |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 105 | return nil |
| 106 | } |
| 107 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 108 | func (c *NngPush) send(ep *rtmgr.Endpoint, policies *[]string) { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 109 | xapp.Logger.Debug("Push policy to endpoint: " + ep.Uuid) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 110 | |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 111 | var policy = []byte{} |
| 112 | cumulative_policy := 0 |
| 113 | count := 0 |
| 114 | maxrecord := xapp.Config.GetInt("maxrecord") |
| 115 | if maxrecord == 0 { |
| 116 | maxrecord = 10 |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 117 | } |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 118 | |
| 119 | for _, pe := range *policies { |
| 120 | b := []byte(pe) |
| 121 | for j := 0; j < len(b); j++ { |
| 122 | policy = append(policy, b[j]) |
| 123 | } |
| 124 | count++ |
| 125 | cumulative_policy++ |
| 126 | if count == maxrecord || cumulative_policy == len(*policies) { |
| 127 | params := &RMRParams{&xapp.RMRParams{}} |
| 128 | params.Mtype = 20 |
| 129 | params.PayloadLen = len(policy) |
| 130 | params.Payload = []byte(policy) |
| 131 | params.Mbuf = nil |
| 132 | params.Whid = ep.Whid |
| 133 | xapp.Rmr.SendMsg(params.RMRParams) |
| 134 | count = 0 |
| 135 | policy = nil |
| 136 | xapp.Logger.Debug("Sent message with payload len = %d", params.PayloadLen) |
| 137 | } |
| 138 | } |
| 139 | |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 140 | xapp.Logger.Info("NNG PUSH to endpoint " + ep.Uuid + ": OK (# of Entries:" + strconv.Itoa(len(*policies)) + ")") |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 141 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 142 | |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 143 | func (c *NngPush) CreateEndpoint(payload string) *rtmgr.Endpoint { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 144 | return c.createEndpoint(payload, c) |
| 145 | } |
| 146 | |
| 147 | func (c *NngPush) DistributeToEp(policies *[]string, ep *rtmgr.Endpoint) error { |
| 148 | xapp.Logger.Debug("Invoked: sbi.DistributeToEp") |
| 149 | xapp.Logger.Debug("args: %v", *policies) |
| 150 | |
| 151 | go c.send(ep, policies) |
| 152 | |
| 153 | return nil |
| 154 | } |