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