Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package control |
| 21 | |
| 22 | import ( |
| 23 | "encoding/json" |
| 24 | "fmt" |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 25 | "strconv" |
| 26 | |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 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" |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 32 | const e2SubSdlNs = "submgr_e2SubsDb" |
| 33 | |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 34 | type SubscriptionInfo struct { |
Anssi Mannila | f682ace | 2021-09-28 13:11:25 +0300 | [diff] [blame] | 35 | Valid bool |
| 36 | ReqId RequestId |
| 37 | Meid xapp.RMRMeid |
| 38 | EpList xapp.RmrEndpointList |
| 39 | SubReqMsg e2ap.E2APSubscriptionRequest |
| 40 | SubRespMsg e2ap.E2APSubscriptionResponse |
| 41 | SubRespRcvd string |
| 42 | PolicyUpdate bool |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func CreateSdl() Sdlnterface { |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 46 | return sdl.NewSyncStorage() |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func (c *Control) WriteSubscriptionToSdl(subId uint32, subs *Subscription) error { |
| 50 | |
| 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 |
Anssi Mannila | f682ace | 2021-09-28 13:11:25 +0300 | [diff] [blame] | 57 | subscriptionInfo.PolicyUpdate = subs.PolicyUpdate |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 58 | |
| 59 | if typeofSubsMessage(subs.SubRFMsg) == "SubResp" { |
| 60 | subscriptionInfo.SubRespRcvd = "SubResp" |
| 61 | subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 62 | } else { |
| 63 | subscriptionInfo.SubRespRcvd = "" |
| 64 | } |
| 65 | |
| 66 | jsonData, err := json.Marshal(subscriptionInfo) |
| 67 | if err != nil { |
| 68 | return fmt.Errorf("SDL: WriteSubscriptionToSdl() json.Marshal error: %s", err.Error()) |
| 69 | } |
| 70 | |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 71 | if err = c.e2SubsDb.Set(e2SubSdlNs, strconv.FormatUint(uint64(subId), 10), jsonData); err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 72 | c.UpdateCounter(cSDLWriteFailure) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 73 | return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error()) |
| 74 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 75 | xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 76 | } |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | func (c *Control) ReadSubscriptionFromSdl(subId uint32) (*Subscription, error) { |
| 81 | |
| 82 | // This function is now just for testing purpose |
| 83 | key := strconv.FormatUint(uint64(subId), 10) |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 84 | retMap, err := c.e2SubsDb.Get(e2SubSdlNs, []string{key}) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 85 | if err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 86 | c.UpdateCounter(cSDLReadFailure) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 87 | return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error()) |
| 88 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 89 | xapp.Logger.Debug("SDL: Subscription read from e2SubsDb. subId = %v", subId) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | subs := &Subscription{} |
| 93 | for _, iSubscriptionInfo := range retMap { |
| 94 | |
| 95 | if iSubscriptionInfo == nil { |
| 96 | return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() subscription not found. subId = %v\n", subId) |
| 97 | } |
| 98 | |
| 99 | subscriptionInfo := &SubscriptionInfo{} |
| 100 | jsonSubscriptionInfo := iSubscriptionInfo.(string) |
| 101 | |
Anssi Mannila | 6d629ad | 2021-01-25 09:59:56 +0200 | [diff] [blame] | 102 | if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 103 | return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl() json.unmarshal error: %s\n", err.Error()) |
| 104 | } |
| 105 | |
| 106 | subs = c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo) |
| 107 | } |
| 108 | return subs, nil |
| 109 | } |
| 110 | |
| 111 | func (c *Control) CreateSubscription(subscriptionInfo *SubscriptionInfo, jsonSubscriptionInfo *string) *Subscription { |
| 112 | |
| 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 |
| 119 | subs.Meid = &meid |
| 120 | subs.EpList = subscriptionInfo.EpList |
| 121 | subs.TheTrans = nil |
| 122 | subReq := e2ap.E2APSubscriptionRequest{} |
| 123 | subReq = subscriptionInfo.SubReqMsg |
| 124 | subs.SubReqMsg = &subReq |
Anssi Mannila | f682ace | 2021-09-28 13:11:25 +0300 | [diff] [blame] | 125 | subs.PolicyUpdate = subscriptionInfo.PolicyUpdate |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 126 | |
| 127 | if subscriptionInfo.SubRespRcvd == "SubResp" { |
| 128 | subs.SubRespRcvd = true |
| 129 | subResp := e2ap.E2APSubscriptionResponse{} |
| 130 | subResp = subscriptionInfo.SubRespMsg |
| 131 | subs.SubRFMsg = &subResp |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 132 | } else { |
| 133 | subs.SubRespRcvd = false |
| 134 | subs.SubRFMsg = nil |
| 135 | xapp.Logger.Debug("SDL: CreateSubscription() subscriptionInfo.SubRespRcvd == '', InstanceId=%v ", subscriptionInfo.ReqId.InstanceId) |
| 136 | } |
| 137 | return subs |
| 138 | } |
| 139 | |
| 140 | func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error { |
| 141 | |
| 142 | key := strconv.FormatUint(uint64(subId), 10) |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 143 | if err := c.e2SubsDb.Remove(e2SubSdlNs, []string{key}); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 144 | return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error()) |
| 145 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 146 | xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func (c *Control) ReadAllSubscriptionsFromSdl() ([]uint32, map[uint32]*Subscription, error) { |
| 152 | |
| 153 | // Read all subscriptionInfos |
| 154 | var subIds []uint32 |
| 155 | var i uint32 |
| 156 | for i = 1; i < 65535; i++ { |
| 157 | subIds = append(subIds, i) |
| 158 | } |
| 159 | |
| 160 | retMap := make(map[uint32]*Subscription) |
| 161 | // Get all keys |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 162 | keys, err := c.e2SubsDb.GetAll(e2SubSdlNs) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 163 | if err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 164 | c.UpdateCounter(cSDLReadFailure) |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 165 | return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error()) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if len(keys) == 0 { |
| 169 | return subIds, retMap, nil |
| 170 | } |
| 171 | |
| 172 | // Get all subscriptionInfos |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 173 | iSubscriptionMap, err := c.e2SubsDb.Get(e2SubSdlNs, keys) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 174 | if err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 175 | c.UpdateCounter(cSDLReadFailure) |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 176 | return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl(), Get(): Error while reading E2 subscriptions from DBAAS %s\n", err.Error()) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | for _, iSubscriptionInfo := range iSubscriptionMap { |
| 180 | |
| 181 | if iSubscriptionInfo == nil { |
| 182 | return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() iSubscriptionInfo = nil\n") |
| 183 | } |
| 184 | |
| 185 | subscriptionInfo := &SubscriptionInfo{} |
| 186 | jsonSubscriptionInfo := iSubscriptionInfo.(string) |
| 187 | |
Anssi Mannila | 6d629ad | 2021-01-25 09:59:56 +0200 | [diff] [blame] | 188 | if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 189 | return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() json.unmarshal error: %s\n", err.Error()) |
| 190 | } |
| 191 | |
| 192 | subs := c.CreateSubscription(subscriptionInfo, &jsonSubscriptionInfo) |
| 193 | |
| 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)) |
| 196 | } |
| 197 | retMap[subscriptionInfo.ReqId.InstanceId] = subs |
| 198 | |
| 199 | // Remove subId from free subIds. Original slice is modified here! |
Anssi Mannila | 6b3796f | 2021-02-12 09:11:35 +0200 | [diff] [blame] | 200 | if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 201 | return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error()) |
| 202 | } |
| 203 | } |
| 204 | return subIds, retMap, nil |
| 205 | } |
| 206 | |
| 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 |
| 212 | } |
| 213 | } |
| 214 | return nil, fmt.Errorf("SDL: To be removed number not in the slice. removedNum: %v", removedNum) |
| 215 | } |
| 216 | func (c *Control) RemoveAllSubscriptionsFromSdl() error { |
| 217 | |
Timo Tietavainen | f0bb66c | 2021-10-08 09:16:20 +0300 | [diff] [blame] | 218 | if err := c.e2SubsDb.RemoveAll(e2SubSdlNs); err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 219 | c.UpdateCounter(cSDLRemoveFailure) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 220 | return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error()) |
| 221 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 222 | xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb") |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 223 | } |
| 224 | return nil |
| 225 | } |
Anssi Mannila | afadb6d | 2021-11-15 15:03:12 +0200 | [diff] [blame] | 226 | |
| 227 | func (c *Control) GetE2KeyCount() (int, error) { |
| 228 | |
| 229 | // Get all keys |
| 230 | keys, err := c.e2SubsDb.GetAll(e2SubSdlNs) |
| 231 | if err != nil { |
| 232 | c.UpdateCounter(cSDLReadFailure) |
| 233 | return 0, fmt.Errorf("SDL: GetE2KeyCount(), GetAll(). Error while reading E2 subscriptions keys from DBAAS %s\n", err.Error()) |
| 234 | } |
| 235 | return len(keys), nil |
| 236 | } |