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 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
26 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
27 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
31 type SubscriptionInfo struct {
35 EpList xapp.RmrEndpointList
36 SubReqMsg e2ap.E2APSubscriptionRequest
37 SubRespMsg e2ap.E2APSubscriptionResponse
41 func CreateSdl() Sdlnterface {
42 return sdl.NewSdlInstance("submgr", sdl.NewDatabase())
45 func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
47 var subscriptionInfo SubscriptionInfo
48 subscriptionInfo.Valid = subs.valid
49 subscriptionInfo.ReqId = subs.ReqId
50 subscriptionInfo.Meid = *subs.Meid
51 subscriptionInfo.EpList = subs.EpList
52 subscriptionInfo.SubReqMsg = *subs.SubReqMsg
54 if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
55 subscriptionInfo.SubRespRcvd = "SubResp"
56 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
58 subscriptionInfo.SubRespRcvd = ""
61 jsonData, err := json.Marshal(subscriptionInfo)
63 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
66 if err = c.db.Set(strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
67 c.UpdateCounter(cSDLWriteFailure)
68 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
70 xapp.Logger.Debug("SDL: Subscription written in db. subId = %v", subId)
75 func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
77 // This function is now just for testing purpose
78 key := strconv.FormatUint(uint64(subId), 10)
79 retMap, err := c.db.Get([]string{key})
81 c.UpdateCounter(cSDLReadFailure)
82 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
84 xapp.Logger.Debug("SDL: Subscription read from db. subId = %v", subId)
87 subs := &Subscription{}
88 for _, iSubscriptionInfo := range retMap {
90 if iSubscriptionInfo == nil {
91 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
94 subscriptionInfo := &SubscriptionInfo{}
95 jsonSubscriptionInfo := iSubscriptionInfo.(string)
97 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
98 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
101 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
106 func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
108 subs := &Subscription{}
109 subs.registry = c.registry
110 subs.valid = subscriptionInfo.Valid
111 subs.ReqId = subscriptionInfo.ReqId
112 meid := xapp.RMRMeid{}
113 meid = subscriptionInfo.Meid
115 subs.EpList = subscriptionInfo.EpList
117 subReq := e2ap.E2APSubscriptionRequest{}
118 subReq = subscriptionInfo.SubReqMsg
119 subs.SubReqMsg = &subReq
121 if subscriptionInfo.SubRespRcvd == "SubResp" {
122 subs.SubRespRcvd = true
123 subResp := e2ap.E2APSubscriptionResponse{}
124 subResp = subscriptionInfo.SubRespMsg
125 subs.SubRFMsg = &subResp
127 subs.SubRespRcvd = false
129 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
134 func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
136 key := strconv.FormatUint(uint64(subId), 10)
137 if err := c.db.Remove([]string{key}); err != nil {
138 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
140 xapp.Logger.Debug("SDL: Subscription removed from db. subId = %v", subId)
145 func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
147 // Read all subscriptionInfos
150 for i = 1; i < 65535; i++ {
151 subIds = append(subIds, i)
154 retMap := make(map[uint32]*Subscription)
156 keys, err := c.db.GetAll()
158 c.UpdateCounter(cSDLReadFailure)
159 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading keys from DBAAS %s\n", err.Error())
163 return subIds, retMap, nil
166 // Get all subscriptionInfos
167 iSubscriptionMap, err := c.db.Get(keys)
169 c.UpdateCounter(cSDLReadFailure)
170 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading subscriptions from DBAAS %s\n", err.Error())
173 for _, iSubscriptionInfo := range iSubscriptionMap {
175 if iSubscriptionInfo == nil {
176 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
179 subscriptionInfo := &SubscriptionInfo{}
180 jsonSubscriptionInfo := iSubscriptionInfo.(string)
182 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
183 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
186 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
188 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
189 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
191 retMap[subscriptionInfo.ReqId.InstanceId] = subs
193 // Remove subId from free subIds. Original slice is modified here!
194 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
195 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
198 return subIds, retMap, nil
201 func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
202 for i, num := range s {
203 if removedNum == uint32(num) {
204 s = append(s[:i], s[i+1:]...)
205 return s[:len(s)], nil
208 return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
210 func (c *Control) RemoveAllSubscriptionsFromSdl() error {
212 if err := c.db.RemoveAll(); err != nil {
213 c.UpdateCounter(cSDLRemoveFailure)
214 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
216 xapp.Logger.Debug("SDL: All subscriptions removed from db")