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