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"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 type httpEventWaiter struct {
46 func (msg *httpEventWaiter) SetResult(res bool) {
50 func (msg *httpEventWaiter) WaitResult(t *testing.T) bool {
52 case result := <-msg.resultChan:
54 case <-time.After(15 * time.Second):
55 testError(t, "Waiter not received result status from case within 15 secs")
58 testError(t, "Waiter error in default branch")
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 type testingHttpRtmgrControl struct {
69 eventWaiter *httpEventWaiter
72 func (hc *testingHttpRtmgrControl) NextEvent(eventWaiter *httpEventWaiter) {
75 hc.eventWaiter = eventWaiter
78 func (hc *testingHttpRtmgrControl) AllocNextEvent(nextAction bool) *httpEventWaiter {
79 eventWaiter := &httpEventWaiter{
80 resultChan: make(chan bool),
81 nextActionOk: nextAction,
83 hc.NextEvent(eventWaiter)
87 func (hc *testingHttpRtmgrControl) http_handler(w http.ResponseWriter, r *http.Request) {
92 var req rtmgr_models.XappSubscriptionData
93 err := json.NewDecoder(r.Body).Decode(&req)
95 xapp.Logger.Error("%s", err.Error())
97 xapp.Logger.Info("(%s) handling Address=%s Port=%d SubscriptionID=%d", hc.desc, *req.Address, *req.Port, *req.SubscriptionID)
101 case http.MethodPost:
103 if hc.eventWaiter != nil {
104 if hc.eventWaiter.nextActionOk == false {
108 case http.MethodDelete:
110 if hc.eventWaiter != nil {
111 if hc.eventWaiter.nextActionOk == false {
119 waiter := hc.eventWaiter
122 waiter.SetResult(true)
124 xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code)
129 func (hc *testingHttpRtmgrControl) run() {
130 http.HandleFunc("/", hc.http_handler)
131 http.ListenAndServe("localhost:"+hc.port, nil)
134 func initTestingHttpRtmgrControl(desc string, port string) *testingHttpRtmgrControl {
135 hc := &testingHttpRtmgrControl{}
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
144 type testingRmrControl struct {
146 syncChan chan struct{}
149 func (tc *testingRmrControl) ReadyCB(data interface{}) {
150 xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.desc)
151 tc.syncChan <- struct{}{}
155 func (tc *testingRmrControl) WaitCB() {
159 func initTestingControl(desc string, rtfile string, port string) testingRmrControl {
160 tc := testingRmrControl{}
161 os.Setenv("RMR_SEED_RT", rtfile)
162 os.Setenv("RMR_SRC_ID", "localhost:"+port)
163 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
164 xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
165 tc.desc = strings.ToUpper(desc)
166 tc.syncChan = make(chan struct{})
170 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 type testingRmrStubControl struct {
175 rmrClientTest *xapp.RMRClient
179 func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) {
181 //NOTE: Do this way until xapp-frame sending is improved
183 xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String())
186 for ; i <= 10 && status == false; i++ {
187 status = tc.rmrClientTest.SendMsg(params.RMRParams)
189 xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
190 time.Sleep(500 * time.Millisecond)
194 err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
195 xapp.Rmr.Free(params.Mbuf)
200 func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrStubControl {
201 tc := testingRmrStubControl{}
203 tc.testingRmrControl = initTestingControl(desc, rtfile, port)
204 tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
205 tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
206 go tc.rmrClientTest.Start(consumer)
211 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
214 type testingMessageChannel struct {
215 rmrConChan chan *RMRParams
218 func initTestingMessageChannel() testingMessageChannel {
219 mc := testingMessageChannel{}
220 mc.rmrConChan = make(chan *RMRParams)
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
227 type xappTransaction struct {
228 tc *testingXappControl
233 type testingXappControl struct {
234 testingRmrStubControl
235 testingMessageChannel
239 func (tc *testingXappControl) newXid() string {
241 xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
246 func (tc *testingXappControl) newXappTransaction(xid *string, ranname string) *xappTransaction {
247 trans := &xappTransaction{}
250 trans.xid = tc.newXid()
254 trans.meid = &xapp.RMRMeid{RanName: ranname}
258 func (tc *testingXappControl) Consume(params *xapp.RMRParams) (err error) {
259 xapp.Rmr.Free(params.Mbuf)
261 msg := &RMRParams{params}
263 if params.Mtype == 55555 {
264 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
269 if strings.Contains(msg.Xid, tc.desc) {
270 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
273 xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String())
278 func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl {
279 xappCtrl := &testingXappControl{}
280 xappCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
281 xappCtrl.testingMessageChannel = initTestingMessageChannel()
286 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
289 type testingE2termControl struct {
290 testingRmrStubControl
291 testingMessageChannel
294 func (tc *testingE2termControl) Consume(params *xapp.RMRParams) (err error) {
295 xapp.Rmr.Free(params.Mbuf)
297 msg := &RMRParams{params}
299 if params.Mtype == 55555 {
300 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
305 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
310 func createNewE2termControl(desc string, rtfile string, port string, stat string) *testingE2termControl {
311 e2termCtrl := &testingE2termControl{}
312 e2termCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl)
313 e2termCtrl.testingMessageChannel = initTestingMessageChannel()
317 //-----------------------------------------------------------------------------
319 //-----------------------------------------------------------------------------
320 type testingMainControl struct {
325 func createNewMainControl(desc string, rtfile string, port string) *testingMainControl {
326 mainCtrl = &testingMainControl{}
327 mainCtrl.testingRmrControl = initTestingControl(desc, rtfile, port)
328 mainCtrl.c = NewControl()
329 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
330 go xapp.RunWithParams(mainCtrl.c, false)
335 //-----------------------------------------------------------------------------
337 //-----------------------------------------------------------------------------
339 func testError(t *testing.T, pattern string, args ...interface{}) {
340 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
341 t.Errorf(fmt.Sprintf(pattern, args...))
344 func testLog(t *testing.T, pattern string, args ...interface{}) {
345 xapp.Logger.Info(fmt.Sprintf(pattern, args...))
346 t.Logf(fmt.Sprintf(pattern, args...))
349 func testCreateTmpFile(str string) (string, error) {
350 file, err := ioutil.TempFile("/tmp", "*.rt")
354 _, err = file.WriteString(str)
359 return file.Name(), nil
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
366 var xappConn1 *testingXappControl
367 var xappConn2 *testingXappControl
368 var e2termConn *testingE2termControl
369 var mainCtrl *testingMainControl
370 var rtmgrHttp *testingHttpRtmgrControl
372 func TestMain(m *testing.M) {
373 xapp.Logger.Info("TestMain start")
375 //---------------------------------
377 //---------------------------------
378 rtmgrHttp = initTestingHttpRtmgrControl("RTMGRSTUB", "8989")
381 //---------------------------------
383 //---------------------------------
386 //Cfg creation won't work like this as xapp-frame reads it during init.
397 "protPort": "tcp:14560",
400 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
401 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
406 "namespaces": ["sdl", "rnib"]
409 "HostAddr" : "localhost",
415 cfgfilename,_ := testCreateTmpFile(cfgstr)
416 defer os.Remove(cfgfilename)
417 os.Setenv("CFG_FILE", cfgfilename)
419 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
421 //---------------------------------
422 // Static routetable for rmr
424 // NOTE: Routing table is configured so, that responses
425 // are duplicated to xapp1 and xapp2 instances.
426 // If XID is not matching xapp stub will just
427 // drop message. (Messages 12011, 12012, 12021, 12022)
434 //---------------------------------
436 allrt := `newrt|start
437 mse|12010|-1|localhost:14560
438 mse|12010,localhost:14560|-1|localhost:15560
439 mse|12011,localhost:15560|-1|localhost:14560
440 mse|12012,localhost:15560|-1|localhost:14560
441 mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
442 mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
443 mse|12020|-1|localhost:14560
444 mse|12020,localhost:14560|-1|localhost:15560
445 mse|12021,localhost:15560|-1|localhost:14560
446 mse|12022,localhost:15560|-1|localhost:14560
447 mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
448 mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
449 mse|55555|-1|localhost:13660;localhost:13560,localhost:15560
453 //---------------------------------
455 //---------------------------------
456 xapp.Logger.Info("### submgr main run ###")
460 subsrt := `newrt|start
461 mse|12010|-1|localhost:14560
462 mse|12010,localhost:14560|-1|localhost:15560
463 mse|12011,localhost:15560|-1|localhost:14560
464 mse|12011|-1|localhost:13560;localhost:13660
465 mse|12012,localhost:15560|-1|localhost:14560
466 mse|12012|-1|localhost:13560;localhost:13660
467 mse|12020|-1|localhost:14560
468 mse|12020,localhost:14560|-1|localhost:15560
469 mse|12021,localhost:15560|-1|localhost:14560
470 mse|12021|-1|localhost:13560;localhost:13660
471 mse|12022,localhost:15560|-1|localhost:14560
472 mse|12022|-1|localhost:13560;localhost:13660
477 subrtfilename, _ := testCreateTmpFile(subsrt)
478 defer os.Remove(subrtfilename)
479 mainCtrl = createNewMainControl("main", subrtfilename, "14560")
481 //---------------------------------
483 //---------------------------------
484 xapp.Logger.Info("### xapp1 rmr run ###")
488 xapprt1 := `newrt|start
489 mse|12010|-1|localhost:14560
490 mse|12011|-1|localhost:13560
491 mse|12012|-1|localhost:13560
492 mse|12020|-1|localhost:14560
493 mse|12021|-1|localhost:13560
494 mse|12022|-1|localhost:13560
499 xapprtfilename1, _ := testCreateTmpFile(xapprt1)
500 defer os.Remove(xapprtfilename1)
501 xappConn1 = createNewXappControl("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
503 //---------------------------------
505 //---------------------------------
507 xapp.Logger.Info("### xapp2 rmr run ###")
511 xapprt2 := `newrt|start
512 mse|12010|-1|localhost:14560
513 mse|12011|-1|localhost:13660
514 mse|12012|-1|localhost:13660
515 mse|12020|-1|localhost:14560
516 mse|12021|-1|localhost:13660
517 mse|12022|-1|localhost:13660
522 xapprtfilename2, _ := testCreateTmpFile(xapprt2)
523 defer os.Remove(xapprtfilename2)
524 xappConn2 = createNewXappControl("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
526 //---------------------------------
528 //---------------------------------
529 xapp.Logger.Info("### e2term rmr run ###")
533 e2termrt := `newrt|start
534 mse|12010|-1|localhost:15560
535 mse|12011|-1|localhost:14560
536 mse|12012|-1|localhost:14560
537 mse|12020|-1|localhost:15560
538 mse|12021|-1|localhost:14560
539 mse|12022|-1|localhost:14560
544 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
545 defer os.Remove(e2termrtfilename)
546 e2termConn = createNewE2termControl("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
548 //---------------------------------
549 // Testing message sending
550 //---------------------------------
551 var dummyBuf []byte = make([]byte, 100)
553 params := &RMRParams{&xapp.RMRParams{}}
556 params.Payload = dummyBuf
557 params.PayloadLen = 100
558 params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"}
559 params.Xid = "THISISTESTFORSTUBS"
564 for ; i <= 10 && status == false; i++ {
565 xapp.Rmr.Send(params.RMRParams, false)
566 if e2termConn.active == true && xappConn1.active == true && xappConn2.active == true {
570 xapp.Logger.Info("Sleep 0.5 secs and try routes again")
571 time.Sleep(500 * time.Millisecond)
576 xapp.Logger.Error("Could not initialize routes")
580 //---------------------------------
582 //---------------------------------