Replace deprecated SDL APIs
[ric-plt/submgr.git] / pkg / control / sdl_e2SubsDb.go
1 /*
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
20 package control
21
22 import (
23         "encoding/json"
24         "fmt"
25         "strconv"
26
27         "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"
30 )
31
32 const e2SubSdlNs = "submgr_e2SubsDb"
33
34 type SubscriptionInfo struct {
35         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
43 }
44
45 func CreateSdl() Sdlnterface {
46         return sdl.NewSyncStorage()
47 }
48
49 func (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
57         subscriptionInfo.PolicyUpdate = subs.PolicyUpdate
58
59         if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
60                 subscriptionInfo.SubRespRcvd = "SubResp"
61                 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
62         } 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
71         if err = c.e2SubsDb.Set(e2SubSdlNs, strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
72                 c.UpdateCounter(cSDLWriteFailure)
73                 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
74         } else {
75                 xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId)
76         }
77         return nil
78 }
79
80 func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
81
82         // This function is now just for testing purpose
83         key := strconv.FormatUint(uint64(subId), 10)
84         retMap, err := c.e2SubsDb.Get(e2SubSdlNs, []string{key})
85         if err != nil {
86                 c.UpdateCounter(cSDLReadFailure)
87                 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
88         } else {
89                 xapp.Logger.Debug("SDL: Subscription read from e2SubsDb.  subId = %v", subId)
90         }
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
102                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
103                         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
111 func (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
125         subs.PolicyUpdate = subscriptionInfo.PolicyUpdate
126
127         if subscriptionInfo.SubRespRcvd == "SubResp" {
128                 subs.SubRespRcvd = true
129                 subResp := e2ap.E2APSubscriptionResponse{}
130                 subResp = subscriptionInfo.SubRespMsg
131                 subs.SubRFMsg = &subResp
132         } 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
140 func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
141
142         key := strconv.FormatUint(uint64(subId), 10)
143         if err := c.e2SubsDb.Remove(e2SubSdlNs, []string{key}); err != nil {
144                 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
145         } else {
146                 xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId)
147         }
148         return nil
149 }
150
151 func (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
162         keys, err := c.e2SubsDb.GetAll(e2SubSdlNs)
163         if err != nil {
164                 c.UpdateCounter(cSDLReadFailure)
165                 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions  keys from DBAAS %s\n", err.Error())
166         }
167
168         if len(keys) == 0 {
169                 return subIds, retMap, nil
170         }
171
172         // Get all subscriptionInfos
173         iSubscriptionMap, err := c.e2SubsDb.Get(e2SubSdlNs, keys)
174         if err != nil {
175                 c.UpdateCounter(cSDLReadFailure)
176                 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get():  Error while reading E2 subscriptions from DBAAS %s\n", err.Error())
177         }
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
188                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
189                         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!
200                 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
201                         return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
202                 }
203         }
204         return subIds, retMap, nil
205 }
206
207 func 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 }
216 func (c *Control) RemoveAllSubscriptionsFromSdl() error {
217
218         if err := c.e2SubsDb.RemoveAll(e2SubSdlNs); err != nil {
219                 c.UpdateCounter(cSDLRemoveFailure)
220                 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
221         } else {
222                 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")
223         }
224         return nil
225 }