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