RICPLT-2910
[ric-plt/submgr.git] / pkg / control / messaging_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/e2ap/pkg/packer"
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27         "testing"
28         "time"
29 )
30
31 //-----------------------------------------------------------------------------
32 //
33 //-----------------------------------------------------------------------------
34
35 var e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
36
37 func createSubsReq() *e2ap.E2APSubscriptionRequest {
38         req := &e2ap.E2APSubscriptionRequest{}
39
40         req.RequestId.Id = 1
41         req.RequestId.Seq = 22
42         req.FunctionId = 1
43
44         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present = true
45         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.StringPut("310150")
46         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Id = 123
47         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Bits = e2ap.E2AP_ENBIDHomeBits28
48
49         // gnb -> enb outgoing
50         // enb -> gnb incoming
51         // X2 36423-f40.doc
52         req.EventTriggerDefinition.InterfaceDirection = e2ap.E2AP_InterfaceDirectionIncoming
53         req.EventTriggerDefinition.ProcedureCode = 5 //28 35
54         req.EventTriggerDefinition.TypeOfMessage = e2ap.E2AP_InitiatingMessage
55
56         req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1)
57         req.ActionSetups[0].ActionId = 0
58         req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport
59         req.ActionSetups[0].ActionDefinition.Present = false
60         //req.ActionSetups[index].ActionDefinition.StyleId = 255
61         //req.ActionSetups[index].ActionDefinition.ParamId = 222
62         req.ActionSetups[0].SubsequentAction.Present = true
63         req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue
64         req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero
65
66         return req
67 }
68
69 func createSubsResp(req *e2ap.E2APSubscriptionRequest) *e2ap.E2APSubscriptionResponse {
70
71         resp := &e2ap.E2APSubscriptionResponse{}
72
73         resp.RequestId.Id = req.RequestId.Id
74         resp.RequestId.Seq = req.RequestId.Seq
75         resp.FunctionId = req.FunctionId
76
77         resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
78         for index := int(0); index < len(req.ActionSetups); index++ {
79                 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
80         }
81
82         for index := uint64(0); index < 1; index++ {
83                 item := e2ap.ActionNotAdmittedItem{}
84                 item.ActionId = index
85                 item.Cause.Content = 1
86                 item.Cause.CauseVal = 1
87                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
88         }
89
90         return resp
91 }
92
93 //-----------------------------------------------------------------------------
94 // TestSubRequestSubResponseOk
95 //
96 // +-------+     +---------+    +---------+
97 // | xapp  |     | submgr  |    | e2term  |
98 // +-------+     +---------+    +---------+
99 //     |              |              |
100 //     | SubReq       |              |
101 //     |------------->|              |
102 //     |              |              |
103 //     |              | SubReq       |
104 //     |              |------------->|
105 //     |              |              |
106 //     |              |      SubResp |
107 //     |              |<-------------|
108 //     |              |              |
109 //     |      SubResp |              |
110 //     |<-------------|              |
111 //     |              |              |
112 //
113 //-----------------------------------------------------------------------------
114 func TestSubRequestSubResponseOk(t *testing.T) {
115
116         xapp.Logger.Info("TestSimple start")
117         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
118         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
119
120         //---------------------------------
121         // xapp activity
122         //---------------------------------
123         select {
124         case <-time.After(5 * time.Second):
125                 req := createSubsReq()
126                 e2SubsReq.Set(req)
127                 xapp.Logger.Debug("%s", e2SubsReq.String())
128                 err, packedMsg := e2SubsReq.Pack(nil)
129                 if err != nil {
130                         testError(t, "(xappConn) pack NOK %s", err.Error())
131                 } else {
132                         xapp.Logger.Info("(xappConn) pack OK")
133                 }
134
135                 params := &xapp.RMRParams{}
136                 params.Mtype = xapp.RIC_SUB_REQ
137                 params.SubId = -1
138                 params.Payload = packedMsg.Buf
139                 params.Meid = &xapp.RMRMeid{RanName: "RAN_NAME_1"}
140                 params.Xid = "XID_1"
141                 params.Mbuf = nil
142
143                 snderr := xappConn.RmrSend(params)
144                 if snderr != nil {
145                         testError(t, "(xappConn) RMR SEND FAILED: %s", snderr.Error())
146                 }
147         }
148
149         //---------------------------------
150         // e2term activity
151         //---------------------------------
152         select {
153         case msg := <-e2termConn.rmrConChan:
154                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
155                         testError(t, "(e2termConn) Received non RIC_SUB_REQ message")
156                 } else {
157
158                         packedData := &packer.PackedData{}
159                         packedData.Buf = msg.Payload
160                         unpackerr := e2SubsReq.UnPack(packedData)
161                         if unpackerr != nil {
162                                 testError(t, "(e2termConn) RIC_SUB_REQ unpack failed err: %s", unpackerr.Error())
163                         }
164                         geterr, req := e2SubsReq.Get()
165                         if geterr != nil {
166                                 testError(t, "(e2termConn) RIC_SUB_REQ get failed err: %s", geterr.Error())
167                         }
168
169                         resp := createSubsResp(req)
170                         e2SubsResp.Set(resp)
171                         xapp.Logger.Debug("%s", e2SubsResp.String())
172                         packerr, packedMsg := e2SubsResp.Pack(nil)
173                         if packerr != nil {
174                                 testError(t, "(e2termConn) pack NOK %s", packerr.Error())
175                         } else {
176                                 xapp.Logger.Info("(e2termConn) pack OK")
177                         }
178
179                         params := &xapp.RMRParams{}
180                         params.Mtype = xapp.RIC_SUB_RESP
181                         params.SubId = msg.SubId
182                         params.Payload = packedMsg.Buf
183                         params.Meid = msg.Meid
184                         params.Xid = msg.Xid
185                         params.Mbuf = nil
186
187                         snderr := e2termConn.RmrSend(params)
188                         if snderr != nil {
189                                 testError(t, "(e2termConn) RMR SEND FAILED: %s", snderr.Error())
190                         }
191
192                 }
193         case <-time.After(15 * time.Second):
194                 testError(t, "(e2termConn) Not Received RIC_SUB_REQ within 15 secs")
195         }
196
197         //---------------------------------
198         // xapp activity
199         //---------------------------------
200         select {
201         case msg := <-xappConn.rmrConChan:
202                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
203                         testError(t, "(xappConn) Received non RIC_SUB_RESP message")
204                 } else {
205
206                         packedData := &packer.PackedData{}
207                         packedData.Buf = msg.Payload
208                         unpackerr := e2SubsResp.UnPack(packedData)
209                         if unpackerr != nil {
210                                 testError(t, "(xappConn) RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
211                         }
212                         geterr, _ := e2SubsResp.Get()
213                         if geterr != nil {
214                                 testError(t, "(xappConn) RIC_SUB_RESP get failed err: %s", geterr.Error())
215                         }
216
217                 }
218         case <-time.After(15 * time.Second):
219                 testError(t, "(xappConn) Not Received RIC_SUB_RESP within 15 secs")
220         }
221
222 }