From: Juha Hyttinen Date: Fri, 24 Jan 2020 08:05:18 +0000 (+0200) Subject: Restructured test files. stubs locates in own files etc. X-Git-Tag: 0.4.0~25 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=fa0156680b0bd8f7300f49c65d2ee7bedaaa0e44;p=ric-plt%2Fsubmgr.git Restructured test files. stubs locates in own files etc. Change-Id: Idb5de62c8f7b13836f818bdbd8ec49244442965b Signed-off-by: Juha Hyttinen --- diff --git a/pkg/control/main_test.go b/pkg/control/main_test.go deleted file mode 100644 index ab0bff6..0000000 --- a/pkg/control/main_test.go +++ /dev/null @@ -1,607 +0,0 @@ -/* -================================================================================== - Copyright (c) 2019 AT&T Intellectual Property. - Copyright (c) 2019 Nokia - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -================================================================================== -*/ - -package control - -import ( - "encoding/json" - "fmt" - "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models" - "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" - "io/ioutil" - "net/http" - "os" - "strconv" - "strings" - "sync" - "testing" - "time" -) - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- - -type httpEventWaiter struct { - resultChan chan bool - nextActionOk bool -} - -func (msg *httpEventWaiter) SetResult(res bool) { - msg.resultChan <- res -} - -func (msg *httpEventWaiter) WaitResult(t *testing.T) bool { - select { - case result := <-msg.resultChan: - return result - case <-time.After(15 * time.Second): - testError(t, "Waiter not received result status from case within 15 secs") - return false - } - testError(t, "Waiter error in default branch") - return false -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type testingHttpRtmgrControl struct { - sync.Mutex - desc string - port string - eventWaiter *httpEventWaiter -} - -func (hc *testingHttpRtmgrControl) NextEvent(eventWaiter *httpEventWaiter) { - hc.Lock() - defer hc.Unlock() - hc.eventWaiter = eventWaiter -} - -func (hc *testingHttpRtmgrControl) AllocNextEvent(nextAction bool) *httpEventWaiter { - eventWaiter := &httpEventWaiter{ - resultChan: make(chan bool), - nextActionOk: nextAction, - } - hc.NextEvent(eventWaiter) - return eventWaiter -} - -func (hc *testingHttpRtmgrControl) http_handler(w http.ResponseWriter, r *http.Request) { - - hc.Lock() - defer hc.Unlock() - - var req rtmgr_models.XappSubscriptionData - err := json.NewDecoder(r.Body).Decode(&req) - if err != nil { - xapp.Logger.Error("%s", err.Error()) - } - xapp.Logger.Info("(%s) handling Address=%s Port=%d SubscriptionID=%d", hc.desc, *req.Address, *req.Port, *req.SubscriptionID) - - var code int = 0 - switch r.Method { - case http.MethodPost: - code = 201 - if hc.eventWaiter != nil { - if hc.eventWaiter.nextActionOk == false { - code = 400 - } - } - case http.MethodDelete: - code = 200 - if hc.eventWaiter != nil { - if hc.eventWaiter.nextActionOk == false { - code = 400 - } - } - default: - code = 200 - } - - waiter := hc.eventWaiter - hc.eventWaiter = nil - if waiter != nil { - waiter.SetResult(true) - } - xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code) - w.WriteHeader(code) - -} - -func (hc *testingHttpRtmgrControl) run() { - http.HandleFunc("/", hc.http_handler) - http.ListenAndServe("localhost:"+hc.port, nil) -} - -func initTestingHttpRtmgrControl(desc string, port string) *testingHttpRtmgrControl { - hc := &testingHttpRtmgrControl{} - hc.desc = desc - hc.port = port - return hc -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type testingRmrControl struct { - desc string - syncChan chan struct{} -} - -func (tc *testingRmrControl) ReadyCB(data interface{}) { - xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.desc) - tc.syncChan <- struct{}{} - return -} - -func (tc *testingRmrControl) WaitCB() { - <-tc.syncChan -} - -func initTestingControl(desc string, rtfile string, port string) testingRmrControl { - tc := testingRmrControl{} - os.Setenv("RMR_SEED_RT", rtfile) - os.Setenv("RMR_SRC_ID", "localhost:"+port) - xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT")) - xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID")) - tc.desc = strings.ToUpper(desc) - tc.syncChan = make(chan struct{}) - return tc -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type testingRmrStubControl struct { - testingRmrControl - rmrClientTest *xapp.RMRClient - active bool - msgCnt uint64 -} - -func (tc *testingRmrStubControl) GetMsgCnt() uint64 { - return tc.msgCnt -} - -func (tc *testingRmrStubControl) IncMsgCnt() { - tc.msgCnt++ -} - -func (tc *testingRmrStubControl) DecMsgCnt() { - if tc.msgCnt > 0 { - tc.msgCnt-- - } -} - -func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) { - if tc.GetMsgCnt() > 0 { - testError(t, "(%s) message count expected 0 but is %d", tc.desc, tc.GetMsgCnt()) - } -} - -func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) { - // - //NOTE: Do this way until xapp-frame sending is improved - // - xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String()) - status := false - i := 1 - for ; i <= 10 && status == false; i++ { - status = tc.rmrClientTest.SendMsg(params.RMRParams) - if status == false { - xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String()) - time.Sleep(500 * time.Millisecond) - } - } - if status == false { - err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String()) - xapp.Rmr.Free(params.Mbuf) - } - return -} - -func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrStubControl { - tc := testingRmrStubControl{} - tc.active = false - tc.testingRmrControl = initTestingControl(desc, rtfile, port) - tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat) - tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil) - go tc.rmrClientTest.Start(consumer) - tc.WaitCB() - return tc -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type testingMessageChannel struct { - rmrConChan chan *RMRParams -} - -func initTestingMessageChannel() testingMessageChannel { - mc := testingMessageChannel{} - mc.rmrConChan = make(chan *RMRParams) - return mc -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type xappTransaction struct { - tc *testingXappControl - xid string - meid *xapp.RMRMeid -} - -type testingXappControl struct { - testingRmrStubControl - testingMessageChannel - xid_seq uint64 -} - -func (tc *testingXappControl) newXid() string { - var xid string - xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10) - tc.xid_seq++ - return xid -} - -func (tc *testingXappControl) newXappTransaction(xid *string, ranname string) *xappTransaction { - trans := &xappTransaction{} - trans.tc = tc - if xid == nil { - trans.xid = tc.newXid() - } else { - trans.xid = *xid - } - trans.meid = &xapp.RMRMeid{RanName: ranname} - return trans -} - -func (tc *testingXappControl) Consume(params *xapp.RMRParams) (err error) { - xapp.Rmr.Free(params.Mbuf) - params.Mbuf = nil - msg := &RMRParams{params} - - if params.Mtype == 55555 { - xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String()) - tc.active = true - return - } - - if strings.Contains(msg.Xid, tc.desc) { - xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String()) - tc.IncMsgCnt() - tc.rmrConChan <- msg - } else { - xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String()) - } - return -} - -func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl { - xappCtrl := &testingXappControl{} - xappCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl) - xappCtrl.testingMessageChannel = initTestingMessageChannel() - xappCtrl.xid_seq = 1 - return xappCtrl -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type testingE2termControl struct { - testingRmrStubControl - testingMessageChannel -} - -func (tc *testingE2termControl) Consume(params *xapp.RMRParams) (err error) { - xapp.Rmr.Free(params.Mbuf) - params.Mbuf = nil - msg := &RMRParams{params} - - if params.Mtype == 55555 { - xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String()) - tc.active = true - return - } - - xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String()) - tc.rmrConChan <- msg - return -} - -func createNewE2termControl(desc string, rtfile string, port string, stat string) *testingE2termControl { - e2termCtrl := &testingE2termControl{} - e2termCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl) - e2termCtrl.testingMessageChannel = initTestingMessageChannel() - return e2termCtrl -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type testingMainControl struct { - testingRmrControl - c *Control -} - -func createNewMainControl(desc string, rtfile string, port string) *testingMainControl { - mainCtrl = &testingMainControl{} - mainCtrl.testingRmrControl = initTestingControl(desc, rtfile, port) - mainCtrl.c = NewControl() - xapp.SetReadyCB(mainCtrl.ReadyCB, nil) - go xapp.RunWithParams(mainCtrl.c, false) - mainCtrl.WaitCB() - return mainCtrl -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- - -func testError(t *testing.T, pattern string, args ...interface{}) { - xapp.Logger.Error(fmt.Sprintf(pattern, args...)) - t.Errorf(fmt.Sprintf(pattern, args...)) -} - -func testLog(t *testing.T, pattern string, args ...interface{}) { - xapp.Logger.Info(fmt.Sprintf(pattern, args...)) - t.Logf(fmt.Sprintf(pattern, args...)) -} - -func testCreateTmpFile(str string) (string, error) { - file, err := ioutil.TempFile("/tmp", "*.rt") - if err != nil { - return "", err - } - _, err = file.WriteString(str) - if err != nil { - file.Close() - return "", err - } - return file.Name(), nil -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- - -var xappConn1 *testingXappControl -var xappConn2 *testingXappControl -var e2termConn *testingE2termControl -var mainCtrl *testingMainControl -var rtmgrHttp *testingHttpRtmgrControl - -func TestMain(m *testing.M) { - xapp.Logger.Info("TestMain start") - - //--------------------------------- - // - //--------------------------------- - rtmgrHttp = initTestingHttpRtmgrControl("RTMGRSTUB", "8989") - go rtmgrHttp.run() - - //--------------------------------- - // - //--------------------------------- - - // - //Cfg creation won't work like this as xapp-frame reads it during init. - // - /* - cfgstr:=`{ - "local": { - "host": ":8080" - }, - "logger": { - "level": 4 - }, - "rmr": { - "protPort": "tcp:14560", - "maxSize": 4096, - "numWorkers": 1, - "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"], - "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"] - }, - "db": { - "host": "localhost", - "port": 6379, - "namespaces": ["sdl", "rnib"] - }, - "rtmgr" : { - "HostAddr" : "localhost", - "port" : "8989", - "baseUrl" : "/" - } - ` - - cfgfilename,_ := testCreateTmpFile(cfgstr) - defer os.Remove(cfgfilename) - os.Setenv("CFG_FILE", cfgfilename) - */ - xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE")) - - //--------------------------------- - // Static routetable for rmr - // - // NOTE: Routing table is configured so, that responses - // are duplicated to xapp1 and xapp2 instances. - // If XID is not matching xapp stub will just - // drop message. (Messages 12011, 12012, 12021, 12022) - // - // 14560 submgr - // 15560 e2term stub - // 13560 xapp1 stub - // 13660 xapp2 stub - // - //--------------------------------- - - allrt := `newrt|start -mse|12010|-1|localhost:14560 -mse|12010,localhost:14560|-1|localhost:15560 -mse|12011,localhost:15560|-1|localhost:14560 -mse|12012,localhost:15560|-1|localhost:14560 -mse|12011,localhost:14560|-1|localhost:13660;localhost:13560 -mse|12012,localhost:14560|-1|localhost:13660;localhost:13560 -mse|12020|-1|localhost:14560 -mse|12020,localhost:14560|-1|localhost:15560 -mse|12021,localhost:15560|-1|localhost:14560 -mse|12022,localhost:15560|-1|localhost:14560 -mse|12021,localhost:14560|-1|localhost:13660;localhost:13560 -mse|12022,localhost:14560|-1|localhost:13660;localhost:13560 -mse|55555|-1|localhost:13660;localhost:13560,localhost:15560 -newrt|end -` - - //--------------------------------- - // - //--------------------------------- - xapp.Logger.Info("### submgr main run ###") - - subsrt := allrt - /* - subsrt := `newrt|start - mse|12010|-1|localhost:14560 - mse|12010,localhost:14560|-1|localhost:15560 - mse|12011,localhost:15560|-1|localhost:14560 - mse|12011|-1|localhost:13560;localhost:13660 - mse|12012,localhost:15560|-1|localhost:14560 - mse|12012|-1|localhost:13560;localhost:13660 - mse|12020|-1|localhost:14560 - mse|12020,localhost:14560|-1|localhost:15560 - mse|12021,localhost:15560|-1|localhost:14560 - mse|12021|-1|localhost:13560;localhost:13660 - mse|12022,localhost:15560|-1|localhost:14560 - mse|12022|-1|localhost:13560;localhost:13660 - newrt|end - ` - */ - - subrtfilename, _ := testCreateTmpFile(subsrt) - defer os.Remove(subrtfilename) - mainCtrl = createNewMainControl("main", subrtfilename, "14560") - - //--------------------------------- - // - //--------------------------------- - xapp.Logger.Info("### xapp1 rmr run ###") - - xapprt1 := allrt - /* - xapprt1 := `newrt|start - mse|12010|-1|localhost:14560 - mse|12011|-1|localhost:13560 - mse|12012|-1|localhost:13560 - mse|12020|-1|localhost:14560 - mse|12021|-1|localhost:13560 - mse|12022|-1|localhost:13560 - newrt|end - ` - */ - - xapprtfilename1, _ := testCreateTmpFile(xapprt1) - defer os.Remove(xapprtfilename1) - xappConn1 = createNewXappControl("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB") - - //--------------------------------- - // - //--------------------------------- - - xapp.Logger.Info("### xapp2 rmr run ###") - - xapprt2 := allrt - /* - xapprt2 := `newrt|start - mse|12010|-1|localhost:14560 - mse|12011|-1|localhost:13660 - mse|12012|-1|localhost:13660 - mse|12020|-1|localhost:14560 - mse|12021|-1|localhost:13660 - mse|12022|-1|localhost:13660 - newrt|end - ` - */ - - xapprtfilename2, _ := testCreateTmpFile(xapprt2) - defer os.Remove(xapprtfilename2) - xappConn2 = createNewXappControl("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB") - - //--------------------------------- - // - //--------------------------------- - xapp.Logger.Info("### e2term rmr run ###") - - e2termrt := allrt - /* - e2termrt := `newrt|start - mse|12010|-1|localhost:15560 - mse|12011|-1|localhost:14560 - mse|12012|-1|localhost:14560 - mse|12020|-1|localhost:15560 - mse|12021|-1|localhost:14560 - mse|12022|-1|localhost:14560 - newrt|end - ` - */ - - e2termrtfilename, _ := testCreateTmpFile(e2termrt) - defer os.Remove(e2termrtfilename) - e2termConn = createNewE2termControl("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB") - - //--------------------------------- - // Testing message sending - //--------------------------------- - var dummyBuf []byte = make([]byte, 100) - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = 55555 - params.SubId = -1 - params.Payload = dummyBuf - params.PayloadLen = 100 - params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"} - params.Xid = "THISISTESTFORSTUBS" - params.Mbuf = nil - - status := false - i := 1 - for ; i <= 10 && status == false; i++ { - xapp.Rmr.Send(params.RMRParams, false) - if e2termConn.active == true && xappConn1.active == true && xappConn2.active == true { - status = true - break - } else { - xapp.Logger.Info("Sleep 0.5 secs and try routes again") - time.Sleep(500 * time.Millisecond) - } - } - - if status == false { - xapp.Logger.Error("Could not initialize routes") - os.Exit(1) - } - - //--------------------------------- - // - //--------------------------------- - code := m.Run() - os.Exit(code) -} diff --git a/pkg/control/ut_ctrl_submgr_test.go b/pkg/control/ut_ctrl_submgr_test.go new file mode 100644 index 0000000..197eb43 --- /dev/null +++ b/pkg/control/ut_ctrl_submgr_test.go @@ -0,0 +1,122 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 2019 Nokia + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +================================================================================== +*/ + +package control + +import ( + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "testing" + "time" +) + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type testingSubmgrControl struct { + testingRmrControl + c *Control +} + +func createSubmgrControl(desc string, rtfile string, port string) *testingSubmgrControl { + mainCtrl = &testingSubmgrControl{} + mainCtrl.testingRmrControl.init(desc, rtfile, port) + mainCtrl.c = NewControl() + xapp.SetReadyCB(mainCtrl.ReadyCB, nil) + go xapp.RunWithParams(mainCtrl.c, false) + mainCtrl.WaitCB() + return mainCtrl +} + +func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId int, secs int) bool { + var subs *Subscription + i := 1 + for ; i <= secs*2; i++ { + subs = mc.c.registry.GetSubscription(uint16(e2SubsId)) + if subs == nil { + return true + } + time.Sleep(500 * time.Millisecond) + } + if subs != nil { + testError(t, "(general) no clean within %d secs: %s", secs, subs.String()) + } else { + testError(t, "(general) no clean within %d secs: subs(N/A)", secs) + } + return false +} + +func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId int, secs int) bool { + var trans *Transaction + i := 1 + for ; i <= secs*2; i++ { + subs := mc.c.registry.GetSubscription(uint16(e2SubsId)) + if subs == nil { + return true + } + trans = subs.GetTransaction() + if trans == nil { + return true + } + time.Sleep(500 * time.Millisecond) + } + if trans != nil { + testError(t, "(general) no clean within %d secs: %s", secs, trans.String()) + } else { + testError(t, "(general) no clean within %d secs: trans(N/A)", secs) + } + return false +} + +func (mc *testingSubmgrControl) get_subid(t *testing.T) uint16 { + mc.c.registry.mutex.Lock() + defer mc.c.registry.mutex.Unlock() + return mc.c.registry.subIds[0] +} + +func (mc *testingSubmgrControl) wait_subid_change(t *testing.T, origSubId uint16, secs int) (uint16, bool) { + i := 1 + for ; i <= secs*2; i++ { + mc.c.registry.mutex.Lock() + currSubId := mc.c.registry.subIds[0] + mc.c.registry.mutex.Unlock() + if currSubId != origSubId { + return currSubId, true + } + time.Sleep(500 * time.Millisecond) + } + testError(t, "(general) no subId change within %d secs", secs) + return 0, false +} + +func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 { + return mc.c.msgCounter +} + +func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) { + i := 1 + for ; i <= secs*2; i++ { + curr := mc.c.msgCounter + if curr != orig { + return curr, true + } + time.Sleep(500 * time.Millisecond) + } + testError(t, "(general) no msg counter change within %d secs", secs) + return 0, false +} diff --git a/pkg/control/messaging_test.go b/pkg/control/ut_messaging_test.go similarity index 58% rename from pkg/control/messaging_test.go rename to pkg/control/ut_messaging_test.go index 4465d6c..3b43c3a 100644 --- a/pkg/control/messaging_test.go +++ b/pkg/control/ut_messaging_test.go @@ -20,609 +20,10 @@ package control import ( - "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" - "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper" - "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer" "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" "testing" - "time" ) -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -var e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer() - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (xappConn *testingXappControl) handle_xapp_subs_req(t *testing.T, oldTrans *xappTransaction) *xappTransaction { - xapp.Logger.Info("(%s) handle_xapp_subs_req", xappConn.desc) - e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest() - - //--------------------------------- - // xapp activity: Send Subs Req - //--------------------------------- - xapp.Logger.Info("(%s) Send Subs Req", xappConn.desc) - - req := &e2ap.E2APSubscriptionRequest{} - - req.RequestId.Id = 1 - req.RequestId.Seq = 0 - req.FunctionId = 1 - - req.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present = true - req.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.StringPut("310150") - req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Id = 123 - req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Bits = e2ap.E2AP_ENBIDHomeBits28 - - // gnb -> enb outgoing - // enb -> gnb incoming - // X2 36423-f40.doc - req.EventTriggerDefinition.InterfaceDirection = e2ap.E2AP_InterfaceDirectionIncoming - req.EventTriggerDefinition.ProcedureCode = 5 //28 35 - req.EventTriggerDefinition.TypeOfMessage = e2ap.E2AP_InitiatingMessage - - req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1) - req.ActionSetups[0].ActionId = 0 - req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport - req.ActionSetups[0].ActionDefinition.Present = false - //req.ActionSetups[index].ActionDefinition.StyleId = 255 - //req.ActionSetups[index].ActionDefinition.ParamId = 222 - req.ActionSetups[0].SubsequentAction.Present = true - req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue - req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero - - e2SubsReq.Set(req) - xapp.Logger.Debug("%s", e2SubsReq.String()) - err, packedMsg := e2SubsReq.Pack(nil) - if err != nil { - testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error()) - return nil - } - - var trans *xappTransaction = oldTrans - if trans == nil { - trans = xappConn.newXappTransaction(nil, "RAN_NAME_1") - } - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = xapp.RIC_SUB_REQ - params.SubId = -1 - params.Payload = packedMsg.Buf - params.Meid = trans.meid - params.Xid = trans.xid - params.Mbuf = nil - - snderr := xappConn.RmrSend(params) - if snderr != nil { - testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error()) - return nil - } - return trans -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (xappConn *testingXappControl) handle_xapp_subs_resp(t *testing.T, trans *xappTransaction) int { - xapp.Logger.Info("(%s) handle_xapp_subs_resp", xappConn.desc) - e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse() - var e2SubsId int - - //--------------------------------- - // xapp activity: Recv Subs Resp - //--------------------------------- - select { - case msg := <-xappConn.rmrConChan: - xappConn.DecMsgCnt() - if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] { - testError(t, "(%s) Received RIC_SUB_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype]) - return -1 - } else if msg.Xid != trans.xid { - testError(t, "(%s) Received RIC_SUB_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid) - return -1 - } else { - packedData := &packer.PackedData{} - packedData.Buf = msg.Payload - e2SubsId = msg.SubId - unpackerr := e2SubsResp.UnPack(packedData) - - if unpackerr != nil { - testError(t, "(%s) RIC_SUB_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error()) - } - geterr, resp := e2SubsResp.Get() - if geterr != nil { - testError(t, "(%s) RIC_SUB_RESP get failed err: %s", xappConn.desc, geterr.Error()) - } - - xapp.Logger.Info("(%s) Recv Subs Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq) - return e2SubsId - } - case <-time.After(15 * time.Second): - testError(t, "(%s) Not Received RIC_SUB_RESP within 15 secs", xappConn.desc) - return -1 - } - return -1 -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (xappConn *testingXappControl) handle_xapp_subs_fail(t *testing.T, trans *xappTransaction) int { - xapp.Logger.Info("(%s) handle_xapp_subs_fail", xappConn.desc) - e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure() - var e2SubsId int - - //------------------------------- - // xapp activity: Recv Subs Fail - //------------------------------- - select { - case msg := <-xappConn.rmrConChan: - xappConn.DecMsgCnt() - if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] { - testError(t, "(%s) Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype]) - return -1 - } else if msg.Xid != trans.xid { - testError(t, "(%s) Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid) - return -1 - } else { - packedData := &packer.PackedData{} - packedData.Buf = msg.Payload - e2SubsId = msg.SubId - unpackerr := e2SubsFail.UnPack(packedData) - - if unpackerr != nil { - testError(t, "(%s) RIC_SUB_FAILURE unpack failed err: %s", xappConn.desc, unpackerr.Error()) - } - geterr, resp := e2SubsFail.Get() - if geterr != nil { - testError(t, "(%s) RIC_SUB_FAILURE get failed err: %s", xappConn.desc, geterr.Error()) - } - - xapp.Logger.Info("(%s) Recv Subs Fail rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq) - return e2SubsId - } - case <-time.After(15 * time.Second): - testError(t, "(%s) Not Received RIC_SUB_FAILURE within 15 secs", xappConn.desc) - return -1 - } - return -1 -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (xappConn *testingXappControl) handle_xapp_subs_del_req(t *testing.T, oldTrans *xappTransaction, e2SubsId int) *xappTransaction { - xapp.Logger.Info("(%s) handle_xapp_subs_del_req", xappConn.desc) - e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest() - - //--------------------------------- - // xapp activity: Send Subs Del Req - //--------------------------------- - xapp.Logger.Info("(%s) Send Subs Del Req", xappConn.desc) - - req := &e2ap.E2APSubscriptionDeleteRequest{} - req.RequestId.Id = 1 - req.RequestId.Seq = uint32(e2SubsId) - req.FunctionId = 1 - - e2SubsDelReq.Set(req) - xapp.Logger.Debug("%s", e2SubsDelReq.String()) - err, packedMsg := e2SubsDelReq.Pack(nil) - if err != nil { - testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error()) - return nil - } - - var trans *xappTransaction = oldTrans - if trans == nil { - trans = xappConn.newXappTransaction(nil, "RAN_NAME_1") - } - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = xapp.RIC_SUB_DEL_REQ - params.SubId = e2SubsId - params.Payload = packedMsg.Buf - params.Meid = trans.meid - params.Xid = trans.xid - params.Mbuf = nil - - snderr := xappConn.RmrSend(params) - if snderr != nil { - testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error()) - return nil - } - return trans -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (xappConn *testingXappControl) handle_xapp_subs_del_resp(t *testing.T, trans *xappTransaction) { - xapp.Logger.Info("(%s) handle_xapp_subs_del_resp", xappConn.desc) - e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse() - - //--------------------------------- - // xapp activity: Recv Subs Del Resp - //--------------------------------- - select { - case msg := <-xappConn.rmrConChan: - xappConn.DecMsgCnt() - if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] { - testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype]) - return - } else if msg.Xid != trans.xid { - testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid) - return - } else { - packedData := &packer.PackedData{} - packedData.Buf = msg.Payload - unpackerr := e2SubsDelResp.UnPack(packedData) - if unpackerr != nil { - testError(t, "(%s) RIC_SUB_DEL_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error()) - } - geterr, resp := e2SubsDelResp.Get() - if geterr != nil { - testError(t, "(%s) RIC_SUB_DEL_RESP get failed err: %s", xappConn.desc, geterr.Error()) - } - xapp.Logger.Info("(%s) Recv Subs Del Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq) - return - } - case <-time.After(15 * time.Second): - testError(t, "(%s) Not Received RIC_SUB_DEL_RESP within 15 secs", xappConn.desc) - } -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (e2termConn *testingE2termControl) handle_e2term_subs_req(t *testing.T) (*e2ap.E2APSubscriptionRequest, *RMRParams) { - xapp.Logger.Info("(%s) handle_e2term_subs_req", e2termConn.desc) - e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest() - - //--------------------------------- - // e2term activity: Recv Subs Req - //--------------------------------- - select { - case msg := <-e2termConn.rmrConChan: - e2termConn.DecMsgCnt() - if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] { - testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.desc, "RIC_SUB_REQ", xapp.RicMessageTypeToName[msg.Mtype]) - } else { - xapp.Logger.Info("(%s) Recv Subs Req", e2termConn.desc) - packedData := &packer.PackedData{} - packedData.Buf = msg.Payload - unpackerr := e2SubsReq.UnPack(packedData) - if unpackerr != nil { - testError(t, "(%s) RIC_SUB_REQ unpack failed err: %s", e2termConn.desc, unpackerr.Error()) - } - geterr, req := e2SubsReq.Get() - if geterr != nil { - testError(t, "(%s) RIC_SUB_REQ get failed err: %s", e2termConn.desc, geterr.Error()) - } - return req, msg - } - case <-time.After(15 * time.Second): - testError(t, "(%s) Not Received RIC_SUB_REQ within 15 secs", e2termConn.desc) - } - return nil, nil -} - -func (e2termConn *testingE2termControl) handle_e2term_subs_resp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *RMRParams) { - xapp.Logger.Info("(%s) handle_e2term_subs_resp", e2termConn.desc) - e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse() - - //--------------------------------- - // e2term activity: Send Subs Resp - //--------------------------------- - xapp.Logger.Info("(%s) Send Subs Resp", e2termConn.desc) - - resp := &e2ap.E2APSubscriptionResponse{} - - resp.RequestId.Id = req.RequestId.Id - resp.RequestId.Seq = req.RequestId.Seq - resp.FunctionId = req.FunctionId - - resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups)) - for index := int(0); index < len(req.ActionSetups); index++ { - resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId - } - - for index := uint64(0); index < 1; index++ { - item := e2ap.ActionNotAdmittedItem{} - item.ActionId = index - item.Cause.Content = 1 - item.Cause.CauseVal = 1 - resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item) - } - - e2SubsResp.Set(resp) - xapp.Logger.Debug("%s", e2SubsResp.String()) - packerr, packedMsg := e2SubsResp.Pack(nil) - if packerr != nil { - testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) - } - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = xapp.RIC_SUB_RESP - //params.SubId = msg.SubId - params.SubId = -1 - params.Payload = packedMsg.Buf - params.Meid = msg.Meid - //params.Xid = msg.Xid - params.Mbuf = nil - - snderr := e2termConn.RmrSend(params) - if snderr != nil { - testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) - } -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -type test_subs_fail_params struct { - req *e2ap.E2APSubscriptionRequest - fail *e2ap.E2APSubscriptionFailure -} - -func (p *test_subs_fail_params) Set(req *e2ap.E2APSubscriptionRequest) { - p.req = req - - p.fail = &e2ap.E2APSubscriptionFailure{} - p.fail.RequestId.Id = p.req.RequestId.Id - p.fail.RequestId.Seq = p.req.RequestId.Seq - p.fail.FunctionId = p.req.FunctionId - p.fail.ActionNotAdmittedList.Items = make([]e2ap.ActionNotAdmittedItem, len(p.req.ActionSetups)) - for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ { - p.fail.ActionNotAdmittedList.Items[index].ActionId = p.req.ActionSetups[index].ActionId - p.SetCauseVal(index, 5, 1) - } -} - -func (p *test_subs_fail_params) SetCauseVal(ind int, content uint8, causeval uint8) { - - if ind < 0 { - for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ { - p.fail.ActionNotAdmittedList.Items[index].Cause.Content = content - p.fail.ActionNotAdmittedList.Items[index].Cause.CauseVal = causeval - } - return - } - p.fail.ActionNotAdmittedList.Items[ind].Cause.Content = content - p.fail.ActionNotAdmittedList.Items[ind].Cause.CauseVal = causeval -} - -func (e2termConn *testingE2termControl) handle_e2term_subs_fail(t *testing.T, fparams *test_subs_fail_params, msg *RMRParams) { - xapp.Logger.Info("(%s) handle_e2term_subs_fail", e2termConn.desc) - e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure() - - //--------------------------------- - // e2term activity: Send Subs Fail - //--------------------------------- - xapp.Logger.Info("(%s) Send Subs Fail", e2termConn.desc) - - e2SubsFail.Set(fparams.fail) - xapp.Logger.Debug("%s", e2SubsFail.String()) - packerr, packedMsg := e2SubsFail.Pack(nil) - if packerr != nil { - testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) - } - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = xapp.RIC_SUB_FAILURE - params.SubId = msg.SubId - params.Payload = packedMsg.Buf - params.Meid = msg.Meid - params.Xid = msg.Xid - params.Mbuf = nil - - snderr := e2termConn.RmrSend(params) - if snderr != nil { - testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) - } -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (e2termConn *testingE2termControl) handle_e2term_subs_del_req(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *RMRParams) { - xapp.Logger.Info("(%s) handle_e2term_subs_del_req", e2termConn.desc) - e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest() - - //--------------------------------- - // e2term activity: Recv Subs Del Req - //--------------------------------- - select { - case msg := <-e2termConn.rmrConChan: - e2termConn.DecMsgCnt() - if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] { - testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.desc, "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype]) - } else { - xapp.Logger.Info("(%s) Recv Subs Del Req", e2termConn.desc) - - packedData := &packer.PackedData{} - packedData.Buf = msg.Payload - unpackerr := e2SubsDelReq.UnPack(packedData) - if unpackerr != nil { - testError(t, "(%s) RIC_SUB_DEL_REQ unpack failed err: %s", e2termConn.desc, unpackerr.Error()) - } - geterr, req := e2SubsDelReq.Get() - if geterr != nil { - testError(t, "(%s) RIC_SUB_DEL_REQ get failed err: %s", e2termConn.desc, geterr.Error()) - } - return req, msg - } - case <-time.After(15 * time.Second): - testError(t, "(%s) Not Received RIC_SUB_DEL_REQ within 15 secs", e2termConn.desc) - } - return nil, nil -} - -func handle_e2term_recv_empty() bool { - if len(e2termConn.rmrConChan) > 0 { - return false - } - return true -} - -func (e2termConn *testingE2termControl) handle_e2term_subs_del_resp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) { - xapp.Logger.Info("(%s) handle_e2term_subs_del_resp", e2termConn.desc) - e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse() - - //--------------------------------- - // e2term activity: Send Subs Del Resp - //--------------------------------- - xapp.Logger.Info("(%s) Send Subs Del Resp", e2termConn.desc) - - resp := &e2ap.E2APSubscriptionDeleteResponse{} - resp.RequestId.Id = req.RequestId.Id - resp.RequestId.Seq = req.RequestId.Seq - resp.FunctionId = req.FunctionId - - e2SubsDelResp.Set(resp) - xapp.Logger.Debug("%s", e2SubsDelResp.String()) - packerr, packedMsg := e2SubsDelResp.Pack(nil) - if packerr != nil { - testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) - } - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = xapp.RIC_SUB_DEL_RESP - params.SubId = msg.SubId - params.Payload = packedMsg.Buf - params.Meid = msg.Meid - params.Xid = msg.Xid - params.Mbuf = nil - - snderr := e2termConn.RmrSend(params) - if snderr != nil { - testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) - } -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (e2termConn *testingE2termControl) handle_e2term_subs_del_fail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) { - xapp.Logger.Info("(%s) handle_e2term_del_subs_fail", e2termConn.desc) - e2SubsDelFail := e2asnpacker.NewPackerSubscriptionDeleteFailure() - - //--------------------------------- - // e2term activity: Send Subs Del Fail - //--------------------------------- - xapp.Logger.Info("(%s) Send Subs Del Fail", e2termConn.desc) - - resp := &e2ap.E2APSubscriptionDeleteFailure{} - resp.RequestId.Id = req.RequestId.Id - resp.RequestId.Seq = req.RequestId.Seq - resp.FunctionId = req.FunctionId - resp.Cause.Content = 3 // CauseMisc - resp.Cause.CauseVal = 4 // unspecified - - e2SubsDelFail.Set(resp) - xapp.Logger.Debug("%s", e2SubsDelFail.String()) - packerr, packedMsg := e2SubsDelFail.Pack(nil) - if packerr != nil { - testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) - } - - params := &RMRParams{&xapp.RMRParams{}} - params.Mtype = xapp.RIC_SUB_DEL_FAILURE - params.SubId = msg.SubId - params.Payload = packedMsg.Buf - params.Meid = msg.Meid - params.Xid = msg.Xid - params.Mbuf = nil - - snderr := e2termConn.RmrSend(params) - if snderr != nil { - testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) - } -} - -//----------------------------------------------------------------------------- -// -//----------------------------------------------------------------------------- -func (mc *testingMainControl) wait_subs_clean(t *testing.T, e2SubsId int, secs int) bool { - var subs *Subscription - i := 1 - for ; i <= secs*2; i++ { - subs = mc.c.registry.GetSubscription(uint16(e2SubsId)) - if subs == nil { - return true - } - time.Sleep(500 * time.Millisecond) - } - if subs != nil { - testError(t, "(general) no clean within %d secs: %s", secs, subs.String()) - } else { - testError(t, "(general) no clean within %d secs: subs(N/A)", secs) - } - return false -} - -func (mc *testingMainControl) wait_subs_trans_clean(t *testing.T, e2SubsId int, secs int) bool { - var trans *Transaction - i := 1 - for ; i <= secs*2; i++ { - subs := mc.c.registry.GetSubscription(uint16(e2SubsId)) - if subs == nil { - return true - } - trans = subs.GetTransaction() - if trans == nil { - return true - } - time.Sleep(500 * time.Millisecond) - } - if trans != nil { - testError(t, "(general) no clean within %d secs: %s", secs, trans.String()) - } else { - testError(t, "(general) no clean within %d secs: trans(N/A)", secs) - } - return false -} - -func (mc *testingMainControl) get_subid(t *testing.T) uint16 { - mc.c.registry.mutex.Lock() - defer mc.c.registry.mutex.Unlock() - return mc.c.registry.subIds[0] -} - -func (mc *testingMainControl) wait_subid_change(t *testing.T, origSubId uint16, secs int) (uint16, bool) { - i := 1 - for ; i <= secs*2; i++ { - mc.c.registry.mutex.Lock() - currSubId := mc.c.registry.subIds[0] - mc.c.registry.mutex.Unlock() - if currSubId != origSubId { - return currSubId, true - } - time.Sleep(500 * time.Millisecond) - } - testError(t, "(general) no subId change within %d secs", secs) - return 0, false -} - -func (mc *testingMainControl) get_msgcounter(t *testing.T) uint64 { - return mc.c.msgCounter -} - -func (mc *testingMainControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) { - i := 1 - for ; i <= secs*2; i++ { - curr := mc.c.msgCounter - if curr != orig { - return curr, true - } - time.Sleep(500 * time.Millisecond) - } - testError(t, "(general) no msg counter change within %d secs", secs) - return 0, false -} - //----------------------------------------------------------------------------- // TestSubReqAndRouteNok // diff --git a/pkg/control/ut_stub_e2term_test.go b/pkg/control/ut_stub_e2term_test.go new file mode 100644 index 0000000..18a6264 --- /dev/null +++ b/pkg/control/ut_stub_e2term_test.go @@ -0,0 +1,337 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 2019 Nokia + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +================================================================================== +*/ + +package control + +import ( + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper" + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "testing" + "time" +) + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +var e2t_e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer() + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type testingE2termStub struct { + testingRmrStubControl +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func createNewE2termStub(desc string, rtfile string, port string, stat string) *testingE2termStub { + e2termCtrl := &testingE2termStub{} + e2termCtrl.testingRmrStubControl.init(desc, rtfile, port, stat, e2termCtrl) + return e2termCtrl +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (tc *testingE2termStub) Consume(params *xapp.RMRParams) (err error) { + xapp.Rmr.Free(params.Mbuf) + params.Mbuf = nil + msg := &RMRParams{params} + + if params.Mtype == 55555 { + xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String()) + tc.active = true + return + } + + xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String()) + tc.rmrConChan <- msg + return +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (e2termConn *testingE2termStub) handle_e2term_subs_req(t *testing.T) (*e2ap.E2APSubscriptionRequest, *RMRParams) { + xapp.Logger.Info("(%s) handle_e2term_subs_req", e2termConn.desc) + e2SubsReq := e2t_e2asnpacker.NewPackerSubscriptionRequest() + + //--------------------------------- + // e2term activity: Recv Subs Req + //--------------------------------- + select { + case msg := <-e2termConn.rmrConChan: + e2termConn.DecMsgCnt() + if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] { + testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.desc, "RIC_SUB_REQ", xapp.RicMessageTypeToName[msg.Mtype]) + } else { + xapp.Logger.Info("(%s) Recv Subs Req", e2termConn.desc) + packedData := &packer.PackedData{} + packedData.Buf = msg.Payload + unpackerr := e2SubsReq.UnPack(packedData) + if unpackerr != nil { + testError(t, "(%s) RIC_SUB_REQ unpack failed err: %s", e2termConn.desc, unpackerr.Error()) + } + geterr, req := e2SubsReq.Get() + if geterr != nil { + testError(t, "(%s) RIC_SUB_REQ get failed err: %s", e2termConn.desc, geterr.Error()) + } + return req, msg + } + case <-time.After(15 * time.Second): + testError(t, "(%s) Not Received RIC_SUB_REQ within 15 secs", e2termConn.desc) + } + return nil, nil +} + +func (e2termConn *testingE2termStub) handle_e2term_subs_resp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *RMRParams) { + xapp.Logger.Info("(%s) handle_e2term_subs_resp", e2termConn.desc) + e2SubsResp := e2t_e2asnpacker.NewPackerSubscriptionResponse() + + //--------------------------------- + // e2term activity: Send Subs Resp + //--------------------------------- + xapp.Logger.Info("(%s) Send Subs Resp", e2termConn.desc) + + resp := &e2ap.E2APSubscriptionResponse{} + + resp.RequestId.Id = req.RequestId.Id + resp.RequestId.Seq = req.RequestId.Seq + resp.FunctionId = req.FunctionId + + resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups)) + for index := int(0); index < len(req.ActionSetups); index++ { + resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId + } + + for index := uint64(0); index < 1; index++ { + item := e2ap.ActionNotAdmittedItem{} + item.ActionId = index + item.Cause.Content = 1 + item.Cause.CauseVal = 1 + resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item) + } + + e2SubsResp.Set(resp) + xapp.Logger.Debug("%s", e2SubsResp.String()) + packerr, packedMsg := e2SubsResp.Pack(nil) + if packerr != nil { + testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) + } + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = xapp.RIC_SUB_RESP + //params.SubId = msg.SubId + params.SubId = -1 + params.Payload = packedMsg.Buf + params.Meid = msg.Meid + //params.Xid = msg.Xid + params.Mbuf = nil + + snderr := e2termConn.RmrSend(params) + if snderr != nil { + testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) + } +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type test_subs_fail_params struct { + req *e2ap.E2APSubscriptionRequest + fail *e2ap.E2APSubscriptionFailure +} + +func (p *test_subs_fail_params) Set(req *e2ap.E2APSubscriptionRequest) { + p.req = req + + p.fail = &e2ap.E2APSubscriptionFailure{} + p.fail.RequestId.Id = p.req.RequestId.Id + p.fail.RequestId.Seq = p.req.RequestId.Seq + p.fail.FunctionId = p.req.FunctionId + p.fail.ActionNotAdmittedList.Items = make([]e2ap.ActionNotAdmittedItem, len(p.req.ActionSetups)) + for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ { + p.fail.ActionNotAdmittedList.Items[index].ActionId = p.req.ActionSetups[index].ActionId + p.SetCauseVal(index, 5, 1) + } +} + +func (p *test_subs_fail_params) SetCauseVal(ind int, content uint8, causeval uint8) { + + if ind < 0 { + for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ { + p.fail.ActionNotAdmittedList.Items[index].Cause.Content = content + p.fail.ActionNotAdmittedList.Items[index].Cause.CauseVal = causeval + } + return + } + p.fail.ActionNotAdmittedList.Items[ind].Cause.Content = content + p.fail.ActionNotAdmittedList.Items[ind].Cause.CauseVal = causeval +} + +func (e2termConn *testingE2termStub) handle_e2term_subs_fail(t *testing.T, fparams *test_subs_fail_params, msg *RMRParams) { + xapp.Logger.Info("(%s) handle_e2term_subs_fail", e2termConn.desc) + e2SubsFail := e2t_e2asnpacker.NewPackerSubscriptionFailure() + + //--------------------------------- + // e2term activity: Send Subs Fail + //--------------------------------- + xapp.Logger.Info("(%s) Send Subs Fail", e2termConn.desc) + + e2SubsFail.Set(fparams.fail) + xapp.Logger.Debug("%s", e2SubsFail.String()) + packerr, packedMsg := e2SubsFail.Pack(nil) + if packerr != nil { + testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) + } + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = xapp.RIC_SUB_FAILURE + params.SubId = msg.SubId + params.Payload = packedMsg.Buf + params.Meid = msg.Meid + params.Xid = msg.Xid + params.Mbuf = nil + + snderr := e2termConn.RmrSend(params) + if snderr != nil { + testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) + } +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (e2termConn *testingE2termStub) handle_e2term_subs_del_req(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *RMRParams) { + xapp.Logger.Info("(%s) handle_e2term_subs_del_req", e2termConn.desc) + e2SubsDelReq := e2t_e2asnpacker.NewPackerSubscriptionDeleteRequest() + + //--------------------------------- + // e2term activity: Recv Subs Del Req + //--------------------------------- + select { + case msg := <-e2termConn.rmrConChan: + e2termConn.DecMsgCnt() + if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] { + testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.desc, "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype]) + } else { + xapp.Logger.Info("(%s) Recv Subs Del Req", e2termConn.desc) + + packedData := &packer.PackedData{} + packedData.Buf = msg.Payload + unpackerr := e2SubsDelReq.UnPack(packedData) + if unpackerr != nil { + testError(t, "(%s) RIC_SUB_DEL_REQ unpack failed err: %s", e2termConn.desc, unpackerr.Error()) + } + geterr, req := e2SubsDelReq.Get() + if geterr != nil { + testError(t, "(%s) RIC_SUB_DEL_REQ get failed err: %s", e2termConn.desc, geterr.Error()) + } + return req, msg + } + case <-time.After(15 * time.Second): + testError(t, "(%s) Not Received RIC_SUB_DEL_REQ within 15 secs", e2termConn.desc) + } + return nil, nil +} + +func handle_e2term_recv_empty() bool { + if len(e2termConn.rmrConChan) > 0 { + return false + } + return true +} + +func (e2termConn *testingE2termStub) handle_e2term_subs_del_resp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) { + xapp.Logger.Info("(%s) handle_e2term_subs_del_resp", e2termConn.desc) + e2SubsDelResp := e2t_e2asnpacker.NewPackerSubscriptionDeleteResponse() + + //--------------------------------- + // e2term activity: Send Subs Del Resp + //--------------------------------- + xapp.Logger.Info("(%s) Send Subs Del Resp", e2termConn.desc) + + resp := &e2ap.E2APSubscriptionDeleteResponse{} + resp.RequestId.Id = req.RequestId.Id + resp.RequestId.Seq = req.RequestId.Seq + resp.FunctionId = req.FunctionId + + e2SubsDelResp.Set(resp) + xapp.Logger.Debug("%s", e2SubsDelResp.String()) + packerr, packedMsg := e2SubsDelResp.Pack(nil) + if packerr != nil { + testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) + } + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = xapp.RIC_SUB_DEL_RESP + params.SubId = msg.SubId + params.Payload = packedMsg.Buf + params.Meid = msg.Meid + params.Xid = msg.Xid + params.Mbuf = nil + + snderr := e2termConn.RmrSend(params) + if snderr != nil { + testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) + } +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (e2termConn *testingE2termStub) handle_e2term_subs_del_fail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) { + xapp.Logger.Info("(%s) handle_e2term_del_subs_fail", e2termConn.desc) + e2SubsDelFail := e2t_e2asnpacker.NewPackerSubscriptionDeleteFailure() + + //--------------------------------- + // e2term activity: Send Subs Del Fail + //--------------------------------- + xapp.Logger.Info("(%s) Send Subs Del Fail", e2termConn.desc) + + resp := &e2ap.E2APSubscriptionDeleteFailure{} + resp.RequestId.Id = req.RequestId.Id + resp.RequestId.Seq = req.RequestId.Seq + resp.FunctionId = req.FunctionId + resp.Cause.Content = 3 // CauseMisc + resp.Cause.CauseVal = 4 // unspecified + + e2SubsDelFail.Set(resp) + xapp.Logger.Debug("%s", e2SubsDelFail.String()) + packerr, packedMsg := e2SubsDelFail.Pack(nil) + if packerr != nil { + testError(t, "(%s) pack NOK %s", e2termConn.desc, packerr.Error()) + } + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = xapp.RIC_SUB_DEL_FAILURE + params.SubId = msg.SubId + params.Payload = packedMsg.Buf + params.Meid = msg.Meid + params.Xid = msg.Xid + params.Mbuf = nil + + snderr := e2termConn.RmrSend(params) + if snderr != nil { + testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.desc, snderr.Error()) + } +} diff --git a/pkg/control/ut_stub_rtmgr_test.go b/pkg/control/ut_stub_rtmgr_test.go new file mode 100644 index 0000000..bdca293 --- /dev/null +++ b/pkg/control/ut_stub_rtmgr_test.go @@ -0,0 +1,134 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 2019 Nokia + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +================================================================================== +*/ + +package control + +import ( + "encoding/json" + "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "net/http" + "sync" + "testing" + "time" +) + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +type httpEventWaiter struct { + resultChan chan bool + nextActionOk bool +} + +func (msg *httpEventWaiter) SetResult(res bool) { + msg.resultChan <- res +} + +func (msg *httpEventWaiter) WaitResult(t *testing.T) bool { + select { + case result := <-msg.resultChan: + return result + case <-time.After(15 * time.Second): + testError(t, "Waiter not received result status from case within 15 secs") + return false + } + testError(t, "Waiter error in default branch") + return false +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type testingHttpRtmgrStub struct { + sync.Mutex + desc string + port string + eventWaiter *httpEventWaiter +} + +func (hc *testingHttpRtmgrStub) NextEvent(eventWaiter *httpEventWaiter) { + hc.Lock() + defer hc.Unlock() + hc.eventWaiter = eventWaiter +} + +func (hc *testingHttpRtmgrStub) AllocNextEvent(nextAction bool) *httpEventWaiter { + eventWaiter := &httpEventWaiter{ + resultChan: make(chan bool), + nextActionOk: nextAction, + } + hc.NextEvent(eventWaiter) + return eventWaiter +} + +func (hc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Request) { + + hc.Lock() + defer hc.Unlock() + + var req rtmgr_models.XappSubscriptionData + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + xapp.Logger.Error("%s", err.Error()) + } + xapp.Logger.Info("(%s) handling Address=%s Port=%d SubscriptionID=%d", hc.desc, *req.Address, *req.Port, *req.SubscriptionID) + + var code int = 0 + switch r.Method { + case http.MethodPost: + code = 201 + if hc.eventWaiter != nil { + if hc.eventWaiter.nextActionOk == false { + code = 400 + } + } + case http.MethodDelete: + code = 200 + if hc.eventWaiter != nil { + if hc.eventWaiter.nextActionOk == false { + code = 400 + } + } + default: + code = 200 + } + + waiter := hc.eventWaiter + hc.eventWaiter = nil + if waiter != nil { + waiter.SetResult(true) + } + xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code) + w.WriteHeader(code) + +} + +func (hc *testingHttpRtmgrStub) run() { + http.HandleFunc("/", hc.http_handler) + http.ListenAndServe("localhost:"+hc.port, nil) +} + +func createNewHttpRtmgrStub(desc string, port string) *testingHttpRtmgrStub { + hc := &testingHttpRtmgrStub{} + hc.desc = desc + hc.port = port + return hc +} diff --git a/pkg/control/ut_stub_xapp_test.go b/pkg/control/ut_stub_xapp_test.go new file mode 100644 index 0000000..a524d9b --- /dev/null +++ b/pkg/control/ut_stub_xapp_test.go @@ -0,0 +1,350 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 2019 Nokia + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +================================================================================== +*/ + +package control + +import ( + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper" + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "strconv" + "strings" + "testing" + "time" +) + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +var xapp_e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer() + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type xappTransaction struct { + tc *testingXappStub + xid string + meid *xapp.RMRMeid +} + +type testingXappStub struct { + testingRmrStubControl + xid_seq uint64 +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func createNewXappStub(desc string, rtfile string, port string, stat string) *testingXappStub { + xappCtrl := &testingXappStub{} + xappCtrl.testingRmrStubControl.init(desc, rtfile, port, stat, xappCtrl) + xappCtrl.xid_seq = 1 + return xappCtrl +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (tc *testingXappStub) newXid() string { + var xid string + xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10) + tc.xid_seq++ + return xid +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (tc *testingXappStub) newXappTransaction(xid *string, ranname string) *xappTransaction { + trans := &xappTransaction{} + trans.tc = tc + if xid == nil { + trans.xid = tc.newXid() + } else { + trans.xid = *xid + } + trans.meid = &xapp.RMRMeid{RanName: ranname} + return trans +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (tc *testingXappStub) Consume(params *xapp.RMRParams) (err error) { + xapp.Rmr.Free(params.Mbuf) + params.Mbuf = nil + msg := &RMRParams{params} + + if params.Mtype == 55555 { + xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String()) + tc.active = true + return + } + + if strings.Contains(msg.Xid, tc.desc) { + xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String()) + tc.IncMsgCnt() + tc.rmrConChan <- msg + } else { + xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String()) + } + return +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (xappConn *testingXappStub) handle_xapp_subs_req(t *testing.T, oldTrans *xappTransaction) *xappTransaction { + xapp.Logger.Info("(%s) handle_xapp_subs_req", xappConn.desc) + e2SubsReq := xapp_e2asnpacker.NewPackerSubscriptionRequest() + + //--------------------------------- + // xapp activity: Send Subs Req + //--------------------------------- + xapp.Logger.Info("(%s) Send Subs Req", xappConn.desc) + + req := &e2ap.E2APSubscriptionRequest{} + + req.RequestId.Id = 1 + req.RequestId.Seq = 0 + req.FunctionId = 1 + + req.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present = true + req.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.StringPut("310150") + req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Id = 123 + req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Bits = e2ap.E2AP_ENBIDHomeBits28 + + // gnb -> enb outgoing + // enb -> gnb incoming + // X2 36423-f40.doc + req.EventTriggerDefinition.InterfaceDirection = e2ap.E2AP_InterfaceDirectionIncoming + req.EventTriggerDefinition.ProcedureCode = 5 //28 35 + req.EventTriggerDefinition.TypeOfMessage = e2ap.E2AP_InitiatingMessage + + req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1) + req.ActionSetups[0].ActionId = 0 + req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport + req.ActionSetups[0].ActionDefinition.Present = false + //req.ActionSetups[index].ActionDefinition.StyleId = 255 + //req.ActionSetups[index].ActionDefinition.ParamId = 222 + req.ActionSetups[0].SubsequentAction.Present = true + req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue + req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero + + e2SubsReq.Set(req) + xapp.Logger.Debug("%s", e2SubsReq.String()) + err, packedMsg := e2SubsReq.Pack(nil) + if err != nil { + testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error()) + return nil + } + + var trans *xappTransaction = oldTrans + if trans == nil { + trans = xappConn.newXappTransaction(nil, "RAN_NAME_1") + } + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = xapp.RIC_SUB_REQ + params.SubId = -1 + params.Payload = packedMsg.Buf + params.Meid = trans.meid + params.Xid = trans.xid + params.Mbuf = nil + + snderr := xappConn.RmrSend(params) + if snderr != nil { + testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error()) + return nil + } + return trans +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (xappConn *testingXappStub) handle_xapp_subs_resp(t *testing.T, trans *xappTransaction) int { + xapp.Logger.Info("(%s) handle_xapp_subs_resp", xappConn.desc) + e2SubsResp := xapp_e2asnpacker.NewPackerSubscriptionResponse() + var e2SubsId int + + //--------------------------------- + // xapp activity: Recv Subs Resp + //--------------------------------- + select { + case msg := <-xappConn.rmrConChan: + xappConn.DecMsgCnt() + if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] { + testError(t, "(%s) Received RIC_SUB_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype]) + return -1 + } else if msg.Xid != trans.xid { + testError(t, "(%s) Received RIC_SUB_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid) + return -1 + } else { + packedData := &packer.PackedData{} + packedData.Buf = msg.Payload + e2SubsId = msg.SubId + unpackerr := e2SubsResp.UnPack(packedData) + + if unpackerr != nil { + testError(t, "(%s) RIC_SUB_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error()) + } + geterr, resp := e2SubsResp.Get() + if geterr != nil { + testError(t, "(%s) RIC_SUB_RESP get failed err: %s", xappConn.desc, geterr.Error()) + } + + xapp.Logger.Info("(%s) Recv Subs Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq) + return e2SubsId + } + case <-time.After(15 * time.Second): + testError(t, "(%s) Not Received RIC_SUB_RESP within 15 secs", xappConn.desc) + return -1 + } + return -1 +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (xappConn *testingXappStub) handle_xapp_subs_fail(t *testing.T, trans *xappTransaction) int { + xapp.Logger.Info("(%s) handle_xapp_subs_fail", xappConn.desc) + e2SubsFail := xapp_e2asnpacker.NewPackerSubscriptionFailure() + var e2SubsId int + + //------------------------------- + // xapp activity: Recv Subs Fail + //------------------------------- + select { + case msg := <-xappConn.rmrConChan: + xappConn.DecMsgCnt() + if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] { + testError(t, "(%s) Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype]) + return -1 + } else if msg.Xid != trans.xid { + testError(t, "(%s) Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid) + return -1 + } else { + packedData := &packer.PackedData{} + packedData.Buf = msg.Payload + e2SubsId = msg.SubId + unpackerr := e2SubsFail.UnPack(packedData) + + if unpackerr != nil { + testError(t, "(%s) RIC_SUB_FAILURE unpack failed err: %s", xappConn.desc, unpackerr.Error()) + } + geterr, resp := e2SubsFail.Get() + if geterr != nil { + testError(t, "(%s) RIC_SUB_FAILURE get failed err: %s", xappConn.desc, geterr.Error()) + } + + xapp.Logger.Info("(%s) Recv Subs Fail rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq) + return e2SubsId + } + case <-time.After(15 * time.Second): + testError(t, "(%s) Not Received RIC_SUB_FAILURE within 15 secs", xappConn.desc) + return -1 + } + return -1 +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (xappConn *testingXappStub) handle_xapp_subs_del_req(t *testing.T, oldTrans *xappTransaction, e2SubsId int) *xappTransaction { + xapp.Logger.Info("(%s) handle_xapp_subs_del_req", xappConn.desc) + e2SubsDelReq := xapp_e2asnpacker.NewPackerSubscriptionDeleteRequest() + + //--------------------------------- + // xapp activity: Send Subs Del Req + //--------------------------------- + xapp.Logger.Info("(%s) Send Subs Del Req", xappConn.desc) + + req := &e2ap.E2APSubscriptionDeleteRequest{} + req.RequestId.Id = 1 + req.RequestId.Seq = uint32(e2SubsId) + req.FunctionId = 1 + + e2SubsDelReq.Set(req) + xapp.Logger.Debug("%s", e2SubsDelReq.String()) + err, packedMsg := e2SubsDelReq.Pack(nil) + if err != nil { + testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error()) + return nil + } + + var trans *xappTransaction = oldTrans + if trans == nil { + trans = xappConn.newXappTransaction(nil, "RAN_NAME_1") + } + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = xapp.RIC_SUB_DEL_REQ + params.SubId = e2SubsId + params.Payload = packedMsg.Buf + params.Meid = trans.meid + params.Xid = trans.xid + params.Mbuf = nil + + snderr := xappConn.RmrSend(params) + if snderr != nil { + testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error()) + return nil + } + return trans +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func (xappConn *testingXappStub) handle_xapp_subs_del_resp(t *testing.T, trans *xappTransaction) { + xapp.Logger.Info("(%s) handle_xapp_subs_del_resp", xappConn.desc) + e2SubsDelResp := xapp_e2asnpacker.NewPackerSubscriptionDeleteResponse() + + //--------------------------------- + // xapp activity: Recv Subs Del Resp + //--------------------------------- + select { + case msg := <-xappConn.rmrConChan: + xappConn.DecMsgCnt() + if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] { + testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype]) + return + } else if msg.Xid != trans.xid { + testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid) + return + } else { + packedData := &packer.PackedData{} + packedData.Buf = msg.Payload + unpackerr := e2SubsDelResp.UnPack(packedData) + if unpackerr != nil { + testError(t, "(%s) RIC_SUB_DEL_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error()) + } + geterr, resp := e2SubsDelResp.Get() + if geterr != nil { + testError(t, "(%s) RIC_SUB_DEL_RESP get failed err: %s", xappConn.desc, geterr.Error()) + } + xapp.Logger.Info("(%s) Recv Subs Del Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq) + return + } + case <-time.After(15 * time.Second): + testError(t, "(%s) Not Received RIC_SUB_DEL_RESP within 15 secs", xappConn.desc) + } +} diff --git a/pkg/control/ut_test.go b/pkg/control/ut_test.go new file mode 100644 index 0000000..23e4374 --- /dev/null +++ b/pkg/control/ut_test.go @@ -0,0 +1,331 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 2019 Nokia + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +================================================================================== +*/ + +package control + +import ( + "fmt" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "io/ioutil" + "os" + "strings" + "testing" + "time" +) + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type testingRmrControl struct { + desc string + syncChan chan struct{} +} + +func (tc *testingRmrControl) ReadyCB(data interface{}) { + xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.desc) + tc.syncChan <- struct{}{} + return +} + +func (tc *testingRmrControl) WaitCB() { + <-tc.syncChan +} + +func (tc *testingRmrControl) init(desc string, rtfile string, port string) { + os.Setenv("RMR_SEED_RT", rtfile) + os.Setenv("RMR_SRC_ID", "localhost:"+port) + xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT")) + xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID")) + tc.desc = strings.ToUpper(desc) + tc.syncChan = make(chan struct{}) +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type testingRmrStubControl struct { + testingRmrControl + rmrConChan chan *RMRParams + rmrClientTest *xapp.RMRClient + active bool + msgCnt uint64 +} + +func (tc *testingRmrStubControl) GetMsgCnt() uint64 { + return tc.msgCnt +} + +func (tc *testingRmrStubControl) IncMsgCnt() { + tc.msgCnt++ +} + +func (tc *testingRmrStubControl) DecMsgCnt() { + if tc.msgCnt > 0 { + tc.msgCnt-- + } +} + +func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) { + if tc.GetMsgCnt() > 0 { + testError(t, "(%s) message count expected 0 but is %d", tc.desc, tc.GetMsgCnt()) + } +} + +func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) { + // + //NOTE: Do this way until xapp-frame sending is improved + // + xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String()) + status := false + i := 1 + for ; i <= 10 && status == false; i++ { + status = tc.rmrClientTest.SendMsg(params.RMRParams) + if status == false { + xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String()) + time.Sleep(500 * time.Millisecond) + } + } + if status == false { + err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String()) + xapp.Rmr.Free(params.Mbuf) + } + return +} + +func (tc *testingRmrStubControl) init(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) { + tc.active = false + tc.testingRmrControl.init(desc, rtfile, port) + tc.rmrConChan = make(chan *RMRParams) + tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat) + tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil) + go tc.rmrClientTest.Start(consumer) + tc.WaitCB() + allRmrStubs = append(allRmrStubs, tc) +} + +var allRmrStubs []*testingRmrStubControl + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +func testError(t *testing.T, pattern string, args ...interface{}) { + xapp.Logger.Error(fmt.Sprintf(pattern, args...)) + t.Errorf(fmt.Sprintf(pattern, args...)) +} + +func testLog(t *testing.T, pattern string, args ...interface{}) { + xapp.Logger.Info(fmt.Sprintf(pattern, args...)) + t.Logf(fmt.Sprintf(pattern, args...)) +} + +func testCreateTmpFile(str string) (string, error) { + file, err := ioutil.TempFile("/tmp", "*.rt") + if err != nil { + return "", err + } + _, err = file.WriteString(str) + if err != nil { + file.Close() + return "", err + } + return file.Name(), nil +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +var xappConn1 *testingXappStub +var xappConn2 *testingXappStub +var e2termConn *testingE2termStub +var rtmgrHttp *testingHttpRtmgrStub +var mainCtrl *testingSubmgrControl + +func ut_test_init() { + xapp.Logger.Info("ut_test_init") + + //--------------------------------- + // + //--------------------------------- + rtmgrHttp = createNewHttpRtmgrStub("RTMGRSTUB", "8989") + go rtmgrHttp.run() + + //--------------------------------- + // + //--------------------------------- + + // + //Cfg creation won't work like this as xapp-frame reads it during init. + // + /* + cfgstr:=`{ + "local": { + "host": ":8080" + }, + "logger": { + "level": 4 + }, + "rmr": { + "protPort": "tcp:14560", + "maxSize": 4096, + "numWorkers": 1, + "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"], + "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"] + }, + "db": { + "host": "localhost", + "port": 6379, + "namespaces": ["sdl", "rnib"] + }, + "rtmgr" : { + "HostAddr" : "localhost", + "port" : "8989", + "baseUrl" : "/" + } + ` + + cfgfilename,_ := testCreateTmpFile(cfgstr) + defer os.Remove(cfgfilename) + os.Setenv("CFG_FILE", cfgfilename) + */ + xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE")) + + //--------------------------------- + // Static routetable for rmr + // + // NOTE: Routing table is configured so, that responses + // are duplicated to xapp1 and xapp2 instances. + // If XID is not matching xapp stub will just + // drop message. (Messages 12011, 12012, 12021, 12022) + // + // NOTE2: 55555 message type is for stub rmr connectivity probing + // + // NOTE3: Ports per entity: + // + // Port Entity + // ------------------- + // 14560 submgr + // 15560 e2term stub + // 13560 xapp1 stub + // 13660 xapp2 stub + // + //--------------------------------- + + allrt := `newrt|start +mse|12010|-1|localhost:14560 +mse|12010,localhost:14560|-1|localhost:15560 +mse|12011,localhost:15560|-1|localhost:14560 +mse|12012,localhost:15560|-1|localhost:14560 +mse|12011,localhost:14560|-1|localhost:13660;localhost:13560 +mse|12012,localhost:14560|-1|localhost:13660;localhost:13560 +mse|12020|-1|localhost:14560 +mse|12020,localhost:14560|-1|localhost:15560 +mse|12021,localhost:15560|-1|localhost:14560 +mse|12022,localhost:15560|-1|localhost:14560 +mse|12021,localhost:14560|-1|localhost:13660;localhost:13560 +mse|12022,localhost:14560|-1|localhost:13660;localhost:13560 +mse|55555|-1|localhost:13660;localhost:13560,localhost:15560 +newrt|end +` + + //--------------------------------- + // + //--------------------------------- + xapp.Logger.Info("### submgr ctrl run ###") + subsrt := allrt + subrtfilename, _ := testCreateTmpFile(subsrt) + defer os.Remove(subrtfilename) + mainCtrl = createSubmgrControl("main", subrtfilename, "14560") + + //--------------------------------- + // + //--------------------------------- + xapp.Logger.Info("### xapp1 stub run ###") + xapprt1 := allrt + xapprtfilename1, _ := testCreateTmpFile(xapprt1) + defer os.Remove(xapprtfilename1) + xappConn1 = createNewXappStub("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB") + + //--------------------------------- + // + //--------------------------------- + xapp.Logger.Info("### xapp2 stub run ###") + xapprt2 := allrt + xapprtfilename2, _ := testCreateTmpFile(xapprt2) + defer os.Remove(xapprtfilename2) + xappConn2 = createNewXappStub("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB") + + //--------------------------------- + // + //--------------------------------- + xapp.Logger.Info("### e2term stub run ###") + e2termrt := allrt + e2termrtfilename, _ := testCreateTmpFile(e2termrt) + defer os.Remove(e2termrtfilename) + e2termConn = createNewE2termStub("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB") + + //--------------------------------- + // Testing message sending + //--------------------------------- + var dummyBuf []byte = make([]byte, 100) + + params := &RMRParams{&xapp.RMRParams{}} + params.Mtype = 55555 + params.SubId = -1 + params.Payload = dummyBuf + params.PayloadLen = 100 + params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"} + params.Xid = "THISISTESTFORSTUBS" + params.Mbuf = nil + + status := false + i := 1 + for ; i <= 10 && status == false; i++ { + xapp.Rmr.Send(params.RMRParams, false) + + status = true + for _, val := range allRmrStubs { + if val.active == false { + status = false + break + } + } + if status == true { + break + } + xapp.Logger.Info("Sleep 0.5 secs and try routes again") + time.Sleep(500 * time.Millisecond) + } + + if status == false { + xapp.Logger.Error("Could not initialize routes") + os.Exit(1) + } +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +func TestMain(m *testing.M) { + xapp.Logger.Info("TestMain start") + ut_test_init() + code := m.Run() + os.Exit(code) +}