blob: 58521213f71016c0fa2b025722dff624835bd820 [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"
Anssi Mannila54838ed2021-11-19 11:25:01 +020025 "sync"
Anssi Mannilac92b4212020-12-07 14:59:34 +020026 "time"
27
Juha Hyttinen86a46202020-01-14 12:49:09 +020028 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
kalnagy93cc3e22019-09-19 11:29:29 +020029 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020030 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
Anssi Mannilaf682ace2021-09-28 13:11:25 +030031 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/common"
kalnagy1455c852019-10-21 13:06:23 +020032 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 httptransport "github.com/go-openapi/runtime/client"
34 "github.com/go-openapi/strfmt"
archaggeafbf95f2021-04-14 08:54:05 +030035 "github.com/segmentio/ksuid"
kalnagy1455c852019-10-21 13:06:23 +020036 "github.com/spf13/viper"
kalnagy45114752019-06-18 14:40:39 +020037)
38
Juha Hyttinene406a342020-01-13 13:02:26 +020039//-----------------------------------------------------------------------------
40//
41//-----------------------------------------------------------------------------
42
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020043func idstring(err error, entries ...fmt.Stringer) string {
44 var retval string = ""
45 var filler string = ""
46 for _, entry := range entries {
Markku Virtanen42723e22021-06-15 10:09:23 +030047 if entry != nil {
48 retval += filler + entry.String()
49 filler = " "
50 } else {
51 retval += filler + "(NIL)"
52 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020053 }
54 if err != nil {
55 retval += filler + "err(" + err.Error() + ")"
56 filler = " "
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +020057 }
58 return retval
59}
60
61//-----------------------------------------------------------------------------
62//
63//-----------------------------------------------------------------------------
64
Anssi Mannila2f26fb22020-12-07 08:32:13 +020065var e2tSubReqTimeout time.Duration
66var e2tSubDelReqTime time.Duration
67var e2tRecvMsgTimeout time.Duration
Markku Virtanenfe2cdab2021-05-21 10:59:29 +000068var waitRouteCleanup_ms time.Duration
Anssi Mannila2f26fb22020-12-07 08:32:13 +020069var e2tMaxSubReqTryCount uint64 // Initial try + retry
70var e2tMaxSubDelReqTryCount uint64 // Initial try + retry
Anssi Mannilac7da4ee2021-10-22 09:52:02 +030071var checkE2State string
Anssi Mannilac92b4212020-12-07 14:59:34 +020072var readSubsFromDb string
Anssi Mannilab73e7cd2021-08-03 11:57:11 +030073var dbRetryForever string
74var dbTryCount int
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020075
kalnagy45114752019-06-18 14:40:39 +020076type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030077 *xapp.RMRClient
Anssi Mannila3cdd2e02021-10-14 12:35:41 +030078 e2ap *E2ap
79 registry *Registry
80 tracker *Tracker
81 restDuplicateCtrl *DuplicateCtrl
Anssi Mannilac7da4ee2021-10-22 09:52:02 +030082 e2IfState *E2IfState
83 e2IfStateDb XappRnibInterface
Anssi Mannila3cdd2e02021-10-14 12:35:41 +030084 e2SubsDb Sdlnterface
85 restSubsDb Sdlnterface
86 CntRecvMsg uint64
87 ResetTestFlag bool
88 Counters map[string]xapp.Counter
89 LoggerLevel int
90 UTTesting bool
kalnagy93cc3e22019-09-19 11:29:29 +020091}
92
93type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020094 PlmnID string
95 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020096 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020097}
98
Anssi Mannilac92b4212020-12-07 14:59:34 +020099type SubmgrRestartTestEvent struct{}
100type SubmgrRestartUpEvent struct{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300101type PackSubscriptionRequestErrortEvent struct {
102 ErrorInfo ErrorInfo
103}
104
105func (p *PackSubscriptionRequestErrortEvent) SetEvent(errorInfo *ErrorInfo) {
106 p.ErrorInfo = *errorInfo
107}
108
109type SDLWriteErrortEvent struct {
110 ErrorInfo ErrorInfo
111}
112
113func (s *SDLWriteErrortEvent) SetEvent(errorInfo *ErrorInfo) {
114 s.ErrorInfo = *errorInfo
115}
Anssi Mannilac92b4212020-12-07 14:59:34 +0200116
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000117func init() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300118 xapp.Logger.Debug("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000119 viper.AutomaticEnv()
120 viper.SetEnvPrefix("submgr")
121 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +0200122}
123
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200124func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200125
Anssi Mannilac92b4212020-12-07 14:59:34 +0200126 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
127 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
128
129 registry := new(Registry)
130 registry.Initialize()
131 registry.rtmgrClient = &rtmgrClient
132
133 tracker := new(Tracker)
134 tracker.Init()
135
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300136 restDuplicateCtrl := new(DuplicateCtrl)
137 restDuplicateCtrl.Init()
138
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300139 e2IfState := new(E2IfState)
140
Anssi Mannilac92b4212020-12-07 14:59:34 +0200141 c := &Control{e2ap: new(E2ap),
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300142 registry: registry,
143 tracker: tracker,
144 restDuplicateCtrl: restDuplicateCtrl,
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300145 e2IfState: e2IfState,
146 e2IfStateDb: CreateXappRnibIfInstance(),
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300147 e2SubsDb: CreateSdl(),
148 restSubsDb: CreateRESTSdl(),
149 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
Anssi Mannilae1af87f2021-11-10 13:10:54 +0200150 LoggerLevel: 1,
Anssi Mannilac92b4212020-12-07 14:59:34 +0200151 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300152
153 e2IfState.Init(c)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200154 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200155
156 // Register REST handler for testing support
Anssi Mannila92c38552021-12-29 09:59:24 +0200157 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200158 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300159 xapp.Resource.InjectRoute("/ric/v1/restsubscriptions", c.GetAllRestSubscriptions, "GET")
Anssi Mannila92c38552021-12-29 09:59:24 +0200160
161 xapp.Resource.InjectRoute("/ric/v1/get_all_e2nodes", c.GetAllE2Nodes, "GET")
162 xapp.Resource.InjectRoute("/ric/v1/get_e2node_rest_subscriptions/{ranName}", c.GetAllE2NodeRestSubscriptions, "GET")
163
164 xapp.Resource.InjectRoute("/ric/v1/get_all_xapps", c.GetAllXapps, "GET")
165 xapp.Resource.InjectRoute("/ric/v1/get_xapp_rest_restsubscriptions/{xappServiceName}", c.GetAllXappRestSubscriptions, "GET")
166 xapp.Resource.InjectRoute("/ric/v1/get_e2subscriptions/{restId}", c.GetE2Subscriptions, "GET")
167
Anssi Mannilad13ba632022-01-04 14:22:26 +0200168 xapp.Resource.InjectRoute("/ric/v1/delete_all_e2node_subscriptions/{ranName}", c.DeleteAllE2nodeSubscriptions, "DELETE")
169 xapp.Resource.InjectRoute("/ric/v1/delete_all_xapp_subscriptions/{xappServiceName}", c.DeleteAllXappSubscriptions, "DELETE")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200170
Anssi Mannilae1af87f2021-11-10 13:10:54 +0200171 if readSubsFromDb == "true" {
172 // Read subscriptions from db
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300173 err := c.ReadE2Subscriptions()
174 if err != nil {
175 xapp.Logger.Error("ReadE2Subscriptions() failed %s", err.Error())
176 }
177 err = c.ReadRESTSubscriptions()
178 if err != nil {
179 xapp.Logger.Error("ReadRESTSubscriptions() failed %s", err.Error())
180 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200181 }
182
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300183 go func() {
184 err := xapp.Subscription.Listen(c.RESTSubscriptionHandler, c.RESTQueryHandler, c.RESTSubscriptionDeleteHandler)
185 if err != nil {
186 xapp.Logger.Error("xapp.Subscription.Listen failure: %s", err.Error())
187 }
188 }()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200189 return c
190}
191
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000192func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300193 subscriptions, err := c.registry.QueryHandler()
194 if err != nil {
195 xapp.Logger.Error("QueryHandler() failed %s", err.Error())
196 }
197
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000198 xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
199}
200
Anssi Mannilac92b4212020-12-07 14:59:34 +0200201//-------------------------------------------------------------------
202//
203//-------------------------------------------------------------------
Anssi Mannila92c38552021-12-29 09:59:24 +0200204func (c *Control) RESTQueryHandler() (models.SubscriptionList, error) {
205 xapp.Logger.Debug("RESTQueryHandler() called")
206
207 c.CntRecvMsg++
208
209 return c.registry.QueryHandler()
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300210}
211
212//-------------------------------------------------------------------
213//
214//-------------------------------------------------------------------
215func (c *Control) ReadE2Subscriptions() error {
216 var err error
217 var subIds []uint32
218 var register map[uint32]*Subscription
219 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300220 xapp.Logger.Debug("Reading E2 subscriptions from db")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300221 subIds, register, err = c.ReadAllSubscriptionsFromSdl()
222 if err != nil {
223 xapp.Logger.Error("%v", err)
224 <-time.After(1 * time.Second)
225 } else {
226 c.registry.subIds = subIds
227 c.registry.register = register
Anssi Mannila316b8d22021-12-21 09:43:59 +0200228 go c.HandleUncompletedSubscriptions(register)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300229 return nil
230 }
231 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300232 xapp.Logger.Debug("Continuing without retring")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300233 return err
234}
235
236//-------------------------------------------------------------------
237//
238//-------------------------------------------------------------------
239func (c *Control) ReadRESTSubscriptions() error {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200240
241 xapp.Logger.Debug("ReadRESTSubscriptions()")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300242 var err error
243 var restSubscriptions map[string]*RESTSubscription
244 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300245 xapp.Logger.Debug("Reading REST subscriptions from db")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300246 restSubscriptions, err = c.ReadAllRESTSubscriptionsFromSdl()
247 if err != nil {
248 xapp.Logger.Error("%v", err)
249 <-time.After(1 * time.Second)
250 } else {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200251 // Fix REST subscriptions ongoing status after restart
252 for restSubId, restSubscription := range restSubscriptions {
253 restSubscription.SubReqOngoing = false
254 restSubscription.SubDelReqOngoing = false
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300255 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubscription)
256 if err != nil {
257 xapp.Logger.Error("WriteRESTSubscriptionToSdl() failed:%s", err.Error())
258 }
Anssi Mannila54838ed2021-11-19 11:25:01 +0200259 }
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300260 c.registry.restSubscriptions = restSubscriptions
261 return nil
262 }
263 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300264 xapp.Logger.Debug("Continuing without retring")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300265 return err
266}
267
268//-------------------------------------------------------------------
269//
270//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200271func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200272
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300273 xapp.Logger.Debug("ReadConfigParameters")
274
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300275 c.LoggerLevel = int(xapp.Logger.GetLevel())
Anssi Mannila3d80b722021-11-12 13:17:15 +0200276 xapp.Logger.Info("LoggerLevel = %v", c.LoggerLevel)
Anssi Mannilae1af87f2021-11-10 13:10:54 +0200277 c.e2ap.SetASN1DebugPrintStatus(c.LoggerLevel)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300278
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200279 // viper.GetDuration returns nanoseconds
280 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200281 if e2tSubReqTimeout == 0 {
282 e2tSubReqTimeout = 2000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300283 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tSubReqTimeout")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200284 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300285 xapp.Logger.Debug("e2tSubReqTimeout= %v", e2tSubReqTimeout)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300286
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200287 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200288 if e2tSubDelReqTime == 0 {
289 e2tSubDelReqTime = 2000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300290 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tSubDelReqTime")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200291 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300292 xapp.Logger.Debug("e2tSubDelReqTime= %v", e2tSubDelReqTime)
293
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200294 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200295 if e2tRecvMsgTimeout == 0 {
296 e2tRecvMsgTimeout = 2000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300297 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tRecvMsgTimeout")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200298 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300299 xapp.Logger.Debug("e2tRecvMsgTimeout= %v", e2tRecvMsgTimeout)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000300
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200301 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200302 if e2tMaxSubReqTryCount == 0 {
303 e2tMaxSubReqTryCount = 1
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300304 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200305 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300306 xapp.Logger.Debug("e2tMaxSubReqTryCount= %v", e2tMaxSubReqTryCount)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000307
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200308 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200309 if e2tMaxSubDelReqTryCount == 0 {
310 e2tMaxSubDelReqTryCount = 1
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300311 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200312 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300313 xapp.Logger.Debug("e2tMaxSubDelReqTryCount= %v", e2tMaxSubDelReqTryCount)
314
315 checkE2State = viper.GetString("controls.checkE2State")
316 if checkE2State == "" {
317 checkE2State = "true"
318 xapp.Logger.Debug("WARNING: Using hard coded default value for checkE2State")
319 }
320 xapp.Logger.Debug("checkE2State= %v", checkE2State)
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200321
Anssi Mannilac92b4212020-12-07 14:59:34 +0200322 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
323 if readSubsFromDb == "" {
324 readSubsFromDb = "true"
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300325 xapp.Logger.Debug("WARNING: Using hard coded default value for readSubsFromDb")
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200326 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300327 xapp.Logger.Debug("readSubsFromDb= %v", readSubsFromDb)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300328
329 dbTryCount = viper.GetInt("controls.dbTryCount")
330 if dbTryCount == 0 {
331 dbTryCount = 200
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300332 xapp.Logger.Debug("WARNING: Using hard coded default value for dbTryCount")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300333 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300334 xapp.Logger.Debug("dbTryCount= %v", dbTryCount)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300335
336 dbRetryForever = viper.GetString("controls.dbRetryForever")
337 if dbRetryForever == "" {
338 dbRetryForever = "true"
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300339 xapp.Logger.Debug("WARNING: Using hard coded default value for dbRetryForever")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300340 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300341 xapp.Logger.Debug("dbRetryForever= %v", dbRetryForever)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300342
343 // Internal cfg parameter, used to define a wait time for RMR route clean-up. None default
344 // value 100ms used currently only in unittests.
345 waitRouteCleanup_ms = viper.GetDuration("controls.waitRouteCleanup_ms") * 1000000
346 if waitRouteCleanup_ms == 0 {
347 waitRouteCleanup_ms = 5000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300348 xapp.Logger.Debug("WARNING: Using hard coded default value for waitRouteCleanup_ms")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300349 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300350 xapp.Logger.Debug("waitRouteCleanup= %v", waitRouteCleanup_ms)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200351}
352
353//-------------------------------------------------------------------
354//
355//-------------------------------------------------------------------
356func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
357
358 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
359 for subId, subs := range register {
360 if subs.SubRespRcvd == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300361 // If policy subscription has already been made successfully unsuccessful update should not be deleted.
362 if subs.PolicyUpdate == false {
363 subs.NoRespToXapp = true
364 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
gunjarastogi4378baf2023-01-18 17:51:55 +0530365 c.SendSubscriptionDeleteReq(subs, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300366 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200367 }
368 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200369}
370
371func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300372 if c.RMRClient == nil {
373 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200374 }
kalnagy45114752019-06-18 14:40:39 +0200375}
376
377func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200378 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200379 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000380 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200381}
382
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200383//-------------------------------------------------------------------
384//
385//-------------------------------------------------------------------
Anssi Mannila92c38552021-12-29 09:59:24 +0200386func (c *Control) GetOrCreateRestSubscription(p *models.SubscriptionParams, md5sum string, xAppRmrEndpoint string, xAppServiceName string) (*RESTSubscription, string, error) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300387
388 var restSubId string
389 var restSubscription *RESTSubscription
390 var err error
391
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300392 prevRestSubsId, exists := c.restDuplicateCtrl.GetLastKnownRestSubsIdBasedOnMd5sum(md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300393 if p.SubscriptionID == "" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300394 // Subscription does not contain REST subscription Id
Markku Virtanen42723e22021-06-15 10:09:23 +0300395 if exists {
396 restSubscription, err = c.registry.GetRESTSubscription(prevRestSubsId, false)
397 if restSubscription != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300398 // Subscription not found
Markku Virtanen42723e22021-06-15 10:09:23 +0300399 restSubId = prevRestSubsId
400 if err == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300401 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 +0300402 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300403 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 +0300404 }
405 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300406 xapp.Logger.Debug("None existing restSubId %s referred by MD5sum %s for a request without subscription ID - deleting cached entry", prevRestSubsId, md5sum)
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300407 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300408 }
409 }
410
411 if restSubscription == nil {
412 restSubId = ksuid.New().String()
Anssi Mannila92c38552021-12-29 09:59:24 +0200413 restSubscription = c.registry.CreateRESTSubscription(&restSubId, &xAppServiceName, &xAppRmrEndpoint, p.Meid)
Markku Virtanen42723e22021-06-15 10:09:23 +0300414 }
415 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300416 // Subscription contains REST subscription Id
Markku Virtanen42723e22021-06-15 10:09:23 +0300417 restSubId = p.SubscriptionID
418
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300419 xapp.Logger.Debug("RestSubscription ID %s provided via REST request", restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300420 restSubscription, err = c.registry.GetRESTSubscription(restSubId, false)
421 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300422 // Subscription with id in REST request does not exist
Markku Virtanen42723e22021-06-15 10:09:23 +0300423 xapp.Logger.Error("%s", err.Error())
424 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300425 return nil, "", err
Markku Virtanen42723e22021-06-15 10:09:23 +0300426 }
427
428 if !exists {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300429 xapp.Logger.Debug("Existing restSubscription found for ID %s, new request based on md5sum", restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300430 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300431 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 +0300432 }
433 }
434
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300435 return restSubscription, restSubId, nil
Markku Virtanen42723e22021-06-15 10:09:23 +0300436}
437
438//-------------------------------------------------------------------
439//
440//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300441func (c *Control) RESTSubscriptionHandler(params interface{}) (*models.SubscriptionResponse, int) {
archaggeafbf95f2021-04-14 08:54:05 +0300442
Anssi Mannila316d8a12021-06-02 11:08:54 +0300443 c.CntRecvMsg++
444 c.UpdateCounter(cRestSubReqFromXapp)
445
archaggeafbf95f2021-04-14 08:54:05 +0300446 subResp := models.SubscriptionResponse{}
archaggeafbf95f2021-04-14 08:54:05 +0300447 p := params.(*models.SubscriptionParams)
448
Anssi Mannilade457a12021-06-02 16:19:48 +0300449 if c.LoggerLevel > 2 {
450 c.PrintRESTSubscriptionRequest(p)
451 }
452
naman.gupta7dbc4ad2023-05-09 17:24:05 +0530453 if c.e2IfState.IsE2ConnectionUp(p.Meid) == false || c.e2IfState.IsE2ConnectionUnderReset(p.Meid) == true {
454 if c.e2IfState.IsE2ConnectionUp(p.Meid) == false {
455 xapp.Logger.Error("No E2 connection for ranName %v", *p.Meid)
456 } else if c.e2IfState.IsE2ConnectionUnderReset(p.Meid) == true {
457 xapp.Logger.Error("E2 Node for ranName %v UNDER RESET", *p.Meid)
458 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300459 c.UpdateCounter(cRestReqRejDueE2Down)
460 return nil, common.SubscribeServiceUnavailableCode
461 }
462
archaggeafbf95f2021-04-14 08:54:05 +0300463 if p.ClientEndpoint == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300464 err := fmt.Errorf("ClientEndpoint == nil")
465 xapp.Logger.Error("%v", err)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300466 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300467 return nil, common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300468 }
469
Anssi Mannila6bd57912022-06-16 09:47:23 +0300470 e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(p)
471 if err != nil {
472 xapp.Logger.Error("%s", err)
473 c.UpdateCounter(cRestSubFailToXapp)
474 return nil, common.SubscribeBadRequestCode
475 }
archaggeafbf95f2021-04-14 08:54:05 +0300476 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
477 if err != nil {
478 xapp.Logger.Error("%s", err.Error())
Anssi Mannila316d8a12021-06-02 11:08:54 +0300479 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300480 return nil, common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300481 }
482
Markku Virtanen42723e22021-06-15 10:09:23 +0300483 md5sum, err := CalculateRequestMd5sum(params)
484 if err != nil {
485 xapp.Logger.Error("Failed to generate md5sum from incoming request - %s", err.Error())
486 }
487
Anssi Mannila92c38552021-12-29 09:59:24 +0200488 restSubscription, restSubId, err := c.GetOrCreateRestSubscription(p, md5sum, xAppRmrEndpoint, p.ClientEndpoint.Host)
Markku Virtanen42723e22021-06-15 10:09:23 +0300489 if err != nil {
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300490 xapp.Logger.Error("Subscription with id in REST request does not exist")
491 return nil, common.SubscribeNotFoundCode
archaggeafbf95f2021-04-14 08:54:05 +0300492 }
493
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000494 subResp.SubscriptionID = &restSubId
archaggeafbf95f2021-04-14 08:54:05 +0300495 subReqList := e2ap.SubscriptionRequestList{}
496 err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
497 if err != nil {
498 xapp.Logger.Error("%s", err.Error())
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300499 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300500 c.registry.DeleteRESTSubscription(&restSubId)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300501 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300502 return nil, common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300503 }
504
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300505 duplicate := c.restDuplicateCtrl.IsDuplicateToOngoingTransaction(restSubId, md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300506 if duplicate {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300507 err := fmt.Errorf("Retransmission blocker direct ACK for request of restSubsId %s restSubId MD5sum %s as retransmission", restSubId, md5sum)
508 xapp.Logger.Debug("%s", err)
Anssi Mannilab43397e2021-11-16 12:06:16 +0200509 c.registry.DeleteRESTSubscription(&restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300510 c.UpdateCounter(cRestSubRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300511 return &subResp, common.SubscribeCreatedCode
Markku Virtanenb642a192021-06-09 09:08:14 +0000512 }
513
Markku Virtanencd73ecc2021-06-23 06:16:01 +0000514 c.WriteRESTSubscriptionToDb(restSubId, restSubscription)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300515 go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint, md5sum, e2SubscriptionDirectives)
archaggeafbf95f2021-04-14 08:54:05 +0300516
Anssi Mannila316d8a12021-06-02 11:08:54 +0300517 c.UpdateCounter(cRestSubRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300518 return &subResp, common.SubscribeCreatedCode
519}
520
521//-------------------------------------------------------------------
522//
523//-------------------------------------------------------------------
524func (c *Control) GetE2SubscriptionDirectives(p *models.SubscriptionParams) (*E2SubscriptionDirectives, error) {
525
526 e2SubscriptionDirectives := &E2SubscriptionDirectives{}
527 if p == nil || p.E2SubscriptionDirectives == nil {
528 e2SubscriptionDirectives.E2TimeoutTimerValue = e2tSubReqTimeout
529 e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
530 e2SubscriptionDirectives.CreateRMRRoute = true
531 xapp.Logger.Debug("p == nil || p.E2SubscriptionDirectives == nil. Using default values for E2TimeoutTimerValue = %v and E2RetryCount = %v RMRRoutingNeeded = true", e2tSubReqTimeout, e2tMaxSubReqTryCount)
532 } else {
533 if p.E2SubscriptionDirectives.E2TimeoutTimerValue >= 1 && p.E2SubscriptionDirectives.E2TimeoutTimerValue <= 10 {
534 e2SubscriptionDirectives.E2TimeoutTimerValue = time.Duration(p.E2SubscriptionDirectives.E2TimeoutTimerValue) * 1000000000 // Duration type cast returns nano seconds
535 } else {
536 return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2TimeoutTimerValue out of range (1-10 seconds): %v", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
537 }
538 if p.E2SubscriptionDirectives.E2RetryCount == nil {
539 xapp.Logger.Error("p.E2SubscriptionDirectives.E2RetryCount == nil. Using default value")
540 e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
541 } else {
542 if *p.E2SubscriptionDirectives.E2RetryCount >= 0 && *p.E2SubscriptionDirectives.E2RetryCount <= 10 {
543 e2SubscriptionDirectives.E2MaxTryCount = *p.E2SubscriptionDirectives.E2RetryCount + 1 // E2MaxTryCount = First sending plus two retries
544 } else {
545 return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2RetryCount out of range (0-10): %v", *p.E2SubscriptionDirectives.E2RetryCount)
546 }
547 }
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300548 e2SubscriptionDirectives.CreateRMRRoute = p.E2SubscriptionDirectives.RMRRoutingNeeded
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300549 }
550 xapp.Logger.Debug("e2SubscriptionDirectives.E2TimeoutTimerValue: %v", e2SubscriptionDirectives.E2TimeoutTimerValue)
551 xapp.Logger.Debug("e2SubscriptionDirectives.E2MaxTryCount: %v", e2SubscriptionDirectives.E2MaxTryCount)
552 xapp.Logger.Debug("e2SubscriptionDirectives.CreateRMRRoute: %v", e2SubscriptionDirectives.CreateRMRRoute)
553 return e2SubscriptionDirectives, nil
554}
555
556//-------------------------------------------------------------------
557//
558//-------------------------------------------------------------------
archaggeafbf95f2021-04-14 08:54:05 +0300559
560func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300561 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string, e2SubscriptionDirectives *E2SubscriptionDirectives) {
archaggeafbf95f2021-04-14 08:54:05 +0300562
Anssi Mannila08c8fbd2021-10-05 12:47:41 +0300563 c.SubscriptionProcessingStartDelay()
Anssi Mannila54838ed2021-11-19 11:25:01 +0200564 xapp.Logger.Debug("E2 SubscriptionRequest count = %v ", len(subReqList.E2APSubscriptionRequests))
archaggeafbf95f2021-04-14 08:54:05 +0300565
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000566 var xAppEventInstanceID int64
567 var e2EventInstanceID int64
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300568 errorInfo := &ErrorInfo{}
Markku Virtanenb642a192021-06-09 09:08:14 +0000569
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300570 defer c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(*restSubId, md5sum)
Markku Virtanenb642a192021-06-09 09:08:14 +0000571
archaggeafbf95f2021-04-14 08:54:05 +0300572 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
573 subReqMsg := subReqList.E2APSubscriptionRequests[index]
Markku Virtanenb642a192021-06-09 09:08:14 +0000574 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300575
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000576 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300577 if trans == nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000578 // Send notification to xApp that prosessing of a Subscription Request has failed.
579 err := fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300580 errorInfo.ErrorCause = err.Error()
581 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
Markku Virtanenb642a192021-06-09 09:08:14 +0000582 continue
archaggeafbf95f2021-04-14 08:54:05 +0300583 }
584
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300585 xapp.Logger.Debug("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000586
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300587 subRespMsg, errorInfo, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, *restSubId, e2SubscriptionDirectives)
Markku Virtanen42723e22021-06-15 10:09:23 +0300588
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300589 xapp.Logger.Debug("Handled SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Anssi Mannila53dbe7e2021-10-06 14:21:48 +0300590 trans.Release()
Markku Virtanen42723e22021-06-15 10:09:23 +0300591
archaggeafbf95f2021-04-14 08:54:05 +0300592 if err != nil {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200593 if err.Error() == "TEST: restart event received" {
594 // This is just for UT cases. Stop here subscription processing
595 return
596 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300597 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
archaggeafbf95f2021-04-14 08:54:05 +0300598 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000599 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300600 restSubscription.AddMd5Sum(md5sum)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300601 xapp.Logger.Debug("SubscriptionRequest index=%v processed successfullyfor %s. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
Markku Virtanen42723e22021-06-15 10:09:23 +0300602 index, *restSubId, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Anssi Mannila56233b02022-01-24 13:26:42 +0200603 c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans, errorInfo)
archaggeafbf95f2021-04-14 08:54:05 +0300604 }
archaggeafbf95f2021-04-14 08:54:05 +0300605 }
606}
607
608//-------------------------------------------------------------------
609//
610//------------------------------------------------------------------
Anssi Mannila08c8fbd2021-10-05 12:47:41 +0300611func (c *Control) SubscriptionProcessingStartDelay() {
612 if c.UTTesting == true {
613 // This is temporary fix for the UT problem that notification arrives before subscription response
614 // Correct fix would be to allow notification come before response and process it correctly
615 xapp.Logger.Debug("Setting 50 ms delay before starting processing Subscriptions")
616 <-time.After(time.Millisecond * 50)
617 xapp.Logger.Debug("Continuing after delay")
618 }
619}
620
621//-------------------------------------------------------------------
622//
623//------------------------------------------------------------------
archaggeafbf95f2021-04-14 08:54:05 +0300624func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300625 restSubId string, e2SubscriptionDirectives *E2SubscriptionDirectives) (*e2ap.E2APSubscriptionResponse, *ErrorInfo, error) {
626
627 errorInfo := ErrorInfo{}
archaggeafbf95f2021-04-14 08:54:05 +0300628
629 err := c.tracker.Track(trans)
630 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300631 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300632 errorInfo.ErrorCause = err.Error()
Markku Virtanen55d2a282021-06-04 14:46:56 +0300633 err = fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300634 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300635 }
636
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300637 subs, errorInfo, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, e2SubscriptionDirectives.CreateRMRRoute)
archaggeafbf95f2021-04-14 08:54:05 +0300638 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300639 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300640 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300641 }
642
643 //
644 // Wake subs request
645 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300646 subs.OngoingReqCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +0200647 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, 0)
archaggeafbf95f2021-04-14 08:54:05 +0300648 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300649 subs.OngoingReqCount--
archaggeafbf95f2021-04-14 08:54:05 +0300650
651 err = nil
652 if event != nil {
653 switch themsg := event.(type) {
654 case *e2ap.E2APSubscriptionResponse:
655 trans.Release()
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300656 if c.e2IfState.IsE2ConnectionUp(meid) == true {
Anssi Mannila56233b02022-01-24 13:26:42 +0200657 errorInfo = c.e2ap.CheckActionNotAdmittedList(xapp.RIC_SUB_RESP, themsg.ActionNotAdmittedList, c)
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300658 return themsg, &errorInfo, nil
659 } else {
660 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
661 c.RemoveSubscriptionFromDb(subs)
662 err = fmt.Errorf("E2 interface down")
663 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300664 }
archaggeafbf95f2021-04-14 08:54:05 +0300665 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila483bd162022-01-20 11:11:48 +0200666 err = fmt.Errorf("RICSubscriptionFailure. E2NodeCause: (Cause:%v, Value %v)", themsg.Cause.Content, themsg.Cause.Value)
667 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300668 case *PackSubscriptionRequestErrortEvent:
Anssi Mannila56233b02022-01-24 13:26:42 +0200669 err = fmt.Errorf("E2 RICSubscriptionRequest pack failure")
Anssi Mannilab43397e2021-11-16 12:06:16 +0200670 errorInfo = themsg.ErrorInfo
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300671 case *SDLWriteErrortEvent:
672 err = fmt.Errorf("SDL write failure")
Anssi Mannilab43397e2021-11-16 12:06:16 +0200673 errorInfo = themsg.ErrorInfo
Anssi Mannila54838ed2021-11-19 11:25:01 +0200674 case *SubmgrRestartTestEvent:
675 err = fmt.Errorf("TEST: restart event received")
676 xapp.Logger.Debug("%s", err)
677 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300678 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300679 err = fmt.Errorf("Unexpected E2 subscription response received")
680 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
archaggeafbf95f2021-04-14 08:54:05 +0300681 break
682 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300683 } else {
Anssi Mannila3d80b722021-11-12 13:17:15 +0200684 // Timer expiry
Anssi Mannila56233b02022-01-24 13:26:42 +0200685 err = fmt.Errorf("E2 RICSubscriptionResponse timeout")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300686 errorInfo.SetInfo(err.Error(), "", models.SubscriptionInstanceTimeoutTypeE2Timeout)
687 if subs.PolicyUpdate == true {
688 return nil, &errorInfo, err
689 }
archaggeafbf95f2021-04-14 08:54:05 +0300690 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300691
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300692 xapp.Logger.Error("XAPP-SubReq E2 subscription failed: %s", idstring(err, trans, subs))
aditverm323c77e2023-09-20 09:50:47 +0530693 // If policy type subscription fails we cannot remove it only internally. Once subscription has been created
694 // successfully, it must be deleted on both sides.
695 if subs.PolicyUpdate == false {
696 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
697 }
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300698
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300699 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300700}
701
702//-------------------------------------------------------------------
703//
704//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300705func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300706 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300707
708 // Send notification to xApp that prosessing of a Subscription Request has failed.
709 e2EventInstanceID := (int64)(0)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300710 if errorInfo.ErrorSource == "" {
711 // Submgr is default source of error
712 errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceSUBMGR
713 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300714 resp := &models.SubscriptionResponse{
715 SubscriptionID: restSubId,
716 SubscriptionInstances: []*models.SubscriptionInstance{
717 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300718 ErrorCause: errorInfo.ErrorCause,
719 ErrorSource: errorInfo.ErrorSource,
720 TimeoutType: errorInfo.TimeoutType,
Markku Virtanen42723e22021-06-15 10:09:23 +0300721 XappEventInstanceID: &xAppEventInstanceID},
722 },
723 }
724 // Mark REST subscription request processed.
725 restSubscription.SetProcessed(err)
726 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
727 if trans != nil {
Anssi Mannila6bd57912022-06-16 09:47:23 +0300728 xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
729 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300730 } else {
Anssi Mannila6bd57912022-06-16 09:47:23 +0300731 xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
732 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
Markku Virtanen42723e22021-06-15 10:09:23 +0300733 }
734
735 c.UpdateCounter(cRestSubFailNotifToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300736 err = xapp.Subscription.Notify(resp, *clientEndpoint)
737 if err != nil {
738 xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
739 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300740
Anssi Mannilab43397e2021-11-16 12:06:16 +0200741 // E2 is down. Delete completely processed request safely now
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300742 if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
743 c.registry.DeleteRESTSubscription(restSubId)
744 c.RemoveRESTSubscriptionFromDb(*restSubId)
745 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300746}
747
748//-------------------------------------------------------------------
749//
750//-------------------------------------------------------------------
751func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
Anssi Mannila56233b02022-01-24 13:26:42 +0200752 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300753
754 // Store successfully processed InstanceId for deletion
755 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
756 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
757
758 // Send notification to xApp that a Subscription Request has been processed.
759 resp := &models.SubscriptionResponse{
760 SubscriptionID: restSubId,
761 SubscriptionInstances: []*models.SubscriptionInstance{
762 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannila56233b02022-01-24 13:26:42 +0200763 ErrorCause: errorInfo.ErrorCause,
764 ErrorSource: errorInfo.ErrorSource,
Markku Virtanen42723e22021-06-15 10:09:23 +0300765 XappEventInstanceID: &xAppEventInstanceID},
766 },
767 }
768 // Mark REST subscription request processesd.
769 restSubscription.SetProcessed(nil)
770 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Anssi Mannila6bd57912022-06-16 09:47:23 +0300771 xapp.Logger.Debug("Sending successful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
772 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300773 c.UpdateCounter(cRestSubNotifToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300774 err := xapp.Subscription.Notify(resp, *clientEndpoint)
775 if err != nil {
776 xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
777 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300778
Anssi Mannilab43397e2021-11-16 12:06:16 +0200779 // E2 is down. Delete completely processed request safely now
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300780 if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
781 c.registry.DeleteRESTSubscription(restSubId)
782 c.RemoveRESTSubscriptionFromDb(*restSubId)
783 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300784}
785
786//-------------------------------------------------------------------
787//
788//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300789func (c *Control) RESTSubscriptionDeleteHandler(restSubId string) int {
archaggeafbf95f2021-04-14 08:54:05 +0300790
791 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000792 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300793
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300794 xapp.Logger.Debug("SubscriptionDeleteRequest from XAPP")
archaggeafbf95f2021-04-14 08:54:05 +0300795
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000796 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300797 if err != nil {
798 xapp.Logger.Error("%s", err.Error())
799 if restSubscription == nil {
800 // Subscription was not found
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300801 c.UpdateCounter(cRestSubDelRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300802 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300803 } else {
804 if restSubscription.SubReqOngoing == true {
805 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
806 xapp.Logger.Error("%s", err.Error())
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300807 c.UpdateCounter(cRestSubDelFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300808 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300809 } else if restSubscription.SubDelReqOngoing == true {
810 // Previous request for same restSubId still ongoing
Anssi Mannila3d80b722021-11-12 13:17:15 +0200811 c.UpdateCounter(cRestSubDelRespToXapp)
812 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300813 }
814 }
815 }
816
817 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
818 go func() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300819 xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
archaggeafbf95f2021-04-14 08:54:05 +0300820 for _, instanceId := range restSubscription.InstanceIds {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200821 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId, 0)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000822
archaggeafbf95f2021-04-14 08:54:05 +0300823 if err != nil {
824 xapp.Logger.Error("%s", err.Error())
archaggeafbf95f2021-04-14 08:54:05 +0300825 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300826 xapp.Logger.Debug("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000827 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
828 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300829 }
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300830 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300831 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300832 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300833 }()
834
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000835 c.UpdateCounter(cRestSubDelRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300836 return common.UnsubscribeNoContentCode
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200837}
838
archaggeafbf95f2021-04-14 08:54:05 +0300839//-------------------------------------------------------------------
840//
841//-------------------------------------------------------------------
Anssi Mannila54838ed2021-11-19 11:25:01 +0200842func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32, waitRouteCleanupTime time.Duration) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300843
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000844 var xAppEventInstanceID int64
845 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
846 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300847 xapp.Logger.Debug("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000848 restSubId, instanceId, idstring(err, nil))
849 return xAppEventInstanceID, nil
850 }
851
852 xAppEventInstanceID = int64(subs.ReqId.Id)
853 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300854 if trans == nil {
855 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
856 xapp.Logger.Error("%s", err.Error())
857 }
858 defer trans.Release()
859
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000860 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300861 if err != nil {
862 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
863 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000864 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300865 }
866 //
867 // Wake subs delete
868 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300869 subs.OngoingDelCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +0200870 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanupTime)
archaggeafbf95f2021-04-14 08:54:05 +0300871 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300872 subs.OngoingDelCount--
archaggeafbf95f2021-04-14 08:54:05 +0300873
874 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
875
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300876 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300877
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000878 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300879}
880
881//-------------------------------------------------------------------
882//
883//-------------------------------------------------------------------
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200884
Juha Hyttinen83ada002020-01-30 10:36:33 +0200885func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300886 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200887 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300888 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200889 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200890 params.Meid = subs.GetMeid()
891 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200892 params.PayloadLen = len(trans.Payload.Buf)
893 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200894 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300895 xapp.Logger.Debug("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000896 err = c.SendWithRetry(params, false, 5)
897 if err != nil {
898 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
899 }
900 return err
kalnagye0018682019-09-26 16:28:25 +0200901}
902
Juha Hyttinen83ada002020-01-30 10:36:33 +0200903func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
904
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300905 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200906 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300907 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200908 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200909 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200910 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200911 params.PayloadLen = len(trans.Payload.Buf)
912 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200913 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300914 xapp.Logger.Debug("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000915 err = c.SendWithRetry(params, false, 5)
916 if err != nil {
917 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
918 }
919 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200920}
921
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300922func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
923 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200924 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
925 xapp.Logger.Error("%s", err.Error())
926 return
927 }
928 c.CntRecvMsg++
929
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300930 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200931
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300932 // xapp-frame might use direct access to c buffer and
933 // when msg.Mbuf is freed, someone might take it into use
934 // and payload data might be invalid inside message handle function
935 //
936 // subscriptions won't load system a lot so there is no
937 // real performance hit by cloning buffer into new go byte slice
938 cPay := append(msg.Payload[:0:0], msg.Payload...)
939 msg.Payload = cPay
940 msg.PayloadLen = len(cPay)
941
Anssi Mannila90fa0212019-12-12 10:47:47 +0200942 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200943 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200944 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200945 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200946 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200947 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200948 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200949 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200950 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200951 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200952 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200953 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200954 go c.handleE2TSubscriptionDeleteFailure(msg)
gunjarastogi4378baf2023-01-18 17:51:55 +0530955 case xapp.RIC_SUB_DEL_REQUIRED:
956 go c.handleE2TSubscriptionDeleteRequired(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200957 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300958 xapp.Logger.Debug("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000959 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200960 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200961}
962
Juha Hyttinen422d0182020-01-17 13:37:05 +0200963//-------------------------------------------------------------------
964// handle from XAPP Subscription Request
965//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300966func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300967 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200968 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200969
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300970 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
971 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
972 return
973 }
974
Juha Hyttinen422d0182020-01-17 13:37:05 +0200975 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
976 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200977 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200978 return
979 }
980
Anssi Mannila4c626a22021-02-11 12:50:48 +0200981 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200982 if trans == nil {
983 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200984 return
985 }
986 defer trans.Release()
987
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200988 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200989 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
990 return
991 }
992
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300993 subs, _, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, true)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200994 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200995 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200996 return
997 }
998
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200999 c.wakeSubscriptionRequest(subs, trans)
1000}
1001
1002//-------------------------------------------------------------------
1003// Wake Subscription Request to E2node
1004//------------------------------------------------------------------
1005func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
1006
Anssi Mannila55d6fed2022-08-02 12:41:48 +03001007 e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(nil)
1008 if err != nil {
1009 xapp.Logger.Error("c.GetE2SubscriptionDirectives failure: %s", err.Error())
1010 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001011 subs.OngoingReqCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +02001012 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, waitRouteCleanup_ms)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001013 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001014 subs.OngoingReqCount--
Juha Hyttinen422d0182020-01-17 13:37:05 +02001015 if event != nil {
1016 switch themsg := event.(type) {
1017 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +02001018 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +02001019 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
1020 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +02001021 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001022 c.UpdateCounter(cSubRespToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001023 err := c.rmrSendToXapp("", subs, trans)
1024 if err != nil {
1025 xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
1026 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001027 return
1028 }
1029 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +02001030 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +02001031 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
1032 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001033 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001034 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001035 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001036 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +02001037 break
1038 }
1039 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001040 xapp.Logger.Debug("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen31797b42020-01-16 14:05:01 +02001041}
1042
Juha Hyttinen422d0182020-01-17 13:37:05 +02001043//-------------------------------------------------------------------
1044// handle from XAPP Subscription Delete Request
1045//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001046func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001047 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001048 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001049
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001050 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
1051 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
1052 return
1053 }
1054
Juha Hyttinen422d0182020-01-17 13:37:05 +02001055 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001056 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001057 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001058 return
1059 }
1060
Anssi Mannila4c626a22021-02-11 12:50:48 +02001061 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001062 if trans == nil {
1063 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001064 return
1065 }
1066 defer trans.Release()
1067
Juha Hyttinen83ada002020-01-30 10:36:33 +02001068 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001069 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001070 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
1071 return
1072 }
1073
Juha Hyttinen47942b42020-02-27 10:41:43 +02001074 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +02001075 if err != nil {
1076 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001077 return
1078 }
1079
Juha Hyttinen422d0182020-01-17 13:37:05 +02001080 //
1081 // Wake subs delete
1082 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001083 subs.OngoingDelCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +02001084 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001085 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001086 subs.OngoingDelCount--
Juha Hyttinen422d0182020-01-17 13:37:05 +02001087
Juha Hyttinen83ada002020-01-30 10:36:33 +02001088 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1089
Anssi Mannilac92b4212020-12-07 14:59:34 +02001090 if subs.NoRespToXapp == true {
1091 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001092 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
Anssi Mannilac92b4212020-12-07 14:59:34 +02001093 return
1094 }
1095
1096 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001097 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +02001098 subDelRespMsg.RequestId.Id = trans.RequestId.Id
1099 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001100 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
1101 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
1102 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001103 c.UpdateCounter(cSubDelRespToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001104 err := c.rmrSendToXapp("", subs, trans)
1105 if err != nil {
1106 xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
1107 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001108 }
1109}
1110
1111//-------------------------------------------------------------------
1112// SUBS CREATE Handling
1113//-------------------------------------------------------------------
Anssi Mannila54838ed2021-11-19 11:25:01 +02001114func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives, waitRouteCleanupTime time.Duration) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001115
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001116 var event interface{} = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001117 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +02001118 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001119 subs.WaitTransactionTurn(trans)
1120 defer subs.ReleaseTransactionTurn(trans)
1121 defer trans.Release()
1122
Juha Hyttinen83ada002020-01-30 10:36:33 +02001123 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001124
Juha Hyttinen83ada002020-01-30 10:36:33 +02001125 subRfMsg, valid := subs.GetCachedResponse()
1126 if subRfMsg == nil && valid == true {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001127 event = c.sendE2TSubscriptionRequest(subs, trans, parentTrans, e2SubscriptionDirectives)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001128 switch event.(type) {
1129 case *e2ap.E2APSubscriptionResponse:
1130 subRfMsg, valid = subs.SetCachedResponse(event, true)
1131 subs.SubRespRcvd = true
1132 case *e2ap.E2APSubscriptionFailure:
aditverm323c77e2023-09-20 09:50:47 +05301133 if subs.PolicyUpdate == false {
1134 subRfMsg, valid = subs.SetCachedResponse(event, false)
1135 } else {
1136 // In policy update case where subscription has already been created successfully in Gnb
1137 // we cannot delete subscription internally in submgr
1138 subRfMsg, valid = subs.SetCachedResponse(event, true)
1139 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001140 xapp.Logger.Debug("SUBS-SubReq: internal delete due failure event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001141 case *SubmgrRestartTestEvent:
Anssi Mannila54838ed2021-11-19 11:25:01 +02001142 // This is used to simulate that no response has been received and after restart, subscriptions are restored from db
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001143 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
Anssi Mannila54838ed2021-11-19 11:25:01 +02001144 subRfMsg, valid = subs.SetCachedResponse(event, false)
1145 parentTrans.SendEvent(subRfMsg, 0)
1146 return
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001147 case *PackSubscriptionRequestErrortEvent, *SDLWriteErrortEvent:
1148 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001149 default:
Anssi Mannila3d80b722021-11-12 13:17:15 +02001150 // Timer expiry
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001151 if subs.PolicyUpdate == false {
1152 xapp.Logger.Debug("SUBS-SubReq: internal delete due default event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001153 subRfMsg, valid = subs.SetCachedResponse(nil, false)
1154 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila54838ed2021-11-19 11:25:01 +02001155 } else {
1156 subRfMsg, valid = subs.SetCachedResponse(nil, true)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001157 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001158 }
1159 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
1160 } else {
1161 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001162 }
aditverm323c77e2023-09-20 09:50:47 +05301163 xapp.Logger.Debug("subs.PolicyUpdate: %v", subs.PolicyUpdate)
1164 xapp.Logger.Debug("subs: %v", subs)
1165
Anssi Mannila54838ed2021-11-19 11:25:01 +02001166 if valid == false {
1167 removeSubscriptionFromDb = true
1168 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001169
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001170 err := c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
1171 if err != nil {
Anssi Mannila54838ed2021-11-19 11:25:01 +02001172 valid = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001173 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001174
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001175 }
1176
Anssi Mannila27385172021-12-22 09:41:54 +02001177 // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001178 if valid == false {
Anssi Mannila55d6fed2022-08-02 12:41:48 +03001179 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001180 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001181
Juha Hyttinen83ada002020-01-30 10:36:33 +02001182 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001183}
1184
1185//-------------------------------------------------------------------
1186// SUBS DELETE Handling
1187//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +02001188
Anssi Mannila54838ed2021-11-19 11:25:01 +02001189func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp, waitRouteCleanupTime time.Duration) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001190
Juha Hyttinen83ada002020-01-30 10:36:33 +02001191 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001192 subs.WaitTransactionTurn(trans)
1193 defer subs.ReleaseTransactionTurn(trans)
1194 defer trans.Release()
1195
Juha Hyttinen83ada002020-01-30 10:36:33 +02001196 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001197
Juha Hyttinen3944a222020-01-24 11:51:46 +02001198 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001199
Juha Hyttinen3944a222020-01-24 11:51:46 +02001200 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
1201 subs.valid = false
1202 subs.mutex.Unlock()
1203 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1204 } else {
1205 subs.mutex.Unlock()
1206 }
Anssi Mannila27385172021-12-22 09:41:54 +02001207
1208 // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
Anssi Mannila54838ed2021-11-19 11:25:01 +02001209 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +02001210 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001211}
1212
1213//-------------------------------------------------------------------
1214// send to E2T Subscription Request
1215//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001216func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001217 var err error
1218 var event interface{} = nil
1219 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001220 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001221
1222 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +02001223 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001224 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001225 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
1226 if err != nil {
Anssi Mannilab43397e2021-11-16 12:06:16 +02001227 xapp.Logger.Error("SUBS-SubReq ASN1 pack error: %s", idstring(err, trans, subs, parentTrans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001228 return &PackSubscriptionRequestErrortEvent{
1229 ErrorInfo{
1230 ErrorSource: models.SubscriptionInstanceErrorSourceASN1,
1231 ErrorCause: err.Error(),
1232 },
1233 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001234 }
1235
Anssi Mannilac92b4212020-12-07 14:59:34 +02001236 // 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 +03001237 err = c.WriteSubscriptionToDb(subs)
1238 if err != nil {
1239 return &SDLWriteErrortEvent{
1240 ErrorInfo{
1241 ErrorSource: models.SubscriptionInstanceErrorSourceDBAAS,
1242 ErrorCause: err.Error(),
1243 },
1244 }
1245 }
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001246
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001247 for retries := int64(0); retries < e2SubscriptionDirectives.E2MaxTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001248 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001249 if retries == 0 {
1250 c.UpdateCounter(cSubReqToE2)
1251 } else {
1252 c.UpdateCounter(cSubReReqToE2)
1253 }
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001254 err := c.rmrSendToE2T(desc, subs, trans)
1255 if err != nil {
1256 xapp.Logger.Error("rmrSendToE2T() failed:%s", err.Error())
1257 }
1258
Anssi Mannilac92b4212020-12-07 14:59:34 +02001259 if subs.DoNotWaitSubResp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001260 event, timedOut = trans.WaitEvent(e2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001261 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001262 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001263 continue
1264 }
1265 } else {
1266 // Simulating case where subscrition request has been sent but response has not been received before restart
1267 event = &SubmgrRestartTestEvent{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001268 xapp.Logger.Debug("Restart event, DoNotWaitSubResp == true")
Juha Hyttinen422d0182020-01-17 13:37:05 +02001269 }
1270 break
1271 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001272 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001273 return event
1274}
1275
1276//-------------------------------------------------------------------
1277// send to E2T Subscription Delete Request
1278//-------------------------------------------------------------------
1279
Juha Hyttinen83ada002020-01-30 10:36:33 +02001280func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001281 var err error
1282 var event interface{}
1283 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001284 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001285
1286 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +02001287 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001288 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +02001289 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001290 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1291 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001292 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001293 return event
1294 }
1295
1296 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001297 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001298 if retries == 0 {
1299 c.UpdateCounter(cSubDelReqToE2)
1300 } else {
1301 c.UpdateCounter(cSubDelReReqToE2)
1302 }
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001303 err := c.rmrSendToE2T(desc, subs, trans)
1304 if err != nil {
1305 xapp.Logger.Error("SUBS-SubDelReq: rmrSendToE2T failure: %s", idstring(err, trans, subs, parentTrans))
1306 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001307 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
1308 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001309 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001310 continue
1311 }
1312 break
1313 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001314 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001315 return event
1316}
1317
1318//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +02001319// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001320//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001321func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001322 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001323 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +03001324
Juha Hyttinen422d0182020-01-17 13:37:05 +02001325 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
1326 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001327 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001328 return
1329 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001330 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +02001331 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001332 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001333 return
Juha Hyttinen31797b42020-01-16 14:05:01 +02001334 }
1335 trans := subs.GetTransaction()
1336 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001337 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001338 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001339 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001340 }
Anssi Mannila54838ed2021-11-19 11:25:01 +02001341 xapp.Logger.Debug("SUBS-SubResp: Sending event, trans= %v", trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001342 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
1343 if sendOk == false {
1344 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001345 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001346 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001347 return
kalnagy45114752019-06-18 14:40:39 +02001348}
1349
Juha Hyttinen422d0182020-01-17 13:37:05 +02001350//-------------------------------------------------------------------
1351// handle from E2T Subscription Failure
1352//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001353func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001354 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001355 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001356 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001357 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001358 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001359 return
1360 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001361 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001362 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001363 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001364 return
1365 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001366 trans := subs.GetTransaction()
1367 if trans == nil {
1368 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001369 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001370 return
1371 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001372 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1373 if sendOk == false {
1374 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001375 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001376 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001377 return
1378}
1379
Juha Hyttinen422d0182020-01-17 13:37:05 +02001380//-------------------------------------------------------------------
1381// handle from E2T Subscription Delete Response
1382//-------------------------------------------------------------------
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001383func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001384 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001385 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001386 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001387 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001388 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001389 return
1390 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001391 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001392 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001393 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001394 return
1395 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001396 trans := subs.GetTransaction()
1397 if trans == nil {
1398 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001399 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001400 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001401 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001402 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1403 if sendOk == false {
1404 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001405 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001406 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001407 return
1408}
1409
Juha Hyttinen422d0182020-01-17 13:37:05 +02001410//-------------------------------------------------------------------
1411// handle from E2T Subscription Delete Failure
1412//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001413func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001414 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001415 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001416 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001417 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001418 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001419 return
1420 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001421 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001422 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001423 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001424 return
1425 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001426 trans := subs.GetTransaction()
1427 if trans == nil {
1428 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001429 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001430 return
1431 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001432 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1433 if sendOk == false {
1434 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001435 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001436 }
kalnagy93cc3e22019-09-19 11:29:29 +02001437 return
1438}
1439
Juha Hyttinen422d0182020-01-17 13:37:05 +02001440//-------------------------------------------------------------------
1441//
1442//-------------------------------------------------------------------
1443func typeofSubsMessage(v interface{}) string {
1444 if v == nil {
1445 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001446 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001447 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001448 //case *e2ap.E2APSubscriptionRequest:
1449 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001450 case *e2ap.E2APSubscriptionResponse:
1451 return "SubResp"
1452 case *e2ap.E2APSubscriptionFailure:
1453 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001454 //case *e2ap.E2APSubscriptionDeleteRequest:
1455 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001456 case *e2ap.E2APSubscriptionDeleteResponse:
1457 return "SubDelResp"
1458 case *e2ap.E2APSubscriptionDeleteFailure:
1459 return "SubDelFail"
1460 default:
1461 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001462 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001463}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001464
1465//-------------------------------------------------------------------
1466//
1467//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001468func (c *Control) WriteSubscriptionToDb(subs *Subscription) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001469 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1470 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1471 if err != nil {
1472 xapp.Logger.Error("%v", err)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001473 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001474 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001475 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001476}
1477
1478//-------------------------------------------------------------------
1479//
1480//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001481func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001482
1483 if removeSubscriptionFromDb == true {
1484 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1485 c.RemoveSubscriptionFromDb(subs)
1486 } else {
1487 // Update is needed for successful response and merge case here
1488 if subs.RetryFromXapp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001489 err := c.WriteSubscriptionToDb(subs)
1490 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001491 }
1492 }
1493 subs.RetryFromXapp = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001494 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001495}
1496
1497//-------------------------------------------------------------------
1498//
1499//-------------------------------------------------------------------
1500func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1501 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1502 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1503 if err != nil {
1504 xapp.Logger.Error("%v", err)
1505 }
1506}
1507
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001508//-------------------------------------------------------------------
1509//
1510//-------------------------------------------------------------------
1511func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1512 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1513 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1514 if err != nil {
1515 xapp.Logger.Error("%v", err)
1516 }
1517}
1518
1519//-------------------------------------------------------------------
1520//
1521//-------------------------------------------------------------------
1522func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1523
1524 if removeRestSubscriptionFromDb == true {
1525 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1526 c.RemoveRESTSubscriptionFromDb(restSubId)
1527 } else {
1528 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1529 }
1530}
1531
1532//-------------------------------------------------------------------
1533//
1534//-------------------------------------------------------------------
1535func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1536 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1537 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1538 if err != nil {
1539 xapp.Logger.Error("%v", err)
1540 }
1541}
1542
gunjarastogi4378baf2023-01-18 17:51:55 +05301543func (c *Control) SendSubscriptionDeleteReq(subs *Subscription, e2SubsDelRequired bool) {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001544
Anssi Mannila54838ed2021-11-19 11:25:01 +02001545 if c.UTTesting == true {
1546 // Reqistry mutex is not locked after real restart but it can be when restart is simulated in unit tests
1547 c.registry.mutex = new(sync.Mutex)
1548 }
1549
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001550 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001551 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1552
1553 // Send delete for every endpoint in the subscription
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001554 if subs.PolicyUpdate == false {
1555 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1556 subDelReqMsg.RequestId = subs.GetReqId().RequestId
1557 subDelReqMsg.RequestId.Id = ricRequestorId
1558 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1559 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1560 if err != nil {
1561 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1562 return
1563 }
1564 for _, endPoint := range subs.EpList.Endpoints {
1565 params := &xapp.RMRParams{}
1566 params.Mtype = mType
1567 params.SubId = int(subs.GetReqId().InstanceId)
1568 params.Xid = ""
1569 params.Meid = subs.Meid
1570 params.Src = endPoint.String()
1571 params.PayloadLen = len(payload.Buf)
1572 params.Payload = payload.Buf
1573 params.Mbuf = nil
1574 subs.DeleteFromDb = true
gunjarastogi4378baf2023-01-18 17:51:55 +05301575 if !e2SubsDelRequired {
1576 c.handleXAPPSubscriptionDeleteRequest(params)
1577 } else {
1578 c.SendSubscriptionDeleteReqToE2T(subs, params)
1579 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001580 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001581 }
1582}
Anssi Mannilade457a12021-06-02 16:19:48 +03001583
1584func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1585
1586 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001587
Markku Virtanen2b512b62021-07-30 12:04:00 +00001588 if p == nil {
1589 return
1590 }
1591
Markku Virtanen987f8b12021-06-10 15:05:02 +03001592 if p.SubscriptionID != "" {
1593 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1594 } else {
1595 fmt.Println(" SubscriptionID = ''")
1596 }
1597
Anssi Mannilade457a12021-06-02 16:19:48 +03001598 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1599
1600 if p.ClientEndpoint.HTTPPort != nil {
1601 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1602 } else {
1603 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1604 }
1605
1606 if p.ClientEndpoint.RMRPort != nil {
1607 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1608 } else {
1609 fmt.Println(" ClientEndpoint.RMRPort = nil")
1610 }
1611
1612 if p.Meid != nil {
1613 fmt.Printf(" Meid = %s\n", *p.Meid)
1614 } else {
1615 fmt.Println(" Meid = nil")
1616 }
1617
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001618 if p.E2SubscriptionDirectives == nil {
1619 fmt.Println(" E2SubscriptionDirectives = nil")
1620 } else {
1621 fmt.Println(" E2SubscriptionDirectives")
1622 if p.E2SubscriptionDirectives.E2RetryCount == nil {
1623 fmt.Println(" E2RetryCount == nil")
1624 } else {
1625 fmt.Printf(" E2RetryCount = %v\n", *p.E2SubscriptionDirectives.E2RetryCount)
1626 }
1627 fmt.Printf(" E2TimeoutTimerValue = %v\n", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilacb7dd492021-10-01 11:10:26 +03001628 fmt.Printf(" RMRRoutingNeeded = %v\n", p.E2SubscriptionDirectives.RMRRoutingNeeded)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001629 }
Anssi Mannilade457a12021-06-02 16:19:48 +03001630 for _, subscriptionDetail := range p.SubscriptionDetails {
1631 if p.RANFunctionID != nil {
1632 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1633 } else {
1634 fmt.Println(" RANFunctionID = nil")
1635 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001636 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001637 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001638
1639 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1640 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1641 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001642 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001643
Anssi Mannilade457a12021-06-02 16:19:48 +03001644 if actionToBeSetup.SubsequentAction != nil {
1645 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1646 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1647 } else {
1648 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1649 }
1650 }
1651 }
1652}
gunjarastogi4378baf2023-01-18 17:51:55 +05301653
1654//-------------------------------------------------------------------
1655// handle from E2T Subscription Delete Required
1656//-------------------------------------------------------------------
1657func (c *Control) handleE2TSubscriptionDeleteRequired(params *xapp.RMRParams) {
1658 xapp.Logger.Info("MSG from E2T: %s", params.String())
1659 c.UpdateCounter(cSubDelRequFromE2)
1660 subsDelRequMsg, err := c.e2ap.UnpackSubscriptionDeleteRequired(params.Payload)
1661 if err != nil {
1662 xapp.Logger.Error("MSG-SubDelRequired: %s", idstring(err, params))
1663 //c.sendE2TErrorIndication(nil)
1664 return
1665 }
1666 var subscriptions = map[string][]e2ap.E2APSubscriptionDeleteRequired{}
1667 var subDB = []*Subscription{}
1668 for _, subsTobeRemove := range subsDelRequMsg.E2APSubscriptionDeleteRequiredRequests {
1669 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subsTobeRemove.RequestId.InstanceId})
1670 if err != nil {
1671 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
1672 continue
1673 }
1674 // Check if Delete Subscription Already triggered
1675 if subs.OngoingDelCount > 0 {
1676 continue
1677 }
1678 subDB = append(subDB, subs)
1679 for _, endpoint := range subs.EpList.Endpoints {
1680 subscriptions[endpoint.Addr] = append(subscriptions[endpoint.Addr], subsTobeRemove)
1681 }
1682 // Sending Subscription Delete Request to E2T
1683 // c.SendSubscriptionDeleteReq(subs, true)
1684 }
1685 for _, subsTobeRemove := range subDB {
1686 // Sending Subscription Delete Request to E2T
1687 c.SendSubscriptionDeleteReq(subsTobeRemove, true)
1688 }
1689}
1690
1691//-----------------------------------------------------------------
1692// Initiate RIC Subscription Delete Request after receiving
1693// RIC Subscription Delete Required from E2T
1694//-----------------------------------------------------------------
1695func (c *Control) SendSubscriptionDeleteReqToE2T(subs *Subscription, params *xapp.RMRParams) {
1696 xapp.Logger.Debug("MSG TO E2T: %s", params.String())
1697 c.UpdateCounter(cSubDelReqToE2)
1698
1699 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
1700 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
1701 return
1702 }
1703
1704 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subs.ReqId.RequestId, params.Meid)
1705 if trans == nil {
1706 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
1707 return
1708 }
1709 defer trans.Release()
1710
1711 err := c.tracker.Track(trans)
1712 if err != nil {
1713 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
1714 return
1715 }
1716
1717 //
1718 // Wake subs delete
1719 //
1720 subs.OngoingDelCount++
1721 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
1722 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
1723 subs.OngoingDelCount--
1724
1725 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1726
1727 if subs.NoRespToXapp == true {
1728 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
1729 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
1730 return
1731 }
1732}