blob: 0d7f845d000f711094ddc0fd6f5939e364f1b326 [file] [log] [blame]
Anssi Mannilac92b4212020-12-07 14:59:34 +02001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
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==================================================================================
18*/
19
20package control
21
22import (
23 "encoding/json"
24 "fmt"
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"
28 "strconv"
29)
30
31type SubscriptionInfo struct {
32 Valid bool
33 ReqId RequestId
34 Meid xapp.RMRMeid
35 EpList xapp.RmrEndpointList
36 SubReqMsg e2ap.E2APSubscriptionRequest
37 SubRespMsg e2ap.E2APSubscriptionResponse
38 SubFailMsg e2ap.E2APSubscriptionFailure
39 SubRespRcvd string
40}
41
42func CreateSdl() Sdlnterface {
43 return sdl.NewSdlInstance("submgr", sdl.NewDatabase())
44}
45
46func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
47
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
54
55 if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
56 subscriptionInfo.SubRespRcvd = "SubResp"
57 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
58 } else if typeofSubsMessage(subs.SubRFMsg) == "SubFail" {
59 subscriptionInfo.SubRespRcvd = "SubFail"
60 subscriptionInfo.SubFailMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionFailure)
61 } else {
62 subscriptionInfo.SubRespRcvd = ""
63 }
64
65 jsonData, err := json.Marshal(subscriptionInfo)
66 if err != nil {
67 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
68 }
69
Anssi Mannila6d629ad2021-01-25 09:59:56 +020070 if err = c.db.Set(strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +020071 c.UpdateCounter(cSDLWriteFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +020072 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
73 } else {
74 xapp.Logger.Debug("SDL: Subscription written in db. subId = %v", subId)
75 }
76 return nil
77}
78
79func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
80
81 // This function is now just for testing purpose
82 key := strconv.FormatUint(uint64(subId), 10)
83 retMap, err := c.db.Get([]string{key})
84 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +020085 c.UpdateCounter(cSDLReadFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +020086 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
87 } else {
88 xapp.Logger.Debug("SDL: Subscription read from db. subId = %v", subId)
89 }
90
91 subs := &Subscription{}
92 for _, iSubscriptionInfo := range retMap {
93
94 if iSubscriptionInfo == nil {
95 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
96 }
97
98 subscriptionInfo := &SubscriptionInfo{}
99 jsonSubscriptionInfo := iSubscriptionInfo.(string)
100
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200101 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200102 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
103 }
104
105 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
106 }
107 return subs, nil
108}
109
110func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
111
112 subs := &Subscription{}
113 subs.registry = c.registry
114 subs.valid = subscriptionInfo.Valid
115 subs.ReqId = subscriptionInfo.ReqId
116 meid := xapp.RMRMeid{}
117 meid = subscriptionInfo.Meid
118 subs.Meid = &meid
119 subs.EpList = subscriptionInfo.EpList
120 subs.TheTrans = nil
121 subReq := e2ap.E2APSubscriptionRequest{}
122 subReq = subscriptionInfo.SubReqMsg
123 subs.SubReqMsg = &subReq
124
125 if subscriptionInfo.SubRespRcvd == "SubResp" {
126 subs.SubRespRcvd = true
127 subResp := e2ap.E2APSubscriptionResponse{}
128 subResp = subscriptionInfo.SubRespMsg
129 subs.SubRFMsg = &subResp
130 } else if subscriptionInfo.SubRespRcvd == "SubFail" {
131 subs.SubRespRcvd = false
132 subFail := e2ap.E2APSubscriptionFailure{}
133 subFail = subscriptionInfo.SubFailMsg
134 subs.SubRFMsg = &subFail
135 } else {
136 subs.SubRespRcvd = false
137 subs.SubRFMsg = nil
138 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
139 }
140 return subs
141}
142
143func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
144
145 key := strconv.FormatUint(uint64(subId), 10)
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200146 if err := c.db.Remove([]string{key}); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200147 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
148 } else {
149 xapp.Logger.Debug("SDL: Subscription removed from db. subId = %v", subId)
150 }
151 return nil
152}
153
154func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
155
156 // Read all subscriptionInfos
157 var subIds []uint32
158 var i uint32
159 for i = 1; i < 65535; i++ {
160 subIds = append(subIds, i)
161 }
162
163 retMap := make(map[uint32]*Subscription)
164 // Get all keys
165 keys, err := c.db.GetAll()
166 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200167 c.UpdateCounter(cSDLReadFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200168 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading keys from DBAAS %s\n", err.Error())
169 }
170
171 if len(keys) == 0 {
172 return subIds, retMap, nil
173 }
174
175 // Get all subscriptionInfos
176 iSubscriptionMap, err := c.db.Get(keys)
177 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200178 c.UpdateCounter(cSDLReadFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200179 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading subscriptions from DBAAS %s\n", err.Error())
180 }
181
182 for _, iSubscriptionInfo := range iSubscriptionMap {
183
184 if iSubscriptionInfo == nil {
185 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
186 }
187
188 subscriptionInfo := &SubscriptionInfo{}
189 jsonSubscriptionInfo := iSubscriptionInfo.(string)
190
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200191 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200192 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
193 }
194
195 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
196
197 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
198 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
199 }
200 retMap[subscriptionInfo.ReqId.InstanceId] = subs
201
202 // Remove subId from free subIds. Original slice is modified here!
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200203 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200204 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
205 }
206 }
207 return subIds, retMap, nil
208}
209
210func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
211 for i, num := range s {
212 if removedNum == uint32(num) {
213 s = append(s[:i], s[i+1:]...)
214 return s[:len(s)], nil
215 }
216 }
217 return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
218}
219func (c *Control) RemoveAllSubscriptionsFromSdl() error {
220
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200221 if err := c.db.RemoveAll(); err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200222 c.UpdateCounter(cSDLRemoveFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200223 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
224 } else {
225 xapp.Logger.Debug("SDL: All subscriptions removed from db")
226 }
227 return nil
228}