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 type RESTSubscriptionInfo struct {
31 XAppRmrEndPoint string
34 XAppIdToE2Id map[int64]int64
40 func CreateRESTSdl() Sdlnterface {
41 return sdl.NewSdlInstance("submgr_restSubsDb", sdl.NewDatabase())
44 func (c *Control) WriteRESTSubscriptionToSdl(restSubId string, restSubs *RESTSubscription) error {
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
55 jsonData, err := json.Marshal(restSubscriptionInfo)
57 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
60 if err = c.restSubsDb.Set(restSubId, jsonData); err != nil {
61 c.UpdateCounter(cSDLWriteFailure)
62 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
64 xapp.Logger.Debug("SDL: Subscription written in restSubsDb. restSubId = %v", restSubId)
69 func (c *Control) ReadRESTSubscriptionFromSdl(restSubId string) (*RESTSubscription, error) {
71 // This function is now just for testing purpose
73 retMap, err := c.restSubsDb.Get([]string{key})
75 c.UpdateCounter(cSDLReadFailure)
76 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
78 xapp.Logger.Debug("SDL: Subscription read from restSubsDb. restSubId = %v", restSubId)
81 restSubs := &RESTSubscription{}
82 for _, iRESTSubscriptionInfo := range retMap {
84 if iRESTSubscriptionInfo == nil {
85 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() REST subscription not found. restSubId = %v\n", restSubId)
88 restSubscriptionInfo := &RESTSubscriptionInfo{}
89 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
91 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
92 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
95 restSubs = c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
97 restDuplicateCtrl.SetMd5sumFromLastOkRequest(restSubId, restSubs.lastReqMd5sum)
102 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
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
116 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
119 if err := c.restSubsDb.Remove([]string{key}); err != nil {
120 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
122 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
127 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
129 retMap := make(map[string]*RESTSubscription)
131 keys, err := c.restSubsDb.GetAll()
133 c.UpdateCounter(cSDLReadFailure)
134 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
141 // Get all subscriptionInfos
142 iRESTSubscriptionMap, err := c.restSubsDb.Get(keys)
144 c.UpdateCounter(cSDLReadFailure)
145 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading REST subscriptions from DBAAS %s\n", err.Error())
148 for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
150 if iRESTSubscriptionInfo == nil {
151 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
154 restSubscriptionInfo := &RESTSubscriptionInfo{}
155 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
157 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
158 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
161 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
162 retMap[iRESTSubId] = restSubs
167 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
169 if err := c.restSubsDb.RemoveAll(); err != nil {
170 c.UpdateCounter(cSDLRemoveFailure)
171 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
173 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")