Delete all subscriptions when Ran is under reset.
[ric-plt/submgr.git] / pkg / control / sdl_restSubsDb.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
26         sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
27         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28 )
29
30 const restSubSdlNs = "submgr_restSubsDb"
31
32 type RESTSubscriptionInfo struct {
33         Created          string
34         XAppServiceName  string
35         XAppRmrEndPoint  string
36         Meid             string
37         InstanceIds      []uint32
38         XAppIdToE2Id     map[int64]int64
39         SubReqOngoing    bool
40         SubDelReqOngoing bool
41         Md5sum           string
42 }
43
44 func CreateRESTSdl() Sdlnterface {
45         return sdl.NewSyncStorage()
46 }
47
48 func (c *Control) WriteRESTSubscriptionToSdl(restSubId string, restSubs *RESTSubscription) error {
49
50         var restSubscriptionInfo RESTSubscriptionInfo
51         restSubscriptionInfo.Created = restSubs.Created
52         restSubscriptionInfo.XAppServiceName = restSubs.xAppServiceName
53         restSubscriptionInfo.XAppRmrEndPoint = restSubs.xAppRmrEndPoint
54         restSubscriptionInfo.Meid = restSubs.Meid
55         restSubscriptionInfo.InstanceIds = restSubs.InstanceIds
56         restSubscriptionInfo.XAppIdToE2Id = restSubs.xAppIdToE2Id
57         restSubscriptionInfo.SubReqOngoing = restSubs.SubReqOngoing
58         restSubscriptionInfo.SubDelReqOngoing = restSubs.SubDelReqOngoing
59         restSubscriptionInfo.Md5sum = restSubs.lastReqMd5sum
60
61         jsonData, err := json.Marshal(restSubscriptionInfo)
62         if err != nil {
63                 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
64         }
65
66         if err = c.restSubsDb.Set(restSubSdlNs, restSubId, jsonData); err != nil {
67                 c.UpdateCounter(cSDLWriteFailure)
68                 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
69         } else {
70                 xapp.Logger.Debug("SDL: Subscription written in restSubsDb. restSubId = %v", restSubId)
71         }
72         return nil
73 }
74
75 func (c *Control) ReadRESTSubscriptionFromSdl(restSubId string) (*RESTSubscription, error) {
76
77         // This function is now just for testing purpose
78         key := restSubId
79         retMap, err := c.restSubsDb.Get(restSubSdlNs, []string{key})
80         if err != nil {
81                 c.UpdateCounter(cSDLReadFailure)
82                 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
83         } else {
84                 xapp.Logger.Debug("SDL: Subscription read from restSubsDb.  restSubId = %v", restSubId)
85         }
86
87         restSubs := &RESTSubscription{}
88         for _, iRESTSubscriptionInfo := range retMap {
89
90                 if iRESTSubscriptionInfo == nil {
91                         return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() REST subscription not found. restSubId = %v\n", restSubId)
92                 }
93
94                 restSubscriptionInfo := &RESTSubscriptionInfo{}
95                 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
96
97                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
98                         return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
99                 }
100
101                 restSubs = c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
102
103                 c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(restSubId, restSubs.lastReqMd5sum)
104         }
105         return restSubs, nil
106 }
107
108 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
109
110         restSubs := &RESTSubscription{}
111         restSubs.Created = restSubscriptionInfo.Created
112         restSubs.xAppServiceName = restSubscriptionInfo.XAppServiceName
113         restSubs.xAppRmrEndPoint = restSubscriptionInfo.XAppRmrEndPoint
114         restSubs.Meid = restSubscriptionInfo.Meid
115         restSubs.InstanceIds = restSubscriptionInfo.InstanceIds
116         restSubs.xAppIdToE2Id = restSubscriptionInfo.XAppIdToE2Id
117         restSubs.SubReqOngoing = restSubscriptionInfo.SubReqOngoing
118         restSubs.SubDelReqOngoing = restSubscriptionInfo.SubDelReqOngoing
119         restSubs.lastReqMd5sum = restSubscriptionInfo.Md5sum
120
121         return restSubs
122 }
123
124 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
125
126         key := restSubId
127         if err := c.restSubsDb.Remove(restSubSdlNs, []string{key}); err != nil {
128                 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
129         } else {
130                 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
131         }
132         return nil
133 }
134
135 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
136
137         retMap := make(map[string]*RESTSubscription)
138         // Get all keys
139         keys, err := c.restSubsDb.GetAll(restSubSdlNs)
140         if err != nil {
141                 c.UpdateCounter(cSDLReadFailure)
142                 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
143         }
144
145         if len(keys) == 0 {
146                 return retMap, nil
147         }
148
149         // Get all subscriptionInfos
150         iRESTSubscriptionMap, err := c.restSubsDb.Get(restSubSdlNs, keys)
151         if err != nil {
152                 c.UpdateCounter(cSDLReadFailure)
153                 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get():  Error while reading REST subscriptions from DBAAS %s\n", err.Error())
154         }
155
156         for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
157
158                 if iRESTSubscriptionInfo == nil {
159                         return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
160                 }
161
162                 restSubscriptionInfo := &RESTSubscriptionInfo{}
163                 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
164
165                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
166                         return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
167                 }
168
169                 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
170                 retMap[iRESTSubId] = restSubs
171         }
172         return retMap, nil
173 }
174
175 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
176
177         if err := c.restSubsDb.RemoveAll(restSubSdlNs); err != nil {
178                 c.UpdateCounter(cSDLRemoveFailure)
179                 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
180         } else {
181                 xapp.Logger.Debug("SDL: All subscriptions removed from restSubsDb")
182         }
183         return nil
184 }
185
186 func (c *Control) GetRESTKeyCount() (int, error) {
187
188         // Get all keys
189         keys, err := c.restSubsDb.GetAll(restSubSdlNs)
190         if err != nil {
191                 c.UpdateCounter(cSDLReadFailure)
192                 return 0, fmt.Errorf("SDL: GetRESTKeyCount(), GetAll(). Error while reading E2 subscriptions  keys from DBAAS %s\n", err.Error())
193         }
194         return len(keys), nil
195 }