blob: aaad62565598bca33f81ff264033762396dee813 [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"
Anssi Mannilaf682ace2021-09-28 13:11:25 +030033 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/common"
kalnagy1455c852019-10-21 13:06:23 +020034 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35 httptransport "github.com/go-openapi/runtime/client"
36 "github.com/go-openapi/strfmt"
Anssi Mannilac92b4212020-12-07 14:59:34 +020037 "github.com/gorilla/mux"
archaggeafbf95f2021-04-14 08:54:05 +030038 "github.com/segmentio/ksuid"
kalnagy1455c852019-10-21 13:06:23 +020039 "github.com/spf13/viper"
kalnagy45114752019-06-18 14:40:39 +020040)
41
Juha Hyttinene406a342020-01-13 13:02:26 +020042//-----------------------------------------------------------------------------
43//
44//-----------------------------------------------------------------------------
45
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020046func idstring(err error, entries ...fmt.Stringer) string {
47 var retval string = ""
48 var filler string = ""
49 for _, entry := range entries {
Markku Virtanen42723e22021-06-15 10:09:23 +030050 if entry != nil {
51 retval += filler + entry.String()
52 filler = " "
53 } else {
54 retval += filler + "(NIL)"
55 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020056 }
57 if err != nil {
58 retval += filler + "err(" + err.Error() + ")"
59 filler = " "
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020060 }
61 return retval
62}
63
64//-----------------------------------------------------------------------------
65//
66//-----------------------------------------------------------------------------
67
Anssi Mannila2f26fb22020-12-07 08:32:13 +020068var e2tSubReqTimeout time.Duration
69var e2tSubDelReqTime time.Duration
70var e2tRecvMsgTimeout time.Duration
Markku Virtanenfe2cdab2021-05-21 10:59:29 +000071var waitRouteCleanup_ms time.Duration
Anssi Mannila2f26fb22020-12-07 08:32:13 +020072var e2tMaxSubReqTryCount uint64 // Initial try + retry
73var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
Anssi Mannilac92b4212020-12-07 14:59:34 +020074var readSubsFromDb string
Markku Virtanenb642a192021-06-09 09:08:14 +000075var restDuplicateCtrl duplicateCtrl
Anssi Mannilab73e7cd2021-08-03 11:57:11 +030076var dbRetryForever string
77var dbTryCount int
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020078
kalnagy45114752019-06-18 14:40:39 +020079type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030080 *xapp.RMRClient
Anssi Mannilade457a12021-06-02 16:19:48 +030081 e2ap *E2ap
82 registry *Registry
83 tracker *Tracker
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030084 e2SubsDb Sdlnterface
85 restSubsDb Sdlnterface
Anssi Mannilac92b4212020-12-07 14:59:34 +020086 CntRecvMsg uint64
87 ResetTestFlag bool
Anssi Mannila4abf1802021-01-28 13:06:46 +020088 Counters map[string]xapp.Counter
Anssi Mannilaf682ace2021-09-28 13:11:25 +030089 LoggerLevel int
kalnagy93cc3e22019-09-19 11:29:29 +020090}
91
92type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020093 PlmnID string
94 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020095 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020096}
97
Anssi Mannilac92b4212020-12-07 14:59:34 +020098type SubmgrRestartTestEvent struct{}
99type SubmgrRestartUpEvent struct{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300100type PackSubscriptionRequestErrortEvent struct {
101 ErrorInfo ErrorInfo
102}
103
104func (p *PackSubscriptionRequestErrortEvent) SetEvent(errorInfo *ErrorInfo) {
105 p.ErrorInfo = *errorInfo
106}
107
108type SDLWriteErrortEvent struct {
109 ErrorInfo ErrorInfo
110}
111
112func (s *SDLWriteErrortEvent) SetEvent(errorInfo *ErrorInfo) {
113 s.ErrorInfo = *errorInfo
114}
Anssi Mannilac92b4212020-12-07 14:59:34 +0200115
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000116func init() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300117 xapp.Logger.Debug("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000118 viper.AutomaticEnv()
119 viper.SetEnvPrefix("submgr")
120 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +0200121}
122
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200123func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200124
Anssi Mannilac92b4212020-12-07 14:59:34 +0200125 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
126 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
127
128 registry := new(Registry)
129 registry.Initialize()
130 registry.rtmgrClient = &rtmgrClient
131
132 tracker := new(Tracker)
133 tracker.Init()
134
Anssi Mannilac92b4212020-12-07 14:59:34 +0200135 c := &Control{e2ap: new(E2ap),
Anssi Mannilade457a12021-06-02 16:19:48 +0300136 registry: registry,
137 tracker: tracker,
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300138 e2SubsDb: CreateSdl(),
139 restSubsDb: CreateRESTSdl(),
Anssi Mannilade457a12021-06-02 16:19:48 +0300140 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
141 LoggerLevel: 3,
Anssi Mannilac92b4212020-12-07 14:59:34 +0200142 }
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200143 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200144
145 // Register REST handler for testing support
146 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300147 xapp.Resource.InjectRoute("/ric/v1/restsubscriptions", c.GetAllRestSubscriptions, "GET")
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000148 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200149
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300150 go xapp.Subscription.Listen(c.RESTSubscriptionHandler, c.RESTQueryHandler, c.RESTSubscriptionDeleteHandler)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200151
152 if readSubsFromDb == "false" {
153 return c
154 }
155
Markku Virtanenb642a192021-06-09 09:08:14 +0000156 restDuplicateCtrl.Init()
157
Anssi Mannilac92b4212020-12-07 14:59:34 +0200158 // Read subscriptions from db
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300159 c.ReadE2Subscriptions()
160 c.ReadRESTSubscriptions()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200161 return c
162}
163
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000164func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
165 subscriptions, _ := c.registry.QueryHandler()
166 xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
167}
168
Anssi Mannilac92b4212020-12-07 14:59:34 +0200169//-------------------------------------------------------------------
170//
171//-------------------------------------------------------------------
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300172func (c *Control) GetAllRestSubscriptions(w http.ResponseWriter, r *http.Request) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300173 xapp.Logger.Debug("GetAllRestSubscriptions() called")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300174 response := c.registry.GetAllRestSubscriptions()
175 w.Write(response)
176}
177
178//-------------------------------------------------------------------
179//
180//-------------------------------------------------------------------
181func (c *Control) ReadE2Subscriptions() error {
182 var err error
183 var subIds []uint32
184 var register map[uint32]*Subscription
185 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300186 xapp.Logger.Debug("Reading E2 subscriptions from db")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300187 subIds, register, err = c.ReadAllSubscriptionsFromSdl()
188 if err != nil {
189 xapp.Logger.Error("%v", err)
190 <-time.After(1 * time.Second)
191 } else {
192 c.registry.subIds = subIds
193 c.registry.register = register
194 c.HandleUncompletedSubscriptions(register)
195 return nil
196 }
197 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300198 xapp.Logger.Debug("Continuing without retring")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300199 return err
200}
201
202//-------------------------------------------------------------------
203//
204//-------------------------------------------------------------------
205func (c *Control) ReadRESTSubscriptions() error {
206 var err error
207 var restSubscriptions map[string]*RESTSubscription
208 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300209 xapp.Logger.Debug("Reading REST subscriptions from db")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300210 restSubscriptions, err = c.ReadAllRESTSubscriptionsFromSdl()
211 if err != nil {
212 xapp.Logger.Error("%v", err)
213 <-time.After(1 * time.Second)
214 } else {
215 c.registry.restSubscriptions = restSubscriptions
216 return nil
217 }
218 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300219 xapp.Logger.Debug("Continuing without retring")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300220 return err
221}
222
223//-------------------------------------------------------------------
224//
225//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200226func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200227
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300228 c.LoggerLevel = int(xapp.Logger.GetLevel())
229 xapp.Logger.Debug("LoggerLevel %v", c.LoggerLevel)
230
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200231 // viper.GetDuration returns nanoseconds
232 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200233 if e2tSubReqTimeout == 0 {
234 e2tSubReqTimeout = 2000 * 1000000
235 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300236 xapp.Logger.Debug("e2tSubReqTimeout %v", e2tSubReqTimeout)
237
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200238 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200239 if e2tSubDelReqTime == 0 {
240 e2tSubDelReqTime = 2000 * 1000000
241 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300242 xapp.Logger.Debug("e2tSubDelReqTime %v", e2tSubDelReqTime)
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200243 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200244 if e2tRecvMsgTimeout == 0 {
245 e2tRecvMsgTimeout = 2000 * 1000000
246 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300247 xapp.Logger.Debug("e2tRecvMsgTimeout %v", e2tRecvMsgTimeout)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000248
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200249 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200250 if e2tMaxSubReqTryCount == 0 {
251 e2tMaxSubReqTryCount = 1
252 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300253 xapp.Logger.Debug("e2tMaxSubReqTryCount %v", e2tMaxSubReqTryCount)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000254
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200255 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200256 if e2tMaxSubDelReqTryCount == 0 {
257 e2tMaxSubDelReqTryCount = 1
258 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300259 xapp.Logger.Debug("e2tMaxSubDelReqTryCount %v", e2tMaxSubDelReqTryCount)
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200260
Anssi Mannilac92b4212020-12-07 14:59:34 +0200261 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
262 if readSubsFromDb == "" {
263 readSubsFromDb = "true"
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200264 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300265 xapp.Logger.Debug("readSubsFromDb %v", readSubsFromDb)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300266
267 dbTryCount = viper.GetInt("controls.dbTryCount")
268 if dbTryCount == 0 {
269 dbTryCount = 200
270 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300271 xapp.Logger.Debug("dbTryCount %v", dbTryCount)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300272
273 dbRetryForever = viper.GetString("controls.dbRetryForever")
274 if dbRetryForever == "" {
275 dbRetryForever = "true"
276 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300277 xapp.Logger.Debug("dbRetryForever %v", dbRetryForever)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300278
279 // Internal cfg parameter, used to define a wait time for RMR route clean-up. None default
280 // value 100ms used currently only in unittests.
281 waitRouteCleanup_ms = viper.GetDuration("controls.waitRouteCleanup_ms") * 1000000
282 if waitRouteCleanup_ms == 0 {
283 waitRouteCleanup_ms = 5000 * 1000000
284 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300285 xapp.Logger.Debug("waitRouteCleanup %v", waitRouteCleanup_ms)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200286}
287
288//-------------------------------------------------------------------
289//
290//-------------------------------------------------------------------
291func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
292
293 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
294 for subId, subs := range register {
295 if subs.SubRespRcvd == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300296 // If policy subscription has already been made successfully unsuccessful update should not be deleted.
297 if subs.PolicyUpdate == false {
298 subs.NoRespToXapp = true
299 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
300 c.SendSubscriptionDeleteReq(subs)
301 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200302 }
303 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200304}
305
306func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300307 if c.RMRClient == nil {
308 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200309 }
kalnagy45114752019-06-18 14:40:39 +0200310}
311
312func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200313 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200314 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000315 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200316}
317
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200318//-------------------------------------------------------------------
319//
320//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300321func (c *Control) GetOrCreateRestSubscription(p *models.SubscriptionParams, md5sum string, xAppRmrEndpoint string) (*RESTSubscription, string, string, error) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300322
323 var restSubId string
324 var restSubscription *RESTSubscription
325 var err error
326
327 prevRestSubsId, exists := restDuplicateCtrl.GetLastKnownRestSubsIdBasedOnMd5sum(md5sum)
328 if p.SubscriptionID == "" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300329 // Subscription does not contain REST subscription Id
Markku Virtanen42723e22021-06-15 10:09:23 +0300330 if exists {
331 restSubscription, err = c.registry.GetRESTSubscription(prevRestSubsId, false)
332 if restSubscription != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300333 // Subscription not found
Markku Virtanen42723e22021-06-15 10:09:23 +0300334 restSubId = prevRestSubsId
335 if err == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300336 xapp.Logger.Debug("Existing restSubId %s found by MD5sum %s for a request without subscription ID - using previous subscription", prevRestSubsId, md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300337 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300338 xapp.Logger.Debug("Existing restSubId %s found by MD5sum %s for a request without subscription ID - Note: %s", prevRestSubsId, md5sum, err.Error())
Markku Virtanen42723e22021-06-15 10:09:23 +0300339 }
340 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300341 xapp.Logger.Debug("None existing restSubId %s referred by MD5sum %s for a request without subscription ID - deleting cached entry", prevRestSubsId, md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300342 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
343 }
344 }
345
346 if restSubscription == nil {
347 restSubId = ksuid.New().String()
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300348 restSubscription = c.registry.CreateRESTSubscription(&restSubId, &xAppRmrEndpoint, p.Meid)
Markku Virtanen42723e22021-06-15 10:09:23 +0300349 }
350 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300351 // Subscription contains REST subscription Id
Markku Virtanen42723e22021-06-15 10:09:23 +0300352 restSubId = p.SubscriptionID
353
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300354 xapp.Logger.Debug("RestSubscription ID %s provided via REST request", restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300355 restSubscription, err = c.registry.GetRESTSubscription(restSubId, false)
356 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300357 // Subscription with id in REST request does not exist
Markku Virtanen42723e22021-06-15 10:09:23 +0300358 xapp.Logger.Error("%s", err.Error())
359 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300360 return nil, "", models.SubscriptionInstanceRejectCauseRESTSubscriptionWithGivenIDDoesNotExist, err
Markku Virtanen42723e22021-06-15 10:09:23 +0300361 }
362
363 if !exists {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300364 xapp.Logger.Debug("Existing restSubscription found for ID %s, new request based on md5sum", restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300365 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300366 xapp.Logger.Debug("Existing restSubscription found for ID %s(%s), re-transmission based on md5sum match with previous request", prevRestSubsId, restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300367 }
368 }
369
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300370 return restSubscription, restSubId, "", nil
Markku Virtanen42723e22021-06-15 10:09:23 +0300371}
372
373//-------------------------------------------------------------------
374//
375//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300376func (c *Control) RESTSubscriptionHandler(params interface{}) (*models.SubscriptionResponse, int) {
archaggeafbf95f2021-04-14 08:54:05 +0300377
Anssi Mannila316d8a12021-06-02 11:08:54 +0300378 c.CntRecvMsg++
379 c.UpdateCounter(cRestSubReqFromXapp)
380
archaggeafbf95f2021-04-14 08:54:05 +0300381 subResp := models.SubscriptionResponse{}
archaggeafbf95f2021-04-14 08:54:05 +0300382 p := params.(*models.SubscriptionParams)
383
Anssi Mannilade457a12021-06-02 16:19:48 +0300384 if c.LoggerLevel > 2 {
385 c.PrintRESTSubscriptionRequest(p)
386 }
387
archaggeafbf95f2021-04-14 08:54:05 +0300388 if p.ClientEndpoint == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300389 err := fmt.Errorf("ClientEndpoint == nil")
390 xapp.Logger.Error("%v", err)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300391 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300392 return c.GetSubscriptionResponse(models.SubscriptionInstanceRejectCauseInvalidRESTRequestMessage, err.Error(), "SUBMGR", ""), common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300393 }
394
395 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
396 if err != nil {
397 xapp.Logger.Error("%s", err.Error())
Anssi Mannila316d8a12021-06-02 11:08:54 +0300398 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300399 return c.GetSubscriptionResponse(models.SubscriptionInstanceRejectCauseInvalidRESTRequestMessage, err.Error(), "SUBMGR", ""), common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300400 }
401
Markku Virtanen42723e22021-06-15 10:09:23 +0300402 md5sum, err := CalculateRequestMd5sum(params)
403 if err != nil {
404 xapp.Logger.Error("Failed to generate md5sum from incoming request - %s", err.Error())
405 }
406
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300407 restSubscription, restSubId, rejectCause, err := c.GetOrCreateRestSubscription(p, md5sum, xAppRmrEndpoint)
Markku Virtanen42723e22021-06-15 10:09:23 +0300408 if err != nil {
409 xapp.Logger.Error("Failed to get/allocate REST subscription")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300410 return c.GetSubscriptionResponse(rejectCause, err.Error(), "SUBMGR", ""), common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300411 }
412
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000413 subResp.SubscriptionID = &restSubId
archaggeafbf95f2021-04-14 08:54:05 +0300414 subReqList := e2ap.SubscriptionRequestList{}
415 err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
416 if err != nil {
417 xapp.Logger.Error("%s", err.Error())
Markku Virtanen42723e22021-06-15 10:09:23 +0300418 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300419 c.registry.DeleteRESTSubscription(&restSubId)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300420 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300421 return c.GetSubscriptionResponse(models.SubscriptionInstanceRejectCauseInvalidRESTRequestMessage, err.Error(), "SUBMGR", ""), common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300422 }
423
Markku Virtanen42723e22021-06-15 10:09:23 +0300424 duplicate := restDuplicateCtrl.IsDuplicateToOngoingTransaction(restSubId, md5sum)
425 if duplicate {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300426 err := fmt.Errorf("Retransmission blocker direct ACK for request of restSubsId %s restSubId MD5sum %s as retransmission", restSubId, md5sum)
427 xapp.Logger.Debug("%s", err)
Markku Virtanen42723e22021-06-15 10:09:23 +0300428 c.UpdateCounter(cRestSubRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300429 return &subResp, common.SubscribeCreatedCode
Markku Virtanenb642a192021-06-09 09:08:14 +0000430 }
431
Markku Virtanencd73ecc2021-06-23 06:16:01 +0000432 c.WriteRESTSubscriptionToDb(restSubId, restSubscription)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300433 e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(p)
434 if err != nil {
435 xapp.Logger.Error("%s", err)
436 return c.GetSubscriptionResponse(models.SubscriptionInstanceRejectCauseInvalidRESTRequestMessage, err.Error(), "SUBMGR", ""), common.SubscribeBadRequestCode
437 }
438 go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint, md5sum, e2SubscriptionDirectives)
archaggeafbf95f2021-04-14 08:54:05 +0300439
Anssi Mannila316d8a12021-06-02 11:08:54 +0300440 c.UpdateCounter(cRestSubRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300441 return &subResp, common.SubscribeCreatedCode
442}
443
444//-------------------------------------------------------------------
445//
446//-------------------------------------------------------------------
447func (c *Control) GetE2SubscriptionDirectives(p *models.SubscriptionParams) (*E2SubscriptionDirectives, error) {
448
449 e2SubscriptionDirectives := &E2SubscriptionDirectives{}
450 if p == nil || p.E2SubscriptionDirectives == nil {
451 e2SubscriptionDirectives.E2TimeoutTimerValue = e2tSubReqTimeout
452 e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
453 e2SubscriptionDirectives.CreateRMRRoute = true
454 xapp.Logger.Debug("p == nil || p.E2SubscriptionDirectives == nil. Using default values for E2TimeoutTimerValue = %v and E2RetryCount = %v RMRRoutingNeeded = true", e2tSubReqTimeout, e2tMaxSubReqTryCount)
455 } else {
456 if p.E2SubscriptionDirectives.E2TimeoutTimerValue >= 1 && p.E2SubscriptionDirectives.E2TimeoutTimerValue <= 10 {
457 e2SubscriptionDirectives.E2TimeoutTimerValue = time.Duration(p.E2SubscriptionDirectives.E2TimeoutTimerValue) * 1000000000 // Duration type cast returns nano seconds
458 } else {
459 return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2TimeoutTimerValue out of range (1-10 seconds): %v", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
460 }
461 if p.E2SubscriptionDirectives.E2RetryCount == nil {
462 xapp.Logger.Error("p.E2SubscriptionDirectives.E2RetryCount == nil. Using default value")
463 e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
464 } else {
465 if *p.E2SubscriptionDirectives.E2RetryCount >= 0 && *p.E2SubscriptionDirectives.E2RetryCount <= 10 {
466 e2SubscriptionDirectives.E2MaxTryCount = *p.E2SubscriptionDirectives.E2RetryCount + 1 // E2MaxTryCount = First sending plus two retries
467 } else {
468 return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2RetryCount out of range (0-10): %v", *p.E2SubscriptionDirectives.E2RetryCount)
469 }
470 }
471 if p.E2SubscriptionDirectives.RMRRoutingNeeded == nil {
472 xapp.Logger.Error("p.E2SubscriptionDirectives.RMRRoutingNeeded == nil")
473 e2SubscriptionDirectives.CreateRMRRoute = true
474 } else {
475 e2SubscriptionDirectives.CreateRMRRoute = *p.E2SubscriptionDirectives.RMRRoutingNeeded
476 }
477 }
478 xapp.Logger.Debug("e2SubscriptionDirectives.E2TimeoutTimerValue: %v", e2SubscriptionDirectives.E2TimeoutTimerValue)
479 xapp.Logger.Debug("e2SubscriptionDirectives.E2MaxTryCount: %v", e2SubscriptionDirectives.E2MaxTryCount)
480 xapp.Logger.Debug("e2SubscriptionDirectives.CreateRMRRoute: %v", e2SubscriptionDirectives.CreateRMRRoute)
481 return e2SubscriptionDirectives, nil
482}
483
484//-------------------------------------------------------------------
485//
486//-------------------------------------------------------------------
487func (c *Control) GetSubscriptionResponse(rejectCause string, errorCause string, errorSource string, timeoutType string) *models.SubscriptionResponse {
488 subResp := models.SubscriptionResponse{}
489 subscriptionInstance := models.SubscriptionInstance{}
490 subscriptionInstance.RejectCause = &rejectCause
491 subscriptionInstance.ErrorCause = &errorCause
492 subscriptionInstance.ErrorSource = &errorSource
493 if timeoutType != "" {
494 subscriptionInstance.TimeoutType = &timeoutType
495 }
496 subResp.SubscriptionInstances = append(subResp.SubscriptionInstances, &subscriptionInstance)
497 xapp.Logger.Error("etSubscriptionResponse() %+v", subscriptionInstance)
498
499 return &subResp
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300500}
501
archaggeafbf95f2021-04-14 08:54:05 +0300502//-------------------------------------------------------------------
503//
504//-------------------------------------------------------------------
505
506func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300507 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string, e2SubscriptionDirectives *E2SubscriptionDirectives) {
archaggeafbf95f2021-04-14 08:54:05 +0300508
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300509 xapp.Logger.Debug("Subscription Request count=%v ", len(subReqList.E2APSubscriptionRequests))
archaggeafbf95f2021-04-14 08:54:05 +0300510
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000511 var xAppEventInstanceID int64
512 var e2EventInstanceID int64
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300513 errorInfo := &ErrorInfo{}
Markku Virtanenb642a192021-06-09 09:08:14 +0000514
Markku Virtanen42723e22021-06-15 10:09:23 +0300515 defer restDuplicateCtrl.SetMd5sumFromLastOkRequest(*restSubId, md5sum)
Markku Virtanenb642a192021-06-09 09:08:14 +0000516
archaggeafbf95f2021-04-14 08:54:05 +0300517 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
518 subReqMsg := subReqList.E2APSubscriptionRequests[index]
Markku Virtanenb642a192021-06-09 09:08:14 +0000519 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300520
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000521 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300522 if trans == nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000523 // Send notification to xApp that prosessing of a Subscription Request has failed.
524 err := fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300525 errorInfo.ErrorCause = err.Error()
526 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
Markku Virtanenb642a192021-06-09 09:08:14 +0000527 continue
archaggeafbf95f2021-04-14 08:54:05 +0300528 }
529
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300530 xapp.Logger.Debug("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000531
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300532 subRespMsg, errorInfo, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, *restSubId, e2SubscriptionDirectives)
Markku Virtanen42723e22021-06-15 10:09:23 +0300533
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300534 xapp.Logger.Debug("Handled SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300535
archaggeafbf95f2021-04-14 08:54:05 +0300536 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300537 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
archaggeafbf95f2021-04-14 08:54:05 +0300538 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000539 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300540 restSubscription.AddMd5Sum(md5sum)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300541 xapp.Logger.Debug("SubscriptionRequest index=%v processed successfullyfor %s. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
Markku Virtanen42723e22021-06-15 10:09:23 +0300542 index, *restSubId, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000543 c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans)
archaggeafbf95f2021-04-14 08:54:05 +0300544 }
Markku Virtanenb642a192021-06-09 09:08:14 +0000545 trans.Release()
archaggeafbf95f2021-04-14 08:54:05 +0300546 }
547}
548
549//-------------------------------------------------------------------
550//
551//------------------------------------------------------------------
552func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300553 restSubId string, e2SubscriptionDirectives *E2SubscriptionDirectives) (*e2ap.E2APSubscriptionResponse, *ErrorInfo, error) {
554
555 errorInfo := ErrorInfo{}
archaggeafbf95f2021-04-14 08:54:05 +0300556
557 err := c.tracker.Track(trans)
558 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300559 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300560 errorInfo.ErrorCause = err.Error()
Markku Virtanen55d2a282021-06-04 14:46:56 +0300561 err = fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300562 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300563 }
564
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300565 subs, errorInfo, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, e2SubscriptionDirectives.CreateRMRRoute)
archaggeafbf95f2021-04-14 08:54:05 +0300566 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300567 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300568 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300569 }
570
571 //
572 // Wake subs request
573 //
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300574 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives)
archaggeafbf95f2021-04-14 08:54:05 +0300575 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
576
577 err = nil
578 if event != nil {
579 switch themsg := event.(type) {
580 case *e2ap.E2APSubscriptionResponse:
581 trans.Release()
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300582 return themsg, &errorInfo, nil
archaggeafbf95f2021-04-14 08:54:05 +0300583 case *e2ap.E2APSubscriptionFailure:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300584 err = fmt.Errorf("E2 SubscriptionFailure received")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300585 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
586 return nil, &errorInfo, err
587 case *PackSubscriptionRequestErrortEvent:
588 err = fmt.Errorf("E2 SubscriptionRequest pack failure")
589 return nil, &themsg.ErrorInfo, err
590 case *SDLWriteErrortEvent:
591 err = fmt.Errorf("SDL write failure")
592 return nil, &themsg.ErrorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300593 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300594 err = fmt.Errorf("Unexpected E2 subscription response received")
595 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
archaggeafbf95f2021-04-14 08:54:05 +0300596 break
597 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300598 } else {
599 err = fmt.Errorf("E2 subscription response timeout")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300600 errorInfo.SetInfo(err.Error(), "", models.SubscriptionInstanceTimeoutTypeE2Timeout)
601 if subs.PolicyUpdate == true {
602 return nil, &errorInfo, err
603 }
archaggeafbf95f2021-04-14 08:54:05 +0300604 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300605
606 xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000607 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300608 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300609}
610
611//-------------------------------------------------------------------
612//
613//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300614func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300615 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300616
617 // Send notification to xApp that prosessing of a Subscription Request has failed.
618 e2EventInstanceID := (int64)(0)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300619 if errorInfo.ErrorSource == "" {
620 // Submgr is default source of error
621 errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceSUBMGR
622 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300623 resp := &models.SubscriptionResponse{
624 SubscriptionID: restSubId,
625 SubscriptionInstances: []*models.SubscriptionInstance{
626 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300627 ErrorCause: &errorInfo.ErrorCause,
628 ErrorSource: &errorInfo.ErrorSource,
629 TimeoutType: &errorInfo.TimeoutType,
Markku Virtanen42723e22021-06-15 10:09:23 +0300630 XappEventInstanceID: &xAppEventInstanceID},
631 },
632 }
633 // Mark REST subscription request processed.
634 restSubscription.SetProcessed(err)
635 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
636 if trans != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300637 xapp.Logger.Debug("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
638 errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300639 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300640 xapp.Logger.Debug("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
641 errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
Markku Virtanen42723e22021-06-15 10:09:23 +0300642 }
643
644 c.UpdateCounter(cRestSubFailNotifToXapp)
645 xapp.Subscription.Notify(resp, *clientEndpoint)
646}
647
648//-------------------------------------------------------------------
649//
650//-------------------------------------------------------------------
651func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
652 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp) {
653
654 // Store successfully processed InstanceId for deletion
655 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
656 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
657
658 // Send notification to xApp that a Subscription Request has been processed.
659 resp := &models.SubscriptionResponse{
660 SubscriptionID: restSubId,
661 SubscriptionInstances: []*models.SubscriptionInstance{
662 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
663 ErrorCause: nil,
664 XappEventInstanceID: &xAppEventInstanceID},
665 },
666 }
667 // Mark REST subscription request processesd.
668 restSubscription.SetProcessed(nil)
669 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300670 xapp.Logger.Debug("Sending successful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
Markku Virtanen42723e22021-06-15 10:09:23 +0300671 clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
672
673 c.UpdateCounter(cRestSubNotifToXapp)
674 xapp.Subscription.Notify(resp, *clientEndpoint)
675}
676
677//-------------------------------------------------------------------
678//
679//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300680func (c *Control) RESTSubscriptionDeleteHandler(restSubId string) int {
archaggeafbf95f2021-04-14 08:54:05 +0300681
682 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000683 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300684
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300685 xapp.Logger.Debug("SubscriptionDeleteRequest from XAPP")
archaggeafbf95f2021-04-14 08:54:05 +0300686
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000687 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300688 if err != nil {
689 xapp.Logger.Error("%s", err.Error())
690 if restSubscription == nil {
691 // Subscription was not found
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300692 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300693 } else {
694 if restSubscription.SubReqOngoing == true {
695 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
696 xapp.Logger.Error("%s", err.Error())
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300697 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300698 } else if restSubscription.SubDelReqOngoing == true {
699 // Previous request for same restSubId still ongoing
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300700 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300701 }
702 }
703 }
704
705 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
706 go func() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300707 xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
archaggeafbf95f2021-04-14 08:54:05 +0300708 for _, instanceId := range restSubscription.InstanceIds {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000709 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
710
archaggeafbf95f2021-04-14 08:54:05 +0300711 if err != nil {
712 xapp.Logger.Error("%s", err.Error())
archaggeafbf95f2021-04-14 08:54:05 +0300713 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300714 xapp.Logger.Debug("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000715 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
716 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300717 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300718 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300719 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300720 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300721 }()
722
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000723 c.UpdateCounter(cRestSubDelRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300724
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300725 return common.UnsubscribeNoContentCode
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200726}
727
archaggeafbf95f2021-04-14 08:54:05 +0300728//-------------------------------------------------------------------
729//
730//-------------------------------------------------------------------
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000731func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300732
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000733 var xAppEventInstanceID int64
734 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
735 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300736 xapp.Logger.Debug("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000737 restSubId, instanceId, idstring(err, nil))
738 return xAppEventInstanceID, nil
739 }
740
741 xAppEventInstanceID = int64(subs.ReqId.Id)
742 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300743 if trans == nil {
744 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
745 xapp.Logger.Error("%s", err.Error())
746 }
747 defer trans.Release()
748
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000749 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300750 if err != nil {
751 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
752 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000753 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300754 }
755 //
756 // Wake subs delete
757 //
758 go c.handleSubscriptionDelete(subs, trans)
759 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
760
761 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
762
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000763 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300764
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000765 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300766}
767
768//-------------------------------------------------------------------
769//
770//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300771func (c *Control) RESTQueryHandler() (models.SubscriptionList, error) {
772 xapp.Logger.Debug("RESTQueryHandler() called")
Anssi Mannila4c626a22021-02-11 12:50:48 +0200773
archaggeafbf95f2021-04-14 08:54:05 +0300774 c.CntRecvMsg++
775
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200776 return c.registry.QueryHandler()
777}
778
Anssi Mannilac92b4212020-12-07 14:59:34 +0200779func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300780 xapp.Logger.Debug("RESTTestRestHandler() called")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200781
782 pathParams := mux.Vars(r)
783 s := pathParams["testId"]
784
785 // This can be used to delete single subscription from db
786 if contains := strings.Contains(s, "deletesubid="); contains == true {
787 var splits = strings.Split(s, "=")
788 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300789 xapp.Logger.Debug("RemoveSubscriptionFromSdl() called. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200790 c.RemoveSubscriptionFromSdl(uint32(subId))
791 return
792 }
793 }
794
795 // This can be used to remove all subscriptions db from
796 if s == "emptydb" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300797 xapp.Logger.Debug("RemoveAllSubscriptionsFromSdl() called")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200798 c.RemoveAllSubscriptionsFromSdl()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300799 c.RemoveAllRESTSubscriptionsFromSdl()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200800 return
801 }
802
803 // This is meant to cause submgr's restart in testing
804 if s == "restart" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300805 xapp.Logger.Debug("os.Exit(1) called")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200806 os.Exit(1)
807 }
808
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300809 xapp.Logger.Debug("Unsupported rest command received %s", s)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200810}
811
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200812//-------------------------------------------------------------------
813//
814//-------------------------------------------------------------------
815
Juha Hyttinen83ada002020-01-30 10:36:33 +0200816func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300817 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200818 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300819 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200820 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200821 params.Meid = subs.GetMeid()
822 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200823 params.PayloadLen = len(trans.Payload.Buf)
824 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200825 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300826 xapp.Logger.Debug("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000827 err = c.SendWithRetry(params, false, 5)
828 if err != nil {
829 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
830 }
831 return err
kalnagye0018682019-09-26 16:28:25 +0200832}
833
Juha Hyttinen83ada002020-01-30 10:36:33 +0200834func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
835
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300836 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200837 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300838 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200839 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200840 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200841 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200842 params.PayloadLen = len(trans.Payload.Buf)
843 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200844 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300845 xapp.Logger.Debug("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000846 err = c.SendWithRetry(params, false, 5)
847 if err != nil {
848 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
849 }
850 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200851}
852
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300853func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
854 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200855 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
856 xapp.Logger.Error("%s", err.Error())
857 return
858 }
859 c.CntRecvMsg++
860
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300861 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200862
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300863 // xapp-frame might use direct access to c buffer and
864 // when msg.Mbuf is freed, someone might take it into use
865 // and payload data might be invalid inside message handle function
866 //
867 // subscriptions won't load system a lot so there is no
868 // real performance hit by cloning buffer into new go byte slice
869 cPay := append(msg.Payload[:0:0], msg.Payload...)
870 msg.Payload = cPay
871 msg.PayloadLen = len(cPay)
872
Anssi Mannila90fa0212019-12-12 10:47:47 +0200873 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200874 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200875 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200876 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200877 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200878 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200879 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200880 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200881 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200882 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200883 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200884 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200885 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200886 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300887 xapp.Logger.Debug("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000888 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200889 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200890}
891
Juha Hyttinen422d0182020-01-17 13:37:05 +0200892//-------------------------------------------------------------------
893// handle from XAPP Subscription Request
894//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300895func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300896 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200897 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200898
899 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
900 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200901 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200902 return
903 }
904
Anssi Mannila4c626a22021-02-11 12:50:48 +0200905 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200906 if trans == nil {
907 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200908 return
909 }
910 defer trans.Release()
911
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200912 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200913 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
914 return
915 }
916
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300917 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300918 subs, _, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, true)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200919 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200920 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200921 return
922 }
923
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200924 c.wakeSubscriptionRequest(subs, trans)
925}
926
927//-------------------------------------------------------------------
928// Wake Subscription Request to E2node
929//------------------------------------------------------------------
930func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
931
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300932 e2SubscriptionDirectives, _ := c.GetE2SubscriptionDirectives(nil)
933 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200934 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200935 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200936 if event != nil {
937 switch themsg := event.(type) {
938 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200939 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200940 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
941 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200942 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200943 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200944 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200945 return
946 }
947 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200948 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200949 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
950 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200951 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200952 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200953 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200954 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200955 break
956 }
957 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300958 xapp.Logger.Debug("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300959 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200960}
961
Juha Hyttinen422d0182020-01-17 13:37:05 +0200962//-------------------------------------------------------------------
963// handle from XAPP Subscription Delete Request
964//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300965func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300966 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200967 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200968
969 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200970 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200971 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200972 return
973 }
974
Anssi Mannila4c626a22021-02-11 12:50:48 +0200975 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200976 if trans == nil {
977 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200978 return
979 }
980 defer trans.Release()
981
Juha Hyttinen83ada002020-01-30 10:36:33 +0200982 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200983 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200984 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
985 return
986 }
987
Juha Hyttinen47942b42020-02-27 10:41:43 +0200988 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200989 if err != nil {
990 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200991 return
992 }
993
Juha Hyttinen422d0182020-01-17 13:37:05 +0200994 //
995 // Wake subs delete
996 //
997 go c.handleSubscriptionDelete(subs, trans)
998 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
999
Juha Hyttinen83ada002020-01-30 10:36:33 +02001000 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1001
Anssi Mannilac92b4212020-12-07 14:59:34 +02001002 if subs.NoRespToXapp == true {
1003 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001004 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
Anssi Mannilac92b4212020-12-07 14:59:34 +02001005 return
1006 }
1007
1008 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001009 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +02001010 subDelRespMsg.RequestId.Id = trans.RequestId.Id
1011 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001012 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
1013 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
1014 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001015 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001016 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001017 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001018
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001019 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
1020 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001021}
1022
1023//-------------------------------------------------------------------
1024// SUBS CREATE Handling
1025//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001026func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001027
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001028 var event interface{} = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001029 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +02001030 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001031 subs.WaitTransactionTurn(trans)
1032 defer subs.ReleaseTransactionTurn(trans)
1033 defer trans.Release()
1034
Juha Hyttinen83ada002020-01-30 10:36:33 +02001035 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001036
Juha Hyttinen83ada002020-01-30 10:36:33 +02001037 subRfMsg, valid := subs.GetCachedResponse()
1038 if subRfMsg == nil && valid == true {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001039 event = c.sendE2TSubscriptionRequest(subs, trans, parentTrans, e2SubscriptionDirectives)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001040 switch event.(type) {
1041 case *e2ap.E2APSubscriptionResponse:
1042 subRfMsg, valid = subs.SetCachedResponse(event, true)
1043 subs.SubRespRcvd = true
1044 case *e2ap.E2APSubscriptionFailure:
1045 removeSubscriptionFromDb = true
1046 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001047 xapp.Logger.Debug("SUBS-SubReq: internal delete due failure event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
archaggeafbf95f2021-04-14 08:54:05 +03001048 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001049 case *SubmgrRestartTestEvent:
1050 // This simulates that no response has been received and after restart subscriptions are restored from db
1051 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001052 case *PackSubscriptionRequestErrortEvent, *SDLWriteErrortEvent:
1053 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001054 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001055 if subs.PolicyUpdate == false {
1056 xapp.Logger.Debug("SUBS-SubReq: internal delete due default event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
1057 removeSubscriptionFromDb = true
1058 subRfMsg, valid = subs.SetCachedResponse(nil, false)
1059 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1060 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001061 }
1062 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
1063 } else {
1064 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001065 }
1066
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001067 err := c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
1068 if err != nil {
1069 subRfMsg, valid = subs.SetCachedResponse(event, false)
1070 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1071 }
1072
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001073 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
1074 if valid == false {
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001075 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001076 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001077
Juha Hyttinen83ada002020-01-30 10:36:33 +02001078 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001079}
1080
1081//-------------------------------------------------------------------
1082// SUBS DELETE Handling
1083//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +02001084
Juha Hyttinen83ada002020-01-30 10:36:33 +02001085func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001086
Juha Hyttinen83ada002020-01-30 10:36:33 +02001087 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001088 subs.WaitTransactionTurn(trans)
1089 defer subs.ReleaseTransactionTurn(trans)
1090 defer trans.Release()
1091
Juha Hyttinen83ada002020-01-30 10:36:33 +02001092 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001093
Juha Hyttinen3944a222020-01-24 11:51:46 +02001094 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001095
Juha Hyttinen3944a222020-01-24 11:51:46 +02001096 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
1097 subs.valid = false
1098 subs.mutex.Unlock()
1099 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1100 } else {
1101 subs.mutex.Unlock()
1102 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001103 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
1104 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
1105 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001106 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001107 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +02001108 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001109}
1110
1111//-------------------------------------------------------------------
1112// send to E2T Subscription Request
1113//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001114func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001115 var err error
1116 var event interface{} = nil
1117 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001118 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001119
1120 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +02001121 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001122 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001123 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
1124 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001125 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001126 return &PackSubscriptionRequestErrortEvent{
1127 ErrorInfo{
1128 ErrorSource: models.SubscriptionInstanceErrorSourceASN1,
1129 ErrorCause: err.Error(),
1130 },
1131 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001132 }
1133
Anssi Mannilac92b4212020-12-07 14:59:34 +02001134 // Write uncompleted subscrition in db. If no response for subscrition it need to be re-processed (deleted) after restart
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001135 err = c.WriteSubscriptionToDb(subs)
1136 if err != nil {
1137 return &SDLWriteErrortEvent{
1138 ErrorInfo{
1139 ErrorSource: models.SubscriptionInstanceErrorSourceDBAAS,
1140 ErrorCause: err.Error(),
1141 },
1142 }
1143 }
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001144
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001145 for retries := int64(0); retries < e2SubscriptionDirectives.E2MaxTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001146 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001147 if retries == 0 {
1148 c.UpdateCounter(cSubReqToE2)
1149 } else {
1150 c.UpdateCounter(cSubReReqToE2)
1151 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001152 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001153 if subs.DoNotWaitSubResp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001154 event, timedOut = trans.WaitEvent(e2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001155 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001156 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001157 continue
1158 }
1159 } else {
1160 // Simulating case where subscrition request has been sent but response has not been received before restart
1161 event = &SubmgrRestartTestEvent{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001162 xapp.Logger.Debug("Restart event, DoNotWaitSubResp == true")
Juha Hyttinen422d0182020-01-17 13:37:05 +02001163 }
1164 break
1165 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001166 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001167 return event
1168}
1169
1170//-------------------------------------------------------------------
1171// send to E2T Subscription Delete Request
1172//-------------------------------------------------------------------
1173
Juha Hyttinen83ada002020-01-30 10:36:33 +02001174func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001175 var err error
1176 var event interface{}
1177 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001178 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001179
1180 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +02001181 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001182 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +02001183 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001184 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1185 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001186 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001187 return event
1188 }
1189
1190 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001191 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001192 if retries == 0 {
1193 c.UpdateCounter(cSubDelReqToE2)
1194 } else {
1195 c.UpdateCounter(cSubDelReReqToE2)
1196 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001197 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001198 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
1199 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001200 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001201 continue
1202 }
1203 break
1204 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001205 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001206 return event
1207}
1208
1209//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +02001210// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001211//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001212func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001213 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001214 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +03001215
Juha Hyttinen422d0182020-01-17 13:37:05 +02001216 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
1217 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001218 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001219 return
1220 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001221 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +02001222 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001223 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001224 return
Juha Hyttinen31797b42020-01-16 14:05:01 +02001225 }
1226 trans := subs.GetTransaction()
1227 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001228 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001229 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001230 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001231 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001232 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
1233 if sendOk == false {
1234 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001235 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001236 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001237 return
kalnagy45114752019-06-18 14:40:39 +02001238}
1239
Juha Hyttinen422d0182020-01-17 13:37:05 +02001240//-------------------------------------------------------------------
1241// handle from E2T Subscription Failure
1242//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001243func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001244 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001245 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001246 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001247 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001248 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001249 return
1250 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001251 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001252 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001253 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001254 return
1255 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001256 trans := subs.GetTransaction()
1257 if trans == nil {
1258 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001259 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001260 return
1261 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001262 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1263 if sendOk == false {
1264 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001265 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001266 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001267 return
1268}
1269
Juha Hyttinen422d0182020-01-17 13:37:05 +02001270//-------------------------------------------------------------------
1271// handle from E2T Subscription Delete Response
1272//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001273func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001274 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001275 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001276 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001277 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001278 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001279 return
1280 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001281 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001282 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001283 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001284 return
1285 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001286 trans := subs.GetTransaction()
1287 if trans == nil {
1288 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001289 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001290 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001291 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001292 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1293 if sendOk == false {
1294 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001295 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001296 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001297 return
1298}
1299
Juha Hyttinen422d0182020-01-17 13:37:05 +02001300//-------------------------------------------------------------------
1301// handle from E2T Subscription Delete Failure
1302//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001303func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001304 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001305 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001306 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001307 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001308 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001309 return
1310 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001311 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001312 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001313 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001314 return
1315 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001316 trans := subs.GetTransaction()
1317 if trans == nil {
1318 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001319 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001320 return
1321 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001322 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1323 if sendOk == false {
1324 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001325 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001326 }
kalnagy93cc3e22019-09-19 11:29:29 +02001327 return
1328}
1329
Juha Hyttinen422d0182020-01-17 13:37:05 +02001330//-------------------------------------------------------------------
1331//
1332//-------------------------------------------------------------------
1333func typeofSubsMessage(v interface{}) string {
1334 if v == nil {
1335 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001336 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001337 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001338 //case *e2ap.E2APSubscriptionRequest:
1339 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001340 case *e2ap.E2APSubscriptionResponse:
1341 return "SubResp"
1342 case *e2ap.E2APSubscriptionFailure:
1343 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001344 //case *e2ap.E2APSubscriptionDeleteRequest:
1345 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001346 case *e2ap.E2APSubscriptionDeleteResponse:
1347 return "SubDelResp"
1348 case *e2ap.E2APSubscriptionDeleteFailure:
1349 return "SubDelFail"
1350 default:
1351 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001352 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001353}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001354
1355//-------------------------------------------------------------------
1356//
1357//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001358func (c *Control) WriteSubscriptionToDb(subs *Subscription) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001359 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1360 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1361 if err != nil {
1362 xapp.Logger.Error("%v", err)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001363 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001364 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001365 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001366}
1367
1368//-------------------------------------------------------------------
1369//
1370//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001371func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001372
1373 if removeSubscriptionFromDb == true {
1374 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1375 c.RemoveSubscriptionFromDb(subs)
1376 } else {
1377 // Update is needed for successful response and merge case here
1378 if subs.RetryFromXapp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001379 err := c.WriteSubscriptionToDb(subs)
1380 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001381 }
1382 }
1383 subs.RetryFromXapp = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001384 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001385}
1386
1387//-------------------------------------------------------------------
1388//
1389//-------------------------------------------------------------------
1390func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1391 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1392 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1393 if err != nil {
1394 xapp.Logger.Error("%v", err)
1395 }
1396}
1397
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001398//-------------------------------------------------------------------
1399//
1400//-------------------------------------------------------------------
1401func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1402 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1403 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1404 if err != nil {
1405 xapp.Logger.Error("%v", err)
1406 }
1407}
1408
1409//-------------------------------------------------------------------
1410//
1411//-------------------------------------------------------------------
1412func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1413
1414 if removeRestSubscriptionFromDb == true {
1415 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1416 c.RemoveRESTSubscriptionFromDb(restSubId)
1417 } else {
1418 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1419 }
1420}
1421
1422//-------------------------------------------------------------------
1423//
1424//-------------------------------------------------------------------
1425func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1426 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1427 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1428 if err != nil {
1429 xapp.Logger.Error("%v", err)
1430 }
1431}
1432
Anssi Mannilac92b4212020-12-07 14:59:34 +02001433func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
1434
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001435 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001436 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1437
1438 // Send delete for every endpoint in the subscription
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001439 if subs.PolicyUpdate == false {
1440 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1441 subDelReqMsg.RequestId = subs.GetReqId().RequestId
1442 subDelReqMsg.RequestId.Id = ricRequestorId
1443 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1444 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1445 if err != nil {
1446 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1447 return
1448 }
1449 for _, endPoint := range subs.EpList.Endpoints {
1450 params := &xapp.RMRParams{}
1451 params.Mtype = mType
1452 params.SubId = int(subs.GetReqId().InstanceId)
1453 params.Xid = ""
1454 params.Meid = subs.Meid
1455 params.Src = endPoint.String()
1456 params.PayloadLen = len(payload.Buf)
1457 params.Payload = payload.Buf
1458 params.Mbuf = nil
1459 subs.DeleteFromDb = true
1460 c.handleXAPPSubscriptionDeleteRequest(params)
1461 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001462 }
1463}
Anssi Mannilade457a12021-06-02 16:19:48 +03001464
1465func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1466
1467 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001468
Markku Virtanen2b512b62021-07-30 12:04:00 +00001469 if p == nil {
1470 return
1471 }
1472
Markku Virtanen987f8b12021-06-10 15:05:02 +03001473 if p.SubscriptionID != "" {
1474 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1475 } else {
1476 fmt.Println(" SubscriptionID = ''")
1477 }
1478
Anssi Mannilade457a12021-06-02 16:19:48 +03001479 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1480
1481 if p.ClientEndpoint.HTTPPort != nil {
1482 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1483 } else {
1484 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1485 }
1486
1487 if p.ClientEndpoint.RMRPort != nil {
1488 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1489 } else {
1490 fmt.Println(" ClientEndpoint.RMRPort = nil")
1491 }
1492
1493 if p.Meid != nil {
1494 fmt.Printf(" Meid = %s\n", *p.Meid)
1495 } else {
1496 fmt.Println(" Meid = nil")
1497 }
1498
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001499 if p.E2SubscriptionDirectives == nil {
1500 fmt.Println(" E2SubscriptionDirectives = nil")
1501 } else {
1502 fmt.Println(" E2SubscriptionDirectives")
1503 if p.E2SubscriptionDirectives.E2RetryCount == nil {
1504 fmt.Println(" E2RetryCount == nil")
1505 } else {
1506 fmt.Printf(" E2RetryCount = %v\n", *p.E2SubscriptionDirectives.E2RetryCount)
1507 }
1508 fmt.Printf(" E2TimeoutTimerValue = %v\n", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
1509 if p.E2SubscriptionDirectives.RMRRoutingNeeded == nil {
1510 fmt.Println(" RMRRoutingNeeded == nil")
1511 } else {
1512 fmt.Printf(" RMRRoutingNeeded = %v\n", *p.E2SubscriptionDirectives.RMRRoutingNeeded)
1513 }
1514 }
Anssi Mannilade457a12021-06-02 16:19:48 +03001515 for _, subscriptionDetail := range p.SubscriptionDetails {
1516 if p.RANFunctionID != nil {
1517 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1518 } else {
1519 fmt.Println(" RANFunctionID = nil")
1520 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001521 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001522 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001523
1524 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1525 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1526 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001527 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001528
Anssi Mannilade457a12021-06-02 16:19:48 +03001529 if actionToBeSetup.SubsequentAction != nil {
1530 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1531 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1532 } else {
1533 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1534 }
1535 }
1536 }
1537}