blob: 2d7d535e7e099937667a25334fef95c1143bebe5 [file] [log] [blame]
kalnagy45114752019-06-18 14:40:39 +02001/*
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
20package control
21
kalnagy45114752019-06-18 14:40:39 +020022import (
Juha Hyttinene406a342020-01-13 13:02:26 +020023 "fmt"
Juha Hyttinen86a46202020-01-14 12:49:09 +020024 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
kalnagy93cc3e22019-09-19 11:29:29 +020025 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020026 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
kalnagy1455c852019-10-21 13:06:23 +020027 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28 httptransport "github.com/go-openapi/runtime/client"
29 "github.com/go-openapi/strfmt"
30 "github.com/spf13/viper"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000031 "time"
kalnagy45114752019-06-18 14:40:39 +020032)
33
Juha Hyttinene406a342020-01-13 13:02:26 +020034//-----------------------------------------------------------------------------
35//
36//-----------------------------------------------------------------------------
37
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020038func idstring(err error, entries ...fmt.Stringer) string {
39 var retval string = ""
40 var filler string = ""
41 for _, entry := range entries {
42 retval += filler + entry.String()
43 filler = " "
44 }
45 if err != nil {
46 retval += filler + "err(" + err.Error() + ")"
47 filler = " "
48
49 }
50 return retval
51}
52
53//-----------------------------------------------------------------------------
54//
55//-----------------------------------------------------------------------------
56
Juha Hyttinen422d0182020-01-17 13:37:05 +020057var e2tSubReqTimeout time.Duration = 5 * time.Second
58var e2tSubDelReqTime time.Duration = 5 * time.Second
59var e2tMaxSubReqTryCount uint64 = 2 // Initial try + retry
60var e2tMaxSubDelReqTryCount uint64 = 2 // Initial try + retry
61
62var e2tRecvMsgTimeout time.Duration = 5 * time.Second
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020063
kalnagy45114752019-06-18 14:40:39 +020064type Control struct {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020065 xapptweaks.XappWrapper
66 e2ap *E2ap
67 registry *Registry
68 tracker *Tracker
69 timerMap *TimerMap
kalnagy93cc3e22019-09-19 11:29:29 +020070}
71
72type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020073 PlmnID string
74 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020075 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020076}
77
Peter Szilagyifbc56f92019-07-23 19:29:46 +000078func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020079 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000080 viper.AutomaticEnv()
81 viper.SetEnvPrefix("submgr")
82 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020083}
84
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020085func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020086
Juha Hyttinen56e03832020-01-14 17:08:43 +020087 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
Juha Hyttinen12d31af2020-01-22 12:59:01 +020088 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
Juha Hyttinen56e03832020-01-14 17:08:43 +020089
Peter Szilagyifbc56f92019-07-23 19:29:46 +000090 registry := new(Registry)
Anssi Mannila5c161a92020-01-15 15:40:57 +020091 registry.Initialize()
Juha Hyttinen56e03832020-01-14 17:08:43 +020092 registry.rtmgrClient = &rtmgrClient
kalnagy93cc3e22019-09-19 11:29:29 +020093
Balint Uvegese9608cd2019-09-20 18:00:32 +000094 tracker := new(Tracker)
95 tracker.Init()
96
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020097 timerMap := new(TimerMap)
98 timerMap.Init()
99
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200100 c := &Control{e2ap: new(E2ap),
101 registry: registry,
102 tracker: tracker,
103 timerMap: timerMap,
104 }
105 c.XappWrapper.Init("")
106 return c
107}
108
109func (c *Control) ReadyCB(data interface{}) {
110 if c.Rmr == nil {
111 c.Rmr = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200112 }
kalnagy45114752019-06-18 14:40:39 +0200113}
114
115func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200116 xapp.SetReadyCB(c.ReadyCB, nil)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000117 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200118}
119
Juha Hyttinen83ada002020-01-30 10:36:33 +0200120func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200121 params := xapptweaks.NewParams(nil)
Juha Hyttinene406a342020-01-13 13:02:26 +0200122 params.Mtype = trans.GetMtype()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200123 params.SubId = int(subs.GetReqId().Seq)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200124 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200125 params.Meid = subs.GetMeid()
126 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200127 params.PayloadLen = len(trans.Payload.Buf)
128 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200129 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200130 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
131 return c.RmrSend(params)
kalnagye0018682019-09-26 16:28:25 +0200132}
133
Juha Hyttinen83ada002020-01-30 10:36:33 +0200134func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
135
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200136 params := xapptweaks.NewParams(nil)
Juha Hyttinen63284a22020-01-15 10:45:11 +0200137 params.Mtype = trans.GetMtype()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200138 params.SubId = int(subs.GetReqId().Seq)
Juha Hyttinene406a342020-01-13 13:02:26 +0200139 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200140 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200141 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200142 params.PayloadLen = len(trans.Payload.Buf)
143 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200144 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200145 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
146 return c.RmrSend(params)
Juha Hyttinene406a342020-01-13 13:02:26 +0200147}
148
149func (c *Control) Consume(params *xapp.RMRParams) (err error) {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200150 msg := xapptweaks.NewParams(params)
151 if c.Rmr == nil {
152 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
153 xapp.Logger.Error("%s", err.Error())
154 return
155 }
156 c.CntRecvMsg++
157
158 defer c.Rmr.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200159
Anssi Mannila90fa0212019-12-12 10:47:47 +0200160 switch msg.Mtype {
161 case xapp.RICMessageTypes["RIC_SUB_REQ"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200162 go c.handleXAPPSubscriptionRequest(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200163 case xapp.RICMessageTypes["RIC_SUB_RESP"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200164 go c.handleE2TSubscriptionResponse(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200165 case xapp.RICMessageTypes["RIC_SUB_FAILURE"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200166 go c.handleE2TSubscriptionFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200167 case xapp.RICMessageTypes["RIC_SUB_DEL_REQ"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200168 go c.handleXAPPSubscriptionDeleteRequest(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200169 case xapp.RICMessageTypes["RIC_SUB_DEL_RESP"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200170 go c.handleE2TSubscriptionDeleteResponse(msg)
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200171 case xapp.RICMessageTypes["RIC_SUB_DEL_FAILURE"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200172 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200173 default:
174 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000175 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200176 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200177}
178
Juha Hyttinen422d0182020-01-17 13:37:05 +0200179//-------------------------------------------------------------------
180// handle from XAPP Subscription Request
181//------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200182func (c *Control) handleXAPPSubscriptionRequest(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200183 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200184
185 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
186 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200187 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200188 return
189 }
190
Juha Hyttinen47942b42020-02-27 10:41:43 +0200191 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId.Seq, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200192 if trans == nil {
193 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200194 return
195 }
196 defer trans.Release()
197
Juha Hyttinen83ada002020-01-30 10:36:33 +0200198 err = c.tracker.Track(trans)
199 if err != nil {
200 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
201 return
202 }
203
Juha Hyttinen422d0182020-01-17 13:37:05 +0200204 subs, err := c.registry.AssignToSubscription(trans, subReqMsg)
205 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200206 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200207 return
208 }
209
Juha Hyttinen422d0182020-01-17 13:37:05 +0200210 //
211 // Wake subs request
212 //
213 go c.handleSubscriptionCreate(subs, trans)
214 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
215
216 err = nil
217 if event != nil {
218 switch themsg := event.(type) {
219 case *e2ap.E2APSubscriptionResponse:
220 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
221 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200222 trans.Release()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200223 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200224 return
225 }
226 case *e2ap.E2APSubscriptionFailure:
227 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
228 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200229 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200230 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200231 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200232 break
233 }
234 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200235 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200236 c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200237}
238
Juha Hyttinen422d0182020-01-17 13:37:05 +0200239//-------------------------------------------------------------------
240// handle from XAPP Subscription Delete Request
241//------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200242func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200243 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200244
245 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200246 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200247 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200248 return
249 }
250
Juha Hyttinen47942b42020-02-27 10:41:43 +0200251 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId.Seq, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200252 if trans == nil {
253 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200254 return
255 }
256 defer trans.Release()
257
Juha Hyttinen83ada002020-01-30 10:36:33 +0200258 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200259 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200260 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
261 return
262 }
263
Juha Hyttinen47942b42020-02-27 10:41:43 +0200264 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200265 if err != nil {
266 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200267 return
268 }
269
Juha Hyttinen422d0182020-01-17 13:37:05 +0200270 //
271 // Wake subs delete
272 //
273 go c.handleSubscriptionDelete(subs, trans)
274 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
275
Juha Hyttinen83ada002020-01-30 10:36:33 +0200276 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
277
Juha Hyttinen422d0182020-01-17 13:37:05 +0200278 // Whatever is received send ok delete response
279 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Juha Hyttinen47942b42020-02-27 10:41:43 +0200280 subDelRespMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200281 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
282 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
283 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200284 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200285 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200286
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200287 c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200288}
289
290//-------------------------------------------------------------------
291// SUBS CREATE Handling
292//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200293func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200294
Juha Hyttinen83ada002020-01-30 10:36:33 +0200295 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200296 subs.WaitTransactionTurn(trans)
297 defer subs.ReleaseTransactionTurn(trans)
298 defer trans.Release()
299
Juha Hyttinen83ada002020-01-30 10:36:33 +0200300 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200301
Juha Hyttinen83ada002020-01-30 10:36:33 +0200302 subRfMsg, valid := subs.GetCachedResponse()
303 if subRfMsg == nil && valid == true {
304 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
305 switch event.(type) {
306 case *e2ap.E2APSubscriptionResponse:
307 subRfMsg, valid = subs.SetCachedResponse(event, true)
308 case *e2ap.E2APSubscriptionFailure:
309 subRfMsg, valid = subs.SetCachedResponse(event, false)
310 default:
311 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
312 subRfMsg, valid = subs.SetCachedResponse(nil, false)
313 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
314 }
315 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
316 } else {
317 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200318 }
319
Juha Hyttinen83ada002020-01-30 10:36:33 +0200320 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200321}
322
323//-------------------------------------------------------------------
324// SUBS DELETE Handling
325//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200326
Juha Hyttinen83ada002020-01-30 10:36:33 +0200327func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200328
Juha Hyttinen83ada002020-01-30 10:36:33 +0200329 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200330 subs.WaitTransactionTurn(trans)
331 defer subs.ReleaseTransactionTurn(trans)
332 defer trans.Release()
333
Juha Hyttinen83ada002020-01-30 10:36:33 +0200334 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200335
Juha Hyttinen3944a222020-01-24 11:51:46 +0200336 subs.mutex.Lock()
337 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
338 subs.valid = false
339 subs.mutex.Unlock()
340 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
341 } else {
342 subs.mutex.Unlock()
343 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200344
Juha Hyttinen47942b42020-02-27 10:41:43 +0200345 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200346}
347
348//-------------------------------------------------------------------
349// send to E2T Subscription Request
350//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200351func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200352 var err error
353 var event interface{} = nil
354 var timedOut bool = false
355
356 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200357 subReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200358 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
359 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200360 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200361 return event
362 }
363
364 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200365 desc := fmt.Sprintf("(retry %d)", retries)
366 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200367 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
368 if timedOut {
369 continue
370 }
371 break
372 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200373 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200374 return event
375}
376
377//-------------------------------------------------------------------
378// send to E2T Subscription Delete Request
379//-------------------------------------------------------------------
380
Juha Hyttinen83ada002020-01-30 10:36:33 +0200381func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200382 var err error
383 var event interface{}
384 var timedOut bool
385
386 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200387 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200388 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200389 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
390 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200391 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200392 return event
393 }
394
395 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200396 desc := fmt.Sprintf("(retry %d)", retries)
397 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200398 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
399 if timedOut {
400 continue
401 }
402 break
403 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200404 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200405 return event
406}
407
408//-------------------------------------------------------------------
409// handle from E2T Subscription Reponse
410//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200411func (c *Control) handleE2TSubscriptionResponse(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200412 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200413 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
414 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200415 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200416 return
417 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200418 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.Seq})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200419 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200420 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200421 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200422 }
423 trans := subs.GetTransaction()
424 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200425 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200426 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200427 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200428 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200429 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
430 if sendOk == false {
431 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200432 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000433 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000434 return
kalnagy45114752019-06-18 14:40:39 +0200435}
436
Juha Hyttinen422d0182020-01-17 13:37:05 +0200437//-------------------------------------------------------------------
438// handle from E2T Subscription Failure
439//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200440func (c *Control) handleE2TSubscriptionFailure(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200441 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200442 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000443 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200444 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000445 return
446 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200447 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.Seq})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200448 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200449 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200450 return
451 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200452 trans := subs.GetTransaction()
453 if trans == nil {
454 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200455 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200456 return
457 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200458 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
459 if sendOk == false {
460 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200461 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200462 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200463 return
464}
465
Juha Hyttinen422d0182020-01-17 13:37:05 +0200466//-------------------------------------------------------------------
467// handle from E2T Subscription Delete Response
468//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200469func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapptweaks.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200470 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200471 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200472 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200473 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200474 return
475 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200476 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.Seq})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200477 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200478 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200479 return
480 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200481 trans := subs.GetTransaction()
482 if trans == nil {
483 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200484 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200485 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200486 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200487 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
488 if sendOk == false {
489 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200490 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +0200491 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200492 return
493}
494
Juha Hyttinen422d0182020-01-17 13:37:05 +0200495//-------------------------------------------------------------------
496// handle from E2T Subscription Delete Failure
497//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200498func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200499 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200500 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200501 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200502 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200503 return
504 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200505 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.Seq})
Anssi Mannila8046c702020-01-02 13:39:05 +0200506 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200507 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +0200508 return
509 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200510 trans := subs.GetTransaction()
511 if trans == nil {
512 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200513 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +0200514 return
515 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200516 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
517 if sendOk == false {
518 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200519 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200520 }
kalnagy93cc3e22019-09-19 11:29:29 +0200521 return
522}
523
Juha Hyttinen422d0182020-01-17 13:37:05 +0200524//-------------------------------------------------------------------
525//
526//-------------------------------------------------------------------
527func typeofSubsMessage(v interface{}) string {
528 if v == nil {
529 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +0200530 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200531 switch v.(type) {
532 case *e2ap.E2APSubscriptionRequest:
533 return "SubReq"
534 case *e2ap.E2APSubscriptionResponse:
535 return "SubResp"
536 case *e2ap.E2APSubscriptionFailure:
537 return "SubFail"
538 case *e2ap.E2APSubscriptionDeleteRequest:
539 return "SubDelReq"
540 case *e2ap.E2APSubscriptionDeleteResponse:
541 return "SubDelResp"
542 case *e2ap.E2APSubscriptionDeleteFailure:
543 return "SubDelFail"
544 default:
545 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200546 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200547}