2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
26 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
27 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30 const restSubSdlNs = "submgr_restSubsDb"
32 type RESTSubscriptionInfo struct {
34 XAppServiceName string
35 XAppRmrEndPoint string
38 XAppIdToE2Id map[int64]int64
44 func CreateRESTSdl() Sdlnterface {
45 return sdl.NewSyncStorage()
48 func (c *Control) WriteRESTSubscriptionToSdl(restSubId string, restSubs *RESTSubscription) error {
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
61 jsonData, err := json.Marshal(restSubscriptionInfo)
63 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
66 if err = c.restSubsDb.Set(restSubSdlNs, restSubId, jsonData); err != nil {
67 c.UpdateCounter(cSDLWriteFailure)
68 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
70 xapp.Logger.Debug("SDL: Subscription written in restSubsDb. restSubId = %v", restSubId)
75 func (c *Control) ReadRESTSubscriptionFromSdl(restSubId string) (*RESTSubscription, error) {
77 // This function is now just for testing purpose
79 retMap, err := c.restSubsDb.Get(restSubSdlNs, []string{key})
81 c.UpdateCounter(cSDLReadFailure)
82 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
84 xapp.Logger.Debug("SDL: Subscription read from restSubsDb. restSubId = %v", restSubId)
87 restSubs := &RESTSubscription{}
88 for _, iRESTSubscriptionInfo := range retMap {
90 if iRESTSubscriptionInfo == nil {
91 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() REST subscription not found. restSubId = %v\n", restSubId)
94 restSubscriptionInfo := &RESTSubscriptionInfo{}
95 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
97 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
98 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
101 restSubs = c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
103 c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(restSubId, restSubs.lastReqMd5sum)
108 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
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
124 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
127 if err := c.restSubsDb.Remove(restSubSdlNs, []string{key}); err != nil {
128 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
130 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
135 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
137 retMap := make(map[string]*RESTSubscription)
139 keys, err := c.restSubsDb.GetAll(restSubSdlNs)
141 c.UpdateCounter(cSDLReadFailure)
142 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
149 // Get all subscriptionInfos
150 iRESTSubscriptionMap, err := c.restSubsDb.Get(restSubSdlNs, keys)
152 c.UpdateCounter(cSDLReadFailure)
153 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading REST subscriptions from DBAAS %s\n", err.Error())
156 for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
158 if iRESTSubscriptionInfo == nil {
159 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
162 restSubscriptionInfo := &RESTSubscriptionInfo{}
163 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
165 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
166 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
169 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
170 retMap[iRESTSubId] = restSubs
175 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
177 if err := c.restSubsDb.RemoveAll(restSubSdlNs); err != nil {
178 c.UpdateCounter(cSDLRemoveFailure)
179 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
181 xapp.Logger.Debug("SDL: All subscriptions removed from restSubsDb")
186 func (c *Control) GetRESTKeyCount() (int, error) {
189 keys, err := c.restSubsDb.GetAll(restSubSdlNs)
191 c.UpdateCounter(cSDLReadFailure)
192 return 0, fmt.Errorf("SDL: GetRESTKeyCount(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
194 return len(keys), nil