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 | /* |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 24 | Mnemonic: rmrpipe.go |
| 25 | Abstract: mangos (RMR) Pipeline SBI implementation |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 26 | Date: 12 March 2019 |
| 27 | */ |
| 28 | |
| 29 | package sbi |
| 30 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 31 | /* |
| 32 | #include <rmr/rmr.h> |
| 33 | */ |
| 34 | import "C" |
| 35 | |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 36 | import ( |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 37 | "bytes" |
| 38 | "crypto/md5" |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 39 | "errors" |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 40 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
prabhukaliswamy | b47d12d | 2019-12-03 15:06:30 +0000 | [diff] [blame] | 41 | "routing-manager/pkg/rtmgr" |
| 42 | "strconv" |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 43 | "strings" |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 44 | "fmt" |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 45 | ) |
| 46 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 47 | var rmrcallid = 1 |
| 48 | var rmrdynamiccallid = 201 |
| 49 | |
| 50 | type RmrPush struct { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 51 | Sbi |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 52 | rcChan chan *xapp.RMRParams |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 53 | } |
| 54 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 55 | type EPStatus struct { |
| 56 | endpoint string |
| 57 | status bool |
| 58 | } |
| 59 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 60 | type RMRParams struct { |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 61 | *xapp.RMRParams |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 62 | } |
| 63 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 64 | func (params *RMRParams) String() string { |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 65 | var b bytes.Buffer |
| 66 | sum := md5.Sum(params.Payload) |
| 67 | 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) |
| 68 | return b.String() |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 71 | func NewRmrPush() *RmrPush { |
| 72 | instance := new(RmrPush) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 73 | return instance |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 74 | } |
| 75 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 76 | func (c *RmrPush) Initialize(ip string) error { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 77 | return nil |
| 78 | } |
| 79 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 80 | func (c *RmrPush) Terminate() error { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 81 | return nil |
| 82 | } |
| 83 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 84 | func (c *RmrPush) AddEndpoint(ep *rtmgr.Endpoint) error { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 85 | |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 86 | xapp.Logger.Debug("Invoked sbi.AddEndpoint") |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 87 | endpoint := ep.Ip + ":" + strconv.Itoa(DefaultRmrPipelineSocketNumber) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 88 | ep.Whid = int(xapp.Rmr.Openwh(endpoint)) |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 89 | if ep.Whid < 0 { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 90 | 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] | 91 | } else { |
| 92 | 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] | 93 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 94 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 95 | return nil |
| 96 | } |
| 97 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 98 | func (c *RmrPush) DeleteEndpoint(ep *rtmgr.Endpoint) error { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 99 | xapp.Logger.Debug("Invoked sbi. DeleteEndpoint") |
| 100 | xapp.Logger.Debug("args: %v", *ep) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 101 | |
| 102 | xapp.Rmr.Closewh(ep.Whid) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 103 | return nil |
| 104 | } |
| 105 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 106 | func (c *RmrPush) UpdateEndpoints(rcs *rtmgr.RicComponents) { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 107 | c.updateEndpoints(rcs, c) |
| 108 | } |
| 109 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 110 | func (c *RmrPush) DistributeAll(policies *[]string) error { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 111 | xapp.Logger.Debug("Invoked: sbi.DistributeAll") |
| 112 | xapp.Logger.Debug("args: %v", *policies) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 113 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 114 | /*for _, ep := range rtmgr.Eps { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 115 | go c.send(ep, policies) |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 116 | }*/ |
| 117 | channel := make(chan EPStatus) |
| 118 | |
| 119 | if rmrcallid == 200 { |
| 120 | rmrcallid = 1 |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 121 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 122 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 123 | for _, ep := range rtmgr.Eps { |
| 124 | go c.send_sync(ep, policies, channel, rmrcallid) |
| 125 | } |
rangajal | 2037469 | 2020-08-11 06:51:17 +0000 | [diff] [blame] | 126 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 127 | rmrcallid++ |
| 128 | |
| 129 | count := 0 |
| 130 | result := make([]EPStatus, len(rtmgr.Eps)) |
| 131 | for i, _ := range result { |
| 132 | result[i] = <-channel |
| 133 | if result[i].status == true { |
| 134 | count++ |
| 135 | } else { |
| 136 | xapp.Logger.Error("RMR send failed for endpoint %v", result[i].endpoint) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if count < len(rtmgr.Eps) { |
| 141 | return errors.New(" RMR response count " + string(count) + " is less than half of endpoint list " + string(len(rtmgr.Eps))) |
| 142 | } |
| 143 | |
| 144 | |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 145 | return nil |
| 146 | } |
| 147 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 148 | func (c *RmrPush) send_sync(ep *rtmgr.Endpoint, policies *[]string, channel chan EPStatus, call_id int) { |
| 149 | xapp.Logger.Debug("Push policy to endpoint: " + ep.Uuid) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 150 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 151 | ret := c.send_data(ep, policies, call_id) |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 152 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 153 | channel <- EPStatus{ep.Uuid, ret} |
wahidw | 37c510b | 2020-04-15 10:48:50 +0000 | [diff] [blame] | 154 | |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 155 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 156 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 157 | func (c *RmrPush) send_data(ep *rtmgr.Endpoint, policies *[]string, call_id int) bool { |
| 158 | xapp.Logger.Debug("Invoked send_data to endpoint: " + ep.Uuid + " call_id: " + strconv.Itoa(call_id)) |
| 159 | var state int |
| 160 | var retstr string |
| 161 | |
| 162 | var policy = []byte{} |
| 163 | |
| 164 | for _, pe := range *policies { |
| 165 | b:= []byte(pe) |
| 166 | for j:=0; j<len(b); j++{ |
| 167 | policy = append(policy,b[j]) |
| 168 | } |
| 169 | } |
| 170 | params := &RMRParams{&xapp.RMRParams{}} |
| 171 | params.Mtype = 20 |
| 172 | params.PayloadLen = len(policy) |
| 173 | params.Payload =[]byte(policy) |
| 174 | params.Mbuf = nil |
| 175 | params.Whid = ep.Whid |
| 176 | params.Callid = call_id |
| 177 | params.Timeout = 200 |
| 178 | state, retstr = xapp.Rmr.SendCallMsg(params.RMRParams) |
| 179 | routestatus := strings.Split(retstr," ") |
| 180 | if state != C.RMR_OK && routestatus[0] == "OK" { |
| 181 | xapp.Logger.Error("Updating Routes to Endpoint: " + ep.Uuid + " failed, call_id: " + strconv.Itoa(call_id) + " for xapp.Rmr.SendCallMsg " + " Route Update Status: " + routestatus[0]) |
| 182 | return false |
| 183 | } else { |
| 184 | xapp.Logger.Info("Update Routes to Endpoint: " + ep.Uuid + " successful, call_id: " + strconv.Itoa(call_id) + ", Payload length: " + strconv.Itoa(params.PayloadLen) + ", Route Update Status: " + routestatus[0] + "(# of Entries:" + strconv.Itoa(len(*policies))) |
| 185 | return true |
| 186 | } |
| 187 | |
| 188 | xapp.Logger.Error("Route Update to endpoint: " + ep.Uuid + " failed, call_id: " + strconv.Itoa(call_id) + " xapp.Rmr.SendCallMsg not called") |
| 189 | return false |
| 190 | } |
| 191 | |
wahidw | 6a9ce49 | 2020-08-15 11:29:43 +0530 | [diff] [blame^] | 192 | func (c *RmrPush) CreateEndpoint(payload string,rmrsrc string)(ep *string,whid int) { |
rangajal | 2037469 | 2020-08-11 06:51:17 +0000 | [diff] [blame] | 193 | return c.createEndpoint(payload,rmrsrc, c) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 194 | } |
| 195 | |
wahidw | 6a9ce49 | 2020-08-15 11:29:43 +0530 | [diff] [blame^] | 196 | func (c *RmrPush) DistributeToEp(policies *[]string, ep string, whid int) error { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 197 | xapp.Logger.Debug("Invoked: sbi.DistributeToEp") |
| 198 | xapp.Logger.Debug("args: %v", *policies) |
| 199 | |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 200 | if rmrdynamiccallid == 255 { |
| 201 | rmrdynamiccallid = 201 |
| 202 | } |
| 203 | |
wahidw | 6a9ce49 | 2020-08-15 11:29:43 +0530 | [diff] [blame^] | 204 | go c.sendDynamicRoutes(ep, whid, policies,rmrdynamiccallid) |
wahidw | bce6747 | 2020-06-15 13:52:55 +0000 | [diff] [blame] | 205 | rmrdynamiccallid++ |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 206 | |
| 207 | return nil |
| 208 | } |
rangajal | 2037469 | 2020-08-11 06:51:17 +0000 | [diff] [blame] | 209 | |
wahidw | 6a9ce49 | 2020-08-15 11:29:43 +0530 | [diff] [blame^] | 210 | func (c *RmrPush) sendDynamicRoutes(ep string,whid int, policies *[]string, call_id int) bool { |
rangajal | 2037469 | 2020-08-11 06:51:17 +0000 | [diff] [blame] | 211 | xapp.Logger.Debug("Invoked send_rt_process to endpoint: " + ep + " call_id: " + strconv.Itoa(call_id) + "whid: " + strconv.Itoa(whid)) |
| 212 | var state int |
| 213 | var retstr string |
| 214 | |
| 215 | var policy = []byte{} |
| 216 | |
| 217 | for _, pe := range *policies { |
| 218 | b:= []byte(pe) |
| 219 | for j:=0; j<len(b); j++{ |
| 220 | policy = append(policy,b[j]) |
| 221 | } |
| 222 | } |
| 223 | params := &RMRParams{&xapp.RMRParams{}} |
| 224 | params.Mtype = 20 |
| 225 | params.PayloadLen = len(policy) |
| 226 | params.Payload =[]byte(policy) |
| 227 | params.Mbuf = nil |
| 228 | params.Whid = whid |
| 229 | params.Callid = call_id |
| 230 | params.Timeout = 200 |
| 231 | state, retstr = xapp.Rmr.SendCallMsg(params.RMRParams) |
| 232 | routestatus := strings.Split(retstr," ") |
| 233 | if state != C.RMR_OK && routestatus[0] == "OK" { |
| 234 | xapp.Logger.Error("Updating Routes to Endpoint: " + ep + " failed, call_id: " + strconv.Itoa(call_id) + ",whi_id: " + strconv.Itoa(whid) + " for xapp.Rmr.SendCallMsg " + " Route Update Status: " + routestatus[0]) |
| 235 | return false |
| 236 | } else { |
| 237 | xapp.Logger.Info("Update Routes to Endpoint: " + ep + " successful, call_id: " + strconv.Itoa(call_id) + ", Payload length: " + strconv.Itoa(params.PayloadLen) + ",whi_id: " + strconv.Itoa(whid) + ", Route Update Status: " + routestatus[0] + "(# of Entries:" + strconv.Itoa(len(*policies))) |
| 238 | return true |
| 239 | } |
| 240 | |
| 241 | xapp.Logger.Error("Route Update to endpoint: " + ep + " failed, call_id: " + strconv.Itoa(call_id) + ",whi_id: " + strconv.Itoa(whid) + " xapp.Rmr.SendCallMsg not called") |
| 242 | return false |
| 243 | } |
| 244 | |