blob: 59a840e1dbe76f4eea4dd73a36b1d040e2183364 [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 = " "
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020054 }
55 return retval
56}
57
58//-----------------------------------------------------------------------------
59//
60//-----------------------------------------------------------------------------
61
Anssi Mannila2f26fb22020-12-07 08:32:13 +020062var e2tSubReqTimeout time.Duration
63var e2tSubDelReqTime time.Duration
64var e2tRecvMsgTimeout time.Duration
65var e2tMaxSubReqTryCount uint64 // Initial try + retry
66var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
Anssi Mannilac92b4212020-12-07 14:59:34 +020067var readSubsFromDb string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020068
kalnagy45114752019-06-18 14:40:39 +020069type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030070 *xapp.RMRClient
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020071 e2ap *E2ap
72 registry *Registry
73 tracker *Tracker
Anssi Mannilac92b4212020-12-07 14:59:34 +020074 db Sdlnterface
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020075 //subscriber *xapp.Subscriber
Anssi Mannilac92b4212020-12-07 14:59:34 +020076 CntRecvMsg uint64
77 ResetTestFlag bool
Anssi Mannila4abf1802021-01-28 13:06:46 +020078 Counters map[string]xapp.Counter
kalnagy93cc3e22019-09-19 11:29:29 +020079}
80
81type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020082 PlmnID string
83 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020084 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020085}
86
Anssi Mannilac92b4212020-12-07 14:59:34 +020087type SubmgrRestartTestEvent struct{}
88type SubmgrRestartUpEvent struct{}
89
Peter Szilagyifbc56f92019-07-23 19:29:46 +000090func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020091 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000092 viper.AutomaticEnv()
93 viper.SetEnvPrefix("submgr")
94 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020095}
96
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020097func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020098
Anssi Mannilac92b4212020-12-07 14:59:34 +020099 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
100 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
101
102 registry := new(Registry)
103 registry.Initialize()
104 registry.rtmgrClient = &rtmgrClient
105
106 tracker := new(Tracker)
107 tracker.Init()
108
Anssi Mannilac92b4212020-12-07 14:59:34 +0200109 c := &Control{e2ap: new(E2ap),
110 registry: registry,
111 tracker: tracker,
112 db: CreateSdl(),
113 //subscriber: subscriber,
Anssi Mannila4abf1802021-01-28 13:06:46 +0200114 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
Anssi Mannilac92b4212020-12-07 14:59:34 +0200115 }
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200116 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200117
118 // Register REST handler for testing support
119 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
120
121 go xapp.Subscription.Listen(c.SubscriptionHandler, c.QueryHandler, c.SubscriptionDeleteHandler)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200122
123 if readSubsFromDb == "false" {
124 return c
125 }
126
127 // Read subscriptions from db
128 xapp.Logger.Info("Reading subscriptions from db")
129 subIds, register, err := c.ReadAllSubscriptionsFromSdl()
130 if err != nil {
131 xapp.Logger.Error("%v", err)
132 } else {
133 c.registry.subIds = subIds
134 c.registry.register = register
135 c.HandleUncompletedSubscriptions(register)
136 }
137 return c
138}
139
140//-------------------------------------------------------------------
141//
142//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200143func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200144
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200145 // viper.GetDuration returns nanoseconds
146 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200147 if e2tSubReqTimeout == 0 {
148 e2tSubReqTimeout = 2000 * 1000000
149 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200150 xapp.Logger.Info("e2tSubReqTimeout %v", e2tSubReqTimeout)
151 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200152 if e2tSubDelReqTime == 0 {
153 e2tSubDelReqTime = 2000 * 1000000
154 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200155 xapp.Logger.Info("e2tSubDelReqTime %v", e2tSubDelReqTime)
156 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200157 if e2tRecvMsgTimeout == 0 {
158 e2tRecvMsgTimeout = 2000 * 1000000
159 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200160 xapp.Logger.Info("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
161 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200162 if e2tMaxSubReqTryCount == 0 {
163 e2tMaxSubReqTryCount = 1
164 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200165 xapp.Logger.Info("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
166 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200167 if e2tMaxSubDelReqTryCount == 0 {
168 e2tMaxSubDelReqTryCount = 1
169 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200170 xapp.Logger.Info("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
171
Anssi Mannilac92b4212020-12-07 14:59:34 +0200172 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
173 if readSubsFromDb == "" {
174 readSubsFromDb = "true"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200175 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200176 xapp.Logger.Info("readSubsFromDb %v", readSubsFromDb)
177}
178
179//-------------------------------------------------------------------
180//
181//-------------------------------------------------------------------
182func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
183
184 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
185 for subId, subs := range register {
186 if subs.SubRespRcvd == false {
187 subs.NoRespToXapp = true
188 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
189 c.SendSubscriptionDeleteReq(subs)
190 }
191 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200192}
193
194func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300195 if c.RMRClient == nil {
196 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200197 }
kalnagy45114752019-06-18 14:40:39 +0200198}
199
200func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200201 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200202 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000203 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200204}
205
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200206//-------------------------------------------------------------------
207//
208//-------------------------------------------------------------------
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300209func (c *Control) SubscriptionHandler(stype models.SubscriptionType, params interface{}) (*models.SubscriptionResponse, error) {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200210 /*
211 switch p := params.(type) {
212 case *models.ReportParams:
213 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(p.ClientEndpoint),"" , 0, &xapp.RMRMeid{RanName: p.Meid})
214 if trans == nil {
215 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
216 return
217 }
218 defer trans.Release()
219 case *models.ControlParams:
220 case *models.PolicyParams:
221 }
222 */
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300223 return &models.SubscriptionResponse{}, fmt.Errorf("Subscription rest interface not implemented")
224}
225
Anssi Mannilac92b4212020-12-07 14:59:34 +0200226func (c *Control) SubscriptionDeleteHandler(s string) error {
227 return nil
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200228}
229
230func (c *Control) QueryHandler() (models.SubscriptionList, error) {
Anssi Mannila4c626a22021-02-11 12:50:48 +0200231 xapp.Logger.Info("QueryHandler() called")
232
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200233 return c.registry.QueryHandler()
234}
235
Anssi Mannilac92b4212020-12-07 14:59:34 +0200236func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200237 xapp.Logger.Info("TestRestHandler() called")
238
239 pathParams := mux.Vars(r)
240 s := pathParams["testId"]
241
242 // This can be used to delete single subscription from db
243 if contains := strings.Contains(s, "deletesubid="); contains == true {
244 var splits = strings.Split(s, "=")
245 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
246 xapp.Logger.Info("RemoveSubscriptionFromSdl() called. subId = %v", subId)
247 c.RemoveSubscriptionFromSdl(uint32(subId))
248 return
249 }
250 }
251
252 // This can be used to remove all subscriptions db from
253 if s == "emptydb" {
254 xapp.Logger.Info("RemoveAllSubscriptionsFromSdl() called")
255 c.RemoveAllSubscriptionsFromSdl()
256 return
257 }
258
259 // This is meant to cause submgr's restart in testing
260 if s == "restart" {
261 xapp.Logger.Info("os.Exit(1) called")
262 os.Exit(1)
263 }
264
265 xapp.Logger.Info("Unsupported rest command received %s", s)
266}
267
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200268//-------------------------------------------------------------------
269//
270//-------------------------------------------------------------------
271
Juha Hyttinen83ada002020-01-30 10:36:33 +0200272func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300273 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200274 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300275 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200276 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200277 params.Meid = subs.GetMeid()
278 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200279 params.PayloadLen = len(trans.Payload.Buf)
280 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200281 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200282 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300283 return c.SendWithRetry(params, false, 5)
kalnagye0018682019-09-26 16:28:25 +0200284}
285
Juha Hyttinen83ada002020-01-30 10:36:33 +0200286func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
287
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300288 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200289 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300290 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200291 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200292 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200293 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200294 params.PayloadLen = len(trans.Payload.Buf)
295 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200296 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200297 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300298 return c.SendWithRetry(params, false, 5)
Juha Hyttinene406a342020-01-13 13:02:26 +0200299}
300
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300301func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
302 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200303 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
304 xapp.Logger.Error("%s", err.Error())
305 return
306 }
307 c.CntRecvMsg++
308
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300309 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200310
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300311 // xapp-frame might use direct access to c buffer and
312 // when msg.Mbuf is freed, someone might take it into use
313 // and payload data might be invalid inside message handle function
314 //
315 // subscriptions won't load system a lot so there is no
316 // real performance hit by cloning buffer into new go byte slice
317 cPay := append(msg.Payload[:0:0], msg.Payload...)
318 msg.Payload = cPay
319 msg.PayloadLen = len(cPay)
320
Anssi Mannila90fa0212019-12-12 10:47:47 +0200321 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200322 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200323 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200324 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200325 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200326 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200327 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200328 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200329 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200330 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200331 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200332 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200333 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200334 default:
335 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000336 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200337 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200338}
339
Juha Hyttinen422d0182020-01-17 13:37:05 +0200340//-------------------------------------------------------------------
341// handle from XAPP Subscription Request
342//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300343func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200344 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200345 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200346
347 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
348 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200349 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200350 return
351 }
352
Anssi Mannila4c626a22021-02-11 12:50:48 +0200353 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200354 if trans == nil {
355 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200356 return
357 }
358 defer trans.Release()
359
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200360 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200361 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
362 return
363 }
364
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300365 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannila4abf1802021-01-28 13:06:46 +0200366 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200367 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200368 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200369 return
370 }
371
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200372 c.wakeSubscriptionRequest(subs, trans)
373}
374
375//-------------------------------------------------------------------
376// Wake Subscription Request to E2node
377//------------------------------------------------------------------
378func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
379
Juha Hyttinen422d0182020-01-17 13:37:05 +0200380 go c.handleSubscriptionCreate(subs, trans)
381 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200382 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200383 if event != nil {
384 switch themsg := event.(type) {
385 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200386 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200387 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
388 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200389 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200390 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200391 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200392 return
393 }
394 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200395 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200396 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
397 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200398 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200399 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200400 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200401 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200402 break
403 }
404 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200405 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300406 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200407}
408
Juha Hyttinen422d0182020-01-17 13:37:05 +0200409//-------------------------------------------------------------------
410// handle from XAPP Subscription Delete Request
411//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300412func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200413 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200414 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200415
416 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200417 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200418 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200419 return
420 }
421
Anssi Mannila4c626a22021-02-11 12:50:48 +0200422 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200423 if trans == nil {
424 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200425 return
426 }
427 defer trans.Release()
428
Juha Hyttinen83ada002020-01-30 10:36:33 +0200429 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200430 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200431 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
432 return
433 }
434
Juha Hyttinen47942b42020-02-27 10:41:43 +0200435 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200436 if err != nil {
437 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200438 return
439 }
440
Juha Hyttinen422d0182020-01-17 13:37:05 +0200441 //
442 // Wake subs delete
443 //
444 go c.handleSubscriptionDelete(subs, trans)
445 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
446
Juha Hyttinen83ada002020-01-30 10:36:33 +0200447 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
448
Anssi Mannilac92b4212020-12-07 14:59:34 +0200449 if subs.NoRespToXapp == true {
450 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
451 return
452 }
453
454 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200455 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +0200456 subDelRespMsg.RequestId.Id = trans.RequestId.Id
457 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200458 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
459 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
460 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200461 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200462 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200463 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200464
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300465 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
466 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200467}
468
469//-------------------------------------------------------------------
470// SUBS CREATE Handling
471//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200472func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200473
Anssi Mannilac92b4212020-12-07 14:59:34 +0200474 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +0200475 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200476 subs.WaitTransactionTurn(trans)
477 defer subs.ReleaseTransactionTurn(trans)
478 defer trans.Release()
479
Juha Hyttinen83ada002020-01-30 10:36:33 +0200480 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200481
Juha Hyttinen83ada002020-01-30 10:36:33 +0200482 subRfMsg, valid := subs.GetCachedResponse()
483 if subRfMsg == nil && valid == true {
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200484 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
485 switch event.(type) {
486 case *e2ap.E2APSubscriptionResponse:
487 subRfMsg, valid = subs.SetCachedResponse(event, true)
488 subs.SubRespRcvd = true
489 case *e2ap.E2APSubscriptionFailure:
490 removeSubscriptionFromDb = true
491 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaebaeae62021-02-12 11:56:20 +0200492 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 +0200493 case *SubmgrRestartTestEvent:
494 // This simulates that no response has been received and after restart subscriptions are restored from db
495 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
496 return
497 default:
498 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
499 removeSubscriptionFromDb = true
500 subRfMsg, valid = subs.SetCachedResponse(nil, false)
501 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200502 }
503 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
504 } else {
505 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200506 }
507
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300508 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
509 if valid == false {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200510 c.registry.RemoveFromSubscription(subs, parentTrans, 5*time.Second, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300511 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200512
513 c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200514 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200515}
516
517//-------------------------------------------------------------------
518// SUBS DELETE Handling
519//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200520
Juha Hyttinen83ada002020-01-30 10:36:33 +0200521func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200522
Juha Hyttinen83ada002020-01-30 10:36:33 +0200523 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200524 subs.WaitTransactionTurn(trans)
525 defer subs.ReleaseTransactionTurn(trans)
526 defer trans.Release()
527
Juha Hyttinen83ada002020-01-30 10:36:33 +0200528 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200529
Juha Hyttinen3944a222020-01-24 11:51:46 +0200530 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200531
Juha Hyttinen3944a222020-01-24 11:51:46 +0200532 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
533 subs.valid = false
534 subs.mutex.Unlock()
535 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
536 } else {
537 subs.mutex.Unlock()
538 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300539 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
540 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
541 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Anssi Mannilac92b4212020-12-07 14:59:34 +0200542 c.registry.RemoveFromSubscription(subs, parentTrans, 5*time.Second, c)
543 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +0200544 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200545}
546
547//-------------------------------------------------------------------
548// send to E2T Subscription Request
549//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200550func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200551 var err error
552 var event interface{} = nil
553 var timedOut bool = false
554
555 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200556 subReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200557 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
558 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200559 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200560 return event
561 }
562
Anssi Mannilac92b4212020-12-07 14:59:34 +0200563 // Write uncompleted subscrition in db. If no response for subscrition it need to be re-processed (deleted) after restart
564 c.WriteSubscriptionToDb(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200565 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200566 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200567 if retries == 0 {
568 c.UpdateCounter(cSubReqToE2)
569 } else {
570 c.UpdateCounter(cSubReReqToE2)
571 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200572 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200573 if subs.DoNotWaitSubResp == false {
574 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
575 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200576 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200577 continue
578 }
579 } else {
580 // Simulating case where subscrition request has been sent but response has not been received before restart
581 event = &SubmgrRestartTestEvent{}
Juha Hyttinen422d0182020-01-17 13:37:05 +0200582 }
583 break
584 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200585 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200586 return event
587}
588
589//-------------------------------------------------------------------
590// send to E2T Subscription Delete Request
591//-------------------------------------------------------------------
592
Juha Hyttinen83ada002020-01-30 10:36:33 +0200593func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200594 var err error
595 var event interface{}
596 var timedOut bool
597
598 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200599 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200600 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200601 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
602 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200603 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200604 return event
605 }
606
607 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200608 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200609 if retries == 0 {
610 c.UpdateCounter(cSubDelReqToE2)
611 } else {
612 c.UpdateCounter(cSubDelReReqToE2)
613 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200614 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200615 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
616 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200617 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200618 continue
619 }
620 break
621 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200622 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200623 return event
624}
625
626//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +0200627// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200628//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300629func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200630 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200631 c.UpdateCounter(cSubRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200632 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
633 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200634 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200635 return
636 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300637 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200638 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200639 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200640 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200641 }
642 trans := subs.GetTransaction()
643 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200644 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200645 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200646 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200647 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200648 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
649 if sendOk == false {
650 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200651 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000652 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000653 return
kalnagy45114752019-06-18 14:40:39 +0200654}
655
Juha Hyttinen422d0182020-01-17 13:37:05 +0200656//-------------------------------------------------------------------
657// handle from E2T Subscription Failure
658//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300659func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200660 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200661 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200662 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000663 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200664 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000665 return
666 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300667 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200668 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200669 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200670 return
671 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200672 trans := subs.GetTransaction()
673 if trans == nil {
674 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200675 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200676 return
677 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200678 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
679 if sendOk == false {
680 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200681 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200682 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200683 return
684}
685
Juha Hyttinen422d0182020-01-17 13:37:05 +0200686//-------------------------------------------------------------------
687// handle from E2T Subscription Delete Response
688//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300689func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200690 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200691 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200692 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200693 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200694 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200695 return
696 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300697 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200698 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200699 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200700 return
701 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200702 trans := subs.GetTransaction()
703 if trans == nil {
704 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200705 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200706 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200707 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200708 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
709 if sendOk == false {
710 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200711 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +0200712 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200713 return
714}
715
Juha Hyttinen422d0182020-01-17 13:37:05 +0200716//-------------------------------------------------------------------
717// handle from E2T Subscription Delete Failure
718//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300719func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200720 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200721 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200722 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200723 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200724 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200725 return
726 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300727 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +0200728 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200729 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +0200730 return
731 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200732 trans := subs.GetTransaction()
733 if trans == nil {
734 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200735 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +0200736 return
737 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200738 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
739 if sendOk == false {
740 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200741 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200742 }
kalnagy93cc3e22019-09-19 11:29:29 +0200743 return
744}
745
Juha Hyttinen422d0182020-01-17 13:37:05 +0200746//-------------------------------------------------------------------
747//
748//-------------------------------------------------------------------
749func typeofSubsMessage(v interface{}) string {
750 if v == nil {
751 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +0200752 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200753 switch v.(type) {
754 case *e2ap.E2APSubscriptionRequest:
755 return "SubReq"
756 case *e2ap.E2APSubscriptionResponse:
757 return "SubResp"
758 case *e2ap.E2APSubscriptionFailure:
759 return "SubFail"
760 case *e2ap.E2APSubscriptionDeleteRequest:
761 return "SubDelReq"
762 case *e2ap.E2APSubscriptionDeleteResponse:
763 return "SubDelResp"
764 case *e2ap.E2APSubscriptionDeleteFailure:
765 return "SubDelFail"
766 default:
767 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200768 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200769}
Anssi Mannilac92b4212020-12-07 14:59:34 +0200770
771//-------------------------------------------------------------------
772//
773//-------------------------------------------------------------------
774func (c *Control) WriteSubscriptionToDb(subs *Subscription) {
775 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
776 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
777 if err != nil {
778 xapp.Logger.Error("%v", err)
779 }
780}
781
782//-------------------------------------------------------------------
783//
784//-------------------------------------------------------------------
785func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) {
786
787 if removeSubscriptionFromDb == true {
788 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
789 c.RemoveSubscriptionFromDb(subs)
790 } else {
791 // Update is needed for successful response and merge case here
792 if subs.RetryFromXapp == false {
793 c.WriteSubscriptionToDb(subs)
794 }
795 }
796 subs.RetryFromXapp = false
797}
798
799//-------------------------------------------------------------------
800//
801//-------------------------------------------------------------------
802func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
803 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
804 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
805 if err != nil {
806 xapp.Logger.Error("%v", err)
807 }
808}
809
810func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
811
812 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
813
814 // Send delete for every endpoint in the subscription
815 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
816 subDelReqMsg.RequestId = subs.GetReqId().RequestId
817 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
818 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
819 if err != nil {
820 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
821 return
822 }
823 for _, endPoint := range subs.EpList.Endpoints {
824 params := &xapp.RMRParams{}
825 params.Mtype = mType
826 params.SubId = int(subs.GetReqId().InstanceId)
827 params.Xid = ""
828 params.Meid = subs.Meid
829 params.Src = endPoint.String()
830 params.PayloadLen = len(payload.Buf)
831 params.Payload = payload.Buf
832 params.Mbuf = nil
833
834 if params == nil {
835 xapp.Logger.Error("SendSubscriptionDeleteReq() params == nil")
836 return
837 }
838
839 subs.DeleteFromDb = true
840 c.handleXAPPSubscriptionDeleteRequest(params)
841 }
842}