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