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