ccf4e574a1b1c22944845365546bfd6bc40e5ae8
[ric-plt/rtmgr.git] / pkg / nbi / control.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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
19    platform project (RICP).
20
21 ==================================================================================
22 */
23 package nbi
24
25 import "C"
26
27 import (
28         "errors"
29         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30         "routing-manager/pkg/rpe"
31         "routing-manager/pkg/rtmgr"
32         "routing-manager/pkg/sbi"
33         "routing-manager/pkg/sdl"
34         "strconv"
35         "sync"
36 )
37
38 func NewControl() Control {
39
40         return Control{make(chan *xapp.RMRParams)}
41 }
42
43 type Control struct {
44         rcChan chan *xapp.RMRParams
45 }
46
47 func (c *Control) Run(sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) {
48         go c.controlLoop(sbiEngine, sdlEngine, rpeEngine, m)
49         xapp.Run(c)
50 }
51
52 func (c *Control) Consume(rp *xapp.RMRParams) (err error) {
53         c.rcChan <- rp
54         return
55 }
56
57 func (c *Control) controlLoop(sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) {
58         for {
59                 msg := <-c.rcChan
60                 xapp_msg := sbi.RMRParams{msg}
61                 switch msg.Mtype {
62                 case xapp.RICMessageTypes["RMRRM_REQ_TABLE"]:
63                         if rtmgr.Rtmgr_ready == false {
64                                 xapp.Logger.Info("Update Route Table Request(RMR to RM), message discarded as routing manager is not ready")
65                         } else {
66                                 xapp.Logger.Info("Update Route Table Request(RMR to RM)")
67                                 go c.handleUpdateToRoutingManagerRequest(msg, sbiEngine, sdlEngine, rpeEngine, m)
68                         }
69                 case xapp.RICMessageTypes["RMRRM_TABLE_STATE"]:
70                         xapp.Logger.Info("state of table to route mgr %s,payload %s", xapp_msg.String(), msg.Payload)
71
72                 default:
73                         err := errors.New("Message Type " + strconv.Itoa(msg.Mtype) + " is discarded")
74                         xapp.Logger.Error("Unknown message type: %v", err)
75                 }
76                 xapp.Rmr.Free(msg.Mbuf)
77         }
78 }
79
80 func (c *Control) handleUpdateToRoutingManagerRequest(params *xapp.RMRParams, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) {
81
82         msg := sbi.RMRParams{params}
83
84         xapp.Logger.Info("Update Route Table Request, msg.String() : %s", msg.String())
85         xapp.Logger.Info("Update Route Table Request, params.Payload : %s", string(params.Payload))
86
87         m.Lock()
88         data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
89         m.Unlock()
90         if err != nil || data == nil {
91                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
92                 return
93         }
94
95         ep := sbiEngine.CreateEndpoint(string(params.Payload))
96         if ep == nil {
97                 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))
98                 return
99         }
100
101         policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data)
102         err = sbiEngine.DistributeToEp(policies, ep)
103         if err != nil {
104                 xapp.Logger.Error("Routing table cannot be published due to: " + err.Error())
105                 return
106         }
107 }