blob: 1d64f3c64bbf17c77fd2be1235800275b732bbd2 [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"
Juha Hyttinen86a46202020-01-14 12:49:09 +020024 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
kalnagy93cc3e22019-09-19 11:29:29 +020025 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
kalnagy1455c852019-10-21 13:06:23 +020026 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27 httptransport "github.com/go-openapi/runtime/client"
28 "github.com/go-openapi/strfmt"
29 "github.com/spf13/viper"
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020030 "sync"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000031 "time"
kalnagy45114752019-06-18 14:40:39 +020032)
33
Juha Hyttinene406a342020-01-13 13:02:26 +020034//-----------------------------------------------------------------------------
35//
36//-----------------------------------------------------------------------------
37
Juha Hyttinen422d0182020-01-17 13:37:05 +020038var e2tSubReqTimeout time.Duration = 5 * time.Second
39var e2tSubDelReqTime time.Duration = 5 * time.Second
40var e2tMaxSubReqTryCount uint64 = 2 // Initial try + retry
41var e2tMaxSubDelReqTryCount uint64 = 2 // Initial try + retry
42
43var e2tRecvMsgTimeout time.Duration = 5 * time.Second
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020044
kalnagy45114752019-06-18 14:40:39 +020045type Control struct {
Juha Hyttinenc65f5b02019-12-30 14:24:04 +020046 e2ap *E2ap
47 registry *Registry
Juha Hyttinenc65f5b02019-12-30 14:24:04 +020048 tracker *Tracker
49 timerMap *TimerMap
50 rmrSendMutex sync.Mutex
Juha Hyttinen47b842b2020-01-08 13:01:52 +020051 msgCounter uint64
kalnagy93cc3e22019-09-19 11:29:29 +020052}
53
54type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020055 PlmnID string
56 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020057 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020058}
59
Peter Szilagyifbc56f92019-07-23 19:29:46 +000060func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020061 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000062 viper.AutomaticEnv()
63 viper.SetEnvPrefix("submgr")
64 viper.AllowEmptyEnv(true)
kalnagy45114752019-06-18 14:40:39 +020065}
66
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020067func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020068
Juha Hyttinen56e03832020-01-14 17:08:43 +020069 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
Juha Hyttinen12d31af2020-01-22 12:59:01 +020070 rtmgrClient := RtmgrClient{rtClient: rtmgrclient.New(transport, strfmt.Default)}
Juha Hyttinen56e03832020-01-14 17:08:43 +020071
Peter Szilagyifbc56f92019-07-23 19:29:46 +000072 registry := new(Registry)
Anssi Mannila5c161a92020-01-15 15:40:57 +020073 registry.Initialize()
Juha Hyttinen56e03832020-01-14 17:08:43 +020074 registry.rtmgrClient = &rtmgrClient
kalnagy93cc3e22019-09-19 11:29:29 +020075
Balint Uvegese9608cd2019-09-20 18:00:32 +000076 tracker := new(Tracker)
77 tracker.Init()
78
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020079 timerMap := new(TimerMap)
80 timerMap.Init()
81
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020082 return &Control{e2ap: new(E2ap),
Juha Hyttinen56e03832020-01-14 17:08:43 +020083 registry: registry,
84 tracker: tracker,
85 timerMap: timerMap,
86 msgCounter: 0,
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020087 }
kalnagy45114752019-06-18 14:40:39 +020088}
89
90func (c *Control) Run() {
Peter Szilagyifbc56f92019-07-23 19:29:46 +000091 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +020092}
93
Juha Hyttinene406a342020-01-13 13:02:26 +020094func (c *Control) rmrSendRaw(desc string, params *RMRParams) (err error) {
95
96 xapp.Logger.Info("%s: %s", desc, params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020097 status := false
98 i := 1
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020099 for ; i <= 10 && status == false; i++ {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200100 c.rmrSendMutex.Lock()
Juha Hyttinene406a342020-01-13 13:02:26 +0200101 status = xapp.Rmr.Send(params.RMRParams, false)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200102 c.rmrSendMutex.Unlock()
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200103 if status == false {
Juha Hyttinene406a342020-01-13 13:02:26 +0200104 xapp.Logger.Info("rmr.Send() failed. Retry count %d, %s", i, params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200105 time.Sleep(500 * time.Millisecond)
106 }
107 }
108 if status == false {
Juha Hyttinene406a342020-01-13 13:02:26 +0200109 err = fmt.Errorf("rmr.Send() failed. Retry count %d, %s", i, params.String())
110 xapp.Logger.Error("%s: %s", desc, err.Error())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200111 xapp.Rmr.Free(params.Mbuf)
112 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000113 return
kalnagy45114752019-06-18 14:40:39 +0200114}
115
Juha Hyttinen83ada002020-01-30 10:36:33 +0200116func (c *Control) rmrSendToE2T(desc string, subs *Subscription, trans *TransactionSubs) (err error) {
Juha Hyttinene406a342020-01-13 13:02:26 +0200117 params := &RMRParams{&xapp.RMRParams{}}
118 params.Mtype = trans.GetMtype()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200119 params.SubId = int(subs.GetReqId().Seq)
Juha Hyttinen86a46202020-01-14 12:49:09 +0200120 params.Xid = ""
Juha Hyttinene406a342020-01-13 13:02:26 +0200121 params.Meid = subs.GetMeid()
122 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200123 params.PayloadLen = len(trans.Payload.Buf)
124 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200125 params.Mbuf = nil
126
Juha Hyttinen83ada002020-01-30 10:36:33 +0200127 return c.rmrSendRaw("MSG to E2T:"+desc+":"+trans.String(), params)
kalnagye0018682019-09-26 16:28:25 +0200128}
129
Juha Hyttinen83ada002020-01-30 10:36:33 +0200130func (c *Control) rmrSendToXapp(desc string, subs *Subscription, trans *TransactionXapp) (err error) {
131
Juha Hyttinene406a342020-01-13 13:02:26 +0200132 params := &RMRParams{&xapp.RMRParams{}}
Juha Hyttinen63284a22020-01-15 10:45:11 +0200133 params.Mtype = trans.GetMtype()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200134 params.SubId = int(subs.GetReqId().Seq)
Juha Hyttinene406a342020-01-13 13:02:26 +0200135 params.Xid = trans.GetXid()
Juha Hyttinen86a46202020-01-14 12:49:09 +0200136 params.Meid = trans.GetMeid()
Juha Hyttinene406a342020-01-13 13:02:26 +0200137 params.Src = ""
Juha Hyttinen63284a22020-01-15 10:45:11 +0200138 params.PayloadLen = len(trans.Payload.Buf)
139 params.Payload = trans.Payload.Buf
Juha Hyttinene406a342020-01-13 13:02:26 +0200140 params.Mbuf = nil
141
Juha Hyttinen83ada002020-01-30 10:36:33 +0200142 return c.rmrSendRaw("MSG to XAPP:"+desc+":"+trans.String(), params)
Juha Hyttinene406a342020-01-13 13:02:26 +0200143}
144
145func (c *Control) Consume(params *xapp.RMRParams) (err error) {
146 xapp.Rmr.Free(params.Mbuf)
147 params.Mbuf = nil
Juha Hyttinene406a342020-01-13 13:02:26 +0200148 msg := &RMRParams{params}
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200149 c.msgCounter++
Juha Hyttinen83ada002020-01-30 10:36:33 +0200150
Anssi Mannila90fa0212019-12-12 10:47:47 +0200151 switch msg.Mtype {
152 case xapp.RICMessageTypes["RIC_SUB_REQ"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200153 go c.handleXAPPSubscriptionRequest(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200154 case xapp.RICMessageTypes["RIC_SUB_RESP"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200155 go c.handleE2TSubscriptionResponse(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200156 case xapp.RICMessageTypes["RIC_SUB_FAILURE"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200157 go c.handleE2TSubscriptionFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200158 case xapp.RICMessageTypes["RIC_SUB_DEL_REQ"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200159 go c.handleXAPPSubscriptionDeleteRequest(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200160 case xapp.RICMessageTypes["RIC_SUB_DEL_RESP"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200161 go c.handleE2TSubscriptionDeleteResponse(msg)
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200162 case xapp.RICMessageTypes["RIC_SUB_DEL_FAILURE"]:
Juha Hyttinen422d0182020-01-17 13:37:05 +0200163 go c.handleE2TSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200164 default:
165 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000166 }
Juha Hyttinen86a46202020-01-14 12:49:09 +0200167
Anssi Mannila90fa0212019-12-12 10:47:47 +0200168 return nil
Balint Uvegescd3881b2019-10-02 15:01:43 +0000169}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200170
171func idstring(err error, entries ...fmt.Stringer) string {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200172 var retval string = ""
173 var filler string = ""
Juha Hyttinen83ada002020-01-30 10:36:33 +0200174 for _, entry := range entries {
175 retval += filler + entry.String()
Juha Hyttinen31797b42020-01-16 14:05:01 +0200176 filler = " "
177 }
178 if err != nil {
179 retval += filler + "err(" + err.Error() + ")"
180 filler = " "
Juha Hyttinen422d0182020-01-17 13:37:05 +0200181
Juha Hyttinen31797b42020-01-16 14:05:01 +0200182 }
183 return retval
184}
185
Juha Hyttinen422d0182020-01-17 13:37:05 +0200186//-------------------------------------------------------------------
187// handle from XAPP Subscription Request
188//------------------------------------------------------------------
189func (c *Control) handleXAPPSubscriptionRequest(params *RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200190 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200191
192 subReqMsg, err := c.e2ap.UnpackSubscriptionRequest(params.Payload)
193 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200194 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200195 return
196 }
197
Juha Hyttinen83ada002020-01-30 10:36:33 +0200198 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(params.Src), params.Xid, &RequestId{subReqMsg.RequestId}, params.Meid)
199 if trans == nil {
200 xapp.Logger.Error("XAPP-SubReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200201 return
202 }
203 defer trans.Release()
204
Juha Hyttinen83ada002020-01-30 10:36:33 +0200205 err = c.tracker.Track(trans)
206 if err != nil {
207 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
208 return
209 }
210
Juha Hyttinen422d0182020-01-17 13:37:05 +0200211 subs, err := c.registry.AssignToSubscription(trans, subReqMsg)
212 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200213 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200214 return
215 }
216
Juha Hyttinen422d0182020-01-17 13:37:05 +0200217 //
218 // Wake subs request
219 //
220 go c.handleSubscriptionCreate(subs, trans)
221 event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
222
223 err = nil
224 if event != nil {
225 switch themsg := event.(type) {
226 case *e2ap.E2APSubscriptionResponse:
227 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionResponse(themsg)
228 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200229 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200230 return
231 }
232 case *e2ap.E2APSubscriptionFailure:
233 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionFailure(themsg)
234 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200235 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200236 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200237 default:
Juha Hyttinen31797b42020-01-16 14:05:01 +0200238 break
239 }
240 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200241 xapp.Logger.Info("XAPP-SubReq: failed %s", idstring(err, trans, subs))
242 go c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200243}
244
Juha Hyttinen422d0182020-01-17 13:37:05 +0200245//-------------------------------------------------------------------
246// handle from XAPP Subscription Delete Request
247//------------------------------------------------------------------
248func (c *Control) handleXAPPSubscriptionDeleteRequest(params *RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200249 xapp.Logger.Info("MSG from XAPP: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200250
251 subDelReqMsg, err := c.e2ap.UnpackSubscriptionDeleteRequest(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200252 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200253 xapp.Logger.Error("XAPP-SubDelReq %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200254 return
255 }
256
Juha Hyttinen83ada002020-01-30 10:36:33 +0200257 trans := c.tracker.NewXappTransaction(NewRmrEndpoint(params.Src), params.Xid, &RequestId{subDelReqMsg.RequestId}, params.Meid)
258 if trans == nil {
259 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200260 return
261 }
262 defer trans.Release()
263
Juha Hyttinen83ada002020-01-30 10:36:33 +0200264 err = c.tracker.Track(trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200265 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200266 xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
267 return
268 }
269
270 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelReqMsg.RequestId.Seq})
271 if err != nil {
272 xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(err, trans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200273 return
274 }
275
Juha Hyttinen422d0182020-01-17 13:37:05 +0200276 //
277 // Wake subs delete
278 //
279 go c.handleSubscriptionDelete(subs, trans)
280 trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
281
Juha Hyttinen83ada002020-01-30 10:36:33 +0200282 xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
283
Juha Hyttinen422d0182020-01-17 13:37:05 +0200284 // Whatever is received send ok delete response
285 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200286 subDelRespMsg.RequestId = subs.SubReqMsg.RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200287 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
288 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
289 if err == nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200290 c.rmrSendToXapp("", subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200291 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200292
293 go c.registry.RemoveFromSubscription(subs, trans, 5*time.Second)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200294}
295
296//-------------------------------------------------------------------
297// SUBS CREATE Handling
298//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200299func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200300
Juha Hyttinen83ada002020-01-30 10:36:33 +0200301 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200302 subs.WaitTransactionTurn(trans)
303 defer subs.ReleaseTransactionTurn(trans)
304 defer trans.Release()
305
Juha Hyttinen83ada002020-01-30 10:36:33 +0200306 xapp.Logger.Debug("SUBS-SubReq: Handling %s ", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200307
Juha Hyttinen83ada002020-01-30 10:36:33 +0200308 subRfMsg, valid := subs.GetCachedResponse()
309 if subRfMsg == nil && valid == true {
310 event := c.sendE2TSubscriptionRequest(subs, trans, parentTrans)
311 switch event.(type) {
312 case *e2ap.E2APSubscriptionResponse:
313 subRfMsg, valid = subs.SetCachedResponse(event, true)
314 case *e2ap.E2APSubscriptionFailure:
315 subRfMsg, valid = subs.SetCachedResponse(event, false)
316 default:
317 xapp.Logger.Info("SUBS-SubReq: internal delete due event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
318 subRfMsg, valid = subs.SetCachedResponse(nil, false)
319 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
320 }
321 xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
322 } else {
323 xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200324 }
325
Juha Hyttinen83ada002020-01-30 10:36:33 +0200326 parentTrans.SendEvent(subRfMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200327}
328
329//-------------------------------------------------------------------
330// SUBS DELETE Handling
331//-------------------------------------------------------------------
Juha Hyttinen3944a222020-01-24 11:51:46 +0200332
Juha Hyttinen83ada002020-01-30 10:36:33 +0200333func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200334
Juha Hyttinen83ada002020-01-30 10:36:33 +0200335 trans := c.tracker.NewSubsTransaction(subs)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200336 subs.WaitTransactionTurn(trans)
337 defer subs.ReleaseTransactionTurn(trans)
338 defer trans.Release()
339
Juha Hyttinen83ada002020-01-30 10:36:33 +0200340 xapp.Logger.Debug("SUBS-SubDelReq: Handling %s", idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200341
Juha Hyttinen3944a222020-01-24 11:51:46 +0200342 subs.mutex.Lock()
343 if subs.valid && subs.EpList.HasEndpoint(parentTrans.GetEndpoint()) && subs.EpList.Size() == 1 {
344 subs.valid = false
345 subs.mutex.Unlock()
346 c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
347 } else {
348 subs.mutex.Unlock()
349 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200350
Juha Hyttinen3944a222020-01-24 11:51:46 +0200351 subDelRespMsg := &e2ap.E2APSubscriptionDeleteResponse{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200352 subDelRespMsg.RequestId = subs.SubReqMsg.RequestId
Juha Hyttinen3944a222020-01-24 11:51:46 +0200353 subDelRespMsg.FunctionId = subs.SubReqMsg.FunctionId
354 parentTrans.SendEvent(subDelRespMsg, 0)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200355}
356
357//-------------------------------------------------------------------
358// send to E2T Subscription Request
359//-------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200360func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200361 var err error
362 var event interface{} = nil
363 var timedOut bool = false
364
365 subReqMsg := subs.SubReqMsg
Juha Hyttinen83ada002020-01-30 10:36:33 +0200366 subReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200367 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionRequest(subReqMsg)
368 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200369 xapp.Logger.Error("SUBS-SubReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200370 return event
371 }
372
373 for retries := uint64(0); retries < e2tMaxSubReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200374 desc := fmt.Sprintf("(retry %d)", retries)
375 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200376 event, timedOut = trans.WaitEvent(e2tSubReqTimeout)
377 if timedOut {
378 continue
379 }
380 break
381 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200382 xapp.Logger.Debug("SUBS-SubReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200383 return event
384}
385
386//-------------------------------------------------------------------
387// send to E2T Subscription Delete Request
388//-------------------------------------------------------------------
389
Juha Hyttinen83ada002020-01-30 10:36:33 +0200390func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *TransactionSubs, parentTrans *TransactionXapp) interface{} {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200391 var err error
392 var event interface{}
393 var timedOut bool
394
395 subDelReqMsg := &e2ap.E2APSubscriptionDeleteRequest{}
Juha Hyttinen83ada002020-01-30 10:36:33 +0200396 subDelReqMsg.RequestId = subs.GetReqId().RequestId
Juha Hyttinenaf91f972020-01-24 08:38:47 +0200397 subDelReqMsg.FunctionId = subs.SubReqMsg.FunctionId
Juha Hyttinen422d0182020-01-17 13:37:05 +0200398 trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteRequest(subDelReqMsg)
399 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200400 xapp.Logger.Error("SUBS-SubDelReq: %s", idstring(err, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200401 return event
402 }
403
404 for retries := uint64(0); retries < e2tMaxSubDelReqTryCount; retries++ {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200405 desc := fmt.Sprintf("(retry %d)", retries)
406 c.rmrSendToE2T(desc, subs, trans)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200407 event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
408 if timedOut {
409 continue
410 }
411 break
412 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200413 xapp.Logger.Debug("SUBS-SubDelReq: Response handling event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200414 return event
415}
416
417//-------------------------------------------------------------------
418// handle from E2T Subscription Reponse
419//-------------------------------------------------------------------
420func (c *Control) handleE2TSubscriptionResponse(params *RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200421 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200422 subRespMsg, err := c.e2ap.UnpackSubscriptionResponse(params.Payload)
423 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200424 xapp.Logger.Error("MSG-SubResp %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200425 return
426 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200427 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subRespMsg.RequestId.Seq})
Juha Hyttinen422d0182020-01-17 13:37:05 +0200428 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200429 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params))
Juha Hyttinen422d0182020-01-17 13:37:05 +0200430 return
Juha Hyttinen31797b42020-01-16 14:05:01 +0200431 }
432 trans := subs.GetTransaction()
433 if trans == nil {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200434 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200435 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200436 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200437 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200438 sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
439 if sendOk == false {
440 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200441 xapp.Logger.Error("MSG-SubResp: %s", idstring(err, trans, subs))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000442 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000443 return
kalnagy45114752019-06-18 14:40:39 +0200444}
445
Juha Hyttinen422d0182020-01-17 13:37:05 +0200446//-------------------------------------------------------------------
447// handle from E2T Subscription Failure
448//-------------------------------------------------------------------
449func (c *Control) handleE2TSubscriptionFailure(params *RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200450 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200451 subFailMsg, err := c.e2ap.UnpackSubscriptionFailure(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000452 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200453 xapp.Logger.Error("MSG-SubFail %s", idstring(err, params))
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000454 return
455 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200456 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subFailMsg.RequestId.Seq})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200457 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200458 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200459 return
460 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200461 trans := subs.GetTransaction()
462 if trans == nil {
463 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200464 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200465 return
466 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200467 sendOk, timedOut := trans.SendEvent(subFailMsg, e2tRecvMsgTimeout)
468 if sendOk == false {
469 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200470 xapp.Logger.Error("MSG-SubFail: %s", idstring(err, trans, subs))
Juha Hyttinenaafee7f2020-01-14 14:54:51 +0200471 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200472 return
473}
474
Juha Hyttinen422d0182020-01-17 13:37:05 +0200475//-------------------------------------------------------------------
476// handle from E2T Subscription Delete Response
477//-------------------------------------------------------------------
478func (c *Control) handleE2TSubscriptionDeleteResponse(params *RMRParams) (err error) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200479 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200480 subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200481 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200482 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200483 return
484 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200485 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelRespMsg.RequestId.Seq})
Juha Hyttinen31797b42020-01-16 14:05:01 +0200486 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200487 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200488 return
489 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200490 trans := subs.GetTransaction()
491 if trans == nil {
492 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200493 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, params, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200494 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200495 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200496 sendOk, timedOut := trans.SendEvent(subDelRespMsg, e2tRecvMsgTimeout)
497 if sendOk == false {
498 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200499 xapp.Logger.Error("MSG-SubDelResp: %s", idstring(err, trans, subs))
Juha Hyttinen60bfcf92020-01-14 15:14:24 +0200500 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200501 return
502}
503
Juha Hyttinen422d0182020-01-17 13:37:05 +0200504//-------------------------------------------------------------------
505// handle from E2T Subscription Delete Failure
506//-------------------------------------------------------------------
507func (c *Control) handleE2TSubscriptionDeleteFailure(params *RMRParams) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200508 xapp.Logger.Info("MSG from E2T: %s", params.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +0200509 subDelFailMsg, err := c.e2ap.UnpackSubscriptionDeleteFailure(params.Payload)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200510 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200511 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200512 return
513 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200514 subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subDelFailMsg.RequestId.Seq})
Anssi Mannila8046c702020-01-02 13:39:05 +0200515 if err != nil {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200516 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
Anssi Mannila8046c702020-01-02 13:39:05 +0200517 return
518 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200519 trans := subs.GetTransaction()
520 if trans == nil {
521 err = fmt.Errorf("Ongoing transaction not found")
Juha Hyttinen83ada002020-01-30 10:36:33 +0200522 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params, subs))
Juha Hyttinen86a46202020-01-14 12:49:09 +0200523 return
524 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200525 sendOk, timedOut := trans.SendEvent(subDelFailMsg, e2tRecvMsgTimeout)
526 if sendOk == false {
527 err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200528 xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, trans, subs))
Anssi Mannila8046c702020-01-02 13:39:05 +0200529 }
kalnagy93cc3e22019-09-19 11:29:29 +0200530 return
531}
532
Juha Hyttinen422d0182020-01-17 13:37:05 +0200533//-------------------------------------------------------------------
534//
535//-------------------------------------------------------------------
536func typeofSubsMessage(v interface{}) string {
537 if v == nil {
538 return "NIL"
kalnagy93cc3e22019-09-19 11:29:29 +0200539 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200540 switch v.(type) {
541 case *e2ap.E2APSubscriptionRequest:
542 return "SubReq"
543 case *e2ap.E2APSubscriptionResponse:
544 return "SubResp"
545 case *e2ap.E2APSubscriptionFailure:
546 return "SubFail"
547 case *e2ap.E2APSubscriptionDeleteRequest:
548 return "SubDelReq"
549 case *e2ap.E2APSubscriptionDeleteResponse:
550 return "SubDelResp"
551 case *e2ap.E2APSubscriptionDeleteFailure:
552 return "SubDelFail"
553 default:
554 return "Unknown"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200555 }
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200556}