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 {
33 XAppRmrEndPoint string
36 XAppIdToE2Id map[int64]int64
42 func CreateRESTSdl() Sdlnterface {
43 return sdl.NewSyncStorage()
46 func (c *Control) WriteRESTSubscriptionToSdl(restSubId string, restSubs *RESTSubscription) error {
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
57 jsonData, err := json.Marshal(restSubscriptionInfo)
59 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
62 if err = c.restSubsDb.Set(restSubSdlNs, restSubId, jsonData); err != nil {
63 c.UpdateCounter(cSDLWriteFailure)
64 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
66 xapp.Logger.Debug("SDL: Subscription written in restSubsDb. restSubId = %v", restSubId)
71 func (c *Control) ReadRESTSubscriptionFromSdl(restSubId string) (*RESTSubscription, error) {
73 // This function is now just for testing purpose
75 retMap, err := c.restSubsDb.Get(restSubSdlNs, []string{key})
77 c.UpdateCounter(cSDLReadFailure)
78 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
80 xapp.Logger.Debug("SDL: Subscription read from restSubsDb. restSubId = %v", restSubId)
83 restSubs := &RESTSubscription{}
84 for _, iRESTSubscriptionInfo := range retMap {
86 if iRESTSubscriptionInfo == nil {
87 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() REST subscription not found. restSubId = %v\n", restSubId)
90 restSubscriptionInfo := &RESTSubscriptionInfo{}
91 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
93 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
94 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
97 restSubs = c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
99 c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(restSubId, restSubs.lastReqMd5sum)
104 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
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
118 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
121 if err := c.restSubsDb.Remove(restSubSdlNs, []string{key}); err != nil {
122 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
124 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
129 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
131 retMap := make(map[string]*RESTSubscription)
133 keys, err := c.restSubsDb.GetAll(restSubSdlNs)
135 c.UpdateCounter(cSDLReadFailure)
136 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
143 // Get all subscriptionInfos
144 iRESTSubscriptionMap, err := c.restSubsDb.Get(restSubSdlNs, keys)
146 c.UpdateCounter(cSDLReadFailure)
147 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading REST subscriptions from DBAAS %s\n", err.Error())
150 for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
152 if iRESTSubscriptionInfo == nil {
153 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
156 restSubscriptionInfo := &RESTSubscriptionInfo{}
157 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
159 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
160 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
163 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
164 retMap[iRESTSubId] = restSubs
169 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
171 if err := c.restSubsDb.RemoveAll(restSubSdlNs); err != nil {
172 c.UpdateCounter(cSDLRemoveFailure)
173 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
175 xapp.Logger.Debug("SDL: All subscriptions removed from restSubsDb")
180 func (c *Control) GetRESTKeyCount() (int, error) {
183 keys, err := c.restSubsDb.GetAll(restSubSdlNs)
185 c.UpdateCounter(cSDLReadFailure)
186 return 0, fmt.Errorf("SDL: GetRESTKeyCount(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
188 return len(keys), nil