2185d537ccdca89c410c772c340613474d794426
[ric-plt/submgr.git] / pkg / control / main_test.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 control
21
22 import (
23         "errors"
24         "fmt"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "io/ioutil"
27         "net/http"
28         "os"
29         "testing"
30         "time"
31 )
32
33 //-----------------------------------------------------------------------------
34 //
35 //-----------------------------------------------------------------------------
36 type testingControl struct {
37         desc     string
38         syncChan chan struct{}
39 }
40
41 func (tc *testingControl) ReadyCB(data interface{}) {
42         xapp.Logger.Info("testingControl(%s) ReadyCB", tc.desc)
43         tc.syncChan <- struct{}{}
44         return
45 }
46
47 //-----------------------------------------------------------------------------
48 //
49 //-----------------------------------------------------------------------------
50 type testingRmrControl struct {
51         testingControl
52         rmrClientTest *xapp.RMRClient
53         rmrConChan    chan *xapp.RMRParams
54 }
55
56 func (tc *testingRmrControl) Consume(msg *xapp.RMRParams) (err error) {
57         xapp.Logger.Info("testingRmrControl(%s) Consume", tc.desc)
58         tc.rmrConChan <- msg
59         return
60 }
61
62 func (tc *testingRmrControl) RmrSend(params *xapp.RMRParams) (err error) {
63         //
64         //NOTE: Do this way until xapp-frame sending is improved
65         //
66         status := false
67         i := 1
68         for ; i <= 10 && status == false; i++ {
69                 status = tc.rmrClientTest.SendMsg(params)
70                 if status == false {
71                         xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s", i, params.Mtype, params.SubId, params.Xid)
72                         time.Sleep(500 * time.Millisecond)
73                 }
74         }
75         if status == false {
76                 err = errors.New("rmr.Send() failed")
77                 tc.rmrClientTest.Free(params.Mbuf)
78         }
79         return
80 }
81
82 func createNewRmrControl(desc string, rtfile string, port string, stat string) *testingRmrControl {
83         os.Setenv("RMR_SEED_RT", rtfile)
84         os.Setenv("RMR_SRC_ID", "localhost:"+port)
85         xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
86         xapp.Logger.Info("Using src id  %s", os.Getenv("RMR_SRC_ID"))
87         newConn := &testingRmrControl{}
88         newConn.desc = desc
89         newConn.syncChan = make(chan struct{})
90         newConn.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
91         newConn.rmrConChan = make(chan *xapp.RMRParams)
92         newConn.rmrClientTest.SetReadyCB(newConn.ReadyCB, nil)
93         go newConn.rmrClientTest.Start(newConn)
94         <-newConn.syncChan
95         return newConn
96 }
97
98 //-----------------------------------------------------------------------------
99 //
100 //-----------------------------------------------------------------------------
101 type testingMainControl struct {
102         testingControl
103         c *Control
104 }
105
106 func (mc *testingMainControl) wait_subs_clean(e2SubsId int, secs int) bool {
107         i := 1
108         for ; i <= secs*2; i++ {
109                 if mc.c.registry.IsValidSequenceNumber(uint16(e2SubsId)) == false {
110                         return true
111                 }
112                 time.Sleep(500 * time.Millisecond)
113         }
114         return false
115 }
116
117 //-----------------------------------------------------------------------------
118 //
119 //-----------------------------------------------------------------------------
120
121 func testError(t *testing.T, pattern string, args ...interface{}) {
122         xapp.Logger.Error(fmt.Sprintf(pattern, args...))
123         t.Errorf(fmt.Sprintf(pattern, args...))
124 }
125
126 func testCreateTmpFile(str string) (string, error) {
127         file, err := ioutil.TempFile("/tmp", "*.rt")
128         if err != nil {
129                 return "", err
130         }
131         _, err = file.WriteString(str)
132         if err != nil {
133                 file.Close()
134                 return "", err
135         }
136         return file.Name(), nil
137 }
138
139 //-----------------------------------------------------------------------------
140 //
141 //-----------------------------------------------------------------------------
142
143 var xappConn *testingRmrControl
144 var e2termConn *testingRmrControl
145 var mainCtrl *testingMainControl
146
147 func TestMain(m *testing.M) {
148         xapp.Logger.Info("TestMain start")
149
150         //
151         //Cfg creation won't work like this as xapp-frame reads it during init.
152         //
153         /*
154             cfgstr:=`{
155               "local": {
156                   "host": ":8080"
157               },
158               "logger": {
159                   "level": 4
160               },
161               "rmr": {
162                  "protPort": "tcp:14560",
163                  "maxSize": 4096,
164                  "numWorkers": 1,
165                  "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
166                  "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
167               },
168               "db": {
169                   "host": "localhost",
170                   "port": 6379,
171                   "namespaces": ["sdl", "rnib"]
172               },
173                  "rtmgr" : {
174                    "HostAddr" : "localhost",
175                    "port" : "8989",
176                    "baseUrl" : "/"
177                  }
178            `
179
180            cfgfilename,_ := testCreateTmpFile(cfgstr)
181            defer os.Remove(cfgfilename)
182            os.Setenv("CFG_FILE", cfgfilename)
183         */
184         xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
185
186         //---------------------------------
187         //
188         //---------------------------------
189         xapp.Logger.Info("### submgr main run ###")
190
191         subsrt := `newrt|start
192 mse|12010|-1|localhost:14560
193 mse|12010,localhost:14560|-1|localhost:15560
194 mse|12011,localhost:15560|-1|localhost:14560
195 mse|12011|-1|localhost:13560
196 mse|12012,localhost:15560|-1|localhost:14560
197 mse|12012|-1|localhost:13560
198 mse|12020|-1|localhost:14560
199 mse|12020,localhost:14560|-1|localhost:15560
200 mse|12021,localhost:15560|-1|localhost:14560
201 mse|12021|-1|localhost:13560
202 mse|12022,localhost:15560|-1|localhost:14560
203 mse|12022|-1|localhost:13560
204 newrt|end
205 `
206
207         subrtfilename, _ := testCreateTmpFile(subsrt)
208         defer os.Remove(subrtfilename)
209         os.Setenv("RMR_SEED_RT", subrtfilename)
210         os.Setenv("RMR_SRC_ID", "localhost:14560")
211         xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
212         xapp.Logger.Info("Using src id  %s", os.Getenv("RMR_SRC_ID"))
213
214         mainCtrl = &testingMainControl{}
215         mainCtrl.desc = "main"
216         mainCtrl.syncChan = make(chan struct{})
217
218         mainCtrl.c = NewControl()
219         xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
220         go xapp.RunWithParams(mainCtrl.c, false)
221         <-mainCtrl.syncChan
222
223         //---------------------------------
224         //
225         //---------------------------------
226         xapp.Logger.Info("### xapp rmr run ###")
227
228         xapprt := `newrt|start
229 mse|12010|-1|localhost:14560
230 mse|12011|-1|localhost:13560
231 mse|12012|-1|localhost:13560
232 mse|12020|-1|localhost:14560
233 mse|12021|-1|localhost:13560
234 mse|12022|-1|localhost:13560
235 newrt|end
236 `
237
238         xapprtfilename, _ := testCreateTmpFile(xapprt)
239         defer os.Remove(xapprtfilename)
240         xappConn = createNewRmrControl("xappConn", xapprtfilename, "13560", "RMRXAPPSTUB")
241
242         //---------------------------------
243         //
244         //---------------------------------
245         xapp.Logger.Info("### e2term rmr run ###")
246
247         e2termrt := `newrt|start
248 mse|12010|-1|localhost:15560
249 mse|12011|-1|localhost:14560
250 mse|12012|-1|localhost:14560
251 mse|12020|-1|localhost:15560
252 mse|12021|-1|localhost:14560
253 mse|12022|-1|localhost:14560
254 newrt|end
255 `
256
257         e2termrtfilename, _ := testCreateTmpFile(e2termrt)
258         defer os.Remove(e2termrtfilename)
259         e2termConn = createNewRmrControl("e2termConn", e2termrtfilename, "15560", "RMRE2TERMSTUB")
260
261         //---------------------------------
262         //
263         //---------------------------------
264         http_handler := func(w http.ResponseWriter, r *http.Request) {
265                 xapp.Logger.Info("(http handler) handling")
266                 w.WriteHeader(200)
267         }
268
269         go func() {
270                 http.HandleFunc("/", http_handler)
271                 http.ListenAndServe("localhost:8989", nil)
272         }()
273
274         //---------------------------------
275         //
276         //---------------------------------
277         code := m.Run()
278         os.Exit(code)
279 }