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