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