11440dbe6190d7291c7be116e6c8e45b3015cea6
[ric-plt/rtmgr.git] / pkg / rpe / rpe.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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22 ==================================================================================
23 */
24 /*
25   Mnemonic:     rpe.go
26   Abstract:     Contains RPE (Route Policy Engine) module definitions and generic RPE components
27   Date:         16 March 2019
28 */
29
30 package rpe
31
32 import (
33         "errors"
34         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35         "routing-manager/pkg/rtmgr"
36         "routing-manager/pkg/sbi"
37         "runtime"
38         "strconv"
39 )
40
41 var (
42         SupportedRpes = []*EngineConfig{
43                 {
44                         Name:        "rmrpush",
45                         Version:     "pubsush",
46                         Protocol:    "rmruta",
47                         Instance:    NewRmrPush(),
48                         IsAvailable: true,
49                 },
50         }
51 )
52
53 func GetRpe(rpeName string) (Engine, error) {
54         for _, rpe := range SupportedRpes {
55                 if rpe.Name == rpeName && rpe.IsAvailable {
56                         return rpe.Instance, nil
57                 }
58         }
59         return nil, errors.New("SBI:" + rpeName + " is not supported or still not a available")
60 }
61
62 type Rpe struct {
63 }
64
65 func getEndpointByName(eps *rtmgr.Endpoints, name string) *rtmgr.Endpoint {
66         for _, ep := range *eps {
67                 if ep.Name == name {
68                         xapp.Logger.Debug("name: %s", ep.Name)
69                         xapp.Logger.Debug("ep: %v", ep)
70                         return ep
71                 }
72         }
73         return nil
74 }
75
76 func getEndpointListByName(eps *rtmgr.Endpoints, name string) []rtmgr.Endpoint {
77         var eplist []rtmgr.Endpoint
78
79         for _, ep := range *eps {
80                 if ep.Name == name {
81                         xapp.Logger.Debug("name: %s", ep.Name)
82                         xapp.Logger.Debug("ep: %v", ep)
83                         eplist = append(eplist, *ep)
84                 }
85         }
86         return eplist
87 }
88
89 func getEndpointByUuid(uuid string) *rtmgr.Endpoint {
90         endPoints := rtmgr.Eps
91         for _, ep := range endPoints {
92                 if ep.Uuid == uuid {
93                         xapp.Logger.Debug("name: %s", ep.Uuid)
94                         xapp.Logger.Debug("ep: %v", ep)
95                         return ep
96                 }
97         }
98         return nil
99 }
100
101 func (r *Rpe) addRoute(messageType string, tx *rtmgr.Endpoint, rx *rtmgr.Endpoint, routeTable *rtmgr.RouteTable, subId int32, routeType string) {
102         txList := rtmgr.EndpointList{}
103         rxList := []rtmgr.EndpointList{}
104
105         if tx == nil && rx == nil {
106                 pc, _, _, ok := runtime.Caller(1)
107                 details := runtime.FuncForPC(pc)
108                 if ok && details != nil {
109                         xapp.Logger.Error("Route addition skipped: Either TX or RX endpoint not present. Caller function is %s", details.Name())
110                 }
111         } else {
112                 if (tx != nil) {
113                         txList = rtmgr.EndpointList{*tx}
114                 }
115                 if (rx != nil) {
116                         rxList = []rtmgr.EndpointList{[]rtmgr.Endpoint{*rx}}
117                 }
118                 messageId := rtmgr.MessageTypes[messageType]
119                 route := rtmgr.RouteTableEntry{
120                         MessageType: messageId,
121                         TxList:      txList,
122                         RxGroups:    rxList,
123                         SubID:       subId,
124                         RouteType:   routeType}
125                 *routeTable = append(*routeTable, route)
126 //              xapp.Logger.Debug("Route added: MessageTyp: %v, Tx: %v, Rx: %v, SubId: %v", messageId, tx.Uuid, rx.Uuid, subId)
127 //              xapp.Logger.Trace("Route added: MessageTyp: %v, Tx: %v, Rx: %v, SubId: %v", messageId, tx, rx, subId)
128         }
129 }
130
131 func (r *Rpe) addRoute_rx_list(messageType string, tx *rtmgr.Endpoint, rx []rtmgr.Endpoint, routeTable *rtmgr.RouteTable, subId int32, routeType string) {
132         txList := rtmgr.EndpointList{}
133         rxList := []rtmgr.EndpointList{}
134
135         if (tx != nil) {
136                 txList = rtmgr.EndpointList{*tx}
137         }
138
139         if (rx != nil) {
140                 rxList = []rtmgr.EndpointList{rx}
141         }
142
143         messageId := rtmgr.MessageTypes[messageType]
144         route := rtmgr.RouteTableEntry{
145                 MessageType: messageId,
146                 TxList:      txList,
147                 RxGroups:    rxList,
148                 SubID:       subId,
149                 RouteType:   routeType}
150         *routeTable = append(*routeTable, route)
151 //      xapp.Logger.Debug("Route added: MessageTyp: %v, Tx: %v, Rx: %v, SubId: %v", messageId, tx.Uuid, rx.Uuid, subId)
152 //      xapp.Logger.Trace("Route added: MessageTyp: %v, Tx: %v, Rx: %v, SubId: %v", messageId, tx, rx, subId)
153 }
154
155
156
157 func (r *Rpe) generateXappRoutes(xAppEp *rtmgr.Endpoint, e2TermEp *rtmgr.Endpoint, subManEp *rtmgr.Endpoint, routeTable *rtmgr.RouteTable) {
158         xapp.Logger.Debug("rpe.generateXappRoutes invoked")
159         xapp.Logger.Debug("Endpoint: %v, xAppType: %v", xAppEp.Name, xAppEp.XAppType)
160         if xAppEp.XAppType != sbi.PlatformType && (len(xAppEp.TxMessages) > 0 || len(xAppEp.RxMessages) > 0) {
161                 /// TODO --- 
162                 //xApp -> Subscription Manager
163                 r.addRoute("RIC_SUB_REQ", xAppEp, subManEp, routeTable, -1, "")
164                 r.addRoute("RIC_SUB_DEL_REQ", xAppEp, subManEp, routeTable, -1, "")
165                 //xApp -> E2 Termination
166 //              r.addRoute("RIC_CONTROL_REQ", xAppEp, e2TermEp, routeTable, -1, "")
167                 r.addRoute("RIC_CONTROL_REQ", xAppEp, nil, routeTable, -1, "%meid")
168                 //E2 Termination -> xApp
169 ///             r.addRoute("RIC_CONTROL_ACK", e2TermEp, xAppEp, routeTable, -1, "")
170 ///             r.addRoute("RIC_CONTROL_FAILURE", e2TermEp, xAppEp, routeTable, -1, "")
171                 r.addRoute("RIC_CONTROL_ACK", nil, xAppEp, routeTable, -1, "")
172                 r.addRoute("RIC_CONTROL_FAILURE", nil, xAppEp, routeTable, -1, "")
173         }
174         //xApp->A1Mediator
175         if xAppEp.XAppType != sbi.PlatformType && len(xAppEp.Policies) > 0 {
176                 xapp.Logger.Debug("rpe.generateXappRoutes found policies section")
177                 for _, policy := range xAppEp.Policies {
178                         r.addRoute("A1_POLICY_REQ", nil, xAppEp, routeTable, policy, "")
179                 }
180         }
181
182 }
183
184 func (r *Rpe) generateSubscriptionRoutes(selectedxAppEp *rtmgr.Endpoint, e2TermEp *rtmgr.Endpoint, subManEp *rtmgr.Endpoint, routeTable *rtmgr.RouteTable) {
185         xapp.Logger.Debug("rpe.addSubscriptionRoutes invoked")
186         subscriptionList := &rtmgr.Subs
187         for _, subscription := range *subscriptionList {
188                 xapp.Logger.Debug("Subscription: %v", subscription)
189                 xAppUuid := subscription.Fqdn + ":" + strconv.Itoa(int(subscription.Port))
190                 xapp.Logger.Debug("xApp UUID: %v", xAppUuid)
191                 xAppEp := getEndpointByUuid(xAppUuid)
192                 if xAppEp.Uuid == selectedxAppEp.Uuid {
193                         xapp.Logger.Debug("xApp UUID is matched for selected xApp.UUID: %v and xApp.Name: %v", selectedxAppEp.Uuid, selectedxAppEp.Name)
194 /// TODO
195                         //Subscription Manager -> xApp
196                         r.addRoute("RIC_SUB_RESP", subManEp, xAppEp, routeTable, subscription.SubID, "")
197                         r.addRoute("RIC_SUB_FAILURE", subManEp, xAppEp, routeTable, subscription.SubID, "")
198                         r.addRoute("RIC_SUB_DEL_RESP", subManEp, xAppEp, routeTable, subscription.SubID, "")
199                         r.addRoute("RIC_SUB_DEL_FAILURE", subManEp, xAppEp, routeTable, subscription.SubID, "")
200                         //E2 Termination -> xApp
201                         r.addRoute("RIC_INDICATION", e2TermEp, xAppEp, routeTable, subscription.SubID, "")
202                         r.addRoute("RIC_CONTROL_ACK", e2TermEp, xAppEp, routeTable, subscription.SubID, "")
203                         r.addRoute("RIC_CONTROL_FAILURE", e2TermEp, xAppEp, routeTable, subscription.SubID, "")
204                 }
205         }
206 }
207
208 func (r *Rpe) generatePlatformRoutes(e2TermEp []rtmgr.Endpoint, subManEp *rtmgr.Endpoint, e2ManEp *rtmgr.Endpoint, ueManEp *rtmgr.Endpoint, rsmEp *rtmgr.Endpoint, a1mediatorEp *rtmgr.Endpoint, routeTable *rtmgr.RouteTable) {
209         xapp.Logger.Debug("rpe.generatePlatformRoutes invoked")
210         //Platform Routes --- Subscription Routes
211         //Subscription Manager -> E2 Termination
212         r.addRoute("RIC_SUB_REQ", subManEp, nil, routeTable, -1, "%meid")
213         r.addRoute("RIC_SUB_DEL_REQ", subManEp, nil, routeTable, -1, "%meid")
214         //E2 Termination -> Subscription Manager
215         r.addRoute("RIC_SUB_RESP", nil, subManEp, routeTable, -1, "")
216         r.addRoute("RIC_SUB_DEL_RESP", nil, subManEp, routeTable, -1, "")
217         r.addRoute("RIC_SUB_FAILURE", nil, subManEp, routeTable, -1, "")
218         r.addRoute("RIC_SUB_DEL_FAILURE", nil, subManEp, routeTable, -1, "")
219
220         //TODO: UE Man Routes removed (since it is not existing)
221         //UE Manager -> Subscription Manager
222         //r.addRoute("RIC_SUB_REQ", ueManEp, subManEp, routeTable)
223         //r.addRoute("RIC_SUB_DEL_REQ", ueManEp, subManEp, routeTable)
224         ////UE Manager -> E2 Termination
225         //r.addRoute("RIC_CONTROL_REQ", ueManEp, e2TermEp, routeTable)
226
227         //Platform Routes --- X2 Routes
228         //E2 Manager -> E2 Termination
229         r.addRoute("RIC_X2_SETUP_REQ", e2ManEp, nil, routeTable, -1, "%meid")
230         r.addRoute("RIC_X2_RESET_REQ", e2ManEp, nil, routeTable, -1, "%meid")
231         r.addRoute("RIC_X2_RESET_RESP", e2ManEp, nil, routeTable, -1, "%meid")
232         r.addRoute("RIC_ENDC_X2_SETUP_REQ", e2ManEp, nil, routeTable, -1, "%meid")
233         r.addRoute("RIC_ENB_CONF_UPDATE_ACK", e2ManEp, nil, routeTable, -1, "%meid")
234         r.addRoute("RIC_ENB_CONF_UPDATE_FAILURE", e2ManEp, nil, routeTable, -1, "%meid")
235         r.addRoute("RIC_ENDC_CONF_UPDATE_ACK", e2ManEp, nil, routeTable, -1, "%meid")
236         r.addRoute("RIC_ENDC_CONF_UPDATE_FAILURE", e2ManEp, nil, routeTable, -1, "%meid")
237
238         if len(e2TermEp) > 0 {
239                 r.addRoute_rx_list("RIC_SCTP_CLEAR_ALL", e2ManEp, e2TermEp, routeTable, -1, "")
240                 r.addRoute_rx_list("E2_TERM_KEEP_ALIVE_REQ", e2ManEp, e2TermEp, routeTable, -1, "")
241         }
242
243         //E2 Termination -> E2 Manager
244         r.addRoute("E2_TERM_INIT", nil, e2ManEp, routeTable, -1, "")
245         r.addRoute("RIC_X2_SETUP_RESP", nil, e2ManEp, routeTable, -1, "")
246         r.addRoute("RIC_X2_SETUP_FAILURE", nil, e2ManEp, routeTable, -1, "")
247         r.addRoute("RIC_X2_RESET_REQ", nil, e2ManEp, routeTable, -1, "")
248         r.addRoute("RIC_X2_RESET_RESP", nil, e2ManEp, routeTable, -1, "")
249         r.addRoute("RIC_ENDC_X2_SETUP_RESP", nil, e2ManEp, routeTable, -1, "")
250         r.addRoute("RIC_ENDC_X2_SETUP_FAILURE", nil, e2ManEp, routeTable, -1, "")
251         r.addRoute("RIC_ENDC_CONF_UPDATE", nil, e2ManEp, routeTable, -1, "")
252         r.addRoute("RIC_SCTP_CONNECTION_FAILURE", nil, e2ManEp, routeTable, -1, "")
253         r.addRoute("RIC_ERROR_INDICATION", nil, e2ManEp, routeTable, -1, "")
254         r.addRoute("RIC_ENB_CONF_UPDATE", nil, e2ManEp, routeTable, -1, "")
255         r.addRoute("RIC_ENB_LOAD_INFORMATION", nil, e2ManEp, routeTable, -1, "")
256         r.addRoute("E2_TERM_KEEP_ALIVE_RESP", nil, e2ManEp, routeTable, -1, "")
257
258
259
260         //E2 Manager -> Resource Status Manager
261         r.addRoute("RAN_CONNECTED", e2ManEp, rsmEp, routeTable, -1, "")
262         r.addRoute("RAN_RESTARTED", e2ManEp, rsmEp, routeTable, -1, "")
263         r.addRoute("RAN_RECONFIGURED", e2ManEp, rsmEp, routeTable, -1, "")
264
265         //Resource Status Manager -> E2 Termination
266         r.addRoute("RIC_RES_STATUS_REQ", rsmEp, nil, routeTable, -1, "%meid")
267         //E2 Termination -> Resource Status Manager
268         r.addRoute("RIC_RES_STATUS_RESP", nil, rsmEp, routeTable, -1, "")
269         r.addRoute("RIC_RES_STATUS_FAILURE", nil, rsmEp, routeTable, -1, "")
270
271         //ACxapp -> A1 Mediator
272         r.addRoute("A1_POLICY_QUERY", nil, a1mediatorEp, routeTable, -1, "")
273         r.addRoute("A1_POLICY_RESPONSE", nil, a1mediatorEp, routeTable, -1, "")
274 }
275
276 func (r *Rpe) generateRouteTable(endPointList rtmgr.Endpoints) *rtmgr.RouteTable {
277         xapp.Logger.Debug("rpe.generateRouteTable invoked")
278         xapp.Logger.Debug("Endpoint List:  %v", endPointList)
279         routeTable := &rtmgr.RouteTable{}
280         e2TermEp := getEndpointByName(&endPointList, "E2TERM")
281         if e2TermEp == nil {
282                 xapp.Logger.Error("Platform component not found: %v", "E2 Termination")
283                 xapp.Logger.Debug("Endpoints: %v", endPointList)
284         }
285         subManEp := getEndpointByName(&endPointList, "SUBMAN")
286         if subManEp == nil {
287                 xapp.Logger.Error("Platform component not found: %v", "Subscription Manager")
288                 xapp.Logger.Debug("Endpoints: %v", endPointList)
289         }
290         e2ManEp := getEndpointByName(&endPointList, "E2MAN")
291         if e2ManEp == nil {
292                 xapp.Logger.Error("Platform component not found: %v", "E2 Manager")
293                 xapp.Logger.Debug("Endpoints: %v", endPointList)
294         }
295         ueManEp := getEndpointByName(&endPointList, "UEMAN")
296         if ueManEp == nil {
297                 xapp.Logger.Error("Platform component not found: %v", "UE Manger")
298                 xapp.Logger.Debug("Endpoints: %v", endPointList)
299         }
300         rsmEp := getEndpointByName(&endPointList, "RSM")
301         if rsmEp == nil {
302                 xapp.Logger.Error("Platform component not found: %v", "Resource Status Manager")
303                 xapp.Logger.Debug("Endpoints: %v", endPointList)
304         }
305         A1MediatorEp := getEndpointByName(&endPointList, "A1MEDIATOR")
306         if A1MediatorEp == nil {
307                 xapp.Logger.Error("Platform component not found: %v", "A1Mediator")
308                 xapp.Logger.Debug("Endpoints: %v", endPointList)
309         }
310
311         e2TermListEp := getEndpointListByName(&endPointList, "E2TERMINST")
312         if len(e2TermListEp) == 0 {
313                 xapp.Logger.Error("Platform component not found: %v", "E2 Termination List")
314                 xapp.Logger.Debug("Endpoints: %v", endPointList)
315         }
316         r.generatePlatformRoutes(e2TermListEp, subManEp, e2ManEp, ueManEp, rsmEp, A1MediatorEp, routeTable)
317
318         for _, endPoint := range endPointList {
319                 xapp.Logger.Debug("Endpoint: %v, xAppType: %v", endPoint.Name, endPoint.XAppType)
320                 if endPoint.XAppType != sbi.PlatformType && (len(endPoint.TxMessages) > 0 || len(endPoint.RxMessages) > 0) {
321                         r.generateXappRoutes(endPoint, e2TermEp, subManEp, routeTable)
322                         r.generateSubscriptionRoutes(endPoint, e2TermEp, subManEp, routeTable)
323                 }
324         }
325         return routeTable
326 }