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.
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
21 ==================================================================================
30 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 "routing-manager/pkg/models"
34 "routing-manager/pkg/rpe"
35 "routing-manager/pkg/rtmgr"
36 "routing-manager/pkg/sbi"
37 "routing-manager/pkg/sdl"
45 var EndpointLock sync.Mutex
48 var sbiEngine sbi.Engine
49 var sdlEngine sdl.Engine
50 var rpeEngine rpe.Engine
52 const INTERVAL time.Duration = 60
54 func NewControl() Control {
55 return Control{make(chan *xapp.RMRParams)}
59 rcChan chan *xapp.RMRParams
62 func (c *Control) Run() {
65 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err = initRtmgr()
67 xapp.Logger.Error(err.Error())
71 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
76 func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
77 resp, _ := DumpDebugData()
78 xapp.Resource.SendSymptomDataJson(w, r, resp, "platform/rttable.json")
81 func (c *Control) Consume(rp *xapp.RMRParams) (err error) {
86 func initRtmgr() (nbiEngine Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) {
87 if nbiEngine, err = GetNbi(xapp.Config.GetString("nbi")); err == nil && nbiEngine != nil {
88 if sbiEngine, err = sbi.GetSbi(xapp.Config.GetString("sbi")); err == nil && sbiEngine != nil {
89 if sdlEngine, err = sdl.GetSdl(xapp.Config.GetString("sdl")); err == nil && sdlEngine != nil {
90 if rpeEngine, err = rpe.GetRpe(xapp.Config.GetString("rpe")); err == nil && rpeEngine != nil {
91 return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil
96 return nil, nil, nil, nil, err
99 func (c *Control) controlLoop() {
106 func (c *Control) recievermr(msg *xapp.RMRParams) {
107 xapp_msg := sbi.RMRParams{msg}
109 case xapp.RICMessageTypes["RMRRM_REQ_TABLE"]:
110 if rtmgr.Rtmgr_ready == false {
111 xapp.Logger.Info("Update Route Table Request(RMR to RM), message discarded as routing manager is not ready")
113 xapp.Logger.Info("Update Route Table Request(RMR to RM)")
114 go c.handleUpdateToRoutingManagerRequest(msg)
116 case xapp.RICMessageTypes["RMRRM_TABLE_STATE"]:
117 xapp.Logger.Info("state of table to route mgr %s,payload %s", xapp_msg.String(), msg.Payload)
119 err := errors.New("Message Type " + strconv.Itoa(msg.Mtype) + " is discarded")
120 xapp.Logger.Error("Unknown message type: %v", err)
122 xapp.Rmr.Free(msg.Mbuf)
125 func (c *Control) handleUpdateToRoutingManagerRequest(params *xapp.RMRParams) {
127 msg := sbi.RMRParams{params}
129 xapp.Logger.Info("Update Route Table Request, msg.String() : %s", msg.String())
130 xapp.Logger.Info("Update Route Table Request, params.Payload : %s", string(params.Payload))
133 data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
137 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
140 xapp.Logger.Debug("Cannot get data from sdl interface")
145 /* hack with WA only for mcxapp in near future */
146 if strings.Contains(msg.String(), "ricxapp") {
147 ep := sbiEngine.CheckEndpoint(string(params.Payload))
149 xapp.Logger.Error("Update Routing Table Request can't handle due to end point %s is not avail in complete ep list: ", string(params.Payload))
154 epstr, whid := sbiEngine.CreateEndpoint(msg.String())
155 if epstr == nil || whid < 0 {
156 xapp.Logger.Error("Wormhole Id creation failed %d for %s", whid, msg.String())
160 /*This is to ensure the latest routes are sent.
161 Assumption is that in this time interval the routes are built for this endpoint */
162 time.Sleep(100 * time.Millisecond)
163 policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data)
164 err = sbiEngine.DistributeToEp(policies, *epstr, whid)
166 xapp.Logger.Error("Routing table cannot be published due to: " + err.Error())
171 func getConfigData() (*rtmgr.RicComponents, error) {
172 var data *rtmgr.RicComponents
174 data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
179 return nil, errors.New("Cannot get data from sdl interface due to: " + err.Error())
181 xapp.Logger.Debug("Cannot get data from sdl interface, data is null")
182 return nil, errors.New("Cannot get data from sdl interface")
189 func updateEp() (err error) {
190 data, err := getConfigData()
192 return errors.New("Routing table cannot be published due to: " + err.Error())
195 sbiEngine.UpdateEndpoints(data)
196 EndpointLock.Unlock()
201 func sendPartialRoutesToAll(xappSubData *models.XappSubscriptionData, updatetype rtmgr.RMRUpdateType) (err error) {
202 policies := rpeEngine.GeneratePartialPolicies(rtmgr.Eps, xappSubData, updatetype)
203 err = sbiEngine.DistributeAll(policies)
205 return errors.New("Routing table cannot be published due to: " + err.Error())
211 func sendRoutesToAll() (err error) {
213 data, err := getConfigData()
215 return errors.New("Routing table cannot be published due to: " + err.Error())
218 policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data)
219 err = sbiEngine.DistributeAll(policies)
221 return errors.New("Routing table cannot be published due to: " + err.Error())
229 nbiErr := nbiEngine.Initialize(xapp.Config.GetString("xmurl"), xapp.Config.GetString("nbiurl"), xapp.Config.GetString("rtfile"), xapp.Config.GetString("cfgfile"), xapp.Config.GetString("e2murl"), sdlEngine, rpeEngine, &m)
231 xapp.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
235 err := sbiEngine.Initialize(xapp.Config.GetString("sbiurl"))
237 xapp.Logger.Info("Failed to open push socket due to: " + err.Error())
240 defer nbiEngine.Terminate()
241 defer sbiEngine.Terminate()
243 /* used for rtmgr restart case to connect to Endpoints */
245 time.Sleep(5 * time.Second)
247 /* Sometimes first message fails, retry after 5 sec */
248 time.Sleep(5 * time.Second)
252 xapp.Logger.Debug("Periodic Routes value = %s", xapp.Config.GetString("periodicroutes"))
253 if xapp.Config.GetString("periodicroutes") == "enable" {
255 time.Sleep(5 * time.Second)
259 rtmgr.Rtmgr_ready = true
260 time.Sleep(INTERVAL * time.Second)
261 xapp.Logger.Debug("Periodic loop timed out. Setting triggerSBI flag to distribute updated routes.")