blob: 0b39f2c5d08ae69afe0586b2c13b708d3b7f68eb [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"
archaggeafbf95f2021-04-14 08:54:05 +030037 "github.com/segmentio/ksuid"
kalnagy1455c852019-10-21 13:06:23 +020038 "github.com/spf13/viper"
kalnagy45114752019-06-18 14:40:39 +020039)
40
Juha Hyttinene406a342020-01-13 13:02:26 +020041//-----------------------------------------------------------------------------
42//
43//-----------------------------------------------------------------------------
44
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020045func idstring(err error, entries ...fmt.Stringer) string {
46 var retval string = ""
47 var filler string = ""
48 for _, entry := range entries {
Markku Virtanen42723e22021-06-15 10:09:23 +030049 if entry != nil {
50 retval += filler + entry.String()
51 filler = " "
52 } else {
53 retval += filler + "(NIL)"
54 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020055 }
56 if err != nil {
57 retval += filler + "err(" + err.Error() + ")"
58 filler = " "
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020059 }
60 return retval
61}
62
63//-----------------------------------------------------------------------------
64//
65//-----------------------------------------------------------------------------
66
Anssi Mannila2f26fb22020-12-07 08:32:13 +020067var e2tSubReqTimeout time.Duration
68var e2tSubDelReqTime time.Duration
69var e2tRecvMsgTimeout time.Duration
Markku Virtanenfe2cdab2021-05-21 10:59:29 +000070var waitRouteCleanup_ms time.Duration
Anssi Mannila2f26fb22020-12-07 08:32:13 +020071var e2tMaxSubReqTryCount uint64 // Initial try + retry
72var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
Anssi Mannilac92b4212020-12-07 14:59:34 +020073var readSubsFromDb string
Markku Virtanenb642a192021-06-09 09:08:14 +000074var restDuplicateCtrl duplicateCtrl
Anssi Mannilab73e7cd2021-08-03 11:57:11 +030075var dbRetryForever string
76var dbTryCount int
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020077
kalnagy45114752019-06-18 14:40:39 +020078type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030079 *xapp.RMRClient
Anssi Mannilade457a12021-06-02 16:19:48 +030080 e2ap *E2ap
81 registry *Registry
82 tracker *Tracker
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030083 e2SubsDb Sdlnterface
84 restSubsDb Sdlnterface
Anssi Mannilac92b4212020-12-07 14:59:34 +020085 CntRecvMsg uint64
86 ResetTestFlag bool
Anssi Mannila4abf1802021-01-28 13:06:46 +020087 Counters map[string]xapp.Counter
Anssi Mannilade457a12021-06-02 16:19:48 +030088 LoggerLevel uint32
kalnagy93cc3e22019-09-19 11:29:29 +020089}
90
91type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020092 PlmnID string
93 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020094 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020095}
96
Anssi Mannilac92b4212020-12-07 14:59:34 +020097type SubmgrRestartTestEvent struct{}
98type SubmgrRestartUpEvent struct{}
99
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000100func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +0200101 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000102 viper.AutomaticEnv()
103 viper.SetEnvPrefix("submgr")
104 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +0200105}
106
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200107func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200108
Anssi Mannilac92b4212020-12-07 14:59:34 +0200109 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
110 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
111
112 registry := new(Registry)
113 registry.Initialize()
114 registry.rtmgrClient = &rtmgrClient
115
116 tracker := new(Tracker)
117 tracker.Init()
118
Anssi Mannilac92b4212020-12-07 14:59:34 +0200119 c := &Control{e2ap: new(E2ap),
Anssi Mannilade457a12021-06-02 16:19:48 +0300120 registry: registry,
121 tracker: tracker,
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300122 e2SubsDb: CreateSdl(),
123 restSubsDb: CreateRESTSdl(),
Anssi Mannilade457a12021-06-02 16:19:48 +0300124 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
125 LoggerLevel: 3,
Anssi Mannilac92b4212020-12-07 14:59:34 +0200126 }
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200127 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200128
129 // Register REST handler for testing support
130 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300131 xapp.Resource.InjectRoute("/ric/v1/restsubscriptions", c.GetAllRestSubscriptions, "GET")
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000132 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200133
archaggeafbf95f2021-04-14 08:54:05 +0300134 go xapp.Subscription.Listen(c.SubscriptionHandler, c.QueryHandler, c.SubscriptionDeleteHandlerCB)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200135
136 if readSubsFromDb == "false" {
137 return c
138 }
139
Markku Virtanenb642a192021-06-09 09:08:14 +0000140 restDuplicateCtrl.Init()
141
Anssi Mannilac92b4212020-12-07 14:59:34 +0200142 // Read subscriptions from db
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300143 c.ReadE2Subscriptions()
144 c.ReadRESTSubscriptions()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300145
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300146 /*
147 xapp.Logger.Info("Reading subscriptions from db")
148 subIds, register, err := c.ReadAllSubscriptionsFromSdl()
149 if err != nil {
150 xapp.Logger.Error("%v", err)
151 } else {
152 c.registry.subIds = subIds
153 c.registry.register = register
154 c.HandleUncompletedSubscriptions(register)
155 }
156
157 restSubscriptions, err := c.ReadAllRESTSubscriptionsFromSdl()
158 if err != nil {
159 xapp.Logger.Error("%v", err)
160 } else {
161 c.registry.restSubscriptions = restSubscriptions
162 }
163 */
Anssi Mannilac92b4212020-12-07 14:59:34 +0200164 return c
165}
166
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000167func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
168 subscriptions, _ := c.registry.QueryHandler()
169 xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
170}
171
Anssi Mannilac92b4212020-12-07 14:59:34 +0200172//-------------------------------------------------------------------
173//
174//-------------------------------------------------------------------
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300175func (c *Control) GetAllRestSubscriptions(w http.ResponseWriter, r *http.Request) {
176 xapp.Logger.Info("GetAllRestSubscriptions() called")
177 response := c.registry.GetAllRestSubscriptions()
178 w.Write(response)
179}
180
181//-------------------------------------------------------------------
182//
183//-------------------------------------------------------------------
184func (c *Control) ReadE2Subscriptions() error {
185 var err error
186 var subIds []uint32
187 var register map[uint32]*Subscription
188 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
189 xapp.Logger.Info("Reading E2 subscriptions from db")
190 subIds, register, err = c.ReadAllSubscriptionsFromSdl()
191 if err != nil {
192 xapp.Logger.Error("%v", err)
193 <-time.After(1 * time.Second)
194 } else {
195 c.registry.subIds = subIds
196 c.registry.register = register
197 c.HandleUncompletedSubscriptions(register)
198 return nil
199 }
200 }
201 xapp.Logger.Info("Continuing without retring")
202 return err
203}
204
205//-------------------------------------------------------------------
206//
207//-------------------------------------------------------------------
208func (c *Control) ReadRESTSubscriptions() error {
209 var err error
210 var restSubscriptions map[string]*RESTSubscription
211 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
212 xapp.Logger.Info("Reading REST subscriptions from db")
213 restSubscriptions, err = c.ReadAllRESTSubscriptionsFromSdl()
214 if err != nil {
215 xapp.Logger.Error("%v", err)
216 <-time.After(1 * time.Second)
217 } else {
218 c.registry.restSubscriptions = restSubscriptions
219 return nil
220 }
221 }
222 xapp.Logger.Info("Continuing without retring")
223 return err
224}
225
226//-------------------------------------------------------------------
227//
228//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200229func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200230
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200231 // viper.GetDuration returns nanoseconds
232 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200233 if e2tSubReqTimeout == 0 {
234 e2tSubReqTimeout = 2000 * 1000000
235 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200236 xapp.Logger.Info("e2tSubReqTimeout %v", e2tSubReqTimeout)
237 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200238 if e2tSubDelReqTime == 0 {
239 e2tSubDelReqTime = 2000 * 1000000
240 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200241 xapp.Logger.Info("e2tSubDelReqTime %v", e2tSubDelReqTime)
242 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200243 if e2tRecvMsgTimeout == 0 {
244 e2tRecvMsgTimeout = 2000 * 1000000
245 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200246 xapp.Logger.Info("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000247
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200248 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200249 if e2tMaxSubReqTryCount == 0 {
250 e2tMaxSubReqTryCount = 1
251 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200252 xapp.Logger.Info("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000253
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200254 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200255 if e2tMaxSubDelReqTryCount == 0 {
256 e2tMaxSubDelReqTryCount = 1
257 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200258 xapp.Logger.Info("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
259
Anssi Mannilac92b4212020-12-07 14:59:34 +0200260 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
261 if readSubsFromDb == "" {
262 readSubsFromDb = "true"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200263 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200264 xapp.Logger.Info("readSubsFromDb %v", readSubsFromDb)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300265
266 dbTryCount = viper.GetInt("controls.dbTryCount")
267 if dbTryCount == 0 {
268 dbTryCount = 200
269 }
270 xapp.Logger.Info("dbTryCount %v", dbTryCount)
271
272 dbRetryForever = viper.GetString("controls.dbRetryForever")
273 if dbRetryForever == "" {
274 dbRetryForever = "true"
275 }
276 xapp.Logger.Info("dbRetryForever %v", dbRetryForever)
277
Anssi Mannilade457a12021-06-02 16:19:48 +0300278 c.LoggerLevel = viper.GetUint32("logger.level")
279 if c.LoggerLevel == 0 {
280 c.LoggerLevel = 3
281 }
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300282 xapp.Logger.Info("LoggerLevel %v", c.LoggerLevel)
283
284 // Internal cfg parameter, used to define a wait time for RMR route clean-up. None default
285 // value 100ms used currently only in unittests.
286 waitRouteCleanup_ms = viper.GetDuration("controls.waitRouteCleanup_ms") * 1000000
287 if waitRouteCleanup_ms == 0 {
288 waitRouteCleanup_ms = 5000 * 1000000
289 }
290 xapp.Logger.Info("waitRouteCleanup %v", waitRouteCleanup_ms)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200291}
292
293//-------------------------------------------------------------------
294//
295//-------------------------------------------------------------------
296func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
297
298 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
299 for subId, subs := range register {
300 if subs.SubRespRcvd == false {
301 subs.NoRespToXapp = true
302 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
303 c.SendSubscriptionDeleteReq(subs)
304 }
305 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200306}
307
308func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300309 if c.RMRClient == nil {
310 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200311 }
kalnagy45114752019-06-18 14:40:39 +0200312}
313
314func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200315 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200316 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000317 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200318}
319
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200320//-------------------------------------------------------------------
321//
322//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300323func (c *Control) GetOrCreateRestSubscription(p *models.SubscriptionParams, md5sum string, xAppRmrEndpoint string) (*RESTSubscription, string, error) {
324
325 var restSubId string
326 var restSubscription *RESTSubscription
327 var err error
328
329 prevRestSubsId, exists := restDuplicateCtrl.GetLastKnownRestSubsIdBasedOnMd5sum(md5sum)
330 if p.SubscriptionID == "" {
331 if exists {
332 restSubscription, err = c.registry.GetRESTSubscription(prevRestSubsId, false)
333 if restSubscription != nil {
334 restSubId = prevRestSubsId
335 if err == nil {
336 xapp.Logger.Info("Existing restSubId %s found by MD5sum %s for a request without subscription ID - using previous subscription", prevRestSubsId, md5sum)
337 } else {
338 xapp.Logger.Info("Existing restSubId %s found by MD5sum %s for a request without subscription ID - Note: %s", prevRestSubsId, md5sum, err.Error())
339 }
340 } else {
341 xapp.Logger.Info("None existing restSubId %s referred by MD5sum %s for a request without subscription ID - deleting cached entry", prevRestSubsId, md5sum)
342 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
343 }
344 }
345
346 if restSubscription == nil {
347 restSubId = ksuid.New().String()
348 restSubscription, err = c.registry.CreateRESTSubscription(&restSubId, &xAppRmrEndpoint, p.Meid)
349 if err != nil {
350 xapp.Logger.Error("%s", err.Error())
351 c.UpdateCounter(cRestSubFailToXapp)
352 return nil, "", err
353 }
354 }
355 } else {
356 restSubId = p.SubscriptionID
357
358 xapp.Logger.Info("RestSubscription ID %s provided via REST request", restSubId)
359
360 restSubscription, err = c.registry.GetRESTSubscription(restSubId, false)
361 if err != nil {
362 xapp.Logger.Error("%s", err.Error())
363 c.UpdateCounter(cRestSubFailToXapp)
364 return nil, "", err
365 }
366
367 if !exists {
368 xapp.Logger.Info("Existing restSubscription found for ID %s, new request based on md5sum", restSubId)
369 } else {
370 xapp.Logger.Info("Existing restSubscription found for ID %s(%s), re-transmission based on md5sum match with previous request", prevRestSubsId, restSubId)
371 }
372 }
373
374 return restSubscription, restSubId, nil
375}
376
377//-------------------------------------------------------------------
378//
379//-------------------------------------------------------------------
archaggea5c58bc2021-04-14 08:54:05 +0300380func (c *Control) SubscriptionHandler(params interface{}) (*models.SubscriptionResponse, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300381
Anssi Mannila316d8a12021-06-02 11:08:54 +0300382 c.CntRecvMsg++
383 c.UpdateCounter(cRestSubReqFromXapp)
384
archaggeafbf95f2021-04-14 08:54:05 +0300385 subResp := models.SubscriptionResponse{}
archaggeafbf95f2021-04-14 08:54:05 +0300386 p := params.(*models.SubscriptionParams)
387
Anssi Mannilade457a12021-06-02 16:19:48 +0300388 if c.LoggerLevel > 2 {
389 c.PrintRESTSubscriptionRequest(p)
390 }
391
archaggeafbf95f2021-04-14 08:54:05 +0300392 if p.ClientEndpoint == nil {
393 xapp.Logger.Error("ClientEndpoint == nil")
Anssi Mannila316d8a12021-06-02 11:08:54 +0300394 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300395 return nil, fmt.Errorf("")
396 }
397
398 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
399 if err != nil {
400 xapp.Logger.Error("%s", err.Error())
Anssi Mannila316d8a12021-06-02 11:08:54 +0300401 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300402 return nil, err
403 }
404
Markku Virtanen42723e22021-06-15 10:09:23 +0300405 md5sum, err := CalculateRequestMd5sum(params)
406 if err != nil {
407 xapp.Logger.Error("Failed to generate md5sum from incoming request - %s", err.Error())
408 }
409
410 restSubscription, restSubId, err := c.GetOrCreateRestSubscription(p, md5sum, xAppRmrEndpoint)
411 if err != nil {
412 xapp.Logger.Error("Failed to get/allocate REST subscription")
413 return nil, err
archaggeafbf95f2021-04-14 08:54:05 +0300414 }
415
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000416 subResp.SubscriptionID = &restSubId
archaggeafbf95f2021-04-14 08:54:05 +0300417 subReqList := e2ap.SubscriptionRequestList{}
418 err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
419 if err != nil {
420 xapp.Logger.Error("%s", err.Error())
Markku Virtanen42723e22021-06-15 10:09:23 +0300421 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300422 c.registry.DeleteRESTSubscription(&restSubId)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300423 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300424 return nil, err
425 }
426
Markku Virtanen42723e22021-06-15 10:09:23 +0300427 duplicate := restDuplicateCtrl.IsDuplicateToOngoingTransaction(restSubId, md5sum)
428 if duplicate {
429 xapp.Logger.Info("Retransmission blocker direct ACK for request of restSubsId %s restSubId MD5sum %s as retransmission", restSubId, md5sum)
430 c.UpdateCounter(cRestSubRespToXapp)
431 return &subResp, nil
Markku Virtanenb642a192021-06-09 09:08:14 +0000432 }
433
Markku Virtanen42723e22021-06-15 10:09:23 +0300434 go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint, md5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300435
Anssi Mannila316d8a12021-06-02 11:08:54 +0300436 c.UpdateCounter(cRestSubRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300437 return &subResp, nil
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300438}
439
archaggeafbf95f2021-04-14 08:54:05 +0300440//-------------------------------------------------------------------
441//
442//-------------------------------------------------------------------
443
444func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
Markku Virtanen42723e22021-06-15 10:09:23 +0300445 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string) {
archaggeafbf95f2021-04-14 08:54:05 +0300446
447 xapp.Logger.Info("Subscription Request count=%v ", len(subReqList.E2APSubscriptionRequests))
448
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000449 var xAppEventInstanceID int64
450 var e2EventInstanceID int64
Markku Virtanenb642a192021-06-09 09:08:14 +0000451
Markku Virtanen42723e22021-06-15 10:09:23 +0300452 defer restDuplicateCtrl.SetMd5sumFromLastOkRequest(*restSubId, md5sum)
Markku Virtanenb642a192021-06-09 09:08:14 +0000453
archaggeafbf95f2021-04-14 08:54:05 +0300454 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
455 subReqMsg := subReqList.E2APSubscriptionRequests[index]
Markku Virtanenb642a192021-06-09 09:08:14 +0000456 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300457
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000458 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300459 if trans == nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000460 // Send notification to xApp that prosessing of a Subscription Request has failed.
461 err := fmt.Errorf("Tracking failure")
462 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans)
463 continue
archaggeafbf95f2021-04-14 08:54:05 +0300464 }
465
archaggeafbf95f2021-04-14 08:54:05 +0300466 xapp.Logger.Info("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000467
Markku Virtanen42723e22021-06-15 10:09:23 +0300468 subRespMsg, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, *restSubId)
469
470 xapp.Logger.Info("Handled SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
471
archaggeafbf95f2021-04-14 08:54:05 +0300472 if err != nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000473 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans)
archaggeafbf95f2021-04-14 08:54:05 +0300474 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000475 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300476 restSubscription.AddMd5Sum(md5sum)
477 xapp.Logger.Info("SubscriptionRequest index=%v processed successfullyfor %s. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
478 index, *restSubId, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000479 c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans)
archaggeafbf95f2021-04-14 08:54:05 +0300480 }
Markku Virtanenb642a192021-06-09 09:08:14 +0000481 trans.Release()
archaggeafbf95f2021-04-14 08:54:05 +0300482 }
483}
484
485//-------------------------------------------------------------------
486//
487//------------------------------------------------------------------
488func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
Markku Virtanen42723e22021-06-15 10:09:23 +0300489 restSubId string) (*e2ap.E2APSubscriptionResponse, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300490
491 err := c.tracker.Track(trans)
492 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300493 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
494 err = fmt.Errorf("Tracking failure")
archaggeafbf95f2021-04-14 08:54:05 +0300495 return nil, err
496 }
497
498 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
499 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300500 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
archaggeafbf95f2021-04-14 08:54:05 +0300501 return nil, err
502 }
503
504 //
505 // Wake subs request
506 //
507 go c.handleSubscriptionCreate(subs, trans)
508 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
509
510 err = nil
511 if event != nil {
512 switch themsg := event.(type) {
513 case *e2ap.E2APSubscriptionResponse:
514 trans.Release()
515 return themsg, nil
516 case *e2ap.E2APSubscriptionFailure:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300517 err = fmt.Errorf("E2 SubscriptionFailure received")
archaggeafbf95f2021-04-14 08:54:05 +0300518 return nil, err
519 default:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300520 err = fmt.Errorf("unexpected E2 subscription response received")
archaggeafbf95f2021-04-14 08:54:05 +0300521 break
522 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300523 } else {
524 err = fmt.Errorf("E2 subscription response timeout")
archaggeafbf95f2021-04-14 08:54:05 +0300525 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300526
527 xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000528 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300529 return nil, err
530}
531
532//-------------------------------------------------------------------
533//
534//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300535func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
536 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp) {
537
538 // Send notification to xApp that prosessing of a Subscription Request has failed.
539 e2EventInstanceID := (int64)(0)
540 errorCause := err.Error()
541 resp := &models.SubscriptionResponse{
542 SubscriptionID: restSubId,
543 SubscriptionInstances: []*models.SubscriptionInstance{
544 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
545 ErrorCause: &errorCause,
546 XappEventInstanceID: &xAppEventInstanceID},
547 },
548 }
549 // Mark REST subscription request processed.
550 restSubscription.SetProcessed(err)
551 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
552 if trans != nil {
553 xapp.Logger.Info("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
554 errorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
555 } else {
556 xapp.Logger.Info("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
557 errorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
558 }
559
560 c.UpdateCounter(cRestSubFailNotifToXapp)
561 xapp.Subscription.Notify(resp, *clientEndpoint)
562}
563
564//-------------------------------------------------------------------
565//
566//-------------------------------------------------------------------
567func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
568 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp) {
569
570 // Store successfully processed InstanceId for deletion
571 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
572 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
573
574 // Send notification to xApp that a Subscription Request has been processed.
575 resp := &models.SubscriptionResponse{
576 SubscriptionID: restSubId,
577 SubscriptionInstances: []*models.SubscriptionInstance{
578 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
579 ErrorCause: nil,
580 XappEventInstanceID: &xAppEventInstanceID},
581 },
582 }
583 // Mark REST subscription request processesd.
584 restSubscription.SetProcessed(nil)
585 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
586 xapp.Logger.Info("Sending successful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
587 clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
588
589 c.UpdateCounter(cRestSubNotifToXapp)
590 xapp.Subscription.Notify(resp, *clientEndpoint)
591}
592
593//-------------------------------------------------------------------
594//
595//-------------------------------------------------------------------
archaggeafbf95f2021-04-14 08:54:05 +0300596func (c *Control) SubscriptionDeleteHandlerCB(restSubId string) error {
597
598 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000599 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300600
601 xapp.Logger.Info("SubscriptionDeleteRequest from XAPP")
602
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000603 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300604 if err != nil {
605 xapp.Logger.Error("%s", err.Error())
606 if restSubscription == nil {
607 // Subscription was not found
608 return nil
609 } else {
610 if restSubscription.SubReqOngoing == true {
611 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
612 xapp.Logger.Error("%s", err.Error())
613 return err
614 } else if restSubscription.SubDelReqOngoing == true {
615 // Previous request for same restSubId still ongoing
616 return nil
617 }
618 }
619 }
620
621 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
622 go func() {
Markku Virtanen42723e22021-06-15 10:09:23 +0300623 xapp.Logger.Info("Deleteting instances = %v", restSubscription.InstanceIds)
archaggeafbf95f2021-04-14 08:54:05 +0300624 for _, instanceId := range restSubscription.InstanceIds {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000625 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
626
archaggeafbf95f2021-04-14 08:54:05 +0300627 if err != nil {
628 xapp.Logger.Error("%s", err.Error())
629 //return err
630 }
631 xapp.Logger.Info("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000632 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
633 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300634 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300635 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300636 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300637 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300638 }()
639
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000640 c.UpdateCounter(cRestSubDelRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300641
Anssi Mannilac92b4212020-12-07 14:59:34 +0200642 return nil
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200643}
644
archaggeafbf95f2021-04-14 08:54:05 +0300645//-------------------------------------------------------------------
646//
647//-------------------------------------------------------------------
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000648func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300649
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000650 var xAppEventInstanceID int64
651 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
652 if err != nil {
653 xapp.Logger.Info("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
654 restSubId, instanceId, idstring(err, nil))
655 return xAppEventInstanceID, nil
656 }
657
658 xAppEventInstanceID = int64(subs.ReqId.Id)
659 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300660 if trans == nil {
661 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
662 xapp.Logger.Error("%s", err.Error())
663 }
664 defer trans.Release()
665
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000666 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300667 if err != nil {
668 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
669 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000670 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300671 }
672 //
673 // Wake subs delete
674 //
675 go c.handleSubscriptionDelete(subs, trans)
676 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
677
678 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
679
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000680 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300681
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000682 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300683}
684
685//-------------------------------------------------------------------
686//
687//-------------------------------------------------------------------
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200688func (c *Control) QueryHandler() (models.SubscriptionList, error) {
Anssi Mannila4c626a22021-02-11 12:50:48 +0200689 xapp.Logger.Info("QueryHandler() called")
690
archaggeafbf95f2021-04-14 08:54:05 +0300691 c.CntRecvMsg++
692
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200693 return c.registry.QueryHandler()
694}
695
Anssi Mannilac92b4212020-12-07 14:59:34 +0200696func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200697 xapp.Logger.Info("TestRestHandler() called")
698
699 pathParams := mux.Vars(r)
700 s := pathParams["testId"]
701
702 // This can be used to delete single subscription from db
703 if contains := strings.Contains(s, "deletesubid="); contains == true {
704 var splits = strings.Split(s, "=")
705 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
706 xapp.Logger.Info("RemoveSubscriptionFromSdl() called. subId = %v", subId)
707 c.RemoveSubscriptionFromSdl(uint32(subId))
708 return
709 }
710 }
711
712 // This can be used to remove all subscriptions db from
713 if s == "emptydb" {
714 xapp.Logger.Info("RemoveAllSubscriptionsFromSdl() called")
715 c.RemoveAllSubscriptionsFromSdl()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300716 c.RemoveAllRESTSubscriptionsFromSdl()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200717 return
718 }
719
720 // This is meant to cause submgr's restart in testing
721 if s == "restart" {
722 xapp.Logger.Info("os.Exit(1) called")
723 os.Exit(1)
724 }
725
726 xapp.Logger.Info("Unsupported rest command received %s", s)
727}
728
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200729//-------------------------------------------------------------------
730//
731//-------------------------------------------------------------------
732
Juha Hyttinen83ada002020-01-30 10:36:33 +0200733func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300734 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200735 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300736 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200737 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200738 params.Meid = subs.GetMeid()
739 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200740 params.PayloadLen = len(trans.Payload.Buf)
741 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200742 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200743 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000744 err = c.SendWithRetry(params, false, 5)
745 if err != nil {
746 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
747 }
748 return err
kalnagye0018682019-09-26 16:28:25 +0200749}
750
Juha Hyttinen83ada002020-01-30 10:36:33 +0200751func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
752
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300753 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200754 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300755 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200756 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200757 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200758 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200759 params.PayloadLen = len(trans.Payload.Buf)
760 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200761 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200762 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000763 err = c.SendWithRetry(params, false, 5)
764 if err != nil {
765 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
766 }
767 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200768}
769
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300770func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
771 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200772 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
773 xapp.Logger.Error("%s", err.Error())
774 return
775 }
776 c.CntRecvMsg++
777
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300778 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200779
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300780 // xapp-frame might use direct access to c buffer and
781 // when msg.Mbuf is freed, someone might take it into use
782 // and payload data might be invalid inside message handle function
783 //
784 // subscriptions won't load system a lot so there is no
785 // real performance hit by cloning buffer into new go byte slice
786 cPay := append(msg.Payload[:0:0], msg.Payload...)
787 msg.Payload = cPay
788 msg.PayloadLen = len(cPay)
789
Anssi Mannila90fa0212019-12-12 10:47:47 +0200790 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200791 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200792 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200793 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200794 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200795 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200796 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200797 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200798 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200799 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200800 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200801 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200802 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200803 default:
804 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000805 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200806 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200807}
808
Juha Hyttinen422d0182020-01-17 13:37:05 +0200809//-------------------------------------------------------------------
810// handle from XAPP Subscription Request
811//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300812func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200813 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200814 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200815
816 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
817 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200818 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200819 return
820 }
821
Anssi Mannila4c626a22021-02-11 12:50:48 +0200822 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200823 if trans == nil {
824 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200825 return
826 }
827 defer trans.Release()
828
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200829 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200830 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
831 return
832 }
833
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300834 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannila4abf1802021-01-28 13:06:46 +0200835 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200836 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200837 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200838 return
839 }
840
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200841 c.wakeSubscriptionRequest(subs, trans)
842}
843
844//-------------------------------------------------------------------
845// Wake Subscription Request to E2node
846//------------------------------------------------------------------
847func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
848
Juha Hyttinen422d0182020-01-17 13:37:05 +0200849 go c.handleSubscriptionCreate(subs, trans)
850 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200851 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200852 if event != nil {
853 switch themsg := event.(type) {
854 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200855 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200856 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
857 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200858 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200859 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200860 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200861 return
862 }
863 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200864 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200865 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
866 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200867 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200868 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200869 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200870 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200871 break
872 }
873 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200874 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300875 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200876}
877
Juha Hyttinen422d0182020-01-17 13:37:05 +0200878//-------------------------------------------------------------------
879// handle from XAPP Subscription Delete Request
880//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300881func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200882 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200883 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200884
885 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200886 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200887 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200888 return
889 }
890
Anssi Mannila4c626a22021-02-11 12:50:48 +0200891 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200892 if trans == nil {
893 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200894 return
895 }
896 defer trans.Release()
897
Juha Hyttinen83ada002020-01-30 10:36:33 +0200898 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200899 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200900 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
901 return
902 }
903
Juha Hyttinen47942b42020-02-27 10:41:43 +0200904 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200905 if err != nil {
906 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200907 return
908 }
909
Juha Hyttinen422d0182020-01-17 13:37:05 +0200910 //
911 // Wake subs delete
912 //
913 go c.handleSubscriptionDelete(subs, trans)
914 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
915
Juha Hyttinen83ada002020-01-30 10:36:33 +0200916 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
917
Anssi Mannilac92b4212020-12-07 14:59:34 +0200918 if subs.NoRespToXapp == true {
919 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
920 return
921 }
922
923 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200924 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +0200925 subDelRespMsg.RequestId.Id = trans.RequestId.Id
926 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200927 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
928 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
929 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200930 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200931 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200932 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200933
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300934 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
935 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200936}
937
938//-------------------------------------------------------------------
939// SUBS CREATE Handling
940//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200941func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200942
Anssi Mannilac92b4212020-12-07 14:59:34 +0200943 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +0200944 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200945 subs.WaitTransactionTurn(trans)
946 defer subs.ReleaseTransactionTurn(trans)
947 defer trans.Release()
948
Juha Hyttinen83ada002020-01-30 10:36:33 +0200949 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200950
Juha Hyttinen83ada002020-01-30 10:36:33 +0200951 subRfMsg, valid := subs.GetCachedResponse()
952 if subRfMsg == nil && valid == true {
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200953 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
954 switch event.(type) {
955 case *e2ap.E2APSubscriptionResponse:
956 subRfMsg, valid = subs.SetCachedResponse(event, true)
957 subs.SubRespRcvd = true
958 case *e2ap.E2APSubscriptionFailure:
959 removeSubscriptionFromDb = true
960 subRfMsg, valid = subs.SetCachedResponse(event, false)
Markku Virtanen42723e22021-06-15 10:09:23 +0300961 xapp.Logger.Info("SUBS-SubReq: internal delete due failure event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
archaggeafbf95f2021-04-14 08:54:05 +0300962 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200963 case *SubmgrRestartTestEvent:
964 // This simulates that no response has been received and after restart subscriptions are restored from db
965 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
966 return
967 default:
Markku Virtanen42723e22021-06-15 10:09:23 +0300968 xapp.Logger.Info("SUBS-SubReq: internal delete due default event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200969 removeSubscriptionFromDb = true
970 subRfMsg, valid = subs.SetCachedResponse(nil, false)
971 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200972 }
973 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
974 } else {
975 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200976 }
977
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300978 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
979 if valid == false {
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000980 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300981 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200982
983 c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200984 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200985}
986
987//-------------------------------------------------------------------
988// SUBS DELETE Handling
989//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200990
Juha Hyttinen83ada002020-01-30 10:36:33 +0200991func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200992
Juha Hyttinen83ada002020-01-30 10:36:33 +0200993 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200994 subs.WaitTransactionTurn(trans)
995 defer subs.ReleaseTransactionTurn(trans)
996 defer trans.Release()
997
Juha Hyttinen83ada002020-01-30 10:36:33 +0200998 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200999
Juha Hyttinen3944a222020-01-24 11:51:46 +02001000 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001001
Juha Hyttinen3944a222020-01-24 11:51:46 +02001002 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
1003 subs.valid = false
1004 subs.mutex.Unlock()
1005 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1006 } else {
1007 subs.mutex.Unlock()
1008 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001009 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
1010 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
1011 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001012 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001013 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +02001014 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001015}
1016
1017//-------------------------------------------------------------------
1018// send to E2T Subscription Request
1019//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +02001020func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001021 var err error
1022 var event interface{} = nil
1023 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001024 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001025
1026 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +02001027 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001028 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001029 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
1030 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001031 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001032 return event
1033 }
1034
Anssi Mannilac92b4212020-12-07 14:59:34 +02001035 // Write uncompleted subscrition in db. If no response for subscrition it need to be re-processed (deleted) after restart
1036 c.WriteSubscriptionToDb(subs)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001037
Juha Hyttinen422d0182020-01-17 13:37:05 +02001038 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001039 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001040 if retries == 0 {
1041 c.UpdateCounter(cSubReqToE2)
1042 } else {
1043 c.UpdateCounter(cSubReReqToE2)
1044 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001045 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001046 if subs.DoNotWaitSubResp == false {
1047 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
1048 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001049 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001050 continue
1051 }
1052 } else {
1053 // Simulating case where subscrition request has been sent but response has not been received before restart
1054 event = &SubmgrRestartTestEvent{}
Juha Hyttinen422d0182020-01-17 13:37:05 +02001055 }
1056 break
1057 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001058 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001059 return event
1060}
1061
1062//-------------------------------------------------------------------
1063// send to E2T Subscription Delete Request
1064//-------------------------------------------------------------------
1065
Juha Hyttinen83ada002020-01-30 10:36:33 +02001066func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001067 var err error
1068 var event interface{}
1069 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001070 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001071
1072 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +02001073 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001074 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +02001075 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001076 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1077 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001078 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001079 return event
1080 }
1081
1082 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001083 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001084 if retries == 0 {
1085 c.UpdateCounter(cSubDelReqToE2)
1086 } else {
1087 c.UpdateCounter(cSubDelReReqToE2)
1088 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001089 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001090 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
1091 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001092 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001093 continue
1094 }
1095 break
1096 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001097 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001098 return event
1099}
1100
1101//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +02001102// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001103//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001104func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001105 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001106 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +03001107
Juha Hyttinen422d0182020-01-17 13:37:05 +02001108 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
1109 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001110 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001111 return
1112 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001113 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +02001114 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001115 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001116 return
Juha Hyttinen31797b42020-01-16 14:05:01 +02001117 }
1118 trans := subs.GetTransaction()
1119 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001120 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001121 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001122 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001123 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001124 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
1125 if sendOk == false {
1126 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001127 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001128 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001129 return
kalnagy45114752019-06-18 14:40:39 +02001130}
1131
Juha Hyttinen422d0182020-01-17 13:37:05 +02001132//-------------------------------------------------------------------
1133// handle from E2T Subscription Failure
1134//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001135func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001136 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001137 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001138 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001139 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001140 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001141 return
1142 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001143 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001144 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001145 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001146 return
1147 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001148 trans := subs.GetTransaction()
1149 if trans == nil {
1150 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001151 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001152 return
1153 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001154 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1155 if sendOk == false {
1156 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001157 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001158 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001159 return
1160}
1161
Juha Hyttinen422d0182020-01-17 13:37:05 +02001162//-------------------------------------------------------------------
1163// handle from E2T Subscription Delete Response
1164//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001165func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001166 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001167 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001168 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001169 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001170 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001171 return
1172 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001173 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001174 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001175 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001176 return
1177 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001178 trans := subs.GetTransaction()
1179 if trans == nil {
1180 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001181 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001182 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001183 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001184 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1185 if sendOk == false {
1186 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001187 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001188 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001189 return
1190}
1191
Juha Hyttinen422d0182020-01-17 13:37:05 +02001192//-------------------------------------------------------------------
1193// handle from E2T Subscription Delete Failure
1194//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001195func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001196 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001197 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001198 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001199 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001200 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001201 return
1202 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001203 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001204 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001205 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001206 return
1207 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001208 trans := subs.GetTransaction()
1209 if trans == nil {
1210 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001211 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001212 return
1213 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001214 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1215 if sendOk == false {
1216 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001217 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001218 }
kalnagy93cc3e22019-09-19 11:29:29 +02001219 return
1220}
1221
Juha Hyttinen422d0182020-01-17 13:37:05 +02001222//-------------------------------------------------------------------
1223//
1224//-------------------------------------------------------------------
1225func typeofSubsMessage(v interface{}) string {
1226 if v == nil {
1227 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001228 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001229 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001230 //case *e2ap.E2APSubscriptionRequest:
1231 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001232 case *e2ap.E2APSubscriptionResponse:
1233 return "SubResp"
1234 case *e2ap.E2APSubscriptionFailure:
1235 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001236 //case *e2ap.E2APSubscriptionDeleteRequest:
1237 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001238 case *e2ap.E2APSubscriptionDeleteResponse:
1239 return "SubDelResp"
1240 case *e2ap.E2APSubscriptionDeleteFailure:
1241 return "SubDelFail"
1242 default:
1243 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001244 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001245}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001246
1247//-------------------------------------------------------------------
1248//
1249//-------------------------------------------------------------------
1250func (c *Control) WriteSubscriptionToDb(subs *Subscription) {
1251 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1252 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1253 if err != nil {
1254 xapp.Logger.Error("%v", err)
1255 }
1256}
1257
1258//-------------------------------------------------------------------
1259//
1260//-------------------------------------------------------------------
1261func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) {
1262
1263 if removeSubscriptionFromDb == true {
1264 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1265 c.RemoveSubscriptionFromDb(subs)
1266 } else {
1267 // Update is needed for successful response and merge case here
1268 if subs.RetryFromXapp == false {
1269 c.WriteSubscriptionToDb(subs)
1270 }
1271 }
1272 subs.RetryFromXapp = false
1273}
1274
1275//-------------------------------------------------------------------
1276//
1277//-------------------------------------------------------------------
1278func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1279 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1280 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1281 if err != nil {
1282 xapp.Logger.Error("%v", err)
1283 }
1284}
1285
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001286//-------------------------------------------------------------------
1287//
1288//-------------------------------------------------------------------
1289func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1290 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1291 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1292 if err != nil {
1293 xapp.Logger.Error("%v", err)
1294 }
1295}
1296
1297//-------------------------------------------------------------------
1298//
1299//-------------------------------------------------------------------
1300func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1301
1302 if removeRestSubscriptionFromDb == true {
1303 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1304 c.RemoveRESTSubscriptionFromDb(restSubId)
1305 } else {
1306 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1307 }
1308}
1309
1310//-------------------------------------------------------------------
1311//
1312//-------------------------------------------------------------------
1313func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1314 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1315 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1316 if err != nil {
1317 xapp.Logger.Error("%v", err)
1318 }
1319}
1320
Anssi Mannilac92b4212020-12-07 14:59:34 +02001321func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
1322
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001323 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001324 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1325
1326 // Send delete for every endpoint in the subscription
1327 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1328 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001329 subDelReqMsg.RequestId.Id = ricRequestorId
Anssi Mannilac92b4212020-12-07 14:59:34 +02001330 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1331 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1332 if err != nil {
1333 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1334 return
1335 }
1336 for _, endPoint := range subs.EpList.Endpoints {
1337 params := &xapp.RMRParams{}
1338 params.Mtype = mType
1339 params.SubId = int(subs.GetReqId().InstanceId)
1340 params.Xid = ""
1341 params.Meid = subs.Meid
1342 params.Src = endPoint.String()
1343 params.PayloadLen = len(payload.Buf)
1344 params.Payload = payload.Buf
1345 params.Mbuf = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001346 subs.DeleteFromDb = true
1347 c.handleXAPPSubscriptionDeleteRequest(params)
1348 }
1349}
Anssi Mannilade457a12021-06-02 16:19:48 +03001350
1351func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1352
1353 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001354
1355 if p.SubscriptionID != "" {
1356 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1357 } else {
1358 fmt.Println(" SubscriptionID = ''")
1359 }
1360
Anssi Mannilade457a12021-06-02 16:19:48 +03001361 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1362
1363 if p.ClientEndpoint.HTTPPort != nil {
1364 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1365 } else {
1366 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1367 }
1368
1369 if p.ClientEndpoint.RMRPort != nil {
1370 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1371 } else {
1372 fmt.Println(" ClientEndpoint.RMRPort = nil")
1373 }
1374
1375 if p.Meid != nil {
1376 fmt.Printf(" Meid = %s\n", *p.Meid)
1377 } else {
1378 fmt.Println(" Meid = nil")
1379 }
1380
1381 for _, subscriptionDetail := range p.SubscriptionDetails {
1382 if p.RANFunctionID != nil {
1383 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1384 } else {
1385 fmt.Println(" RANFunctionID = nil")
1386 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001387 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001388 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001389
1390 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1391 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1392 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001393 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001394
Anssi Mannilade457a12021-06-02 16:19:48 +03001395 if actionToBeSetup.SubsequentAction != nil {
1396 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1397 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1398 } else {
1399 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1400 }
1401 }
1402 }
1403}