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