blob: 2afa36e306e9a8e78b4f561b8c46dc6d34787c96 [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"
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030025 "strconv"
26
Anssi Mannilac92b4212020-12-07 14:59:34 +020027 "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"
Anssi Mannilac92b4212020-12-07 14:59:34 +020030)
31
32type SubscriptionInfo struct {
Anssi Mannilaf682ace2021-09-28 13:11:25 +030033 Valid bool
34 ReqId RequestId
35 Meid xapp.RMRMeid
36 EpList xapp.RmrEndpointList
37 SubReqMsg e2ap.E2APSubscriptionRequest
38 SubRespMsg e2ap.E2APSubscriptionResponse
39 SubRespRcvd string
40 PolicyUpdate bool
Anssi Mannilac92b4212020-12-07 14:59:34 +020041}
42
43func CreateSdl() Sdlnterface {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030044 return sdl.NewSdlInstance("submgr_e2SubsDb", sdl.NewDatabase())
Anssi Mannilac92b4212020-12-07 14:59:34 +020045}
46
47func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error {
48
49 var subscriptionInfo SubscriptionInfo
50 subscriptionInfo.Valid = subs.valid
51 subscriptionInfo.ReqId = subs.ReqId
52 subscriptionInfo.Meid = *subs.Meid
53 subscriptionInfo.EpList = subs.EpList
54 subscriptionInfo.SubReqMsg = *subs.SubReqMsg
Anssi Mannilaf682ace2021-09-28 13:11:25 +030055 subscriptionInfo.PolicyUpdate = subs.PolicyUpdate
Anssi Mannilac92b4212020-12-07 14:59:34 +020056
57 if typeofSubsMessage(subs.SubRFMsg) == "SubResp" {
58 subscriptionInfo.SubRespRcvd = "SubResp"
59 subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
Anssi Mannilac92b4212020-12-07 14:59:34 +020060 } else {
61 subscriptionInfo.SubRespRcvd = ""
62 }
63
64 jsonData, err := json.Marshal(subscriptionInfo)
65 if err != nil {
66 return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error())
67 }
68
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030069 if err = c.e2SubsDb.Set(strconv.FormatUint(uint64(subId), 10), jsonData); err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +020070 c.UpdateCounter(cSDLWriteFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +020071 return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error())
72 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030073 xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +020074 }
75 return nil
76}
77
78func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) {
79
80 // This function is now just for testing purpose
81 key := strconv.FormatUint(uint64(subId), 10)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030082 retMap, err := c.e2SubsDb.Get([]string{key})
Anssi Mannilac92b4212020-12-07 14:59:34 +020083 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +020084 c.UpdateCounter(cSDLReadFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +020085 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error())
86 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030087 xapp.Logger.Debug("SDL: Subscription read from e2SubsDb. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +020088 }
89
90 subs := &Subscription{}
91 for _, iSubscriptionInfo := range retMap {
92
93 if iSubscriptionInfo == nil {
94 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId)
95 }
96
97 subscriptionInfo := &SubscriptionInfo{}
98 jsonSubscriptionInfo := iSubscriptionInfo.(string)
99
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200100 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200101 return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error())
102 }
103
104 subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
105 }
106 return subs, nil
107}
108
109func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription {
110
111 subs := &Subscription{}
112 subs.registry = c.registry
113 subs.valid = subscriptionInfo.Valid
114 subs.ReqId = subscriptionInfo.ReqId
115 meid := xapp.RMRMeid{}
116 meid = subscriptionInfo.Meid
117 subs.Meid = &meid
118 subs.EpList = subscriptionInfo.EpList
119 subs.TheTrans = nil
120 subReq := e2ap.E2APSubscriptionRequest{}
121 subReq = subscriptionInfo.SubReqMsg
122 subs.SubReqMsg = &subReq
Anssi Mannilaf682ace2021-09-28 13:11:25 +0300123 subs.PolicyUpdate = subscriptionInfo.PolicyUpdate
Anssi Mannilac92b4212020-12-07 14:59:34 +0200124
125 if subscriptionInfo.SubRespRcvd == "SubResp" {
126 subs.SubRespRcvd = true
127 subResp := e2ap.E2APSubscriptionResponse{}
128 subResp = subscriptionInfo.SubRespMsg
129 subs.SubRFMsg = &subResp
Anssi Mannilac92b4212020-12-07 14:59:34 +0200130 } else {
131 subs.SubRespRcvd = false
132 subs.SubRFMsg = nil
133 xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId)
134 }
135 return subs
136}
137
138func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error {
139
140 key := strconv.FormatUint(uint64(subId), 10)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300141 if err := c.e2SubsDb.Remove([]string{key}); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200142 return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error())
143 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300144 xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200145 }
146 return nil
147}
148
149func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) {
150
151 // Read all subscriptionInfos
152 var subIds []uint32
153 var i uint32
154 for i = 1; i < 65535; i++ {
155 subIds = append(subIds, i)
156 }
157
158 retMap := make(map[uint32]*Subscription)
159 // Get all keys
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300160 keys, err := c.e2SubsDb.GetAll()
Anssi Mannilac92b4212020-12-07 14:59:34 +0200161 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200162 c.UpdateCounter(cSDLReadFailure)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300163 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error())
Anssi Mannilac92b4212020-12-07 14:59:34 +0200164 }
165
166 if len(keys) == 0 {
167 return subIds, retMap, nil
168 }
169
170 // Get all subscriptionInfos
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300171 iSubscriptionMap, err := c.e2SubsDb.Get(keys)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200172 if err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200173 c.UpdateCounter(cSDLReadFailure)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300174 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading E2 subscriptions from DBAAS %s\n", err.Error())
Anssi Mannilac92b4212020-12-07 14:59:34 +0200175 }
176
177 for _, iSubscriptionInfo := range iSubscriptionMap {
178
179 if iSubscriptionInfo == nil {
180 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n")
181 }
182
183 subscriptionInfo := &SubscriptionInfo{}
184 jsonSubscriptionInfo := iSubscriptionInfo.(string)
185
Anssi Mannila6d629ad2021-01-25 09:59:56 +0200186 if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200187 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error())
188 }
189
190 subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo)
191
192 if int(subscriptionInfo.ReqId.InstanceId) >= len(subIds) {
193 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() index is out of range. Index is %d with slice length %d", subscriptionInfo.ReqId.InstanceId, len(subIds))
194 }
195 retMap[subscriptionInfo.ReqId.InstanceId] = subs
196
197 // Remove subId from free subIds. Original slice is modified here!
Anssi Mannila6b3796f2021-02-12 09:11:35 +0200198 if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil {
Anssi Mannilac92b4212020-12-07 14:59:34 +0200199 return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error())
200 }
201 }
202 return subIds, retMap, nil
203}
204
205func removeNumber(s []uint32, removedNum uint32) ([]uint32, error) {
206 for i, num := range s {
207 if removedNum == uint32(num) {
208 s = append(s[:i], s[i+1:]...)
209 return s[:len(s)], nil
210 }
211 }
212 return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum)
213}
214func (c *Control) RemoveAllSubscriptionsFromSdl() error {
215
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300216 if err := c.e2SubsDb.RemoveAll(); err != nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200217 c.UpdateCounter(cSDLRemoveFailure)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200218 return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error())
219 } else {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300220 xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb")
Anssi Mannilac92b4212020-12-07 14:59:34 +0200221 }
222 return nil
223}