blob: 7b0760c31cd156b60e5b4bb89b117074e1614e4c [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"
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020027 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
kalnagy1455c852019-10-21 13:06:23 +020028 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
29 httptransport "github.com/go-openapi/runtime/client"
30 "github.com/go-openapi/strfmt"
31 "github.com/spf13/viper"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000032 "time"
kalnagy45114752019-06-18 14:40:39 +020033)
34
Juha Hyttinene406a342020-01-13 13:02:26 +020035//-----------------------------------------------------------------------------
36//
37//-----------------------------------------------------------------------------
38
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020039func idstring(err error, entries ...fmt.Stringer) string {
40 var retval string = ""
41 var filler string = ""
42 for _, entry := range entries {
43 retval += filler + entry.String()
44 filler = " "
45 }
46 if err != nil {
47 retval += filler + "err(" + err.Error() + ")"
48 filler = " "
49
50 }
51 return retval
52}
53
54//-----------------------------------------------------------------------------
55//
56//-----------------------------------------------------------------------------
57
Juha Hyttinen422d0182020-01-17 13:37:05 +020058var e2tSubReqTimeout time.Duration = 5 * time.Second
59var e2tSubDelReqTime time.Duration = 5 * time.Second
60var e2tMaxSubReqTryCount uint64 = 2 // Initial try + retry
61var e2tMaxSubDelReqTryCount uint64 = 2 // Initial try + retry
62
63var e2tRecvMsgTimeout time.Duration = 5 * time.Second
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020064
kalnagy45114752019-06-18 14:40:39 +020065type Control struct {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020066 xapptweaks.XappWrapper
67 e2ap *E2ap
68 registry *Registry
69 tracker *Tracker
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020070 //subscriber *xapp.Subscriber
kalnagy93cc3e22019-09-19 11:29:29 +020071}
72
73type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020074 PlmnID string
75 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020076 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020077}
78
Peter Szilagyifbc56f92019-07-23 19:29:46 +000079func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020080 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000081 viper.AutomaticEnv()
82 viper.SetEnvPrefix("submgr")
83 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020084}
85
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020086func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020087
Juha Hyttinen56e03832020-01-14 17:08:43 +020088 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
Juha Hyttinen12d31af2020-01-22 12:59:01 +020089 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
Juha Hyttinen56e03832020-01-14 17:08:43 +020090
Peter Szilagyifbc56f92019-07-23 19:29:46 +000091 registry := new(Registry)
Anssi Mannila5c161a92020-01-15 15:40:57 +020092 registry.Initialize()
Juha Hyttinen56e03832020-01-14 17:08:43 +020093 registry.rtmgrClient = &rtmgrClient
kalnagy93cc3e22019-09-19 11:29:29 +020094
Balint Uvegese9608cd2019-09-20 18:00:32 +000095 tracker := new(Tracker)
96 tracker.Init()
97
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020098 //subscriber := xapp.NewSubscriber(viper.GetString("subscription.host"), viper.GetInt("subscription.timeout"))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020099
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200100 c := &Control{e2ap: new(E2ap),
101 registry: registry,
102 tracker: tracker,
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200103 //subscriber: subscriber,
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200104 }
105 c.XappWrapper.Init("")
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200106 go xapp.Subscription.Listen(c.SubscriptionHandler, c.QueryHandler)
107 //go c.subscriber.Listen(c.SubscriptionHandler, c.QueryHandler)
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200108 return c
109}
110
111func (c *Control) ReadyCB(data interface{}) {
112 if c.Rmr == nil {
113 c.Rmr = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200114 }
kalnagy45114752019-06-18 14:40:39 +0200115}
116
117func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200118 xapp.SetReadyCB(c.ReadyCB, nil)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000119 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200120}
121
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200122//-------------------------------------------------------------------
123//
124//-------------------------------------------------------------------
125func (c *Control) SubscriptionHandler(stype models.SubscriptionType, params interface{}) (models.SubscriptionResult, error) {
126 /*
127 switch p := params.(type) {
128 case *models.ReportParams:
129 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(p.ClientEndpoint),"" , 0, &xapp.RMRMeid{RanName: p.Meid})
130 if trans == nil {
131 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
132 return
133 }
134 defer trans.Release()
135 case *models.ControlParams:
136 case *models.PolicyParams:
137 }
138 */
139 return models.SubscriptionResult{}, fmt.Errorf("Subscription rest interface not implemented")
140}
141
142func (c *Control) QueryHandler() (models.SubscriptionList, error) {
143 return c.registry.QueryHandler()
144}
145
146//-------------------------------------------------------------------
147//
148//-------------------------------------------------------------------
149
Juha Hyttinen83ada002020-01-30 10:36:33 +0200150func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200151 params := xapptweaks.NewParams(nil)
Juha Hyttinene406a342020-01-13 13:02:26 +0200152 params.Mtype = trans.GetMtype()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200153 params.SubId = int(subs.GetReqId().Seq)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200154 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200155 params.Meid = subs.GetMeid()
156 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200157 params.PayloadLen = len(trans.Payload.Buf)
158 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200159 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200160 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
161 return c.RmrSend(params)
kalnagye0018682019-09-26 16:28:25 +0200162}
163
Juha Hyttinen83ada002020-01-30 10:36:33 +0200164func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
165
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200166 params := xapptweaks.NewParams(nil)
Juha Hyttinen63284a22020-01-15 10:45:11 +0200167 params.Mtype = trans.GetMtype()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200168 params.SubId = int(subs.GetReqId().Seq)
Juha Hyttinene406a342020-01-13 13:02:26 +0200169 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200170 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200171 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200172 params.PayloadLen = len(trans.Payload.Buf)
173 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200174 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200175 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
176 return c.RmrSend(params)
Juha Hyttinene406a342020-01-13 13:02:26 +0200177}
178
179func (c *Control) Consume(params *xapp.RMRParams) (err error) {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200180 msg := xapptweaks.NewParams(params)
181 if c.Rmr == nil {
182 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
183 xapp.Logger.Error("%s", err.Error())
184 return
185 }
186 c.CntRecvMsg++
187
188 defer c.Rmr.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200189
Anssi Mannila90fa0212019-12-12 10:47:47 +0200190 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200191 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200192 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200193 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200194 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200195 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200196 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200197 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200198 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200199 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200200 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200201 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200202 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200203 default:
204 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000205 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200206 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200207}
208
Juha Hyttinen422d0182020-01-17 13:37:05 +0200209//-------------------------------------------------------------------
210// handle from XAPP Subscription Request
211//------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200212func (c *Control) handleXAPPSubscriptionRequest(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200213 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200214
215 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
216 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200217 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200218 return
219 }
220
Juha Hyttinen47942b42020-02-27 10:41:43 +0200221 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId.Seq, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200222 if trans == nil {
223 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200224 return
225 }
226 defer trans.Release()
227
Juha Hyttinen83ada002020-01-30 10:36:33 +0200228 err = c.tracker.Track(trans)
229 if err != nil {
230 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
231 return
232 }
233
Juha Hyttinen422d0182020-01-17 13:37:05 +0200234 subs, err := c.registry.AssignToSubscription(trans, subReqMsg)
235 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200236 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200237 return
238 }
239
Juha Hyttinen422d0182020-01-17 13:37:05 +0200240 //
241 // Wake subs request
242 //
243 go c.handleSubscriptionCreate(subs, trans)
244 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
245
246 err = nil
247 if event != nil {
248 switch themsg := event.(type) {
249 case *e2ap.E2APSubscriptionResponse:
250 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
251 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200252 trans.Release()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200253 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200254 return
255 }
256 case *e2ap.E2APSubscriptionFailure:
257 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
258 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200259 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200260 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200261 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200262 break
263 }
264 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200265 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200266 c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200267}
268
Juha Hyttinen422d0182020-01-17 13:37:05 +0200269//-------------------------------------------------------------------
270// handle from XAPP Subscription Delete Request
271//------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200272func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200273 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200274
275 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200276 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200277 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200278 return
279 }
280
Juha Hyttinen47942b42020-02-27 10:41:43 +0200281 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId.Seq, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200282 if trans == nil {
283 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200284 return
285 }
286 defer trans.Release()
287
Juha Hyttinen83ada002020-01-30 10:36:33 +0200288 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200289 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200290 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
291 return
292 }
293
Juha Hyttinen47942b42020-02-27 10:41:43 +0200294 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200295 if err != nil {
296 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200297 return
298 }
299
Juha Hyttinen422d0182020-01-17 13:37:05 +0200300 //
301 // Wake subs delete
302 //
303 go c.handleSubscriptionDelete(subs, trans)
304 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
305
Juha Hyttinen83ada002020-01-30 10:36:33 +0200306 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
307
Juha Hyttinen422d0182020-01-17 13:37:05 +0200308 // Whatever is received send ok delete response
309 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Juha Hyttinen47942b42020-02-27 10:41:43 +0200310 subDelRespMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200311 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
312 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
313 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200314 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200315 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200316
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200317 c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200318}
319
320//-------------------------------------------------------------------
321// SUBS CREATE Handling
322//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200323func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200324
Juha Hyttinen83ada002020-01-30 10:36:33 +0200325 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200326 subs.WaitTransactionTurn(trans)
327 defer subs.ReleaseTransactionTurn(trans)
328 defer trans.Release()
329
Juha Hyttinen83ada002020-01-30 10:36:33 +0200330 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200331
Juha Hyttinen83ada002020-01-30 10:36:33 +0200332 subRfMsg, valid := subs.GetCachedResponse()
333 if subRfMsg == nil && valid == true {
334 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
335 switch event.(type) {
336 case *e2ap.E2APSubscriptionResponse:
337 subRfMsg, valid = subs.SetCachedResponse(event, true)
338 case *e2ap.E2APSubscriptionFailure:
339 subRfMsg, valid = subs.SetCachedResponse(event, false)
340 default:
341 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
342 subRfMsg, valid = subs.SetCachedResponse(nil, false)
343 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
344 }
345 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
346 } else {
347 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200348 }
349
Juha Hyttinen83ada002020-01-30 10:36:33 +0200350 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200351}
352
353//-------------------------------------------------------------------
354// SUBS DELETE Handling
355//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200356
Juha Hyttinen83ada002020-01-30 10:36:33 +0200357func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200358
Juha Hyttinen83ada002020-01-30 10:36:33 +0200359 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200360 subs.WaitTransactionTurn(trans)
361 defer subs.ReleaseTransactionTurn(trans)
362 defer trans.Release()
363
Juha Hyttinen83ada002020-01-30 10:36:33 +0200364 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200365
Juha Hyttinen3944a222020-01-24 11:51:46 +0200366 subs.mutex.Lock()
367 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
368 subs.valid = false
369 subs.mutex.Unlock()
370 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
371 } else {
372 subs.mutex.Unlock()
373 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200374
Juha Hyttinen47942b42020-02-27 10:41:43 +0200375 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200376}
377
378//-------------------------------------------------------------------
379// send to E2T Subscription Request
380//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200381func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200382 var err error
383 var event interface{} = nil
384 var timedOut bool = false
385
386 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200387 subReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200388 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
389 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200390 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200391 return event
392 }
393
394 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200395 desc := fmt.Sprintf("(retry %d)", retries)
396 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200397 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
398 if timedOut {
399 continue
400 }
401 break
402 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200403 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200404 return event
405}
406
407//-------------------------------------------------------------------
408// send to E2T Subscription Delete Request
409//-------------------------------------------------------------------
410
Juha Hyttinen83ada002020-01-30 10:36:33 +0200411func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200412 var err error
413 var event interface{}
414 var timedOut bool
415
416 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200417 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200418 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200419 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
420 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200421 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200422 return event
423 }
424
425 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200426 desc := fmt.Sprintf("(retry %d)", retries)
427 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200428 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
429 if timedOut {
430 continue
431 }
432 break
433 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200434 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200435 return event
436}
437
438//-------------------------------------------------------------------
439// handle from E2T Subscription Reponse
440//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200441func (c *Control) handleE2TSubscriptionResponse(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200442 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200443 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
444 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200445 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200446 return
447 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200448 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.Seq})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200449 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200450 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200451 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200452 }
453 trans := subs.GetTransaction()
454 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200455 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200456 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200457 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200458 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200459 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
460 if sendOk == false {
461 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200462 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000463 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000464 return
kalnagy45114752019-06-18 14:40:39 +0200465}
466
Juha Hyttinen422d0182020-01-17 13:37:05 +0200467//-------------------------------------------------------------------
468// handle from E2T Subscription Failure
469//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200470func (c *Control) handleE2TSubscriptionFailure(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200471 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200472 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000473 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200474 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000475 return
476 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200477 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.Seq})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200478 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200479 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200480 return
481 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200482 trans := subs.GetTransaction()
483 if trans == nil {
484 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200485 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200486 return
487 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200488 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
489 if sendOk == false {
490 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200491 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200492 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200493 return
494}
495
Juha Hyttinen422d0182020-01-17 13:37:05 +0200496//-------------------------------------------------------------------
497// handle from E2T Subscription Delete Response
498//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200499func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapptweaks.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200500 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200501 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200502 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200503 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200504 return
505 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200506 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.Seq})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200507 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200508 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200509 return
510 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200511 trans := subs.GetTransaction()
512 if trans == nil {
513 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200514 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200515 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200516 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200517 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
518 if sendOk == false {
519 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200520 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +0200521 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200522 return
523}
524
Juha Hyttinen422d0182020-01-17 13:37:05 +0200525//-------------------------------------------------------------------
526// handle from E2T Subscription Delete Failure
527//-------------------------------------------------------------------
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200528func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapptweaks.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200529 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200530 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200531 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200532 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200533 return
534 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200535 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.Seq})
Anssi Mannila8046c702020-01-02 13:39:05 +0200536 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200537 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +0200538 return
539 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200540 trans := subs.GetTransaction()
541 if trans == nil {
542 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200543 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +0200544 return
545 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200546 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
547 if sendOk == false {
548 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200549 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200550 }
kalnagy93cc3e22019-09-19 11:29:29 +0200551 return
552}
553
Juha Hyttinen422d0182020-01-17 13:37:05 +0200554//-------------------------------------------------------------------
555//
556//-------------------------------------------------------------------
557func typeofSubsMessage(v interface{}) string {
558 if v == nil {
559 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +0200560 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200561 switch v.(type) {
562 case *e2ap.E2APSubscriptionRequest:
563 return "SubReq"
564 case *e2ap.E2APSubscriptionResponse:
565 return "SubResp"
566 case *e2ap.E2APSubscriptionFailure:
567 return "SubFail"
568 case *e2ap.E2APSubscriptionDeleteRequest:
569 return "SubDelReq"
570 case *e2ap.E2APSubscriptionDeleteResponse:
571 return "SubDelResp"
572 case *e2ap.E2APSubscriptionDeleteFailure:
573 return "SubDelFail"
574 default:
575 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200576 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200577}