blob: 70570cd022ea3eb0b6d5c13ba695fd956eb48bad [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
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020070
kalnagy45114752019-06-18 14:40:39 +020071type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030072 *xapp.RMRClient
Anssi Mannilade457a12021-06-02 16:19:48 +030073 e2ap *E2ap
74 registry *Registry
75 tracker *Tracker
76 db Sdlnterface
Anssi Mannilac92b4212020-12-07 14:59:34 +020077 CntRecvMsg uint64
78 ResetTestFlag bool
Anssi Mannila4abf1802021-01-28 13:06:46 +020079 Counters map[string]xapp.Counter
Anssi Mannilade457a12021-06-02 16:19:48 +030080 LoggerLevel uint32
kalnagy93cc3e22019-09-19 11:29:29 +020081}
82
83type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020084 PlmnID string
85 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020086 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020087}
88
Anssi Mannilac92b4212020-12-07 14:59:34 +020089type SubmgrRestartTestEvent struct{}
90type SubmgrRestartUpEvent struct{}
91
Peter Szilagyifbc56f92019-07-23 19:29:46 +000092func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020093 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000094 viper.AutomaticEnv()
95 viper.SetEnvPrefix("submgr")
96 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020097}
98
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020099func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200100
Anssi Mannilac92b4212020-12-07 14:59:34 +0200101 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
102 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
103
104 registry := new(Registry)
105 registry.Initialize()
106 registry.rtmgrClient = &rtmgrClient
107
108 tracker := new(Tracker)
109 tracker.Init()
110
Anssi Mannilac92b4212020-12-07 14:59:34 +0200111 c := &Control{e2ap: new(E2ap),
Anssi Mannilade457a12021-06-02 16:19:48 +0300112 registry: registry,
113 tracker: tracker,
114 db: CreateSdl(),
115 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
116 LoggerLevel: 3,
Anssi Mannilac92b4212020-12-07 14:59:34 +0200117 }
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200118 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200119
120 // Register REST handler for testing support
121 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000122 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200123
archaggeafbf95f2021-04-14 08:54:05 +0300124 go xapp.Subscription.Listen(c.SubscriptionHandler, c.QueryHandler, c.SubscriptionDeleteHandlerCB)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200125
126 if readSubsFromDb == "false" {
127 return c
128 }
129
130 // Read subscriptions from db
131 xapp.Logger.Info("Reading subscriptions from db")
132 subIds, register, err := c.ReadAllSubscriptionsFromSdl()
133 if err != nil {
134 xapp.Logger.Error("%v", err)
135 } else {
136 c.registry.subIds = subIds
137 c.registry.register = register
138 c.HandleUncompletedSubscriptions(register)
139 }
140 return c
141}
142
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000143func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
144 subscriptions, _ := c.registry.QueryHandler()
145 xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
146}
147
Anssi Mannilac92b4212020-12-07 14:59:34 +0200148//-------------------------------------------------------------------
149//
150//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200151func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200152
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200153 // viper.GetDuration returns nanoseconds
154 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200155 if e2tSubReqTimeout == 0 {
156 e2tSubReqTimeout = 2000 * 1000000
157 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200158 xapp.Logger.Info("e2tSubReqTimeout %v", e2tSubReqTimeout)
159 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200160 if e2tSubDelReqTime == 0 {
161 e2tSubDelReqTime = 2000 * 1000000
162 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200163 xapp.Logger.Info("e2tSubDelReqTime %v", e2tSubDelReqTime)
164 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200165 if e2tRecvMsgTimeout == 0 {
166 e2tRecvMsgTimeout = 2000 * 1000000
167 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200168 xapp.Logger.Info("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000169
170 // Internal cfg parameter, used to define a wait time for RMR route clean-up. None default
171 // value 100ms used currently only in unittests.
172 waitRouteCleanup_ms = viper.GetDuration("controls.waitRouteCleanup_ms") * 1000000
173 if waitRouteCleanup_ms == 0 {
174 waitRouteCleanup_ms = 5000 * 1000000
175 }
176 xapp.Logger.Info("waitRouteCleanup %v", waitRouteCleanup_ms)
177
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200178 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200179 if e2tMaxSubReqTryCount == 0 {
180 e2tMaxSubReqTryCount = 1
181 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200182 xapp.Logger.Info("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000183
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200184 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200185 if e2tMaxSubDelReqTryCount == 0 {
186 e2tMaxSubDelReqTryCount = 1
187 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200188 xapp.Logger.Info("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
189
Anssi Mannilac92b4212020-12-07 14:59:34 +0200190 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
191 if readSubsFromDb == "" {
192 readSubsFromDb = "true"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200193 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200194 xapp.Logger.Info("readSubsFromDb %v", readSubsFromDb)
Anssi Mannilade457a12021-06-02 16:19:48 +0300195 c.LoggerLevel = viper.GetUint32("logger.level")
196 if c.LoggerLevel == 0 {
197 c.LoggerLevel = 3
198 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200199}
200
201//-------------------------------------------------------------------
202//
203//-------------------------------------------------------------------
204func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
205
206 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
207 for subId, subs := range register {
208 if subs.SubRespRcvd == false {
209 subs.NoRespToXapp = true
210 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
211 c.SendSubscriptionDeleteReq(subs)
212 }
213 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200214}
215
216func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300217 if c.RMRClient == nil {
218 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200219 }
kalnagy45114752019-06-18 14:40:39 +0200220}
221
222func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200223 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200224 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000225 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200226}
227
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200228//-------------------------------------------------------------------
229//
230//-------------------------------------------------------------------
archaggea5c58bc2021-04-14 08:54:05 +0300231func (c *Control) SubscriptionHandler(params interface{}) (*models.SubscriptionResponse, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300232
Anssi Mannila316d8a12021-06-02 11:08:54 +0300233 c.CntRecvMsg++
234 c.UpdateCounter(cRestSubReqFromXapp)
235
archaggeafbf95f2021-04-14 08:54:05 +0300236 subResp := models.SubscriptionResponse{}
archaggeafbf95f2021-04-14 08:54:05 +0300237 p := params.(*models.SubscriptionParams)
238
Anssi Mannilade457a12021-06-02 16:19:48 +0300239 if c.LoggerLevel > 2 {
240 c.PrintRESTSubscriptionRequest(p)
241 }
242
archaggeafbf95f2021-04-14 08:54:05 +0300243 if p.ClientEndpoint == nil {
244 xapp.Logger.Error("ClientEndpoint == nil")
Anssi Mannila316d8a12021-06-02 11:08:54 +0300245 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300246 return nil, fmt.Errorf("")
247 }
248
249 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
250 if err != nil {
251 xapp.Logger.Error("%s", err.Error())
Anssi Mannila316d8a12021-06-02 11:08:54 +0300252 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300253 return nil, err
254 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000255 var restSubId string
256 var restSubscription *RESTSubscription
257 if p.SubscriptionID == "" {
258 restSubId = ksuid.New().String()
259 restSubscription, err = c.registry.CreateRESTSubscription(&restSubId, &xAppRmrEndpoint, p.Meid)
260 if err != nil {
261 xapp.Logger.Error("%s", err.Error())
262 c.UpdateCounter(cRestSubFailToXapp)
263 return nil, err
264 }
archaggeafbf95f2021-04-14 08:54:05 +0300265
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000266 } else {
267 restSubId = p.SubscriptionID
268 restSubscription, err = c.registry.GetRESTSubscription(restSubId, false)
269 if err != nil {
270 xapp.Logger.Error("%s", err.Error())
271 c.UpdateCounter(cRestSubFailToXapp)
272 return nil, err
273 }
archaggeafbf95f2021-04-14 08:54:05 +0300274 }
275
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000276 subResp.SubscriptionID = &restSubId
archaggeafbf95f2021-04-14 08:54:05 +0300277 subReqList := e2ap.SubscriptionRequestList{}
278 err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
279 if err != nil {
280 xapp.Logger.Error("%s", err.Error())
281 c.registry.DeleteRESTSubscription(&restSubId)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300282 c.UpdateCounter(cRestSubFailToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300283 return nil, err
284 }
285
286 go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId)
287
Anssi Mannila316d8a12021-06-02 11:08:54 +0300288 c.UpdateCounter(cRestSubRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300289 return &subResp, nil
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300290}
291
archaggeafbf95f2021-04-14 08:54:05 +0300292//-------------------------------------------------------------------
293//
294//-------------------------------------------------------------------
295
296func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
297 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string) {
298
299 xapp.Logger.Info("Subscription Request count=%v ", len(subReqList.E2APSubscriptionRequests))
300
301 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*clientEndpoint)
302 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300303 c.registry.DeleteRESTSubscription(restSubId)
304 xapp.Logger.Error("XAPP-SubReq transaction not created, endpoint createtion failed for RESTSubId=%s, Meid=%s", *restSubId, *meid)
archaggeafbf95f2021-04-14 08:54:05 +0300305 return
306 }
307
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000308 var xAppEventInstanceID int64
309 var e2EventInstanceID int64
Markku Virtanen55d2a282021-06-04 14:46:56 +0300310 var errorCause string
archaggeafbf95f2021-04-14 08:54:05 +0300311 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
312 subReqMsg := subReqList.E2APSubscriptionRequests[index]
313
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000314 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300315 if trans == nil {
316 c.registry.DeleteRESTSubscription(restSubId)
317 xapp.Logger.Error("XAPP-SubReq transaction not created. RESTSubId=%s, EndPoint=%s, Meid=%s", *restSubId, xAppRmrEndpoint, *meid)
318 return
319 }
320
321 defer trans.Release()
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000322 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300323 xapp.Logger.Info("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
324 subRespMsg, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, restSubId)
325 if err != nil {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000326 // Send notification to xApp that prosessing of a Subscription Request has failed.
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000327 e2EventInstanceID = (int64)(0)
Markku Virtanen55d2a282021-06-04 14:46:56 +0300328 errorCause = err.Error()
archaggeafbf95f2021-04-14 08:54:05 +0300329 resp := &models.SubscriptionResponse{
330 SubscriptionID: restSubId,
331 SubscriptionInstances: []*models.SubscriptionInstance{
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000332 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Markku Virtanen55d2a282021-06-04 14:46:56 +0300333 ErrorCause: &errorCause,
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000334 XappEventInstanceID: &xAppEventInstanceID},
archaggeafbf95f2021-04-14 08:54:05 +0300335 },
336 }
337 // Mark REST subscription request processed.
338 restSubscription.SetProcessed()
Markku Virtanen55d2a282021-06-04 14:46:56 +0300339 xapp.Logger.Info("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
340 errorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
archaggeafbf95f2021-04-14 08:54:05 +0300341 xapp.Subscription.Notify(resp, *clientEndpoint)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300342 c.UpdateCounter(cRestSubFailNotifToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300343 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000344 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
345
346 xapp.Logger.Info("SubscriptionRequest index=%v processed successfully. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
347 index, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
archaggeafbf95f2021-04-14 08:54:05 +0300348
349 // Store successfully processed InstanceId for deletion
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000350 restSubscription.AddE2InstanceId(subRespMsg.RequestId.InstanceId)
351 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
archaggeafbf95f2021-04-14 08:54:05 +0300352
353 // Send notification to xApp that a Subscription Request has been processed.
archaggeafbf95f2021-04-14 08:54:05 +0300354 resp := &models.SubscriptionResponse{
355 SubscriptionID: restSubId,
356 SubscriptionInstances: []*models.SubscriptionInstance{
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000357 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
358 ErrorCause: nil,
359 XappEventInstanceID: &xAppEventInstanceID},
archaggeafbf95f2021-04-14 08:54:05 +0300360 },
361 }
362 // Mark REST subscription request processesd.
363 restSubscription.SetProcessed()
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000364 xapp.Logger.Info("Sending successful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
365 clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
archaggeafbf95f2021-04-14 08:54:05 +0300366 xapp.Subscription.Notify(resp, *clientEndpoint)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300367 c.UpdateCounter(cRestSubNotifToXapp)
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000368
archaggeafbf95f2021-04-14 08:54:05 +0300369 }
archaggeafbf95f2021-04-14 08:54:05 +0300370 }
371}
372
373//-------------------------------------------------------------------
374//
375//------------------------------------------------------------------
376func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
377 restSubId *string) (*e2ap.E2APSubscriptionResponse, error) {
378
379 err := c.tracker.Track(trans)
380 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300381 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
382 err = fmt.Errorf("Tracking failure")
archaggeafbf95f2021-04-14 08:54:05 +0300383 return nil, err
384 }
385
386 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
387 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300388 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
archaggeafbf95f2021-04-14 08:54:05 +0300389 return nil, err
390 }
391
392 //
393 // Wake subs request
394 //
395 go c.handleSubscriptionCreate(subs, trans)
396 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
397
398 err = nil
399 if event != nil {
400 switch themsg := event.(type) {
401 case *e2ap.E2APSubscriptionResponse:
402 trans.Release()
403 return themsg, nil
404 case *e2ap.E2APSubscriptionFailure:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300405 err = fmt.Errorf("E2 SubscriptionFailure received")
archaggeafbf95f2021-04-14 08:54:05 +0300406 return nil, err
407 default:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300408 err = fmt.Errorf("unexpected E2 subscription response received")
archaggeafbf95f2021-04-14 08:54:05 +0300409 break
410 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300411 } else {
412 err = fmt.Errorf("E2 subscription response timeout")
archaggeafbf95f2021-04-14 08:54:05 +0300413 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300414
415 xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000416 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300417 return nil, err
418}
419
420//-------------------------------------------------------------------
421//
422//-------------------------------------------------------------------
423func (c *Control) SubscriptionDeleteHandlerCB(restSubId string) error {
424
425 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000426 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300427
428 xapp.Logger.Info("SubscriptionDeleteRequest from XAPP")
429
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000430 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300431 if err != nil {
432 xapp.Logger.Error("%s", err.Error())
433 if restSubscription == nil {
434 // Subscription was not found
435 return nil
436 } else {
437 if restSubscription.SubReqOngoing == true {
438 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
439 xapp.Logger.Error("%s", err.Error())
440 return err
441 } else if restSubscription.SubDelReqOngoing == true {
442 // Previous request for same restSubId still ongoing
443 return nil
444 }
445 }
446 }
447
448 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
449 go func() {
450 for _, instanceId := range restSubscription.InstanceIds {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000451 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
452
archaggeafbf95f2021-04-14 08:54:05 +0300453 if err != nil {
454 xapp.Logger.Error("%s", err.Error())
455 //return err
456 }
457 xapp.Logger.Info("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000458 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
459 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300460 }
461 c.registry.DeleteRESTSubscription(&restSubId)
462 }()
463
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000464 c.UpdateCounter(cRestSubDelRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300465
Anssi Mannilac92b4212020-12-07 14:59:34 +0200466 return nil
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200467}
468
archaggeafbf95f2021-04-14 08:54:05 +0300469//-------------------------------------------------------------------
470//
471//-------------------------------------------------------------------
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000472func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300473
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000474 var xAppEventInstanceID int64
475 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
476 if err != nil {
477 xapp.Logger.Info("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
478 restSubId, instanceId, idstring(err, nil))
479 return xAppEventInstanceID, nil
480 }
481
482 xAppEventInstanceID = int64(subs.ReqId.Id)
483 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300484 if trans == nil {
485 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
486 xapp.Logger.Error("%s", err.Error())
487 }
488 defer trans.Release()
489
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000490 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300491 if err != nil {
492 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
493 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000494 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300495 }
496 //
497 // Wake subs delete
498 //
499 go c.handleSubscriptionDelete(subs, trans)
500 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
501
502 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
503
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000504 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300505
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000506 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300507}
508
509//-------------------------------------------------------------------
510//
511//-------------------------------------------------------------------
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200512func (c *Control) QueryHandler() (models.SubscriptionList, error) {
Anssi Mannila4c626a22021-02-11 12:50:48 +0200513 xapp.Logger.Info("QueryHandler() called")
514
archaggeafbf95f2021-04-14 08:54:05 +0300515 c.CntRecvMsg++
516
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200517 return c.registry.QueryHandler()
518}
519
Anssi Mannilac92b4212020-12-07 14:59:34 +0200520func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200521 xapp.Logger.Info("TestRestHandler() called")
522
523 pathParams := mux.Vars(r)
524 s := pathParams["testId"]
525
526 // This can be used to delete single subscription from db
527 if contains := strings.Contains(s, "deletesubid="); contains == true {
528 var splits = strings.Split(s, "=")
529 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
530 xapp.Logger.Info("RemoveSubscriptionFromSdl() called. subId = %v", subId)
531 c.RemoveSubscriptionFromSdl(uint32(subId))
532 return
533 }
534 }
535
536 // This can be used to remove all subscriptions db from
537 if s == "emptydb" {
538 xapp.Logger.Info("RemoveAllSubscriptionsFromSdl() called")
539 c.RemoveAllSubscriptionsFromSdl()
540 return
541 }
542
543 // This is meant to cause submgr's restart in testing
544 if s == "restart" {
545 xapp.Logger.Info("os.Exit(1) called")
546 os.Exit(1)
547 }
548
549 xapp.Logger.Info("Unsupported rest command received %s", s)
550}
551
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200552//-------------------------------------------------------------------
553//
554//-------------------------------------------------------------------
555
Juha Hyttinen83ada002020-01-30 10:36:33 +0200556func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300557 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200558 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300559 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200560 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200561 params.Meid = subs.GetMeid()
562 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200563 params.PayloadLen = len(trans.Payload.Buf)
564 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200565 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200566 xapp.Logger.Info("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000567 err = c.SendWithRetry(params, false, 5)
568 if err != nil {
569 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
570 }
571 return err
kalnagye0018682019-09-26 16:28:25 +0200572}
573
Juha Hyttinen83ada002020-01-30 10:36:33 +0200574func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
575
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300576 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200577 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300578 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200579 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200580 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200581 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200582 params.PayloadLen = len(trans.Payload.Buf)
583 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200584 params.Mbuf = nil
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200585 xapp.Logger.Info("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000586 err = c.SendWithRetry(params, false, 5)
587 if err != nil {
588 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
589 }
590 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200591}
592
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300593func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
594 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200595 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
596 xapp.Logger.Error("%s", err.Error())
597 return
598 }
599 c.CntRecvMsg++
600
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300601 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200602
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300603 // xapp-frame might use direct access to c buffer and
604 // when msg.Mbuf is freed, someone might take it into use
605 // and payload data might be invalid inside message handle function
606 //
607 // subscriptions won't load system a lot so there is no
608 // real performance hit by cloning buffer into new go byte slice
609 cPay := append(msg.Payload[:0:0], msg.Payload...)
610 msg.Payload = cPay
611 msg.PayloadLen = len(cPay)
612
Anssi Mannila90fa0212019-12-12 10:47:47 +0200613 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200614 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200615 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200616 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200617 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200618 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200619 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200620 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200621 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200622 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200623 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200624 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200625 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200626 default:
627 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000628 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200629 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200630}
631
Juha Hyttinen422d0182020-01-17 13:37:05 +0200632//-------------------------------------------------------------------
633// handle from XAPP Subscription Request
634//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300635func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200636 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200637 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200638
639 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
640 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200641 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200642 return
643 }
644
Anssi Mannila4c626a22021-02-11 12:50:48 +0200645 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200646 if trans == nil {
647 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200648 return
649 }
650 defer trans.Release()
651
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200652 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200653 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
654 return
655 }
656
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300657 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannila4abf1802021-01-28 13:06:46 +0200658 subs, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200659 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200660 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200661 return
662 }
663
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200664 c.wakeSubscriptionRequest(subs, trans)
665}
666
667//-------------------------------------------------------------------
668// Wake Subscription Request to E2node
669//------------------------------------------------------------------
670func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
671
Juha Hyttinen422d0182020-01-17 13:37:05 +0200672 go c.handleSubscriptionCreate(subs, trans)
673 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200674 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200675 if event != nil {
676 switch themsg := event.(type) {
677 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200678 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200679 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
680 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200681 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200682 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200683 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200684 return
685 }
686 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200687 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200688 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
689 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200690 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200691 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200692 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200693 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200694 break
695 }
696 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200697 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300698 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200699}
700
Juha Hyttinen422d0182020-01-17 13:37:05 +0200701//-------------------------------------------------------------------
702// handle from XAPP Subscription Delete Request
703//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300704func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200705 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200706 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200707
708 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200709 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200710 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200711 return
712 }
713
Anssi Mannila4c626a22021-02-11 12:50:48 +0200714 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200715 if trans == nil {
716 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200717 return
718 }
719 defer trans.Release()
720
Juha Hyttinen83ada002020-01-30 10:36:33 +0200721 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200722 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200723 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
724 return
725 }
726
Juha Hyttinen47942b42020-02-27 10:41:43 +0200727 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200728 if err != nil {
729 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200730 return
731 }
732
Juha Hyttinen422d0182020-01-17 13:37:05 +0200733 //
734 // Wake subs delete
735 //
736 go c.handleSubscriptionDelete(subs, trans)
737 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
738
Juha Hyttinen83ada002020-01-30 10:36:33 +0200739 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
740
Anssi Mannilac92b4212020-12-07 14:59:34 +0200741 if subs.NoRespToXapp == true {
742 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
743 return
744 }
745
746 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200747 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +0200748 subDelRespMsg.RequestId.Id = trans.RequestId.Id
749 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200750 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
751 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
752 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200753 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200754 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200755 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200756
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300757 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
758 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200759}
760
761//-------------------------------------------------------------------
762// SUBS CREATE Handling
763//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200764func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200765
Anssi Mannilac92b4212020-12-07 14:59:34 +0200766 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +0200767 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200768 subs.WaitTransactionTurn(trans)
769 defer subs.ReleaseTransactionTurn(trans)
770 defer trans.Release()
771
Juha Hyttinen83ada002020-01-30 10:36:33 +0200772 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200773
Juha Hyttinen83ada002020-01-30 10:36:33 +0200774 subRfMsg, valid := subs.GetCachedResponse()
775 if subRfMsg == nil && valid == true {
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200776 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
777 switch event.(type) {
778 case *e2ap.E2APSubscriptionResponse:
779 subRfMsg, valid = subs.SetCachedResponse(event, true)
780 subs.SubRespRcvd = true
781 case *e2ap.E2APSubscriptionFailure:
782 removeSubscriptionFromDb = true
783 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaebaeae62021-02-12 11:56:20 +0200784 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
archaggeafbf95f2021-04-14 08:54:05 +0300785 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200786 case *SubmgrRestartTestEvent:
787 // This simulates that no response has been received and after restart subscriptions are restored from db
788 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
789 return
790 default:
791 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
792 removeSubscriptionFromDb = true
793 subRfMsg, valid = subs.SetCachedResponse(nil, false)
794 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200795 }
796 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
797 } else {
798 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200799 }
800
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300801 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
802 if valid == false {
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000803 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300804 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200805
806 c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200807 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200808}
809
810//-------------------------------------------------------------------
811// SUBS DELETE Handling
812//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200813
Juha Hyttinen83ada002020-01-30 10:36:33 +0200814func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200815
Juha Hyttinen83ada002020-01-30 10:36:33 +0200816 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200817 subs.WaitTransactionTurn(trans)
818 defer subs.ReleaseTransactionTurn(trans)
819 defer trans.Release()
820
Juha Hyttinen83ada002020-01-30 10:36:33 +0200821 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200822
Juha Hyttinen3944a222020-01-24 11:51:46 +0200823 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200824
Juha Hyttinen3944a222020-01-24 11:51:46 +0200825 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
826 subs.valid = false
827 subs.mutex.Unlock()
828 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
829 } else {
830 subs.mutex.Unlock()
831 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300832 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
833 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
834 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000835 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200836 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +0200837 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200838}
839
840//-------------------------------------------------------------------
841// send to E2T Subscription Request
842//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200843func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200844 var err error
845 var event interface{} = nil
846 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000847 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +0200848
849 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200850 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000851 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200852 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
853 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200854 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200855 return event
856 }
857
Anssi Mannilac92b4212020-12-07 14:59:34 +0200858 // Write uncompleted subscrition in db. If no response for subscrition it need to be re-processed (deleted) after restart
859 c.WriteSubscriptionToDb(subs)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000860
Juha Hyttinen422d0182020-01-17 13:37:05 +0200861 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200862 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200863 if retries == 0 {
864 c.UpdateCounter(cSubReqToE2)
865 } else {
866 c.UpdateCounter(cSubReReqToE2)
867 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200868 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200869 if subs.DoNotWaitSubResp == false {
870 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
871 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200872 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200873 continue
874 }
875 } else {
876 // Simulating case where subscrition request has been sent but response has not been received before restart
877 event = &SubmgrRestartTestEvent{}
Juha Hyttinen422d0182020-01-17 13:37:05 +0200878 }
879 break
880 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200881 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200882 return event
883}
884
885//-------------------------------------------------------------------
886// send to E2T Subscription Delete Request
887//-------------------------------------------------------------------
888
Juha Hyttinen83ada002020-01-30 10:36:33 +0200889func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200890 var err error
891 var event interface{}
892 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000893 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +0200894
895 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200896 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +0000897 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200898 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200899 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
900 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200901 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200902 return event
903 }
904
905 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200906 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200907 if retries == 0 {
908 c.UpdateCounter(cSubDelReqToE2)
909 } else {
910 c.UpdateCounter(cSubDelReReqToE2)
911 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200912 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200913 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
914 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200915 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200916 continue
917 }
918 break
919 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200920 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200921 return event
922}
923
924//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +0200925// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200926//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300927func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200928 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200929 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +0300930
Juha Hyttinen422d0182020-01-17 13:37:05 +0200931 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
932 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200933 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200934 return
935 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300936 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200937 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200938 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200939 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200940 }
941 trans := subs.GetTransaction()
942 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200943 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200944 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200945 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200946 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200947 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
948 if sendOk == false {
949 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200950 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000951 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000952 return
kalnagy45114752019-06-18 14:40:39 +0200953}
954
Juha Hyttinen422d0182020-01-17 13:37:05 +0200955//-------------------------------------------------------------------
956// handle from E2T Subscription Failure
957//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300958func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200959 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200960 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200961 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000962 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200963 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000964 return
965 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300966 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200967 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200968 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200969 return
970 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200971 trans := subs.GetTransaction()
972 if trans == nil {
973 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200974 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200975 return
976 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200977 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
978 if sendOk == false {
979 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200980 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200981 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200982 return
983}
984
Juha Hyttinen422d0182020-01-17 13:37:05 +0200985//-------------------------------------------------------------------
986// handle from E2T Subscription Delete Response
987//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300988func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200989 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200990 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200991 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200992 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200993 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200994 return
995 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300996 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200997 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200998 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200999 return
1000 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001001 trans := subs.GetTransaction()
1002 if trans == nil {
1003 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001004 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001005 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001006 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001007 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1008 if sendOk == false {
1009 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001010 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001011 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001012 return
1013}
1014
Juha Hyttinen422d0182020-01-17 13:37:05 +02001015//-------------------------------------------------------------------
1016// handle from E2T Subscription Delete Failure
1017//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001018func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001019 xapp.Logger.Info("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001020 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001021 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001022 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001023 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001024 return
1025 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001026 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001027 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001028 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001029 return
1030 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001031 trans := subs.GetTransaction()
1032 if trans == nil {
1033 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001034 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001035 return
1036 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001037 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1038 if sendOk == false {
1039 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001040 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001041 }
kalnagy93cc3e22019-09-19 11:29:29 +02001042 return
1043}
1044
Juha Hyttinen422d0182020-01-17 13:37:05 +02001045//-------------------------------------------------------------------
1046//
1047//-------------------------------------------------------------------
1048func typeofSubsMessage(v interface{}) string {
1049 if v == nil {
1050 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001051 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001052 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001053 //case *e2ap.E2APSubscriptionRequest:
1054 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001055 case *e2ap.E2APSubscriptionResponse:
1056 return "SubResp"
1057 case *e2ap.E2APSubscriptionFailure:
1058 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001059 //case *e2ap.E2APSubscriptionDeleteRequest:
1060 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001061 case *e2ap.E2APSubscriptionDeleteResponse:
1062 return "SubDelResp"
1063 case *e2ap.E2APSubscriptionDeleteFailure:
1064 return "SubDelFail"
1065 default:
1066 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001067 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001068}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001069
1070//-------------------------------------------------------------------
1071//
1072//-------------------------------------------------------------------
1073func (c *Control) WriteSubscriptionToDb(subs *Subscription) {
1074 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1075 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1076 if err != nil {
1077 xapp.Logger.Error("%v", err)
1078 }
1079}
1080
1081//-------------------------------------------------------------------
1082//
1083//-------------------------------------------------------------------
1084func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) {
1085
1086 if removeSubscriptionFromDb == true {
1087 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1088 c.RemoveSubscriptionFromDb(subs)
1089 } else {
1090 // Update is needed for successful response and merge case here
1091 if subs.RetryFromXapp == false {
1092 c.WriteSubscriptionToDb(subs)
1093 }
1094 }
1095 subs.RetryFromXapp = false
1096}
1097
1098//-------------------------------------------------------------------
1099//
1100//-------------------------------------------------------------------
1101func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1102 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1103 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1104 if err != nil {
1105 xapp.Logger.Error("%v", err)
1106 }
1107}
1108
1109func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
1110
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001111 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001112 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1113
1114 // Send delete for every endpoint in the subscription
1115 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1116 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001117 subDelReqMsg.RequestId.Id = ricRequestorId
Anssi Mannilac92b4212020-12-07 14:59:34 +02001118 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1119 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1120 if err != nil {
1121 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1122 return
1123 }
1124 for _, endPoint := range subs.EpList.Endpoints {
1125 params := &xapp.RMRParams{}
1126 params.Mtype = mType
1127 params.SubId = int(subs.GetReqId().InstanceId)
1128 params.Xid = ""
1129 params.Meid = subs.Meid
1130 params.Src = endPoint.String()
1131 params.PayloadLen = len(payload.Buf)
1132 params.Payload = payload.Buf
1133 params.Mbuf = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001134 subs.DeleteFromDb = true
1135 c.handleXAPPSubscriptionDeleteRequest(params)
1136 }
1137}
Anssi Mannilade457a12021-06-02 16:19:48 +03001138
1139func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1140
1141 fmt.Println("CRESTSubscriptionRequest")
1142 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1143
1144 if p.ClientEndpoint.HTTPPort != nil {
1145 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1146 } else {
1147 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1148 }
1149
1150 if p.ClientEndpoint.RMRPort != nil {
1151 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1152 } else {
1153 fmt.Println(" ClientEndpoint.RMRPort = nil")
1154 }
1155
1156 if p.Meid != nil {
1157 fmt.Printf(" Meid = %s\n", *p.Meid)
1158 } else {
1159 fmt.Println(" Meid = nil")
1160 }
1161
1162 for _, subscriptionDetail := range p.SubscriptionDetails {
1163 if p.RANFunctionID != nil {
1164 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1165 } else {
1166 fmt.Println(" RANFunctionID = nil")
1167 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001168 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001169 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001170
1171 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1172 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1173 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001174 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001175
Anssi Mannilade457a12021-06-02 16:19:48 +03001176 if actionToBeSetup.SubsequentAction != nil {
1177 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1178 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1179 } else {
1180 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1181 }
1182 }
1183 }
1184}