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 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
28 sdl "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
29 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
32 const e2SubSdlNs = "submgr_e2SubsDb"
34 type SubscriptionInfo struct {
38 EpList xapp.RmrEndpointList
39 SubReqMsg e2ap.E2APSubscriptionRequest
40 SubRespMsg e2ap.E2APSubscriptionResponse
45 func CreateSdl() Sdlnterface {
46 return sdl.NewSyncStorage()
49 func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
51 var subscriptionInfo SubscriptionInfo
52 subscriptionInfo.Valid = subs.valid
53 subscriptionInfo.ReqId = subs.ReqId
54 subscriptionInfo.Meid = *subs.Meid
55 subscriptionInfo.EpList = subs.EpList
56 subscriptionInfo.SubReqMsg = *subs.SubReqMsg
57 subscriptionInfo.PolicyUpdate = subs.PolicyUpdate
59 if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
60 subscriptionInfo.SubRespRcvd = "SubResp"
61 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
63 subscriptionInfo.SubRespRcvd = ""
66 jsonData, err := json.Marshal(subscriptionInfo)
68 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
71 if err = c.e2SubsDb.Set(e2SubSdlNs, strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
72 c.UpdateCounter(cSDLWriteFailure)
73 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
75 xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId)
80 func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
82 // This function is now just for testing purpose
83 key := strconv.FormatUint(uint64(subId), 10)
84 retMap, err := c.e2SubsDb.Get(e2SubSdlNs, []string{key})
86 c.UpdateCounter(cSDLReadFailure)
87 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
89 xapp.Logger.Debug("SDL: Subscription read from e2SubsDb. subId = %v", subId)
92 subs := &Subscription{}
93 for _, iSubscriptionInfo := range retMap {
95 if iSubscriptionInfo == nil {
96 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
99 subscriptionInfo := &SubscriptionInfo{}
100 jsonSubscriptionInfo := iSubscriptionInfo.(string)
102 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
103 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
106 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
111 func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
113 subs := &Subscription{}
114 subs.registry = c.registry
115 subs.valid = subscriptionInfo.Valid
116 subs.ReqId = subscriptionInfo.ReqId
117 meid := xapp.RMRMeid{}
118 meid = subscriptionInfo.Meid
120 subs.EpList = subscriptionInfo.EpList
122 subReq := e2ap.E2APSubscriptionRequest{}
123 subReq = subscriptionInfo.SubReqMsg
124 subs.SubReqMsg = &subReq
125 subs.PolicyUpdate = subscriptionInfo.PolicyUpdate
127 if subscriptionInfo.SubRespRcvd == "SubResp" {
128 subs.SubRespRcvd = true
129 subResp := e2ap.E2APSubscriptionResponse{}
130 subResp = subscriptionInfo.SubRespMsg
131 subs.SubRFMsg = &subResp
133 subs.SubRespRcvd = false
135 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
140 func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
142 key := strconv.FormatUint(uint64(subId), 10)
143 if err := c.e2SubsDb.Remove(e2SubSdlNs, []string{key}); err != nil {
144 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
146 xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId)
151 func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
153 // Read all subscriptionInfos
156 for i = 1; i < 65535; i++ {
157 subIds = append(subIds, i)
160 retMap := make(map[uint32]*Subscription)
162 keys, err := c.e2SubsDb.GetAll(e2SubSdlNs)
164 c.UpdateCounter(cSDLReadFailure)
165 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
169 return subIds, retMap, nil
172 // Get all subscriptionInfos
173 iSubscriptionMap, err := c.e2SubsDb.Get(e2SubSdlNs, keys)
175 c.UpdateCounter(cSDLReadFailure)
176 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading E2 subscriptions from DBAAS %s\n", err.Error())
179 for _, iSubscriptionInfo := range iSubscriptionMap {
181 if iSubscriptionInfo == nil {
182 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
185 subscriptionInfo := &SubscriptionInfo{}
186 jsonSubscriptionInfo := iSubscriptionInfo.(string)
188 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
189 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
192 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
194 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
195 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
197 retMap[subscriptionInfo.ReqId.InstanceId] = subs
199 // Remove subId from free subIds. Original slice is modified here!
200 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
201 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
204 return subIds, retMap, nil
207 func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
208 for i, num := range s {
209 if removedNum == uint32(num) {
210 s = append(s[:i], s[i+1:]...)
211 return s[:len(s)], nil
214 return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
216 func (c *Control) RemoveAllSubscriptionsFromSdl() error {
218 if err := c.e2SubsDb.RemoveAll(e2SubSdlNs); err != nil {
219 c.UpdateCounter(cSDLRemoveFailure)
220 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
222 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")
227 func (c *Control) GetE2KeyCount() (int, error) {
230 keys, err := c.e2SubsDb.GetAll(e2SubSdlNs)
232 c.UpdateCounter(cSDLReadFailure)
233 return 0, fmt.Errorf("SDL: GetE2KeyCount(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
235 return len(keys), nil