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 | |
| 32 | type SubscriptionInfo struct { |
Anssi Mannila | f682ace | 2021-09-28 13:11:25 +0300 | [diff] [blame] | 33 | 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 Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func CreateSdl() Sdlnterface { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 44 | return sdl.NewSdlInstance("submgr_e2SubsDb", sdl.NewDatabase()) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func (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 Mannila | f682ace | 2021-09-28 13:11:25 +0300 | [diff] [blame] | 55 | subscriptionInfo.PolicyUpdate = subs.PolicyUpdate |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 56 | |
| 57 | if typeofSubsMessage(subs.SubRFMsg) == "SubResp" { |
| 58 | subscriptionInfo.SubRespRcvd = "SubResp" |
| 59 | subscriptionInfo.SubRespMsg = *subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 60 | } 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 Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 69 | if err = c.e2SubsDb.Set(strconv.FormatUint(uint64(subId), 10), jsonData); err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 70 | c.UpdateCounter(cSDLWriteFailure) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 71 | return fmt.Errorf("SDL: WriteSubscriptionToSdl(): %s", err.Error()) |
| 72 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 73 | xapp.Logger.Debug("SDL: Subscription written in e2SubsDb. subId = %v", subId) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 74 | } |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | func (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 Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 82 | retMap, err := c.e2SubsDb.Get([]string{key}) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 83 | if err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 84 | c.UpdateCounter(cSDLReadFailure) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 85 | return nil, fmt.Errorf("SDL: ReadSubscriptionFromSdl(): %s", err.Error()) |
| 86 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 87 | xapp.Logger.Debug("SDL: Subscription read from e2SubsDb. subId = %v", subId) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 88 | } |
| 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 Mannila | 6d629ad | 2021-01-25 09:59:56 +0200 | [diff] [blame] | 100 | if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 101 | 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 | |
| 109 | func (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 Mannila | f682ace | 2021-09-28 13:11:25 +0300 | [diff] [blame] | 123 | subs.PolicyUpdate = subscriptionInfo.PolicyUpdate |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 124 | |
| 125 | if subscriptionInfo.SubRespRcvd == "SubResp" { |
| 126 | subs.SubRespRcvd = true |
| 127 | subResp := e2ap.E2APSubscriptionResponse{} |
| 128 | subResp = subscriptionInfo.SubRespMsg |
| 129 | subs.SubRFMsg = &subResp |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 130 | } 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 | |
| 138 | func (c *Control) RemoveSubscriptionFromSdl(subId uint32) error { |
| 139 | |
| 140 | key := strconv.FormatUint(uint64(subId), 10) |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 141 | if err := c.e2SubsDb.Remove([]string{key}); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 142 | return fmt.Errorf("SDL: RemoveSubscriptionfromSdl(): %s\n", err.Error()) |
| 143 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 144 | xapp.Logger.Debug("SDL: Subscription removed from e2SubsDb. subId = %v", subId) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 145 | } |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | func (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 Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 160 | keys, err := c.e2SubsDb.GetAll() |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 161 | if err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 162 | c.UpdateCounter(cSDLReadFailure) |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 163 | 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] | 164 | } |
| 165 | |
| 166 | if len(keys) == 0 { |
| 167 | return subIds, retMap, nil |
| 168 | } |
| 169 | |
| 170 | // Get all subscriptionInfos |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 171 | iSubscriptionMap, err := c.e2SubsDb.Get(keys) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 172 | if err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 173 | c.UpdateCounter(cSDLReadFailure) |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 174 | 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] | 175 | } |
| 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 Mannila | 6d629ad | 2021-01-25 09:59:56 +0200 | [diff] [blame] | 186 | if err := json.Unmarshal([]byte(jsonSubscriptionInfo), subscriptionInfo); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 187 | 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 Mannila | 6b3796f | 2021-02-12 09:11:35 +0200 | [diff] [blame] | 198 | if subIds, err = removeNumber(subIds, subscriptionInfo.ReqId.InstanceId); err != nil { |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 199 | return nil, nil, fmt.Errorf("SDL: ReadAllSubscriptionsFromSdl() error: %s\n", err.Error()) |
| 200 | } |
| 201 | } |
| 202 | return subIds, retMap, nil |
| 203 | } |
| 204 | |
| 205 | func 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 | } |
| 214 | func (c *Control) RemoveAllSubscriptionsFromSdl() error { |
| 215 | |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 216 | if err := c.e2SubsDb.RemoveAll(); err != nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 217 | c.UpdateCounter(cSDLRemoveFailure) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 218 | return fmt.Errorf("SDL: RemoveAllSubscriptionsFromSdl(): %s\n", err.Error()) |
| 219 | } else { |
Konstantinos Archangelof | 268d715 | 2021-06-14 12:24:00 +0300 | [diff] [blame] | 220 | xapp.Logger.Debug("SDL: All subscriptions removed from e2SubsDb") |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 221 | } |
| 222 | return nil |
| 223 | } |