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
180 func (tc *testingRmrStubControl) GetMsgCnt() uint64 {
184 func (tc *testingRmrStubControl) IncMsgCnt() {
188 func (tc *testingRmrStubControl) DecMsgCnt() {
194 func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) {
195 if tc.GetMsgCnt() > 0 {
196 testError(t, "(%s) message count expected 0 but is %d", tc.desc, tc.GetMsgCnt())
200 func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) {
202 //NOTE: Do this way until xapp-frame sending is improved
204 xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String())
207 for ; i <= 10 && status == false; i++ {
208 status = tc.rmrClientTest.SendMsg(params.RMRParams)
210 xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
211 time.Sleep(500 * time.Millisecond)
215 err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
216 xapp.Rmr.Free(params.Mbuf)
221 func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrStubControl {
222 tc := testingRmrStubControl{}
224 tc.testingRmrControl = initTestingControl(desc, rtfile, port)
225 tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
226 tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
227 go tc.rmrClientTest.Start(consumer)
232 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
235 type testingMessageChannel struct {
236 rmrConChan chan *RMRParams
239 func initTestingMessageChannel() testingMessageChannel {
240 mc := testingMessageChannel{}
241 mc.rmrConChan = make(chan *RMRParams)
245 //-----------------------------------------------------------------------------
247 //-----------------------------------------------------------------------------
248 type xappTransaction struct {
249 tc *testingXappControl
254 type testingXappControl struct {
255 testingRmrStubControl
256 testingMessageChannel
260 func (tc *testingXappControl) newXid() string {
262 xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
267 func (tc *testingXappControl) newXappTransaction(xid *string, ranname string) *xappTransaction {
268 trans := &xappTransaction{}
271 trans.xid = tc.newXid()
275 trans.meid = &xapp.RMRMeid{RanName: ranname}
279 func (tc *testingXappControl) Consume(params *xapp.RMRParams) (err error) {
280 xapp.Rmr.Free(params.Mbuf)
282 msg := &RMRParams{params}
284 if params.Mtype == 55555 {
285 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
290 if strings.Contains(msg.Xid, tc.desc) {
291 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
295 xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String())
300 func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl {
301 xappCtrl := &testingXappControl{}
302 xappCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
303 xappCtrl.testingMessageChannel = initTestingMessageChannel()
308 //-----------------------------------------------------------------------------
310 //-----------------------------------------------------------------------------
311 type testingE2termControl struct {
312 testingRmrStubControl
313 testingMessageChannel
316 func (tc *testingE2termControl) Consume(params *xapp.RMRParams) (err error) {
317 xapp.Rmr.Free(params.Mbuf)
319 msg := &RMRParams{params}
321 if params.Mtype == 55555 {
322 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
327 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
332 func createNewE2termControl(desc string, rtfile string, port string, stat string) *testingE2termControl {
333 e2termCtrl := &testingE2termControl{}
334 e2termCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl)
335 e2termCtrl.testingMessageChannel = initTestingMessageChannel()
339 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
342 type testingMainControl struct {
347 func createNewMainControl(desc string, rtfile string, port string) *testingMainControl {
348 mainCtrl = &testingMainControl{}
349 mainCtrl.testingRmrControl = initTestingControl(desc, rtfile, port)
350 mainCtrl.c = NewControl()
351 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
352 go xapp.RunWithParams(mainCtrl.c, false)
357 //-----------------------------------------------------------------------------
359 //-----------------------------------------------------------------------------
361 func testError(t *testing.T, pattern string, args ...interface{}) {
362 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
363 t.Errorf(fmt.Sprintf(pattern, args...))
366 func testLog(t *testing.T, pattern string, args ...interface{}) {
367 xapp.Logger.Info(fmt.Sprintf(pattern, args...))
368 t.Logf(fmt.Sprintf(pattern, args...))
371 func testCreateTmpFile(str string) (string, error) {
372 file, err := ioutil.TempFile("/tmp", "*.rt")
376 _, err = file.WriteString(str)
381 return file.Name(), nil
384 //-----------------------------------------------------------------------------
386 //-----------------------------------------------------------------------------
388 var xappConn1 *testingXappControl
389 var xappConn2 *testingXappControl
390 var e2termConn *testingE2termControl
391 var mainCtrl *testingMainControl
392 var rtmgrHttp *testingHttpRtmgrControl
394 func TestMain(m *testing.M) {
395 xapp.Logger.Info("TestMain start")
397 //---------------------------------
399 //---------------------------------
400 rtmgrHttp = initTestingHttpRtmgrControl("RTMGRSTUB", "8989")
403 //---------------------------------
405 //---------------------------------
408 //Cfg creation won't work like this as xapp-frame reads it during init.
419 "protPort": "tcp:14560",
422 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
423 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
428 "namespaces": ["sdl", "rnib"]
431 "HostAddr" : "localhost",
437 cfgfilename,_ := testCreateTmpFile(cfgstr)
438 defer os.Remove(cfgfilename)
439 os.Setenv("CFG_FILE", cfgfilename)
441 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
443 //---------------------------------
444 // Static routetable for rmr
446 // NOTE: Routing table is configured so, that responses
447 // are duplicated to xapp1 and xapp2 instances.
448 // If XID is not matching xapp stub will just
449 // drop message. (Messages 12011, 12012, 12021, 12022)
456 //---------------------------------
458 allrt := `newrt|start
459 mse|12010|-1|localhost:14560
460 mse|12010,localhost:14560|-1|localhost:15560
461 mse|12011,localhost:15560|-1|localhost:14560
462 mse|12012,localhost:15560|-1|localhost:14560
463 mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
464 mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
465 mse|12020|-1|localhost:14560
466 mse|12020,localhost:14560|-1|localhost:15560
467 mse|12021,localhost:15560|-1|localhost:14560
468 mse|12022,localhost:15560|-1|localhost:14560
469 mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
470 mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
471 mse|55555|-1|localhost:13660;localhost:13560,localhost:15560
475 //---------------------------------
477 //---------------------------------
478 xapp.Logger.Info("### submgr main run ###")
482 subsrt := `newrt|start
483 mse|12010|-1|localhost:14560
484 mse|12010,localhost:14560|-1|localhost:15560
485 mse|12011,localhost:15560|-1|localhost:14560
486 mse|12011|-1|localhost:13560;localhost:13660
487 mse|12012,localhost:15560|-1|localhost:14560
488 mse|12012|-1|localhost:13560;localhost:13660
489 mse|12020|-1|localhost:14560
490 mse|12020,localhost:14560|-1|localhost:15560
491 mse|12021,localhost:15560|-1|localhost:14560
492 mse|12021|-1|localhost:13560;localhost:13660
493 mse|12022,localhost:15560|-1|localhost:14560
494 mse|12022|-1|localhost:13560;localhost:13660
499 subrtfilename, _ := testCreateTmpFile(subsrt)
500 defer os.Remove(subrtfilename)
501 mainCtrl = createNewMainControl("main", subrtfilename, "14560")
503 //---------------------------------
505 //---------------------------------
506 xapp.Logger.Info("### xapp1 rmr run ###")
510 xapprt1 := `newrt|start
511 mse|12010|-1|localhost:14560
512 mse|12011|-1|localhost:13560
513 mse|12012|-1|localhost:13560
514 mse|12020|-1|localhost:14560
515 mse|12021|-1|localhost:13560
516 mse|12022|-1|localhost:13560
521 xapprtfilename1, _ := testCreateTmpFile(xapprt1)
522 defer os.Remove(xapprtfilename1)
523 xappConn1 = createNewXappControl("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
525 //---------------------------------
527 //---------------------------------
529 xapp.Logger.Info("### xapp2 rmr run ###")
533 xapprt2 := `newrt|start
534 mse|12010|-1|localhost:14560
535 mse|12011|-1|localhost:13660
536 mse|12012|-1|localhost:13660
537 mse|12020|-1|localhost:14560
538 mse|12021|-1|localhost:13660
539 mse|12022|-1|localhost:13660
544 xapprtfilename2, _ := testCreateTmpFile(xapprt2)
545 defer os.Remove(xapprtfilename2)
546 xappConn2 = createNewXappControl("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
548 //---------------------------------
550 //---------------------------------
551 xapp.Logger.Info("### e2term rmr run ###")
555 e2termrt := `newrt|start
556 mse|12010|-1|localhost:15560
557 mse|12011|-1|localhost:14560
558 mse|12012|-1|localhost:14560
559 mse|12020|-1|localhost:15560
560 mse|12021|-1|localhost:14560
561 mse|12022|-1|localhost:14560
566 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
567 defer os.Remove(e2termrtfilename)
568 e2termConn = createNewE2termControl("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
570 //---------------------------------
571 // Testing message sending
572 //---------------------------------
573 var dummyBuf []byte = make([]byte, 100)
575 params := &RMRParams{&xapp.RMRParams{}}
578 params.Payload = dummyBuf
579 params.PayloadLen = 100
580 params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"}
581 params.Xid = "THISISTESTFORSTUBS"
586 for ; i <= 10 && status == false; i++ {
587 xapp.Rmr.Send(params.RMRParams, false)
588 if e2termConn.active == true && xappConn1.active == true && xappConn2.active == true {
592 xapp.Logger.Info("Sleep 0.5 secs and try routes again")
593 time.Sleep(500 * time.Millisecond)
598 xapp.Logger.Error("Could not initialize routes")
602 //---------------------------------
604 //---------------------------------