Startup enhancement: routing manager is more resilient to missing components
[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 /*
20   Mnemonic:     rpe.go
21   Abstract:     Contains RPE (Route Policy Engine) module definitions and generic RPE components
22   Date:         16 March 2019
23 */
24
25 package rpe
26
27 import (
28         "errors"
29         "routing-manager/pkg/rtmgr"
30         "routing-manager/pkg/sbi"
31         "strconv"
32         "runtime"
33 )
34
35 var (
36         SupportedRpes = []*EngineConfig{
37                 {
38                         Name:        "rmrpush",
39                         Version:     "pubsush",
40                         Protocol:    "rmruta",
41                         Instance:    NewRmrPush(),
42                         IsAvailable: true,
43                 },
44         }
45 )
46
47 func GetRpe(rpeName string) (Engine, error) {
48         for _, rpe := range SupportedRpes {
49                 if rpe.Name == rpeName && rpe.IsAvailable {
50                         return rpe.Instance, nil
51                 }
52         }
53         return nil, errors.New("SBI:" + rpeName + " is not supported or still not a available")
54 }
55
56 type Rpe struct {
57 }
58
59 func getEndpointByName(eps *rtmgr.Endpoints, name string) *rtmgr.Endpoint {
60         for _, ep := range *eps {
61                 if ep.Name == name {
62                         rtmgr.Logger.Debug("name: %s", ep.Name)
63                         rtmgr.Logger.Debug("ep: %v", ep)
64                         return ep
65                 }
66         }
67         return nil
68 }
69
70 func getEndpointByUuid(uuid string) *rtmgr.Endpoint {
71         endPoints := rtmgr.Eps
72         for _, ep := range endPoints {
73                 if ep.Uuid == uuid {
74                         rtmgr.Logger.Debug("name: %s", ep.Uuid)
75                         rtmgr.Logger.Debug("ep: %v", ep)
76                         return ep
77                 }
78         }
79         return nil
80 }
81
82 func (r *Rpe) addRoute(messageType string, tx *rtmgr.Endpoint, rx *rtmgr.Endpoint, routeTable *rtmgr.RouteTable, subId int32) {
83         if tx != nil && rx != nil {
84                 txList := rtmgr.EndpointList{*tx}
85                 rxList := []rtmgr.EndpointList{[]rtmgr.Endpoint{*rx}}
86                 messageId := rtmgr.MessageTypes[messageType]
87                 route := rtmgr.RouteTableEntry{
88                                 MessageType: messageId,
89                                 TxList:      txList,
90                                 RxGroups:    rxList,
91                                 SubID:       subId}
92                                 *routeTable = append(*routeTable, route)
93                         rtmgr.Logger.Debug("Route added: MessageTyp: %v, Tx: %v, Rx: %v, SubId: %v", messageId, tx.Uuid, rx.Uuid, subId)
94                         rtmgr.Logger.Trace("Route added: MessageTyp: %v, Tx: %v, Rx: %v, SubId: %v", messageId, tx, rx, subId)
95                 } else {
96                         pc,_,_,ok := runtime.Caller(1)
97                         details := runtime.FuncForPC(pc)
98                         if ok && details != nil {
99                                 rtmgr.Logger.Error("Route addition skipped: Either TX or RX endpoint not present. Caller function is %s", details.Name())
100                         }
101                 }
102 }
103
104 func (r *Rpe) generateXappRoutes(xAppEp *rtmgr.Endpoint, e2TermEp *rtmgr.Endpoint, subManEp *rtmgr.Endpoint, routeTable *rtmgr.RouteTable) {
105         rtmgr.Logger.Debug("rpe.generateXappRoutes invoked")
106         rtmgr.Logger.Debug("Endpoint: %v, xAppType: %v", xAppEp.Name, xAppEp.XAppType)
107         if xAppEp.XAppType != sbi.PlatformType && len(xAppEp.TxMessages) > 0 && len(xAppEp.RxMessages) > 0 {
108                 //xApp -> Subscription Manager
109                 r.addRoute("RIC_SUB_REQ", xAppEp, subManEp, routeTable, -1)
110                 r.addRoute("RIC_SUB_DEL_REQ", xAppEp, subManEp, routeTable, -1)
111                 //xApp -> E2 Termination
112                 r.addRoute("RIC_CONTROL_REQ", xAppEp, e2TermEp, routeTable, -1)
113         }
114 }
115
116 func (r *Rpe) generateSubscriptionRoutes(e2TermEp *rtmgr.Endpoint, subManEp *rtmgr.Endpoint, routeTable *rtmgr.RouteTable) {
117         rtmgr.Logger.Debug("rpe.addSubscriptionRoutes invoked")
118         subscriptionList := &rtmgr.Subs
119         for _, subscription := range *subscriptionList {
120                 rtmgr.Logger.Debug("Subscription: %v", subscription)
121                 xAppUuid := subscription.Fqdn + ":" + strconv.Itoa(int(subscription.Port))
122                 rtmgr.Logger.Debug("xApp UUID: %v", xAppUuid)
123                 xAppEp := getEndpointByUuid(xAppUuid)
124                 //Subscription Manager -> xApp
125                 r.addRoute("RIC_SUB_RESP", subManEp, xAppEp, routeTable, subscription.SubID)
126                 r.addRoute("RIC_SUB_FAILURE", subManEp, xAppEp, routeTable, subscription.SubID)
127                 r.addRoute("RIC_SUB_DEL_RESP", subManEp, xAppEp, routeTable, subscription.SubID)
128                 r.addRoute("RIC_SUB_DEL_FAILURE", subManEp, xAppEp, routeTable, subscription.SubID)
129                 //E2 Termination -> xApp
130                 r.addRoute("RIC_INDICATION", e2TermEp, xAppEp, routeTable, subscription.SubID)
131                 r.addRoute("RIC_CONTROL_ACK", e2TermEp, xAppEp, routeTable, subscription.SubID)
132                 r.addRoute("RIC_CONTROL_FAILURE", e2TermEp, xAppEp, routeTable, subscription.SubID)
133         }
134 }
135
136 func (r *Rpe) generatePlatformRoutes(e2TermEp *rtmgr.Endpoint, subManEp *rtmgr.Endpoint, e2ManEp *rtmgr.Endpoint, ueManEp *rtmgr.Endpoint, routeTable *rtmgr.RouteTable) {
137         rtmgr.Logger.Debug("rpe.generatePlatformRoutes invoked")
138         //Platform Routes --- Subscription Routes
139         //Subscription Manager -> E2 Termination
140         r.addRoute("RIC_SUB_REQ", subManEp, e2TermEp, routeTable, -1)
141         r.addRoute("RIC_SUB_DEL_REQ", subManEp, e2TermEp, routeTable, -1)
142         //E2 Termination -> Subscription Manager
143         r.addRoute("RIC_SUB_RESP", e2TermEp, subManEp, routeTable, -1)
144         r.addRoute("RIC_SUB_DEL_RESP", e2TermEp, subManEp, routeTable, -1)
145         r.addRoute("RIC_SUB_FAILURE", e2TermEp, subManEp, routeTable, -1)
146         r.addRoute("RIC_SUB_DEL_FAILURE", e2TermEp, subManEp, routeTable, -1)
147         //TODO: UE Man Routes removed (since it is not existing)
148         //UE Manager -> Subscription Manager
149         //r.addRoute("RIC_SUB_REQ", ueManEp, subManEp, routeTable)
150         //r.addRoute("RIC_SUB_DEL_REQ", ueManEp, subManEp, routeTable)
151         ////UE Manager -> E2 Termination
152         //r.addRoute("RIC_CONTROL_REQ", ueManEp, e2TermEp, routeTable)
153
154         //Platform Routes --- X2 Routes
155         //E2 Manager -> E2 Termination
156         r.addRoute("RIC_X2_SETUP_REQ", e2ManEp, e2TermEp, routeTable, -1)
157         r.addRoute("RIC_X2_SETUP_RESP", e2ManEp, e2TermEp, routeTable, -1)
158         r.addRoute("RIC_X2_SETUP_FAILURE", e2ManEp, e2TermEp, routeTable, -1)
159         r.addRoute("RIC_X2_RESET_RESP", e2ManEp, e2TermEp, routeTable, -1)
160         r.addRoute("RIC_ENDC_X2_SETUP_REQ", e2ManEp, e2TermEp, routeTable, -1)
161         r.addRoute("RIC_ENDC_X2_SETUP_RESP", e2ManEp, e2TermEp, routeTable, -1)
162         r.addRoute("RIC_ENDC_X2_SETUP_FAILURE", e2ManEp, e2TermEp, routeTable, -1)
163         r.addRoute("RIC_SCTP_CLEAR_ALL", e2ManEp, e2TermEp, routeTable, -1)
164         //E2 Termination -> E2 Manager
165         r.addRoute("E2_TERM_INIT", e2TermEp, e2ManEp, routeTable, -1)
166         r.addRoute("RIC_X2_SETUP_REQ", e2TermEp, e2ManEp, routeTable, -1)
167         r.addRoute("RIC_X2_SETUP_RESP", e2TermEp, e2ManEp, routeTable, -1)
168         r.addRoute("RIC_X2_RESET", e2TermEp, e2ManEp, routeTable, -1)
169         r.addRoute("RIC_X2_RESOURCE_STATUS_RESPONSE", e2TermEp, e2ManEp, routeTable, -1)
170         r.addRoute("RIC_X2_RESET_RESP", e2TermEp, e2ManEp, routeTable, -1)
171         r.addRoute("RIC_ENDC_X2_SETUP_REQ", e2ManEp, e2TermEp, routeTable, -1)
172         r.addRoute("RIC_ENDC_X2_SETUP_RESP", e2ManEp, e2TermEp, routeTable, -1)
173         r.addRoute("RIC_ENDC_X2_SETUP_FAILURE", e2ManEp, e2TermEp, routeTable, -1)
174         r.addRoute("RIC_SCTP_CONNECTION_FAILURE", e2TermEp, e2ManEp, routeTable, -1)
175 }
176
177 func (r *Rpe) generateRouteTable(endPointList rtmgr.Endpoints) *rtmgr.RouteTable {
178         rtmgr.Logger.Debug("rpe.generateRouteTable invoked")
179         rtmgr.Logger.Debug("Endpoint List:  %v", endPointList)
180         routeTable := &rtmgr.RouteTable{}
181         e2TermEp := getEndpointByName(&endPointList, "E2TERM")
182         if e2TermEp == nil {
183                 rtmgr.Logger.Error("Platform component not found: %v", "E2 Termination")
184                 rtmgr.Logger.Debug("Endpoints: %v", endPointList)
185         }
186         subManEp := getEndpointByName(&endPointList, "SUBMAN")
187         if subManEp == nil {
188                 rtmgr.Logger.Error("Platform component not found: %v", "Subscription Manager")
189                 rtmgr.Logger.Debug("Endpoints: %v", endPointList)
190         }
191         e2ManEp := getEndpointByName(&endPointList, "E2MAN")
192         if e2ManEp == nil {
193                 rtmgr.Logger.Error("Platform component not found: %v", "E2 Manager")
194                 rtmgr.Logger.Debug("Endpoints: %v", endPointList)
195         }
196         ueManEp := getEndpointByName(&endPointList, "UEMAN")
197         if ueManEp == nil {
198                 rtmgr.Logger.Error("Platform component not found: %v", "UE Manger")
199                 rtmgr.Logger.Debug("Endpoints: %v", endPointList)
200         }
201         r.generatePlatformRoutes(e2TermEp, subManEp, e2ManEp, ueManEp, routeTable)
202
203         for _, endPoint := range endPointList {
204                 rtmgr.Logger.Debug("Endpoint: %v, xAppType: %v", endPoint.Name, endPoint.XAppType)
205                 if endPoint.XAppType != sbi.PlatformType && len(endPoint.TxMessages) > 0 && len(endPoint.RxMessages) > 0 {
206                         r.generateXappRoutes(endPoint, e2TermEp, subManEp, routeTable)
207                         r.generateSubscriptionRoutes(e2TermEp, subManEp, routeTable)
208                 }
209         }
210         return routeTable
211 }