blob: 8a06174e998a1d592cfa41b8042e14bdc962e6fa [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 "C"
23
kalnagy45114752019-06-18 14:40:39 +020024import (
Peter Szilagyifbc56f92019-07-23 19:29:46 +000025 "errors"
kalnagy93cc3e22019-09-19 11:29:29 +020026 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
27 rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
kalnagy1455c852019-10-21 13:06:23 +020028 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
29 httptransport "github.com/go-openapi/runtime/client"
30 "github.com/go-openapi/strfmt"
31 "github.com/spf13/viper"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000032 "math/rand"
Peter Szilagyifbc56f92019-07-23 19:29:46 +000033 "time"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020034 "sync"
kalnagy45114752019-06-18 14:40:39 +020035)
36
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020037var subReqTime time.Duration = 2 * time.Second
38var SubDelReqTime time.Duration = 2 * time.Second
39
kalnagy45114752019-06-18 14:40:39 +020040type Control struct {
kalnagy93cc3e22019-09-19 11:29:29 +020041 e2ap *E2ap
42 registry *Registry
43 rtmgrClient *RtmgrClient
44 tracker *Tracker
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020045 timerMap *TimerMap
Anssi Mannila90fa0212019-12-12 10:47:47 +020046 rmrSendMutex *sync.Mutex
kalnagy93cc3e22019-09-19 11:29:29 +020047}
48
49type RMRMeid struct {
50 PlmnID string
51 EnbID string
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020052 RanName string
kalnagy93cc3e22019-09-19 11:29:29 +020053}
54
kalnagy1455c852019-10-21 13:06:23 +020055var seedSN uint16
kalnagy93cc3e22019-09-19 11:29:29 +020056
57const (
58 CREATE Action = 0
kalnagy1455c852019-10-21 13:06:23 +020059 MERGE Action = 1
Anssi Mannila90fa0212019-12-12 10:47:47 +020060 NONE Action = 2
kalnagy93cc3e22019-09-19 11:29:29 +020061 DELETE Action = 3
62)
Peter Szilagyifbc56f92019-07-23 19:29:46 +000063
64func init() {
Anssi Mannila90fa0212019-12-12 10:47:47 +020065 xapp.Logger.Info("SUBMGR /ric-plt-submgr:r3-test-v4")
Peter Szilagyifbc56f92019-07-23 19:29:46 +000066 viper.AutomaticEnv()
67 viper.SetEnvPrefix("submgr")
68 viper.AllowEmptyEnv(true)
kalnagy1455c852019-10-21 13:06:23 +020069 seedSN = uint16(viper.GetInt("seed_sn"))
70 if seedSN == 0 {
Peter Szilagyifbc56f92019-07-23 19:29:46 +000071 rand.Seed(time.Now().UnixNano())
kalnagy1455c852019-10-21 13:06:23 +020072 seedSN = uint16(rand.Intn(65535))
Peter Szilagyifbc56f92019-07-23 19:29:46 +000073 }
kalnagy1455c852019-10-21 13:06:23 +020074 if seedSN > 65535 {
75 seedSN = 0
Peter Szilagyifbc56f92019-07-23 19:29:46 +000076 }
kalnagy1455c852019-10-21 13:06:23 +020077 xapp.Logger.Info("SUBMGR: Initial Sequence Number: %v", seedSN)
kalnagy45114752019-06-18 14:40:39 +020078}
79
80func NewControl() Control {
Peter Szilagyifbc56f92019-07-23 19:29:46 +000081 registry := new(Registry)
kalnagy1455c852019-10-21 13:06:23 +020082 registry.Initialize(seedSN)
kalnagy93cc3e22019-09-19 11:29:29 +020083
Balint Uvegese9608cd2019-09-20 18:00:32 +000084 tracker := new(Tracker)
85 tracker.Init()
86
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020087 timerMap := new(TimerMap)
88 timerMap.Init()
89
Anssi Mannila90fa0212019-12-12 10:47:47 +020090 rmrSendMutex := &sync.Mutex{}
91
kalnagy1455c852019-10-21 13:06:23 +020092 transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
kalnagy93cc3e22019-09-19 11:29:29 +020093 client := rtmgrclient.New(transport, strfmt.Default)
94 handle := rtmgrhandle.NewProvideXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
kalnagy1455c852019-10-21 13:06:23 +020095 deleteHandle := rtmgrhandle.NewDeleteXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
96 rtmgrClient := RtmgrClient{client, handle, deleteHandle}
kalnagy93cc3e22019-09-19 11:29:29 +020097
Anssi Mannila90fa0212019-12-12 10:47:47 +020098 return Control{new(E2ap), registry, &rtmgrClient, tracker, timerMap, rmrSendMutex}
kalnagy45114752019-06-18 14:40:39 +020099}
100
101func (c *Control) Run() {
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000102 xapp.Run(c)
kalnagy45114752019-06-18 14:40:39 +0200103}
104
kalnagy93cc3e22019-09-19 11:29:29 +0200105func (c *Control) rmrSend(params *xapp.RMRParams) (err error) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200106 status := false
107 i := 1
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200108 for ; i <= 10 && status == false; i++ {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200109 c.rmrSendMutex.Lock()
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200110 status = xapp.Rmr.Send(params, false)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200111 c.rmrSendMutex.Unlock()
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200112 if status == false {
113 xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s",i, params.Mtype, params.SubId, params.Xid)
114 time.Sleep(500 * time.Millisecond)
115 }
116 }
117 if status == false {
118 err = errors.New("rmr.Send() failed")
119 xapp.Rmr.Free(params.Mbuf)
120 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000121 return
kalnagy45114752019-06-18 14:40:39 +0200122}
123
kalnagye0018682019-09-26 16:28:25 +0200124func (c *Control) rmrReplyToSender(params *xapp.RMRParams) (err error) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200125 c.rmrSend(params)
kalnagye0018682019-09-26 16:28:25 +0200126 return
127}
128
Anssi Mannila90fa0212019-12-12 10:47:47 +0200129func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
130 switch msg.Mtype {
131 case xapp.RICMessageTypes["RIC_SUB_REQ"]:
132 go c.handleSubscriptionRequest(msg)
133 case xapp.RICMessageTypes["RIC_SUB_RESP"]:
134 go c.handleSubscriptionResponse(msg)
135 case xapp.RICMessageTypes["RIC_SUB_FAILURE"]:
136 go c.handleSubscriptionFailure(msg)
137 case xapp.RICMessageTypes["RIC_SUB_DEL_REQ"]:
138 go c.handleSubscriptionDeleteRequest(msg)
139 case xapp.RICMessageTypes["RIC_SUB_DEL_RESP"]:
140 go c.handleSubscriptionDeleteResponse(msg)
141 default:
142 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
Balint Uvegescd3881b2019-10-02 15:01:43 +0000143 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200144 return nil
Balint Uvegescd3881b2019-10-02 15:01:43 +0000145}
146
Anssi Mannila90fa0212019-12-12 10:47:47 +0200147func (c *Control) handleSubscriptionRequest(params *xapp.RMRParams) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200148 xapp.Logger.Info("Subscription Request Received from Src: %s, Mtype: %v, SubId: %v, Xid: %s, Meid: %v",params.Src, params.Mtype, params.SubId, params.Xid, params.Meid)
149 xapp.Rmr.Free(params.Mbuf)
150 params.Mbuf = nil
kalnagy93cc3e22019-09-19 11:29:29 +0200151
152 /* Reserve a sequence number and set it in the payload */
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200153 newSubId, isIdValid := c.registry.ReserveSequenceNumber()
154 if isIdValid != true {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200155 xapp.Logger.Error("Further processing of this SubscriptionRequest stopped. SubId: %v, Xid: %s",params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200156 return
157 }
kalnagy93cc3e22019-09-19 11:29:29 +0200158
Anssi Mannila90fa0212019-12-12 10:47:47 +0200159 err := c.e2ap.SetSubscriptionRequestSequenceNumber(params.Payload, newSubId)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000160 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200161 xapp.Logger.Error("Unable to set Subscription Sequence Number in Payload. Dropping this Subscription Request message. Err: v%, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000162 return
163 }
kalnagy93cc3e22019-09-19 11:29:29 +0200164
kalnagy1455c852019-10-21 13:06:23 +0200165 srcAddr, srcPort, err := c.rtmgrClient.SplitSource(params.Src)
kalnagy93cc3e22019-09-19 11:29:29 +0200166 if err != nil {
167 xapp.Logger.Error("Failed to update routing-manager about the subscription request with reason: %s", err)
168 return
169 }
170
171 /* Create transatcion records for every subscription request */
kalnagy1455c852019-10-21 13:06:23 +0200172 xactKey := TransactionKey{newSubId, CREATE}
173 xactValue := Transaction{*srcAddr, *srcPort, params}
174 err = c.tracker.TrackTransaction(xactKey, xactValue)
kalnagy93cc3e22019-09-19 11:29:29 +0200175 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200176 xapp.Logger.Error("Failed to create a Subscription Request transaction record. Err: %v", err)
kalnagy93cc3e22019-09-19 11:29:29 +0200177 return
178 }
179
180 /* Update routing manager about the new subscription*/
kalnagy1455c852019-10-21 13:06:23 +0200181 subRouteAction := SubRouteInfo{CREATE, *srcAddr, *srcPort, newSubId}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200182 xapp.Logger.Info("Starting routing manager update")
Anssi Mannila90fa0212019-12-12 10:47:47 +0200183 err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
184 if err != nil {
185 xapp.Logger.Error("Failed to update routing manager. Dropping this Subscription Request message. Err: %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
186 return
187 }
kalnagy93cc3e22019-09-19 11:29:29 +0200188
189 // Setting new subscription ID in the RMR header
kalnagy1455c852019-10-21 13:06:23 +0200190 params.SubId = int(newSubId)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200191 xapp.Logger.Info("Forwarding Subscription Request to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v",params.Mtype, params.SubId, params.Xid, params.Meid)
192 err = c.rmrSend(params)
193 if err != nil {
194 xapp.Logger.Error("Failed to send request to E2T %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
195 } /*else {
196 c.timerMap.StartTimer(newSubId, subReqTime, c.handleSubscriptionRequestTimer)
197 }*/
kalnagy1455c852019-10-21 13:06:23 +0200198 xapp.Logger.Debug("--- Debugging transaction table = %v", c.tracker.transactionTable)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000199 return
kalnagy45114752019-06-18 14:40:39 +0200200}
201
Anssi Mannila90fa0212019-12-12 10:47:47 +0200202func (c *Control) handleSubscriptionResponse(params *xapp.RMRParams) {
Anssi Mannila0a9f2602019-12-09 15:26:44 +0200203 xapp.Logger.Info("Subscription Response Received from Src: %s, Mtype: %v, SubId: %v, Meid: %v",params.Src, params.Mtype, params.SubId, params.Meid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200204 xapp.Rmr.Free(params.Mbuf)
205 params.Mbuf = nil
206
kalnagy1455c852019-10-21 13:06:23 +0200207 payloadSeqNum, err := c.e2ap.GetSubscriptionResponseSequenceNumber(params.Payload)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000208 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200209 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload. Dropping this Subscription Responsemessage. Err: v%, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000210 return
211 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200212
213 xapp.Logger.Info("Received payloadSeqNum: %v",payloadSeqNum)
kalnagy1455c852019-10-21 13:06:23 +0200214 if !c.registry.IsValidSequenceNumber(payloadSeqNum) {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200215 xapp.Logger.Error("Unknown payloadSeqNum. Dropping this Subscription Response message. PayloadSeqNum: %v, SubId: %v, Xid: %s", payloadSeqNum, params.SubId, params.Xid)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000216 return
217 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200218
219// c.timerMap.StopTimer(payloadSeqNum)
220
kalnagy1455c852019-10-21 13:06:23 +0200221 c.registry.setSubscriptionToConfirmed(payloadSeqNum)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200222 var transaction Transaction
223 transaction, err = c.tracker.RetriveTransaction(payloadSeqNum, CREATE)
224 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200225 xapp.Logger.Error("Failed to retrive transaction record. Dropping this Subscription Response message. Err: v%, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200226 return
227 }
228 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Retrieved old subId...", int(payloadSeqNum), transaction.XappInstanceAddress, transaction.XappPort)
229
230 params.SubId = int(payloadSeqNum)
231 params.Xid = transaction.OrigParams.Xid
232
233 xapp.Logger.Info("Forwarding Subscription Response to UEEC: Mtype: %v, SubId: %v, Xid: %s, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
234 err = c.rmrReplyToSender(params)
235 if err != nil {
236 xapp.Logger.Error("Failed to send response to requestor %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
237 }
238
239 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Deleting transaction record", int(payloadSeqNum), transaction.XappInstanceAddress, transaction.XappPort)
240 transaction, err = c.tracker.completeTransaction(payloadSeqNum, CREATE)
kalnagye0018682019-09-26 16:28:25 +0200241 if err != nil {
kalnagy1455c852019-10-21 13:06:23 +0200242 xapp.Logger.Error("Failed to delete a Subscription Request transaction record due to %v", err)
kalnagye0018682019-09-26 16:28:25 +0200243 return
244 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200245 return
246}
247
Anssi Mannila90fa0212019-12-12 10:47:47 +0200248func (c *Control) handleSubscriptionFailure(params *xapp.RMRParams) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200249 xapp.Logger.Info("Subscription Failure Received from Src: %s, Mtype: %v, SubId: %v, Meid: %v",params.Src, params.Mtype, params.SubId, params.Meid)
250 xapp.Rmr.Free(params.Mbuf)
251 params.Mbuf = nil
252
253 payloadSeqNum, err := c.e2ap.GetSubscriptionFailureSequenceNumber(params.Payload)
254 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200255 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload. Dropping this Subscription Failure message. Err: v%, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200256 return
257 }
258 xapp.Logger.Info("Received payloadSeqNum: %v", payloadSeqNum)
259
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200260// c.timerMap.StopTimer(payloadSeqNum)
261
262 var transaction Transaction
263 transaction, err = c.tracker.RetriveTransaction(payloadSeqNum, CREATE)
264 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200265 xapp.Logger.Error("Failed to retrive transaction record. Dropping this Subscription Failure message. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200266 return
267 }
268 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Forwarding response to requestor...", int(payloadSeqNum), transaction.XappInstanceAddress, transaction.XappPort)
269
270 params.SubId = int(payloadSeqNum)
271 params.Xid = transaction.OrigParams.Xid
272
273 xapp.Logger.Info("Forwarding Subscription Failure to UEEC: Mtype: %v, SubId: %v, Xid: %v, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
274 err = c.rmrReplyToSender(params)
275 if err != nil {
276 xapp.Logger.Error("Failed to send response to requestor %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
277 }
278
279 time.Sleep(3 * time.Second)
280
281 xapp.Logger.Info("Starting routing manager update")
Anssi Mannila0a9f2602019-12-09 15:26:44 +0200282 subRouteAction := SubRouteInfo{CREATE, transaction.XappInstanceAddress, transaction.XappPort, payloadSeqNum}
Anssi Mannila90fa0212019-12-12 10:47:47 +0200283 err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
284 if err != nil {
285 xapp.Logger.Error("Failed to update routing manager. Err: %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
286 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200287
288 xapp.Logger.Info("Deleting trancaction record")
289 if c.registry.releaseSequenceNumber(payloadSeqNum) {
290 transaction, err = c.tracker.completeTransaction(payloadSeqNum, CREATE)
291 if err != nil {
292 xapp.Logger.Error("Failed to delete a Subscription Request transaction record due to %v", err)
293 return
294 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200295 } else {
296 xapp.Logger.Error("Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
297 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200298 }
299 return
300}
301
302func (c *Control) handleSubscriptionRequestTimer(subId uint16) {
303 xapp.Logger.Info("Subscription Request timer expired. SubId: %v",subId)
304/*
305 transaction, err := c.tracker.completeTransaction(subId, CREATE)
306 if err != nil {
307 xapp.Logger.Error("Failed to delete a Subscription Request transaction record due to %v", err)
308 return
309 }
310 xapp.Logger.Info("SubId: %v, Xid %v, Meid: %v",subId, transaction.OrigParams.Xid, transaction.OrigParams.Meid)
311
312 var params xapp.RMRParams
313 params.Mtype = 12012 //xapp.RICMessageTypes["RIC_SUB_FAILURE"]
314 params.SubId = int(subId)
315 params.Meid = transaction.OrigParams.Meid
316 params.Xid = transaction.OrigParams.Xid
317 payload := []byte("40C9408098000003EA7E00050000010016EA6300020021EA6E00808180EA6F000400000000EA6F000400010040EA6F000400020080EA6F0004000300C0EA6F000400040100EA6F000400050140EA6F000400060180EA6F0004000701C0EA6F000400080200EA6F000400090240EA6F0004000A0280EA6F0004000B02C0EA6F0004000C0300EA6F0004000D0340EA6F0004000E0380EA6F0004000F03C0")
318 params.PayloadLen = len(payload)
319 params.Payload = payload
320
321 xapp.Logger.Info("Forwarding Subscription Failure to UEEC: Mtype: %v, SubId: %v, Xid: %s, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
322 err = c.rmrReplyToSender(&params)
323 if err != nil {
324 xapp.Logger.Error("Failed to send response to requestor %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
325 }
326*/
327/*
328 time.Sleep(3 * time.Second)
329
330 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Deleting transaction record", int(subId), transaction.XappInstanceAddress, transaction.XappPort)
331
332 xapp.Logger.Info("Starting routing manager update")
333 subRouteAction := SubRouteInfo{DELETE, transaction.XappInstanceAddress, transaction.XappPort, payloadSeqNum}
334 c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
335
336 xapp.Logger.Info("Deleting trancaction record")
337 if c.registry.releaseSequenceNumber(payloadSeqNum) {
338 transaction, err = c.tracker.completeTransaction(payloadSeqNum, CREATE)
339 if err != nil {
340 xapp.Logger.Error("Failed to delete a Subscription Request transaction record due to %v", err)
341 return
342 }
343 }
344*/
kalnagy93cc3e22019-09-19 11:29:29 +0200345 return
346}
347
348func (act Action) String() string {
349 actions := [...]string{
350 "CREATE",
351 "MERGE",
Anssi Mannila90fa0212019-12-12 10:47:47 +0200352 "NONE",
kalnagy93cc3e22019-09-19 11:29:29 +0200353 "DELETE",
354 }
355
356 if act < CREATE || act > DELETE {
357 return "Unknown"
358 }
359 return actions[act]
360}
361
362func (act Action) valid() bool {
363 switch act {
364 case CREATE, MERGE, DELETE:
365 return true
366 default:
367 return false
368 }
369}
370
Anssi Mannila90fa0212019-12-12 10:47:47 +0200371func (c *Control) handleSubscriptionDeleteRequest(params *xapp.RMRParams) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200372 xapp.Logger.Info("Subscription Delete Request Received from Src: %s, Mtype: %v, SubId: %v, Xid: %s, Meid: %v",params.Src, params.Mtype, params.SubId, params.Xid, params.Meid)
373 xapp.Rmr.Free(params.Mbuf)
374 params.Mbuf = nil
375
kalnagy1455c852019-10-21 13:06:23 +0200376 payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteRequestSequenceNumber(params.Payload)
kalnagy93cc3e22019-09-19 11:29:29 +0200377 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200378 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload. Dropping this Subscription Delete Request message. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
kalnagy93cc3e22019-09-19 11:29:29 +0200379 return
380 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200381 xapp.Logger.Info("Received payloadSeqNum: %v", payloadSeqNum)
kalnagy1455c852019-10-21 13:06:23 +0200382 if c.registry.IsValidSequenceNumber(payloadSeqNum) {
383 c.registry.deleteSubscription(payloadSeqNum)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200384 err = c.trackDeleteTransaction(params, payloadSeqNum)
385 if err != nil {
386 xapp.Logger.Error("Failed to create transaction record. Dropping this Subscription Delete Request message. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
387 return
kalnagye0018682019-09-26 16:28:25 +0200388 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200389 } else {
390 xapp.Logger.Error("Not valid sequence number. Dropping this Subscription Delete Request message. SubId: %v, Xid: %s", params.SubId, params.Xid)
391 return
kalnagy93cc3e22019-09-19 11:29:29 +0200392 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200393
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200394 xapp.Logger.Info("Forwarding Delete Subscription Request to E2T: Mtype: %v, SubId: %v, Xid: %s, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
kalnagy93cc3e22019-09-19 11:29:29 +0200395 c.rmrSend(params)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200396 if err != nil {
397 xapp.Logger.Error("Failed to send request to E2T %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
398 } /*else {
399 c.timerMap.StartTimer(payloadSeqNum, SubDelReqTime, c.handleSubscriptionDeleteRequestTimer)
400 }*/
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000401 return
402}
kalnagye0018682019-09-26 16:28:25 +0200403
kalnagy1455c852019-10-21 13:06:23 +0200404func (c *Control) trackDeleteTransaction(params *xapp.RMRParams, payloadSeqNum uint16) (err error) {
405 srcAddr, srcPort, err := c.rtmgrClient.SplitSource(params.Src)
406 if err != nil {
407 xapp.Logger.Error("Failed to update routing-manager about the subscription delete request with reason: %s", err)
408 }
409 xactKey := TransactionKey{payloadSeqNum, DELETE}
410 xactValue := Transaction{*srcAddr, *srcPort, params}
411 err = c.tracker.TrackTransaction(xactKey, xactValue)
kalnagye0018682019-09-26 16:28:25 +0200412 return
413}
414
415func (c *Control) handleSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200416 xapp.Logger.Info("Subscription Delete Response Received from Src: %s, Mtype: %v, SubId: %v, Meid: %v",params.Src, params.Mtype, params.SubId, params.Meid)
417 xapp.Rmr.Free(params.Mbuf)
418 params.Mbuf = nil
419
kalnagy1455c852019-10-21 13:06:23 +0200420 payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteResponseSequenceNumber(params.Payload)
kalnagye0018682019-09-26 16:28:25 +0200421 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200422 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload. Dropping this Subscription Delete Response message. Err: %, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
kalnagye0018682019-09-26 16:28:25 +0200423 return
424 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200425 xapp.Logger.Info("Received payloadSeqNum: %v", payloadSeqNum)
kalnagye0018682019-09-26 16:28:25 +0200426
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200427// c.timerMap.StopTimer(payloadSeqNum)
428
429 var transaction Transaction
430 transaction, err = c.tracker.RetriveTransaction(payloadSeqNum, DELETE)
431 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200432 xapp.Logger.Error("Failed to retrive transaction record. Dropping this Subscription Delete Response message. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200433 return
434 }
435 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Forwarding response to requestor...", int(payloadSeqNum), transaction.XappInstanceAddress, transaction.XappPort)
436
437 params.SubId = int(payloadSeqNum)
438 params.Xid = transaction.OrigParams.Xid
439 xapp.Logger.Info("Forwarding Subscription Delete Response to UEEC: Mtype: %v, SubId: %v, Xid: %v, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
440 err = c.rmrReplyToSender(params)
441 if err != nil {
442 xapp.Logger.Error("Failed to send response to requestor %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
443// return
444 }
445
446 time.Sleep(3 * time.Second)
447
448 xapp.Logger.Info("Starting routing manager update")
449 subRouteAction := SubRouteInfo{DELETE, transaction.XappInstanceAddress, transaction.XappPort, payloadSeqNum}
Anssi Mannila90fa0212019-12-12 10:47:47 +0200450 err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
451 if err != nil {
452 xapp.Logger.Error("Failed to update routing manager %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
453 return
454 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200455
456 xapp.Logger.Info("Deleting trancaction record")
kalnagy1455c852019-10-21 13:06:23 +0200457 if c.registry.releaseSequenceNumber(payloadSeqNum) {
458 transaction, err = c.tracker.completeTransaction(payloadSeqNum, DELETE)
kalnagye0018682019-09-26 16:28:25 +0200459 if err != nil {
kalnagy1455c852019-10-21 13:06:23 +0200460 xapp.Logger.Error("Failed to delete a Subscription Delete Request transaction record due to %v", err)
kalnagye0018682019-09-26 16:28:25 +0200461 return
462 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200463 } else {
464 xapp.Logger.Error("Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
465 return
kalnagye0018682019-09-26 16:28:25 +0200466 }
467 return
468}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200469
Anssi Mannila90fa0212019-12-12 10:47:47 +0200470func (c *Control) handleSubscriptionDeleteFailure(params *xapp.RMRParams) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200471 xapp.Logger.Info("Subscription Delete Failure Received from Src: %s, Mtype: %v, SubId: %v, Meid: %v",params.Src, params.Mtype, params.SubId, params.Meid)
472 xapp.Rmr.Free(params.Mbuf)
473 params.Mbuf = nil
474
475 payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteFailureSequenceNumber(params.Payload)
476 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200477 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload. Dropping this Subscription Delete Failure message. Err: %, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200478 return
479 }
480 xapp.Logger.Info("Received payloadSeqNum: %v", payloadSeqNum)
481
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200482// c.timerMap.StopTimer(payloadSeqNum)
483
484 var transaction Transaction
485 transaction, err = c.tracker.RetriveTransaction(payloadSeqNum, DELETE)
486 if err != nil {
Anssi Mannila90fa0212019-12-12 10:47:47 +0200487 xapp.Logger.Error("Failed to retrive transaction record. Dropping this Subscription Delete Failure message. Err %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200488 return
489 }
490 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Forwarding response to requestor...", int(payloadSeqNum), transaction.XappInstanceAddress, transaction.XappPort)
491
492 params.SubId = int(payloadSeqNum)
493 params.Xid = transaction.OrigParams.Xid
494 xapp.Logger.Info("Forwarding Subscription Delete Failure to UEEC: Mtype: %v, SubId: %v, Xid: %v, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
495 err = c.rmrReplyToSender(params)
496 if err != nil {
497 xapp.Logger.Error("Failed to send response to requestor %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
498// return
499 }
500
501 time.Sleep(3 * time.Second)
502
503 xapp.Logger.Info("Starting routing manager update")
504 subRouteAction := SubRouteInfo{DELETE, transaction.XappInstanceAddress, transaction.XappPort, payloadSeqNum}
505 c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
Anssi Mannila90fa0212019-12-12 10:47:47 +0200506 if err != nil {
507 xapp.Logger.Error("Failed to update routing manager %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
508 return
509 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200510
511 xapp.Logger.Info("Deleting trancaction record")
512 if c.registry.releaseSequenceNumber(payloadSeqNum) {
513 transaction, err = c.tracker.completeTransaction(payloadSeqNum, DELETE)
514 if err != nil {
515 xapp.Logger.Error("Failed to delete a Subscription Delete Request transaction record due to %v", err)
516 return
517 }
Anssi Mannila90fa0212019-12-12 10:47:47 +0200518 } else {
519 xapp.Logger.Error("Failed to release sequency number. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
520 return
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200521 }
522 return
523}
524
525func (c *Control) handleSubscriptionDeleteRequestTimer(subId uint16) {
526 xapp.Logger.Info("Subscription Delete Request timer expired. SubId: %v",subId)
527/*
528 transaction, err := c.tracker.completeTransaction(subId, DELETE)
529 if err != nil {
530 xapp.Logger.Error("Failed to delete a Subscription Delete Request transaction record due to %v", err)
531 return
532 }
533 xapp.Logger.Info("SubId: %v, Xid %v, Meid: %v",subId, transaction.OrigParams.Xid, transaction.OrigParams.Meid)
534
535 var params xapp.RMRParams
536 params.Mtype = 12022 //xapp.RICMessageTypes["RIC_SUB_DEL_FAILURE"]
537 params.SubId = int(subId)
538 params.Meid = transaction.OrigParams.Meid
539 params.Xid = transaction.OrigParams.Xid
540 payload := []byte("40CA4018000003EA7E00050000010016EA6300020021EA74000200C0")
541 params.PayloadLen = len(payload)
542 params.Payload = payload
543
544 xapp.Logger.Info("Forwarding Subscription Delete Failure to UEEC: Mtype: %v, SubId: %v, Xid: %s, Meid: %v",params.Mtype, params.SubId, params.Xid, params.Meid)
545 err = c.rmrReplyToSender(&params)
546 if err != nil {
547 xapp.Logger.Error("Failed to send response to requestor %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
548 }
549*/
550/*
551 time.Sleep(3 * time.Second)
552 xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Deleting transaction record", int(subId), transaction.XappInstanceAddress, transaction.XappPort)
553
554 xapp.Logger.Info("Starting routing manager update")
555 subRouteAction := SubRouteInfo{DELETE, transaction.XappInstanceAddress, transaction.XappPort, payloadSeqNum}
556 c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
557
558 xapp.Logger.Info("Deleting trancaction record")
559 if c.registry.releaseSequenceNumber(payloadSeqNum) {
560 transaction, err = c.tracker.completeTransaction(payloadSeqNum, DELETE)
561 if err != nil {
562 xapp.Logger.Error("Failed to delete a Subscription Delete Request transaction record due to %v", err)
563 return
564 }
565 }
566*/
567 return
568 }