Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package ponresourcemanager |
| 18 | |
| 19 | import ( |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 20 | "encoding/base64" |
| 21 | "encoding/json" |
| 22 | "errors" |
| 23 | "fmt" |
| 24 | "strconv" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 25 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 26 | bitmap "github.com/boljen/go-bitmap" |
| 27 | "github.com/opencord/voltha-go/common/log" |
| 28 | tp "github.com/opencord/voltha-go/common/techprofile" |
| 29 | "github.com/opencord/voltha-go/db/kvstore" |
| 30 | "github.com/opencord/voltha-go/db/model" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | const ( |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 34 | //Constants to identify resource pool |
| 35 | UNI_ID = "UNI_ID" |
| 36 | ONU_ID = "ONU_ID" |
| 37 | ALLOC_ID = "ALLOC_ID" |
| 38 | GEMPORT_ID = "GEMPORT_ID" |
| 39 | FLOW_ID = "FLOW_ID" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 40 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 41 | //Constants for passing command line arugments |
| 42 | OLT_MODEL_ARG = "--olt_model" |
| 43 | PATH_PREFIX = "service/voltha/resource_manager/{%s}" |
| 44 | /*The resource ranges for a given device model should be placed |
| 45 | at 'resource_manager/<technology>/resource_ranges/<olt_model_type>' |
| 46 | path on the KV store. |
| 47 | If Resource Range parameters are to be read from the external KV store, |
| 48 | they are expected to be stored in the following format. |
| 49 | Note: All parameters are MANDATORY for now. |
| 50 | constants used as keys to reference the resource range parameters from |
| 51 | and external KV store. |
| 52 | */ |
| 53 | UNI_ID_START_IDX = "uni_id_start" |
| 54 | UNI_ID_END_IDX = "uni_id_end" |
| 55 | ONU_ID_START_IDX = "onu_id_start" |
| 56 | ONU_ID_END_IDX = "onu_id_end" |
| 57 | ONU_ID_SHARED_IDX = "onu_id_shared" |
| 58 | ALLOC_ID_START_IDX = "alloc_id_start" |
| 59 | ALLOC_ID_END_IDX = "alloc_id_end" |
| 60 | ALLOC_ID_SHARED_IDX = "alloc_id_shared" |
| 61 | GEMPORT_ID_START_IDX = "gemport_id_start" |
| 62 | GEMPORT_ID_END_IDX = "gemport_id_end" |
| 63 | GEMPORT_ID_SHARED_IDX = "gemport_id_shared" |
| 64 | FLOW_ID_START_IDX = "flow_id_start" |
| 65 | FLOW_ID_END_IDX = "flow_id_end" |
| 66 | FLOW_ID_SHARED_IDX = "flow_id_shared" |
| 67 | NUM_OF_PON_PORT = "pon_ports" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 68 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 69 | /* |
| 70 | The KV store backend is initialized with a path prefix and we need to |
| 71 | provide only the suffix. |
| 72 | */ |
| 73 | PON_RESOURCE_RANGE_CONFIG_PATH = "resource_ranges/%s" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 74 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 75 | //resource path suffix |
| 76 | //Path on the KV store for storing alloc id ranges and resource pool for a given interface |
| 77 | //Format: <device_id>/alloc_id_pool/<pon_intf_id> |
| 78 | ALLOC_ID_POOL_PATH = "{%s}/alloc_id_pool/{%d}" |
| 79 | //Path on the KV store for storing gemport id ranges and resource pool for a given interface |
| 80 | //Format: <device_id>/gemport_id_pool/<pon_intf_id> |
| 81 | GEMPORT_ID_POOL_PATH = "{%s}/gemport_id_pool/{%d}" |
| 82 | //Path on the KV store for storing onu id ranges and resource pool for a given interface |
| 83 | //Format: <device_id>/onu_id_pool/<pon_intf_id> |
| 84 | ONU_ID_POOL_PATH = "{%s}/onu_id_pool/{%d}" |
| 85 | //Path on the KV store for storing flow id ranges and resource pool for a given interface |
| 86 | //Format: <device_id>/flow_id_pool/<pon_intf_id> |
| 87 | FLOW_ID_POOL_PATH = "{%s}/flow_id_pool/{%d}" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 88 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 89 | //Path on the KV store for storing list of alloc IDs for a given ONU |
| 90 | //Format: <device_id>/<(pon_intf_id, onu_id)>/alloc_ids |
| 91 | ALLOC_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/alloc_ids" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 92 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 93 | //Path on the KV store for storing list of gemport IDs for a given ONU |
| 94 | //Format: <device_id>/<(pon_intf_id, onu_id)>/gemport_ids |
| 95 | GEMPORT_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/gemport_ids" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 96 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 97 | //Path on the KV store for storing list of Flow IDs for a given ONU |
| 98 | //Format: <device_id>/<(pon_intf_id, onu_id)>/flow_ids |
| 99 | FLOW_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/flow_ids" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 100 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 101 | //Flow Id info: Use to store more metadata associated with the flow_id |
| 102 | //Format: <device_id>/<(pon_intf_id, onu_id)>/flow_id_info/<flow_id> |
| 103 | FLOW_ID_INFO_PATH = "{%s}/{%s}/flow_id_info/{%d}" |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 104 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 105 | //Constants for internal usage. |
| 106 | PON_INTF_ID = "pon_intf_id" |
| 107 | START_IDX = "start_idx" |
| 108 | END_IDX = "end_idx" |
| 109 | POOL = "pool" |
| 110 | NUM_OF_PON_INTF = 16 |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 111 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 112 | KVSTORE_RETRY_TIMEOUT = 5 |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 113 | ) |
| 114 | |
| 115 | //type ResourceTypeIndex string |
| 116 | //type ResourceType string |
| 117 | |
| 118 | type PONResourceManager struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 119 | //Implements APIs to initialize/allocate/release alloc/gemport/onu IDs. |
| 120 | Technology string |
| 121 | DeviceType string |
| 122 | DeviceID string |
| 123 | Backend string // ETCD, or consul |
| 124 | Host string // host ip of the KV store |
| 125 | Port int // port number for the KV store |
| 126 | OLTModel string |
| 127 | KVStore *model.Backend |
| 128 | TechProfileMgr *tp.TechProfileMgr |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 129 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 130 | // Below attribute, pon_resource_ranges, should be initialized |
| 131 | // by reading from KV store. |
| 132 | PonResourceRanges map[string]interface{} |
| 133 | SharedResourceMgrs map[string]*PONResourceManager |
| 134 | SharedIdxByType map[string]string |
| 135 | IntfIDs []uint32 // list of pon interface IDs |
manikkaraj k | 6c9689d | 2019-05-09 12:59:52 -0400 | [diff] [blame] | 136 | Globalorlocal string |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 140 | log.Infow("kv-store-type", log.Fields{"store": storeType}) |
| 141 | switch storeType { |
| 142 | case "consul": |
| 143 | return kvstore.NewConsulClient(address, timeout) |
| 144 | case "etcd": |
| 145 | return kvstore.NewEtcdClient(address, timeout) |
| 146 | } |
| 147 | return nil, errors.New("unsupported-kv-store") |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | func SetKVClient(Technology string, Backend string, Host string, Port int) *model.Backend { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 151 | addr := Host + ":" + strconv.Itoa(Port) |
| 152 | // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some |
| 153 | // issue between kv store and backend , core is not calling NewBackend directly |
| 154 | kvClient, err := newKVClient(Backend, addr, KVSTORE_RETRY_TIMEOUT) |
| 155 | if err != nil { |
| 156 | log.Fatalw("Failed to init KV client\n", log.Fields{"err": err}) |
| 157 | return nil |
| 158 | } |
| 159 | kvbackend := &model.Backend{ |
| 160 | Client: kvClient, |
| 161 | StoreType: Backend, |
| 162 | Host: Host, |
| 163 | Port: Port, |
| 164 | Timeout: KVSTORE_RETRY_TIMEOUT, |
| 165 | PathPrefix: fmt.Sprintf(PATH_PREFIX, Technology)} |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 166 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 167 | return kvbackend |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // NewPONResourceManager creates a new PON resource manager. |
| 171 | func NewPONResourceManager(Technology string, DeviceType string, DeviceID string, Backend string, Host string, Port int) (*PONResourceManager, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 172 | var PONMgr PONResourceManager |
| 173 | PONMgr.Technology = Technology |
| 174 | PONMgr.DeviceType = DeviceType |
| 175 | PONMgr.DeviceID = DeviceID |
| 176 | PONMgr.Backend = Backend |
| 177 | PONMgr.Host = Host |
| 178 | PONMgr.Port = Port |
| 179 | PONMgr.KVStore = SetKVClient(Technology, Backend, Host, Port) |
| 180 | if PONMgr.KVStore == nil { |
| 181 | log.Error("KV Client initilization failed") |
| 182 | return nil, errors.New("Failed to init KV client") |
| 183 | } |
| 184 | // Initialize techprofile for this technology |
manikkaraj k | 53de550 | 2019-05-12 05:11:53 -0400 | [diff] [blame] | 185 | if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(&PONMgr, Backend, Host, Port); PONMgr.TechProfileMgr == nil { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 186 | log.Error("Techprofile initialization failed") |
| 187 | return nil, errors.New("Failed to init tech profile") |
| 188 | } |
| 189 | PONMgr.PonResourceRanges = make(map[string]interface{}) |
| 190 | PONMgr.SharedResourceMgrs = make(map[string]*PONResourceManager) |
| 191 | PONMgr.SharedIdxByType = make(map[string]string) |
| 192 | PONMgr.SharedIdxByType[ONU_ID] = ONU_ID_SHARED_IDX |
| 193 | PONMgr.SharedIdxByType[ALLOC_ID] = ALLOC_ID_SHARED_IDX |
| 194 | PONMgr.SharedIdxByType[GEMPORT_ID] = GEMPORT_ID_SHARED_IDX |
| 195 | PONMgr.SharedIdxByType[FLOW_ID] = FLOW_ID_SHARED_IDX |
| 196 | PONMgr.IntfIDs = make([]uint32, NUM_OF_PON_INTF) |
| 197 | PONMgr.OLTModel = DeviceType |
| 198 | return &PONMgr, nil |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
| 202 | Initialize PON resource ranges with config fetched from kv store. |
| 203 | return boolean: True if PON resource ranges initialized else false |
| 204 | Try to initialize the PON Resource Ranges from KV store based on the |
| 205 | OLT model key, if available |
| 206 | */ |
| 207 | |
| 208 | func (PONRMgr *PONResourceManager) InitResourceRangesFromKVStore() bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 209 | //Initialize PON resource ranges with config fetched from kv store. |
| 210 | //:return boolean: True if PON resource ranges initialized else false |
| 211 | // Try to initialize the PON Resource Ranges from KV store based on the |
| 212 | // OLT model key, if available |
| 213 | if PONRMgr.OLTModel == "" { |
| 214 | log.Error("Failed to get OLT model") |
| 215 | return false |
| 216 | } |
| 217 | Path := fmt.Sprintf(PON_RESOURCE_RANGE_CONFIG_PATH, PONRMgr.OLTModel) |
| 218 | //get resource from kv store |
| 219 | Result, err := PONRMgr.KVStore.Get(Path) |
| 220 | if err != nil { |
| 221 | log.Debugf("Error in fetching resource %s from KV strore", Path) |
| 222 | return false |
| 223 | } |
| 224 | if Result == nil { |
| 225 | log.Debug("There may be no resources in the KV store in case of fresh bootup, return true") |
| 226 | return false |
| 227 | } |
| 228 | //update internal ranges from kv ranges. If there are missing |
| 229 | // values in the KV profile, continue to use the defaults |
| 230 | Value, err := ToByte(Result.Value) |
| 231 | if err != nil { |
| 232 | log.Error("Failed to convert kvpair to byte string") |
| 233 | return false |
| 234 | } |
| 235 | if err := json.Unmarshal(Value, &PONRMgr.PonResourceRanges); err != nil { |
| 236 | log.Error("Failed to Unmarshal json byte") |
| 237 | return false |
| 238 | } |
| 239 | log.Debug("Init resource ranges from kvstore success") |
| 240 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | func (PONRMgr *PONResourceManager) UpdateRanges(StartIDx string, StartID uint32, EndIDx string, EndID uint32, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 244 | SharedIDx string, SharedPoolID uint32, RMgr *PONResourceManager) { |
| 245 | /* |
| 246 | Update the ranges for all reosurce type in the intermnal maps |
| 247 | param: resource type start index |
| 248 | param: start ID |
| 249 | param: resource type end index |
| 250 | param: end ID |
| 251 | param: resource type shared index |
| 252 | param: shared pool id |
| 253 | param: global resource manager |
| 254 | */ |
| 255 | log.Debugf("update ranges for %s, %d", StartIDx, StartID) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 256 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 257 | if StartID != 0 { |
manikkaraj k | 6c9689d | 2019-05-09 12:59:52 -0400 | [diff] [blame] | 258 | if (PONRMgr.PonResourceRanges[StartIDx] == nil) || (PONRMgr.PonResourceRanges[StartIDx].(uint32) < StartID) { |
| 259 | PONRMgr.PonResourceRanges[StartIDx] = StartID |
| 260 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 261 | } |
| 262 | if EndID != 0 { |
manikkaraj k | 6c9689d | 2019-05-09 12:59:52 -0400 | [diff] [blame] | 263 | if (PONRMgr.PonResourceRanges[EndIDx] == nil) || (PONRMgr.PonResourceRanges[EndIDx].(uint32) > EndID) { |
| 264 | PONRMgr.PonResourceRanges[EndIDx] = EndID |
| 265 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 266 | } |
| 267 | //if SharedPoolID != 0 { |
| 268 | PONRMgr.PonResourceRanges[SharedIDx] = SharedPoolID |
| 269 | //} |
| 270 | if RMgr != nil { |
| 271 | PONRMgr.SharedResourceMgrs[SharedIDx] = RMgr |
| 272 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ONUIDStart uint32, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 276 | ONUIDEnd uint32, |
| 277 | ONUIDSharedPoolID uint32, |
| 278 | AllocIDStart uint32, |
| 279 | AllocIDEnd uint32, |
| 280 | AllocIDSharedPoolID uint32, |
| 281 | GEMPortIDStart uint32, |
| 282 | GEMPortIDEnd uint32, |
| 283 | GEMPortIDSharedPoolID uint32, |
| 284 | FlowIDStart uint32, |
| 285 | FlowIDEnd uint32, |
| 286 | FlowIDSharedPoolID uint32, |
| 287 | UNIIDStart uint32, |
| 288 | UNIIDEnd uint32, |
| 289 | NoOfPONPorts uint32, |
| 290 | IntfIDs []uint32) bool { |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 291 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 292 | /*Initialize default PON resource ranges |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 293 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 294 | :param onu_id_start_idx: onu id start index |
| 295 | :param onu_id_end_idx: onu id end index |
| 296 | :param onu_id_shared_pool_id: pool idx for id shared by all intfs or None for no sharing |
| 297 | :param alloc_id_start_idx: alloc id start index |
| 298 | :param alloc_id_end_idx: alloc id end index |
| 299 | :param alloc_id_shared_pool_id: pool idx for alloc id shared by all intfs or None for no sharing |
| 300 | :param gemport_id_start_idx: gemport id start index |
| 301 | :param gemport_id_end_idx: gemport id end index |
| 302 | :param gemport_id_shared_pool_id: pool idx for gemport id shared by all intfs or None for no sharing |
| 303 | :param flow_id_start_idx: flow id start index |
| 304 | :param flow_id_end_idx: flow id end index |
| 305 | :param flow_id_shared_pool_id: pool idx for flow id shared by all intfs or None for no sharing |
| 306 | :param num_of_pon_ports: number of PON ports |
| 307 | :param intf_ids: interfaces serviced by this manager |
| 308 | */ |
| 309 | PONRMgr.UpdateRanges(ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil) |
| 310 | PONRMgr.UpdateRanges(ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil) |
| 311 | PONRMgr.UpdateRanges(GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil) |
| 312 | PONRMgr.UpdateRanges(FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil) |
| 313 | PONRMgr.UpdateRanges(UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil) |
| 314 | log.Debug("Initialize default range values") |
| 315 | var i uint32 |
| 316 | if IntfIDs == nil { |
| 317 | for i = 0; i < NoOfPONPorts; i++ { |
| 318 | PONRMgr.IntfIDs = append(PONRMgr.IntfIDs, i) |
| 319 | } |
| 320 | } else { |
| 321 | PONRMgr.IntfIDs = IntfIDs |
| 322 | } |
| 323 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | func (PONRMgr *PONResourceManager) InitDeviceResourcePool() error { |
| 327 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 328 | //Initialize resource pool for all PON ports. |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 329 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 330 | log.Debug("Init resource ranges") |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 331 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 332 | var err error |
| 333 | for _, Intf := range PONRMgr.IntfIDs { |
| 334 | SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32) |
| 335 | if SharedPoolID != 0 { |
| 336 | Intf = SharedPoolID |
| 337 | } |
| 338 | if err = PONRMgr.InitResourceIDPool(Intf, ONU_ID, |
| 339 | PONRMgr.PonResourceRanges[ONU_ID_START_IDX].(uint32), |
| 340 | PONRMgr.PonResourceRanges[ONU_ID_END_IDX].(uint32)); err != nil { |
| 341 | log.Error("Failed to init ONU ID resource pool") |
| 342 | return err |
| 343 | } |
| 344 | if SharedPoolID != 0 { |
| 345 | break |
| 346 | } |
| 347 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 348 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 349 | for _, Intf := range PONRMgr.IntfIDs { |
| 350 | SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32) |
| 351 | if SharedPoolID != 0 { |
| 352 | Intf = SharedPoolID |
| 353 | } |
| 354 | if err = PONRMgr.InitResourceIDPool(Intf, ALLOC_ID, |
| 355 | PONRMgr.PonResourceRanges[ALLOC_ID_START_IDX].(uint32), |
| 356 | PONRMgr.PonResourceRanges[ALLOC_ID_END_IDX].(uint32)); err != nil { |
| 357 | log.Error("Failed to init ALLOC ID resource pool ") |
| 358 | return err |
| 359 | } |
| 360 | if SharedPoolID != 0 { |
| 361 | break |
| 362 | } |
| 363 | } |
| 364 | for _, Intf := range PONRMgr.IntfIDs { |
| 365 | SharedPoolID := PONRMgr.PonResourceRanges[GEMPORT_ID_SHARED_IDX].(uint32) |
| 366 | if SharedPoolID != 0 { |
| 367 | Intf = SharedPoolID |
| 368 | } |
| 369 | if err = PONRMgr.InitResourceIDPool(Intf, GEMPORT_ID, |
| 370 | PONRMgr.PonResourceRanges[GEMPORT_ID_START_IDX].(uint32), |
| 371 | PONRMgr.PonResourceRanges[GEMPORT_ID_END_IDX].(uint32)); err != nil { |
| 372 | log.Error("Failed to init GEMPORT ID resource pool") |
| 373 | return err |
| 374 | } |
| 375 | if SharedPoolID != 0 { |
| 376 | break |
| 377 | } |
| 378 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 379 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 380 | for _, Intf := range PONRMgr.IntfIDs { |
| 381 | SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32) |
| 382 | if SharedPoolID != 0 { |
| 383 | Intf = SharedPoolID |
| 384 | } |
| 385 | if err = PONRMgr.InitResourceIDPool(Intf, FLOW_ID, |
| 386 | PONRMgr.PonResourceRanges[FLOW_ID_START_IDX].(uint32), |
| 387 | PONRMgr.PonResourceRanges[FLOW_ID_END_IDX].(uint32)); err != nil { |
| 388 | log.Error("Failed to init FLOW ID resource pool") |
| 389 | return err |
| 390 | } |
| 391 | if SharedPoolID != 0 { |
| 392 | break |
| 393 | } |
| 394 | } |
| 395 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 396 | } |
| 397 | |
Devmalya Paul | 63e7d33 | 2019-08-27 19:31:44 -0400 | [diff] [blame] | 398 | func (PONRMgr *PONResourceManager) ClearDeviceResourcePool() error { |
| 399 | |
| 400 | //Clear resource pool for all PON ports. |
| 401 | |
| 402 | log.Debug("Clear resource ranges") |
| 403 | |
| 404 | for _, Intf := range PONRMgr.IntfIDs { |
| 405 | SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32) |
| 406 | if SharedPoolID != 0 { |
| 407 | Intf = SharedPoolID |
| 408 | } |
| 409 | if status := PONRMgr.ClearResourceIDPool(Intf, ONU_ID); status != true { |
| 410 | log.Error("Failed to clear ONU ID resource pool") |
| 411 | return errors.New("Failed to clear ONU ID resource pool") |
| 412 | } |
| 413 | if SharedPoolID != 0 { |
| 414 | break |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | for _, Intf := range PONRMgr.IntfIDs { |
| 419 | SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32) |
| 420 | if SharedPoolID != 0 { |
| 421 | Intf = SharedPoolID |
| 422 | } |
| 423 | if status := PONRMgr.ClearResourceIDPool(Intf, ALLOC_ID); status != true { |
| 424 | log.Error("Failed to clear ALLOC ID resource pool ") |
| 425 | return errors.New("Failed to clear ALLOC ID resource pool") |
| 426 | } |
| 427 | if SharedPoolID != 0 { |
| 428 | break |
| 429 | } |
| 430 | } |
| 431 | for _, Intf := range PONRMgr.IntfIDs { |
| 432 | SharedPoolID := PONRMgr.PonResourceRanges[GEMPORT_ID_SHARED_IDX].(uint32) |
| 433 | if SharedPoolID != 0 { |
| 434 | Intf = SharedPoolID |
| 435 | } |
| 436 | if status := PONRMgr.ClearResourceIDPool(Intf, GEMPORT_ID); status != true { |
| 437 | log.Error("Failed to clear GEMPORT ID resource pool") |
| 438 | return errors.New("Failed to clear GEMPORT ID resource pool") |
| 439 | } |
| 440 | if SharedPoolID != 0 { |
| 441 | break |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | for _, Intf := range PONRMgr.IntfIDs { |
| 446 | SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32) |
| 447 | if SharedPoolID != 0 { |
| 448 | Intf = SharedPoolID |
| 449 | } |
| 450 | if status := PONRMgr.ClearResourceIDPool(Intf, FLOW_ID); status != true { |
| 451 | log.Error("Failed to clear FLOW ID resource pool") |
| 452 | return errors.New("Failed to clear FLOW ID resource pool") |
| 453 | } |
| 454 | if SharedPoolID != 0 { |
| 455 | break |
| 456 | } |
| 457 | } |
| 458 | return nil |
| 459 | } |
| 460 | |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 461 | func (PONRMgr *PONResourceManager) InitResourceIDPool(Intf uint32, ResourceType string, StartID uint32, EndID uint32) error { |
| 462 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 463 | /*Initialize Resource ID pool for a given Resource Type on a given PON Port |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 464 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 465 | :param pon_intf_id: OLT PON interface id |
| 466 | :param resource_type: String to identify type of resource |
| 467 | :param start_idx: start index for onu id pool |
| 468 | :param end_idx: end index for onu id pool |
| 469 | :return boolean: True if resource id pool initialized else false |
| 470 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 471 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 472 | // delegate to the master instance if sharing enabled across instances |
| 473 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 474 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
| 475 | return SharedResourceMgr.InitResourceIDPool(Intf, ResourceType, StartID, EndID) |
| 476 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 477 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 478 | Path := PONRMgr.GetPath(Intf, ResourceType) |
| 479 | if Path == "" { |
| 480 | log.Errorf("Failed to get path for resource type %s", ResourceType) |
| 481 | return errors.New(fmt.Sprintf("Failed to get path for resource type %s", ResourceType)) |
| 482 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 483 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 484 | //In case of adapter reboot and reconciliation resource in kv store |
| 485 | //checked for its presence if not kv store update happens |
| 486 | Res, err := PONRMgr.GetResource(Path) |
| 487 | if (err == nil) && (Res != nil) { |
| 488 | log.Debugf("Resource %s already present in store ", Path) |
| 489 | return nil |
| 490 | } else { |
| 491 | FormatResult, err := PONRMgr.FormatResource(Intf, StartID, EndID) |
| 492 | if err != nil { |
| 493 | log.Errorf("Failed to format resource") |
| 494 | return err |
| 495 | } |
| 496 | // Add resource as json in kv store. |
| 497 | err = PONRMgr.KVStore.Put(Path, FormatResult) |
| 498 | if err == nil { |
| 499 | log.Debug("Successfuly posted to kv store") |
| 500 | return err |
| 501 | } |
| 502 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 503 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 504 | log.Debug("Error initializing pool") |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 505 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 506 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | func (PONRMgr *PONResourceManager) FormatResource(IntfID uint32, StartIDx uint32, EndIDx uint32) ([]byte, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 510 | /* |
| 511 | Format resource as json. |
| 512 | :param pon_intf_id: OLT PON interface id |
| 513 | :param start_idx: start index for id pool |
| 514 | :param end_idx: end index for id pool |
| 515 | :return dictionary: resource formatted as map |
| 516 | */ |
| 517 | // Format resource as json to be stored in backend store |
| 518 | Resource := make(map[string]interface{}) |
| 519 | Resource[PON_INTF_ID] = IntfID |
| 520 | Resource[START_IDX] = StartIDx |
| 521 | Resource[END_IDX] = EndIDx |
| 522 | /* |
| 523 | Resource pool stored in backend store as binary string. |
| 524 | Tracking the resource allocation will be done by setting the bits \ |
| 525 | in the byte array. The index set will be the resource number allocated. |
| 526 | */ |
| 527 | var TSData *bitmap.Threadsafe |
| 528 | if TSData = bitmap.NewTS(int(EndIDx)); TSData == nil { |
| 529 | log.Error("Failed to create a bitmap") |
| 530 | return nil, errors.New("Failed to create bitmap") |
| 531 | } |
| 532 | Resource[POOL] = TSData.Data(false) //we pass false so as the TSData lib api does not do a copy of the data and return |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 533 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 534 | Value, err := json.Marshal(Resource) |
| 535 | if err != nil { |
| 536 | log.Errorf("Failed to marshall resource") |
| 537 | return nil, err |
| 538 | } |
| 539 | return Value, err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 540 | } |
| 541 | func (PONRMgr *PONResourceManager) GetResource(Path string) (map[string]interface{}, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 542 | /* |
| 543 | Get resource from kv store. |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 544 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 545 | :param path: path to get resource |
| 546 | :return: resource if resource present in kv store else None |
| 547 | */ |
| 548 | //get resource from kv store |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 549 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 550 | var Value []byte |
| 551 | Result := make(map[string]interface{}) |
| 552 | var Str string |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 553 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 554 | Resource, err := PONRMgr.KVStore.Get(Path) |
| 555 | if (err != nil) || (Resource == nil) { |
| 556 | log.Debugf("Resource unavailable at %s", Path) |
| 557 | return nil, err |
| 558 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 559 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 560 | Value, err = ToByte(Resource.Value) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 561 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 562 | // decode resource fetched from backend store to dictionary |
| 563 | err = json.Unmarshal(Value, &Result) |
| 564 | if err != nil { |
| 565 | log.Error("Failed to decode resource") |
| 566 | return Result, err |
| 567 | } |
| 568 | /* |
| 569 | resource pool in backend store stored as binary string whereas to |
| 570 | access the pool to generate/release IDs it need to be converted |
| 571 | as BitArray |
| 572 | */ |
| 573 | Str, err = ToString(Result[POOL]) |
| 574 | if err != nil { |
| 575 | log.Error("Failed to conver to kv pair to string") |
| 576 | return Result, err |
| 577 | } |
| 578 | Decode64, _ := base64.StdEncoding.DecodeString(Str) |
| 579 | Result[POOL], err = ToByte(Decode64) |
| 580 | if err != nil { |
| 581 | log.Error("Failed to convert resource pool to byte") |
| 582 | return Result, err |
| 583 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 584 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 585 | return Result, err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | func (PONRMgr *PONResourceManager) GetPath(IntfID uint32, ResourceType string) string { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 589 | /* |
| 590 | Get path for given resource type. |
| 591 | :param pon_intf_id: OLT PON interface id |
| 592 | :param resource_type: String to identify type of resource |
| 593 | :return: path for given resource type |
| 594 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 595 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 596 | /* |
| 597 | Get the shared pool for the given resource type. |
| 598 | all the resource ranges and the shared resource maps are initialized during the init. |
| 599 | */ |
| 600 | SharedPoolID := PONRMgr.PonResourceRanges[PONRMgr.SharedIdxByType[ResourceType]].(uint32) |
| 601 | if SharedPoolID != 0 { |
| 602 | IntfID = SharedPoolID |
| 603 | } |
| 604 | var Path string |
| 605 | if ResourceType == ONU_ID { |
| 606 | Path = fmt.Sprintf(ONU_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 607 | } else if ResourceType == ALLOC_ID { |
| 608 | Path = fmt.Sprintf(ALLOC_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 609 | } else if ResourceType == GEMPORT_ID { |
| 610 | Path = fmt.Sprintf(GEMPORT_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 611 | } else if ResourceType == FLOW_ID { |
| 612 | Path = fmt.Sprintf(FLOW_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 613 | } else { |
| 614 | log.Error("Invalid resource pool identifier") |
| 615 | } |
| 616 | return Path |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | func (PONRMgr *PONResourceManager) GetResourceID(IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 620 | /* |
| 621 | Create alloc/gemport/onu/flow id for given OLT PON interface. |
| 622 | :param pon_intf_id: OLT PON interface id |
| 623 | :param resource_type: String to identify type of resource |
| 624 | :param num_of_id: required number of ids |
| 625 | :return list/uint32/None: list, uint32 or None if resource type is |
| 626 | alloc_id/gemport_id, onu_id or invalid type respectively |
| 627 | */ |
| 628 | if NumIDs < 1 { |
| 629 | log.Error("Invalid number of resources requested") |
| 630 | return nil, errors.New(fmt.Sprintf("Invalid number of resources requested %d", NumIDs)) |
| 631 | } |
| 632 | // delegate to the master instance if sharing enabled across instances |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 633 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 634 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 635 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
| 636 | return SharedResourceMgr.GetResourceID(IntfID, ResourceType, NumIDs) |
| 637 | } |
manikkaraj k | 6c9689d | 2019-05-09 12:59:52 -0400 | [diff] [blame] | 638 | log.Debugf("Fetching resource from %s rsrc mgr for resource %s", PONRMgr.Globalorlocal, ResourceType) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 639 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 640 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
| 641 | if Path == "" { |
| 642 | log.Errorf("Failed to get path for resource type %s", ResourceType) |
| 643 | return nil, errors.New(fmt.Sprintf("Failed to get path for resource type %s", ResourceType)) |
| 644 | } |
| 645 | log.Debugf("Get resource for type %s on path %s", ResourceType, Path) |
| 646 | var Result []uint32 |
| 647 | var NextID uint32 |
| 648 | Resource, err := PONRMgr.GetResource(Path) |
| 649 | if (err == nil) && (ResourceType == ONU_ID) || (ResourceType == FLOW_ID) { |
| 650 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
| 651 | log.Error("Failed to Generate ID") |
| 652 | return Result, err |
| 653 | } |
| 654 | Result = append(Result, NextID) |
| 655 | } else if (err == nil) && ((ResourceType == GEMPORT_ID) || (ResourceType == ALLOC_ID)) { |
| 656 | if NumIDs == 1 { |
| 657 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
| 658 | log.Error("Failed to Generate ID") |
| 659 | return Result, err |
| 660 | } |
| 661 | Result = append(Result, NextID) |
| 662 | } else { |
| 663 | for NumIDs > 0 { |
| 664 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
| 665 | log.Error("Failed to Generate ID") |
| 666 | return Result, err |
| 667 | } |
| 668 | Result = append(Result, NextID) |
| 669 | NumIDs-- |
| 670 | } |
| 671 | } |
| 672 | } else { |
| 673 | log.Error("get resource failed") |
| 674 | return Result, err |
| 675 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 676 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 677 | //Update resource in kv store |
| 678 | if PONRMgr.UpdateResource(Path, Resource) != nil { |
| 679 | log.Errorf("Failed to update resource %s", Path) |
| 680 | return nil, errors.New(fmt.Sprintf("Failed to update resource %s", Path)) |
| 681 | } |
| 682 | return Result, nil |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | func checkValidResourceType(ResourceType string) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 686 | KnownResourceTypes := []string{ONU_ID, ALLOC_ID, GEMPORT_ID, FLOW_ID} |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 687 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 688 | for _, v := range KnownResourceTypes { |
| 689 | if v == ResourceType { |
| 690 | return true |
| 691 | } |
| 692 | } |
| 693 | return false |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | func (PONRMgr *PONResourceManager) FreeResourceID(IntfID uint32, ResourceType string, ReleaseContent []uint32) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 697 | /* |
| 698 | Release alloc/gemport/onu/flow id for given OLT PON interface. |
| 699 | :param pon_intf_id: OLT PON interface id |
| 700 | :param resource_type: String to identify type of resource |
| 701 | :param release_content: required number of ids |
| 702 | :return boolean: True if all IDs in given release_content release else False |
| 703 | */ |
| 704 | if checkValidResourceType(ResourceType) == false { |
| 705 | log.Error("Invalid resource type") |
| 706 | return false |
| 707 | } |
| 708 | if ReleaseContent == nil { |
| 709 | log.Debug("Nothing to release") |
| 710 | return true |
| 711 | } |
| 712 | // delegate to the master instance if sharing enabled across instances |
| 713 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 714 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
| 715 | return SharedResourceMgr.FreeResourceID(IntfID, ResourceType, ReleaseContent) |
| 716 | } |
| 717 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
| 718 | if Path == "" { |
| 719 | log.Error("Failed to get path") |
| 720 | return false |
| 721 | } |
| 722 | Resource, err := PONRMgr.GetResource(Path) |
| 723 | if err != nil { |
| 724 | log.Error("Failed to get resource") |
| 725 | return false |
| 726 | } |
| 727 | for _, Val := range ReleaseContent { |
| 728 | PONRMgr.ReleaseID(Resource, Val) |
| 729 | } |
| 730 | if PONRMgr.UpdateResource(Path, Resource) != nil { |
| 731 | log.Errorf("Free resource for %s failed", Path) |
| 732 | return false |
| 733 | } |
| 734 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | func (PONRMgr *PONResourceManager) UpdateResource(Path string, Resource map[string]interface{}) error { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 738 | /* |
| 739 | Update resource in resource kv store. |
| 740 | :param path: path to update resource |
| 741 | :param resource: resource need to be updated |
| 742 | :return boolean: True if resource updated in kv store else False |
| 743 | */ |
| 744 | // TODO resource[POOL] = resource[POOL].bin |
| 745 | Value, err := json.Marshal(Resource) |
| 746 | if err != nil { |
| 747 | log.Error("failed to Marshal") |
| 748 | return err |
| 749 | } |
| 750 | err = PONRMgr.KVStore.Put(Path, Value) |
| 751 | if err != nil { |
| 752 | log.Error("failed to put data to kv store %s", Path) |
| 753 | return err |
| 754 | } |
| 755 | return nil |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | func (PONRMgr *PONResourceManager) ClearResourceIDPool(IntfID uint32, ResourceType string) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 759 | /* |
| 760 | Clear Resource Pool for a given Resource Type on a given PON Port. |
| 761 | :return boolean: True if removed else False |
| 762 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 763 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 764 | // delegate to the master instance if sharing enabled across instances |
| 765 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 766 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
| 767 | return SharedResourceMgr.ClearResourceIDPool(IntfID, ResourceType) |
| 768 | } |
| 769 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
| 770 | if Path == "" { |
| 771 | log.Error("Failed to get path") |
| 772 | return false |
| 773 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 774 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 775 | if err := PONRMgr.KVStore.Delete(Path); err != nil { |
| 776 | log.Errorf("Failed to delete resource %s", Path) |
| 777 | return false |
| 778 | } |
| 779 | log.Debugf("Cleared resource %s", Path) |
| 780 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | func (PONRMgr PONResourceManager) InitResourceMap(PONIntfONUID string) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 784 | /* |
| 785 | Initialize resource map |
| 786 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 787 | */ |
| 788 | // initialize pon_intf_onu_id tuple to alloc_ids map |
| 789 | AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 790 | var AllocIDs []byte |
| 791 | Result := PONRMgr.KVStore.Put(AllocIDPath, AllocIDs) |
| 792 | if Result != nil { |
| 793 | log.Error("Failed to update the KV store") |
| 794 | return |
| 795 | } |
| 796 | // initialize pon_intf_onu_id tuple to gemport_ids map |
| 797 | GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 798 | var GEMPortIDs []byte |
| 799 | Result = PONRMgr.KVStore.Put(GEMPortIDPath, GEMPortIDs) |
| 800 | if Result != nil { |
| 801 | log.Error("Failed to update the KV store") |
| 802 | return |
| 803 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | func (PONRMgr PONResourceManager) RemoveResourceMap(PONIntfONUID string) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 807 | /* |
| 808 | Remove resource map |
| 809 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 810 | */ |
| 811 | // remove pon_intf_onu_id tuple to alloc_ids map |
| 812 | var err error |
| 813 | AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 814 | if err = PONRMgr.KVStore.Delete(AllocIDPath); err != nil { |
| 815 | log.Errorf("Failed to remove resource %s", AllocIDPath) |
| 816 | return false |
| 817 | } |
| 818 | // remove pon_intf_onu_id tuple to gemport_ids map |
| 819 | GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 820 | err = PONRMgr.KVStore.Delete(GEMPortIDPath) |
| 821 | if err != nil { |
| 822 | log.Errorf("Failed to remove resource %s", GEMPortIDPath) |
| 823 | return false |
| 824 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 825 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 826 | FlowIDPath := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 827 | if FlowIDs, err := PONRMgr.KVStore.List(FlowIDPath); err != nil { |
| 828 | for _, Flow := range FlowIDs { |
| 829 | FlowIDInfoPath := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, PONIntfONUID, Flow.Value) |
| 830 | if err = PONRMgr.KVStore.Delete(FlowIDInfoPath); err != nil { |
| 831 | log.Errorf("Failed to remove resource %s", FlowIDInfoPath) |
| 832 | return false |
| 833 | } |
| 834 | } |
| 835 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 836 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 837 | if err = PONRMgr.KVStore.Delete(FlowIDPath); err != nil { |
| 838 | log.Errorf("Failed to remove resource %s", FlowIDPath) |
| 839 | return false |
| 840 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 841 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 842 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | func (PONRMgr *PONResourceManager) GetCurrentAllocIDForOnu(IntfONUID string) []uint32 { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 846 | /* |
| 847 | Get currently configured alloc ids for given pon_intf_onu_id |
| 848 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 849 | :return list: List of alloc_ids if available, else None |
| 850 | */ |
| 851 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 852 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 853 | var Data []uint32 |
| 854 | Value, err := PONRMgr.KVStore.Get(Path) |
| 855 | if err == nil { |
| 856 | if Value != nil { |
| 857 | Val, err := ToByte(Value.Value) |
| 858 | if err != nil { |
| 859 | log.Errorw("Failed to convert into byte array", log.Fields{"error": err}) |
| 860 | return Data |
| 861 | } |
| 862 | if err = json.Unmarshal(Val, &Data); err != nil { |
| 863 | log.Error("Failed to unmarshal", log.Fields{"error": err}) |
| 864 | return Data |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | return Data |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | func (PONRMgr *PONResourceManager) GetCurrentGEMPortIDsForOnu(IntfONUID string) []uint32 { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 872 | /* |
| 873 | Get currently configured gemport ids for given pon_intf_onu_id |
| 874 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 875 | :return list: List of gemport IDs if available, else None |
| 876 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 877 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 878 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 879 | log.Debugf("Getting current gemports for %s", Path) |
| 880 | var Data []uint32 |
| 881 | Value, err := PONRMgr.KVStore.Get(Path) |
| 882 | if err == nil { |
| 883 | if Value != nil { |
| 884 | Val, _ := ToByte(Value.Value) |
| 885 | if err = json.Unmarshal(Val, &Data); err != nil { |
| 886 | log.Errorw("Failed to unmarshal", log.Fields{"error": err}) |
| 887 | return Data |
| 888 | } |
| 889 | } |
| 890 | } else { |
| 891 | log.Errorf("Failed to get data from kvstore for %s", Path) |
| 892 | } |
| 893 | return Data |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | func (PONRMgr *PONResourceManager) GetCurrentFlowIDsForOnu(IntfONUID string) []uint32 { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 897 | /* |
| 898 | Get currently configured flow ids for given pon_intf_onu_id |
| 899 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 900 | :return list: List of Flow IDs if available, else None |
| 901 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 902 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 903 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 904 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 905 | var Data []uint32 |
| 906 | Value, err := PONRMgr.KVStore.Get(Path) |
| 907 | if err == nil { |
| 908 | if Value != nil { |
| 909 | Val, _ := ToByte(Value.Value) |
| 910 | if err = json.Unmarshal(Val, &Data); err != nil { |
| 911 | log.Error("Failed to unmarshal") |
| 912 | return Data |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | return Data |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 917 | } |
| 918 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 919 | func (PONRMgr *PONResourceManager) GetFlowIDInfo(IntfONUID string, FlowID uint32, Data interface{}) error { |
| 920 | /* |
| 921 | Get flow details configured for the ONU. |
| 922 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 923 | :param flow_id: Flow Id reference |
| 924 | :param Data: Result |
| 925 | :return error: nil if no error in getting from KV store |
| 926 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 927 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 928 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 929 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 930 | Value, err := PONRMgr.KVStore.Get(Path) |
| 931 | if err == nil { |
| 932 | if Value != nil { |
| 933 | Val, err := ToByte(Value.Value) |
| 934 | if err != nil { |
| 935 | log.Errorw("Failed to convert flowinfo into byte array", log.Fields{"error": err}) |
| 936 | return err |
| 937 | } |
| 938 | if err = json.Unmarshal(Val, Data); err != nil { |
| 939 | log.Errorw("Failed to unmarshal", log.Fields{"error": err}) |
| 940 | return err |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | func (PONRMgr *PONResourceManager) RemoveFlowIDInfo(IntfONUID string, FlowID uint32) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 948 | /* |
| 949 | Get flow_id details configured for the ONU. |
| 950 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 951 | :param flow_id: Flow Id reference |
| 952 | */ |
| 953 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 954 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 955 | if err := PONRMgr.KVStore.Delete(Path); err != nil { |
| 956 | log.Errorf("Falied to remove resource %s", Path) |
| 957 | return false |
| 958 | } |
| 959 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | func (PONRMgr *PONResourceManager) UpdateAllocIdsForOnu(IntfONUID string, AllocIDs []uint32) error { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 963 | /* |
| 964 | Update currently configured alloc ids for given pon_intf_onu_id |
| 965 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 966 | :param alloc_ids: list of alloc ids |
| 967 | */ |
| 968 | var Value []byte |
| 969 | var err error |
| 970 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 971 | Value, err = json.Marshal(AllocIDs) |
| 972 | if err != nil { |
| 973 | log.Error("failed to Marshal") |
| 974 | return err |
| 975 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 976 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 977 | if err = PONRMgr.KVStore.Put(Path, Value); err != nil { |
| 978 | log.Errorf("Failed to update resource %s", Path) |
| 979 | return err |
| 980 | } |
| 981 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | func (PONRMgr *PONResourceManager) UpdateGEMPortIDsForOnu(IntfONUID string, GEMPortIDs []uint32) error { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 985 | /* |
| 986 | Update currently configured gemport ids for given pon_intf_onu_id |
| 987 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 988 | :param gemport_ids: list of gem port ids |
| 989 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 990 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 991 | var Value []byte |
| 992 | var err error |
| 993 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 994 | log.Debugf("Updating gemport ids for %s", Path) |
| 995 | Value, err = json.Marshal(GEMPortIDs) |
| 996 | if err != nil { |
| 997 | log.Error("failed to Marshal") |
| 998 | return err |
| 999 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1000 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1001 | if err = PONRMgr.KVStore.Put(Path, Value); err != nil { |
| 1002 | log.Errorf("Failed to update resource %s", Path) |
| 1003 | return err |
| 1004 | } |
| 1005 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | func checkForFlowIDInList(FlowIDList []uint32, FlowID uint32) (bool, uint32) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1009 | /* |
| 1010 | Check for a flow id in a given list of flow IDs. |
| 1011 | :param FLowIDList: List of Flow IDs |
| 1012 | :param FlowID: Flowd to check in the list |
| 1013 | : return true and the index if present false otherwise. |
| 1014 | */ |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1015 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1016 | for idx, _ := range FlowIDList { |
| 1017 | if FlowID == FlowIDList[idx] { |
| 1018 | return true, uint32(idx) |
| 1019 | } |
| 1020 | } |
| 1021 | return false, 0 |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | func (PONRMgr *PONResourceManager) UpdateFlowIDForOnu(IntfONUID string, FlowID uint32, Add bool) error { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1025 | /* |
| 1026 | Update the flow_id list of the ONU (add or remove flow_id from the list) |
| 1027 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1028 | :param flow_id: flow ID |
| 1029 | :param add: Boolean flag to indicate whether the flow_id should be |
| 1030 | added or removed from the list. Defaults to adding the flow. |
| 1031 | */ |
| 1032 | var Value []byte |
| 1033 | var err error |
| 1034 | var RetVal bool |
| 1035 | var IDx uint32 |
| 1036 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 1037 | FlowIDs := PONRMgr.GetCurrentFlowIDsForOnu(IntfONUID) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1038 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1039 | if Add { |
| 1040 | if RetVal, IDx = checkForFlowIDInList(FlowIDs, FlowID); RetVal == true { |
| 1041 | return err |
| 1042 | } |
| 1043 | FlowIDs = append(FlowIDs, FlowID) |
| 1044 | } else { |
| 1045 | if RetVal, IDx = checkForFlowIDInList(FlowIDs, FlowID); RetVal == false { |
| 1046 | return err |
| 1047 | } |
| 1048 | // delete the index and shift |
| 1049 | FlowIDs = append(FlowIDs[:IDx], FlowIDs[IDx+1:]...) |
| 1050 | } |
| 1051 | Value, err = json.Marshal(FlowIDs) |
| 1052 | if err != nil { |
| 1053 | log.Error("Failed to Marshal") |
| 1054 | return err |
| 1055 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1056 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1057 | if err = PONRMgr.KVStore.Put(Path, Value); err != nil { |
| 1058 | log.Errorf("Failed to update resource %s", Path) |
| 1059 | return err |
| 1060 | } |
| 1061 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1062 | } |
| 1063 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1064 | func (PONRMgr *PONResourceManager) UpdateFlowIDInfoForOnu(IntfONUID string, FlowID uint32, FlowData interface{}) error { |
| 1065 | /* |
| 1066 | Update any metadata associated with the flow_id. The flow_data could be json |
| 1067 | or any of other data structure. The resource manager doesnt care |
| 1068 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1069 | :param flow_id: Flow ID |
| 1070 | :param flow_data: Flow data blob |
| 1071 | */ |
| 1072 | var Value []byte |
| 1073 | var err error |
| 1074 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1075 | Value, err = json.Marshal(FlowData) |
| 1076 | if err != nil { |
| 1077 | log.Error("failed to Marshal") |
| 1078 | return err |
| 1079 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1080 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1081 | if err = PONRMgr.KVStore.Put(Path, Value); err != nil { |
| 1082 | log.Errorf("Failed to update resource %s", Path) |
| 1083 | return err |
| 1084 | } |
| 1085 | return err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | func (PONRMgr *PONResourceManager) GenerateNextID(Resource map[string]interface{}) (uint32, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1089 | /* |
| 1090 | Generate unique id having OFFSET as start |
| 1091 | :param resource: resource used to generate ID |
| 1092 | :return uint32: generated id |
| 1093 | */ |
| 1094 | ByteArray, err := ToByte(Resource[POOL]) |
| 1095 | if err != nil { |
| 1096 | log.Error("Failed to convert resource to byte array") |
| 1097 | return 0, err |
| 1098 | } |
| 1099 | Data := bitmap.TSFromData(ByteArray, false) |
| 1100 | if Data == nil { |
| 1101 | log.Error("Failed to get data from byte array") |
| 1102 | return 0, errors.New("Failed to get data from byte array") |
| 1103 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1104 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1105 | Len := Data.Len() |
| 1106 | var Idx int |
| 1107 | for Idx = 0; Idx < Len; Idx++ { |
| 1108 | Val := Data.Get(Idx) |
| 1109 | if Val == false { |
| 1110 | break |
| 1111 | } |
| 1112 | } |
| 1113 | Data.Set(Idx, true) |
| 1114 | res := uint32(Resource[START_IDX].(float64)) |
| 1115 | Resource[POOL] = Data.Data(false) |
| 1116 | log.Debugf("Generated ID for %d", (uint32(Idx) + res)) |
| 1117 | return (uint32(Idx) + res), err |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | func (PONRMgr *PONResourceManager) ReleaseID(Resource map[string]interface{}, Id uint32) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1121 | /* |
| 1122 | Release unique id having OFFSET as start index. |
| 1123 | :param resource: resource used to release ID |
| 1124 | :param unique_id: id need to be released |
| 1125 | */ |
| 1126 | ByteArray, err := ToByte(Resource[POOL]) |
| 1127 | if err != nil { |
| 1128 | log.Error("Failed to convert resource to byte array") |
| 1129 | return false |
| 1130 | } |
| 1131 | Data := bitmap.TSFromData(ByteArray, false) |
| 1132 | if Data == nil { |
| 1133 | log.Error("Failed to get resource pool") |
| 1134 | return false |
| 1135 | } |
| 1136 | var Idx uint32 |
| 1137 | Idx = Id - uint32(Resource[START_IDX].(float64)) |
| 1138 | Data.Set(int(Idx), false) |
| 1139 | Resource[POOL] = Data.Data(false) |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1140 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1141 | return true |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1142 | } |
| 1143 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1144 | func (PONRMgr *PONResourceManager) GetTechnology() string { |
| 1145 | return PONRMgr.Technology |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1146 | } |
| 1147 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1148 | func (PONRMgr *PONResourceManager) GetResourceTypeAllocID() string { |
| 1149 | return ALLOC_ID |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1150 | } |
| 1151 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1152 | func (PONRMgr *PONResourceManager) GetResourceTypeGemPortID() string { |
| 1153 | return GEMPORT_ID |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1154 | } |
| 1155 | |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1156 | // ToByte converts an interface value to a []byte. The interface should either be of |
| 1157 | // a string type or []byte. Otherwise, an error is returned. |
| 1158 | func ToByte(value interface{}) ([]byte, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1159 | switch t := value.(type) { |
| 1160 | case []byte: |
| 1161 | return value.([]byte), nil |
| 1162 | case string: |
| 1163 | return []byte(value.(string)), nil |
| 1164 | default: |
| 1165 | return nil, fmt.Errorf("unexpected-type-%T", t) |
| 1166 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | // ToString converts an interface value to a string. The interface should either be of |
| 1170 | // a string type or []byte. Otherwise, an error is returned. |
| 1171 | func ToString(value interface{}) (string, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 1172 | switch t := value.(type) { |
| 1173 | case []byte: |
| 1174 | return string(value.([]byte)), nil |
| 1175 | case string: |
| 1176 | return value.(string), nil |
| 1177 | default: |
| 1178 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 1179 | } |
Abhilash S.Laxmeshwar | 068867d | 2019-03-15 16:55:19 +0530 | [diff] [blame] | 1180 | } |