blob: d1830dee9df6f11ac43ff1e648dd0285a9da6a0e [file] [log] [blame]
Anssi Mannilac92b4212020-12-07 14:59:34 +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
22import (
23 "encoding/json"
24 "fmt"
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030025 "strconv"
26
Anssi Mannilac92b4212020-12-07 14:59:34 +020027 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
28 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
29 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Anssi Mannilac92b4212020-12-07 14:59:34 +020030)
31
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +030032const e2SubSdlNs = "submgr_e2SubsDb"
33
Anssi Mannilac92b4212020-12-07 14:59:34 +020034type SubscriptionInfo struct {
Anssi Mannilaf682ace2021-09-28 13:11:25 +030035 Valid bool
36 ReqId RequestId
37 Meid xapp.RMRMeid
38 EpList xapp.RmrEndpointList
39 SubReqMsg e2ap.E2APSubscriptionRequest
40 SubRespMsg e2ap.E2APSubscriptionResponse
41 SubRespRcvd string
42 PolicyUpdate bool
Anssi Mannilac92b4212020-12-07 14:59:34 +020043}
44
45func CreateSdl() Sdlnterface {
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +030046 return sdl.NewSyncStorage()
Anssi Mannilac92b4212020-12-07 14:59:34 +020047}
48
49func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
50
51 var subscriptionInfo SubscriptionInfo
52 subscriptionInfo.Valid = subs.valid
53 subscriptionInfo.ReqId = subs.ReqId
54 subscriptionInfo.Meid = *subs.Meid
55 subscriptionInfo.EpList = subs.EpList
56 subscriptionInfo.SubReqMsg = *subs.SubReqMsg
Anssi Mannilaf682ace2021-09-28 13:11:25 +030057 subscriptionInfo.PolicyUpdate = subs.PolicyUpdate
Anssi Mannilac92b4212020-12-07 14:59:34 +020058
59 if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
60 subscriptionInfo.SubRespRcvd = "SubResp"
61 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
Anssi Mannilac92b4212020-12-07 14:59:34 +020062 } else {
63 subscriptionInfo.SubRespRcvd = ""
64 }
65
66 jsonData, err := json.Marshal(subscriptionInfo)
67 if err != nil {
68 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
69 }
70
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +030071 if err = c.e2SubsDb.Set(e2SubSdlNs, strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +020072 c.UpdateCounter(cSDLWriteFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +020073 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
74 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030075 xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +020076 }
77 return nil
78}
79
80func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
81
82 // This function is now just for testing purpose
83 key := strconv.FormatUint(uint64(subId), 10)
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +030084 retMap, err := c.e2SubsDb.Get(e2SubSdlNs, []string{key})
Anssi Mannilac92b4212020-12-07 14:59:34 +020085 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +020086 c.UpdateCounter(cSDLReadFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +020087 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
88 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030089 xapp.Logger.Debug("SDL: Subscription read from e2SubsDb. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +020090 }
91
92 subs := &Subscription{}
93 for _, iSubscriptionInfo := range retMap {
94
95 if iSubscriptionInfo == nil {
96 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
97 }
98
99 subscriptionInfo := &SubscriptionInfo{}
100 jsonSubscriptionInfo := iSubscriptionInfo.(string)
101
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200102 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200103 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
104 }
105
106 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
107 }
108 return subs, nil
109}
110
111func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
112
113 subs := &Subscription{}
114 subs.registry = c.registry
115 subs.valid = subscriptionInfo.Valid
116 subs.ReqId = subscriptionInfo.ReqId
117 meid := xapp.RMRMeid{}
118 meid = subscriptionInfo.Meid
119 subs.Meid = &meid
120 subs.EpList = subscriptionInfo.EpList
121 subs.TheTrans = nil
122 subReq := e2ap.E2APSubscriptionRequest{}
123 subReq = subscriptionInfo.SubReqMsg
124 subs.SubReqMsg = &subReq
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300125 subs.PolicyUpdate = subscriptionInfo.PolicyUpdate
Anssi Mannilac92b4212020-12-07 14:59:34 +0200126
127 if subscriptionInfo.SubRespRcvd == "SubResp" {
128 subs.SubRespRcvd = true
129 subResp := e2ap.E2APSubscriptionResponse{}
130 subResp = subscriptionInfo.SubRespMsg
131 subs.SubRFMsg = &subResp
Anssi Mannilac92b4212020-12-07 14:59:34 +0200132 } else {
133 subs.SubRespRcvd = false
134 subs.SubRFMsg = nil
135 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
136 }
137 return subs
138}
139
140func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
141
142 key := strconv.FormatUint(uint64(subId), 10)
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300143 if err := c.e2SubsDb.Remove(e2SubSdlNs, []string{key}); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200144 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
145 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300146 xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200147 }
148 return nil
149}
150
151func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
152
153 // Read all subscriptionInfos
154 var subIds []uint32
155 var i uint32
156 for i = 1; i < 65535; i++ {
157 subIds = append(subIds, i)
158 }
159
160 retMap := make(map[uint32]*Subscription)
161 // Get all keys
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300162 keys, err := c.e2SubsDb.GetAll(e2SubSdlNs)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200163 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200164 c.UpdateCounter(cSDLReadFailure)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300165 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
Anssi Mannilac92b4212020-12-07 14:59:34 +0200166 }
167
168 if len(keys) == 0 {
169 return subIds, retMap, nil
170 }
171
172 // Get all subscriptionInfos
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300173 iSubscriptionMap, err := c.e2SubsDb.Get(e2SubSdlNs, keys)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200174 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200175 c.UpdateCounter(cSDLReadFailure)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300176 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading E2 subscriptions from DBAAS %s\n", err.Error())
Anssi Mannilac92b4212020-12-07 14:59:34 +0200177 }
178
179 for _, iSubscriptionInfo := range iSubscriptionMap {
180
181 if iSubscriptionInfo == nil {
182 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
183 }
184
185 subscriptionInfo := &SubscriptionInfo{}
186 jsonSubscriptionInfo := iSubscriptionInfo.(string)
187
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200188 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200189 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
190 }
191
192 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
193
194 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
195 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
196 }
197 retMap[subscriptionInfo.ReqId.InstanceId] = subs
198
199 // Remove subId from free subIds. Original slice is modified here!
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200200 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200201 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
202 }
203 }
204 return subIds, retMap, nil
205}
206
207func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
208 for i, num := range s {
209 if removedNum == uint32(num) {
210 s = append(s[:i], s[i+1:]...)
211 return s[:len(s)], nil
212 }
213 }
214 return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
215}
216func (c *Control) RemoveAllSubscriptionsFromSdl() error {
217
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300218 if err := c.e2SubsDb.RemoveAll(e2SubSdlNs); err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200219 c.UpdateCounter(cSDLRemoveFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200220 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
221 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300222 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200223 }
224 return nil
225}
Anssi Mannilaafadb6d2021-11-15 15:03:12 +0200226
227func (c *Control) GetE2KeyCount() (int, error) {
228
229 // Get all keys
230 keys, err := c.e2SubsDb.GetAll(e2SubSdlNs)
231 if err != nil {
232 c.UpdateCounter(cSDLReadFailure)
233 return 0, fmt.Errorf("SDL: GetE2KeyCount(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
234 }
235 return len(keys), nil
236}