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 ==================================================================================
27 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
28 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
31 const restSubSdlNs = "submgr_restSubsDb"
33 type RESTSubscriptionInfo struct {
35 XAppServiceName string
36 XAppRmrEndPoint string
39 XAppIdToE2Id map[int64]int64
45 func CreateRESTSdl() Sdlnterface {
46 return sdl.NewSyncStorage()
49 func (c *Control) WriteRESTSubscriptionToSdl(restSubId string, restSubs *RESTSubscription) error {
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
62 jsonData, err := json.Marshal(restSubscriptionInfo)
64 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
67 if err = c.restSubsDb.Set(restSubSdlNs, restSubId, jsonData); err != nil {
68 c.UpdateCounter(cSDLWriteFailure)
69 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
71 xapp.Logger.Debug("SDL: Subscription written in restSubsDb. restSubId = %v", restSubId)
76 func (c *Control) ReadRESTSubscriptionFromSdl(restSubId string) (*RESTSubscription, error) {
78 // This function is now just for testing purpose
80 retMap, err := c.restSubsDb.Get(restSubSdlNs, []string{key})
82 c.UpdateCounter(cSDLReadFailure)
83 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
85 xapp.Logger.Debug("SDL: Subscription read from restSubsDb. restSubId = %v", restSubId)
88 restSubs := &RESTSubscription{}
89 for _, iRESTSubscriptionInfo := range retMap {
91 if iRESTSubscriptionInfo == nil {
92 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() REST subscription not found. restSubId = %v\n", restSubId)
95 restSubscriptionInfo := &RESTSubscriptionInfo{}
96 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
98 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
99 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
102 restSubs = c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
104 c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(restSubId, restSubs.lastReqMd5sum)
109 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
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
125 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
128 if err := c.restSubsDb.Remove(restSubSdlNs, []string{key}); err != nil {
129 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
131 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
136 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
138 retMap := make(map[string]*RESTSubscription)
140 keys, err := c.restSubsDb.GetAll(restSubSdlNs)
142 c.UpdateCounter(cSDLReadFailure)
143 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
150 // Get all subscriptionInfos
151 iRESTSubscriptionMap, err := c.restSubsDb.Get(restSubSdlNs, keys)
153 c.UpdateCounter(cSDLReadFailure)
154 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading REST subscriptions from DBAAS %s\n", err.Error())
157 for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
159 if iRESTSubscriptionInfo == nil {
160 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
163 restSubscriptionInfo := &RESTSubscriptionInfo{}
164 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
166 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
167 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
170 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
171 retMap[iRESTSubId] = restSubs
176 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
178 if err := c.restSubsDb.RemoveAll(restSubSdlNs); err != nil {
179 c.UpdateCounter(cSDLRemoveFailure)
180 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
182 xapp.Logger.Debug("SDL: All subscriptions removed from restSubsDb")
187 func (c *Control) GetRESTKeyCount() (int, error) {
190 keys, err := c.restSubsDb.GetAll(restSubSdlNs)
192 c.UpdateCounter(cSDLReadFailure)
193 return 0, fmt.Errorf("SDL: GetRESTKeyCount(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
195 return len(keys), nil