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 type SubscriptionInfo struct {
36 EpList xapp.RmrEndpointList
37 SubReqMsg e2ap.E2APSubscriptionRequest
38 SubRespMsg e2ap.E2APSubscriptionResponse
42 func CreateSdl() Sdlnterface {
43 return sdl.NewSdlInstance("submgr_e2SubsDb", sdl.NewDatabase())
46 func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
48 var subscriptionInfo SubscriptionInfo
49 subscriptionInfo.Valid = subs.valid
50 subscriptionInfo.ReqId = subs.ReqId
51 subscriptionInfo.Meid = *subs.Meid
52 subscriptionInfo.EpList = subs.EpList
53 subscriptionInfo.SubReqMsg = *subs.SubReqMsg
55 if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
56 subscriptionInfo.SubRespRcvd = "SubResp"
57 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
59 subscriptionInfo.SubRespRcvd = ""
62 jsonData, err := json.Marshal(subscriptionInfo)
64 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
67 if err = c.e2SubsDb.Set(strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
68 c.UpdateCounter(cSDLWriteFailure)
69 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
71 xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId)
76 func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
78 // This function is now just for testing purpose
79 key := strconv.FormatUint(uint64(subId), 10)
80 retMap, err := c.e2SubsDb.Get([]string{key})
82 c.UpdateCounter(cSDLReadFailure)
83 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
85 xapp.Logger.Debug("SDL: Subscription read from e2SubsDb. subId = %v", subId)
88 subs := &Subscription{}
89 for _, iSubscriptionInfo := range retMap {
91 if iSubscriptionInfo == nil {
92 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
95 subscriptionInfo := &SubscriptionInfo{}
96 jsonSubscriptionInfo := iSubscriptionInfo.(string)
98 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
99 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
102 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
107 func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
109 subs := &Subscription{}
110 subs.registry = c.registry
111 subs.valid = subscriptionInfo.Valid
112 subs.ReqId = subscriptionInfo.ReqId
113 meid := xapp.RMRMeid{}
114 meid = subscriptionInfo.Meid
116 subs.EpList = subscriptionInfo.EpList
118 subReq := e2ap.E2APSubscriptionRequest{}
119 subReq = subscriptionInfo.SubReqMsg
120 subs.SubReqMsg = &subReq
122 if subscriptionInfo.SubRespRcvd == "SubResp" {
123 subs.SubRespRcvd = true
124 subResp := e2ap.E2APSubscriptionResponse{}
125 subResp = subscriptionInfo.SubRespMsg
126 subs.SubRFMsg = &subResp
128 subs.SubRespRcvd = false
130 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
135 func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
137 key := strconv.FormatUint(uint64(subId), 10)
138 if err := c.e2SubsDb.Remove([]string{key}); err != nil {
139 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
141 xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId)
146 func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
148 // Read all subscriptionInfos
151 for i = 1; i < 65535; i++ {
152 subIds = append(subIds, i)
155 retMap := make(map[uint32]*Subscription)
157 keys, err := c.e2SubsDb.GetAll()
159 c.UpdateCounter(cSDLReadFailure)
160 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
164 return subIds, retMap, nil
167 // Get all subscriptionInfos
168 iSubscriptionMap, err := c.e2SubsDb.Get(keys)
170 c.UpdateCounter(cSDLReadFailure)
171 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading E2 subscriptions from DBAAS %s\n", err.Error())
174 for _, iSubscriptionInfo := range iSubscriptionMap {
176 if iSubscriptionInfo == nil {
177 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
180 subscriptionInfo := &SubscriptionInfo{}
181 jsonSubscriptionInfo := iSubscriptionInfo.(string)
183 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
184 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
187 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
189 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
190 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
192 retMap[subscriptionInfo.ReqId.InstanceId] = subs
194 // Remove subId from free subIds. Original slice is modified here!
195 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
196 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
199 return subIds, retMap, nil
202 func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
203 for i, num := range s {
204 if removedNum == uint32(num) {
205 s = append(s[:i], s[i+1:]...)
206 return s[:len(s)], nil
209 return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
211 func (c *Control) RemoveAllSubscriptionsFromSdl() error {
213 if err := c.e2SubsDb.RemoveAll(); err != nil {
214 c.UpdateCounter(cSDLRemoveFailure)
215 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
217 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")