2afa36e306e9a8e78b4f561b8c46dc6d34787c96
[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 type SubscriptionInfo struct {
33         Valid        bool
34         ReqId        RequestId
35         Meid         xapp.RMRMeid
36         EpList       xapp.RmrEndpointList
37         SubReqMsg    e2ap.E2APSubscriptionRequest
38         SubRespMsg   e2ap.E2APSubscriptionResponse
39         SubRespRcvd  string
40         PolicyUpdate bool
41 }
42
43 func CreateSdl() Sdlnterface {
44         return sdl.NewSdlInstance("submgr_e2SubsDb", sdl.NewDatabase())
45 }
46
47 func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
48
49         var subscriptionInfo SubscriptionInfo
50         subscriptionInfo.Valid = subs.valid
51         subscriptionInfo.ReqId = subs.ReqId
52         subscriptionInfo.Meid = *subs.Meid
53         subscriptionInfo.EpList = subs.EpList
54         subscriptionInfo.SubReqMsg = *subs.SubReqMsg
55         subscriptionInfo.PolicyUpdate = subs.PolicyUpdate
56
57         if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
58                 subscriptionInfo.SubRespRcvd = "SubResp"
59                 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
60         } else {
61                 subscriptionInfo.SubRespRcvd = ""
62         }
63
64         jsonData, err := json.Marshal(subscriptionInfo)
65         if err != nil {
66                 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
67         }
68
69         if err = c.e2SubsDb.Set(strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
70                 c.UpdateCounter(cSDLWriteFailure)
71                 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
72         } else {
73                 xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId)
74         }
75         return nil
76 }
77
78 func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
79
80         // This function is now just for testing purpose
81         key := strconv.FormatUint(uint64(subId), 10)
82         retMap, err := c.e2SubsDb.Get([]string{key})
83         if err != nil {
84                 c.UpdateCounter(cSDLReadFailure)
85                 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
86         } else {
87                 xapp.Logger.Debug("SDL: Subscription read from e2SubsDb.  subId = %v", subId)
88         }
89
90         subs := &Subscription{}
91         for _, iSubscriptionInfo := range retMap {
92
93                 if iSubscriptionInfo == nil {
94                         return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
95                 }
96
97                 subscriptionInfo := &SubscriptionInfo{}
98                 jsonSubscriptionInfo := iSubscriptionInfo.(string)
99
100                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
101                         return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
102                 }
103
104                 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
105         }
106         return subs, nil
107 }
108
109 func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
110
111         subs := &Subscription{}
112         subs.registry = c.registry
113         subs.valid = subscriptionInfo.Valid
114         subs.ReqId = subscriptionInfo.ReqId
115         meid := xapp.RMRMeid{}
116         meid = subscriptionInfo.Meid
117         subs.Meid = &meid
118         subs.EpList = subscriptionInfo.EpList
119         subs.TheTrans = nil
120         subReq := e2ap.E2APSubscriptionRequest{}
121         subReq = subscriptionInfo.SubReqMsg
122         subs.SubReqMsg = &subReq
123         subs.PolicyUpdate = subscriptionInfo.PolicyUpdate
124
125         if subscriptionInfo.SubRespRcvd == "SubResp" {
126                 subs.SubRespRcvd = true
127                 subResp := e2ap.E2APSubscriptionResponse{}
128                 subResp = subscriptionInfo.SubRespMsg
129                 subs.SubRFMsg = &subResp
130         } else {
131                 subs.SubRespRcvd = false
132                 subs.SubRFMsg = nil
133                 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
134         }
135         return subs
136 }
137
138 func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
139
140         key := strconv.FormatUint(uint64(subId), 10)
141         if err := c.e2SubsDb.Remove([]string{key}); err != nil {
142                 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
143         } else {
144                 xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId)
145         }
146         return nil
147 }
148
149 func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
150
151         // Read all subscriptionInfos
152         var subIds []uint32
153         var i uint32
154         for i = 1; i < 65535; i++ {
155                 subIds = append(subIds, i)
156         }
157
158         retMap := make(map[uint32]*Subscription)
159         // Get all keys
160         keys, err := c.e2SubsDb.GetAll()
161         if err != nil {
162                 c.UpdateCounter(cSDLReadFailure)
163                 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions  keys from DBAAS %s\n", err.Error())
164         }
165
166         if len(keys) == 0 {
167                 return subIds, retMap, nil
168         }
169
170         // Get all subscriptionInfos
171         iSubscriptionMap, err := c.e2SubsDb.Get(keys)
172         if err != nil {
173                 c.UpdateCounter(cSDLReadFailure)
174                 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get():  Error while reading E2 subscriptions from DBAAS %s\n", err.Error())
175         }
176
177         for _, iSubscriptionInfo := range iSubscriptionMap {
178
179                 if iSubscriptionInfo == nil {
180                         return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
181                 }
182
183                 subscriptionInfo := &SubscriptionInfo{}
184                 jsonSubscriptionInfo := iSubscriptionInfo.(string)
185
186                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
187                         return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
188                 }
189
190                 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
191
192                 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
193                         return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
194                 }
195                 retMap[subscriptionInfo.ReqId.InstanceId] = subs
196
197                 // Remove subId from free subIds. Original slice is modified here!
198                 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
199                         return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
200                 }
201         }
202         return subIds, retMap, nil
203 }
204
205 func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
206         for i, num := range s {
207                 if removedNum == uint32(num) {
208                         s = append(s[:i], s[i+1:]...)
209                         return s[:len(s)], nil
210                 }
211         }
212         return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
213 }
214 func (c *Control) RemoveAllSubscriptionsFromSdl() error {
215
216         if err := c.e2SubsDb.RemoveAll(); err != nil {
217                 c.UpdateCounter(cSDLRemoveFailure)
218                 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
219         } else {
220                 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")
221         }
222         return nil
223 }