Updated xapp-frame to 0.4.18. Updated rmr to 4.1.2.
[ric-plt/submgr.git] / pkg / teststub / rmrenv.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 package teststub
21
22 import (
23         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
24         "os"
25         "strconv"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31
32 type RmrRouteTable struct {
33         tmpfile string
34         routes  []string
35         meids   []string
36 }
37
38 func (rrt *RmrRouteTable) AddRoute(mtype int, src string, subid int, trg string) {
39
40         line := "mse|"
41         line += strconv.FormatInt(int64(mtype), 10)
42         if len(src) > 0 {
43                 line += "," + src
44         }
45         line += "|"
46         line += strconv.FormatInt(int64(subid), 10)
47         line += "|"
48         line += trg
49         rrt.routes = append(rrt.routes, line)
50 }
51
52 func (rrt *RmrRouteTable) AddMeid(trg string, meids []string) {
53
54         line := "mme_ar"
55         line += " | "
56         line += trg
57         line += " | "
58         for _, str := range meids {
59                 line += " " + str
60         }
61         rrt.meids = append(rrt.meids, line)
62 }
63
64 func (rrt *RmrRouteTable) DelMeid(meids []string) {
65
66         line := "mme_del"
67         line += " | "
68         for _, str := range meids {
69                 line += " " + str
70         }
71         rrt.meids = append(rrt.meids, line)
72 }
73
74 func (rrt *RmrRouteTable) FileName() string {
75         return rrt.tmpfile
76 }
77
78 func (rrt *RmrRouteTable) Table() string {
79         allrt := "newrt|start\n"
80         for _, val := range rrt.routes {
81                 allrt += val + "\n"
82         }
83         allrt += "newrt|end\n"
84         allrt += "\n"
85         allrt += "meid_map | start\n"
86         for _, val := range rrt.meids {
87                 allrt += val + "\n"
88         }
89         allrt += "meid_map | end | " + strconv.FormatInt(int64(len(rrt.meids)), 10) + "\n"
90         return allrt
91 }
92
93 func (rrt *RmrRouteTable) Enable() {
94         if len(rrt.tmpfile) == 0 {
95                 rrt.tmpfile, _ = CreateTmpFile(rrt.Table())
96         }
97         os.Setenv("RMR_SEED_RT", rrt.tmpfile)
98         os.Setenv("RMR_RTG_SVC", "-1")
99         xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
100 }
101
102 func (rrt *RmrRouteTable) Disable() {
103         if len(rrt.tmpfile) > 0 {
104                 os.Remove(rrt.tmpfile)
105                 os.Unsetenv("RMR_SEED_RT")
106                 os.Unsetenv("RMR_RTG_SVC")
107                 rrt.tmpfile = ""
108                 xapp.Logger.Info("Not using rt file ")
109         }
110 }
111
112 //-----------------------------------------------------------------------------
113 //
114 //-----------------------------------------------------------------------------
115
116 type RmrSrcId struct {
117         xapp.RmrEndpoint
118 }
119
120 func (rsi *RmrSrcId) Enable() {
121         if rsi.Port > 0 {
122                 os.Setenv("RMR_SRC_ID", rsi.String())
123                 xapp.Logger.Info("Using src id  %s", os.Getenv("RMR_SRC_ID"))
124         }
125 }
126
127 func (rsi *RmrSrcId) Disable() {
128         os.Unsetenv("RMR_SRC_ID")
129         xapp.Logger.Info("Not using Using src id")
130 }
131
132 //-----------------------------------------------------------------------------
133 //
134 //-----------------------------------------------------------------------------
135 type RmrRtgSvc struct {
136         xapp.RmrEndpoint
137 }
138
139 func (rrs *RmrRtgSvc) Enable() {
140         if rrs.Port > 0 {
141                 os.Setenv("RMR_RTG_SVC", rrs.String())
142                 xapp.Logger.Info("Using rtg svc  %s", os.Getenv("RMR_SRC_ID"))
143         }
144 }
145
146 func (rrs *RmrRtgSvc) Disable() {
147         os.Unsetenv("RMR_RTG_SVC")
148         xapp.Logger.Info("Not using Using rtg svc")
149 }