f9d837b5e17dc3aca7ff1fb648725d1c38105483
[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.lastReqMd5sum
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                 restDuplicateCtrl.SetMd5sumFromLastOkRequest(restSubId, restSubs.lastReqMd5sum)
98         }
99         return restSubs, nil
100 }
101
102 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
103
104         restSubs := &RESTSubscription{}
105         restSubs.xAppRmrEndPoint = restSubscriptionInfo.XAppRmrEndPoint
106         restSubs.Meid = restSubscriptionInfo.Meid
107         restSubs.InstanceIds = restSubscriptionInfo.InstanceIds
108         restSubs.xAppIdToE2Id = restSubscriptionInfo.XAppIdToE2Id
109         restSubs.SubReqOngoing = restSubscriptionInfo.SubReqOngoing
110         restSubs.SubDelReqOngoing = restSubscriptionInfo.SubDelReqOngoing
111         restSubs.lastReqMd5sum = restSubscriptionInfo.Md5sum
112
113         return restSubs
114 }
115
116 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
117
118         key := restSubId
119         if err := c.restSubsDb.Remove([]string{key}); err != nil {
120                 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
121         } else {
122                 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
123         }
124         return nil
125 }
126
127 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
128
129         retMap := make(map[string]*RESTSubscription)
130         // Get all keys
131         keys, err := c.restSubsDb.GetAll()
132         if err != nil {
133                 c.UpdateCounter(cSDLReadFailure)
134                 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
135         }
136
137         if len(keys) == 0 {
138                 return retMap, nil
139         }
140
141         // Get all subscriptionInfos
142         iRESTSubscriptionMap, err := c.restSubsDb.Get(keys)
143         if err != nil {
144                 c.UpdateCounter(cSDLReadFailure)
145                 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get():  Error while reading REST subscriptions from DBAAS %s\n", err.Error())
146         }
147
148         for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
149
150                 if iRESTSubscriptionInfo == nil {
151                         return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
152                 }
153
154                 restSubscriptionInfo := &RESTSubscriptionInfo{}
155                 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
156
157                 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
158                         return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
159                 }
160
161                 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
162                 retMap[iRESTSubId] = restSubs
163         }
164         return retMap, nil
165 }
166
167 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
168
169         if err := c.restSubsDb.RemoveAll(); err != nil {
170                 c.UpdateCounter(cSDLRemoveFailure)
171                 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
172         } else {
173                 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")
174         }
175         return nil
176 }