2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
26 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 type testingControl struct {
41 syncChan chan struct{}
44 func (tc *testingControl) ReadyCB(data interface{}) {
45 xapp.Logger.Info("testingControl(%s) ReadyCB", tc.desc)
46 tc.syncChan <- struct{}{}
50 func (tc *testingControl) WaitCB() {
54 func initTestingControl(desc string, rtfile string, port string) testingControl {
55 tc := testingControl{}
56 os.Setenv("RMR_SEED_RT", rtfile)
57 os.Setenv("RMR_SRC_ID", "localhost:"+port)
58 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
59 xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
60 tc.desc = strings.ToUpper(desc)
61 tc.syncChan = make(chan struct{})
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 type testingRmrControl struct {
70 rmrClientTest *xapp.RMRClient
73 func (tc *testingRmrControl) RmrSend(params *RMRParams) (err error) {
75 //NOTE: Do this way until xapp-frame sending is improved
77 xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String())
80 for ; i <= 10 && status == false; i++ {
81 status = tc.rmrClientTest.SendMsg(params.RMRParams)
83 xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
84 time.Sleep(500 * time.Millisecond)
88 err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
89 xapp.Rmr.Free(params.Mbuf)
94 func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrControl {
95 tc := testingRmrControl{}
96 tc.testingControl = initTestingControl(desc, rtfile, port)
97 tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
98 tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
99 go tc.rmrClientTest.Start(consumer)
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
107 type testingMessageChannel struct {
108 rmrConChan chan *RMRParams
111 func initTestingMessageChannel() testingMessageChannel {
112 mc := testingMessageChannel{}
113 mc.rmrConChan = make(chan *RMRParams)
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
120 type xappTransaction struct {
121 tc *testingXappControl
126 type testingXappControl struct {
128 testingMessageChannel
132 func (tc *testingXappControl) newXid() string {
134 xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
139 func (tc *testingXappControl) newXappTransaction(xid *string, ranname string) *xappTransaction {
140 trans := &xappTransaction{}
143 trans.xid = tc.newXid()
147 trans.meid = &xapp.RMRMeid{RanName: ranname}
151 func (tc *testingXappControl) Consume(params *xapp.RMRParams) (err error) {
152 xapp.Rmr.Free(params.Mbuf)
154 msg := &RMRParams{params}
156 if strings.Contains(msg.Xid, tc.desc) {
157 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
160 xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String())
165 func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl {
166 xappCtrl := &testingXappControl{}
167 xappCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
168 xappCtrl.testingMessageChannel = initTestingMessageChannel()
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
176 type testingE2termControl struct {
178 testingMessageChannel
181 func (tc *testingE2termControl) Consume(params *xapp.RMRParams) (err error) {
182 xapp.Rmr.Free(params.Mbuf)
184 msg := &RMRParams{params}
185 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
190 func createNewE2termControl(desc string, rtfile string, port string, stat string) *testingE2termControl {
191 e2termCtrl := &testingE2termControl{}
192 e2termCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl)
193 e2termCtrl.testingMessageChannel = initTestingMessageChannel()
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
200 type testingMainControl struct {
205 func createNewMainControl(desc string, rtfile string, port string) *testingMainControl {
206 mainCtrl = &testingMainControl{}
207 mainCtrl.testingControl = initTestingControl(desc, rtfile, port)
208 mainCtrl.c = NewControl()
209 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
210 go xapp.RunWithParams(mainCtrl.c, false)
215 //-----------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------
219 func testError(t *testing.T, pattern string, args ...interface{}) {
220 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
221 t.Errorf(fmt.Sprintf(pattern, args...))
224 func testLog(t *testing.T, pattern string, args ...interface{}) {
225 xapp.Logger.Info(fmt.Sprintf(pattern, args...))
226 t.Logf(fmt.Sprintf(pattern, args...))
229 func testCreateTmpFile(str string) (string, error) {
230 file, err := ioutil.TempFile("/tmp", "*.rt")
234 _, err = file.WriteString(str)
239 return file.Name(), nil
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 var xappConn1 *testingXappControl
247 var xappConn2 *testingXappControl
248 var e2termConn *testingE2termControl
249 var mainCtrl *testingMainControl
251 func TestMain(m *testing.M) {
252 xapp.Logger.Info("TestMain start")
254 //---------------------------------
256 //---------------------------------
257 http_handler := func(w http.ResponseWriter, r *http.Request) {
258 var req rtmgr_models.XappSubscriptionData
259 err := json.NewDecoder(r.Body).Decode(&req)
261 xapp.Logger.Error("%s", err.Error())
263 xapp.Logger.Info("(http handler) handling Address=%s Port=%d SubscriptionID=%d", *req.Address, *req.Port, *req.SubscriptionID)
269 http.HandleFunc("/", http_handler)
270 http.ListenAndServe("localhost:8989", nil)
273 //---------------------------------
275 //---------------------------------
278 //Cfg creation won't work like this as xapp-frame reads it during init.
289 "protPort": "tcp:14560",
292 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
293 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
298 "namespaces": ["sdl", "rnib"]
301 "HostAddr" : "localhost",
307 cfgfilename,_ := testCreateTmpFile(cfgstr)
308 defer os.Remove(cfgfilename)
309 os.Setenv("CFG_FILE", cfgfilename)
311 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
313 //---------------------------------
314 // Static routetable for rmr
316 // NOTE: Routing table is configured so, that responses
317 // are duplicated to xapp1 and xapp2 instances.
318 // If XID is not matching xapp stub will just
319 // drop message. (Messages 12011, 12012, 12021, 12022)
326 //---------------------------------
328 allrt := `newrt|start
329 mse|12010|-1|localhost:14560
330 mse|12010,localhost:14560|-1|localhost:15560
331 mse|12011,localhost:15560|-1|localhost:14560
332 mse|12012,localhost:15560|-1|localhost:14560
333 mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
334 mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
335 mse|12020|-1|localhost:14560
336 mse|12020,localhost:14560|-1|localhost:15560
337 mse|12021,localhost:15560|-1|localhost:14560
338 mse|12022,localhost:15560|-1|localhost:14560
339 mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
340 mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
344 //---------------------------------
346 //---------------------------------
347 xapp.Logger.Info("### submgr main run ###")
351 subsrt := `newrt|start
352 mse|12010|-1|localhost:14560
353 mse|12010,localhost:14560|-1|localhost:15560
354 mse|12011,localhost:15560|-1|localhost:14560
355 mse|12011|-1|localhost:13560;localhost:13660
356 mse|12012,localhost:15560|-1|localhost:14560
357 mse|12012|-1|localhost:13560;localhost:13660
358 mse|12020|-1|localhost:14560
359 mse|12020,localhost:14560|-1|localhost:15560
360 mse|12021,localhost:15560|-1|localhost:14560
361 mse|12021|-1|localhost:13560;localhost:13660
362 mse|12022,localhost:15560|-1|localhost:14560
363 mse|12022|-1|localhost:13560;localhost:13660
368 subrtfilename, _ := testCreateTmpFile(subsrt)
369 defer os.Remove(subrtfilename)
370 mainCtrl = createNewMainControl("main", subrtfilename, "14560")
372 //---------------------------------
374 //---------------------------------
375 xapp.Logger.Info("### xapp1 rmr run ###")
379 xapprt1 := `newrt|start
380 mse|12010|-1|localhost:14560
381 mse|12011|-1|localhost:13560
382 mse|12012|-1|localhost:13560
383 mse|12020|-1|localhost:14560
384 mse|12021|-1|localhost:13560
385 mse|12022|-1|localhost:13560
390 xapprtfilename1, _ := testCreateTmpFile(xapprt1)
391 defer os.Remove(xapprtfilename1)
392 xappConn1 = createNewXappControl("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
394 //---------------------------------
396 //---------------------------------
398 xapp.Logger.Info("### xapp2 rmr run ###")
402 xapprt2 := `newrt|start
403 mse|12010|-1|localhost:14560
404 mse|12011|-1|localhost:13660
405 mse|12012|-1|localhost:13660
406 mse|12020|-1|localhost:14560
407 mse|12021|-1|localhost:13660
408 mse|12022|-1|localhost:13660
413 xapprtfilename2, _ := testCreateTmpFile(xapprt2)
414 defer os.Remove(xapprtfilename2)
415 xappConn2 = createNewXappControl("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
417 //---------------------------------
419 //---------------------------------
420 xapp.Logger.Info("### e2term rmr run ###")
424 e2termrt := `newrt|start
425 mse|12010|-1|localhost:15560
426 mse|12011|-1|localhost:14560
427 mse|12012|-1|localhost:14560
428 mse|12020|-1|localhost:15560
429 mse|12021|-1|localhost:14560
430 mse|12022|-1|localhost:14560
435 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
436 defer os.Remove(e2termrtfilename)
437 e2termConn = createNewE2termControl("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
439 //---------------------------------
440 // Stupid sleep to try improve robustness
441 // due: http handler and rmr routes init delays
442 //---------------------------------
443 <-time.After(2 * time.Second)
445 //---------------------------------
447 //---------------------------------