Xapp-frame updted to v0.6.8. Golog updated to v0.0.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         "strings"
24         "testing"
25         "time"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type RmrStubControl struct {
32         RmrControl
33         *xapp.RMRClient
34         RecvChan chan *xapp.RMRParams
35         Active   bool
36         InitMsg  int
37         CheckXid bool
38 }
39
40 func (tc *RmrStubControl) SetActive() {
41         tc.Active = true
42 }
43
44 func (tc *RmrStubControl) IsActive() bool {
45         return tc.Active
46 }
47
48 func (tc *RmrStubControl) SetCheckXid(val bool) {
49         tc.CheckXid = val
50 }
51
52 func (tc *RmrStubControl) IsCheckXid() bool {
53         return tc.CheckXid
54 }
55
56 func (tc *RmrStubControl) IsChanEmpty() bool {
57         if len(tc.RecvChan) > 0 {
58                 return false
59         }
60         return true
61 }
62
63 func (tc *RmrStubControl) TestMsgChanEmpty(t *testing.T) {
64         if tc.IsChanEmpty() == false {
65                 tc.TestError(t, "message channel not empty")
66         }
67 }
68
69 func (tc *RmrStubControl) Init(desc string, srcId RmrSrcId, rtgSvc RmrRtgSvc, statDesc string, initMsg int) {
70         tc.InitMsg = initMsg
71         tc.Active = false
72         tc.RecvChan = make(chan *xapp.RMRParams)
73         tc.RmrControl.Init(desc, srcId, rtgSvc)
74
75         tc.RMRClient = xapp.NewRMRClientWithParams(&xapp.RMRClientParams{
76                 StatDesc: statDesc,
77                 RmrData: xapp.PortData{
78                         Name:       "", // Not used currently
79                         Port:       int(srcId.Port),
80                         MaxSize:    65534,
81                         ThreadType: 0,
82                         LowLatency: false,
83                         FastAck:    false,
84                         //Policies:                             // Not used currently
85                         MaxRetryOnFailure: 1,
86                 },
87         })
88
89         tc.RMRClient.SetReadyCB(tc.ReadyCB, nil)
90         go tc.RMRClient.Start(tc)
91
92         tc.WaitCB()
93         allRmrStubs = append(allRmrStubs, tc)
94 }
95
96 func (tc *RmrStubControl) Consume(msg *xapp.RMRParams) (err error) {
97         defer tc.RMRClient.Free(msg.Mbuf)
98
99         cPay := append(msg.Payload[:0:0], msg.Payload...)
100         msg.Payload = cPay
101         msg.PayloadLen = len(cPay)
102
103         if msg.Mtype == tc.InitMsg {
104                 tc.Info("Testing message ignore %s", msg.String())
105                 tc.SetActive()
106                 return
107         }
108
109         if tc.IsCheckXid() == true && strings.Contains(msg.Xid, tc.GetDesc()) == false {
110                 tc.Info("Ignore %s", msg.String())
111                 return
112         }
113
114         tc.Info("Consume %s", msg.String())
115         tc.PushMsg(msg)
116         return
117 }
118
119 func (tc *RmrStubControl) PushMsg(msg *xapp.RMRParams) {
120         tc.Debug("RmrStubControl PushMsg ... msg(%d) waiting", msg.Mtype)
121         tc.RecvChan <- msg
122         tc.Debug("RmrStubControl PushMsg ... done")
123 }
124
125 func (tc *RmrStubControl) WaitMsg(secs time.Duration) *xapp.RMRParams {
126         tc.Debug("RmrStubControl WaitMsg ... waiting")
127         if secs == 0 {
128                 msg := <-tc.RecvChan
129                 tc.Debug("RmrStubControl WaitMsg ... msg(%d) done", msg.Mtype)
130                 return msg
131         }
132         select {
133         case msg := <-tc.RecvChan:
134                 tc.Debug("RmrStubControl WaitMsg ... msg(%d) done", msg.Mtype)
135                 return msg
136         case <-time.After(secs * time.Second):
137                 tc.Debug("RmrStubControl WaitMsg ... timeout")
138                 return nil
139         }
140         tc.Debug("RmrStubControl WaitMsg ... error")
141         return nil
142 }
143
144 var allRmrStubs []*RmrStubControl
145
146 //-----------------------------------------------------------------------------
147 //
148 //-----------------------------------------------------------------------------
149
150 func RmrStubControlWaitAlive(seconds int, mtype int, rmr *xapp.RMRClient, tent *TestWrapper) bool {
151
152         var dummyBuf []byte = make([]byte, 100)
153
154         params := &xapp.RMRParams{}
155         params.Mtype = mtype
156         params.SubId = -1
157         params.Payload = dummyBuf
158         params.PayloadLen = 100
159         params.Meid = &xapp.RMRMeid{RanName: "TESTPING"}
160         params.Xid = "TESTPING"
161         params.Mbuf = nil
162
163         if len(allRmrStubs) == 0 {
164                 tent.Info("No rmr stubs so no need to wait those to be alive")
165                 return true
166         }
167         status := false
168         i := 1
169         for ; i <= seconds*2 && status == false; i++ {
170
171                 tent.Info("SEND TESTPING: %s", params.String())
172                 rmr.SendWithRetry(params, false, 0)
173
174                 status = true
175                 for _, val := range allRmrStubs {
176                         if val.IsActive() == false {
177                                 status = false
178                                 break
179                         }
180                 }
181                 if status == true {
182                         break
183                 }
184                 tent.Info("Sleep 0.5 secs and try routes again")
185                 time.Sleep(500 * time.Millisecond)
186         }
187
188         if status == false {
189                 tent.Error("Could not initialize routes")
190         }
191         return status
192 }