blob: ac142eb4688521d95ad57cdd50dc190806005ee1 [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"
Anssi Mannilac92b4212020-12-07 14:59:34 +020024 "net/http"
25 "os"
26 "strconv"
27 "strings"
28 "time"
29
Juha Hyttinen86a46202020-01-14 12:49:09 +020030 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
kalnagy93cc3e22019-09-19 11:29:29 +020031 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020032 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
kalnagy1455c852019-10-21 13:06:23 +020033 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
34 httptransport "github.com/go-openapi/runtime/client"
35 "github.com/go-openapi/strfmt"
Anssi Mannilac92b4212020-12-07 14:59:34 +020036 "github.com/gorilla/mux"
kalnagy1455c852019-10-21 13:06:23 +020037 "github.com/spf13/viper"
kalnagy45114752019-06-18 14:40:39 +020038)
39
Juha Hyttinene406a342020-01-13 13:02:26 +020040//-----------------------------------------------------------------------------
41//
42//-----------------------------------------------------------------------------
43
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020044func idstring(err error, entries ...fmt.Stringer) string {
45 var retval string = ""
46 var filler string = ""
47 for _, entry := range entries {
48 retval += filler + entry.String()
49 filler = " "
50 }
51 if err != nil {
52 retval += filler + "err(" + err.Error() + ")"
53 filler = " "
54
55 }
56 return retval
57}
58
59//-----------------------------------------------------------------------------
60//
61//-----------------------------------------------------------------------------
62
Anssi Mannila2f26fb22020-12-07 08:32:13 +020063var e2tSubReqTimeout time.Duration
64var e2tSubDelReqTime time.Duration
65var e2tRecvMsgTimeout time.Duration
66var e2tMaxSubReqTryCount uint64 // Initial try + retry
67var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
Anssi Mannilac92b4212020-12-07 14:59:34 +020068var readSubsFromDb string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020069
kalnagy45114752019-06-18 14:40:39 +020070type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030071 *xapp.RMRClient
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020072 e2ap *E2ap
73 registry *Registry
74 tracker *Tracker
Anssi Mannilac92b4212020-12-07 14:59:34 +020075 db Sdlnterface
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020076 //subscriber *xapp.Subscriber
Anssi Mannilac92b4212020-12-07 14:59:34 +020077 CntRecvMsg uint64
78 ResetTestFlag bool
Anssi Mannila4abf1802021-01-28 13:06:46 +020079 Counters map[string]xapp.Counter
kalnagy93cc3e22019-09-19 11:29:29 +020080}
81
82type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020083 PlmnID string
84 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020085 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020086}
87
Anssi Mannilac92b4212020-12-07 14:59:34 +020088type SubmgrRestartTestEvent struct{}
89type SubmgrRestartUpEvent struct{}
90
Peter Szilagyifbc56f92019-07-23 19:29:46 +000091func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020092 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000093 viper.AutomaticEnv()
94 viper.SetEnvPrefix("submgr")
95 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020096}
97
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020098func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020099
Anssi Mannilac92b4212020-12-07 14:59:34 +0200100 ReadConfigParameters()
101 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
102 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
103
104 registry := new(Registry)
105 registry.Initialize()
106 registry.rtmgrClient = &rtmgrClient
107
108 tracker := new(Tracker)
109 tracker.Init()
110
111 //subscriber := xapp.NewSubscriber(viper.GetString("subscription.host"), viper.GetInt("subscription.timeout"))
112
113 c := &Control{e2ap: new(E2ap),
114 registry: registry,
115 tracker: tracker,
116 db: CreateSdl(),
117 //subscriber: subscriber,
Anssi Mannila4abf1802021-01-28 13:06:46 +0200118 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
Anssi Mannilac92b4212020-12-07 14:59:34 +0200119 }
120
121 // Register REST handler for testing support
122 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
123
124 go xapp.Subscription.Listen(c.SubscriptionHandler, c.QueryHandler, c.SubscriptionDeleteHandler)
125 //go c.subscriber.Listen(c.SubscriptionHandler, c.QueryHandler)
126
127 if readSubsFromDb == "false" {
128 return c
129 }
130
131 // Read subscriptions from db
132 xapp.Logger.Info("Reading subscriptions from db")
133 subIds, register, err := c.ReadAllSubscriptionsFromSdl()
134 if err != nil {
135 xapp.Logger.Error("%v", err)
136 } else {
137 c.registry.subIds = subIds
138 c.registry.register = register
139 c.HandleUncompletedSubscriptions(register)
140 }
141 return c
142}
143
144//-------------------------------------------------------------------
145//
146//-------------------------------------------------------------------
147func ReadConfigParameters() {
148
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200149 // viper.GetDuration returns nanoseconds
150 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200151 if e2tSubReqTimeout == 0 {
152 e2tSubReqTimeout = 2000 * 1000000
153 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200154 xapp.Logger.Info("e2tSubReqTimeout %v", e2tSubReqTimeout)
155 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200156 if e2tSubDelReqTime == 0 {
157 e2tSubDelReqTime = 2000 * 1000000
158 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200159 xapp.Logger.Info("e2tSubDelReqTime %v", e2tSubDelReqTime)
160 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200161 if e2tRecvMsgTimeout == 0 {
162 e2tRecvMsgTimeout = 2000 * 1000000
163 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200164 xapp.Logger.Info("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
165 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200166 if e2tMaxSubReqTryCount == 0 {
167 e2tMaxSubReqTryCount = 1
168 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200169 xapp.Logger.Info("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
170 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200171 if e2tMaxSubDelReqTryCount == 0 {
172 e2tMaxSubDelReqTryCount = 1
173 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200174 xapp.Logger.Info("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
175
Anssi Mannilac92b4212020-12-07 14:59:34 +0200176 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
177 if readSubsFromDb == "" {
178 readSubsFromDb = "true"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200179 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200180 xapp.Logger.Info("readSubsFromDb %v", readSubsFromDb)
181}
182
183//-------------------------------------------------------------------
184//
185//-------------------------------------------------------------------
186func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
187
188 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
189 for subId, subs := range register {
190 if subs.SubRespRcvd == false {
191 subs.NoRespToXapp = true
192 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
193 c.SendSubscriptionDeleteReq(subs)
194 }
195 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200196}
197
198func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300199 if c.RMRClient == nil {
200 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200201 }
kalnagy45114752019-06-18 14:40:39 +0200202}
203
204func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200205 xapp.SetReadyCB(c.ReadyCB, nil)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000206 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200207}
208
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200209//-------------------------------------------------------------------
210//
211//-------------------------------------------------------------------
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300212func (c *Control) SubscriptionHandler(stype models.SubscriptionType, params interface{}) (*models.SubscriptionResponse, error) {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200213 /*
214 switch p := params.(type) {
215 case *models.ReportParams:
216 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(p.ClientEndpoint),"" , 0, &xapp.RMRMeid{RanName: p.Meid})
217 if trans == nil {
218 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
219 return
220 }
221 defer trans.Release()
222 case *models.ControlParams:
223 case *models.PolicyParams:
224 }
225 */
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300226 return &models.SubscriptionResponse{}, fmt.Errorf("Subscription rest interface not implemented")
227}
228
Anssi Mannilac92b4212020-12-07 14:59:34 +0200229func (c *Control) SubscriptionDeleteHandler(s string) error {
230 return nil
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200231}
232
233func (c *Control) QueryHandler() (models.SubscriptionList, error) {
Anssi Mannila4c626a22021-02-11 12:50:48 +0200234 xapp.Logger.Info("QueryHandler() called")
235
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200236 return c.registry.QueryHandler()
237}
238
Anssi Mannilac92b4212020-12-07 14:59:34 +0200239func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200240 xapp.Logger.Info("TestRestHandler() called")
241
242 pathParams := mux.Vars(r)
243 s := pathParams["testId"]
244
245 // This can be used to delete single subscription from db
246 if contains := strings.Contains(s, "deletesubid="); contains == true {
247 var splits = strings.Split(s, "=")
248 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
249 xapp.Logger.Info("RemoveSubscriptionFromSdl() called. subId = %v", subId)
250 c.RemoveSubscriptionFromSdl(uint32(subId))
251 return
252 }
253 }
254
255 // This can be used to remove all subscriptions db from
256 if s == "emptydb" {
257 xapp.Logger.Info("RemoveAllSubscriptionsFromSdl() called")
258 c.RemoveAllSubscriptionsFromSdl()
259 return
260 }
261
262 // This is meant to cause submgr's restart in testing
263 if s == "restart" {
264 xapp.Logger.Info("os.Exit(1) called")
265 os.Exit(1)
266 }
267
268 xapp.Logger.Info("Unsupported rest command received %s", s)
269}
270
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200271//-------------------------------------------------------------------
272//
273//-------------------------------------------------------------------
274
Juha Hyttinen83ada002020-01-30 10:36:33 +0200275func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300276 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200277 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300278 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200279 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200280 params.Meid = subs.GetMeid()
281 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200282 params.PayloadLen = len(trans.Payload.Buf)
283 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200284 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200285 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300286 return c.SendWithRetry(params, false, 5)
kalnagye0018682019-09-26 16:28:25 +0200287}
288
Juha Hyttinen83ada002020-01-30 10:36:33 +0200289func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
290
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300291 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200292 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300293 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200294 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200295 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200296 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200297 params.PayloadLen = len(trans.Payload.Buf)
298 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200299 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200300 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300301 return c.SendWithRetry(params, false, 5)
Juha Hyttinene406a342020-01-13 13:02:26 +0200302}
303
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300304func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
305 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200306 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
307 xapp.Logger.Error("%s", err.Error())
308 return
309 }
310 c.CntRecvMsg++
311
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300312 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200313
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300314 // xapp-frame might use direct access to c buffer and
315 // when msg.Mbuf is freed, someone might take it into use
316 // and payload data might be invalid inside message handle function
317 //
318 // subscriptions won't load system a lot so there is no
319 // real performance hit by cloning buffer into new go byte slice
320 cPay := append(msg.Payload[:0:0], msg.Payload...)
321 msg.Payload = cPay
322 msg.PayloadLen = len(cPay)
323
Anssi Mannila90fa0212019-12-12 10:47:47 +0200324 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200325 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200326 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200327 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200328 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200329 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200330 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200331 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200332 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200333 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200334 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200335 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200336 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200337 default:
338 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000339 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200340 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200341}
342
Juha Hyttinen422d0182020-01-17 13:37:05 +0200343//-------------------------------------------------------------------
344// handle from XAPP Subscription Request
345//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300346func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200347 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200348 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200349
350 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
351 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200352 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200353 return
354 }
355
Anssi Mannila4c626a22021-02-11 12:50:48 +0200356 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200357 if trans == nil {
358 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200359 return
360 }
361 defer trans.Release()
362
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200363 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200364 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
365 return
366 }
367
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300368 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannila4abf1802021-01-28 13:06:46 +0200369 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200370 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200371 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200372 return
373 }
374
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200375 c.wakeSubscriptionRequest(subs, trans)
376}
377
378//-------------------------------------------------------------------
379// Wake Subscription Request to E2node
380//------------------------------------------------------------------
381func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
382
Juha Hyttinen422d0182020-01-17 13:37:05 +0200383 go c.handleSubscriptionCreate(subs, trans)
384 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200385 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200386 if event != nil {
387 switch themsg := event.(type) {
388 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200389 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200390 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
391 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200392 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200393 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200394 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200395 return
396 }
397 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200398 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200399 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
400 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200401 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200402 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200403 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200404 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200405 break
406 }
407 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200408 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300409 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200410}
411
Juha Hyttinen422d0182020-01-17 13:37:05 +0200412//-------------------------------------------------------------------
413// handle from XAPP Subscription Delete Request
414//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300415func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200416 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200417 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200418
419 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200420 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200421 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200422 return
423 }
424
Anssi Mannila4c626a22021-02-11 12:50:48 +0200425 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200426 if trans == nil {
427 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200428 return
429 }
430 defer trans.Release()
431
Juha Hyttinen83ada002020-01-30 10:36:33 +0200432 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200433 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200434 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
435 return
436 }
437
Juha Hyttinen47942b42020-02-27 10:41:43 +0200438 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200439 if err != nil {
440 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200441 return
442 }
443
Juha Hyttinen422d0182020-01-17 13:37:05 +0200444 //
445 // Wake subs delete
446 //
447 go c.handleSubscriptionDelete(subs, trans)
448 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
449
Juha Hyttinen83ada002020-01-30 10:36:33 +0200450 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
451
Anssi Mannilac92b4212020-12-07 14:59:34 +0200452 if subs.NoRespToXapp == true {
453 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
454 return
455 }
456
457 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200458 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +0200459 subDelRespMsg.RequestId.Id = trans.RequestId.Id
460 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200461 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
462 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
463 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200464 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200465 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200466 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200467
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300468 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
469 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200470}
471
472//-------------------------------------------------------------------
473// SUBS CREATE Handling
474//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200475func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200476
Anssi Mannilac92b4212020-12-07 14:59:34 +0200477 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +0200478 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200479 subs.WaitTransactionTurn(trans)
480 defer subs.ReleaseTransactionTurn(trans)
481 defer trans.Release()
482
Juha Hyttinen83ada002020-01-30 10:36:33 +0200483 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200484
Juha Hyttinen83ada002020-01-30 10:36:33 +0200485 subRfMsg, valid := subs.GetCachedResponse()
486 if subRfMsg == nil && valid == true {
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200487 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
488 switch event.(type) {
489 case *e2ap.E2APSubscriptionResponse:
490 subRfMsg, valid = subs.SetCachedResponse(event, true)
491 subs.SubRespRcvd = true
492 case *e2ap.E2APSubscriptionFailure:
493 removeSubscriptionFromDb = true
494 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaebaeae62021-02-12 11:56:20 +0200495 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200496 case *SubmgrRestartTestEvent:
497 // This simulates that no response has been received and after restart subscriptions are restored from db
498 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
499 return
500 default:
501 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
502 removeSubscriptionFromDb = true
503 subRfMsg, valid = subs.SetCachedResponse(nil, false)
504 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200505 }
506 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
507 } else {
508 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200509 }
510
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300511 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
512 if valid == false {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200513 c.registry.RemoveFromSubscription(subs, parentTrans, 5*time.Second, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300514 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200515
516 c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200517 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200518}
519
520//-------------------------------------------------------------------
521// SUBS DELETE Handling
522//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200523
Juha Hyttinen83ada002020-01-30 10:36:33 +0200524func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200525
Juha Hyttinen83ada002020-01-30 10:36:33 +0200526 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200527 subs.WaitTransactionTurn(trans)
528 defer subs.ReleaseTransactionTurn(trans)
529 defer trans.Release()
530
Juha Hyttinen83ada002020-01-30 10:36:33 +0200531 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200532
Juha Hyttinen3944a222020-01-24 11:51:46 +0200533 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200534
Juha Hyttinen3944a222020-01-24 11:51:46 +0200535 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
536 subs.valid = false
537 subs.mutex.Unlock()
538 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
539 } else {
540 subs.mutex.Unlock()
541 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300542 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
543 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
544 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Anssi Mannilac92b4212020-12-07 14:59:34 +0200545 c.registry.RemoveFromSubscription(subs, parentTrans, 5*time.Second, c)
546 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +0200547 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200548}
549
550//-------------------------------------------------------------------
551// send to E2T Subscription Request
552//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200553func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200554 var err error
555 var event interface{} = nil
556 var timedOut bool = false
557
558 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200559 subReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200560 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
561 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200562 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200563 return event
564 }
565
Anssi Mannilac92b4212020-12-07 14:59:34 +0200566 // Write uncompleted subscrition in db. If no response for subscrition it need to be re-processed (deleted) after restart
567 c.WriteSubscriptionToDb(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200568 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200569 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200570 if retries == 0 {
571 c.UpdateCounter(cSubReqToE2)
572 } else {
573 c.UpdateCounter(cSubReReqToE2)
574 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200575 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200576 if subs.DoNotWaitSubResp == false {
577 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
578 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200579 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200580 continue
581 }
582 } else {
583 // Simulating case where subscrition request has been sent but response has not been received before restart
584 event = &SubmgrRestartTestEvent{}
Juha Hyttinen422d0182020-01-17 13:37:05 +0200585 }
586 break
587 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200588 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200589 return event
590}
591
592//-------------------------------------------------------------------
593// send to E2T Subscription Delete Request
594//-------------------------------------------------------------------
595
Juha Hyttinen83ada002020-01-30 10:36:33 +0200596func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200597 var err error
598 var event interface{}
599 var timedOut bool
600
601 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200602 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200603 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200604 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
605 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200606 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200607 return event
608 }
609
610 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200611 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200612 if retries == 0 {
613 c.UpdateCounter(cSubDelReqToE2)
614 } else {
615 c.UpdateCounter(cSubDelReReqToE2)
616 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200617 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200618 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
619 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200620 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200621 continue
622 }
623 break
624 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200625 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200626 return event
627}
628
629//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +0200630// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200631//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300632func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200633 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200634 c.UpdateCounter(cSubRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200635 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
636 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200637 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200638 return
639 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300640 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200641 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200642 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200643 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200644 }
645 trans := subs.GetTransaction()
646 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200647 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200648 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200649 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200650 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200651 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
652 if sendOk == false {
653 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200654 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000655 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000656 return
kalnagy45114752019-06-18 14:40:39 +0200657}
658
Juha Hyttinen422d0182020-01-17 13:37:05 +0200659//-------------------------------------------------------------------
660// handle from E2T Subscription Failure
661//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300662func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200663 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200664 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200665 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000666 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200667 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000668 return
669 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300670 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200671 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200672 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200673 return
674 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200675 trans := subs.GetTransaction()
676 if trans == nil {
677 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200678 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200679 return
680 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200681 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
682 if sendOk == false {
683 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200684 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200685 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200686 return
687}
688
Juha Hyttinen422d0182020-01-17 13:37:05 +0200689//-------------------------------------------------------------------
690// handle from E2T Subscription Delete Response
691//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300692func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200693 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200694 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200695 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200696 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200697 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200698 return
699 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300700 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200701 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200702 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200703 return
704 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200705 trans := subs.GetTransaction()
706 if trans == nil {
707 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200708 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200709 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200710 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200711 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
712 if sendOk == false {
713 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200714 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +0200715 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200716 return
717}
718
Juha Hyttinen422d0182020-01-17 13:37:05 +0200719//-------------------------------------------------------------------
720// handle from E2T Subscription Delete Failure
721//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300722func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200723 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200724 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200725 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200726 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200727 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200728 return
729 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300730 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +0200731 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200732 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +0200733 return
734 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200735 trans := subs.GetTransaction()
736 if trans == nil {
737 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200738 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +0200739 return
740 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200741 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
742 if sendOk == false {
743 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200744 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200745 }
kalnagy93cc3e22019-09-19 11:29:29 +0200746 return
747}
748
Juha Hyttinen422d0182020-01-17 13:37:05 +0200749//-------------------------------------------------------------------
750//
751//-------------------------------------------------------------------
752func typeofSubsMessage(v interface{}) string {
753 if v == nil {
754 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +0200755 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200756 switch v.(type) {
757 case *e2ap.E2APSubscriptionRequest:
758 return "SubReq"
759 case *e2ap.E2APSubscriptionResponse:
760 return "SubResp"
761 case *e2ap.E2APSubscriptionFailure:
762 return "SubFail"
763 case *e2ap.E2APSubscriptionDeleteRequest:
764 return "SubDelReq"
765 case *e2ap.E2APSubscriptionDeleteResponse:
766 return "SubDelResp"
767 case *e2ap.E2APSubscriptionDeleteFailure:
768 return "SubDelFail"
769 default:
770 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200771 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200772}
Anssi Mannilac92b4212020-12-07 14:59:34 +0200773
774//-------------------------------------------------------------------
775//
776//-------------------------------------------------------------------
777func (c *Control) WriteSubscriptionToDb(subs *Subscription) {
778 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
779 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
780 if err != nil {
781 xapp.Logger.Error("%v", err)
782 }
783}
784
785//-------------------------------------------------------------------
786//
787//-------------------------------------------------------------------
788func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) {
789
790 if removeSubscriptionFromDb == true {
791 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
792 c.RemoveSubscriptionFromDb(subs)
793 } else {
794 // Update is needed for successful response and merge case here
795 if subs.RetryFromXapp == false {
796 c.WriteSubscriptionToDb(subs)
797 }
798 }
799 subs.RetryFromXapp = false
800}
801
802//-------------------------------------------------------------------
803//
804//-------------------------------------------------------------------
805func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
806 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
807 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
808 if err != nil {
809 xapp.Logger.Error("%v", err)
810 }
811}
812
813func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
814
815 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
816
817 // Send delete for every endpoint in the subscription
818 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
819 subDelReqMsg.RequestId = subs.GetReqId().RequestId
820 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
821 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
822 if err != nil {
823 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
824 return
825 }
826 for _, endPoint := range subs.EpList.Endpoints {
827 params := &xapp.RMRParams{}
828 params.Mtype = mType
829 params.SubId = int(subs.GetReqId().InstanceId)
830 params.Xid = ""
831 params.Meid = subs.Meid
832 params.Src = endPoint.String()
833 params.PayloadLen = len(payload.Buf)
834 params.Payload = payload.Buf
835 params.Mbuf = nil
836
837 if params == nil {
838 xapp.Logger.Error("SendSubscriptionDeleteReq() params == nil")
839 return
840 }
841
842 subs.DeleteFromDb = true
843 c.handleXAPPSubscriptionDeleteRequest(params)
844 }
845}