blob: c74b1a5165480211e34697591aec209e0618ca1a [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 {
49 retval += filler + entry.String()
50 filler = " "
51 }
52 if err != nil {
53 retval += filler + "err(" + err.Error() + ")"
54 filler = " "
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020055 }
56 return retval
57}
58
59//-----------------------------------------------------------------------------
60//
61//-----------------------------------------------------------------------------
62
Anssi Mannila2f26fb22020-12-07 08:32:13 +020063var e2tSubReqTimeout time.Duration
64var e2tSubDelReqTime time.Duration
65var e2tRecvMsgTimeout time.Duration
Markku Virtanenfe2cdab2021-05-21 10:59:29 +000066var waitRouteCleanup_ms time.Duration
Anssi Mannila2f26fb22020-12-07 08:32:13 +020067var e2tMaxSubReqTryCount uint64 // Initial try + retry
68var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
Anssi Mannilac92b4212020-12-07 14:59:34 +020069var readSubsFromDb string
Markku Virtanenb642a192021-06-09 09:08:14 +000070var restDuplicateCtrl duplicateCtrl
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020071
kalnagy45114752019-06-18 14:40:39 +020072type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030073 *xapp.RMRClient
Anssi Mannilade457a12021-06-02 16:19:48 +030074 e2ap *E2ap
75 registry *Registry
76 tracker *Tracker
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030077 e2SubsDb Sdlnterface
78 restSubsDb Sdlnterface
Anssi Mannilac92b4212020-12-07 14:59:34 +020079 CntRecvMsg uint64
80 ResetTestFlag bool
Anssi Mannila4abf1802021-01-28 13:06:46 +020081 Counters map[string]xapp.Counter
Anssi Mannilade457a12021-06-02 16:19:48 +030082 LoggerLevel uint32
kalnagy93cc3e22019-09-19 11:29:29 +020083}
84
85type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020086 PlmnID string
87 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020088 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020089}
90
Anssi Mannilac92b4212020-12-07 14:59:34 +020091type SubmgrRestartTestEvent struct{}
92type SubmgrRestartUpEvent struct{}
93
Peter Szilagyifbc56f92019-07-23 19:29:46 +000094func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020095 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000096 viper.AutomaticEnv()
97 viper.SetEnvPrefix("submgr")
98 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020099}
100
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200101func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200102
Anssi Mannilac92b4212020-12-07 14:59:34 +0200103 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
104 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
105
106 registry := new(Registry)
107 registry.Initialize()
108 registry.rtmgrClient = &rtmgrClient
109
110 tracker := new(Tracker)
111 tracker.Init()
112
Anssi Mannilac92b4212020-12-07 14:59:34 +0200113 c := &Control{e2ap: new(E2ap),
Anssi Mannilade457a12021-06-02 16:19:48 +0300114 registry: registry,
115 tracker: tracker,
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300116 e2SubsDb: CreateSdl(),
117 restSubsDb: CreateRESTSdl(),
Anssi Mannilade457a12021-06-02 16:19:48 +0300118 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
119 LoggerLevel: 3,
Anssi Mannilac92b4212020-12-07 14:59:34 +0200120 }
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200121 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200122
123 // Register REST handler for testing support
124 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000125 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200126
archaggeafbf95f2021-04-14 08:54:05 +0300127 go xapp.Subscription.Listen(c.SubscriptionHandler, c.QueryHandler, c.SubscriptionDeleteHandlerCB)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200128
129 if readSubsFromDb == "false" {
130 return c
131 }
132
Markku Virtanenb642a192021-06-09 09:08:14 +0000133 restDuplicateCtrl.Init()
134
Anssi Mannilac92b4212020-12-07 14:59:34 +0200135 // Read subscriptions from db
136 xapp.Logger.Info("Reading subscriptions from db")
137 subIds, register, err := c.ReadAllSubscriptionsFromSdl()
138 if err != nil {
139 xapp.Logger.Error("%v", err)
140 } else {
141 c.registry.subIds = subIds
142 c.registry.register = register
143 c.HandleUncompletedSubscriptions(register)
144 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300145
146 restSubscriptions, err := c.ReadAllRESTSubscriptionsFromSdl()
147 if err != nil {
148 xapp.Logger.Error("%v", err)
149 } else {
150 c.registry.restSubscriptions = restSubscriptions
151 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200152 return c
153}
154
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000155func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
156 subscriptions, _ := c.registry.QueryHandler()
157 xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
158}
159
Anssi Mannilac92b4212020-12-07 14:59:34 +0200160//-------------------------------------------------------------------
161//
162//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200163func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200164
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200165 // viper.GetDuration returns nanoseconds
166 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200167 if e2tSubReqTimeout == 0 {
168 e2tSubReqTimeout = 2000 * 1000000
169 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200170 xapp.Logger.Info("e2tSubReqTimeout %v", e2tSubReqTimeout)
171 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200172 if e2tSubDelReqTime == 0 {
173 e2tSubDelReqTime = 2000 * 1000000
174 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200175 xapp.Logger.Info("e2tSubDelReqTime %v", e2tSubDelReqTime)
176 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200177 if e2tRecvMsgTimeout == 0 {
178 e2tRecvMsgTimeout = 2000 * 1000000
179 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200180 xapp.Logger.Info("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000181
182 // Internal cfg parameter, used to define a wait time for RMR route clean-up. None default
183 // value 100ms used currently only in unittests.
184 waitRouteCleanup_ms = viper.GetDuration("controls.waitRouteCleanup_ms") * 1000000
185 if waitRouteCleanup_ms == 0 {
186 waitRouteCleanup_ms = 5000 * 1000000
187 }
188 xapp.Logger.Info("waitRouteCleanup %v", waitRouteCleanup_ms)
189
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200190 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200191 if e2tMaxSubReqTryCount == 0 {
192 e2tMaxSubReqTryCount = 1
193 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200194 xapp.Logger.Info("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000195
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200196 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200197 if e2tMaxSubDelReqTryCount == 0 {
198 e2tMaxSubDelReqTryCount = 1
199 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200200 xapp.Logger.Info("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
201
Anssi Mannilac92b4212020-12-07 14:59:34 +0200202 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
203 if readSubsFromDb == "" {
204 readSubsFromDb = "true"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200205 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200206 xapp.Logger.Info("readSubsFromDb %v", readSubsFromDb)
Anssi Mannilade457a12021-06-02 16:19:48 +0300207 c.LoggerLevel = viper.GetUint32("logger.level")
208 if c.LoggerLevel == 0 {
209 c.LoggerLevel = 3
210 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200211}
212
213//-------------------------------------------------------------------
214//
215//-------------------------------------------------------------------
216func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
217
218 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
219 for subId, subs := range register {
220 if subs.SubRespRcvd == false {
221 subs.NoRespToXapp = true
222 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
223 c.SendSubscriptionDeleteReq(subs)
224 }
225 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200226}
227
228func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300229 if c.RMRClient == nil {
230 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200231 }
kalnagy45114752019-06-18 14:40:39 +0200232}
233
234func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200235 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200236 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000237 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200238}
239
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200240//-------------------------------------------------------------------
241//
242//-------------------------------------------------------------------
archaggea5c58bc2021-04-14 08:54:05 +0300243func (c *Control) SubscriptionHandler(params interface{}) (*models.SubscriptionResponse, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300244
Anssi Mannila316d8a12021-06-02 11:08:54 +0300245 c.CntRecvMsg++
246 c.UpdateCounter(cRestSubReqFromXapp)
247
archaggeafbf95f2021-04-14 08:54:05 +0300248 subResp := models.SubscriptionResponse{}
archaggeafbf95f2021-04-14 08:54:05 +0300249 p := params.(*models.SubscriptionParams)
250
Anssi Mannilade457a12021-06-02 16:19:48 +0300251 if c.LoggerLevel > 2 {
252 c.PrintRESTSubscriptionRequest(p)
253 }
254
archaggeafbf95f2021-04-14 08:54:05 +0300255 if p.ClientEndpoint == nil {
256 xapp.Logger.Error("ClientEndpoint == nil")
Anssi Mannila316d8a12021-06-02 11:08:54 +0300257 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300258 return nil, fmt.Errorf("")
259 }
260
261 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
262 if err != nil {
263 xapp.Logger.Error("%s", err.Error())
Anssi Mannila316d8a12021-06-02 11:08:54 +0300264 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300265 return nil, err
266 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000267 var restSubId string
268 var restSubscription *RESTSubscription
269 if p.SubscriptionID == "" {
270 restSubId = ksuid.New().String()
271 restSubscription, err = c.registry.CreateRESTSubscription(&restSubId, &xAppRmrEndpoint, p.Meid)
272 if err != nil {
273 xapp.Logger.Error("%s", err.Error())
274 c.UpdateCounter(cRestSubFailToXapp)
275 return nil, err
276 }
archaggeafbf95f2021-04-14 08:54:05 +0300277
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000278 } else {
279 restSubId = p.SubscriptionID
280 restSubscription, err = c.registry.GetRESTSubscription(restSubId, false)
281 if err != nil {
282 xapp.Logger.Error("%s", err.Error())
283 c.UpdateCounter(cRestSubFailToXapp)
284 return nil, err
285 }
archaggeafbf95f2021-04-14 08:54:05 +0300286 }
287
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000288 subResp.SubscriptionID = &restSubId
archaggeafbf95f2021-04-14 08:54:05 +0300289 subReqList := e2ap.SubscriptionRequestList{}
290 err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
291 if err != nil {
292 xapp.Logger.Error("%s", err.Error())
293 c.registry.DeleteRESTSubscription(&restSubId)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300294 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300295 return nil, err
296 }
297
Markku Virtanenb642a192021-06-09 09:08:14 +0000298 err, duplicate, md5sum := restDuplicateCtrl.IsDuplicateToOngoingTransaction(restSubId, params)
299
300 if err != nil {
301 // We were unable to detect whether this request was duplicate or not, proceed
302 xapp.Logger.Info("%s - proceeding with the request", err.Error())
303 } else {
304 if duplicate {
305 if *p.SubscriptionDetails[0].ActionToBeSetupList[0].ActionType == "report" {
306 xapp.Logger.Info("Retransmission blocker dropped for report typer of request")
307 c.UpdateCounter(cRestSubRespToXapp)
308 return &subResp, nil
309 }
310 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300311 restSubscription.Md5sum = md5sum
Markku Virtanenb642a192021-06-09 09:08:14 +0000312 }
313
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300314 c.WriteRESTSubscriptionToDb(restSubId, restSubscription)
315
Markku Virtanenb642a192021-06-09 09:08:14 +0000316 go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint)
archaggeafbf95f2021-04-14 08:54:05 +0300317
Anssi Mannila316d8a12021-06-02 11:08:54 +0300318 c.UpdateCounter(cRestSubRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300319 return &subResp, nil
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300320}
321
Markku Virtanenb642a192021-06-09 09:08:14 +0000322func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
323 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp) {
324
325 // Send notification to xApp that prosessing of a Subscription Request has failed.
326 e2EventInstanceID := (int64)(0)
327 errorCause := err.Error()
328 resp := &models.SubscriptionResponse{
329 SubscriptionID: restSubId,
330 SubscriptionInstances: []*models.SubscriptionInstance{
331 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
332 ErrorCause: &errorCause,
333 XappEventInstanceID: &xAppEventInstanceID},
334 },
335 }
336 // Mark REST subscription request processed.
337 restSubscription.SetProcessed()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300338 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Markku Virtanenb642a192021-06-09 09:08:14 +0000339 if trans != nil {
340 xapp.Logger.Info("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
341 errorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
342 } else {
343 xapp.Logger.Info("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
344 errorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
345 }
Markku Virtanen987f8b12021-06-10 15:05:02 +0300346
Markku Virtanenb642a192021-06-09 09:08:14 +0000347 c.UpdateCounter(cRestSubFailNotifToXapp)
Markku Virtanen987f8b12021-06-10 15:05:02 +0300348 xapp.Subscription.Notify(resp, *clientEndpoint)
Markku Virtanenb642a192021-06-09 09:08:14 +0000349}
350
351func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
352 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp) {
353
354 // Store successfully processed InstanceId for deletion
355 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
356 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
357
358 // Send notification to xApp that a Subscription Request has been processed.
359 resp := &models.SubscriptionResponse{
360 SubscriptionID: restSubId,
361 SubscriptionInstances: []*models.SubscriptionInstance{
362 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
363 ErrorCause: nil,
364 XappEventInstanceID: &xAppEventInstanceID},
365 },
366 }
367 // Mark REST subscription request processesd.
368 restSubscription.SetProcessed()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300369 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Markku Virtanenb642a192021-06-09 09:08:14 +0000370 xapp.Logger.Info("Sending successful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
371 clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen987f8b12021-06-10 15:05:02 +0300372
Markku Virtanenb642a192021-06-09 09:08:14 +0000373 c.UpdateCounter(cRestSubNotifToXapp)
Markku Virtanen987f8b12021-06-10 15:05:02 +0300374 xapp.Subscription.Notify(resp, *clientEndpoint)
Markku Virtanenb642a192021-06-09 09:08:14 +0000375}
376
archaggeafbf95f2021-04-14 08:54:05 +0300377//-------------------------------------------------------------------
378//
379//-------------------------------------------------------------------
380
381func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
Markku Virtanenb642a192021-06-09 09:08:14 +0000382 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string) {
archaggeafbf95f2021-04-14 08:54:05 +0300383
384 xapp.Logger.Info("Subscription Request count=%v ", len(subReqList.E2APSubscriptionRequests))
385
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000386 var xAppEventInstanceID int64
387 var e2EventInstanceID int64
Markku Virtanenb642a192021-06-09 09:08:14 +0000388
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300389 defer restDuplicateCtrl.TransactionComplete(restSubscription.Md5sum)
Markku Virtanenb642a192021-06-09 09:08:14 +0000390
archaggeafbf95f2021-04-14 08:54:05 +0300391 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
392 subReqMsg := subReqList.E2APSubscriptionRequests[index]
Markku Virtanenb642a192021-06-09 09:08:14 +0000393 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300394
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000395 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300396 if trans == nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000397 // Send notification to xApp that prosessing of a Subscription Request has failed.
398 err := fmt.Errorf("Tracking failure")
399 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans)
400 continue
archaggeafbf95f2021-04-14 08:54:05 +0300401 }
402
archaggeafbf95f2021-04-14 08:54:05 +0300403 xapp.Logger.Info("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000404
archaggeafbf95f2021-04-14 08:54:05 +0300405 subRespMsg, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, restSubId)
406 if err != nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000407 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans)
archaggeafbf95f2021-04-14 08:54:05 +0300408 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000409 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000410 xapp.Logger.Info("SubscriptionRequest index=%v processed successfully. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
411 index, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000412 c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans)
archaggeafbf95f2021-04-14 08:54:05 +0300413 }
Markku Virtanenb642a192021-06-09 09:08:14 +0000414 trans.Release()
archaggeafbf95f2021-04-14 08:54:05 +0300415 }
416}
417
418//-------------------------------------------------------------------
419//
420//------------------------------------------------------------------
421func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
422 restSubId *string) (*e2ap.E2APSubscriptionResponse, error) {
423
424 err := c.tracker.Track(trans)
425 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300426 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
427 err = fmt.Errorf("Tracking failure")
archaggeafbf95f2021-04-14 08:54:05 +0300428 return nil, err
429 }
430
431 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
432 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300433 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
archaggeafbf95f2021-04-14 08:54:05 +0300434 return nil, err
435 }
436
437 //
438 // Wake subs request
439 //
440 go c.handleSubscriptionCreate(subs, trans)
441 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
442
443 err = nil
444 if event != nil {
445 switch themsg := event.(type) {
446 case *e2ap.E2APSubscriptionResponse:
447 trans.Release()
448 return themsg, nil
449 case *e2ap.E2APSubscriptionFailure:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300450 err = fmt.Errorf("E2 SubscriptionFailure received")
archaggeafbf95f2021-04-14 08:54:05 +0300451 return nil, err
452 default:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300453 err = fmt.Errorf("unexpected E2 subscription response received")
archaggeafbf95f2021-04-14 08:54:05 +0300454 break
455 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300456 } else {
457 err = fmt.Errorf("E2 subscription response timeout")
archaggeafbf95f2021-04-14 08:54:05 +0300458 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300459
460 xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000461 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300462 return nil, err
463}
464
465//-------------------------------------------------------------------
466//
467//-------------------------------------------------------------------
468func (c *Control) SubscriptionDeleteHandlerCB(restSubId string) error {
469
470 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000471 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300472
473 xapp.Logger.Info("SubscriptionDeleteRequest from XAPP")
474
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000475 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300476 if err != nil {
477 xapp.Logger.Error("%s", err.Error())
478 if restSubscription == nil {
479 // Subscription was not found
480 return nil
481 } else {
482 if restSubscription.SubReqOngoing == true {
483 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
484 xapp.Logger.Error("%s", err.Error())
485 return err
486 } else if restSubscription.SubDelReqOngoing == true {
487 // Previous request for same restSubId still ongoing
488 return nil
489 }
490 }
491 }
492
493 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
494 go func() {
495 for _, instanceId := range restSubscription.InstanceIds {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000496 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
497
archaggeafbf95f2021-04-14 08:54:05 +0300498 if err != nil {
499 xapp.Logger.Error("%s", err.Error())
500 //return err
501 }
502 xapp.Logger.Info("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000503 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
504 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300505 }
506 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300507 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300508 }()
509
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000510 c.UpdateCounter(cRestSubDelRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300511
Anssi Mannilac92b4212020-12-07 14:59:34 +0200512 return nil
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200513}
514
archaggeafbf95f2021-04-14 08:54:05 +0300515//-------------------------------------------------------------------
516//
517//-------------------------------------------------------------------
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000518func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300519
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000520 var xAppEventInstanceID int64
521 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
522 if err != nil {
523 xapp.Logger.Info("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
524 restSubId, instanceId, idstring(err, nil))
525 return xAppEventInstanceID, nil
526 }
527
528 xAppEventInstanceID = int64(subs.ReqId.Id)
529 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300530 if trans == nil {
531 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
532 xapp.Logger.Error("%s", err.Error())
533 }
534 defer trans.Release()
535
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000536 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300537 if err != nil {
538 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
539 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000540 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300541 }
542 //
543 // Wake subs delete
544 //
545 go c.handleSubscriptionDelete(subs, trans)
546 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
547
548 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
549
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000550 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300551
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000552 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300553}
554
555//-------------------------------------------------------------------
556//
557//-------------------------------------------------------------------
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200558func (c *Control) QueryHandler() (models.SubscriptionList, error) {
Anssi Mannila4c626a22021-02-11 12:50:48 +0200559 xapp.Logger.Info("QueryHandler() called")
560
archaggeafbf95f2021-04-14 08:54:05 +0300561 c.CntRecvMsg++
562
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200563 return c.registry.QueryHandler()
564}
565
Anssi Mannilac92b4212020-12-07 14:59:34 +0200566func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200567 xapp.Logger.Info("TestRestHandler() called")
568
569 pathParams := mux.Vars(r)
570 s := pathParams["testId"]
571
572 // This can be used to delete single subscription from db
573 if contains := strings.Contains(s, "deletesubid="); contains == true {
574 var splits = strings.Split(s, "=")
575 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
576 xapp.Logger.Info("RemoveSubscriptionFromSdl() called. subId = %v", subId)
577 c.RemoveSubscriptionFromSdl(uint32(subId))
578 return
579 }
580 }
581
582 // This can be used to remove all subscriptions db from
583 if s == "emptydb" {
584 xapp.Logger.Info("RemoveAllSubscriptionsFromSdl() called")
585 c.RemoveAllSubscriptionsFromSdl()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300586 c.RemoveAllRESTSubscriptionsFromSdl()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200587 return
588 }
589
590 // This is meant to cause submgr's restart in testing
591 if s == "restart" {
592 xapp.Logger.Info("os.Exit(1) called")
593 os.Exit(1)
594 }
595
596 xapp.Logger.Info("Unsupported rest command received %s", s)
597}
598
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200599//-------------------------------------------------------------------
600//
601//-------------------------------------------------------------------
602
Juha Hyttinen83ada002020-01-30 10:36:33 +0200603func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300604 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200605 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300606 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200607 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200608 params.Meid = subs.GetMeid()
609 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200610 params.PayloadLen = len(trans.Payload.Buf)
611 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200612 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200613 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000614 err = c.SendWithRetry(params, false, 5)
615 if err != nil {
616 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
617 }
618 return err
kalnagye0018682019-09-26 16:28:25 +0200619}
620
Juha Hyttinen83ada002020-01-30 10:36:33 +0200621func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
622
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300623 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200624 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300625 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200626 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200627 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200628 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200629 params.PayloadLen = len(trans.Payload.Buf)
630 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200631 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200632 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000633 err = c.SendWithRetry(params, false, 5)
634 if err != nil {
635 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
636 }
637 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200638}
639
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300640func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
641 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200642 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
643 xapp.Logger.Error("%s", err.Error())
644 return
645 }
646 c.CntRecvMsg++
647
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300648 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200649
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300650 // xapp-frame might use direct access to c buffer and
651 // when msg.Mbuf is freed, someone might take it into use
652 // and payload data might be invalid inside message handle function
653 //
654 // subscriptions won't load system a lot so there is no
655 // real performance hit by cloning buffer into new go byte slice
656 cPay := append(msg.Payload[:0:0], msg.Payload...)
657 msg.Payload = cPay
658 msg.PayloadLen = len(cPay)
659
Anssi Mannila90fa0212019-12-12 10:47:47 +0200660 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200661 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200662 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200663 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200664 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200665 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200666 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200667 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200668 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200669 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200670 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200671 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200672 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200673 default:
674 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000675 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200676 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200677}
678
Juha Hyttinen422d0182020-01-17 13:37:05 +0200679//-------------------------------------------------------------------
680// handle from XAPP Subscription Request
681//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300682func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200683 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200684 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200685
686 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
687 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200688 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200689 return
690 }
691
Anssi Mannila4c626a22021-02-11 12:50:48 +0200692 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200693 if trans == nil {
694 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200695 return
696 }
697 defer trans.Release()
698
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200699 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200700 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
701 return
702 }
703
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300704 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannila4abf1802021-01-28 13:06:46 +0200705 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200706 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200707 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200708 return
709 }
710
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200711 c.wakeSubscriptionRequest(subs, trans)
712}
713
714//-------------------------------------------------------------------
715// Wake Subscription Request to E2node
716//------------------------------------------------------------------
717func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
718
Juha Hyttinen422d0182020-01-17 13:37:05 +0200719 go c.handleSubscriptionCreate(subs, trans)
720 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200721 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200722 if event != nil {
723 switch themsg := event.(type) {
724 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200725 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200726 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
727 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200728 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200729 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200730 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200731 return
732 }
733 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200734 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200735 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
736 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200737 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200738 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200739 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200740 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200741 break
742 }
743 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200744 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300745 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200746}
747
Juha Hyttinen422d0182020-01-17 13:37:05 +0200748//-------------------------------------------------------------------
749// handle from XAPP Subscription Delete Request
750//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300751func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200752 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200753 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200754
755 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200756 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200757 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200758 return
759 }
760
Anssi Mannila4c626a22021-02-11 12:50:48 +0200761 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200762 if trans == nil {
763 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200764 return
765 }
766 defer trans.Release()
767
Juha Hyttinen83ada002020-01-30 10:36:33 +0200768 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200769 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200770 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
771 return
772 }
773
Juha Hyttinen47942b42020-02-27 10:41:43 +0200774 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200775 if err != nil {
776 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200777 return
778 }
779
Juha Hyttinen422d0182020-01-17 13:37:05 +0200780 //
781 // Wake subs delete
782 //
783 go c.handleSubscriptionDelete(subs, trans)
784 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
785
Juha Hyttinen83ada002020-01-30 10:36:33 +0200786 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
787
Anssi Mannilac92b4212020-12-07 14:59:34 +0200788 if subs.NoRespToXapp == true {
789 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
790 return
791 }
792
793 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200794 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +0200795 subDelRespMsg.RequestId.Id = trans.RequestId.Id
796 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200797 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
798 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
799 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200800 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200801 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200802 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200803
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300804 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
805 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200806}
807
808//-------------------------------------------------------------------
809// SUBS CREATE Handling
810//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200811func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200812
Anssi Mannilac92b4212020-12-07 14:59:34 +0200813 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +0200814 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200815 subs.WaitTransactionTurn(trans)
816 defer subs.ReleaseTransactionTurn(trans)
817 defer trans.Release()
818
Juha Hyttinen83ada002020-01-30 10:36:33 +0200819 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200820
Juha Hyttinen83ada002020-01-30 10:36:33 +0200821 subRfMsg, valid := subs.GetCachedResponse()
822 if subRfMsg == nil && valid == true {
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200823 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
824 switch event.(type) {
825 case *e2ap.E2APSubscriptionResponse:
826 subRfMsg, valid = subs.SetCachedResponse(event, true)
827 subs.SubRespRcvd = true
828 case *e2ap.E2APSubscriptionFailure:
829 removeSubscriptionFromDb = true
830 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaebaeae62021-02-12 11:56:20 +0200831 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
archaggeafbf95f2021-04-14 08:54:05 +0300832 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200833 case *SubmgrRestartTestEvent:
834 // This simulates that no response has been received and after restart subscriptions are restored from db
835 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
836 return
837 default:
838 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
839 removeSubscriptionFromDb = true
840 subRfMsg, valid = subs.SetCachedResponse(nil, false)
841 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200842 }
843 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
844 } else {
845 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200846 }
847
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300848 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
849 if valid == false {
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000850 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300851 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200852
853 c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200854 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200855}
856
857//-------------------------------------------------------------------
858// SUBS DELETE Handling
859//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200860
Juha Hyttinen83ada002020-01-30 10:36:33 +0200861func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200862
Juha Hyttinen83ada002020-01-30 10:36:33 +0200863 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200864 subs.WaitTransactionTurn(trans)
865 defer subs.ReleaseTransactionTurn(trans)
866 defer trans.Release()
867
Juha Hyttinen83ada002020-01-30 10:36:33 +0200868 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200869
Juha Hyttinen3944a222020-01-24 11:51:46 +0200870 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200871
Juha Hyttinen3944a222020-01-24 11:51:46 +0200872 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
873 subs.valid = false
874 subs.mutex.Unlock()
875 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
876 } else {
877 subs.mutex.Unlock()
878 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300879 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
880 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
881 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000882 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200883 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +0200884 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200885}
886
887//-------------------------------------------------------------------
888// send to E2T Subscription Request
889//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200890func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200891 var err error
892 var event interface{} = nil
893 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000894 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +0200895
896 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200897 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000898 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200899 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
900 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200901 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200902 return event
903 }
904
Anssi Mannilac92b4212020-12-07 14:59:34 +0200905 // Write uncompleted subscrition in db. If no response for subscrition it need to be re-processed (deleted) after restart
906 c.WriteSubscriptionToDb(subs)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000907
Juha Hyttinen422d0182020-01-17 13:37:05 +0200908 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200909 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200910 if retries == 0 {
911 c.UpdateCounter(cSubReqToE2)
912 } else {
913 c.UpdateCounter(cSubReReqToE2)
914 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200915 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200916 if subs.DoNotWaitSubResp == false {
917 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
918 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200919 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200920 continue
921 }
922 } else {
923 // Simulating case where subscrition request has been sent but response has not been received before restart
924 event = &SubmgrRestartTestEvent{}
Juha Hyttinen422d0182020-01-17 13:37:05 +0200925 }
926 break
927 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200928 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200929 return event
930}
931
932//-------------------------------------------------------------------
933// send to E2T Subscription Delete Request
934//-------------------------------------------------------------------
935
Juha Hyttinen83ada002020-01-30 10:36:33 +0200936func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200937 var err error
938 var event interface{}
939 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000940 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +0200941
942 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200943 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000944 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200945 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200946 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
947 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200948 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200949 return event
950 }
951
952 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200953 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200954 if retries == 0 {
955 c.UpdateCounter(cSubDelReqToE2)
956 } else {
957 c.UpdateCounter(cSubDelReReqToE2)
958 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200959 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200960 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
961 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200962 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200963 continue
964 }
965 break
966 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200967 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200968 return event
969}
970
971//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +0200972// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200973//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300974func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200975 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200976 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +0300977
Juha Hyttinen422d0182020-01-17 13:37:05 +0200978 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
979 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200980 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200981 return
982 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300983 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200984 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200985 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200986 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200987 }
988 trans := subs.GetTransaction()
989 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200990 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200991 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200992 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200993 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200994 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
995 if sendOk == false {
996 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200997 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000998 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000999 return
kalnagy45114752019-06-18 14:40:39 +02001000}
1001
Juha Hyttinen422d0182020-01-17 13:37:05 +02001002//-------------------------------------------------------------------
1003// handle from E2T Subscription Failure
1004//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001005func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001006 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001007 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001008 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001009 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001010 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001011 return
1012 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001013 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001014 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001015 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001016 return
1017 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001018 trans := subs.GetTransaction()
1019 if trans == nil {
1020 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001021 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001022 return
1023 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001024 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1025 if sendOk == false {
1026 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001027 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001028 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001029 return
1030}
1031
Juha Hyttinen422d0182020-01-17 13:37:05 +02001032//-------------------------------------------------------------------
1033// handle from E2T Subscription Delete Response
1034//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001035func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001036 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001037 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001038 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001039 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001040 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001041 return
1042 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001043 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001044 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001045 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001046 return
1047 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001048 trans := subs.GetTransaction()
1049 if trans == nil {
1050 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001051 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001052 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001053 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001054 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1055 if sendOk == false {
1056 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001057 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001058 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001059 return
1060}
1061
Juha Hyttinen422d0182020-01-17 13:37:05 +02001062//-------------------------------------------------------------------
1063// handle from E2T Subscription Delete Failure
1064//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001065func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001066 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001067 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001068 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001069 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001070 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001071 return
1072 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001073 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001074 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001075 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001076 return
1077 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001078 trans := subs.GetTransaction()
1079 if trans == nil {
1080 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001081 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001082 return
1083 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001084 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1085 if sendOk == false {
1086 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001087 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001088 }
kalnagy93cc3e22019-09-19 11:29:29 +02001089 return
1090}
1091
Juha Hyttinen422d0182020-01-17 13:37:05 +02001092//-------------------------------------------------------------------
1093//
1094//-------------------------------------------------------------------
1095func typeofSubsMessage(v interface{}) string {
1096 if v == nil {
1097 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001098 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001099 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001100 //case *e2ap.E2APSubscriptionRequest:
1101 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001102 case *e2ap.E2APSubscriptionResponse:
1103 return "SubResp"
1104 case *e2ap.E2APSubscriptionFailure:
1105 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001106 //case *e2ap.E2APSubscriptionDeleteRequest:
1107 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001108 case *e2ap.E2APSubscriptionDeleteResponse:
1109 return "SubDelResp"
1110 case *e2ap.E2APSubscriptionDeleteFailure:
1111 return "SubDelFail"
1112 default:
1113 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001114 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001115}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001116
1117//-------------------------------------------------------------------
1118//
1119//-------------------------------------------------------------------
1120func (c *Control) WriteSubscriptionToDb(subs *Subscription) {
1121 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1122 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1123 if err != nil {
1124 xapp.Logger.Error("%v", err)
1125 }
1126}
1127
1128//-------------------------------------------------------------------
1129//
1130//-------------------------------------------------------------------
1131func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) {
1132
1133 if removeSubscriptionFromDb == true {
1134 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1135 c.RemoveSubscriptionFromDb(subs)
1136 } else {
1137 // Update is needed for successful response and merge case here
1138 if subs.RetryFromXapp == false {
1139 c.WriteSubscriptionToDb(subs)
1140 }
1141 }
1142 subs.RetryFromXapp = false
1143}
1144
1145//-------------------------------------------------------------------
1146//
1147//-------------------------------------------------------------------
1148func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1149 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1150 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1151 if err != nil {
1152 xapp.Logger.Error("%v", err)
1153 }
1154}
1155
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001156//-------------------------------------------------------------------
1157//
1158//-------------------------------------------------------------------
1159func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1160 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1161 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1162 if err != nil {
1163 xapp.Logger.Error("%v", err)
1164 }
1165}
1166
1167//-------------------------------------------------------------------
1168//
1169//-------------------------------------------------------------------
1170func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1171
1172 if removeRestSubscriptionFromDb == true {
1173 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1174 c.RemoveRESTSubscriptionFromDb(restSubId)
1175 } else {
1176 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1177 }
1178}
1179
1180//-------------------------------------------------------------------
1181//
1182//-------------------------------------------------------------------
1183func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1184 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1185 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1186 if err != nil {
1187 xapp.Logger.Error("%v", err)
1188 }
1189}
1190
Anssi Mannilac92b4212020-12-07 14:59:34 +02001191func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
1192
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001193 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001194 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1195
1196 // Send delete for every endpoint in the subscription
1197 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1198 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001199 subDelReqMsg.RequestId.Id = ricRequestorId
Anssi Mannilac92b4212020-12-07 14:59:34 +02001200 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1201 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1202 if err != nil {
1203 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1204 return
1205 }
1206 for _, endPoint := range subs.EpList.Endpoints {
1207 params := &xapp.RMRParams{}
1208 params.Mtype = mType
1209 params.SubId = int(subs.GetReqId().InstanceId)
1210 params.Xid = ""
1211 params.Meid = subs.Meid
1212 params.Src = endPoint.String()
1213 params.PayloadLen = len(payload.Buf)
1214 params.Payload = payload.Buf
1215 params.Mbuf = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001216 subs.DeleteFromDb = true
1217 c.handleXAPPSubscriptionDeleteRequest(params)
1218 }
1219}
Anssi Mannilade457a12021-06-02 16:19:48 +03001220
1221func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1222
1223 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001224
1225 if p.SubscriptionID != "" {
1226 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1227 } else {
1228 fmt.Println(" SubscriptionID = ''")
1229 }
1230
Anssi Mannilade457a12021-06-02 16:19:48 +03001231 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1232
1233 if p.ClientEndpoint.HTTPPort != nil {
1234 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1235 } else {
1236 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1237 }
1238
1239 if p.ClientEndpoint.RMRPort != nil {
1240 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1241 } else {
1242 fmt.Println(" ClientEndpoint.RMRPort = nil")
1243 }
1244
1245 if p.Meid != nil {
1246 fmt.Printf(" Meid = %s\n", *p.Meid)
1247 } else {
1248 fmt.Println(" Meid = nil")
1249 }
1250
1251 for _, subscriptionDetail := range p.SubscriptionDetails {
1252 if p.RANFunctionID != nil {
1253 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1254 } else {
1255 fmt.Println(" RANFunctionID = nil")
1256 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001257 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001258 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001259
1260 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1261 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1262 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001263 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001264
Anssi Mannilade457a12021-06-02 16:19:48 +03001265 if actionToBeSetup.SubsequentAction != nil {
1266 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1267 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1268 } else {
1269 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1270 }
1271 }
1272 }
1273}