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.Md5sum
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)
100 func (c *Control) CreateRESTSubscription(restSubscriptionInfo *RESTSubscriptionInfo, jsonSubscriptionInfo *string) *RESTSubscription {
102 restSubs := &RESTSubscription{}
103 restSubs.xAppRmrEndPoint = restSubscriptionInfo.xAppRmrEndPoint
104 restSubs.Meid = restSubscriptionInfo.Meid
105 restSubs.InstanceIds = restSubscriptionInfo.InstanceIds
106 restSubs.xAppIdToE2Id = restSubscriptionInfo.xAppIdToE2Id
107 restSubs.SubReqOngoing = restSubscriptionInfo.SubReqOngoing
108 restSubs.SubDelReqOngoing = restSubscriptionInfo.SubDelReqOngoing
109 restSubs.Md5sum = restSubscriptionInfo.Md5sum
114 func (c *Control) RemoveRESTSubscriptionFromSdl(restSubId string) error {
117 if err := c.restSubsDb.Remove([]string{key}); err != nil {
118 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
120 xapp.Logger.Debug("SDL: Subscription removed from restSubsDb. restSubId = %v", restSubId)
125 func (c *Control) ReadAllRESTSubscriptionsFromSdl() (map[string]*RESTSubscription, error) {
127 retMap := make(map[string]*RESTSubscription)
129 keys, err := c.restSubsDb.GetAll()
131 c.UpdateCounter(cSDLReadFailure)
132 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading REST subscriptions keys from DBAAS %s\n", err.Error())
139 // Get all subscriptionInfos
140 iRESTSubscriptionMap, err := c.restSubsDb.Get(keys)
142 c.UpdateCounter(cSDLReadFailure)
143 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading REST subscriptions from DBAAS %s\n", err.Error())
146 for iRESTSubId, iRESTSubscriptionInfo := range iRESTSubscriptionMap {
148 if iRESTSubscriptionInfo == nil {
149 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iRESTSubscriptionInfo = nil\n")
152 restSubscriptionInfo := &RESTSubscriptionInfo{}
153 jsonSubscriptionInfo := iRESTSubscriptionInfo.(string)
155 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), restSubscriptionInfo); err != nil {
156 return nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
159 restSubs := c.CreateRESTSubscription(restSubscriptionInfo, &jsonSubscriptionInfo)
160 retMap[iRESTSubId] = restSubs
165 func (c *Control) RemoveAllRESTSubscriptionsFromSdl() error {
167 if err := c.restSubsDb.RemoveAll(); err != nil {
168 c.UpdateCounter(cSDLRemoveFailure)
169 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
171 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")