Updated xapp-frame to 0.4.18. Updated rmr to 4.1.2.
[ric-plt/submgr.git] / pkg / teststub / controlRmrStub.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19 package teststub
20
21 import (
22         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
23         "strconv"
24         "strings"
25         "testing"
26         "time"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type RmrStubControl struct {
33         RmrControl
34         *xapp.RMRClient
35         RecvChan chan *xapp.RMRParams
36         Active   bool
37         InitMsg  int
38         CheckXid bool
39 }
40
41 func (tc *RmrStubControl) SetActive() {
42         tc.Active = true
43 }
44
45 func (tc *RmrStubControl) IsActive() bool {
46         return tc.Active
47 }
48
49 func (tc *RmrStubControl) SetCheckXid(val bool) {
50         tc.CheckXid = val
51 }
52
53 func (tc *RmrStubControl) IsCheckXid() bool {
54         return tc.CheckXid
55 }
56
57 func (tc *RmrStubControl) IsChanEmpty() bool {
58         if len(tc.RecvChan) > 0 {
59                 return false
60         }
61         return true
62 }
63
64 func (tc *RmrStubControl) TestMsgChanEmpty(t *testing.T) {
65         if tc.IsChanEmpty() == false {
66                 tc.TestError(t, "message channel not empty")
67         }
68 }
69
70 func (tc *RmrStubControl) Init(desc string, srcId RmrSrcId, rtgSvc RmrRtgSvc, stat string, initMsg int) {
71         tc.InitMsg = initMsg
72         tc.Active = false
73         tc.RecvChan = make(chan *xapp.RMRParams)
74         tc.RmrControl.Init(desc, srcId, rtgSvc)
75
76         tc.RMRClient = xapp.NewRMRClientWithParams("tcp:"+strconv.FormatUint(uint64(srcId.Port), 10), 65534, 0, stat)
77         tc.RMRClient.SetReadyCB(tc.ReadyCB, nil)
78         go tc.RMRClient.Start(tc)
79
80         tc.WaitCB()
81         allRmrStubs = append(allRmrStubs, tc)
82 }
83
84 func (tc *RmrStubControl) Consume(msg *xapp.RMRParams) (err error) {
85         defer tc.RMRClient.Free(msg.Mbuf)
86
87         cPay := append(msg.Payload[:0:0], msg.Payload...)
88         msg.Payload = cPay
89         msg.PayloadLen = len(cPay)
90
91         if msg.Mtype == tc.InitMsg {
92                 tc.Info("Testing message ignore %s", msg.String())
93                 tc.SetActive()
94                 return
95         }
96
97         if tc.IsCheckXid() == true && strings.Contains(msg.Xid, tc.GetDesc()) == false {
98                 tc.Info("Ignore %s", msg.String())
99                 return
100         }
101
102         tc.Info("Consume %s", msg.String())
103         tc.PushMsg(msg)
104         return
105 }
106
107 func (tc *RmrStubControl) PushMsg(msg *xapp.RMRParams) {
108         tc.Debug("RmrStubControl PushMsg ... msg(%d) waiting", msg.Mtype)
109         tc.RecvChan <- msg
110         tc.Debug("RmrStubControl PushMsg ... done")
111 }
112
113 func (tc *RmrStubControl) WaitMsg(secs time.Duration) *xapp.RMRParams {
114         tc.Debug("RmrStubControl WaitMsg ... waiting")
115         if secs == 0 {
116                 msg := <-tc.RecvChan
117                 tc.Debug("RmrStubControl WaitMsg ... msg(%d) done", msg.Mtype)
118                 return msg
119         }
120         select {
121         case msg := <-tc.RecvChan:
122                 tc.Debug("RmrStubControl WaitMsg ... msg(%d) done", msg.Mtype)
123                 return msg
124         case <-time.After(secs * time.Second):
125                 tc.Debug("RmrStubControl WaitMsg ... timeout")
126                 return nil
127         }
128         tc.Debug("RmrStubControl WaitMsg ... error")
129         return nil
130 }
131
132 var allRmrStubs []*RmrStubControl
133
134 //-----------------------------------------------------------------------------
135 //
136 //-----------------------------------------------------------------------------
137
138 func RmrStubControlWaitAlive(seconds int, mtype int, rmr *xapp.RMRClient, tent *TestWrapper) bool {
139
140         var dummyBuf []byte = make([]byte, 100)
141
142         params := &xapp.RMRParams{}
143         params.Mtype = mtype
144         params.SubId = -1
145         params.Payload = dummyBuf
146         params.PayloadLen = 100
147         params.Meid = &xapp.RMRMeid{RanName: "TESTPING"}
148         params.Xid = "TESTPING"
149         params.Mbuf = nil
150
151         if len(allRmrStubs) == 0 {
152                 tent.Info("No rmr stubs so no need to wait those to be alive")
153                 return true
154         }
155         status := false
156         i := 1
157         for ; i <= seconds*2 && status == false; i++ {
158
159                 tent.Info("SEND TESTPING: %s", params.String())
160                 rmr.SendWithRetry(params, false, 0)
161
162                 status = true
163                 for _, val := range allRmrStubs {
164                         if val.IsActive() == false {
165                                 status = false
166                                 break
167                         }
168                 }
169                 if status == true {
170                         break
171                 }
172                 tent.Info("Sleep 0.5 secs and try routes again")
173                 time.Sleep(500 * time.Millisecond)
174         }
175
176         if status == false {
177                 tent.Error("Could not initialize routes")
178         }
179         return status
180 }