2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
26 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 type testingControl struct {
41 syncChan chan struct{}
44 func (tc *testingControl) ReadyCB(data interface{}) {
45 xapp.Logger.Info("testingControl(%s) ReadyCB", tc.desc)
46 tc.syncChan <- struct{}{}
50 func (tc *testingControl) WaitCB() {
54 func initTestingControl(desc string, rtfile string, port string) testingControl {
55 tc := testingControl{}
56 os.Setenv("RMR_SEED_RT", rtfile)
57 os.Setenv("RMR_SRC_ID", "localhost:"+port)
58 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
59 xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
60 tc.desc = strings.ToUpper(desc)
61 tc.syncChan = make(chan struct{})
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 type testingRmrControl struct {
70 rmrClientTest *xapp.RMRClient
74 func (tc *testingRmrControl) RmrSend(params *RMRParams) (err error) {
76 //NOTE: Do this way until xapp-frame sending is improved
78 xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String())
81 for ; i <= 10 && status == false; i++ {
82 status = tc.rmrClientTest.SendMsg(params.RMRParams)
84 xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
85 time.Sleep(500 * time.Millisecond)
89 err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
90 xapp.Rmr.Free(params.Mbuf)
95 func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrControl {
96 tc := testingRmrControl{}
98 tc.testingControl = initTestingControl(desc, rtfile, port)
99 tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
100 tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
101 go tc.rmrClientTest.Start(consumer)
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
109 type testingMessageChannel struct {
110 rmrConChan chan *RMRParams
113 func initTestingMessageChannel() testingMessageChannel {
114 mc := testingMessageChannel{}
115 mc.rmrConChan = make(chan *RMRParams)
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
122 type xappTransaction struct {
123 tc *testingXappControl
128 type testingXappControl struct {
130 testingMessageChannel
134 func (tc *testingXappControl) newXid() string {
136 xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
141 func (tc *testingXappControl) newXappTransaction(xid *string, ranname string) *xappTransaction {
142 trans := &xappTransaction{}
145 trans.xid = tc.newXid()
149 trans.meid = &xapp.RMRMeid{RanName: ranname}
153 func (tc *testingXappControl) Consume(params *xapp.RMRParams) (err error) {
154 xapp.Rmr.Free(params.Mbuf)
156 msg := &RMRParams{params}
158 if params.Mtype == 55555 {
159 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
164 if strings.Contains(msg.Xid, tc.desc) {
165 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
168 xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String())
173 func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl {
174 xappCtrl := &testingXappControl{}
175 xappCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
176 xappCtrl.testingMessageChannel = initTestingMessageChannel()
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 type testingE2termControl struct {
186 testingMessageChannel
189 func (tc *testingE2termControl) Consume(params *xapp.RMRParams) (err error) {
190 xapp.Rmr.Free(params.Mbuf)
192 msg := &RMRParams{params}
194 if params.Mtype == 55555 {
195 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
200 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
205 func createNewE2termControl(desc string, rtfile string, port string, stat string) *testingE2termControl {
206 e2termCtrl := &testingE2termControl{}
207 e2termCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl)
208 e2termCtrl.testingMessageChannel = initTestingMessageChannel()
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
215 type testingMainControl struct {
220 func createNewMainControl(desc string, rtfile string, port string) *testingMainControl {
221 mainCtrl = &testingMainControl{}
222 mainCtrl.testingControl = initTestingControl(desc, rtfile, port)
223 mainCtrl.c = NewControl()
224 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
225 go xapp.RunWithParams(mainCtrl.c, false)
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 func testError(t *testing.T, pattern string, args ...interface{}) {
235 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
236 t.Errorf(fmt.Sprintf(pattern, args...))
239 func testLog(t *testing.T, pattern string, args ...interface{}) {
240 xapp.Logger.Info(fmt.Sprintf(pattern, args...))
241 t.Logf(fmt.Sprintf(pattern, args...))
244 func testCreateTmpFile(str string) (string, error) {
245 file, err := ioutil.TempFile("/tmp", "*.rt")
249 _, err = file.WriteString(str)
254 return file.Name(), nil
257 //-----------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------
261 var xappConn1 *testingXappControl
262 var xappConn2 *testingXappControl
263 var e2termConn *testingE2termControl
264 var mainCtrl *testingMainControl
266 func TestMain(m *testing.M) {
267 xapp.Logger.Info("TestMain start")
269 //---------------------------------
271 //---------------------------------
272 http_handler := func(w http.ResponseWriter, r *http.Request) {
273 var req rtmgr_models.XappSubscriptionData
274 err := json.NewDecoder(r.Body).Decode(&req)
276 xapp.Logger.Error("%s", err.Error())
278 xapp.Logger.Info("(http handler) handling Address=%s Port=%d SubscriptionID=%d", *req.Address, *req.Port, *req.SubscriptionID)
284 http.HandleFunc("/", http_handler)
285 http.ListenAndServe("localhost:8989", nil)
288 //---------------------------------
290 //---------------------------------
293 //Cfg creation won't work like this as xapp-frame reads it during init.
304 "protPort": "tcp:14560",
307 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
308 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
313 "namespaces": ["sdl", "rnib"]
316 "HostAddr" : "localhost",
322 cfgfilename,_ := testCreateTmpFile(cfgstr)
323 defer os.Remove(cfgfilename)
324 os.Setenv("CFG_FILE", cfgfilename)
326 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
328 //---------------------------------
329 // Static routetable for rmr
331 // NOTE: Routing table is configured so, that responses
332 // are duplicated to xapp1 and xapp2 instances.
333 // If XID is not matching xapp stub will just
334 // drop message. (Messages 12011, 12012, 12021, 12022)
341 //---------------------------------
343 allrt := `newrt|start
344 mse|12010|-1|localhost:14560
345 mse|12010,localhost:14560|-1|localhost:15560
346 mse|12011,localhost:15560|-1|localhost:14560
347 mse|12012,localhost:15560|-1|localhost:14560
348 mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
349 mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
350 mse|12020|-1|localhost:14560
351 mse|12020,localhost:14560|-1|localhost:15560
352 mse|12021,localhost:15560|-1|localhost:14560
353 mse|12022,localhost:15560|-1|localhost:14560
354 mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
355 mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
356 mse|55555|-1|localhost:13660;localhost:13560,localhost:15560
360 //---------------------------------
362 //---------------------------------
363 xapp.Logger.Info("### submgr main run ###")
367 subsrt := `newrt|start
368 mse|12010|-1|localhost:14560
369 mse|12010,localhost:14560|-1|localhost:15560
370 mse|12011,localhost:15560|-1|localhost:14560
371 mse|12011|-1|localhost:13560;localhost:13660
372 mse|12012,localhost:15560|-1|localhost:14560
373 mse|12012|-1|localhost:13560;localhost:13660
374 mse|12020|-1|localhost:14560
375 mse|12020,localhost:14560|-1|localhost:15560
376 mse|12021,localhost:15560|-1|localhost:14560
377 mse|12021|-1|localhost:13560;localhost:13660
378 mse|12022,localhost:15560|-1|localhost:14560
379 mse|12022|-1|localhost:13560;localhost:13660
384 subrtfilename, _ := testCreateTmpFile(subsrt)
385 defer os.Remove(subrtfilename)
386 mainCtrl = createNewMainControl("main", subrtfilename, "14560")
388 //---------------------------------
390 //---------------------------------
391 xapp.Logger.Info("### xapp1 rmr run ###")
395 xapprt1 := `newrt|start
396 mse|12010|-1|localhost:14560
397 mse|12011|-1|localhost:13560
398 mse|12012|-1|localhost:13560
399 mse|12020|-1|localhost:14560
400 mse|12021|-1|localhost:13560
401 mse|12022|-1|localhost:13560
406 xapprtfilename1, _ := testCreateTmpFile(xapprt1)
407 defer os.Remove(xapprtfilename1)
408 xappConn1 = createNewXappControl("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
410 //---------------------------------
412 //---------------------------------
414 xapp.Logger.Info("### xapp2 rmr run ###")
418 xapprt2 := `newrt|start
419 mse|12010|-1|localhost:14560
420 mse|12011|-1|localhost:13660
421 mse|12012|-1|localhost:13660
422 mse|12020|-1|localhost:14560
423 mse|12021|-1|localhost:13660
424 mse|12022|-1|localhost:13660
429 xapprtfilename2, _ := testCreateTmpFile(xapprt2)
430 defer os.Remove(xapprtfilename2)
431 xappConn2 = createNewXappControl("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
433 //---------------------------------
435 //---------------------------------
436 xapp.Logger.Info("### e2term rmr run ###")
440 e2termrt := `newrt|start
441 mse|12010|-1|localhost:15560
442 mse|12011|-1|localhost:14560
443 mse|12012|-1|localhost:14560
444 mse|12020|-1|localhost:15560
445 mse|12021|-1|localhost:14560
446 mse|12022|-1|localhost:14560
451 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
452 defer os.Remove(e2termrtfilename)
453 e2termConn = createNewE2termControl("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
455 //---------------------------------
456 // Testing message sending
457 //---------------------------------
458 var dummyBuf []byte = make([]byte, 100)
460 params := &RMRParams{&xapp.RMRParams{}}
463 params.Payload = dummyBuf
464 params.PayloadLen = 100
465 params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"}
466 params.Xid = "THISISTESTFORSTUBS"
471 for ; i <= 10 && status == false; i++ {
472 xapp.Rmr.Send(params.RMRParams, false)
473 if e2termConn.active == true && xappConn1.active == true && xappConn2.active == true {
477 xapp.Logger.Info("Sleep 0.5 secs and try routes again")
478 time.Sleep(500 * time.Millisecond)
483 xapp.Logger.Error("Could not initialize routes")
487 //---------------------------------
489 //---------------------------------