blob: 94109258bb8a228828c6cf928e3985d88ac5c988 [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 Mannilacb7dd492021-10-01 11:10:26 +0300321func (c *Control) GetOrCreateRestSubscription(p *models.SubscriptionParams, md5sum string, xAppRmrEndpoint string) (*RESTSubscription, 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 Mannilacb7dd492021-10-01 11:10:26 +0300360 return nil, "", 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 Mannilacb7dd492021-10-01 11:10:26 +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 Mannilacb7dd492021-10-01 11:10:26 +0300392 return nil, 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 Mannilacb7dd492021-10-01 11:10:26 +0300399 return nil, 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 Mannilacb7dd492021-10-01 11:10:26 +0300407 restSubscription, restSubId, err := c.GetOrCreateRestSubscription(p, md5sum, xAppRmrEndpoint)
Markku Virtanen42723e22021-06-15 10:09:23 +0300408 if err != nil {
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300409 xapp.Logger.Error("Subscription with id in REST request does not exist")
410 return nil, common.SubscribeNotFoundCode
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 Mannilacb7dd492021-10-01 11:10:26 +0300421 return nil, 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)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300436 return nil, common.SubscribeBadRequestCode
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300437 }
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 }
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300471 e2SubscriptionDirectives.CreateRMRRoute = p.E2SubscriptionDirectives.RMRRoutingNeeded
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300472 }
473 xapp.Logger.Debug("e2SubscriptionDirectives.E2TimeoutTimerValue: %v", e2SubscriptionDirectives.E2TimeoutTimerValue)
474 xapp.Logger.Debug("e2SubscriptionDirectives.E2MaxTryCount: %v", e2SubscriptionDirectives.E2MaxTryCount)
475 xapp.Logger.Debug("e2SubscriptionDirectives.CreateRMRRoute: %v", e2SubscriptionDirectives.CreateRMRRoute)
476 return e2SubscriptionDirectives, nil
477}
478
479//-------------------------------------------------------------------
480//
481//-------------------------------------------------------------------
archaggeafbf95f2021-04-14 08:54:05 +0300482
483func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300484 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string, e2SubscriptionDirectives *E2SubscriptionDirectives) {
archaggeafbf95f2021-04-14 08:54:05 +0300485
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300486 xapp.Logger.Debug("Subscription Request count=%v ", len(subReqList.E2APSubscriptionRequests))
archaggeafbf95f2021-04-14 08:54:05 +0300487
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000488 var xAppEventInstanceID int64
489 var e2EventInstanceID int64
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300490 errorInfo := &ErrorInfo{}
Markku Virtanenb642a192021-06-09 09:08:14 +0000491
Markku Virtanen42723e22021-06-15 10:09:23 +0300492 defer restDuplicateCtrl.SetMd5sumFromLastOkRequest(*restSubId, md5sum)
Markku Virtanenb642a192021-06-09 09:08:14 +0000493
archaggeafbf95f2021-04-14 08:54:05 +0300494 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
495 subReqMsg := subReqList.E2APSubscriptionRequests[index]
Markku Virtanenb642a192021-06-09 09:08:14 +0000496 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300497
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000498 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300499 if trans == nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000500 // Send notification to xApp that prosessing of a Subscription Request has failed.
501 err := fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300502 errorInfo.ErrorCause = err.Error()
503 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
Markku Virtanenb642a192021-06-09 09:08:14 +0000504 continue
archaggeafbf95f2021-04-14 08:54:05 +0300505 }
506
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300507 xapp.Logger.Debug("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000508
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300509 subRespMsg, errorInfo, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, *restSubId, e2SubscriptionDirectives)
Markku Virtanen42723e22021-06-15 10:09:23 +0300510
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300511 xapp.Logger.Debug("Handled SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300512
archaggeafbf95f2021-04-14 08:54:05 +0300513 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300514 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
archaggeafbf95f2021-04-14 08:54:05 +0300515 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000516 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300517 restSubscription.AddMd5Sum(md5sum)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300518 xapp.Logger.Debug("SubscriptionRequest index=%v processed successfullyfor %s. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
Markku Virtanen42723e22021-06-15 10:09:23 +0300519 index, *restSubId, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000520 c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans)
archaggeafbf95f2021-04-14 08:54:05 +0300521 }
Markku Virtanenb642a192021-06-09 09:08:14 +0000522 trans.Release()
archaggeafbf95f2021-04-14 08:54:05 +0300523 }
524}
525
526//-------------------------------------------------------------------
527//
528//------------------------------------------------------------------
529func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300530 restSubId string, e2SubscriptionDirectives *E2SubscriptionDirectives) (*e2ap.E2APSubscriptionResponse, *ErrorInfo, error) {
531
532 errorInfo := ErrorInfo{}
archaggeafbf95f2021-04-14 08:54:05 +0300533
534 err := c.tracker.Track(trans)
535 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300536 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300537 errorInfo.ErrorCause = err.Error()
Markku Virtanen55d2a282021-06-04 14:46:56 +0300538 err = fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300539 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300540 }
541
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300542 subs, errorInfo, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, e2SubscriptionDirectives.CreateRMRRoute)
archaggeafbf95f2021-04-14 08:54:05 +0300543 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300544 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300545 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300546 }
547
548 //
549 // Wake subs request
550 //
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300551 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives)
archaggeafbf95f2021-04-14 08:54:05 +0300552 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
553
554 err = nil
555 if event != nil {
556 switch themsg := event.(type) {
557 case *e2ap.E2APSubscriptionResponse:
558 trans.Release()
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300559 return themsg, &errorInfo, nil
archaggeafbf95f2021-04-14 08:54:05 +0300560 case *e2ap.E2APSubscriptionFailure:
Markku Virtanen55d2a282021-06-04 14:46:56 +0300561 err = fmt.Errorf("E2 SubscriptionFailure received")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300562 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
563 return nil, &errorInfo, err
564 case *PackSubscriptionRequestErrortEvent:
565 err = fmt.Errorf("E2 SubscriptionRequest pack failure")
566 return nil, &themsg.ErrorInfo, err
567 case *SDLWriteErrortEvent:
568 err = fmt.Errorf("SDL write failure")
569 return nil, &themsg.ErrorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300570 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300571 err = fmt.Errorf("Unexpected E2 subscription response received")
572 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
archaggeafbf95f2021-04-14 08:54:05 +0300573 break
574 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300575 } else {
576 err = fmt.Errorf("E2 subscription response timeout")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300577 errorInfo.SetInfo(err.Error(), "", models.SubscriptionInstanceTimeoutTypeE2Timeout)
578 if subs.PolicyUpdate == true {
579 return nil, &errorInfo, err
580 }
archaggeafbf95f2021-04-14 08:54:05 +0300581 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300582
583 xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000584 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300585 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300586}
587
588//-------------------------------------------------------------------
589//
590//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300591func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300592 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300593
594 // Send notification to xApp that prosessing of a Subscription Request has failed.
595 e2EventInstanceID := (int64)(0)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300596 if errorInfo.ErrorSource == "" {
597 // Submgr is default source of error
598 errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceSUBMGR
599 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300600 resp := &models.SubscriptionResponse{
601 SubscriptionID: restSubId,
602 SubscriptionInstances: []*models.SubscriptionInstance{
603 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300604 ErrorCause: errorInfo.ErrorCause,
605 ErrorSource: errorInfo.ErrorSource,
606 TimeoutType: errorInfo.TimeoutType,
Markku Virtanen42723e22021-06-15 10:09:23 +0300607 XappEventInstanceID: &xAppEventInstanceID},
608 },
609 }
610 // Mark REST subscription request processed.
611 restSubscription.SetProcessed(err)
612 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
613 if trans != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300614 xapp.Logger.Debug("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
615 errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300616 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300617 xapp.Logger.Debug("Sending unsuccessful REST notification (cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
618 errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
Markku Virtanen42723e22021-06-15 10:09:23 +0300619 }
620
621 c.UpdateCounter(cRestSubFailNotifToXapp)
622 xapp.Subscription.Notify(resp, *clientEndpoint)
623}
624
625//-------------------------------------------------------------------
626//
627//-------------------------------------------------------------------
628func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
629 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp) {
630
631 // Store successfully processed InstanceId for deletion
632 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
633 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
634
635 // Send notification to xApp that a Subscription Request has been processed.
636 resp := &models.SubscriptionResponse{
637 SubscriptionID: restSubId,
638 SubscriptionInstances: []*models.SubscriptionInstance{
639 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300640 ErrorCause: "",
Markku Virtanen42723e22021-06-15 10:09:23 +0300641 XappEventInstanceID: &xAppEventInstanceID},
642 },
643 }
644 // Mark REST subscription request processesd.
645 restSubscription.SetProcessed(nil)
646 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300647 xapp.Logger.Debug("Sending successful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
Markku Virtanen42723e22021-06-15 10:09:23 +0300648 clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
649
650 c.UpdateCounter(cRestSubNotifToXapp)
651 xapp.Subscription.Notify(resp, *clientEndpoint)
652}
653
654//-------------------------------------------------------------------
655//
656//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300657func (c *Control) RESTSubscriptionDeleteHandler(restSubId string) int {
archaggeafbf95f2021-04-14 08:54:05 +0300658
659 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000660 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300661
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300662 xapp.Logger.Debug("SubscriptionDeleteRequest from XAPP")
archaggeafbf95f2021-04-14 08:54:05 +0300663
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000664 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300665 if err != nil {
666 xapp.Logger.Error("%s", err.Error())
667 if restSubscription == nil {
668 // Subscription was not found
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300669 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300670 } else {
671 if restSubscription.SubReqOngoing == true {
672 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
673 xapp.Logger.Error("%s", err.Error())
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300674 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300675 } else if restSubscription.SubDelReqOngoing == true {
676 // Previous request for same restSubId still ongoing
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300677 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300678 }
679 }
680 }
681
682 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
683 go func() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300684 xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
archaggeafbf95f2021-04-14 08:54:05 +0300685 for _, instanceId := range restSubscription.InstanceIds {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000686 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
687
archaggeafbf95f2021-04-14 08:54:05 +0300688 if err != nil {
689 xapp.Logger.Error("%s", err.Error())
archaggeafbf95f2021-04-14 08:54:05 +0300690 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300691 xapp.Logger.Debug("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000692 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
693 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300694 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300695 restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300696 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300697 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300698 }()
699
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000700 c.UpdateCounter(cRestSubDelRespToXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300701
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300702 return common.UnsubscribeNoContentCode
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200703}
704
archaggeafbf95f2021-04-14 08:54:05 +0300705//-------------------------------------------------------------------
706//
707//-------------------------------------------------------------------
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000708func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300709
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000710 var xAppEventInstanceID int64
711 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
712 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300713 xapp.Logger.Debug("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000714 restSubId, instanceId, idstring(err, nil))
715 return xAppEventInstanceID, nil
716 }
717
718 xAppEventInstanceID = int64(subs.ReqId.Id)
719 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300720 if trans == nil {
721 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
722 xapp.Logger.Error("%s", err.Error())
723 }
724 defer trans.Release()
725
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000726 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300727 if err != nil {
728 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
729 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000730 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300731 }
732 //
733 // Wake subs delete
734 //
735 go c.handleSubscriptionDelete(subs, trans)
736 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
737
738 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
739
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000740 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300741
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000742 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300743}
744
745//-------------------------------------------------------------------
746//
747//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300748func (c *Control) RESTQueryHandler() (models.SubscriptionList, error) {
749 xapp.Logger.Debug("RESTQueryHandler() called")
Anssi Mannila4c626a22021-02-11 12:50:48 +0200750
archaggeafbf95f2021-04-14 08:54:05 +0300751 c.CntRecvMsg++
752
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200753 return c.registry.QueryHandler()
754}
755
Anssi Mannilac92b4212020-12-07 14:59:34 +0200756func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300757 xapp.Logger.Debug("RESTTestRestHandler() called")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200758
759 pathParams := mux.Vars(r)
760 s := pathParams["testId"]
761
762 // This can be used to delete single subscription from db
763 if contains := strings.Contains(s, "deletesubid="); contains == true {
764 var splits = strings.Split(s, "=")
765 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300766 xapp.Logger.Debug("RemoveSubscriptionFromSdl() called. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200767 c.RemoveSubscriptionFromSdl(uint32(subId))
768 return
769 }
770 }
771
772 // This can be used to remove all subscriptions db from
773 if s == "emptydb" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300774 xapp.Logger.Debug("RemoveAllSubscriptionsFromSdl() called")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200775 c.RemoveAllSubscriptionsFromSdl()
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300776 c.RemoveAllRESTSubscriptionsFromSdl()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200777 return
778 }
779
780 // This is meant to cause submgr's restart in testing
781 if s == "restart" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300782 xapp.Logger.Debug("os.Exit(1) called")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200783 os.Exit(1)
784 }
785
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300786 xapp.Logger.Debug("Unsupported rest command received %s", s)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200787}
788
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200789//-------------------------------------------------------------------
790//
791//-------------------------------------------------------------------
792
Juha Hyttinen83ada002020-01-30 10:36:33 +0200793func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300794 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200795 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300796 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200797 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200798 params.Meid = subs.GetMeid()
799 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200800 params.PayloadLen = len(trans.Payload.Buf)
801 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200802 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300803 xapp.Logger.Debug("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000804 err = c.SendWithRetry(params, false, 5)
805 if err != nil {
806 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
807 }
808 return err
kalnagye0018682019-09-26 16:28:25 +0200809}
810
Juha Hyttinen83ada002020-01-30 10:36:33 +0200811func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
812
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300813 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200814 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300815 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200816 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200817 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200818 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200819 params.PayloadLen = len(trans.Payload.Buf)
820 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200821 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300822 xapp.Logger.Debug("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000823 err = c.SendWithRetry(params, false, 5)
824 if err != nil {
825 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
826 }
827 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200828}
829
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300830func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
831 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200832 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
833 xapp.Logger.Error("%s", err.Error())
834 return
835 }
836 c.CntRecvMsg++
837
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300838 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200839
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300840 // xapp-frame might use direct access to c buffer and
841 // when msg.Mbuf is freed, someone might take it into use
842 // and payload data might be invalid inside message handle function
843 //
844 // subscriptions won't load system a lot so there is no
845 // real performance hit by cloning buffer into new go byte slice
846 cPay := append(msg.Payload[:0:0], msg.Payload...)
847 msg.Payload = cPay
848 msg.PayloadLen = len(cPay)
849
Anssi Mannila90fa0212019-12-12 10:47:47 +0200850 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200851 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200852 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200853 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200854 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200855 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200856 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200857 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200858 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200859 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200860 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200861 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200862 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200863 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300864 xapp.Logger.Debug("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000865 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200866 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200867}
868
Juha Hyttinen422d0182020-01-17 13:37:05 +0200869//-------------------------------------------------------------------
870// handle from XAPP Subscription Request
871//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300872func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300873 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200874 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200875
876 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
877 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200878 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200879 return
880 }
881
Anssi Mannila4c626a22021-02-11 12:50:48 +0200882 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200883 if trans == nil {
884 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200885 return
886 }
887 defer trans.Release()
888
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200889 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200890 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
891 return
892 }
893
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300894 //TODO handle subscription toward e2term inside AssignToSubscription / hide handleSubscriptionCreate in it?
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300895 subs, _, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, true)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200896 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200897 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200898 return
899 }
900
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200901 c.wakeSubscriptionRequest(subs, trans)
902}
903
904//-------------------------------------------------------------------
905// Wake Subscription Request to E2node
906//------------------------------------------------------------------
907func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
908
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300909 e2SubscriptionDirectives, _ := c.GetE2SubscriptionDirectives(nil)
910 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200911 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200912 var err error
Juha Hyttinen422d0182020-01-17 13:37:05 +0200913 if event != nil {
914 switch themsg := event.(type) {
915 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200916 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200917 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
918 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +0200919 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +0200920 c.UpdateCounter(cSubRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200921 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200922 return
923 }
924 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +0200925 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +0200926 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
927 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200928 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200929 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200930 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200931 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200932 break
933 }
934 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300935 xapp.Logger.Debug("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300936 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200937}
938
Juha Hyttinen422d0182020-01-17 13:37:05 +0200939//-------------------------------------------------------------------
940// handle from XAPP Subscription Delete Request
941//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300942func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300943 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200944 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200945
946 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200947 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200948 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200949 return
950 }
951
Anssi Mannila4c626a22021-02-11 12:50:48 +0200952 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200953 if trans == nil {
954 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200955 return
956 }
957 defer trans.Release()
958
Juha Hyttinen83ada002020-01-30 10:36:33 +0200959 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200960 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200961 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
962 return
963 }
964
Juha Hyttinen47942b42020-02-27 10:41:43 +0200965 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +0200966 if err != nil {
967 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200968 return
969 }
970
Juha Hyttinen422d0182020-01-17 13:37:05 +0200971 //
972 // Wake subs delete
973 //
974 go c.handleSubscriptionDelete(subs, trans)
975 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
976
Juha Hyttinen83ada002020-01-30 10:36:33 +0200977 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
978
Anssi Mannilac92b4212020-12-07 14:59:34 +0200979 if subs.NoRespToXapp == true {
980 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300981 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200982 return
983 }
984
985 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +0200986 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +0200987 subDelRespMsg.RequestId.Id = trans.RequestId.Id
988 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200989 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
990 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
991 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200992 c.UpdateCounter(cSubDelRespToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200993 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200994 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200995
Juha Hyttinen429cfe62020-03-31 11:23:02 +0300996 //TODO handle subscription toward e2term insiged RemoveFromSubscription / hide handleSubscriptionDelete in it?
997 //c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200998}
999
1000//-------------------------------------------------------------------
1001// SUBS CREATE Handling
1002//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001003func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001004
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001005 var event interface{} = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001006 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +02001007 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001008 subs.WaitTransactionTurn(trans)
1009 defer subs.ReleaseTransactionTurn(trans)
1010 defer trans.Release()
1011
Juha Hyttinen83ada002020-01-30 10:36:33 +02001012 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001013
Juha Hyttinen83ada002020-01-30 10:36:33 +02001014 subRfMsg, valid := subs.GetCachedResponse()
1015 if subRfMsg == nil && valid == true {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001016 event = c.sendE2TSubscriptionRequest(subs, trans, parentTrans, e2SubscriptionDirectives)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001017 switch event.(type) {
1018 case *e2ap.E2APSubscriptionResponse:
1019 subRfMsg, valid = subs.SetCachedResponse(event, true)
1020 subs.SubRespRcvd = true
1021 case *e2ap.E2APSubscriptionFailure:
1022 removeSubscriptionFromDb = true
1023 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001024 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 +03001025 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001026 case *SubmgrRestartTestEvent:
1027 // This simulates that no response has been received and after restart subscriptions are restored from db
1028 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001029 case *PackSubscriptionRequestErrortEvent, *SDLWriteErrortEvent:
1030 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001031 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001032 if subs.PolicyUpdate == false {
1033 xapp.Logger.Debug("SUBS-SubReq: internal delete due default event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
1034 removeSubscriptionFromDb = true
1035 subRfMsg, valid = subs.SetCachedResponse(nil, false)
1036 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1037 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001038 }
1039 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
1040 } else {
1041 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001042 }
1043
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001044 err := c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
1045 if err != nil {
1046 subRfMsg, valid = subs.SetCachedResponse(event, false)
1047 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1048 }
1049
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001050 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
1051 if valid == false {
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001052 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001053 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001054
Juha Hyttinen83ada002020-01-30 10:36:33 +02001055 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001056}
1057
1058//-------------------------------------------------------------------
1059// SUBS DELETE Handling
1060//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +02001061
Juha Hyttinen83ada002020-01-30 10:36:33 +02001062func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001063
Juha Hyttinen83ada002020-01-30 10:36:33 +02001064 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001065 subs.WaitTransactionTurn(trans)
1066 defer subs.ReleaseTransactionTurn(trans)
1067 defer trans.Release()
1068
Juha Hyttinen83ada002020-01-30 10:36:33 +02001069 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001070
Juha Hyttinen3944a222020-01-24 11:51:46 +02001071 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001072
Juha Hyttinen3944a222020-01-24 11:51:46 +02001073 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
1074 subs.valid = false
1075 subs.mutex.Unlock()
1076 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1077 } else {
1078 subs.mutex.Unlock()
1079 }
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001080 //Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
1081 // If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
1082 // RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001083 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001084 c.registry.UpdateSubscriptionToDb(subs, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +02001085 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001086}
1087
1088//-------------------------------------------------------------------
1089// send to E2T Subscription Request
1090//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001091func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001092 var err error
1093 var event interface{} = nil
1094 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001095 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001096
1097 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +02001098 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001099 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001100 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
1101 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001102 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001103 return &PackSubscriptionRequestErrortEvent{
1104 ErrorInfo{
1105 ErrorSource: models.SubscriptionInstanceErrorSourceASN1,
1106 ErrorCause: err.Error(),
1107 },
1108 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001109 }
1110
Anssi Mannilac92b4212020-12-07 14:59:34 +02001111 // 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 +03001112 err = c.WriteSubscriptionToDb(subs)
1113 if err != nil {
1114 return &SDLWriteErrortEvent{
1115 ErrorInfo{
1116 ErrorSource: models.SubscriptionInstanceErrorSourceDBAAS,
1117 ErrorCause: err.Error(),
1118 },
1119 }
1120 }
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001121
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001122 for retries := int64(0); retries < e2SubscriptionDirectives.E2MaxTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001123 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001124 if retries == 0 {
1125 c.UpdateCounter(cSubReqToE2)
1126 } else {
1127 c.UpdateCounter(cSubReReqToE2)
1128 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001129 c.rmrSendToE2T(desc, subs, trans)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001130 if subs.DoNotWaitSubResp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001131 event, timedOut = trans.WaitEvent(e2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001132 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001133 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001134 continue
1135 }
1136 } else {
1137 // Simulating case where subscrition request has been sent but response has not been received before restart
1138 event = &SubmgrRestartTestEvent{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001139 xapp.Logger.Debug("Restart event, DoNotWaitSubResp == true")
Juha Hyttinen422d0182020-01-17 13:37:05 +02001140 }
1141 break
1142 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001143 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001144 return event
1145}
1146
1147//-------------------------------------------------------------------
1148// send to E2T Subscription Delete Request
1149//-------------------------------------------------------------------
1150
Juha Hyttinen83ada002020-01-30 10:36:33 +02001151func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001152 var err error
1153 var event interface{}
1154 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001155 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001156
1157 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +02001158 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001159 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +02001160 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001161 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1162 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001163 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001164 return event
1165 }
1166
1167 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001168 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001169 if retries == 0 {
1170 c.UpdateCounter(cSubDelReqToE2)
1171 } else {
1172 c.UpdateCounter(cSubDelReReqToE2)
1173 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001174 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001175 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
1176 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001177 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001178 continue
1179 }
1180 break
1181 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001182 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001183 return event
1184}
1185
1186//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +02001187// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001188//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001189func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001190 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001191 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +03001192
Juha Hyttinen422d0182020-01-17 13:37:05 +02001193 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
1194 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001195 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001196 return
1197 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001198 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +02001199 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001200 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001201 return
Juha Hyttinen31797b42020-01-16 14:05:01 +02001202 }
1203 trans := subs.GetTransaction()
1204 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001205 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001206 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001207 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001208 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001209 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
1210 if sendOk == false {
1211 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001212 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001213 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001214 return
kalnagy45114752019-06-18 14:40:39 +02001215}
1216
Juha Hyttinen422d0182020-01-17 13:37:05 +02001217//-------------------------------------------------------------------
1218// handle from E2T Subscription Failure
1219//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001220func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001221 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001222 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001223 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001224 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001225 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001226 return
1227 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001228 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001229 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001230 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001231 return
1232 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001233 trans := subs.GetTransaction()
1234 if trans == nil {
1235 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001236 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001237 return
1238 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001239 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1240 if sendOk == false {
1241 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001242 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001243 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001244 return
1245}
1246
Juha Hyttinen422d0182020-01-17 13:37:05 +02001247//-------------------------------------------------------------------
1248// handle from E2T Subscription Delete Response
1249//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001250func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001251 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001252 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001253 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001254 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001255 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001256 return
1257 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001258 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001259 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001260 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001261 return
1262 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001263 trans := subs.GetTransaction()
1264 if trans == nil {
1265 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001266 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001267 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001268 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001269 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1270 if sendOk == false {
1271 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001272 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001273 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001274 return
1275}
1276
Juha Hyttinen422d0182020-01-17 13:37:05 +02001277//-------------------------------------------------------------------
1278// handle from E2T Subscription Delete Failure
1279//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001280func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001281 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001282 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001283 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001284 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001285 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001286 return
1287 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001288 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001289 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001290 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001291 return
1292 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001293 trans := subs.GetTransaction()
1294 if trans == nil {
1295 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001296 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001297 return
1298 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001299 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1300 if sendOk == false {
1301 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001302 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001303 }
kalnagy93cc3e22019-09-19 11:29:29 +02001304 return
1305}
1306
Juha Hyttinen422d0182020-01-17 13:37:05 +02001307//-------------------------------------------------------------------
1308//
1309//-------------------------------------------------------------------
1310func typeofSubsMessage(v interface{}) string {
1311 if v == nil {
1312 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001313 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001314 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001315 //case *e2ap.E2APSubscriptionRequest:
1316 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001317 case *e2ap.E2APSubscriptionResponse:
1318 return "SubResp"
1319 case *e2ap.E2APSubscriptionFailure:
1320 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001321 //case *e2ap.E2APSubscriptionDeleteRequest:
1322 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001323 case *e2ap.E2APSubscriptionDeleteResponse:
1324 return "SubDelResp"
1325 case *e2ap.E2APSubscriptionDeleteFailure:
1326 return "SubDelFail"
1327 default:
1328 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001329 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001330}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001331
1332//-------------------------------------------------------------------
1333//
1334//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001335func (c *Control) WriteSubscriptionToDb(subs *Subscription) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001336 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1337 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1338 if err != nil {
1339 xapp.Logger.Error("%v", err)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001340 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001341 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001342 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001343}
1344
1345//-------------------------------------------------------------------
1346//
1347//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001348func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001349
1350 if removeSubscriptionFromDb == true {
1351 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1352 c.RemoveSubscriptionFromDb(subs)
1353 } else {
1354 // Update is needed for successful response and merge case here
1355 if subs.RetryFromXapp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001356 err := c.WriteSubscriptionToDb(subs)
1357 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001358 }
1359 }
1360 subs.RetryFromXapp = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001361 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001362}
1363
1364//-------------------------------------------------------------------
1365//
1366//-------------------------------------------------------------------
1367func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1368 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1369 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1370 if err != nil {
1371 xapp.Logger.Error("%v", err)
1372 }
1373}
1374
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001375//-------------------------------------------------------------------
1376//
1377//-------------------------------------------------------------------
1378func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1379 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1380 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1381 if err != nil {
1382 xapp.Logger.Error("%v", err)
1383 }
1384}
1385
1386//-------------------------------------------------------------------
1387//
1388//-------------------------------------------------------------------
1389func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1390
1391 if removeRestSubscriptionFromDb == true {
1392 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1393 c.RemoveRESTSubscriptionFromDb(restSubId)
1394 } else {
1395 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1396 }
1397}
1398
1399//-------------------------------------------------------------------
1400//
1401//-------------------------------------------------------------------
1402func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1403 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1404 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1405 if err != nil {
1406 xapp.Logger.Error("%v", err)
1407 }
1408}
1409
Anssi Mannilac92b4212020-12-07 14:59:34 +02001410func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
1411
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001412 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001413 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1414
1415 // Send delete for every endpoint in the subscription
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001416 if subs.PolicyUpdate == false {
1417 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1418 subDelReqMsg.RequestId = subs.GetReqId().RequestId
1419 subDelReqMsg.RequestId.Id = ricRequestorId
1420 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1421 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1422 if err != nil {
1423 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1424 return
1425 }
1426 for _, endPoint := range subs.EpList.Endpoints {
1427 params := &xapp.RMRParams{}
1428 params.Mtype = mType
1429 params.SubId = int(subs.GetReqId().InstanceId)
1430 params.Xid = ""
1431 params.Meid = subs.Meid
1432 params.Src = endPoint.String()
1433 params.PayloadLen = len(payload.Buf)
1434 params.Payload = payload.Buf
1435 params.Mbuf = nil
1436 subs.DeleteFromDb = true
1437 c.handleXAPPSubscriptionDeleteRequest(params)
1438 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001439 }
1440}
Anssi Mannilade457a12021-06-02 16:19:48 +03001441
1442func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1443
1444 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001445
Markku Virtanen2b512b62021-07-30 12:04:00 +00001446 if p == nil {
1447 return
1448 }
1449
Markku Virtanen987f8b12021-06-10 15:05:02 +03001450 if p.SubscriptionID != "" {
1451 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1452 } else {
1453 fmt.Println(" SubscriptionID = ''")
1454 }
1455
Anssi Mannilade457a12021-06-02 16:19:48 +03001456 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1457
1458 if p.ClientEndpoint.HTTPPort != nil {
1459 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1460 } else {
1461 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1462 }
1463
1464 if p.ClientEndpoint.RMRPort != nil {
1465 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1466 } else {
1467 fmt.Println(" ClientEndpoint.RMRPort = nil")
1468 }
1469
1470 if p.Meid != nil {
1471 fmt.Printf(" Meid = %s\n", *p.Meid)
1472 } else {
1473 fmt.Println(" Meid = nil")
1474 }
1475
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001476 if p.E2SubscriptionDirectives == nil {
1477 fmt.Println(" E2SubscriptionDirectives = nil")
1478 } else {
1479 fmt.Println(" E2SubscriptionDirectives")
1480 if p.E2SubscriptionDirectives.E2RetryCount == nil {
1481 fmt.Println(" E2RetryCount == nil")
1482 } else {
1483 fmt.Printf(" E2RetryCount = %v\n", *p.E2SubscriptionDirectives.E2RetryCount)
1484 }
1485 fmt.Printf(" E2TimeoutTimerValue = %v\n", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilacb7dd492021-10-01 11:10:26 +03001486 fmt.Printf(" RMRRoutingNeeded = %v\n", p.E2SubscriptionDirectives.RMRRoutingNeeded)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001487 }
Anssi Mannilade457a12021-06-02 16:19:48 +03001488 for _, subscriptionDetail := range p.SubscriptionDetails {
1489 if p.RANFunctionID != nil {
1490 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1491 } else {
1492 fmt.Println(" RANFunctionID = nil")
1493 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001494 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001495 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001496
1497 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1498 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1499 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001500 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001501
Anssi Mannilade457a12021-06-02 16:19:48 +03001502 if actionToBeSetup.SubsequentAction != nil {
1503 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1504 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1505 } else {
1506 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1507 }
1508 }
1509 }
1510}