blob: 1f769e1229af9560582ae63bd4ded958a98e95b3 [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))
aditverm323c77e2023-09-20 09:50:47 +0530699 // If policy type subscription fails we cannot remove it only internally. Once subscription has been created
700 // successfully, it must be deleted on both sides.
701 if subs.PolicyUpdate == false {
702 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
703 }
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300704
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300705 return nil, &errorInfo, err
archaggeafbf95f2021-04-14 08:54:05 +0300706}
707
708//-------------------------------------------------------------------
709//
710//-------------------------------------------------------------------
Markku Virtanen42723e22021-06-15 10:09:23 +0300711func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300712 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300713
714 // Send notification to xApp that prosessing of a Subscription Request has failed.
715 e2EventInstanceID := (int64)(0)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300716 if errorInfo.ErrorSource == "" {
717 // Submgr is default source of error
718 errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceSUBMGR
719 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300720 resp := &models.SubscriptionResponse{
721 SubscriptionID: restSubId,
722 SubscriptionInstances: []*models.SubscriptionInstance{
723 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannilacb7dd492021-10-01 11:10:26 +0300724 ErrorCause: errorInfo.ErrorCause,
725 ErrorSource: errorInfo.ErrorSource,
726 TimeoutType: errorInfo.TimeoutType,
Markku Virtanen42723e22021-06-15 10:09:23 +0300727 XappEventInstanceID: &xAppEventInstanceID},
728 },
729 }
730 // Mark REST subscription request processed.
731 restSubscription.SetProcessed(err)
732 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
733 if trans != nil {
Anssi Mannila6bd57912022-06-16 09:47:23 +0300734 xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
735 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300736 } else {
Anssi Mannila6bd57912022-06-16 09:47:23 +0300737 xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
738 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
Markku Virtanen42723e22021-06-15 10:09:23 +0300739 }
740
741 c.UpdateCounter(cRestSubFailNotifToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300742 err = xapp.Subscription.Notify(resp, *clientEndpoint)
743 if err != nil {
744 xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
745 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300746
Anssi Mannilab43397e2021-11-16 12:06:16 +0200747 // E2 is down. Delete completely processed request safely now
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300748 if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
749 c.registry.DeleteRESTSubscription(restSubId)
750 c.RemoveRESTSubscriptionFromDb(*restSubId)
751 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300752}
753
754//-------------------------------------------------------------------
755//
756//-------------------------------------------------------------------
757func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
Anssi Mannila56233b02022-01-24 13:26:42 +0200758 clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
Markku Virtanen42723e22021-06-15 10:09:23 +0300759
760 // Store successfully processed InstanceId for deletion
761 restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
762 restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
763
764 // Send notification to xApp that a Subscription Request has been processed.
765 resp := &models.SubscriptionResponse{
766 SubscriptionID: restSubId,
767 SubscriptionInstances: []*models.SubscriptionInstance{
768 &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
Anssi Mannila56233b02022-01-24 13:26:42 +0200769 ErrorCause: errorInfo.ErrorCause,
770 ErrorSource: errorInfo.ErrorSource,
Markku Virtanen42723e22021-06-15 10:09:23 +0300771 XappEventInstanceID: &xAppEventInstanceID},
772 },
773 }
774 // Mark REST subscription request processesd.
775 restSubscription.SetProcessed(nil)
776 c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
Anssi Mannila6bd57912022-06-16 09:47:23 +0300777 xapp.Logger.Debug("Sending successful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
778 errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
Markku Virtanen42723e22021-06-15 10:09:23 +0300779 c.UpdateCounter(cRestSubNotifToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300780 err := xapp.Subscription.Notify(resp, *clientEndpoint)
781 if err != nil {
782 xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
783 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300784
Anssi Mannilab43397e2021-11-16 12:06:16 +0200785 // E2 is down. Delete completely processed request safely now
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300786 if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
787 c.registry.DeleteRESTSubscription(restSubId)
788 c.RemoveRESTSubscriptionFromDb(*restSubId)
789 }
Markku Virtanen42723e22021-06-15 10:09:23 +0300790}
791
792//-------------------------------------------------------------------
793//
794//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300795func (c *Control) RESTSubscriptionDeleteHandler(restSubId string) int {
archaggeafbf95f2021-04-14 08:54:05 +0300796
797 c.CntRecvMsg++
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000798 c.UpdateCounter(cRestSubDelReqFromXapp)
archaggeafbf95f2021-04-14 08:54:05 +0300799
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300800 xapp.Logger.Debug("SubscriptionDeleteRequest from XAPP")
archaggeafbf95f2021-04-14 08:54:05 +0300801
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000802 restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
archaggeafbf95f2021-04-14 08:54:05 +0300803 if err != nil {
804 xapp.Logger.Error("%s", err.Error())
805 if restSubscription == nil {
806 // Subscription was not found
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300807 c.UpdateCounter(cRestSubDelRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300808 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300809 } else {
810 if restSubscription.SubReqOngoing == true {
811 err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
812 xapp.Logger.Error("%s", err.Error())
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300813 c.UpdateCounter(cRestSubDelFailToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300814 return common.UnsubscribeBadRequestCode
archaggeafbf95f2021-04-14 08:54:05 +0300815 } else if restSubscription.SubDelReqOngoing == true {
816 // Previous request for same restSubId still ongoing
Anssi Mannila3d80b722021-11-12 13:17:15 +0200817 c.UpdateCounter(cRestSubDelRespToXapp)
818 return common.UnsubscribeNoContentCode
archaggeafbf95f2021-04-14 08:54:05 +0300819 }
820 }
821 }
822
823 xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
824 go func() {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300825 xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
archaggeafbf95f2021-04-14 08:54:05 +0300826 for _, instanceId := range restSubscription.InstanceIds {
Anssi Mannila54838ed2021-11-19 11:25:01 +0200827 xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId, 0)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000828
archaggeafbf95f2021-04-14 08:54:05 +0300829 if err != nil {
830 xapp.Logger.Error("%s", err.Error())
archaggeafbf95f2021-04-14 08:54:05 +0300831 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300832 xapp.Logger.Debug("Deleteting instanceId = %v", instanceId)
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000833 restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
834 restSubscription.DeleteE2InstanceId(instanceId)
archaggeafbf95f2021-04-14 08:54:05 +0300835 }
Anssi Mannila3cdd2e02021-10-14 12:35:41 +0300836 c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
archaggeafbf95f2021-04-14 08:54:05 +0300837 c.registry.DeleteRESTSubscription(&restSubId)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300838 c.RemoveRESTSubscriptionFromDb(restSubId)
archaggeafbf95f2021-04-14 08:54:05 +0300839 }()
840
Konstantinos Archangelof27433e72021-05-31 08:30:35 +0000841 c.UpdateCounter(cRestSubDelRespToXapp)
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300842 return common.UnsubscribeNoContentCode
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200843}
844
archaggeafbf95f2021-04-14 08:54:05 +0300845//-------------------------------------------------------------------
846//
847//-------------------------------------------------------------------
Anssi Mannila54838ed2021-11-19 11:25:01 +0200848func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32, waitRouteCleanupTime time.Duration) (int64, error) {
archaggeafbf95f2021-04-14 08:54:05 +0300849
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000850 var xAppEventInstanceID int64
851 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
852 if err != nil {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300853 xapp.Logger.Debug("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000854 restSubId, instanceId, idstring(err, nil))
855 return xAppEventInstanceID, nil
856 }
857
858 xAppEventInstanceID = int64(subs.ReqId.Id)
859 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
archaggeafbf95f2021-04-14 08:54:05 +0300860 if trans == nil {
861 err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
862 xapp.Logger.Error("%s", err.Error())
863 }
864 defer trans.Release()
865
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000866 err = c.tracker.Track(trans)
archaggeafbf95f2021-04-14 08:54:05 +0300867 if err != nil {
868 err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
869 xapp.Logger.Error("%s", err.Error())
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000870 return xAppEventInstanceID, &time.ParseError{}
archaggeafbf95f2021-04-14 08:54:05 +0300871 }
872 //
873 // Wake subs delete
874 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300875 subs.OngoingDelCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +0200876 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanupTime)
archaggeafbf95f2021-04-14 08:54:05 +0300877 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300878 subs.OngoingDelCount--
archaggeafbf95f2021-04-14 08:54:05 +0300879
880 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
881
Anssi Mannila55d6fed2022-08-02 12:41:48 +0300882 c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
archaggeafbf95f2021-04-14 08:54:05 +0300883
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000884 return xAppEventInstanceID, nil
archaggeafbf95f2021-04-14 08:54:05 +0300885}
886
887//-------------------------------------------------------------------
888//
889//-------------------------------------------------------------------
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200890
Juha Hyttinen83ada002020-01-30 10:36:33 +0200891func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300892 params := &xapp.RMRParams{}
Juha Hyttinene406a342020-01-13 13:02:26 +0200893 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300894 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200895 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200896 params.Meid = subs.GetMeid()
897 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200898 params.PayloadLen = len(trans.Payload.Buf)
899 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200900 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300901 xapp.Logger.Debug("MSG to E2T: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000902 err = c.SendWithRetry(params, false, 5)
903 if err != nil {
904 xapp.Logger.Error("rmrSendToE2T: Send failed: %+v", err)
905 }
906 return err
kalnagye0018682019-09-26 16:28:25 +0200907}
908
Juha Hyttinen83ada002020-01-30 10:36:33 +0200909func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
910
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300911 params := &xapp.RMRParams{}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200912 params.Mtype = trans.GetMtype()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300913 params.SubId = int(subs.GetReqId().InstanceId)
Juha Hyttinene406a342020-01-13 13:02:26 +0200914 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200915 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200916 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200917 params.PayloadLen = len(trans.Payload.Buf)
918 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200919 params.Mbuf = nil
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300920 xapp.Logger.Debug("MSG to XAPP: %s %s %s", desc, trans.String(), params.String())
Abukar Mohamedfbd4df52021-03-11 06:13:27 +0000921 err = c.SendWithRetry(params, false, 5)
922 if err != nil {
923 xapp.Logger.Error("rmrSendToXapp: Send failed: %+v", err)
924 }
925 return err
Juha Hyttinene406a342020-01-13 13:02:26 +0200926}
927
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300928func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
929 if c.RMRClient == nil {
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200930 err = fmt.Errorf("Rmr object nil can handle %s", msg.String())
931 xapp.Logger.Error("%s", err.Error())
932 return
933 }
934 c.CntRecvMsg++
935
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300936 defer c.RMRClient.Free(msg.Mbuf)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200937
Juha Hyttinena8a908d2020-05-26 15:12:47 +0300938 // xapp-frame might use direct access to c buffer and
939 // when msg.Mbuf is freed, someone might take it into use
940 // and payload data might be invalid inside message handle function
941 //
942 // subscriptions won't load system a lot so there is no
943 // real performance hit by cloning buffer into new go byte slice
944 cPay := append(msg.Payload[:0:0], msg.Payload...)
945 msg.Payload = cPay
946 msg.PayloadLen = len(cPay)
947
Anssi Mannila90fa0212019-12-12 10:47:47 +0200948 switch msg.Mtype {
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200949 case xapp.RIC_SUB_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200950 go c.handleXAPPSubscriptionRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200951 case xapp.RIC_SUB_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200952 go c.handleE2TSubscriptionResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200953 case xapp.RIC_SUB_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200954 go c.handleE2TSubscriptionFailure(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200955 case xapp.RIC_SUB_DEL_REQ:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200956 go c.handleXAPPSubscriptionDeleteRequest(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200957 case xapp.RIC_SUB_DEL_RESP:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200958 go c.handleE2TSubscriptionDeleteResponse(msg)
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200959 case xapp.RIC_SUB_DEL_FAILURE:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200960 go c.handleE2TSubscriptionDeleteFailure(msg)
gunjarastogi4378baf2023-01-18 17:51:55 +0530961 case xapp.RIC_SUB_DEL_REQUIRED:
962 go c.handleE2TSubscriptionDeleteRequired(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200963 default:
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300964 xapp.Logger.Debug("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000965 }
Juha Hyttinen5f8ffa02020-02-06 15:28:59 +0200966 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200967}
968
Juha Hyttinen422d0182020-01-17 13:37:05 +0200969//-------------------------------------------------------------------
970// handle from XAPP Subscription Request
971//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300972func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300973 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +0200974 c.UpdateCounter(cSubReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200975
Anssi Mannilac7da4ee2021-10-22 09:52:02 +0300976 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
977 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
978 return
979 }
980
Juha Hyttinen422d0182020-01-17 13:37:05 +0200981 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
982 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200983 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200984 return
985 }
986
Anssi Mannila4c626a22021-02-11 12:50:48 +0200987 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200988 if trans == nil {
989 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200990 return
991 }
992 defer trans.Release()
993
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200994 if err = c.tracker.Track(trans); err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200995 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
996 return
997 }
998
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300999 subs, _, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, true)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001000 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001001 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001002 return
1003 }
1004
Anssi Mannila6b3796f2021-02-12 09:11:35 +02001005 c.wakeSubscriptionRequest(subs, trans)
1006}
1007
1008//-------------------------------------------------------------------
1009// Wake Subscription Request to E2node
1010//------------------------------------------------------------------
1011func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
1012
Anssi Mannila55d6fed2022-08-02 12:41:48 +03001013 e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(nil)
1014 if err != nil {
1015 xapp.Logger.Error("c.GetE2SubscriptionDirectives failure: %s", err.Error())
1016 }
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001017 subs.OngoingReqCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +02001018 go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, waitRouteCleanup_ms)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001019 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001020 subs.OngoingReqCount--
Juha Hyttinen422d0182020-01-17 13:37:05 +02001021 if event != nil {
1022 switch themsg := event.(type) {
1023 case *e2ap.E2APSubscriptionResponse:
Anssi Mannila4c626a22021-02-11 12:50:48 +02001024 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +02001025 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
1026 if err == nil {
Anssi Mannila64a0df42020-02-26 09:46:03 +02001027 trans.Release()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001028 c.UpdateCounter(cSubRespToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001029 err := c.rmrSendToXapp("", subs, trans)
1030 if err != nil {
1031 xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
1032 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001033 return
1034 }
1035 case *e2ap.E2APSubscriptionFailure:
Anssi Mannila4c626a22021-02-11 12:50:48 +02001036 themsg.RequestId.Id = trans.RequestId.Id
Juha Hyttinen422d0182020-01-17 13:37:05 +02001037 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
1038 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001039 c.UpdateCounter(cSubFailToXapp)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001040 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001041 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001042 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +02001043 break
1044 }
1045 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001046 xapp.Logger.Debug("XAPP-SubReq: failed %s", idstring(err, trans, subs))
Juha Hyttinen31797b42020-01-16 14:05:01 +02001047}
1048
Juha Hyttinen422d0182020-01-17 13:37:05 +02001049//-------------------------------------------------------------------
1050// handle from XAPP Subscription Delete Request
1051//------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001052func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001053 xapp.Logger.Debug("MSG from XAPP: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001054 c.UpdateCounter(cSubDelReqFromXapp)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001055
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001056 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
1057 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
1058 return
1059 }
1060
Juha Hyttinen422d0182020-01-17 13:37:05 +02001061 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001062 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001063 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001064 return
1065 }
1066
Anssi Mannila4c626a22021-02-11 12:50:48 +02001067 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subDelReqMsg.RequestId, params.Meid)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001068 if trans == nil {
1069 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001070 return
1071 }
1072 defer trans.Release()
1073
Juha Hyttinen83ada002020-01-30 10:36:33 +02001074 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001075 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001076 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
1077 return
1078 }
1079
Juha Hyttinen47942b42020-02-27 10:41:43 +02001080 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{trans.GetSubId()})
Juha Hyttinen83ada002020-01-30 10:36:33 +02001081 if err != nil {
1082 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001083 return
1084 }
1085
Juha Hyttinen422d0182020-01-17 13:37:05 +02001086 //
1087 // Wake subs delete
1088 //
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001089 subs.OngoingDelCount++
Anssi Mannila54838ed2021-11-19 11:25:01 +02001090 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001091 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
Anssi Mannilac7da4ee2021-10-22 09:52:02 +03001092 subs.OngoingDelCount--
Juha Hyttinen422d0182020-01-17 13:37:05 +02001093
Juha Hyttinen83ada002020-01-30 10:36:33 +02001094 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1095
Anssi Mannilac92b4212020-12-07 14:59:34 +02001096 if subs.NoRespToXapp == true {
1097 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001098 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
Anssi Mannilac92b4212020-12-07 14:59:34 +02001099 return
1100 }
1101
1102 // Whatever is received success, fail or timeout, send successful delete response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001103 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Anssi Mannila4c626a22021-02-11 12:50:48 +02001104 subDelRespMsg.RequestId.Id = trans.RequestId.Id
1105 subDelRespMsg.RequestId.InstanceId = subs.GetReqId().RequestId.InstanceId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001106 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
1107 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
1108 if err == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001109 c.UpdateCounter(cSubDelRespToXapp)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001110 err := c.rmrSendToXapp("", subs, trans)
1111 if err != nil {
1112 xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
1113 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001114 }
1115}
1116
1117//-------------------------------------------------------------------
1118// SUBS CREATE Handling
1119//-------------------------------------------------------------------
Anssi Mannila54838ed2021-11-19 11:25:01 +02001120func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives, waitRouteCleanupTime time.Duration) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001121
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001122 var event interface{} = nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001123 var removeSubscriptionFromDb bool = false
Juha Hyttinen83ada002020-01-30 10:36:33 +02001124 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001125 subs.WaitTransactionTurn(trans)
1126 defer subs.ReleaseTransactionTurn(trans)
1127 defer trans.Release()
1128
Juha Hyttinen83ada002020-01-30 10:36:33 +02001129 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001130
Juha Hyttinen83ada002020-01-30 10:36:33 +02001131 subRfMsg, valid := subs.GetCachedResponse()
1132 if subRfMsg == nil && valid == true {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001133 event = c.sendE2TSubscriptionRequest(subs, trans, parentTrans, e2SubscriptionDirectives)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001134 switch event.(type) {
1135 case *e2ap.E2APSubscriptionResponse:
1136 subRfMsg, valid = subs.SetCachedResponse(event, true)
1137 subs.SubRespRcvd = true
1138 case *e2ap.E2APSubscriptionFailure:
aditverm323c77e2023-09-20 09:50:47 +05301139 if subs.PolicyUpdate == false {
1140 subRfMsg, valid = subs.SetCachedResponse(event, false)
1141 } else {
1142 // In policy update case where subscription has already been created successfully in Gnb
1143 // we cannot delete subscription internally in submgr
1144 subRfMsg, valid = subs.SetCachedResponse(event, true)
1145 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001146 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 +02001147 case *SubmgrRestartTestEvent:
Anssi Mannila54838ed2021-11-19 11:25:01 +02001148 // 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 +02001149 xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
Anssi Mannila54838ed2021-11-19 11:25:01 +02001150 subRfMsg, valid = subs.SetCachedResponse(event, false)
1151 parentTrans.SendEvent(subRfMsg, 0)
1152 return
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001153 case *PackSubscriptionRequestErrortEvent, *SDLWriteErrortEvent:
1154 subRfMsg, valid = subs.SetCachedResponse(event, false)
Anssi Mannila6d629ad2021-01-25 09:59:56 +02001155 default:
Anssi Mannila3d80b722021-11-12 13:17:15 +02001156 // Timer expiry
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001157 if subs.PolicyUpdate == false {
1158 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 +03001159 subRfMsg, valid = subs.SetCachedResponse(nil, false)
1160 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila54838ed2021-11-19 11:25:01 +02001161 } else {
1162 subRfMsg, valid = subs.SetCachedResponse(nil, true)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001163 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001164 }
1165 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
1166 } else {
1167 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001168 }
aditverm323c77e2023-09-20 09:50:47 +05301169 xapp.Logger.Debug("subs.PolicyUpdate: %v", subs.PolicyUpdate)
1170 xapp.Logger.Debug("subs: %v", subs)
1171
Anssi Mannila54838ed2021-11-19 11:25:01 +02001172 if valid == false {
1173 removeSubscriptionFromDb = true
1174 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001175
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001176 err := c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
1177 if err != nil {
Anssi Mannila54838ed2021-11-19 11:25:01 +02001178 valid = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001179 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001180
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001181 }
1182
Anssi Mannila27385172021-12-22 09:41:54 +02001183 // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001184 if valid == false {
Anssi Mannila55d6fed2022-08-02 12:41:48 +03001185 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
Juha Hyttinen429cfe62020-03-31 11:23:02 +03001186 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001187
Juha Hyttinen83ada002020-01-30 10:36:33 +02001188 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001189}
1190
1191//-------------------------------------------------------------------
1192// SUBS DELETE Handling
1193//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +02001194
Anssi Mannila54838ed2021-11-19 11:25:01 +02001195func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp, waitRouteCleanupTime time.Duration) {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001196
Juha Hyttinen83ada002020-01-30 10:36:33 +02001197 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001198 subs.WaitTransactionTurn(trans)
1199 defer subs.ReleaseTransactionTurn(trans)
1200 defer trans.Release()
1201
Juha Hyttinen83ada002020-01-30 10:36:33 +02001202 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001203
Juha Hyttinen3944a222020-01-24 11:51:46 +02001204 subs.mutex.Lock()
Anssi Mannila4abf1802021-01-28 13:06:46 +02001205
Juha Hyttinen3944a222020-01-24 11:51:46 +02001206 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
1207 subs.valid = false
1208 subs.mutex.Unlock()
1209 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
1210 } else {
1211 subs.mutex.Unlock()
1212 }
Anssi Mannila27385172021-12-22 09:41:54 +02001213
1214 // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
Anssi Mannila54838ed2021-11-19 11:25:01 +02001215 c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
Juha Hyttinen47942b42020-02-27 10:41:43 +02001216 parentTrans.SendEvent(nil, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001217}
1218
1219//-------------------------------------------------------------------
1220// send to E2T Subscription Request
1221//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001222func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001223 var err error
1224 var event interface{} = nil
1225 var timedOut bool = false
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001226 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001227
1228 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +02001229 subReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001230 subReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001231 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
1232 if err != nil {
Anssi Mannilab43397e2021-11-16 12:06:16 +02001233 xapp.Logger.Error("SUBS-SubReq ASN1 pack error: %s", idstring(err, trans, subs, parentTrans))
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001234 return &PackSubscriptionRequestErrortEvent{
1235 ErrorInfo{
1236 ErrorSource: models.SubscriptionInstanceErrorSourceASN1,
1237 ErrorCause: err.Error(),
1238 },
1239 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001240 }
1241
Anssi Mannilac92b4212020-12-07 14:59:34 +02001242 // 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 +03001243 err = c.WriteSubscriptionToDb(subs)
1244 if err != nil {
1245 return &SDLWriteErrortEvent{
1246 ErrorInfo{
1247 ErrorSource: models.SubscriptionInstanceErrorSourceDBAAS,
1248 ErrorCause: err.Error(),
1249 },
1250 }
1251 }
Markku Virtanenfe2cdab2021-05-21 10:59:29 +00001252
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001253 for retries := int64(0); retries < e2SubscriptionDirectives.E2MaxTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001254 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001255 if retries == 0 {
1256 c.UpdateCounter(cSubReqToE2)
1257 } else {
1258 c.UpdateCounter(cSubReReqToE2)
1259 }
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001260 err := c.rmrSendToE2T(desc, subs, trans)
1261 if err != nil {
1262 xapp.Logger.Error("rmrSendToE2T() failed:%s", err.Error())
1263 }
1264
Anssi Mannilac92b4212020-12-07 14:59:34 +02001265 if subs.DoNotWaitSubResp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001266 event, timedOut = trans.WaitEvent(e2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001267 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001268 c.UpdateCounter(cSubReqTimerExpiry)
Anssi Mannilac92b4212020-12-07 14:59:34 +02001269 continue
1270 }
1271 } else {
1272 // Simulating case where subscrition request has been sent but response has not been received before restart
1273 event = &SubmgrRestartTestEvent{}
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001274 xapp.Logger.Debug("Restart event, DoNotWaitSubResp == true")
Juha Hyttinen422d0182020-01-17 13:37:05 +02001275 }
1276 break
1277 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001278 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001279 return event
1280}
1281
1282//-------------------------------------------------------------------
1283// send to E2T Subscription Delete Request
1284//-------------------------------------------------------------------
1285
Juha Hyttinen83ada002020-01-30 10:36:33 +02001286func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001287 var err error
1288 var event interface{}
1289 var timedOut bool
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001290 const ricRequestorId = 123
Juha Hyttinen422d0182020-01-17 13:37:05 +02001291
1292 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +02001293 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001294 subDelReqMsg.RequestId.Id = ricRequestorId
Juha Hyttinenaf91f972020-01-24 08:38:47 +02001295 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +02001296 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1297 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001298 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001299 return event
1300 }
1301
1302 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001303 desc := fmt.Sprintf("(retry %d)", retries)
Anssi Mannila4abf1802021-01-28 13:06:46 +02001304 if retries == 0 {
1305 c.UpdateCounter(cSubDelReqToE2)
1306 } else {
1307 c.UpdateCounter(cSubDelReReqToE2)
1308 }
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001309 err := c.rmrSendToE2T(desc, subs, trans)
1310 if err != nil {
1311 xapp.Logger.Error("SUBS-SubDelReq: rmrSendToE2T failure: %s", idstring(err, trans, subs, parentTrans))
1312 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001313 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
1314 if timedOut {
Anssi Mannila4abf1802021-01-28 13:06:46 +02001315 c.UpdateCounter(cSubDelReqTimerExpiry)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001316 continue
1317 }
1318 break
1319 }
Juha Hyttinen83ada002020-01-30 10:36:33 +02001320 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001321 return event
1322}
1323
1324//-------------------------------------------------------------------
Anssi Mannila4abf1802021-01-28 13:06:46 +02001325// handle from E2T Subscription Response
Juha Hyttinen422d0182020-01-17 13:37:05 +02001326//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001327func (c *Control) handleE2TSubscriptionResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001328 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001329 c.UpdateCounter(cSubRespFromE2)
Anssi Mannila51122392021-05-25 11:51:31 +03001330
Juha Hyttinen422d0182020-01-17 13:37:05 +02001331 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
1332 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001333 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001334 return
1335 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001336 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.InstanceId})
Juha Hyttinen422d0182020-01-17 13:37:05 +02001337 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001338 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +02001339 return
Juha Hyttinen31797b42020-01-16 14:05:01 +02001340 }
1341 trans := subs.GetTransaction()
1342 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +02001343 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001344 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001345 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001346 }
Anssi Mannila54838ed2021-11-19 11:25:01 +02001347 xapp.Logger.Debug("SUBS-SubResp: Sending event, trans= %v", trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001348 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
1349 if sendOk == false {
1350 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001351 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001352 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001353 return
kalnagy45114752019-06-18 14:40:39 +02001354}
1355
Juha Hyttinen422d0182020-01-17 13:37:05 +02001356//-------------------------------------------------------------------
1357// handle from E2T Subscription Failure
1358//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001359func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001360 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001361 c.UpdateCounter(cSubFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001362 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001363 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001364 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001365 return
1366 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001367 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001368 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001369 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001370 return
1371 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001372 trans := subs.GetTransaction()
1373 if trans == nil {
1374 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001375 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001376 return
1377 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001378 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
1379 if sendOk == false {
1380 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001381 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +02001382 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001383 return
1384}
1385
Juha Hyttinen422d0182020-01-17 13:37:05 +02001386//-------------------------------------------------------------------
1387// handle from E2T Subscription Delete Response
1388//-------------------------------------------------------------------
Anssi Mannila9c4697f2022-07-04 15:51:38 +03001389func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001390 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001391 c.UpdateCounter(cSubDelRespFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001392 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001393 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001394 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001395 return
1396 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001397 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.InstanceId})
Juha Hyttinen31797b42020-01-16 14:05:01 +02001398 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001399 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +02001400 return
1401 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001402 trans := subs.GetTransaction()
1403 if trans == nil {
1404 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001405 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001406 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001407 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001408 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
1409 if sendOk == false {
1410 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001411 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +02001412 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +02001413 return
1414}
1415
Juha Hyttinen422d0182020-01-17 13:37:05 +02001416//-------------------------------------------------------------------
1417// handle from E2T Subscription Delete Failure
1418//-------------------------------------------------------------------
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +03001419func (c *Control) handleE2TSubscriptionDeleteFailure(params *xapp.RMRParams) {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001420 xapp.Logger.Debug("MSG from E2T: %s", params.String())
Anssi Mannila4abf1802021-01-28 13:06:46 +02001421 c.UpdateCounter(cSubDelFailFromE2)
Juha Hyttinen422d0182020-01-17 13:37:05 +02001422 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +02001423 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001424 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001425 return
1426 }
Juha Hyttinenaada6452020-04-07 08:47:58 +03001427 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.InstanceId})
Anssi Mannila8046c702020-01-02 13:39:05 +02001428 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +02001429 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +02001430 return
1431 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001432 trans := subs.GetTransaction()
1433 if trans == nil {
1434 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +02001435 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +02001436 return
1437 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001438 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
1439 if sendOk == false {
1440 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +02001441 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +02001442 }
kalnagy93cc3e22019-09-19 11:29:29 +02001443 return
1444}
1445
Juha Hyttinen422d0182020-01-17 13:37:05 +02001446//-------------------------------------------------------------------
1447//
1448//-------------------------------------------------------------------
1449func typeofSubsMessage(v interface{}) string {
1450 if v == nil {
1451 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +02001452 }
Juha Hyttinen422d0182020-01-17 13:37:05 +02001453 switch v.(type) {
Anssi Mannila51122392021-05-25 11:51:31 +03001454 //case *e2ap.E2APSubscriptionRequest:
1455 // return "SubReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001456 case *e2ap.E2APSubscriptionResponse:
1457 return "SubResp"
1458 case *e2ap.E2APSubscriptionFailure:
1459 return "SubFail"
Anssi Mannila51122392021-05-25 11:51:31 +03001460 //case *e2ap.E2APSubscriptionDeleteRequest:
1461 // return "SubDelReq"
Juha Hyttinen422d0182020-01-17 13:37:05 +02001462 case *e2ap.E2APSubscriptionDeleteResponse:
1463 return "SubDelResp"
1464 case *e2ap.E2APSubscriptionDeleteFailure:
1465 return "SubDelFail"
1466 default:
1467 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +02001468 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +02001469}
Anssi Mannilac92b4212020-12-07 14:59:34 +02001470
1471//-------------------------------------------------------------------
1472//
1473//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001474func (c *Control) WriteSubscriptionToDb(subs *Subscription) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001475 xapp.Logger.Debug("WriteSubscriptionToDb() subId = %v", subs.ReqId.InstanceId)
1476 err := c.WriteSubscriptionToSdl(subs.ReqId.InstanceId, subs)
1477 if err != nil {
1478 xapp.Logger.Error("%v", err)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001479 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001480 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001481 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001482}
1483
1484//-------------------------------------------------------------------
1485//
1486//-------------------------------------------------------------------
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001487func (c *Control) UpdateSubscriptionInDB(subs *Subscription, removeSubscriptionFromDb bool) error {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001488
1489 if removeSubscriptionFromDb == true {
1490 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1491 c.RemoveSubscriptionFromDb(subs)
1492 } else {
1493 // Update is needed for successful response and merge case here
1494 if subs.RetryFromXapp == false {
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001495 err := c.WriteSubscriptionToDb(subs)
1496 return err
Anssi Mannilac92b4212020-12-07 14:59:34 +02001497 }
1498 }
1499 subs.RetryFromXapp = false
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001500 return nil
Anssi Mannilac92b4212020-12-07 14:59:34 +02001501}
1502
1503//-------------------------------------------------------------------
1504//
1505//-------------------------------------------------------------------
1506func (c *Control) RemoveSubscriptionFromDb(subs *Subscription) {
1507 xapp.Logger.Debug("RemoveSubscriptionFromDb() subId = %v", subs.ReqId.InstanceId)
1508 err := c.RemoveSubscriptionFromSdl(subs.ReqId.InstanceId)
1509 if err != nil {
1510 xapp.Logger.Error("%v", err)
1511 }
1512}
1513
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001514//-------------------------------------------------------------------
1515//
1516//-------------------------------------------------------------------
1517func (c *Control) WriteRESTSubscriptionToDb(restSubId string, restSubs *RESTSubscription) {
1518 xapp.Logger.Debug("WriteRESTSubscriptionToDb() restSubId = %s", restSubId)
1519 err := c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
1520 if err != nil {
1521 xapp.Logger.Error("%v", err)
1522 }
1523}
1524
1525//-------------------------------------------------------------------
1526//
1527//-------------------------------------------------------------------
1528func (c *Control) UpdateRESTSubscriptionInDB(restSubId string, restSubs *RESTSubscription, removeRestSubscriptionFromDb bool) {
1529
1530 if removeRestSubscriptionFromDb == true {
1531 // Subscription was written in db already when subscription request was sent to BTS, except for merged request
1532 c.RemoveRESTSubscriptionFromDb(restSubId)
1533 } else {
1534 c.WriteRESTSubscriptionToDb(restSubId, restSubs)
1535 }
1536}
1537
1538//-------------------------------------------------------------------
1539//
1540//-------------------------------------------------------------------
1541func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
1542 xapp.Logger.Debug("RemoveRESTSubscriptionFromDb() restSubId = %s", restSubId)
1543 err := c.RemoveRESTSubscriptionFromSdl(restSubId)
1544 if err != nil {
1545 xapp.Logger.Error("%v", err)
1546 }
1547}
1548
gunjarastogi4378baf2023-01-18 17:51:55 +05301549func (c *Control) SendSubscriptionDeleteReq(subs *Subscription, e2SubsDelRequired bool) {
Anssi Mannilac92b4212020-12-07 14:59:34 +02001550
Anssi Mannila54838ed2021-11-19 11:25:01 +02001551 if c.UTTesting == true {
1552 // Reqistry mutex is not locked after real restart but it can be when restart is simulated in unit tests
1553 c.registry.mutex = new(sync.Mutex)
1554 }
1555
Konstantinos Archangelof26dfa712021-06-04 12:10:26 +00001556 const ricRequestorId = 123
Anssi Mannilac92b4212020-12-07 14:59:34 +02001557 xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)
1558
1559 // Send delete for every endpoint in the subscription
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001560 if subs.PolicyUpdate == false {
1561 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
1562 subDelReqMsg.RequestId = subs.GetReqId().RequestId
1563 subDelReqMsg.RequestId.Id = ricRequestorId
1564 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
1565 mType, payload, err := c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
1566 if err != nil {
1567 xapp.Logger.Error("SendSubscriptionDeleteReq() %s", idstring(err))
1568 return
1569 }
1570 for _, endPoint := range subs.EpList.Endpoints {
1571 params := &xapp.RMRParams{}
1572 params.Mtype = mType
1573 params.SubId = int(subs.GetReqId().InstanceId)
1574 params.Xid = ""
1575 params.Meid = subs.Meid
1576 params.Src = endPoint.String()
1577 params.PayloadLen = len(payload.Buf)
1578 params.Payload = payload.Buf
1579 params.Mbuf = nil
1580 subs.DeleteFromDb = true
gunjarastogi4378baf2023-01-18 17:51:55 +05301581 if !e2SubsDelRequired {
1582 c.handleXAPPSubscriptionDeleteRequest(params)
1583 } else {
1584 c.SendSubscriptionDeleteReqToE2T(subs, params)
1585 }
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001586 }
Anssi Mannilac92b4212020-12-07 14:59:34 +02001587 }
1588}
Anssi Mannilade457a12021-06-02 16:19:48 +03001589
1590func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
1591
1592 fmt.Println("CRESTSubscriptionRequest")
Markku Virtanen987f8b12021-06-10 15:05:02 +03001593
Markku Virtanen2b512b62021-07-30 12:04:00 +00001594 if p == nil {
1595 return
1596 }
1597
Markku Virtanen987f8b12021-06-10 15:05:02 +03001598 if p.SubscriptionID != "" {
1599 fmt.Println(" SubscriptionID = ", p.SubscriptionID)
1600 } else {
1601 fmt.Println(" SubscriptionID = ''")
1602 }
1603
Anssi Mannilade457a12021-06-02 16:19:48 +03001604 fmt.Printf(" ClientEndpoint.Host = %s\n", p.ClientEndpoint.Host)
1605
1606 if p.ClientEndpoint.HTTPPort != nil {
1607 fmt.Printf(" ClientEndpoint.HTTPPort = %v\n", *p.ClientEndpoint.HTTPPort)
1608 } else {
1609 fmt.Println(" ClientEndpoint.HTTPPort = nil")
1610 }
1611
1612 if p.ClientEndpoint.RMRPort != nil {
1613 fmt.Printf(" ClientEndpoint.RMRPort = %v\n", *p.ClientEndpoint.RMRPort)
1614 } else {
1615 fmt.Println(" ClientEndpoint.RMRPort = nil")
1616 }
1617
1618 if p.Meid != nil {
1619 fmt.Printf(" Meid = %s\n", *p.Meid)
1620 } else {
1621 fmt.Println(" Meid = nil")
1622 }
1623
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001624 if p.E2SubscriptionDirectives == nil {
1625 fmt.Println(" E2SubscriptionDirectives = nil")
1626 } else {
1627 fmt.Println(" E2SubscriptionDirectives")
1628 if p.E2SubscriptionDirectives.E2RetryCount == nil {
1629 fmt.Println(" E2RetryCount == nil")
1630 } else {
1631 fmt.Printf(" E2RetryCount = %v\n", *p.E2SubscriptionDirectives.E2RetryCount)
1632 }
1633 fmt.Printf(" E2TimeoutTimerValue = %v\n", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
Anssi Mannilacb7dd492021-10-01 11:10:26 +03001634 fmt.Printf(" RMRRoutingNeeded = %v\n", p.E2SubscriptionDirectives.RMRRoutingNeeded)
Anssi Mannilaf682ace2021-09-28 13:11:25 +03001635 }
Anssi Mannilade457a12021-06-02 16:19:48 +03001636 for _, subscriptionDetail := range p.SubscriptionDetails {
1637 if p.RANFunctionID != nil {
1638 fmt.Printf(" RANFunctionID = %v\n", *p.RANFunctionID)
1639 } else {
1640 fmt.Println(" RANFunctionID = nil")
1641 }
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +00001642 fmt.Printf(" SubscriptionDetail.XappEventInstanceID = %v\n", *subscriptionDetail.XappEventInstanceID)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001643 fmt.Printf(" SubscriptionDetail.EventTriggers = %v\n", subscriptionDetail.EventTriggers)
Anssi Mannilade457a12021-06-02 16:19:48 +03001644
1645 for _, actionToBeSetup := range subscriptionDetail.ActionToBeSetupList {
1646 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionID = %v\n", *actionToBeSetup.ActionID)
1647 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionType = %s\n", *actionToBeSetup.ActionType)
Konstantinos Archangelofbd9c98e2021-06-07 17:32:10 +00001648 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.ActionDefinition = %v\n", actionToBeSetup.ActionDefinition)
Anssi Mannilade457a12021-06-02 16:19:48 +03001649
Anssi Mannilade457a12021-06-02 16:19:48 +03001650 if actionToBeSetup.SubsequentAction != nil {
1651 fmt.Printf(" SubscriptionDetail.ActionToBeSetup.SubsequentAction.SubsequentActionType = %s\n", *actionToBeSetup.SubsequentAction.SubsequentActionType)
1652 fmt.Printf(" SubscriptionDetail.ActionToBeSetup..SubsequentAction.TimeToWait = %s\n", *actionToBeSetup.SubsequentAction.TimeToWait)
1653 } else {
1654 fmt.Println(" SubscriptionDetail.ActionToBeSetup.SubsequentAction = nil")
1655 }
1656 }
1657 }
1658}
gunjarastogi4378baf2023-01-18 17:51:55 +05301659
1660//-------------------------------------------------------------------
1661// handle from E2T Subscription Delete Required
1662//-------------------------------------------------------------------
1663func (c *Control) handleE2TSubscriptionDeleteRequired(params *xapp.RMRParams) {
1664 xapp.Logger.Info("MSG from E2T: %s", params.String())
1665 c.UpdateCounter(cSubDelRequFromE2)
1666 subsDelRequMsg, err := c.e2ap.UnpackSubscriptionDeleteRequired(params.Payload)
1667 if err != nil {
1668 xapp.Logger.Error("MSG-SubDelRequired: %s", idstring(err, params))
1669 //c.sendE2TErrorIndication(nil)
1670 return
1671 }
1672 var subscriptions = map[string][]e2ap.E2APSubscriptionDeleteRequired{}
1673 var subDB = []*Subscription{}
1674 for _, subsTobeRemove := range subsDelRequMsg.E2APSubscriptionDeleteRequiredRequests {
1675 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subsTobeRemove.RequestId.InstanceId})
1676 if err != nil {
1677 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
1678 continue
1679 }
1680 // Check if Delete Subscription Already triggered
1681 if subs.OngoingDelCount > 0 {
1682 continue
1683 }
1684 subDB = append(subDB, subs)
1685 for _, endpoint := range subs.EpList.Endpoints {
1686 subscriptions[endpoint.Addr] = append(subscriptions[endpoint.Addr], subsTobeRemove)
1687 }
1688 // Sending Subscription Delete Request to E2T
1689 // c.SendSubscriptionDeleteReq(subs, true)
1690 }
1691 for _, subsTobeRemove := range subDB {
1692 // Sending Subscription Delete Request to E2T
1693 c.SendSubscriptionDeleteReq(subsTobeRemove, true)
1694 }
1695}
1696
1697//-----------------------------------------------------------------
1698// Initiate RIC Subscription Delete Request after receiving
1699// RIC Subscription Delete Required from E2T
1700//-----------------------------------------------------------------
1701func (c *Control) SendSubscriptionDeleteReqToE2T(subs *Subscription, params *xapp.RMRParams) {
1702 xapp.Logger.Debug("MSG TO E2T: %s", params.String())
1703 c.UpdateCounter(cSubDelReqToE2)
1704
1705 if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
1706 xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
1707 return
1708 }
1709
1710 trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subs.ReqId.RequestId, params.Meid)
1711 if trans == nil {
1712 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
1713 return
1714 }
1715 defer trans.Release()
1716
1717 err := c.tracker.Track(trans)
1718 if err != nil {
1719 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
1720 return
1721 }
1722
1723 //
1724 // Wake subs delete
1725 //
1726 subs.OngoingDelCount++
1727 go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
1728 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
1729 subs.OngoingDelCount--
1730
1731 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
1732
1733 if subs.NoRespToXapp == true {
1734 // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
1735 xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
1736 return
1737 }
1738}