Clean packing. Removing packer layer.
[ric-plt/submgr.git] / pkg / control / ut_stub_e2term_test.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
20 package control
21
22 import (
23         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
24         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "testing"
27         "time"
28 )
29
30 //-----------------------------------------------------------------------------
31 //
32 //-----------------------------------------------------------------------------
33 var e2t_e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
34
35 //-----------------------------------------------------------------------------
36 //
37 //-----------------------------------------------------------------------------
38 type testingE2termStub struct {
39         testingRmrStubControl
40 }
41
42 //-----------------------------------------------------------------------------
43 //
44 //-----------------------------------------------------------------------------
45 func createNewE2termStub(desc string, rtfile string, port string, stat string) *testingE2termStub {
46         e2termCtrl := &testingE2termStub{}
47         e2termCtrl.testingRmrStubControl.init(desc, rtfile, port, stat, e2termCtrl)
48         return e2termCtrl
49 }
50
51 //-----------------------------------------------------------------------------
52 //
53 //-----------------------------------------------------------------------------
54 func (tc *testingE2termStub) Consume(params *xapp.RMRParams) (err error) {
55         xapp.Rmr.Free(params.Mbuf)
56         params.Mbuf = nil
57         msg := &RMRParams{params}
58
59         if params.Mtype == 55555 {
60                 xapp.Logger.Info("(%s) Testing message ignore %s", tc.GetDesc(), msg.String())
61                 tc.active = true
62                 return
63         }
64
65         xapp.Logger.Info("(%s) Consume %s", tc.GetDesc(), msg.String())
66         tc.rmrConChan <- msg
67         return
68 }
69
70 //-----------------------------------------------------------------------------
71 //
72 //-----------------------------------------------------------------------------
73 func (e2termConn *testingE2termStub) handle_e2term_subs_req(t *testing.T) (*e2ap.E2APSubscriptionRequest, *RMRParams) {
74         xapp.Logger.Info("(%s) handle_e2term_subs_req", e2termConn.GetDesc())
75         e2SubsReq := e2t_e2asnpacker.NewPackerSubscriptionRequest()
76
77         //---------------------------------
78         // e2term activity: Recv Subs Req
79         //---------------------------------
80         select {
81         case msg := <-e2termConn.rmrConChan:
82                 e2termConn.DecMsgCnt()
83                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
84                         testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.GetDesc(), "RIC_SUB_REQ", xapp.RicMessageTypeToName[msg.Mtype])
85                 } else {
86                         xapp.Logger.Info("(%s) Recv Subs Req", e2termConn.GetDesc())
87                         packedData := &e2ap.PackedData{}
88                         packedData.Buf = msg.Payload
89                         unpackerr, req := e2SubsReq.UnPack(packedData)
90                         if unpackerr != nil {
91                                 testError(t, "(%s) RIC_SUB_REQ unpack failed err: %s", e2termConn.GetDesc(), unpackerr.Error())
92                         }
93                         return req, msg
94                 }
95         case <-time.After(15 * time.Second):
96                 testError(t, "(%s) Not Received RIC_SUB_REQ within 15 secs", e2termConn.GetDesc())
97         }
98         return nil, nil
99 }
100
101 func (e2termConn *testingE2termStub) handle_e2term_subs_resp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *RMRParams) {
102         xapp.Logger.Info("(%s) handle_e2term_subs_resp", e2termConn.GetDesc())
103         e2SubsResp := e2t_e2asnpacker.NewPackerSubscriptionResponse()
104
105         //---------------------------------
106         // e2term activity: Send Subs Resp
107         //---------------------------------
108         xapp.Logger.Info("(%s) Send Subs Resp", e2termConn.GetDesc())
109
110         resp := &e2ap.E2APSubscriptionResponse{}
111
112         resp.RequestId.Id = req.RequestId.Id
113         resp.RequestId.Seq = req.RequestId.Seq
114         resp.FunctionId = req.FunctionId
115
116         resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
117         for index := int(0); index < len(req.ActionSetups); index++ {
118                 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
119         }
120
121         for index := uint64(0); index < 1; index++ {
122                 item := e2ap.ActionNotAdmittedItem{}
123                 item.ActionId = index
124                 item.Cause.Content = 1
125                 item.Cause.CauseVal = 1
126                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
127         }
128
129         packerr, packedMsg := e2SubsResp.Pack(resp)
130         if packerr != nil {
131                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
132         }
133         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsResp.String())
134
135         params := &RMRParams{&xapp.RMRParams{}}
136         params.Mtype = xapp.RIC_SUB_RESP
137         //params.SubId = msg.SubId
138         params.SubId = -1
139         params.Payload = packedMsg.Buf
140         params.Meid = msg.Meid
141         //params.Xid = msg.Xid
142         params.Mbuf = nil
143
144         snderr := e2termConn.RmrSend(params)
145         if snderr != nil {
146                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
147         }
148 }
149
150 //-----------------------------------------------------------------------------
151 //
152 //-----------------------------------------------------------------------------
153 type test_subs_fail_params struct {
154         req  *e2ap.E2APSubscriptionRequest
155         fail *e2ap.E2APSubscriptionFailure
156 }
157
158 func (p *test_subs_fail_params) Set(req *e2ap.E2APSubscriptionRequest) {
159         p.req = req
160
161         p.fail = &e2ap.E2APSubscriptionFailure{}
162         p.fail.RequestId.Id = p.req.RequestId.Id
163         p.fail.RequestId.Seq = p.req.RequestId.Seq
164         p.fail.FunctionId = p.req.FunctionId
165         p.fail.ActionNotAdmittedList.Items = make([]e2ap.ActionNotAdmittedItem, len(p.req.ActionSetups))
166         for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ {
167                 p.fail.ActionNotAdmittedList.Items[index].ActionId = p.req.ActionSetups[index].ActionId
168                 p.SetCauseVal(index, 5, 1)
169         }
170 }
171
172 func (p *test_subs_fail_params) SetCauseVal(ind int, content uint8, causeval uint8) {
173
174         if ind < 0 {
175                 for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ {
176                         p.fail.ActionNotAdmittedList.Items[index].Cause.Content = content
177                         p.fail.ActionNotAdmittedList.Items[index].Cause.CauseVal = causeval
178                 }
179                 return
180         }
181         p.fail.ActionNotAdmittedList.Items[ind].Cause.Content = content
182         p.fail.ActionNotAdmittedList.Items[ind].Cause.CauseVal = causeval
183 }
184
185 func (e2termConn *testingE2termStub) handle_e2term_subs_fail(t *testing.T, fparams *test_subs_fail_params, msg *RMRParams) {
186         xapp.Logger.Info("(%s) handle_e2term_subs_fail", e2termConn.GetDesc())
187         e2SubsFail := e2t_e2asnpacker.NewPackerSubscriptionFailure()
188
189         //---------------------------------
190         // e2term activity: Send Subs Fail
191         //---------------------------------
192         xapp.Logger.Info("(%s) Send Subs Fail", e2termConn.GetDesc())
193
194         packerr, packedMsg := e2SubsFail.Pack(fparams.fail)
195         if packerr != nil {
196                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
197         }
198         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsFail.String())
199
200         params := &RMRParams{&xapp.RMRParams{}}
201         params.Mtype = xapp.RIC_SUB_FAILURE
202         params.SubId = msg.SubId
203         params.Payload = packedMsg.Buf
204         params.Meid = msg.Meid
205         params.Xid = msg.Xid
206         params.Mbuf = nil
207
208         snderr := e2termConn.RmrSend(params)
209         if snderr != nil {
210                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
211         }
212 }
213
214 //-----------------------------------------------------------------------------
215 //
216 //-----------------------------------------------------------------------------
217 func (e2termConn *testingE2termStub) handle_e2term_subs_del_req(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *RMRParams) {
218         xapp.Logger.Info("(%s) handle_e2term_subs_del_req", e2termConn.GetDesc())
219         e2SubsDelReq := e2t_e2asnpacker.NewPackerSubscriptionDeleteRequest()
220
221         //---------------------------------
222         // e2term activity: Recv Subs Del Req
223         //---------------------------------
224         select {
225         case msg := <-e2termConn.rmrConChan:
226                 e2termConn.DecMsgCnt()
227                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
228                         testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.GetDesc(), "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype])
229                 } else {
230                         xapp.Logger.Info("(%s) Recv Subs Del Req", e2termConn.GetDesc())
231
232                         packedData := &e2ap.PackedData{}
233                         packedData.Buf = msg.Payload
234                         unpackerr, req := e2SubsDelReq.UnPack(packedData)
235                         if unpackerr != nil {
236                                 testError(t, "(%s) RIC_SUB_DEL_REQ unpack failed err: %s", e2termConn.GetDesc(), unpackerr.Error())
237                         }
238                         return req, msg
239                 }
240         case <-time.After(15 * time.Second):
241                 testError(t, "(%s) Not Received RIC_SUB_DEL_REQ within 15 secs", e2termConn.GetDesc())
242         }
243         return nil, nil
244 }
245
246 func handle_e2term_recv_empty() bool {
247         if len(e2termConn.rmrConChan) > 0 {
248                 return false
249         }
250         return true
251 }
252
253 func (e2termConn *testingE2termStub) handle_e2term_subs_del_resp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) {
254         xapp.Logger.Info("(%s) handle_e2term_subs_del_resp", e2termConn.GetDesc())
255         e2SubsDelResp := e2t_e2asnpacker.NewPackerSubscriptionDeleteResponse()
256
257         //---------------------------------
258         // e2term activity: Send Subs Del Resp
259         //---------------------------------
260         xapp.Logger.Info("(%s) Send Subs Del Resp", e2termConn.GetDesc())
261
262         resp := &e2ap.E2APSubscriptionDeleteResponse{}
263         resp.RequestId.Id = req.RequestId.Id
264         resp.RequestId.Seq = req.RequestId.Seq
265         resp.FunctionId = req.FunctionId
266
267         packerr, packedMsg := e2SubsDelResp.Pack(resp)
268         if packerr != nil {
269                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
270         }
271         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsDelResp.String())
272
273         params := &RMRParams{&xapp.RMRParams{}}
274         params.Mtype = xapp.RIC_SUB_DEL_RESP
275         params.SubId = msg.SubId
276         params.Payload = packedMsg.Buf
277         params.Meid = msg.Meid
278         params.Xid = msg.Xid
279         params.Mbuf = nil
280
281         snderr := e2termConn.RmrSend(params)
282         if snderr != nil {
283                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
284         }
285 }
286
287 //-----------------------------------------------------------------------------
288 //
289 //-----------------------------------------------------------------------------
290 func (e2termConn *testingE2termStub) handle_e2term_subs_del_fail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) {
291         xapp.Logger.Info("(%s) handle_e2term_del_subs_fail", e2termConn.GetDesc())
292         e2SubsDelFail := e2t_e2asnpacker.NewPackerSubscriptionDeleteFailure()
293
294         //---------------------------------
295         // e2term activity: Send Subs Del Fail
296         //---------------------------------
297         xapp.Logger.Info("(%s) Send Subs Del Fail", e2termConn.GetDesc())
298
299         resp := &e2ap.E2APSubscriptionDeleteFailure{}
300         resp.RequestId.Id = req.RequestId.Id
301         resp.RequestId.Seq = req.RequestId.Seq
302         resp.FunctionId = req.FunctionId
303         resp.Cause.Content = 3  // CauseMisc
304         resp.Cause.CauseVal = 4 // unspecified
305
306         packerr, packedMsg := e2SubsDelFail.Pack(resp)
307         if packerr != nil {
308                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
309         }
310         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsDelFail.String())
311
312         params := &RMRParams{&xapp.RMRParams{}}
313         params.Mtype = xapp.RIC_SUB_DEL_FAILURE
314         params.SubId = msg.SubId
315         params.Payload = packedMsg.Buf
316         params.Meid = msg.Meid
317         params.Xid = msg.Xid
318         params.Mbuf = nil
319
320         snderr := e2termConn.RmrSend(params)
321         if snderr != nil {
322                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
323         }
324 }