dba8db6e0fb0a4401299b8d923a79f0d7e4b4232
[ric-plt/rtmgr.git] / pkg / rpe / rmr.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:     rmr.go
21   Abstract:     RMR Route Policy implementation
22                 Produces RMR (RIC Management Routing) formatted route messages
23   Date:         16 March 2019
24 */
25
26 package rpe
27
28 import (
29         "routing-manager/pkg/rtmgr"
30         "strconv"
31 )
32
33 type Rmr struct {
34         Rpe
35 }
36
37 type RmrPush struct {
38         Rmr
39 }
40
41 func NewRmrPush() *RmrPush {
42         instance := new(RmrPush)
43         return instance
44 }
45
46 /*
47 Produces the raw route message consumable by RMR
48 */
49 func (r *Rmr) generateRMRPolicies(eps rtmgr.Endpoints, key string) *[]string {
50         rawrt := []string{key + "newrt|start\n"}
51         rt := r.generateRouteTable(eps)
52         for _, rte := range *rt {
53                 rawrte := key + "mse|" + rte.MessageType
54                 for _, tx := range rte.TxList {
55                         rawrte += "," + tx.Ip + ":" + strconv.Itoa(int(tx.Port))
56                 }
57                 rawrte += "|" + strconv.Itoa(int(rte.SubID)) + "|"
58                 group := ""
59                 for _, rxg := range rte.RxGroups {
60                         member := ""
61                         for _, rx := range rxg {
62                                 if member == "" {
63                                         member += rx.Ip + ":" + strconv.Itoa(int(rx.Port))
64                                 } else {
65                                         member += "," + rx.Ip + ":" + strconv.Itoa(int(rx.Port))
66                                 }
67                         }
68                         if group == "" {
69                                 group += member
70                         } else {
71                                 group += ";" + member
72                         }
73                 }
74                 rawrte += group
75                 rawrt = append(rawrt, rawrte+"\n")
76         }
77         rawrt = append(rawrt, key+"newrt|end\n")
78         rtmgr.Logger.Debug("rmr.GeneratePolicies returns: %v", rawrt)
79         return &rawrt
80 }
81
82 func (r *RmrPush) GeneratePolicies(eps rtmgr.Endpoints) *[]string {
83         rtmgr.Logger.Debug("Invoked rmr.GeneratePolicies, args: %v: ", eps)
84         return r.generateRMRPolicies(eps, "")
85 }
86
87 func (r *RmrPush) GenerateRouteTable(eps rtmgr.Endpoints) *rtmgr.RouteTable {
88         return r.generateRouteTable(eps)
89 }