blob: 18eeb4c0b9ec77a3ed3551bb5acedede7b0717a3 [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"
24 //"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
kalnagy93cc3e22019-09-19 11:29:29 +020025 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
26 rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
kalnagy1455c852019-10-21 13:06:23 +020027 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28 httptransport "github.com/go-openapi/runtime/client"
29 "github.com/go-openapi/strfmt"
30 "github.com/spf13/viper"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000031 "math/rand"
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020032 "sync"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000033 "time"
kalnagy45114752019-06-18 14:40:39 +020034)
35
Juha Hyttinene406a342020-01-13 13:02:26 +020036//-----------------------------------------------------------------------------
37//
38//-----------------------------------------------------------------------------
39
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +020040var subReqTime time.Duration = 5 * time.Second
Anssi Mannila8046c702020-01-02 13:39:05 +020041var subDelReqTime time.Duration = 5 * time.Second
42var maxSubReqTryCount uint64 = 2 // Initial try + retry
43var maxSubDelReqTryCount uint64 = 2 // Initial try + retry
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
48 rtmgrClient *RtmgrClient
49 tracker *Tracker
50 timerMap *TimerMap
51 rmrSendMutex sync.Mutex
Juha Hyttinen47b842b2020-01-08 13:01:52 +020052 msgCounter uint64
kalnagy93cc3e22019-09-19 11:29:29 +020053}
54
55type RMRMeid struct {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020056 PlmnID string
57 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020058 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020059}
60
kalnagy1455c852019-10-21 13:06:23 +020061var seedSN uint16
kalnagy93cc3e22019-09-19 11:29:29 +020062
63const (
64 CREATE Action = 0
kalnagy1455c852019-10-21 13:06:23 +020065 MERGE Action = 1
Anssi Mannila90fa0212019-12-12 10:47:47 +020066 NONE Action = 2
kalnagy93cc3e22019-09-19 11:29:29 +020067 DELETE Action = 3
68)
Peter Szilagyifbc56f92019-07-23 19:29:46 +000069
70func init() {
Anssi Mannila8046c702020-01-02 13:39:05 +020071 xapp.Logger.Info("SUBMGR")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000072 viper.AutomaticEnv()
73 viper.SetEnvPrefix("submgr")
74 viper.AllowEmptyEnv(true)
kalnagy1455c852019-10-21 13:06:23 +020075 seedSN = uint16(viper.GetInt("seed_sn"))
76 if seedSN == 0 {
Peter Szilagyifbc56f92019-07-23 19:29:46 +000077 rand.Seed(time.Now().UnixNano())
kalnagy1455c852019-10-21 13:06:23 +020078 seedSN = uint16(rand.Intn(65535))
Peter Szilagyifbc56f92019-07-23 19:29:46 +000079 }
kalnagy1455c852019-10-21 13:06:23 +020080 if seedSN > 65535 {
81 seedSN = 0
Peter Szilagyifbc56f92019-07-23 19:29:46 +000082 }
kalnagy1455c852019-10-21 13:06:23 +020083 xapp.Logger.Info("SUBMGR: Initial Sequence Number: %v", seedSN)
kalnagy45114752019-06-18 14:40:39 +020084}
85
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020086func NewControl() *Control {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020087
Peter Szilagyifbc56f92019-07-23 19:29:46 +000088 registry := new(Registry)
kalnagy1455c852019-10-21 13:06:23 +020089 registry.Initialize(seedSN)
kalnagy93cc3e22019-09-19 11:29:29 +020090
Balint Uvegese9608cd2019-09-20 18:00:32 +000091 tracker := new(Tracker)
92 tracker.Init()
93
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020094 timerMap := new(TimerMap)
95 timerMap.Init()
96
kalnagy1455c852019-10-21 13:06:23 +020097 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
kalnagy93cc3e22019-09-19 11:29:29 +020098 client := rtmgrclient.New(transport, strfmt.Default)
99 handle := rtmgrhandle.NewProvideXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
kalnagy1455c852019-10-21 13:06:23 +0200100 deleteHandle := rtmgrhandle.NewDeleteXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
101 rtmgrClient := RtmgrClient{client, handle, deleteHandle}
kalnagy93cc3e22019-09-19 11:29:29 +0200102
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200103 rtmgrClientPtr := &rtmgrClient
104
105 //TODO: to make this better. Now it is just a hack.
106 registry.rtmgrClient = rtmgrClientPtr
107
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200108 return &Control{e2ap: new(E2ap),
Juha Hyttinenc65f5b02019-12-30 14:24:04 +0200109 registry: registry,
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200110 rtmgrClient: rtmgrClientPtr,
Juha Hyttinenc65f5b02019-12-30 14:24:04 +0200111 tracker: tracker,
112 timerMap: timerMap,
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200113 msgCounter: 0,
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200114 }
kalnagy45114752019-06-18 14:40:39 +0200115}
116
117func (c *Control) Run() {
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000118 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200119}
120
Juha Hyttinene406a342020-01-13 13:02:26 +0200121func (c *Control) rmrSendRaw(desc string, params *RMRParams) (err error) {
122
123 xapp.Logger.Info("%s: %s", desc, params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200124 status := false
125 i := 1
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200126 for ; i <= 10 && status == false; i++ {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200127 c.rmrSendMutex.Lock()
Juha Hyttinene406a342020-01-13 13:02:26 +0200128 status = xapp.Rmr.Send(params.RMRParams, false)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200129 c.rmrSendMutex.Unlock()
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200130 if status == false {
Juha Hyttinene406a342020-01-13 13:02:26 +0200131 xapp.Logger.Info("rmr.Send() failed. Retry count %d, %s", i, params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200132 time.Sleep(500 * time.Millisecond)
133 }
134 }
135 if status == false {
Juha Hyttinene406a342020-01-13 13:02:26 +0200136 err = fmt.Errorf("rmr.Send() failed. Retry count %d, %s", i, params.String())
137 xapp.Logger.Error("%s: %s", desc, err.Error())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200138 xapp.Rmr.Free(params.Mbuf)
139 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000140 return
kalnagy45114752019-06-18 14:40:39 +0200141}
142
Juha Hyttinene406a342020-01-13 13:02:26 +0200143func (c *Control) rmrSend(desc string, subs *Subscription, trans *Transaction, payload []byte, payloadLen int) (err error) {
144 params := &RMRParams{&xapp.RMRParams{}}
145 params.Mtype = trans.GetMtype()
146 params.SubId = int(subs.GetSubId())
147 params.Xid = trans.GetXid()
148 params.Meid = subs.GetMeid()
149 params.Src = ""
150 params.PayloadLen = payloadLen
151 params.Payload = payload
152 params.Mbuf = nil
153
154 return c.rmrSendRaw(desc, params)
kalnagye0018682019-09-26 16:28:25 +0200155}
156
Juha Hyttinene406a342020-01-13 13:02:26 +0200157func (c *Control) rmrReplyToSender(desc string, subs *Subscription, trans *Transaction, mType int, payload []byte, payloadLen int) (err error) {
158 params := &RMRParams{&xapp.RMRParams{}}
159 params.Mtype = mType
160 params.SubId = int(subs.GetSubId())
161 params.Xid = trans.GetXid()
162 params.Meid = subs.GetMeid()
163 params.Src = ""
164 params.PayloadLen = payloadLen
165 params.Payload = payload
166 params.Mbuf = nil
167
168 return c.rmrSendRaw(desc, params)
169}
170
171func (c *Control) Consume(params *xapp.RMRParams) (err error) {
172 xapp.Rmr.Free(params.Mbuf)
173 params.Mbuf = nil
174
175 msg := &RMRParams{params}
176
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200177 c.msgCounter++
Juha Hyttinene406a342020-01-13 13:02:26 +0200178
Anssi Mannila90fa0212019-12-12 10:47:47 +0200179 switch msg.Mtype {
180 case xapp.RICMessageTypes["RIC_SUB_REQ"]:
181 go c.handleSubscriptionRequest(msg)
182 case xapp.RICMessageTypes["RIC_SUB_RESP"]:
183 go c.handleSubscriptionResponse(msg)
184 case xapp.RICMessageTypes["RIC_SUB_FAILURE"]:
185 go c.handleSubscriptionFailure(msg)
186 case xapp.RICMessageTypes["RIC_SUB_DEL_REQ"]:
187 go c.handleSubscriptionDeleteRequest(msg)
188 case xapp.RICMessageTypes["RIC_SUB_DEL_RESP"]:
189 go c.handleSubscriptionDeleteResponse(msg)
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200190 case xapp.RICMessageTypes["RIC_SUB_DEL_FAILURE"]:
191 go c.handleSubscriptionDeleteFailure(msg)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200192 default:
193 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000194 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200195 return nil
Balint Uvegescd3881b2019-10-02 15:01:43 +0000196}
197
Juha Hyttinene406a342020-01-13 13:02:26 +0200198func (c *Control) handleSubscriptionRequest(params *RMRParams) {
199 xapp.Logger.Info("SubReq from xapp: %s", params.String())
kalnagy93cc3e22019-09-19 11:29:29 +0200200
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200201 srcAddr, srcPort, err := c.rtmgrClient.SplitSource(params.Src)
202 if err != nil {
203 xapp.Logger.Error("SubReq: Failed to update routing-manager. Dropping this msg. Err: %s, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
204 return
205 }
206
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200207 subs, err := c.registry.ReserveSubscription(RmrEndpoint{*srcAddr, *srcPort}, params.Meid)
208 if err != nil {
209 xapp.Logger.Error("SubReq: %s, Dropping this msg.", err.Error())
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200210 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200211 }
kalnagy93cc3e22019-09-19 11:29:29 +0200212
Juha Hyttinene406a342020-01-13 13:02:26 +0200213 //
214 // WIP RICPLT-2979
215 //
216 /*
217 e2SubReq := packerif.NewPackerSubscriptionRequest()
218 packedData := &packer.PackedData{}
219 packedData.Buf = params.Payload
220 err = e2SubReq.UnPack(packedData)
221 if err != nil {
222 xapp.Logger.Error("SubReq: UnPack() failed: %s", err.Error())
223 }
224 getErr, subReq := e2SubReq.Get()
225 if getErr != nil {
226 xapp.Logger.Error("SubReq: Get() failed: %s", err.Error())
227 }
228
229
230 subReq.RequestId.Seq = uint32(subs.GetSubId())
231
232 err = e2SubReq.Set(subReq)
233 if err != nil {
234 xapp.Logger.Error("SubReq: Set() failed: %s", err.Error())
235 return
236 }
237 err, packedData = e2SubReq.Pack(nil)
238 if err != nil {
239 xapp.Logger.Error("SubReq: Pack() failed: %s", err.Error())
240 return
241 }
242
243 params.PayloadLen = len(packedData.Buf)
244 params.Payload = packedData.Buf
245 */
246 //
247 //
248 //
249
250 params.SubId = int(subs.GetSubId())
251 err = c.e2ap.SetSubscriptionRequestSequenceNumber(params.Payload, subs.GetSubId())
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000252 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200253 xapp.Logger.Error("SubReq: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, %s", err, params.String())
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200254 c.registry.DelSubscription(subs.Seq)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000255 return
256 }
kalnagy93cc3e22019-09-19 11:29:29 +0200257
Anssi Mannila8046c702020-01-02 13:39:05 +0200258 // Create transatcion record for every subscription request
259 var forwardRespToXapp bool = true
260 var responseReceived bool = false
Juha Hyttinene406a342020-01-13 13:02:26 +0200261 trans, err := c.tracker.TrackTransaction(RmrEndpoint{*srcAddr, *srcPort}, params, responseReceived, forwardRespToXapp)
kalnagy93cc3e22019-09-19 11:29:29 +0200262 if err != nil {
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200263 xapp.Logger.Error("SubReq: %s, Dropping this msg.", err.Error())
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200264 c.registry.DelSubscription(subs.Seq)
Juha Hyttinenc65f5b02019-12-30 14:24:04 +0200265 return
Anssi Mannila90fa0212019-12-12 10:47:47 +0200266 }
kalnagy93cc3e22019-09-19 11:29:29 +0200267
Juha Hyttinene406a342020-01-13 13:02:26 +0200268 err = subs.SetTransaction(trans)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200269 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200270 xapp.Logger.Error("SubReq: %s, Dropping this msg.", err.Error())
271 c.registry.DelSubscription(subs.Seq)
272 trans.Release()
273 return
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200274 }
Juha Hyttinene406a342020-01-13 13:02:26 +0200275
276 c.rmrSend("SubReq to E2T", subs, trans, params.Payload, params.PayloadLen)
277
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200278 c.timerMap.StartTimer("RIC_SUB_REQ", int(subs.Seq), subReqTime, FirstTry, c.handleSubscriptionRequestTimer)
Juha Hyttinene406a342020-01-13 13:02:26 +0200279 xapp.Logger.Debug("SubReq: Debugging trans table = %v", c.tracker.transactionXappTable)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000280 return
kalnagy45114752019-06-18 14:40:39 +0200281}
282
Juha Hyttinene406a342020-01-13 13:02:26 +0200283func (c *Control) handleSubscriptionResponse(params *RMRParams) {
284 xapp.Logger.Info("SubResp from E2T: %s", params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200285
kalnagy1455c852019-10-21 13:06:23 +0200286 payloadSeqNum, err := c.e2ap.GetSubscriptionResponseSequenceNumber(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000287 if err != nil {
Anssi Mannila8046c702020-01-02 13:39:05 +0200288 xapp.Logger.Error("SubResp: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000289 return
290 }
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200291 xapp.Logger.Info("SubResp: Received payloadSeqNum: %v", payloadSeqNum)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200292
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200293 subs := c.registry.GetSubscription(payloadSeqNum)
294 if subs == nil {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200295 xapp.Logger.Error("SubResp: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000296 return
297 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200298
Juha Hyttinene406a342020-01-13 13:02:26 +0200299 trans := subs.GetTransaction()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200300
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200301 c.timerMap.StopTimer("RIC_SUB_REQ", int(payloadSeqNum))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200302
Juha Hyttinene406a342020-01-13 13:02:26 +0200303 responseReceived := trans.CheckResponseReceived()
Anssi Mannila8046c702020-01-02 13:39:05 +0200304 if responseReceived == true {
305 // Subscription timer already received
306 return
307 }
Anssi Mannila8046c702020-01-02 13:39:05 +0200308
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200309 subs.Confirmed()
Juha Hyttinene406a342020-01-13 13:02:26 +0200310 trans.Release()
311 c.rmrReplyToSender("SubResp to xapp", subs, trans, params.Mtype, params.Payload, params.PayloadLen)
312 xapp.Logger.Info("SubResp: SubId: %v, from address: %s. Deleting trans record", payloadSeqNum, trans.RmrEndpoint)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200313 return
314}
315
Juha Hyttinene406a342020-01-13 13:02:26 +0200316func (c *Control) handleSubscriptionFailure(params *RMRParams) {
317 xapp.Logger.Info("SubFail from E2T: %s", params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200318
319 payloadSeqNum, err := c.e2ap.GetSubscriptionFailureSequenceNumber(params.Payload)
320 if err != nil {
Anssi Mannila8046c702020-01-02 13:39:05 +0200321 xapp.Logger.Error("SubFail: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200322 return
323 }
Anssi Mannila26c922a2019-12-17 11:18:08 +0200324 xapp.Logger.Info("SubFail: Received payloadSeqNum: %v", payloadSeqNum)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200325
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200326 subs := c.registry.GetSubscription(payloadSeqNum)
327 if subs == nil {
328 xapp.Logger.Error("SubFail: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
329 return
330 }
331
Juha Hyttinene406a342020-01-13 13:02:26 +0200332 trans := subs.GetTransaction()
333 if trans == nil {
334 xapp.Logger.Error("SubFail: Unknown trans. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200335 return
336 }
337
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200338 c.timerMap.StopTimer("RIC_SUB_REQ", int(payloadSeqNum))
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200339
Juha Hyttinene406a342020-01-13 13:02:26 +0200340 responseReceived := trans.CheckResponseReceived()
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200341 if err != nil {
Anssi Mannila8046c702020-01-02 13:39:05 +0200342 xapp.Logger.Info("SubFail: Dropping this msg. Err: %v SubId: %v", err, payloadSeqNum)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200343 return
344 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200345
Anssi Mannila8046c702020-01-02 13:39:05 +0200346 if responseReceived == true {
347 // Subscription timer already received
348 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200349 }
Juha Hyttinene406a342020-01-13 13:02:26 +0200350 xapp.Logger.Info("SubFail: SubId: %v, from address: %s. Forwarding response to xApp", payloadSeqNum, trans.RmrEndpoint)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200351
Juha Hyttinene406a342020-01-13 13:02:26 +0200352 c.rmrReplyToSender("SubFail to xapp", subs, trans, params.Mtype, params.Payload, params.PayloadLen)
Anssi Mannilaa189c862020-01-10 11:36:35 +0200353
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200354 time.Sleep(3 * time.Second)
355
Juha Hyttinene406a342020-01-13 13:02:26 +0200356 xapp.Logger.Info("SubFail: Deleting trans record. SubId: %v, Xid: %s", params.SubId, params.Xid)
357 trans.Release()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200358 if !c.registry.DelSubscription(payloadSeqNum) {
Anssi Mannila26c922a2019-12-17 11:18:08 +0200359 xapp.Logger.Error("SubFail: Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200360 }
361 return
362}
363
Anssi Mannila8046c702020-01-02 13:39:05 +0200364func (c *Control) handleSubscriptionRequestTimer(strId string, nbrId int, tryCount uint64) {
Juha Hyttinene406a342020-01-13 13:02:26 +0200365 xapp.Logger.Info("SubReq timeout: subId: %v, tryCount: %v", nbrId, tryCount)
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200366
Juha Hyttinene406a342020-01-13 13:02:26 +0200367 subs := c.registry.GetSubscription(uint16(nbrId))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200368 if subs == nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200369 xapp.Logger.Error("SubReq timeout: Unknown payloadSeqNum. Dropping this msg. SubId: %v", nbrId)
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200370 return
371 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200372
Juha Hyttinene406a342020-01-13 13:02:26 +0200373 trans := subs.GetTransaction()
374 if trans == nil {
375 xapp.Logger.Error("SubReq timeout: Unknown trans. Dropping this msg. SubId: %v", subs.GetSubId())
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200376 return
377 }
378
Juha Hyttinene406a342020-01-13 13:02:26 +0200379 responseReceived := trans.CheckResponseReceived()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200380
Anssi Mannila8046c702020-01-02 13:39:05 +0200381 if responseReceived == true {
382 // Subscription Response or Failure already received
383 return
384 }
385
386 if tryCount < maxSubReqTryCount {
Juha Hyttinene406a342020-01-13 13:02:26 +0200387 xapp.Logger.Info("SubReq timeout: Resending SubReq to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v", trans.OrigParams.Mtype, subs.GetSubId(), trans.GetXid(), subs.GetMeid())
Anssi Mannila8046c702020-01-02 13:39:05 +0200388
Juha Hyttinene406a342020-01-13 13:02:26 +0200389 trans.RetryTransaction()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200390
Juha Hyttinene406a342020-01-13 13:02:26 +0200391 c.rmrSend("SubReq(SubReq timer) to E2T", subs, trans, trans.OrigParams.Payload, trans.OrigParams.PayloadLen)
Anssi Mannila8046c702020-01-02 13:39:05 +0200392
393 tryCount++
Juha Hyttinene406a342020-01-13 13:02:26 +0200394 c.timerMap.StartTimer("RIC_SUB_REQ", int(subs.GetSubId()), subReqTime, tryCount, c.handleSubscriptionRequestTimer)
Anssi Mannila8046c702020-01-02 13:39:05 +0200395 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200396 }
Anssi Mannila8046c702020-01-02 13:39:05 +0200397
398 var subDelReqPayload []byte
Juha Hyttinene406a342020-01-13 13:02:26 +0200399 subDelReqPayload, err := c.e2ap.PackSubscriptionDeleteRequest(trans.OrigParams.Payload, subs.GetSubId())
Anssi Mannila8046c702020-01-02 13:39:05 +0200400 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200401 xapp.Logger.Error("SubReq timeout: Packing SubDelReq failed. Err: %v", err)
Anssi Mannila8046c702020-01-02 13:39:05 +0200402 return
403 }
404
405 // Cancel failed subscription
Juha Hyttinene406a342020-01-13 13:02:26 +0200406 params := &RMRParams{&xapp.RMRParams{}}
Anssi Mannila8046c702020-01-02 13:39:05 +0200407 params.Mtype = 12020 // RIC SUBSCRIPTION DELETE
Juha Hyttinene406a342020-01-13 13:02:26 +0200408 params.SubId = int(subs.GetSubId())
409 params.Xid = trans.GetXid()
410 params.Meid = subs.GetMeid()
411 params.Src = trans.OrigParams.Src
Anssi Mannila8046c702020-01-02 13:39:05 +0200412 params.PayloadLen = len(subDelReqPayload)
413 params.Payload = subDelReqPayload
414 params.Mbuf = nil
415
416 // Delete CREATE transaction
Juha Hyttinene406a342020-01-13 13:02:26 +0200417 trans.Release()
Anssi Mannila8046c702020-01-02 13:39:05 +0200418
Juha Hyttinene406a342020-01-13 13:02:26 +0200419 // Create DELETE transaction (internal and no messages toward xapp)
420 var forwardRespToXapp bool = false
421 var respReceived bool = false
422 deltrans, err := c.tracker.TrackTransaction(trans.RmrEndpoint, params, respReceived, forwardRespToXapp)
Anssi Mannila8046c702020-01-02 13:39:05 +0200423 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200424 xapp.Logger.Error("SubReq timeout: %s, Dropping this msg.", err.Error())
Anssi Mannila8046c702020-01-02 13:39:05 +0200425 return
426 }
427
Juha Hyttinene406a342020-01-13 13:02:26 +0200428 err = subs.SetTransaction(deltrans)
Anssi Mannila8046c702020-01-02 13:39:05 +0200429 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200430 xapp.Logger.Error("SubReq timeout: %s, Dropping this msg.", err.Error())
431 deltrans.Release()
432 return
Anssi Mannila8046c702020-01-02 13:39:05 +0200433 }
Juha Hyttinene406a342020-01-13 13:02:26 +0200434
435 c.rmrSend("SubDelReq(SubReq timer) to E2T", subs, deltrans, deltrans.OrigParams.Payload, deltrans.OrigParams.PayloadLen)
436
437 c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()), subDelReqTime, FirstTry, c.handleSubscriptionDeleteRequestTimer)
kalnagy93cc3e22019-09-19 11:29:29 +0200438 return
439}
440
Juha Hyttinene406a342020-01-13 13:02:26 +0200441func (c *Control) handleSubscriptionDeleteRequest(params *RMRParams) {
442 xapp.Logger.Info("SubDelReq from xapp: %s", params.String())
kalnagy93cc3e22019-09-19 11:29:29 +0200443
Juha Hyttinene406a342020-01-13 13:02:26 +0200444 srcAddr, srcPort, err := c.rtmgrClient.SplitSource(params.Src)
445 if err != nil {
446 xapp.Logger.Error("SubDelReq: Failed to update routing-manager. Dropping this msg. Err: %s, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
447 return
kalnagy93cc3e22019-09-19 11:29:29 +0200448 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200449
kalnagy1455c852019-10-21 13:06:23 +0200450 payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteRequestSequenceNumber(params.Payload)
kalnagy93cc3e22019-09-19 11:29:29 +0200451 if err != nil {
Anssi Mannila8046c702020-01-02 13:39:05 +0200452 xapp.Logger.Error("SubDelReq: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
kalnagy93cc3e22019-09-19 11:29:29 +0200453 return
454 }
Anssi Mannila26c922a2019-12-17 11:18:08 +0200455 xapp.Logger.Info("SubDelReq: Received payloadSeqNum: %v", payloadSeqNum)
456
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200457 subs := c.registry.GetSubscription(payloadSeqNum)
Juha Hyttinene406a342020-01-13 13:02:26 +0200458 if subs == nil {
Anssi Mannila26c922a2019-12-17 11:18:08 +0200459 xapp.Logger.Error("SubDelReq: Not valid sequence number. Dropping this msg. SubId: %v, Xid: %s", params.SubId, params.Xid)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200460 return
kalnagy93cc3e22019-09-19 11:29:29 +0200461 }
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200462
Juha Hyttinene406a342020-01-13 13:02:26 +0200463 var forwardRespToXapp bool = true
Anssi Mannila8046c702020-01-02 13:39:05 +0200464 var respReceived bool = false
Juha Hyttinene406a342020-01-13 13:02:26 +0200465 trans, err := c.tracker.TrackTransaction(RmrEndpoint{*srcAddr, *srcPort}, params, respReceived, forwardRespToXapp)
466 if err != nil {
467 xapp.Logger.Error("SubDelReq: %s, Dropping this msg.", err.Error())
468 return
469 }
470
471 err = subs.SetTransaction(trans)
472 if err != nil {
473 xapp.Logger.Error("SubDelReq: %s, Dropping this msg.", err.Error())
474 trans.Release()
475 return
476 }
477
478 subs.UnConfirmed()
479
480 c.rmrSend("SubDelReq to E2T", subs, trans, trans.OrigParams.Payload, trans.OrigParams.PayloadLen)
481
482 c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()), subDelReqTime, FirstTry, c.handleSubscriptionDeleteRequestTimer)
kalnagye0018682019-09-26 16:28:25 +0200483 return
484}
485
Juha Hyttinene406a342020-01-13 13:02:26 +0200486func (c *Control) handleSubscriptionDeleteResponse(params *RMRParams) (err error) {
487 xapp.Logger.Info("SubDelResp from E2T:%s", params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200488
kalnagy1455c852019-10-21 13:06:23 +0200489 payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteResponseSequenceNumber(params.Payload)
kalnagye0018682019-09-26 16:28:25 +0200490 if err != nil {
Anssi Mannila8046c702020-01-02 13:39:05 +0200491 xapp.Logger.Error("SubDelResp: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
kalnagye0018682019-09-26 16:28:25 +0200492 return
493 }
Anssi Mannila26c922a2019-12-17 11:18:08 +0200494 xapp.Logger.Info("SubDelResp: Received payloadSeqNum: %v", payloadSeqNum)
kalnagye0018682019-09-26 16:28:25 +0200495
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200496 subs := c.registry.GetSubscription(payloadSeqNum)
497 if subs == nil {
498 xapp.Logger.Error("SubDelResp: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200499 return
500 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200501
Juha Hyttinene406a342020-01-13 13:02:26 +0200502 trans := subs.GetTransaction()
503 if trans == nil {
504 xapp.Logger.Error("SubDelResp: Unknown trans. Dropping this msg. PayloadSeqNum: %v, SubId: %v", subs.GetSubId(), params.SubId)
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200505 return
506 }
507
Juha Hyttinene406a342020-01-13 13:02:26 +0200508 c.timerMap.StopTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200509
Juha Hyttinene406a342020-01-13 13:02:26 +0200510 responseReceived := trans.CheckResponseReceived()
Anssi Mannila8046c702020-01-02 13:39:05 +0200511 if responseReceived == true {
512 // Subscription Delete timer already received
513 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200514 }
515
Juha Hyttinene406a342020-01-13 13:02:26 +0200516 trans.Release()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200517
Juha Hyttinene406a342020-01-13 13:02:26 +0200518 if trans.ForwardRespToXapp == true {
519 c.rmrReplyToSender("SubDelResp to xapp", subs, trans, params.Mtype, params.Payload, params.PayloadLen)
Anssi Mannila8046c702020-01-02 13:39:05 +0200520 time.Sleep(3 * time.Second)
521 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200522
Juha Hyttinene406a342020-01-13 13:02:26 +0200523 xapp.Logger.Info("SubDelResp: Deleting trans record. SubId: %v, Xid: %s", subs.GetSubId(), trans.GetXid())
524 if !c.registry.DelSubscription(subs.GetSubId()) {
525 xapp.Logger.Error("SubDelResp: Failed to release sequency number. SubId: %v, Xid: %s", subs.GetSubId(), trans.GetXid())
Anssi Mannila90fa0212019-12-12 10:47:47 +0200526 return
kalnagye0018682019-09-26 16:28:25 +0200527 }
528 return
529}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200530
Juha Hyttinene406a342020-01-13 13:02:26 +0200531func (c *Control) handleSubscriptionDeleteFailure(params *RMRParams) {
532 xapp.Logger.Info("SubDelFail from E2T:%s", params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200533
534 payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteFailureSequenceNumber(params.Payload)
535 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200536 xapp.Logger.Error("SubDelFail: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, %s", err, params.String())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200537 return
538 }
Anssi Mannila26c922a2019-12-17 11:18:08 +0200539 xapp.Logger.Info("SubDelFail: Received payloadSeqNum: %v", payloadSeqNum)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200540
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200541 subs := c.registry.GetSubscription(payloadSeqNum)
542 if subs == nil {
543 xapp.Logger.Error("SubDelFail: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200544 return
545 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200546
Juha Hyttinene406a342020-01-13 13:02:26 +0200547 trans := subs.GetTransaction()
548 if trans == nil {
549 xapp.Logger.Error("SubDelFail: Unknown trans. Dropping this msg. PayloadSeqNum: %v, SubId: %v", subs.GetSubId(), params.SubId)
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200550 return
551 }
552
Juha Hyttinene406a342020-01-13 13:02:26 +0200553 c.timerMap.StopTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200554
Juha Hyttinene406a342020-01-13 13:02:26 +0200555 responseReceived := trans.CheckResponseReceived()
Anssi Mannila8046c702020-01-02 13:39:05 +0200556 if responseReceived == true {
557 // Subscription Delete timer already received
558 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200559 }
Juha Hyttinene406a342020-01-13 13:02:26 +0200560 if trans.ForwardRespToXapp == true {
Anssi Mannila8046c702020-01-02 13:39:05 +0200561 var subDelRespPayload []byte
Juha Hyttinene406a342020-01-13 13:02:26 +0200562 subDelRespPayload, err = c.e2ap.PackSubscriptionDeleteResponse(trans.OrigParams.Payload, subs.GetSubId())
Anssi Mannila8046c702020-01-02 13:39:05 +0200563 if err != nil {
564 xapp.Logger.Error("SubDelFail:Packing SubDelResp failed. Err: %v", err)
565 return
566 }
567
Juha Hyttinene406a342020-01-13 13:02:26 +0200568 // RIC SUBSCRIPTION DELETE RESPONSE
569 c.rmrReplyToSender("SubDelFail to xapp", subs, trans, 12021, subDelRespPayload, len(subDelRespPayload))
Anssi Mannila8046c702020-01-02 13:39:05 +0200570 time.Sleep(3 * time.Second)
571 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200572
Juha Hyttinene406a342020-01-13 13:02:26 +0200573 xapp.Logger.Info("SubDelFail: Deleting trans record. SubId: %v, Xid: %s", subs.GetSubId(), trans.GetXid())
574 trans.Release()
575 if !c.registry.DelSubscription(subs.GetSubId()) {
576 xapp.Logger.Error("SubDelFail: Failed to release sequency number. Err: %v, SubId: %v, Xid: %s", err, subs.GetSubId(), trans.GetXid())
Anssi Mannila90fa0212019-12-12 10:47:47 +0200577 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200578 }
579 return
580}
581
Anssi Mannila8046c702020-01-02 13:39:05 +0200582func (c *Control) handleSubscriptionDeleteRequestTimer(strId string, nbrId int, tryCount uint64) {
Juha Hyttinene406a342020-01-13 13:02:26 +0200583 xapp.Logger.Info("SubDelReq timeout: subId: %v, tryCount: %v", nbrId, tryCount)
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200584
Juha Hyttinene406a342020-01-13 13:02:26 +0200585 subs := c.registry.GetSubscription(uint16(nbrId))
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200586 if subs == nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200587 xapp.Logger.Error("SubDelReq timeout: Unknown payloadSeqNum. Dropping this msg. SubId: %v", nbrId)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200588 return
589 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200590
Juha Hyttinene406a342020-01-13 13:02:26 +0200591 trans := subs.GetTransaction()
592 if trans == nil {
593 xapp.Logger.Error("SubDelReq timeout: Unknown trans. Dropping this msg. SubId: %v", subs.GetSubId())
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200594 return
595 }
596
Juha Hyttinene406a342020-01-13 13:02:26 +0200597 responseReceived := trans.CheckResponseReceived()
Anssi Mannila8046c702020-01-02 13:39:05 +0200598 if responseReceived == true {
599 // Subscription Delete Response or Failure already received
600 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200601 }
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200602
Anssi Mannila8046c702020-01-02 13:39:05 +0200603 if tryCount < maxSubDelReqTryCount {
Juha Hyttinene406a342020-01-13 13:02:26 +0200604 xapp.Logger.Info("SubDelReq timeout: Resending SubDelReq to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v", trans.OrigParams.Mtype, subs.GetSubId(), trans.GetXid(), subs.GetMeid())
Anssi Mannila8046c702020-01-02 13:39:05 +0200605 // Set possible to handle new response for the subId
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200606
Juha Hyttinene406a342020-01-13 13:02:26 +0200607 trans.RetryTransaction()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200608
Juha Hyttinene406a342020-01-13 13:02:26 +0200609 c.rmrSend("SubDelReq(SubDelReq timer) to E2T", subs, trans, trans.OrigParams.Payload, trans.OrigParams.PayloadLen)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200610
Anssi Mannila8046c702020-01-02 13:39:05 +0200611 tryCount++
Juha Hyttinene406a342020-01-13 13:02:26 +0200612 c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subs.GetSubId()), subReqTime, tryCount, c.handleSubscriptionDeleteRequestTimer)
Anssi Mannila8046c702020-01-02 13:39:05 +0200613 return
614 }
615
Juha Hyttinene406a342020-01-13 13:02:26 +0200616 if trans.ForwardRespToXapp == true {
Anssi Mannila8046c702020-01-02 13:39:05 +0200617 var subDelRespPayload []byte
Juha Hyttinene406a342020-01-13 13:02:26 +0200618 subDelRespPayload, err := c.e2ap.PackSubscriptionDeleteResponse(trans.OrigParams.Payload, subs.GetSubId())
Anssi Mannila8046c702020-01-02 13:39:05 +0200619 if err != nil {
Juha Hyttinene406a342020-01-13 13:02:26 +0200620 xapp.Logger.Error("SubDelReq timeout: Unable to pack payload. Dropping this this msg. Err: %v, SubId: %v, Xid: %s, Payload %x", err, subs.GetSubId(), trans.GetXid(), trans.OrigParams.Payload)
Anssi Mannila8046c702020-01-02 13:39:05 +0200621 return
622 }
623
Juha Hyttinene406a342020-01-13 13:02:26 +0200624 // RIC SUBSCRIPTION DELETE RESPONSE
625 c.rmrReplyToSender("SubDelResp(SubDelReq timer) to xapp", subs, trans, 12021, subDelRespPayload, len(subDelRespPayload))
Anssi Mannila8046c702020-01-02 13:39:05 +0200626
627 time.Sleep(3 * time.Second)
Juha Hyttinene406a342020-01-13 13:02:26 +0200628
Anssi Mannila8046c702020-01-02 13:39:05 +0200629 }
630
Juha Hyttinene406a342020-01-13 13:02:26 +0200631 xapp.Logger.Info("SubDelReq timeout: Deleting trans record. SubId: %v, Xid: %s", subs.GetSubId(), trans.GetXid())
632 trans.Release()
633 if !c.registry.DelSubscription(subs.GetSubId()) {
634 xapp.Logger.Error("SubDelReq timeout: Failed to release sequency number. SubId: %v, Xid: %s", subs.GetSubId(), trans.GetXid())
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200635 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200636 return
Anssi Mannilaf1d0eb62019-12-17 15:29:55 +0200637}