blob: b4667e0e9d166665b3b20b7e9eec2586d054dc5f [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
Himanshu Puorhit84662c42023-08-29 15:46:18 +053075var e2IEOrderCheckValue uint8
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020076
kalnagy45114752019-06-18 14:40:39 +020077type Control struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030078 *xapp.RMRClient
Anssi Mannila3cdd2e02021-10-14 12:35:41 +030079 e2ap *E2ap
80 registry *Registry
81 tracker *Tracker
82 restDuplicateCtrl *DuplicateCtrl
Anssi Mannilac7da4ee2021-10-22 09:52:02 +030083 e2IfState *E2IfState
84 e2IfStateDb XappRnibInterface
Anssi Mannila3cdd2e02021-10-14 12:35:41 +030085 e2SubsDb Sdlnterface
86 restSubsDb Sdlnterface
87 CntRecvMsg uint64
88 ResetTestFlag bool
89 Counters map[string]xapp.Counter
90 LoggerLevel int
91 UTTesting bool
kalnagy93cc3e22019-09-19 11:29:29 +020092}
93
94type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020095 PlmnID string
96 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020097 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020098}
99
Anssi Mannilac92b4212020-12-07 14:59:34 +0200100type SubmgrRestartTestEvent struct{}
101type SubmgrRestartUpEvent struct{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300102type PackSubscriptionRequestErrortEvent struct {
103 ErrorInfo ErrorInfo
104}
105
106func (p *PackSubscriptionRequestErrortEvent) SetEvent(errorInfo *ErrorInfo) {
107 p.ErrorInfo = *errorInfo
108}
109
110type SDLWriteErrortEvent struct {
111 ErrorInfo ErrorInfo
112}
113
114func (s *SDLWriteErrortEvent) SetEvent(errorInfo *ErrorInfo) {
115 s.ErrorInfo = *errorInfo
116}
Anssi Mannilac92b4212020-12-07 14:59:34 +0200117
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000118func init() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300119 xapp.Logger.Debug("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000120 viper.AutomaticEnv()
121 viper.SetEnvPrefix("submgr")
122 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +0200123}
124
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200125func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200126
Anssi Mannilac92b4212020-12-07 14:59:34 +0200127 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
128 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
129
130 registry := new(Registry)
131 registry.Initialize()
132 registry.rtmgrClient = &rtmgrClient
133
134 tracker := new(Tracker)
135 tracker.Init()
136
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300137 restDuplicateCtrl := new(DuplicateCtrl)
138 restDuplicateCtrl.Init()
139
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300140 e2IfState := new(E2IfState)
141
Anssi Mannilac92b4212020-12-07 14:59:34 +0200142 c := &Control{e2ap: new(E2ap),
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300143 registry: registry,
144 tracker: tracker,
145 restDuplicateCtrl: restDuplicateCtrl,
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300146 e2IfState: e2IfState,
147 e2IfStateDb: CreateXappRnibIfInstance(),
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300148 e2SubsDb: CreateSdl(),
149 restSubsDb: CreateRESTSdl(),
150 Counters: xapp.Metric.RegisterCounterGroup(GetMetricsOpts(), "SUBMGR"),
Anssi Mannilae1af87f2021-11-10 13:10:54 +0200151 LoggerLevel: 1,
Anssi Mannilac92b4212020-12-07 14:59:34 +0200152 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300153
154 e2IfState.Init(c)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200155 c.ReadConfigParameters("")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200156
157 // Register REST handler for testing support
Anssi Mannila92c38552021-12-29 09:59:24 +0200158 xapp.Resource.InjectRoute("/ric/v1/symptomdata", c.SymptomDataHandler, "GET")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200159 xapp.Resource.InjectRoute("/ric/v1/test/{testId}", c.TestRestHandler, "POST")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300160 xapp.Resource.InjectRoute("/ric/v1/restsubscriptions", c.GetAllRestSubscriptions, "GET")
Anssi Mannila92c38552021-12-29 09:59:24 +0200161
162 xapp.Resource.InjectRoute("/ric/v1/get_all_e2nodes", c.GetAllE2Nodes, "GET")
163 xapp.Resource.InjectRoute("/ric/v1/get_e2node_rest_subscriptions/{ranName}", c.GetAllE2NodeRestSubscriptions, "GET")
164
165 xapp.Resource.InjectRoute("/ric/v1/get_all_xapps", c.GetAllXapps, "GET")
166 xapp.Resource.InjectRoute("/ric/v1/get_xapp_rest_restsubscriptions/{xappServiceName}", c.GetAllXappRestSubscriptions, "GET")
167 xapp.Resource.InjectRoute("/ric/v1/get_e2subscriptions/{restId}", c.GetE2Subscriptions, "GET")
168
Anssi Mannilad13ba632022-01-04 14:22:26 +0200169 xapp.Resource.InjectRoute("/ric/v1/delete_all_e2node_subscriptions/{ranName}", c.DeleteAllE2nodeSubscriptions, "DELETE")
170 xapp.Resource.InjectRoute("/ric/v1/delete_all_xapp_subscriptions/{xappServiceName}", c.DeleteAllXappSubscriptions, "DELETE")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200171
Anssi Mannilae1af87f2021-11-10 13:10:54 +0200172 if readSubsFromDb == "true" {
173 // Read subscriptions from db
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300174 err := c.ReadE2Subscriptions()
175 if err != nil {
176 xapp.Logger.Error("ReadE2Subscriptions() failed %s", err.Error())
177 }
178 err = c.ReadRESTSubscriptions()
179 if err != nil {
180 xapp.Logger.Error("ReadRESTSubscriptions() failed %s", err.Error())
181 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200182 }
183
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300184 go func() {
185 err := xapp.Subscription.Listen(c.RESTSubscriptionHandler, c.RESTQueryHandler, c.RESTSubscriptionDeleteHandler)
186 if err != nil {
187 xapp.Logger.Error("xapp.Subscription.Listen failure: %s", err.Error())
188 }
189 }()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200190 return c
191}
192
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000193func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300194 subscriptions, err := c.registry.QueryHandler()
195 if err != nil {
196 xapp.Logger.Error("QueryHandler() failed %s", err.Error())
197 }
198
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000199 xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
200}
201
Anssi Mannilac92b4212020-12-07 14:59:34 +0200202//-------------------------------------------------------------------
203//
204//-------------------------------------------------------------------
Anssi Mannila92c38552021-12-29 09:59:24 +0200205func (c *Control) RESTQueryHandler() (models.SubscriptionList, error) {
206 xapp.Logger.Debug("RESTQueryHandler() called")
207
208 c.CntRecvMsg++
209
210 return c.registry.QueryHandler()
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300211}
212
213//-------------------------------------------------------------------
214//
215//-------------------------------------------------------------------
216func (c *Control) ReadE2Subscriptions() error {
217 var err error
218 var subIds []uint32
219 var register map[uint32]*Subscription
220 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300221 xapp.Logger.Debug("Reading E2 subscriptions from db")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300222 subIds, register, err = c.ReadAllSubscriptionsFromSdl()
223 if err != nil {
224 xapp.Logger.Error("%v", err)
225 <-time.After(1 * time.Second)
226 } else {
227 c.registry.subIds = subIds
228 c.registry.register = register
Anssi Mannila316b8d22021-12-21 09:43:59 +0200229 go c.HandleUncompletedSubscriptions(register)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300230 return nil
231 }
232 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300233 xapp.Logger.Debug("Continuing without retring")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300234 return err
235}
236
237//-------------------------------------------------------------------
238//
239//-------------------------------------------------------------------
240func (c *Control) ReadRESTSubscriptions() error {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200241
242 xapp.Logger.Debug("ReadRESTSubscriptions()")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300243 var err error
244 var restSubscriptions map[string]*RESTSubscription
245 for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300246 xapp.Logger.Debug("Reading REST subscriptions from db")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300247 restSubscriptions, err = c.ReadAllRESTSubscriptionsFromSdl()
248 if err != nil {
249 xapp.Logger.Error("%v", err)
250 <-time.After(1 * time.Second)
251 } else {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200252 // Fix REST subscriptions ongoing status after restart
253 for restSubId, restSubscription := range restSubscriptions {
254 restSubscription.SubReqOngoing = false
255 restSubscription.SubDelReqOngoing = false
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300256 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubscription)
257 if err != nil {
258 xapp.Logger.Error("WriteRESTSubscriptionToSdl() failed:%s", err.Error())
259 }
Anssi Mannila54838ed2021-11-19 11:25:01 +0200260 }
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300261 c.registry.restSubscriptions = restSubscriptions
262 return nil
263 }
264 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300265 xapp.Logger.Debug("Continuing without retring")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300266 return err
267}
268
269//-------------------------------------------------------------------
270//
271//-------------------------------------------------------------------
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200272func (c *Control) ReadConfigParameters(f string) {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200273
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300274 xapp.Logger.Debug("ReadConfigParameters")
275
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300276 c.LoggerLevel = int(xapp.Logger.GetLevel())
Anssi Mannila3d80b722021-11-12 13:17:15 +0200277 xapp.Logger.Info("LoggerLevel = %v", c.LoggerLevel)
Anssi Mannilae1af87f2021-11-10 13:10:54 +0200278 c.e2ap.SetASN1DebugPrintStatus(c.LoggerLevel)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300279
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200280 // viper.GetDuration returns nanoseconds
281 e2tSubReqTimeout = viper.GetDuration("controls.e2tSubReqTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200282 if e2tSubReqTimeout == 0 {
283 e2tSubReqTimeout = 2000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300284 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tSubReqTimeout")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200285 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300286 xapp.Logger.Debug("e2tSubReqTimeout= %v", e2tSubReqTimeout)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300287
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200288 e2tSubDelReqTime = viper.GetDuration("controls.e2tSubDelReqTime_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200289 if e2tSubDelReqTime == 0 {
290 e2tSubDelReqTime = 2000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300291 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tSubDelReqTime")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200292 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300293 xapp.Logger.Debug("e2tSubDelReqTime= %v", e2tSubDelReqTime)
294
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200295 e2tRecvMsgTimeout = viper.GetDuration("controls.e2tRecvMsgTimeout_ms") * 1000000
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200296 if e2tRecvMsgTimeout == 0 {
297 e2tRecvMsgTimeout = 2000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300298 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tRecvMsgTimeout")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200299 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300300 xapp.Logger.Debug("e2tRecvMsgTimeout= %v", e2tRecvMsgTimeout)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000301
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200302 e2tMaxSubReqTryCount = viper.GetUint64("controls.e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200303 if e2tMaxSubReqTryCount == 0 {
304 e2tMaxSubReqTryCount = 1
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300305 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tMaxSubReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200306 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300307 xapp.Logger.Debug("e2tMaxSubReqTryCount= %v", e2tMaxSubReqTryCount)
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000308
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200309 e2tMaxSubDelReqTryCount = viper.GetUint64("controls.e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200310 if e2tMaxSubDelReqTryCount == 0 {
311 e2tMaxSubDelReqTryCount = 1
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300312 xapp.Logger.Debug("WARNING: Using hard coded default value for e2tMaxSubDelReqTryCount")
Juha Hyttinen57140bc2020-12-11 13:03:06 +0200313 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300314 xapp.Logger.Debug("e2tMaxSubDelReqTryCount= %v", e2tMaxSubDelReqTryCount)
315
316 checkE2State = viper.GetString("controls.checkE2State")
317 if checkE2State == "" {
318 checkE2State = "true"
319 xapp.Logger.Debug("WARNING: Using hard coded default value for checkE2State")
320 }
321 xapp.Logger.Debug("checkE2State= %v", checkE2State)
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200322
Anssi Mannilac92b4212020-12-07 14:59:34 +0200323 readSubsFromDb = viper.GetString("controls.readSubsFromDb")
324 if readSubsFromDb == "" {
325 readSubsFromDb = "true"
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300326 xapp.Logger.Debug("WARNING: Using hard coded default value for readSubsFromDb")
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200327 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300328 xapp.Logger.Debug("readSubsFromDb= %v", readSubsFromDb)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300329
330 dbTryCount = viper.GetInt("controls.dbTryCount")
331 if dbTryCount == 0 {
332 dbTryCount = 200
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300333 xapp.Logger.Debug("WARNING: Using hard coded default value for dbTryCount")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300334 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300335 xapp.Logger.Debug("dbTryCount= %v", dbTryCount)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300336
337 dbRetryForever = viper.GetString("controls.dbRetryForever")
338 if dbRetryForever == "" {
339 dbRetryForever = "true"
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300340 xapp.Logger.Debug("WARNING: Using hard coded default value for dbRetryForever")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300341 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300342 xapp.Logger.Debug("dbRetryForever= %v", dbRetryForever)
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300343
344 // Internal cfg parameter, used to define a wait time for RMR route clean-up. None default
345 // value 100ms used currently only in unittests.
346 waitRouteCleanup_ms = viper.GetDuration("controls.waitRouteCleanup_ms") * 1000000
347 if waitRouteCleanup_ms == 0 {
348 waitRouteCleanup_ms = 5000 * 1000000
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300349 xapp.Logger.Debug("WARNING: Using hard coded default value for waitRouteCleanup_ms")
Anssi Mannilab73e7cd2021-08-03 11:57:11 +0300350 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300351 xapp.Logger.Debug("waitRouteCleanup= %v", waitRouteCleanup_ms)
Himanshu Puorhit84662c42023-08-29 15:46:18 +0530352
353 viper.SetDefault("controls.checkE2IEOrder", 1)
354 e2IEOrderCheckValue = uint8(viper.GetUint("controls.checkE2IEOrder"))
355 c.e2ap.SetE2IEOrderCheck(e2IEOrderCheckValue)
356 xapp.Logger.Debug("e2IEOrderCheck= %v", e2IEOrderCheckValue)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200357}
358
359//-------------------------------------------------------------------
360//
361//-------------------------------------------------------------------
362func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscription) {
363
364 xapp.Logger.Debug("HandleUncompletedSubscriptions. len(register) = %v", len(register))
365 for subId, subs := range register {
366 if subs.SubRespRcvd == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300367 // If policy subscription has already been made successfully unsuccessful update should not be deleted.
368 if subs.PolicyUpdate == false {
369 subs.NoRespToXapp = true
370 xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
gunjarastogi4378baf2023-01-18 17:51:55 +0530371 c.SendSubscriptionDeleteReq(subs, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300372 }
Anssi Mannilac92b4212020-12-07 14:59:34 +0200373 }
374 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200375}
376
377func (c *Control) ReadyCB(data interface{}) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300378 if c.RMRClient == nil {
379 c.RMRClient = xapp.Rmr
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200380 }
kalnagy45114752019-06-18 14:40:39 +0200381}
382
383func (c *Control) Run() {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200384 xapp.SetReadyCB(c.ReadyCB, nil)
Anssi Mannilaf6b78042021-02-18 12:36:59 +0200385 xapp.AddConfigChangeListener(c.ReadConfigParameters)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000386 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200387}
388
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200389//-------------------------------------------------------------------
390//
391//-------------------------------------------------------------------
Anssi Mannila92c38552021-12-29 09:59:24 +0200392func (c *Control) GetOrCreateRestSubscription(p *models.SubscriptionParams, md5sum string, xAppRmrEndpoint string, xAppServiceName string) (*RESTSubscription, string, error) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300393
394 var restSubId string
395 var restSubscription *RESTSubscription
396 var err error
397
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300398 prevRestSubsId, exists := c.restDuplicateCtrl.GetLastKnownRestSubsIdBasedOnMd5sum(md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300399 if p.SubscriptionID == "" {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300400 // Subscription does not contain REST subscription Id
Markku Virtanen42723e22021-06-15 10:09:23 +0300401 if exists {
402 restSubscription, err = c.registry.GetRESTSubscription(prevRestSubsId, false)
403 if restSubscription != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300404 // Subscription not found
Markku Virtanen42723e22021-06-15 10:09:23 +0300405 restSubId = prevRestSubsId
406 if err == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300407 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 +0300408 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300409 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 +0300410 }
411 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300412 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 +0300413 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300414 }
415 }
416
417 if restSubscription == nil {
418 restSubId = ksuid.New().String()
Anssi Mannila92c38552021-12-29 09:59:24 +0200419 restSubscription = c.registry.CreateRESTSubscription(&restSubId, &xAppServiceName, &xAppRmrEndpoint, p.Meid)
Markku Virtanen42723e22021-06-15 10:09:23 +0300420 }
421 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300422 // Subscription contains REST subscription Id
Markku Virtanen42723e22021-06-15 10:09:23 +0300423 restSubId = p.SubscriptionID
424
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300425 xapp.Logger.Debug("RestSubscription ID %s provided via REST request", restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300426 restSubscription, err = c.registry.GetRESTSubscription(restSubId, false)
427 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300428 // Subscription with id in REST request does not exist
Markku Virtanen42723e22021-06-15 10:09:23 +0300429 xapp.Logger.Error("%s", err.Error())
430 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300431 return nil, "", err
Markku Virtanen42723e22021-06-15 10:09:23 +0300432 }
433
434 if !exists {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300435 xapp.Logger.Debug("Existing restSubscription found for ID %s, new request based on md5sum", restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300436 } else {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300437 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 +0300438 }
439 }
440
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300441 return restSubscription, restSubId, nil
Markku Virtanen42723e22021-06-15 10:09:23 +0300442}
443
444//-------------------------------------------------------------------
445//
446//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300447func (c *Control) RESTSubscriptionHandler(params interface{}) (*models.SubscriptionResponse, int) {
archaggeafbf95f2021-04-14 08:54:05 +0300448
Anssi Mannila316d8a12021-06-02 11:08:54 +0300449 c.CntRecvMsg++
450 c.UpdateCounter(cRestSubReqFromXapp)
451
archaggeafbf95f2021-04-14 08:54:05 +0300452 subResp := models.SubscriptionResponse{}
archaggeafbf95f2021-04-14 08:54:05 +0300453 p := params.(*models.SubscriptionParams)
454
Anssi Mannilade457a12021-06-02 16:19:48 +0300455 if c.LoggerLevel > 2 {
456 c.PrintRESTSubscriptionRequest(p)
457 }
458
naman.gupta7dbc4ad2023-05-09 17:24:05 +0530459 if c.e2IfState.IsE2ConnectionUp(p.Meid) == false || c.e2IfState.IsE2ConnectionUnderReset(p.Meid) == true {
460 if c.e2IfState.IsE2ConnectionUp(p.Meid) == false {
461 xapp.Logger.Error("No E2 connection for ranName %v", *p.Meid)
462 } else if c.e2IfState.IsE2ConnectionUnderReset(p.Meid) == true {
463 xapp.Logger.Error("E2 Node for ranName %v UNDER RESET", *p.Meid)
464 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300465 c.UpdateCounter(cRestReqRejDueE2Down)
466 return nil, common.SubscribeServiceUnavailableCode
467 }
468
archaggeafbf95f2021-04-14 08:54:05 +0300469 if p.ClientEndpoint == nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300470 err := fmt.Errorf("ClientEndpoint == nil")
471 xapp.Logger.Error("%v", err)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300472 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300473 return nil, common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300474 }
475
Anssi Mannila6bd57912022-06-16 09:47:23 +0300476 e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(p)
477 if err != nil {
478 xapp.Logger.Error("%s", err)
479 c.UpdateCounter(cRestSubFailToXapp)
480 return nil, common.SubscribeBadRequestCode
481 }
archaggeafbf95f2021-04-14 08:54:05 +0300482 _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
483 if err != nil {
484 xapp.Logger.Error("%s", err.Error())
Anssi Mannila316d8a12021-06-02 11:08:54 +0300485 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300486 return nil, common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300487 }
488
Markku Virtanen42723e22021-06-15 10:09:23 +0300489 md5sum, err := CalculateRequestMd5sum(params)
490 if err != nil {
491 xapp.Logger.Error("Failed to generate md5sum from incoming request - %s", err.Error())
492 }
493
Anssi Mannila92c38552021-12-29 09:59:24 +0200494 restSubscription, restSubId, err := c.GetOrCreateRestSubscription(p, md5sum, xAppRmrEndpoint, p.ClientEndpoint.Host)
Markku Virtanen42723e22021-06-15 10:09:23 +0300495 if err != nil {
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300496 xapp.Logger.Error("Subscription with id in REST request does not exist")
497 return nil, common.SubscribeNotFoundCode
archaggeafbf95f2021-04-14 08:54:05 +0300498 }
499
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000500 subResp.SubscriptionID = &restSubId
archaggeafbf95f2021-04-14 08:54:05 +0300501 subReqList := e2ap.SubscriptionRequestList{}
502 err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
503 if err != nil {
504 xapp.Logger.Error("%s", err.Error())
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300505 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300506 c.registry.DeleteRESTSubscription(&restSubId)
Anssi Mannila316d8a12021-06-02 11:08:54 +0300507 c.UpdateCounter(cRestSubFailToXapp)
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300508 return nil, common.SubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300509 }
510
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300511 duplicate := c.restDuplicateCtrl.IsDuplicateToOngoingTransaction(restSubId, md5sum)
Markku Virtanen42723e22021-06-15 10:09:23 +0300512 if duplicate {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300513 err := fmt.Errorf("Retransmission blocker direct ACK for request of restSubsId %s restSubId MD5sum %s as retransmission", restSubId, md5sum)
514 xapp.Logger.Debug("%s", err)
Anssi Mannilab43397e2021-11-16 12:06:16 +0200515 c.registry.DeleteRESTSubscription(&restSubId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300516 c.UpdateCounter(cRestSubRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300517 return &subResp, common.SubscribeCreatedCode
Markku Virtanenb642a192021-06-09 09:08:14 +0000518 }
519
Markku Virtanencd73ecc2021-06-23 06:16:01 +0000520 c.WriteRESTSubscriptionToDb(restSubId, restSubscription)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300521 go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint, md5sum, e2SubscriptionDirectives)
archaggeafbf95f2021-04-14 08:54:05 +0300522
Anssi Mannila316d8a12021-06-02 11:08:54 +0300523 c.UpdateCounter(cRestSubRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300524 return &subResp, common.SubscribeCreatedCode
525}
526
527//-------------------------------------------------------------------
528//
529//-------------------------------------------------------------------
530func (c *Control) GetE2SubscriptionDirectives(p *models.SubscriptionParams) (*E2SubscriptionDirectives, error) {
531
532 e2SubscriptionDirectives := &E2SubscriptionDirectives{}
533 if p == nil || p.E2SubscriptionDirectives == nil {
534 e2SubscriptionDirectives.E2TimeoutTimerValue = e2tSubReqTimeout
535 e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
536 e2SubscriptionDirectives.CreateRMRRoute = true
537 xapp.Logger.Debug("p == nil || p.E2SubscriptionDirectives == nil. Using default values for E2TimeoutTimerValue = %v and E2RetryCount = %v RMRRoutingNeeded = true", e2tSubReqTimeout, e2tMaxSubReqTryCount)
538 } else {
539 if p.E2SubscriptionDirectives.E2TimeoutTimerValue >= 1 && p.E2SubscriptionDirectives.E2TimeoutTimerValue <= 10 {
540 e2SubscriptionDirectives.E2TimeoutTimerValue = time.Duration(p.E2SubscriptionDirectives.E2TimeoutTimerValue) * 1000000000 // Duration type cast returns nano seconds
541 } else {
542 return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2TimeoutTimerValue out of range (1-10 seconds): %v", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
543 }
544 if p.E2SubscriptionDirectives.E2RetryCount == nil {
545 xapp.Logger.Error("p.E2SubscriptionDirectives.E2RetryCount == nil. Using default value")
546 e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
547 } else {
548 if *p.E2SubscriptionDirectives.E2RetryCount >= 0 && *p.E2SubscriptionDirectives.E2RetryCount <= 10 {
549 e2SubscriptionDirectives.E2MaxTryCount = *p.E2SubscriptionDirectives.E2RetryCount + 1 // E2MaxTryCount = First sending plus two retries
550 } else {
551 return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2RetryCount out of range (0-10): %v", *p.E2SubscriptionDirectives.E2RetryCount)
552 }
553 }
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300554 e2SubscriptionDirectives.CreateRMRRoute = p.E2SubscriptionDirectives.RMRRoutingNeeded
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300555 }
556 xapp.Logger.Debug("e2SubscriptionDirectives.E2TimeoutTimerValue: %v", e2SubscriptionDirectives.E2TimeoutTimerValue)
557 xapp.Logger.Debug("e2SubscriptionDirectives.E2MaxTryCount: %v", e2SubscriptionDirectives.E2MaxTryCount)
558 xapp.Logger.Debug("e2SubscriptionDirectives.CreateRMRRoute: %v", e2SubscriptionDirectives.CreateRMRRoute)
559 return e2SubscriptionDirectives, nil
560}
561
562//-------------------------------------------------------------------
563//
564//-------------------------------------------------------------------
archaggeafbf95f2021-04-14 08:54:05 +0300565
566func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300567 clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string, e2SubscriptionDirectives *E2SubscriptionDirectives) {
archaggeafbf95f2021-04-14 08:54:05 +0300568
Anssi Mannila08c8fbd2021-10-05 12:47:41 +0300569 c.SubscriptionProcessingStartDelay()
Anssi Mannila54838ed2021-11-19 11:25:01 +0200570 xapp.Logger.Debug("E2 SubscriptionRequest count = %v ", len(subReqList.E2APSubscriptionRequests))
archaggeafbf95f2021-04-14 08:54:05 +0300571
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000572 var xAppEventInstanceID int64
573 var e2EventInstanceID int64
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300574 errorInfo := &ErrorInfo{}
Markku Virtanenb642a192021-06-09 09:08:14 +0000575
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300576 defer c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(*restSubId, md5sum)
Markku Virtanenb642a192021-06-09 09:08:14 +0000577
archaggeafbf95f2021-04-14 08:54:05 +0300578 for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
579 subReqMsg := subReqList.E2APSubscriptionRequests[index]
Markku Virtanenb642a192021-06-09 09:08:14 +0000580 xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
archaggeafbf95f2021-04-14 08:54:05 +0300581
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000582 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300583 if trans == nil {
Markku Virtanenb642a192021-06-09 09:08:14 +0000584 // Send notification to xApp that prosessing of a Subscription Request has failed.
585 err := fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300586 errorInfo.ErrorCause = err.Error()
587 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
Markku Virtanenb642a192021-06-09 09:08:14 +0000588 continue
archaggeafbf95f2021-04-14 08:54:05 +0300589 }
590
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300591 xapp.Logger.Debug("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Markku Virtanenb642a192021-06-09 09:08:14 +0000592
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300593 subRespMsg, errorInfo, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, *restSubId, e2SubscriptionDirectives)
Markku Virtanen42723e22021-06-15 10:09:23 +0300594
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300595 xapp.Logger.Debug("Handled SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
Anssi Mannila53dbe7e2021-10-06 14:21:48 +0300596 trans.Release()
Markku Virtanen42723e22021-06-15 10:09:23 +0300597
archaggeafbf95f2021-04-14 08:54:05 +0300598 if err != nil {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200599 if err.Error() == "TEST: restart event received" {
600 // This is just for UT cases. Stop here subscription processing
601 return
602 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300603 c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
archaggeafbf95f2021-04-14 08:54:05 +0300604 } else {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000605 e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
Markku Virtanen42723e22021-06-15 10:09:23 +0300606 restSubscription.AddMd5Sum(md5sum)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300607 xapp.Logger.Debug("SubscriptionRequest index=%v processed successfullyfor %s. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
Markku Virtanen42723e22021-06-15 10:09:23 +0300608 index, *restSubId, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Anssi Mannila56233b02022-01-24 13:26:42 +0200609 c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans, errorInfo)
archaggeafbf95f2021-04-14 08:54:05 +0300610 }
archaggeafbf95f2021-04-14 08:54:05 +0300611 }
612}
613
614//-------------------------------------------------------------------
615//
616//------------------------------------------------------------------
Anssi Mannila08c8fbd2021-10-05 12:47:41 +0300617func (c *Control) SubscriptionProcessingStartDelay() {
618 if c.UTTesting == true {
619 // This is temporary fix for the UT problem that notification arrives before subscription response
620 // Correct fix would be to allow notification come before response and process it correctly
621 xapp.Logger.Debug("Setting 50 ms delay before starting processing Subscriptions")
622 <-time.After(time.Millisecond * 50)
623 xapp.Logger.Debug("Continuing after delay")
624 }
625}
626
627//-------------------------------------------------------------------
628//
629//------------------------------------------------------------------
archaggeafbf95f2021-04-14 08:54:05 +0300630func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300631 restSubId string, e2SubscriptionDirectives *E2SubscriptionDirectives) (*e2ap.E2APSubscriptionResponse, *ErrorInfo, error) {
632
633 errorInfo := ErrorInfo{}
archaggeafbf95f2021-04-14 08:54:05 +0300634
635 err := c.tracker.Track(trans)
636 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300637 xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300638 errorInfo.ErrorCause = err.Error()
Markku Virtanen55d2a282021-06-04 14:46:56 +0300639 err = fmt.Errorf("Tracking failure")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300640 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300641 }
642
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300643 subs, errorInfo, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, e2SubscriptionDirectives.CreateRMRRoute)
archaggeafbf95f2021-04-14 08:54:05 +0300644 if err != nil {
Markku Virtanen55d2a282021-06-04 14:46:56 +0300645 xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300646 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300647 }
648
649 //
650 // Wake subs request
651 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300652 subs.OngoingReqCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +0200653 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, 0)
archaggeafbf95f2021-04-14 08:54:05 +0300654 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300655 subs.OngoingReqCount--
archaggeafbf95f2021-04-14 08:54:05 +0300656
657 err = nil
658 if event != nil {
659 switch themsg := event.(type) {
660 case *e2ap.E2APSubscriptionResponse:
661 trans.Release()
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300662 if c.e2IfState.IsE2ConnectionUp(meid) == true {
Anssi Mannila56233b02022-01-24 13:26:42 +0200663 errorInfo = c.e2ap.CheckActionNotAdmittedList(xapp.RIC_SUB_RESP, themsg.ActionNotAdmittedList, c)
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300664 return themsg, &errorInfo, nil
665 } else {
666 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
667 c.RemoveSubscriptionFromDb(subs)
668 err = fmt.Errorf("E2 interface down")
669 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300670 }
archaggeafbf95f2021-04-14 08:54:05 +0300671 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila483bd162022-01-20 11:11:48 +0200672 err = fmt.Errorf("RICSubscriptionFailure. E2NodeCause: (Cause:%v, Value %v)", themsg.Cause.Content, themsg.Cause.Value)
673 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300674 case *PackSubscriptionRequestErrortEvent:
Anssi Mannila56233b02022-01-24 13:26:42 +0200675 err = fmt.Errorf("E2 RICSubscriptionRequest pack failure")
Anssi Mannilab43397e2021-11-16 12:06:16 +0200676 errorInfo = themsg.ErrorInfo
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300677 case *SDLWriteErrortEvent:
678 err = fmt.Errorf("SDL write failure")
Anssi Mannilab43397e2021-11-16 12:06:16 +0200679 errorInfo = themsg.ErrorInfo
Anssi Mannila54838ed2021-11-19 11:25:01 +0200680 case *SubmgrRestartTestEvent:
681 err = fmt.Errorf("TEST: restart event received")
682 xapp.Logger.Debug("%s", err)
683 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300684 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300685 err = fmt.Errorf("Unexpected E2 subscription response received")
686 errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
archaggeafbf95f2021-04-14 08:54:05 +0300687 break
688 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300689 } else {
Anssi Mannila3d80b722021-11-12 13:17:15 +0200690 // Timer expiry
Anssi Mannila56233b02022-01-24 13:26:42 +0200691 err = fmt.Errorf("E2 RICSubscriptionResponse timeout")
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300692 errorInfo.SetInfo(err.Error(), "", models.SubscriptionInstanceTimeoutTypeE2Timeout)
693 if subs.PolicyUpdate == true {
694 return nil, &errorInfo, err
695 }
archaggeafbf95f2021-04-14 08:54:05 +0300696 }
Markku Virtanen55d2a282021-06-04 14:46:56 +0300697
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300698 xapp.Logger.Error("XAPP-SubReq E2 subscription failed: %s", idstring(err, trans, subs))
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300699 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
700
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300701 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300702}
703
704//-------------------------------------------------------------------
705//
706//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300707func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300708 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300709
710 // Send notification to xApp that prosessing of a Subscription Request has failed.
711 e2EventInstanceID := (int64)(0)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300712 if errorInfo.ErrorSource == "" {
713 // Submgr is default source of error
714 errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceSUBMGR
715 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300716 resp := &models.SubscriptionResponse{
717 SubscriptionID: restSubId,
718 SubscriptionInstances: []*models.SubscriptionInstance{
719 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300720 ErrorCause: errorInfo.ErrorCause,
721 ErrorSource: errorInfo.ErrorSource,
722 TimeoutType: errorInfo.TimeoutType,
Markku Virtanen42723e22021-06-15 10:09:23 +0300723 XappEventInstanceID: &xAppEventInstanceID},
724 },
725 }
726 // Mark REST subscription request processed.
727 restSubscription.SetProcessed(err)
728 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
729 if trans != nil {
Anssi Mannila6bd57912022-06-16 09:47:23 +0300730 xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
731 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300732 } else {
Anssi Mannila6bd57912022-06-16 09:47:23 +0300733 xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
734 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
Markku Virtanen42723e22021-06-15 10:09:23 +0300735 }
736
737 c.UpdateCounter(cRestSubFailNotifToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300738 err = xapp.Subscription.Notify(resp, *clientEndpoint)
739 if err != nil {
740 xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
741 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300742
Anssi Mannilab43397e2021-11-16 12:06:16 +0200743 // E2 is down. Delete completely processed request safely now
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300744 if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
745 c.registry.DeleteRESTSubscription(restSubId)
746 c.RemoveRESTSubscriptionFromDb(*restSubId)
747 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300748}
749
750//-------------------------------------------------------------------
751//
752//-------------------------------------------------------------------
753func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
Anssi Mannila56233b02022-01-24 13:26:42 +0200754 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300755
756 // Store successfully processed InstanceId for deletion
757 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
758 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
759
760 // Send notification to xApp that a Subscription Request has been processed.
761 resp := &models.SubscriptionResponse{
762 SubscriptionID: restSubId,
763 SubscriptionInstances: []*models.SubscriptionInstance{
764 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannila56233b02022-01-24 13:26:42 +0200765 ErrorCause: errorInfo.ErrorCause,
766 ErrorSource: errorInfo.ErrorSource,
Markku Virtanen42723e22021-06-15 10:09:23 +0300767 XappEventInstanceID: &xAppEventInstanceID},
768 },
769 }
770 // Mark REST subscription request processesd.
771 restSubscription.SetProcessed(nil)
772 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Anssi Mannila6bd57912022-06-16 09:47:23 +0300773 xapp.Logger.Debug("Sending successful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
774 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300775 c.UpdateCounter(cRestSubNotifToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300776 err := xapp.Subscription.Notify(resp, *clientEndpoint)
777 if err != nil {
778 xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
779 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300780
Anssi Mannilab43397e2021-11-16 12:06:16 +0200781 // E2 is down. Delete completely processed request safely now
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300782 if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
783 c.registry.DeleteRESTSubscription(restSubId)
784 c.RemoveRESTSubscriptionFromDb(*restSubId)
785 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300786}
787
788//-------------------------------------------------------------------
789//
790//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300791func (c *Control) RESTSubscriptionDeleteHandler(restSubId string) int {
archaggeafbf95f2021-04-14 08:54:05 +0300792
793 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000794 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300795
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300796 xapp.Logger.Debug("SubscriptionDeleteRequest from XAPP")
archaggeafbf95f2021-04-14 08:54:05 +0300797
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000798 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300799 if err != nil {
800 xapp.Logger.Error("%s", err.Error())
801 if restSubscription == nil {
802 // Subscription was not found
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300803 c.UpdateCounter(cRestSubDelRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300804 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300805 } else {
806 if restSubscription.SubReqOngoing == true {
807 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
808 xapp.Logger.Error("%s", err.Error())
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300809 c.UpdateCounter(cRestSubDelFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300810 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300811 } else if restSubscription.SubDelReqOngoing == true {
812 // Previous request for same restSubId still ongoing
Anssi Mannila3d80b722021-11-12 13:17:15 +0200813 c.UpdateCounter(cRestSubDelRespToXapp)
814 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300815 }
816 }
817 }
818
819 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
820 go func() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300821 xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
archaggeafbf95f2021-04-14 08:54:05 +0300822 for _, instanceId := range restSubscription.InstanceIds {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200823 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId, 0)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000824
archaggeafbf95f2021-04-14 08:54:05 +0300825 if err != nil {
826 xapp.Logger.Error("%s", err.Error())
archaggeafbf95f2021-04-14 08:54:05 +0300827 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300828 xapp.Logger.Debug("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000829 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
830 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300831 }
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300832 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300833 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300834 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300835 }()
836
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000837 c.UpdateCounter(cRestSubDelRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300838 return common.UnsubscribeNoContentCode
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200839}
840
archaggeafbf95f2021-04-14 08:54:05 +0300841//-------------------------------------------------------------------
842//
843//-------------------------------------------------------------------
Anssi Mannila54838ed2021-11-19 11:25:01 +0200844func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32, waitRouteCleanupTime time.Duration) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300845
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000846 var xAppEventInstanceID int64
847 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
848 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300849 xapp.Logger.Debug("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000850 restSubId, instanceId, idstring(err, nil))
851 return xAppEventInstanceID, nil
852 }
853
854 xAppEventInstanceID = int64(subs.ReqId.Id)
855 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300856 if trans == nil {
857 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
858 xapp.Logger.Error("%s", err.Error())
859 }
860 defer trans.Release()
861
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000862 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300863 if err != nil {
864 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
865 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000866 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300867 }
868 //
869 // Wake subs delete
870 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300871 subs.OngoingDelCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +0200872 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanupTime)
archaggeafbf95f2021-04-14 08:54:05 +0300873 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300874 subs.OngoingDelCount--
archaggeafbf95f2021-04-14 08:54:05 +0300875
876 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
877
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300878 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300879
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000880 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300881}
882
883//-------------------------------------------------------------------
884//
885//-------------------------------------------------------------------
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200886
Juha Hyttinen83ada002020-01-30 10:36:33 +0200887func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300888 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200889 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300890 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200891 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200892 params.Meid = subs.GetMeid()
893 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200894 params.PayloadLen = len(trans.Payload.Buf)
895 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200896 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300897 xapp.Logger.Debug("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000898 err = c.SendWithRetry(params, false, 5)
899 if err != nil {
900 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
901 }
902 return err
kalnagye0018682019-09-26 16:28:25 +0200903}
904
Juha Hyttinen83ada002020-01-30 10:36:33 +0200905func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
906
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300907 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200908 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300909 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200910 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200911 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200912 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200913 params.PayloadLen = len(trans.Payload.Buf)
914 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200915 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300916 xapp.Logger.Debug("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000917 err = c.SendWithRetry(params, false, 5)
918 if err != nil {
919 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
920 }
921 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200922}
923
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300924func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
925 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200926 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
927 xapp.Logger.Error("%s", err.Error())
928 return
929 }
930 c.CntRecvMsg++
931
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300932 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200933
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300934 // xapp-frame might use direct access to c buffer and
935 // when msg.Mbuf is freed, someone might take it into use
936 // and payload data might be invalid inside message handle function
937 //
938 // subscriptions won't load system a lot so there is no
939 // real performance hit by cloning buffer into new go byte slice
940 cPay := append(msg.Payload[:0:0], msg.Payload...)
941 msg.Payload = cPay
942 msg.PayloadLen = len(cPay)
943
Anssi Mannila90fa0212019-12-12 10:47:47 +0200944 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200945 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200946 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200947 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200948 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200949 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200950 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200951 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200952 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200953 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200954 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200955 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200956 go c.handleE2TSubscriptionDeleteFailure(msg)
gunjarastogi4378baf2023-01-18 17:51:55 +0530957 case xapp.RIC_SUB_DEL_REQUIRED:
958 go c.handleE2TSubscriptionDeleteRequired(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200959 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300960 xapp.Logger.Debug("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000961 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200962 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200963}
964
Juha Hyttinen422d0182020-01-17 13:37:05 +0200965//-------------------------------------------------------------------
966// handle from XAPP Subscription Request
967//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300968func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300969 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200970 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200971
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300972 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
973 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
974 return
975 }
976
Juha Hyttinen422d0182020-01-17 13:37:05 +0200977 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
978 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200979 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200980 return
981 }
982
Anssi Mannila4c626a22021-02-11 12:50:48 +0200983 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200984 if trans == nil {
985 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200986 return
987 }
988 defer trans.Release()
989
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200990 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200991 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
992 return
993 }
994
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300995 subs, _, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, true)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200996 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200997 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200998 return
999 }
1000
Anssi Mannila6b3796f2021-02-12 09:11:35 +02001001 c.wakeSubscriptionRequest(subs, trans)
1002}
1003
1004//-------------------------------------------------------------------
1005// Wake Subscription Request to E2node
1006//------------------------------------------------------------------
1007func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
1008
Anssi Mannila55d6fed2022-08-02 12:41:48 +03001009 e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(nil)
1010 if err != nil {
1011 xapp.Logger.Error("c.GetE2SubscriptionDirectives failure: %s", err.Error())
1012 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001013 subs.OngoingReqCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +02001014 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, waitRouteCleanup_ms)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001015 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001016 subs.OngoingReqCount--
Juha Hyttinen422d0182020-01-17 13:37:05 +02001017 if event != nil {
1018 switch themsg := event.(type) {
1019 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +02001020 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +02001021 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
1022 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +02001023 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001024 c.UpdateCounter(cSubRespToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001025 err := c.rmrSendToXapp("", subs, trans)
1026 if err != nil {
1027 xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
1028 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001029 return
1030 }
1031 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +02001032 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +02001033 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
1034 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001035 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001036 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001037 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001038 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +02001039 break
1040 }
1041 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001042 xapp.Logger.Debug("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen31797b42020-01-16 14:05:01 +02001043}
1044
Juha Hyttinen422d0182020-01-17 13:37:05 +02001045//-------------------------------------------------------------------
1046// handle from XAPP Subscription Delete Request
1047//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001048func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001049 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001050 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001051
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001052 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
1053 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
1054 return
1055 }
1056
Juha Hyttinen422d0182020-01-17 13:37:05 +02001057 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001058 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001059 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001060 return
1061 }
1062
Anssi Mannila4c626a22021-02-11 12:50:48 +02001063 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001064 if trans == nil {
1065 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001066 return
1067 }
1068 defer trans.Release()
1069
Juha Hyttinen83ada002020-01-30 10:36:33 +02001070 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001071 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001072 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
1073 return
1074 }
1075
Juha Hyttinen47942b42020-02-27 10:41:43 +02001076 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +02001077 if err != nil {
1078 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001079 return
1080 }
1081
Juha Hyttinen422d0182020-01-17 13:37:05 +02001082 //
1083 // Wake subs delete
1084 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001085 subs.OngoingDelCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +02001086 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001087 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001088 subs.OngoingDelCount--
Juha Hyttinen422d0182020-01-17 13:37:05 +02001089
Juha Hyttinen83ada002020-01-30 10:36:33 +02001090 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1091
Anssi Mannilac92b4212020-12-07 14:59:34 +02001092 if subs.NoRespToXapp == true {
1093 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001094 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
Anssi Mannilac92b4212020-12-07 14:59:34 +02001095 return
1096 }
1097
1098 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001099 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +02001100 subDelRespMsg.RequestId.Id = trans.RequestId.Id
1101 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001102 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
1103 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
1104 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001105 c.UpdateCounter(cSubDelRespToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001106 err := c.rmrSendToXapp("", subs, trans)
1107 if err != nil {
1108 xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
1109 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001110 }
1111}
1112
1113//-------------------------------------------------------------------
1114// SUBS CREATE Handling
1115//-------------------------------------------------------------------
Anssi Mannila54838ed2021-11-19 11:25:01 +02001116func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives, waitRouteCleanupTime time.Duration) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001117
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001118 var event interface{} = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001119 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +02001120 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001121 subs.WaitTransactionTurn(trans)
1122 defer subs.ReleaseTransactionTurn(trans)
1123 defer trans.Release()
1124
Juha Hyttinen83ada002020-01-30 10:36:33 +02001125 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001126
Juha Hyttinen83ada002020-01-30 10:36:33 +02001127 subRfMsg, valid := subs.GetCachedResponse()
1128 if subRfMsg == nil && valid == true {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001129 event = c.sendE2TSubscriptionRequest(subs, trans, parentTrans, e2SubscriptionDirectives)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001130 switch event.(type) {
1131 case *e2ap.E2APSubscriptionResponse:
1132 subRfMsg, valid = subs.SetCachedResponse(event, true)
1133 subs.SubRespRcvd = true
1134 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001135 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001136 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 +02001137 case *SubmgrRestartTestEvent:
Anssi Mannila54838ed2021-11-19 11:25:01 +02001138 // 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 +02001139 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
Anssi Mannila54838ed2021-11-19 11:25:01 +02001140 subRfMsg, valid = subs.SetCachedResponse(event, false)
1141 parentTrans.SendEvent(subRfMsg, 0)
1142 return
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001143 case *PackSubscriptionRequestErrortEvent, *SDLWriteErrortEvent:
1144 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001145 default:
Anssi Mannila3d80b722021-11-12 13:17:15 +02001146 // Timer expiry
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001147 if subs.PolicyUpdate == false {
1148 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 +03001149 subRfMsg, valid = subs.SetCachedResponse(nil, false)
1150 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila54838ed2021-11-19 11:25:01 +02001151 } else {
1152 subRfMsg, valid = subs.SetCachedResponse(nil, true)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001153 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001154 }
1155 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
1156 } else {
1157 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001158 }
Anssi Mannila54838ed2021-11-19 11:25:01 +02001159 if valid == false {
1160 removeSubscriptionFromDb = true
1161 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001162
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001163 err := c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
1164 if err != nil {
Anssi Mannila54838ed2021-11-19 11:25:01 +02001165 valid = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001166 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001167
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001168 }
1169
Anssi Mannila27385172021-12-22 09:41:54 +02001170 // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001171 if valid == false {
Anssi Mannila55d6fed2022-08-02 12:41:48 +03001172 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001173 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001174
Juha Hyttinen83ada002020-01-30 10:36:33 +02001175 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001176}
1177
1178//-------------------------------------------------------------------
1179// SUBS DELETE Handling
1180//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +02001181
Anssi Mannila54838ed2021-11-19 11:25:01 +02001182func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp, waitRouteCleanupTime time.Duration) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001183
Juha Hyttinen83ada002020-01-30 10:36:33 +02001184 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001185 subs.WaitTransactionTurn(trans)
1186 defer subs.ReleaseTransactionTurn(trans)
1187 defer trans.Release()
1188
Juha Hyttinen83ada002020-01-30 10:36:33 +02001189 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001190
Juha Hyttinen3944a222020-01-24 11:51:46 +02001191 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001192
Juha Hyttinen3944a222020-01-24 11:51:46 +02001193 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
1194 subs.valid = false
1195 subs.mutex.Unlock()
1196 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1197 } else {
1198 subs.mutex.Unlock()
1199 }
Anssi Mannila27385172021-12-22 09:41:54 +02001200
1201 // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
Anssi Mannila54838ed2021-11-19 11:25:01 +02001202 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +02001203 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001204}
1205
1206//-------------------------------------------------------------------
1207// send to E2T Subscription Request
1208//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001209func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001210 var err error
1211 var event interface{} = nil
1212 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001213 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001214
1215 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +02001216 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001217 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001218 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
1219 if err != nil {
Anssi Mannilab43397e2021-11-16 12:06:16 +02001220 xapp.Logger.Error("SUBS-SubReq ASN1 pack error: %s", idstring(err, trans, subs, parentTrans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001221 return &PackSubscriptionRequestErrortEvent{
1222 ErrorInfo{
1223 ErrorSource: models.SubscriptionInstanceErrorSourceASN1,
1224 ErrorCause: err.Error(),
1225 },
1226 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001227 }
1228
Anssi Mannilac92b4212020-12-07 14:59:34 +02001229 // 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 +03001230 err = c.WriteSubscriptionToDb(subs)
1231 if err != nil {
1232 return &SDLWriteErrortEvent{
1233 ErrorInfo{
1234 ErrorSource: models.SubscriptionInstanceErrorSourceDBAAS,
1235 ErrorCause: err.Error(),
1236 },
1237 }
1238 }
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001239
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001240 for retries := int64(0); retries < e2SubscriptionDirectives.E2MaxTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001241 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001242 if retries == 0 {
1243 c.UpdateCounter(cSubReqToE2)
1244 } else {
1245 c.UpdateCounter(cSubReReqToE2)
1246 }
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001247 err := c.rmrSendToE2T(desc, subs, trans)
1248 if err != nil {
1249 xapp.Logger.Error("rmrSendToE2T() failed:%s", err.Error())
1250 }
1251
Anssi Mannilac92b4212020-12-07 14:59:34 +02001252 if subs.DoNotWaitSubResp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001253 event, timedOut = trans.WaitEvent(e2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001254 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001255 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001256 continue
1257 }
1258 } else {
1259 // Simulating case where subscrition request has been sent but response has not been received before restart
1260 event = &SubmgrRestartTestEvent{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001261 xapp.Logger.Debug("Restart event, DoNotWaitSubResp == true")
Juha Hyttinen422d0182020-01-17 13:37:05 +02001262 }
1263 break
1264 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001265 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001266 return event
1267}
1268
1269//-------------------------------------------------------------------
1270// send to E2T Subscription Delete Request
1271//-------------------------------------------------------------------
1272
Juha Hyttinen83ada002020-01-30 10:36:33 +02001273func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001274 var err error
1275 var event interface{}
1276 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001277 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001278
1279 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +02001280 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001281 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +02001282 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001283 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1284 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001285 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001286 return event
1287 }
1288
1289 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001290 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001291 if retries == 0 {
1292 c.UpdateCounter(cSubDelReqToE2)
1293 } else {
1294 c.UpdateCounter(cSubDelReReqToE2)
1295 }
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001296 err := c.rmrSendToE2T(desc, subs, trans)
1297 if err != nil {
1298 xapp.Logger.Error("SUBS-SubDelReq: rmrSendToE2T failure: %s", idstring(err, trans, subs, parentTrans))
1299 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001300 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
1301 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001302 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001303 continue
1304 }
1305 break
1306 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001307 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001308 return event
1309}
1310
1311//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +02001312// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001313//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001314func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001315 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001316 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +03001317
Juha Hyttinen422d0182020-01-17 13:37:05 +02001318 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
1319 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001320 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001321 return
1322 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001323 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +02001324 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001325 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001326 return
Juha Hyttinen31797b42020-01-16 14:05:01 +02001327 }
1328 trans := subs.GetTransaction()
1329 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001330 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001331 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001332 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001333 }
Anssi Mannila54838ed2021-11-19 11:25:01 +02001334 xapp.Logger.Debug("SUBS-SubResp: Sending event, trans= %v", trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001335 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
1336 if sendOk == false {
1337 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001338 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001339 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001340 return
kalnagy45114752019-06-18 14:40:39 +02001341}
1342
Juha Hyttinen422d0182020-01-17 13:37:05 +02001343//-------------------------------------------------------------------
1344// handle from E2T Subscription Failure
1345//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001346func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001347 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001348 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001349 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001350 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001351 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001352 return
1353 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001354 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001355 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001356 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001357 return
1358 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001359 trans := subs.GetTransaction()
1360 if trans == nil {
1361 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001362 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001363 return
1364 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001365 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1366 if sendOk == false {
1367 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001368 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001369 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001370 return
1371}
1372
Juha Hyttinen422d0182020-01-17 13:37:05 +02001373//-------------------------------------------------------------------
1374// handle from E2T Subscription Delete Response
1375//-------------------------------------------------------------------
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001376func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001377 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001378 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001379 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001380 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001381 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001382 return
1383 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001384 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001385 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001386 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001387 return
1388 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001389 trans := subs.GetTransaction()
1390 if trans == nil {
1391 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001392 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001393 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001394 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001395 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1396 if sendOk == false {
1397 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001398 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001399 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001400 return
1401}
1402
Juha Hyttinen422d0182020-01-17 13:37:05 +02001403//-------------------------------------------------------------------
1404// handle from E2T Subscription Delete Failure
1405//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001406func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001407 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001408 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001409 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001410 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001411 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001412 return
1413 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001414 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001415 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001416 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001417 return
1418 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001419 trans := subs.GetTransaction()
1420 if trans == nil {
1421 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001422 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001423 return
1424 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001425 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1426 if sendOk == false {
1427 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001428 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001429 }
kalnagy93cc3e22019-09-19 11:29:29 +02001430 return
1431}
1432
Juha Hyttinen422d0182020-01-17 13:37:05 +02001433//-------------------------------------------------------------------
1434//
1435//-------------------------------------------------------------------
1436func typeofSubsMessage(v interface{}) string {
1437 if v == nil {
1438 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001439 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001440 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001441 //case *e2ap.E2APSubscriptionRequest:
1442 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001443 case *e2ap.E2APSubscriptionResponse:
1444 return "SubResp"
1445 case *e2ap.E2APSubscriptionFailure:
1446 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001447 //case *e2ap.E2APSubscriptionDeleteRequest:
1448 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001449 case *e2ap.E2APSubscriptionDeleteResponse:
1450 return "SubDelResp"
1451 case *e2ap.E2APSubscriptionDeleteFailure:
1452 return "SubDelFail"
1453 default:
1454 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001455 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001456}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001457
1458//-------------------------------------------------------------------
1459//
1460//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001461func (c *Control) WriteSubscriptionToDb(subs *Subscription) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001462 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1463 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1464 if err != nil {
1465 xapp.Logger.Error("%v", err)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001466 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001467 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001468 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001469}
1470
1471//-------------------------------------------------------------------
1472//
1473//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001474func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001475
1476 if removeSubscriptionFromDb == true {
1477 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1478 c.RemoveSubscriptionFromDb(subs)
1479 } else {
1480 // Update is needed for successful response and merge case here
1481 if subs.RetryFromXapp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001482 err := c.WriteSubscriptionToDb(subs)
1483 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001484 }
1485 }
1486 subs.RetryFromXapp = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001487 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001488}
1489
1490//-------------------------------------------------------------------
1491//
1492//-------------------------------------------------------------------
1493func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1494 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1495 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1496 if err != nil {
1497 xapp.Logger.Error("%v", err)
1498 }
1499}
1500
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001501//-------------------------------------------------------------------
1502//
1503//-------------------------------------------------------------------
1504func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1505 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1506 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1507 if err != nil {
1508 xapp.Logger.Error("%v", err)
1509 }
1510}
1511
1512//-------------------------------------------------------------------
1513//
1514//-------------------------------------------------------------------
1515func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1516
1517 if removeRestSubscriptionFromDb == true {
1518 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1519 c.RemoveRESTSubscriptionFromDb(restSubId)
1520 } else {
1521 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1522 }
1523}
1524
1525//-------------------------------------------------------------------
1526//
1527//-------------------------------------------------------------------
1528func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1529 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1530 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1531 if err != nil {
1532 xapp.Logger.Error("%v", err)
1533 }
1534}
1535
gunjarastogi4378baf2023-01-18 17:51:55 +05301536func (c *Control) SendSubscriptionDeleteReq(subs *Subscription, e2SubsDelRequired bool) {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001537
Anssi Mannila54838ed2021-11-19 11:25:01 +02001538 if c.UTTesting == true {
1539 // Reqistry mutex is not locked after real restart but it can be when restart is simulated in unit tests
1540 c.registry.mutex = new(sync.Mutex)
1541 }
1542
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001543 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001544 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1545
1546 // Send delete for every endpoint in the subscription
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001547 if subs.PolicyUpdate == false {
1548 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1549 subDelReqMsg.RequestId = subs.GetReqId().RequestId
1550 subDelReqMsg.RequestId.Id = ricRequestorId
1551 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1552 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1553 if err != nil {
1554 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1555 return
1556 }
1557 for _, endPoint := range subs.EpList.Endpoints {
1558 params := &xapp.RMRParams{}
1559 params.Mtype = mType
1560 params.SubId = int(subs.GetReqId().InstanceId)
1561 params.Xid = ""
1562 params.Meid = subs.Meid
1563 params.Src = endPoint.String()
1564 params.PayloadLen = len(payload.Buf)
1565 params.Payload = payload.Buf
1566 params.Mbuf = nil
1567 subs.DeleteFromDb = true
gunjarastogi4378baf2023-01-18 17:51:55 +05301568 if !e2SubsDelRequired {
1569 c.handleXAPPSubscriptionDeleteRequest(params)
1570 } else {
1571 c.SendSubscriptionDeleteReqToE2T(subs, params)
1572 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001573 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001574 }
1575}
Anssi Mannilade457a12021-06-02 16:19:48 +03001576
1577func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1578
1579 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001580
Markku Virtanen2b512b62021-07-30 12:04:00 +00001581 if p == nil {
1582 return
1583 }
1584
Markku Virtanen987f8b12021-06-10 15:05:02 +03001585 if p.SubscriptionID != "" {
1586 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1587 } else {
1588 fmt.Println(" SubscriptionID = ''")
1589 }
1590
Anssi Mannilade457a12021-06-02 16:19:48 +03001591 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1592
1593 if p.ClientEndpoint.HTTPPort != nil {
1594 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1595 } else {
1596 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1597 }
1598
1599 if p.ClientEndpoint.RMRPort != nil {
1600 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1601 } else {
1602 fmt.Println(" ClientEndpoint.RMRPort = nil")
1603 }
1604
1605 if p.Meid != nil {
1606 fmt.Printf(" Meid = %s\n", *p.Meid)
1607 } else {
1608 fmt.Println(" Meid = nil")
1609 }
1610
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001611 if p.E2SubscriptionDirectives == nil {
1612 fmt.Println(" E2SubscriptionDirectives = nil")
1613 } else {
1614 fmt.Println(" E2SubscriptionDirectives")
1615 if p.E2SubscriptionDirectives.E2RetryCount == nil {
1616 fmt.Println(" E2RetryCount == nil")
1617 } else {
1618 fmt.Printf(" E2RetryCount = %v\n", *p.E2SubscriptionDirectives.E2RetryCount)
1619 }
1620 fmt.Printf(" E2TimeoutTimerValue = %v\n", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilacb7dd492021-10-01 11:10:26 +03001621 fmt.Printf(" RMRRoutingNeeded = %v\n", p.E2SubscriptionDirectives.RMRRoutingNeeded)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001622 }
Anssi Mannilade457a12021-06-02 16:19:48 +03001623 for _, subscriptionDetail := range p.SubscriptionDetails {
1624 if p.RANFunctionID != nil {
1625 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1626 } else {
1627 fmt.Println(" RANFunctionID = nil")
1628 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001629 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001630 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001631
1632 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1633 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1634 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001635 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001636
Anssi Mannilade457a12021-06-02 16:19:48 +03001637 if actionToBeSetup.SubsequentAction != nil {
1638 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1639 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1640 } else {
1641 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1642 }
1643 }
1644 }
1645}
gunjarastogi4378baf2023-01-18 17:51:55 +05301646
1647//-------------------------------------------------------------------
1648// handle from E2T Subscription Delete Required
1649//-------------------------------------------------------------------
1650func (c *Control) handleE2TSubscriptionDeleteRequired(params *xapp.RMRParams) {
1651 xapp.Logger.Info("MSG from E2T: %s", params.String())
1652 c.UpdateCounter(cSubDelRequFromE2)
1653 subsDelRequMsg, err := c.e2ap.UnpackSubscriptionDeleteRequired(params.Payload)
1654 if err != nil {
1655 xapp.Logger.Error("MSG-SubDelRequired: %s", idstring(err, params))
1656 //c.sendE2TErrorIndication(nil)
1657 return
1658 }
1659 var subscriptions = map[string][]e2ap.E2APSubscriptionDeleteRequired{}
1660 var subDB = []*Subscription{}
1661 for _, subsTobeRemove := range subsDelRequMsg.E2APSubscriptionDeleteRequiredRequests {
1662 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subsTobeRemove.RequestId.InstanceId})
1663 if err != nil {
1664 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
1665 continue
1666 }
1667 // Check if Delete Subscription Already triggered
1668 if subs.OngoingDelCount > 0 {
1669 continue
1670 }
1671 subDB = append(subDB, subs)
1672 for _, endpoint := range subs.EpList.Endpoints {
1673 subscriptions[endpoint.Addr] = append(subscriptions[endpoint.Addr], subsTobeRemove)
1674 }
1675 // Sending Subscription Delete Request to E2T
1676 // c.SendSubscriptionDeleteReq(subs, true)
1677 }
1678 for _, subsTobeRemove := range subDB {
1679 // Sending Subscription Delete Request to E2T
1680 c.SendSubscriptionDeleteReq(subsTobeRemove, true)
1681 }
1682}
1683
1684//-----------------------------------------------------------------
1685// Initiate RIC Subscription Delete Request after receiving
1686// RIC Subscription Delete Required from E2T
1687//-----------------------------------------------------------------
1688func (c *Control) SendSubscriptionDeleteReqToE2T(subs *Subscription, params *xapp.RMRParams) {
1689 xapp.Logger.Debug("MSG TO E2T: %s", params.String())
1690 c.UpdateCounter(cSubDelReqToE2)
1691
1692 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
1693 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
1694 return
1695 }
1696
1697 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subs.ReqId.RequestId, params.Meid)
1698 if trans == nil {
1699 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
1700 return
1701 }
1702 defer trans.Release()
1703
1704 err := c.tracker.Track(trans)
1705 if err != nil {
1706 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
1707 return
1708 }
1709
1710 //
1711 // Wake subs delete
1712 //
1713 subs.OngoingDelCount++
1714 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
1715 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
1716 subs.OngoingDelCount--
1717
1718 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1719
1720 if subs.NoRespToXapp == true {
1721 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
1722 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
1723 return
1724 }
1725}