Seed code for Routing Manager (ric-plt/rtmgr).
[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         "fmt"
30         "rtmgr"
31         "strconv"
32 )
33
34 var (
35         SupportedRpes = []*RpeEngineConfig{
36                 &RpeEngineConfig{
37                         RpeEngine{
38                                 Name:     "rmr",
39                                 Version:  "v1",
40                                 Protocol: "rmruta",
41                         },
42                         generatePolicies(generateRMRPolicies),
43                         true,
44                 },
45         }
46 )
47
48 func ListRpes() {
49         fmt.Printf("RPE:\n")
50         for _, rpe := range SupportedRpes {
51                 if rpe.IsAvailable {
52                         rtmgr.Logger.Info(rpe.Engine.Name + "/" + rpe.Engine.Version)
53                 }
54         }
55 }
56
57 func GetRpe(rpeName string) (*RpeEngineConfig, error) {
58         for _, rpe := range SupportedRpes {
59                 if rpe.Engine.Name == rpeName && rpe.IsAvailable {
60                         return rpe, nil
61                 }
62         }
63         return nil, errors.New("SBI:" + rpeName + "is not supported or still not a available")
64 }
65
66 /*
67 Gets the raw xApp list and generates the list of sender endpoints and receiver endpoint groups
68 Returns the Tx EndpointList map where the key is the messge type and also returns the nested map of Rx EndpointList's map where keys are message type and xapp type
69 Endpoint object's message type already transcoded to integer id
70 */
71 func getEndpointLists(xapps *[]rtmgr.XApp) (*map[string]rtmgr.EndpointList, *map[string]map[string]rtmgr.EndpointList) {
72         txlist := make(map[string]rtmgr.EndpointList)
73         rxgroups := make(map[string]map[string]rtmgr.EndpointList)
74         for _, xapp := range *xapps {
75                 for _, instance := range xapp.Instances {
76                         ep := rtmgr.Endpoint{
77                                 instance.Name,
78                                 xapp.Name,
79                                 instance.Ip + ":" + strconv.Itoa(instance.Port),
80                         }
81                         for _, message := range instance.RxMessages {
82                                 messageid := rtmgr.MESSAGETYPES[message]
83                                 if _, ok := rxgroups[messageid]; !ok {
84                                         rxgroups[messageid] = make(map[string]rtmgr.EndpointList)
85                                 }
86                                 rxgroups[messageid][xapp.Name] = append(rxgroups[messageid][xapp.Name], ep)
87                         }
88                         for _, message := range instance.TxMessages {
89                                 messageid := rtmgr.MESSAGETYPES[message]
90                                 txlist[messageid] = append(txlist[messageid], ep)
91                         }
92                 }
93         }
94         return &txlist, &rxgroups
95 }
96
97 /*
98 Gets the raw xapp list and creates a route table for
99 Returns the array of route table entries
100 */
101 func getRouteTable(xapps *[]rtmgr.XApp) *rtmgr.RouteTable {
102         tx, rx := getEndpointLists(xapps)
103         var rt rtmgr.RouteTable
104         for _, messagetype := range rtmgr.MESSAGETYPES {
105                 if _, ok := (*tx)[messagetype]; !ok {
106                         continue
107                 }
108                 if _, ok := (*rx)[messagetype]; !ok {
109                         continue
110                 }
111                 var rxgroups []rtmgr.EndpointList
112                 for _, endpointlist := range (*rx)[messagetype] {
113                         rxgroups = append(rxgroups, endpointlist)
114                 }
115                 rte := rtmgr.RouteTableEntry{
116                         messagetype,
117                         (*tx)[messagetype],
118                         rxgroups,
119                 }
120                 rt = append(rt, rte)
121         }
122         return &rt
123 }