Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 11f5a57 | 2024-01-12 19:11:32 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 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 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 17 | // Package resourcemanager provides the utility for managing resources |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 18 | package resourcemanager |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 19 | |
| 20 | import ( |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 21 | "context" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 22 | "encoding/json" |
| 23 | "errors" |
| 24 | "fmt" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 25 | "strings" |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 26 | "sync" |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 27 | "time" |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 28 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 29 | tp "github.com/opencord/voltha-lib-go/v7/pkg/techprofile" |
| 30 | |
| 31 | "github.com/opencord/voltha-lib-go/v7/pkg/db" |
| 32 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
| 33 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 34 | ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager" |
| 35 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 36 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 37 | ) |
| 38 | |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 39 | const ( |
| 40 | // KvstoreTimeout specifies the time out for KV Store Connection |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 41 | KvstoreTimeout = 5 * time.Second |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 42 | // BasePathKvStore - <pathPrefix>/openolt/<device_id> |
| 43 | BasePathKvStore = "%s/openolt/{%s}" |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 44 | // tpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id |
| 45 | tpIDPathSuffix = "{%d,%d,%d}/tp_id" |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 46 | //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction> |
| 47 | MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}" |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 48 | |
| 49 | // OnuPacketInPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 50 | //format: onu_packetin/{<intfid>,<onuid>,<logicalport>} |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 51 | OnuPacketInPathPrefix = "onu_packetin/{%d,%d,%d}" |
| 52 | // OnuPacketInPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 53 | //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>} |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 54 | OnuPacketInPath = OnuPacketInPathPrefix + "/{%d,%d}" |
| 55 | |
| 56 | //FlowIDsForGemPathPrefix format: flowids_for_gem/<intfid> |
| 57 | FlowIDsForGemPathPrefix = "flowids_per_gem/{%d}" |
| 58 | //FlowIDsForGem flowids_for_gem/<intfid>/<gemport-id> |
| 59 | FlowIDsForGem = FlowIDsForGemPathPrefix + "/{%d}" |
| 60 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 61 | //McastQueuesForIntf multicast queues for pon interfaces |
| 62 | McastQueuesForIntf = "mcast_qs_for_int" |
| 63 | //FlowGroup flow_groups/<flow_group_id> |
| 64 | // A group is stored under this path on the KV store after it has been installed to the device. |
| 65 | // It should also be deleted after it has been removed from the device accordingly. |
| 66 | FlowGroup = "flow_groups/{%d}" |
| 67 | //FlowGroupCached flow_groups_cached/<flow_group_id> |
| 68 | // When a group add request received, we create the group without setting any members to it since we cannot |
| 69 | // set any members to a group until it is associated with a multicast flow. It is a BAL limitation. |
| 70 | // When the related multicast flow has been created we perform set members operation for the group. |
| 71 | // That is why we need to keep the members of a group until the multicast flow creation request comes. |
| 72 | // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members, |
| 73 | // we remove the group from the cached group store. |
| 74 | FlowGroupCached = "flow_groups_cached/{%d}" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 75 | |
| 76 | //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber |
| 77 | //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids |
| 78 | FlowIDPath = "{%s}/flow_ids" |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 79 | |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 80 | //OnuGemInfoPathPathPrefix format: onu_gem_info/<intfid> |
| 81 | OnuGemInfoPathPathPrefix = "onu_gem_info/{%d}" |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 82 | //OnuGemInfoPath is path on the kvstore to store onugem info map |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 83 | //format: onu_gem_info/<intfid>/<onu_id> |
| 84 | OnuGemInfoPath = OnuGemInfoPathPathPrefix + "/{%d}" |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 85 | |
| 86 | // NNI uint32 version of -1 which represents the NNI port |
| 87 | NNI = 4294967295 |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 88 | ) |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 89 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 90 | // FlowInfo holds the flow information |
Abhilash S.L | 8ee9071 | 2019-04-29 16:24:22 +0530 | [diff] [blame] | 91 | type FlowInfo struct { |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 92 | Flow *openolt.Flow |
| 93 | IsSymmtricFlow bool |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // OnuGemInfo holds onu information along with gem port list and uni port list |
| 97 | type OnuGemInfo struct { |
| 98 | OnuID uint32 |
| 99 | SerialNumber string |
| 100 | IntfID uint32 |
| 101 | GemPorts []uint32 |
| 102 | UniPorts []uint32 |
| 103 | } |
| 104 | |
| 105 | // PacketInInfoKey is the key for packet in gemport |
| 106 | type PacketInInfoKey struct { |
| 107 | IntfID uint32 |
| 108 | OnuID uint32 |
| 109 | LogicalPort uint32 |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 110 | VlanID uint16 |
| 111 | Priority uint8 |
Abhilash S.L | 8ee9071 | 2019-04-29 16:24:22 +0530 | [diff] [blame] | 112 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 113 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 114 | // GroupInfo holds group information |
| 115 | type GroupInfo struct { |
| 116 | GroupID uint32 |
| 117 | OutPorts []uint32 |
| 118 | } |
| 119 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 120 | // MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction> |
| 121 | type MeterInfo struct { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 122 | RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted. |
| 123 | MeterID uint32 |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 126 | // OpenOltResourceMgr holds resource related information as provided below for each field |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 127 | type OpenOltResourceMgr struct { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 128 | PonIntfID uint32 |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 129 | DeviceID string // OLT device id |
| 130 | Address string // Host and port of the kv store to connect to |
| 131 | Args string // args |
| 132 | KVStore *db.Backend // backend kv store connection handle |
| 133 | DeviceType string |
| 134 | DevInfo *openolt.DeviceInfo // device information |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 135 | // array of pon resource managers per interface technology |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 136 | PonRsrMgr *ponrmgr.PONResourceManager |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 137 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 138 | // Local maps used for write-through-cache - start |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 139 | allocIDsForOnu map[string][]uint32 |
| 140 | allocIDsForOnuLock sync.RWMutex |
| 141 | |
| 142 | gemPortIDsForOnu map[string][]uint32 |
| 143 | gemPortIDsForOnuLock sync.RWMutex |
| 144 | |
| 145 | techProfileIDsForOnu map[string][]uint32 |
| 146 | techProfileIDsForOnuLock sync.RWMutex |
| 147 | |
| 148 | meterInfoForOnu map[string]*MeterInfo |
| 149 | meterInfoForOnuLock sync.RWMutex |
| 150 | |
| 151 | onuGemInfo map[string]*OnuGemInfo |
| 152 | onuGemInfoLock sync.RWMutex |
| 153 | |
| 154 | gemPortForPacketInInfo map[string]uint32 |
| 155 | gemPortForPacketInInfoLock sync.RWMutex |
| 156 | |
| 157 | flowIDsForGem map[uint32][]uint64 |
| 158 | flowIDsForGemLock sync.RWMutex |
| 159 | |
| 160 | mcastQueueForIntf map[uint32][]uint32 |
| 161 | mcastQueueForIntfLock sync.RWMutex |
| 162 | mcastQueueForIntfLoadedFromKvStore bool |
| 163 | |
| 164 | groupInfo map[string]*GroupInfo |
| 165 | groupInfoLock sync.RWMutex |
| 166 | // Local maps used for write-through-cache - end |
Girish Gowdra | 4c3d460 | 2021-07-22 16:33:37 -0700 | [diff] [blame] | 167 | |
Girish Gowdra | 76a1b09 | 2021-07-28 10:07:04 -0700 | [diff] [blame] | 168 | TechprofileRef tp.TechProfileIf |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 171 | func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) { |
| 172 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 173 | switch storeType { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 174 | case "etcd": |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 175 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
Abhay Kumar | 9bcfeb2 | 2024-07-12 09:14:25 +0530 | [diff] [blame^] | 176 | case "redis": |
| 177 | return kvstore.NewRedisClient(address, timeout, false) |
| 178 | case "redis-sentinel": |
| 179 | return kvstore.NewRedisClient(address, timeout, true) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 180 | } |
| 181 | return nil, errors.New("unsupported-kv-store") |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 182 | } |
| 183 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 184 | // SetKVClient sets the KV client and return a kv backend |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 185 | func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 186 | // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some |
| 187 | // issue between kv store and backend , core is not calling NewBackend directly |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 188 | kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 189 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 190 | logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 191 | return nil |
| 192 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 193 | // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)) |
Matteo Scandolo | d625b4c | 2020-04-02 16:16:01 -0700 | [diff] [blame] | 194 | |
sbarbari | a8910ba | 2019-11-05 10:12:23 -0500 | [diff] [blame] | 195 | kvbackend := &db.Backend{ |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 196 | Client: kvClient, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 197 | StoreType: backend, |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 198 | Address: addr, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 199 | Timeout: KvstoreTimeout, |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 200 | PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)} |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 201 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 202 | return kvbackend |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 203 | } |
| 204 | |
Holger Hildebrandt | 143b5be | 2023-02-10 08:28:15 +0000 | [diff] [blame] | 205 | // CloseKVClient closes open KV clients |
| 206 | func (rsrcMgr *OpenOltResourceMgr) CloseKVClient(ctx context.Context) { |
| 207 | if rsrcMgr.KVStore != nil { |
| 208 | rsrcMgr.KVStore.Client.Close(ctx) |
| 209 | rsrcMgr.KVStore = nil |
| 210 | } |
| 211 | if rsrcMgr.PonRsrMgr != nil { |
| 212 | rsrcMgr.PonRsrMgr.CloseKVClient(ctx) |
| 213 | } |
| 214 | } |
| 215 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 216 | // NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 217 | // instances according to technology. Initializes the default resource ranges for all |
| 218 | // the resources. |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 219 | func NewResourceMgr(ctx context.Context, PonIntfID uint32, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo, basePathKvStore string) *OpenOltResourceMgr { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 220 | var ResourceMgr OpenOltResourceMgr |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 221 | logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID) |
| 222 | ResourceMgr.PonIntfID = PonIntfID |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 223 | ResourceMgr.DeviceID = deviceID |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 224 | ResourceMgr.Address = KVStoreAddress |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 225 | ResourceMgr.DeviceType = deviceType |
| 226 | ResourceMgr.DevInfo = devInfo |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 227 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 228 | Backend := kvStoreType |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 229 | ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 230 | if ResourceMgr.KVStore == nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 231 | logger.Error(ctx, "Failed to setup KV store") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 232 | } |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 233 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 234 | // TODO self.args = registry('main').get_args() |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 235 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 236 | // Create a separate Resource Manager instance for each range. This assumes that |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 237 | // each technology is represented by only a single range |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 238 | for _, TechRange := range devInfo.Ranges { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 239 | for _, intfID := range TechRange.IntfIds { |
| 240 | if intfID == PonIntfID { |
| 241 | technology := TechRange.Technology |
| 242 | logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 243 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 244 | rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID, |
| 245 | Backend, ResourceMgr.Address, basePathKvStore) |
| 246 | if err != nil { |
| 247 | logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology) |
| 248 | return nil |
| 249 | } |
| 250 | ResourceMgr.PonRsrMgr = rsrMgr |
| 251 | // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange) |
| 252 | InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo) |
| 253 | if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil { |
| 254 | logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID) |
| 255 | return nil |
| 256 | } |
| 257 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 258 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 259 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 260 | |
| 261 | ResourceMgr.InitLocalCache() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 262 | if err := ResourceMgr.LoadLocalCacheFromKVStore(ctx); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 263 | logger.Error(ctx, "failed-to-load-local-cache-from-kvstore") |
| 264 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 265 | logger.Info(ctx, "Initialization of resource manager success!") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 266 | return &ResourceMgr |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 267 | } |
| 268 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 269 | // InitLocalCache initializes local maps used for write-through-cache |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 270 | func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 271 | rsrcMgr.allocIDsForOnu = make(map[string][]uint32) |
| 272 | rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32) |
| 273 | rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32) |
| 274 | rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo) |
| 275 | rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo) |
| 276 | rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32) |
| 277 | rsrcMgr.flowIDsForGem = make(map[uint32][]uint64) |
| 278 | rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32) |
| 279 | rsrcMgr.groupInfo = make(map[string]*GroupInfo) |
| 280 | } |
| 281 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 282 | // LoadLocalCacheFromKVStore loads local maps |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 283 | func (rsrcMgr *OpenOltResourceMgr) LoadLocalCacheFromKVStore(ctx context.Context) error { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 284 | |
| 285 | //List all the keys for OnuGemInfo |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 286 | prefixPath := fmt.Sprintf(OnuGemInfoPathPathPrefix, rsrcMgr.PonIntfID) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 287 | keys, err := rsrcMgr.KVStore.List(ctx, prefixPath) |
| 288 | logger.Debug(ctx, "load-local-cache-from-KV-store-started") |
| 289 | if err != nil { |
| 290 | logger.Errorf(ctx, "failed-to-list-keys-from-path-%s", prefixPath) |
| 291 | return err |
| 292 | } |
| 293 | for path := range keys { |
| 294 | var Val []byte |
| 295 | var onugem OnuGemInfo |
| 296 | // Get rid of the path prefix |
| 297 | stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/" |
| 298 | replacedWith := "" |
| 299 | path = strings.Replace(path, stringToBeReplaced, replacedWith, 1) |
| 300 | |
| 301 | value, err := rsrcMgr.KVStore.Get(ctx, path) |
| 302 | if err != nil { |
| 303 | logger.Errorw(ctx, "failed-to-get-from-kv-store", log.Fields{"path": path}) |
| 304 | return err |
| 305 | } else if value == nil { |
| 306 | logger.Debug(ctx, "no-onugeminfo-for-path", log.Fields{"path": path}) |
| 307 | continue |
| 308 | } |
| 309 | if Val, err = kvstore.ToByte(value.Value); err != nil { |
| 310 | logger.Error(ctx, "failed-to-covert-to-byte-array") |
| 311 | return err |
| 312 | } |
| 313 | if err = json.Unmarshal(Val, &onugem); err != nil { |
| 314 | logger.Error(ctx, "failed-to-unmarshall") |
| 315 | return err |
| 316 | } |
| 317 | logger.Debugw(ctx, "found-onugeminfo-from-path", log.Fields{"path": path, "onuGemInfo": onugem}) |
| 318 | |
| 319 | rsrcMgr.onuGemInfoLock.Lock() |
| 320 | rsrcMgr.onuGemInfo[path] = &onugem |
| 321 | rsrcMgr.onuGemInfoLock.Unlock() |
| 322 | |
| 323 | } |
| 324 | logger.Debug(ctx, "load-local-cache-from-KV-store-finished") |
| 325 | return nil |
| 326 | } |
| 327 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 328 | // InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply |
| 329 | // device specific information. If KV doesn't exist |
| 330 | // or is broader than the device, the device's information will |
| 331 | // dictate the range limits |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 332 | func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 333 | techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 334 | // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType |
| 335 | var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32 |
| 336 | var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32 |
| 337 | |
| 338 | // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function. |
| 339 | // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device |
| 340 | // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI. |
| 341 | var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2 |
| 342 | var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1 |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 343 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 344 | // init the resource range pool according to the sharing type |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 345 | logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology, |
| 346 | "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, |
| 347 | "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd, |
| 348 | "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd, |
| 349 | "intf_ids": techRange.IntfIds, |
| 350 | }) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 351 | for _, RangePool := range techRange.Pools { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 352 | // FIXME: Remove hardcoding |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 353 | if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID { |
| 354 | ONUIDStart = RangePool.Start |
| 355 | ONUIDEnd = RangePool.End |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 356 | ONUIDShared = uint32(RangePool.Sharing) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 357 | } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID { |
| 358 | AllocIDStart = RangePool.Start |
| 359 | AllocIDEnd = RangePool.End |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 360 | AllocIDShared = uint32(RangePool.Sharing) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 361 | } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID { |
| 362 | GEMPortIDStart = RangePool.Start |
| 363 | GEMPortIDEnd = RangePool.End |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 364 | GEMPortIDShared = uint32(RangePool.Sharing) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 365 | } |
| 366 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 367 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 368 | ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared, |
| 369 | AllocIDStart, AllocIDEnd, AllocIDShared, |
| 370 | GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared, |
| 371 | flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 372 | devInfo.PonPorts, techRange.IntfIds) |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 373 | |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 374 | } |
| 375 | |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 376 | // Delete clears used resources for the particular olt device being deleted |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 377 | func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error { |
| 378 | if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil { |
| 379 | logger.Debug(ctx, "Failed to clear device resource pool") |
| 380 | return err |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 381 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 382 | logger.Debug(ctx, "Cleared device resource pool") |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 383 | return nil |
| 384 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 385 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 386 | // GetONUID returns the available onuID for the given pon-port |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 387 | func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context) (uint32, error) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 388 | // Get ONU id for a provided pon interface ID. |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 389 | onuID, err := rsrcMgr.TechprofileRef.GetResourceID(ctx, rsrcMgr.PonIntfID, |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 390 | ponrmgr.ONU_ID, 1) |
| 391 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 392 | logger.Errorf(ctx, "Failed to get resource for interface %d for type %s", |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 393 | rsrcMgr.PonIntfID, ponrmgr.ONU_ID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 394 | return 0, err |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 395 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 396 | if len(onuID) > 0 { |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 397 | return onuID[0], err |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 398 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 399 | |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 400 | return 0, fmt.Errorf("no-onu-id-allocated") |
Abhilash S.L | 8ee9071 | 2019-04-29 16:24:22 +0530 | [diff] [blame] | 401 | } |
| 402 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 403 | // UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 404 | func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, onuID uint32, uniID uint32, allocIDs []uint32) error { |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 405 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 406 | intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID) |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 407 | |
| 408 | // Note: in case the write to DB fails there could be inconsistent data between cache and db. |
| 409 | // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release |
| 410 | if err := rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID, allocIDs); err != nil { |
| 411 | logger.Errorw(ctx, "Failed to update alloc ids for onu", log.Fields{"err": err}) |
| 412 | return err |
| 413 | } |
| 414 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 415 | // update cache |
| 416 | rsrcMgr.allocIDsForOnuLock.Lock() |
| 417 | rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs |
| 418 | rsrcMgr.allocIDsForOnuLock.Unlock() |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 419 | return nil |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 420 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 421 | |
| 422 | // GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 423 | func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, onuID uint32, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 424 | uniID uint32) []uint32 { |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 425 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 426 | intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID) |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 427 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 428 | // fetch from cache |
| 429 | rsrcMgr.gemPortIDsForOnuLock.RLock() |
| 430 | gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] |
| 431 | rsrcMgr.gemPortIDsForOnuLock.RUnlock() |
| 432 | if ok { |
| 433 | return gemIDs |
| 434 | } |
| 435 | /* Get gem ports for given pon interface , onu id and uni id. */ |
| 436 | gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID) |
| 437 | |
| 438 | // update cache |
| 439 | rsrcMgr.gemPortIDsForOnuLock.Lock() |
| 440 | rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs |
| 441 | rsrcMgr.gemPortIDsForOnuLock.Unlock() |
| 442 | |
| 443 | return gemIDs |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 444 | } |
| 445 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 446 | // GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 447 | func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, onuID uint32, uniID uint32) []uint32 { |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 448 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 449 | intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 450 | // fetch from cache |
| 451 | rsrcMgr.allocIDsForOnuLock.RLock() |
| 452 | allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID] |
| 453 | rsrcMgr.allocIDsForOnuLock.RUnlock() |
| 454 | if ok { |
| 455 | return allocIDs |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 456 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 457 | allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID) |
| 458 | |
| 459 | // update cache |
| 460 | rsrcMgr.allocIDsForOnuLock.Lock() |
| 461 | rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs |
| 462 | rsrcMgr.allocIDsForOnuLock.Unlock() |
| 463 | |
| 464 | return allocIDs |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 468 | func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, onuID uint32, uniID uint32, allocID uint32) { |
| 469 | allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, onuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 470 | for i := 0; i < len(allocIDs); i++ { |
| 471 | if allocIDs[i] == allocID { |
| 472 | allocIDs = append(allocIDs[:i], allocIDs[i+1:]...) |
| 473 | break |
| 474 | } |
| 475 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 476 | err := rsrcMgr.UpdateAllocIdsForOnu(ctx, onuID, uniID, allocIDs) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 477 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 478 | logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. intfID %d onuID %d uniID %d allocID %d", |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 479 | rsrcMgr.PonIntfID, onuID, uniID, allocID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | // RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 484 | func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, onuID uint32, uniID uint32, gemPortID uint32) { |
| 485 | gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, onuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 486 | for i := 0; i < len(gemPortIDs); i++ { |
| 487 | if gemPortIDs[i] == gemPortID { |
| 488 | gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...) |
| 489 | break |
| 490 | } |
| 491 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 492 | err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, onuID, uniID, gemPortIDs) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 493 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 494 | logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. intfID %d onuID %d uniID %d gemPortId %d", |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 495 | rsrcMgr.PonIntfID, onuID, uniID, gemPortID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 496 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 497 | } |
| 498 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 499 | // UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 500 | func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, onuID uint32, |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 501 | uniID uint32, gemIDs []uint32) error { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 502 | intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID) |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 503 | |
| 504 | if err := rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID, gemIDs); err != nil { |
| 505 | logger.Errorw(ctx, "Failed to update gem port ids for onu", log.Fields{"err": err}) |
| 506 | return err |
| 507 | } |
| 508 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 509 | // update cache |
| 510 | rsrcMgr.gemPortIDsForOnuLock.Lock() |
| 511 | rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs |
| 512 | rsrcMgr.gemPortIDsForOnuLock.Unlock() |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 513 | return nil |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 514 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 515 | |
| 516 | // FreeonuID releases(make free) onu id for a particular pon-port |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 517 | func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, onuID []uint32) { |
Girish Gowdra | b8f1b5a | 2021-06-27 20:42:40 -0700 | [diff] [blame] | 518 | if len(onuID) == 0 { |
| 519 | logger.Info(ctx, "onu id slice is nil, nothing to free") |
| 520 | return |
| 521 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 522 | if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, ponrmgr.ONU_ID, onuID); err != nil { |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 523 | logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{ |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 524 | "intf-id": rsrcMgr.PonIntfID, |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 525 | "onu-id": onuID, |
| 526 | "err": err.Error(), |
| 527 | }) |
Girish Gowdra | b8f1b5a | 2021-06-27 20:42:40 -0700 | [diff] [blame] | 528 | } else { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 529 | logger.Infow(ctx, "freed onu id", log.Fields{"intfID": rsrcMgr.PonIntfID, "onuID": onuID}) |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 530 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 531 | } |
| 532 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 533 | // FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association |
| 534 | // for the given OLT device. |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 535 | // The caller should ensure that this is a blocking call and this operation is serialized for |
| 536 | // the ONU so as not cause resource corruption since there are no mutexes used here. |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 537 | // Setting freeFromResourcePool to false will not clear it from the resource pool but only |
| 538 | // clear it for the given pon/onu/uni |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 539 | func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, onuID uint32, |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 540 | uniID uint32, allocID uint32, freeFromResourcePool bool) { |
Girish Gowdra | b77ded9 | 2020-04-08 11:45:05 -0700 | [diff] [blame] | 541 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 542 | rsrcMgr.RemoveAllocIDForOnu(ctx, onuID, uniID, allocID) |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 543 | if freeFromResourcePool { |
| 544 | allocIDs := make([]uint32, 0) |
| 545 | allocIDs = append(allocIDs, allocID) |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 546 | if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, ponrmgr.ALLOC_ID, allocIDs); err != nil { |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 547 | logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{ |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 548 | "intf-id": rsrcMgr.PonIntfID, |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 549 | "onu-id": onuID, |
| 550 | "err": err.Error(), |
| 551 | }) |
| 552 | } |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 553 | } |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association |
| 557 | // for the given OLT device. |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 558 | // The caller should ensure that this is a blocking call and this operation is serialized for |
| 559 | // the ONU so as not cause resource corruption since there are no mutexes used here. |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 560 | func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, onuID uint32, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 561 | uniID uint32, gemPortID uint32) { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 562 | rsrcMgr.RemoveGemPortIDForOnu(ctx, onuID, uniID, gemPortID) |
Girish Gowdra | b77ded9 | 2020-04-08 11:45:05 -0700 | [diff] [blame] | 563 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 564 | gemPortIDs := make([]uint32, 0) |
| 565 | gemPortIDs = append(gemPortIDs, gemPortID) |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 566 | if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil { |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 567 | logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{ |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 568 | "intf-id": rsrcMgr.PonIntfID, |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 569 | "onu-id": onuID, |
| 570 | "err": err.Error(), |
| 571 | }) |
| 572 | } |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 575 | // FreePONResourcesForONU make the pon resources free for a given pon interface and onu id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 576 | func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, onuID uint32, uniID uint32) { |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 577 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 578 | intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID) |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 579 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 580 | AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID) |
Matteo Scandolo | d625b4c | 2020-04-02 16:16:01 -0700 | [diff] [blame] | 581 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 582 | if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 583 | ponrmgr.ALLOC_ID, |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 584 | AllocIDs); err != nil { |
| 585 | logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{ |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 586 | "intf-id": rsrcMgr.PonIntfID, |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 587 | "onu-id": onuID, |
| 588 | "err": err.Error(), |
| 589 | }) |
| 590 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 591 | |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 592 | //update cache |
| 593 | rsrcMgr.allocIDsForOnuLock.Lock() |
| 594 | delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID) |
| 595 | rsrcMgr.allocIDsForOnuLock.Unlock() |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 596 | |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 597 | GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 598 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 599 | if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 600 | ponrmgr.GEMPORT_ID, |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 601 | GEMPortIDs); err != nil { |
| 602 | logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{ |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 603 | "intf-id": rsrcMgr.PonIntfID, |
Matteo Scandolo | 8458537 | 2021-03-18 14:21:22 -0700 | [diff] [blame] | 604 | "onu-id": onuID, |
| 605 | "err": err.Error(), |
| 606 | }) |
| 607 | } |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 608 | |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 609 | // update cache |
| 610 | rsrcMgr.gemPortIDsForOnuLock.Lock() |
| 611 | delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID) |
| 612 | rsrcMgr.gemPortIDsForOnuLock.Unlock() |
| 613 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 614 | // Clear resource map associated with (pon_intf_id, gemport_id) tuple. |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 615 | rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID) |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 616 | } |
| 617 | |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 618 | // IsFlowOnKvStore checks if the given flowID is present on the kv store |
| 619 | // Returns true if the flowID is found, otherwise it returns false |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 620 | func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, onuID int32, flowID uint64) (bool, error) { |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 621 | var anyError error |
Abhilash S.L | 7f17e40 | 2019-03-15 17:40:41 +0530 | [diff] [blame] | 622 | |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 623 | // In case of nni trap flow |
| 624 | if onuID == -1 { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 625 | nniTrapflowIDs, err := rsrcMgr.GetFlowIDsForGem(ctx, NNI) |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 626 | if err != nil { |
| 627 | logger.Warnw(ctx, "failed-to-get-nni-trap-flowIDs", log.Fields{"err": err}) |
| 628 | return false, err |
| 629 | } |
| 630 | for _, id := range nniTrapflowIDs { |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 631 | if flowID == id { |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 632 | return true, nil |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | } |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 636 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 637 | path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID) |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 638 | rsrcMgr.onuGemInfoLock.RLock() |
| 639 | val, ok := rsrcMgr.onuGemInfo[path] |
| 640 | rsrcMgr.onuGemInfoLock.RUnlock() |
| 641 | |
| 642 | if ok { |
| 643 | for _, gem := range val.GemPorts { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 644 | flowIDs, err := rsrcMgr.GetFlowIDsForGem(ctx, gem) |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 645 | if err != nil { |
| 646 | anyError = err |
| 647 | logger.Warnw(ctx, "failed-to-get-flowIDs-for-gem", log.Fields{"err": err, "onuID": onuID, "gem": gem}) |
| 648 | } else { |
| 649 | for _, id := range flowIDs { |
| 650 | if flowID == id { |
| 651 | return true, nil |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | return false, anyError |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 658 | } |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 659 | |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 660 | // GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 661 | // This path is formed as the following: {intfID, onuID, uniID}/tp_id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 662 | func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, onuID uint32, uniID uint32) []uint32 { |
| 663 | Path := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 664 | // fetch from cache |
| 665 | rsrcMgr.techProfileIDsForOnuLock.RLock() |
| 666 | tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path] |
| 667 | rsrcMgr.techProfileIDsForOnuLock.RUnlock() |
| 668 | if ok { |
| 669 | return tpIDs |
| 670 | } |
| 671 | Value, err := rsrcMgr.KVStore.Get(ctx, Path) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 672 | if err == nil { |
| 673 | if Value != nil { |
| 674 | Val, err := kvstore.ToByte(Value.Value) |
| 675 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 676 | logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err}) |
| 677 | return tpIDs |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 678 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 679 | if err = json.Unmarshal(Val, &tpIDs); err != nil { |
| 680 | logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err}) |
| 681 | return tpIDs |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | } else { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 685 | logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 686 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 687 | logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path) |
| 688 | |
| 689 | // update cache |
| 690 | rsrcMgr.techProfileIDsForOnuLock.Lock() |
| 691 | rsrcMgr.techProfileIDsForOnu[Path] = tpIDs |
| 692 | rsrcMgr.techProfileIDsForOnuLock.Unlock() |
| 693 | |
| 694 | return tpIDs |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 695 | |
| 696 | } |
| 697 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 698 | // RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 699 | // This path is formed as the following: {intfID, onuID, uniID}/tp_id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 700 | func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, onuID uint32, uniID uint32) error { |
| 701 | intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 702 | |
| 703 | if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil { |
| 704 | logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID}) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 705 | return err |
| 706 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 707 | |
| 708 | // update cache |
| 709 | rsrcMgr.techProfileIDsForOnuLock.Lock() |
| 710 | delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID) |
| 711 | rsrcMgr.techProfileIDsForOnuLock.Unlock() |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 712 | return nil |
| 713 | } |
| 714 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 715 | // RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 716 | // This path is formed as the following: {intfID, onuID, uniID}/tp_id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 717 | func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, onuID uint32, uniID uint32, tpID uint32) error { |
| 718 | tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, onuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 719 | for i, tpIDInList := range tpIDList { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 720 | if tpIDInList == tpID { |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 721 | tpIDList = append(tpIDList[:i], tpIDList[i+1:]...) |
| 722 | } |
| 723 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 724 | intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 725 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 726 | Value, err := json.Marshal(tpIDList) |
| 727 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 728 | logger.Error(ctx, "failed to Marshal") |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 729 | return err |
| 730 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 731 | if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil { |
| 732 | logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 733 | return err |
| 734 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 735 | |
| 736 | // update cache |
| 737 | rsrcMgr.techProfileIDsForOnuLock.Lock() |
| 738 | rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList |
| 739 | rsrcMgr.techProfileIDsForOnuLock.Unlock() |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 740 | return err |
| 741 | } |
| 742 | |
| 743 | // UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 744 | // This path is formed as the following: {intfID, onuID, uniID}/tp_id |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 745 | func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, onuID uint32, |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 746 | uniID uint32, tpID uint32) error { |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 747 | var Value []byte |
| 748 | var err error |
| 749 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 750 | intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 751 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 752 | tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, onuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 753 | for _, value := range tpIDList { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 754 | if value == tpID { |
| 755 | logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 756 | return err |
| 757 | } |
| 758 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 759 | logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID) |
| 760 | tpIDList = append(tpIDList, tpID) |
| 761 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 762 | Value, err = json.Marshal(tpIDList) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 763 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 764 | logger.Error(ctx, "failed to Marshal") |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 765 | return err |
| 766 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 767 | if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil { |
| 768 | logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 769 | return err |
| 770 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 771 | |
| 772 | // update cache |
| 773 | rsrcMgr.techProfileIDsForOnuLock.Lock() |
| 774 | rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList |
| 775 | rsrcMgr.techProfileIDsForOnuLock.Unlock() |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 776 | return err |
| 777 | } |
| 778 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 779 | // StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 780 | // This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction> |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 781 | func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, onuID uint32, |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 782 | uniID uint32, tpID uint32, meterInfo *MeterInfo) error { |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 783 | var Value []byte |
| 784 | var err error |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 785 | intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 786 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 787 | Value, err = json.Marshal(*meterInfo) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 788 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 789 | logger.Error(ctx, "failed to Marshal meter config") |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 790 | return err |
| 791 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 792 | if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil { |
| 793 | logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 794 | return err |
| 795 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 796 | |
| 797 | // update cache |
| 798 | rsrcMgr.meterInfoForOnuLock.Lock() |
| 799 | rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo |
| 800 | rsrcMgr.meterInfoForOnuLock.Unlock() |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 801 | logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo}) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 802 | return err |
| 803 | } |
| 804 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 805 | // GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 806 | // This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction> |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 807 | func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, onuID uint32, |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 808 | uniID uint32, tpID uint32) (*MeterInfo, error) { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 809 | Path := fmt.Sprintf(MeterIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 810 | |
| 811 | // get from cache |
| 812 | rsrcMgr.meterInfoForOnuLock.RLock() |
| 813 | val, ok := rsrcMgr.meterInfoForOnu[Path] |
| 814 | rsrcMgr.meterInfoForOnuLock.RUnlock() |
| 815 | if ok { |
| 816 | return val, nil |
| 817 | } |
| 818 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 819 | var meterInfo MeterInfo |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 820 | Value, err := rsrcMgr.KVStore.Get(ctx, Path) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 821 | if err == nil { |
| 822 | if Value != nil { |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 823 | logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction}) |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 824 | Val, er := kvstore.ToByte(Value.Value) |
| 825 | if er != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 826 | logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er}) |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 827 | return nil, er |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 828 | } |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 829 | if er = json.Unmarshal(Val, &meterInfo); er != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 830 | logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er}) |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 831 | return nil, er |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 832 | } |
| 833 | } else { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 834 | logger.Debug(ctx, "meter-does-not-exists-in-KVStore") |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 835 | return nil, err |
| 836 | } |
| 837 | } else { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 838 | logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 839 | |
| 840 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 841 | // update cache |
| 842 | rsrcMgr.meterInfoForOnuLock.Lock() |
| 843 | rsrcMgr.meterInfoForOnu[Path] = &meterInfo |
| 844 | rsrcMgr.meterInfoForOnuLock.Unlock() |
| 845 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 846 | return &meterInfo, err |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 847 | } |
| 848 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 849 | // HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter. |
| 850 | // When reference count becomes 0, it clears the meter information from the kv store |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 851 | func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string, |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 852 | onuID uint32, uniID uint32, tpID uint32, increment bool) error { |
| 853 | meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, onuID, uniID, tpID) |
Girish Gowdra | 82c8098 | 2021-03-26 16:22:02 -0700 | [diff] [blame] | 854 | if err != nil { |
| 855 | return err |
| 856 | } else if meterInfo == nil { |
| 857 | // If we are increasing the reference count, we expect the meter information to be present on KV store. |
| 858 | // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error. |
| 859 | if increment { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 860 | logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction) |
| 861 | return fmt.Errorf("error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction) |
Girish Gowdra | 82c8098 | 2021-03-26 16:22:02 -0700 | [diff] [blame] | 862 | } |
| 863 | logger.Warnw(ctx, "meter is already cleared", |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 864 | log.Fields{"intfID": rsrcMgr.PonIntfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment}) |
Girish Gowdra | 82c8098 | 2021-03-26 16:22:02 -0700 | [diff] [blame] | 865 | return nil |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 866 | } |
Girish Gowdra | 82c8098 | 2021-03-26 16:22:02 -0700 | [diff] [blame] | 867 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 868 | if increment { |
| 869 | meterInfo.RefCnt++ |
| 870 | } else { |
| 871 | meterInfo.RefCnt-- |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 872 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 873 | if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, onuID, uniID, tpID, meterInfo); err != nil { |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 874 | return err |
| 875 | } |
| 876 | return nil |
| 877 | } |
| 878 | |
| 879 | // RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 880 | // This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction> |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 881 | func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, onuID uint32, |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 882 | uniID uint32, tpID uint32) error { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 883 | Path := fmt.Sprintf(MeterIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 884 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 885 | if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 886 | logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 887 | return err |
| 888 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 889 | |
| 890 | // update cache |
| 891 | rsrcMgr.meterInfoForOnuLock.Lock() |
| 892 | delete(rsrcMgr.meterInfoForOnu, Path) |
| 893 | rsrcMgr.meterInfoForOnuLock.Unlock() |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 894 | return nil |
| 895 | } |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 896 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 897 | // AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 898 | func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, onuID uint32, gemPort uint32) error { |
| 899 | onugem, err := rsrcMgr.GetOnuGemInfo(ctx, onuID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 900 | if err != nil || onugem == nil || onugem.SerialNumber == "" { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 901 | logger.Errorf(ctx, "failed to get onuifo for intfid %d", rsrcMgr.PonIntfID) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 902 | return err |
| 903 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 904 | if onugem.OnuID == onuID { |
| 905 | for _, gem := range onugem.GemPorts { |
| 906 | if gem == gemPort { |
| 907 | logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem}) |
| 908 | return nil |
| 909 | } |
| 910 | } |
| 911 | logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort}) |
| 912 | onugem.GemPorts = append(onugem.GemPorts, gemPort) |
| 913 | } else { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 914 | logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": rsrcMgr.PonIntfID, "onuGemInfoOnuID": onugem.OnuID}) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 915 | return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 916 | } |
| 917 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 918 | err = rsrcMgr.AddOnuGemInfo(ctx, onuID, *onugem) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 919 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 920 | logger.Error(ctx, "Failed to add onugem to kv store") |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 921 | return err |
| 922 | } |
| 923 | return err |
| 924 | } |
| 925 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 926 | // RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 927 | func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, onuID uint32, gemPort uint32) error { |
| 928 | onugem, err := rsrcMgr.GetOnuGemInfo(ctx, onuID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 929 | if err != nil || onugem == nil || onugem.SerialNumber == "" { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 930 | logger.Errorf(ctx, "failed to get onuifo for intfid %d", rsrcMgr.PonIntfID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 931 | return err |
| 932 | } |
| 933 | updated := false |
| 934 | if onugem.OnuID == onuID { |
| 935 | for i, gem := range onugem.GemPorts { |
| 936 | if gem == gemPort { |
| 937 | logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem}) |
| 938 | onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...) |
| 939 | updated = true |
| 940 | break |
| 941 | } |
| 942 | } |
| 943 | } else { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 944 | logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": rsrcMgr.PonIntfID, "onuGemInfoOnuID": onugem.OnuID}) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 945 | return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID) |
| 946 | } |
| 947 | if updated { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 948 | err = rsrcMgr.AddOnuGemInfo(ctx, onuID, *onugem) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 949 | if err != nil { |
| 950 | logger.Error(ctx, "Failed to add onugem to kv store") |
| 951 | return err |
| 952 | } |
| 953 | } else { |
| 954 | logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort}) |
| 955 | } |
| 956 | return nil |
| 957 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 958 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 959 | // GetOnuGemInfo gets onu gem info from the kvstore per interface |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 960 | func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, onuID uint32) (*OnuGemInfo, error) { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 961 | var err error |
| 962 | var Val []byte |
| 963 | var onugem OnuGemInfo |
| 964 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 965 | path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 966 | |
| 967 | rsrcMgr.onuGemInfoLock.RLock() |
| 968 | val, ok := rsrcMgr.onuGemInfo[path] |
| 969 | rsrcMgr.onuGemInfoLock.RUnlock() |
| 970 | if ok { |
| 971 | return val, nil |
| 972 | } |
| 973 | value, err := rsrcMgr.KVStore.Get(ctx, path) |
| 974 | if err != nil { |
| 975 | logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path}) |
| 976 | return nil, err |
| 977 | } else if value == nil { |
| 978 | logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path}) |
| 979 | return nil, nil // returning nil as this could happen if there are no onus for the interface yet |
| 980 | } |
| 981 | if Val, err = kvstore.ToByte(value.Value); err != nil { |
| 982 | logger.Error(ctx, "Failed to convert to byte array") |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 983 | return nil, err |
| 984 | } |
| 985 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 986 | if err = json.Unmarshal(Val, &onugem); err != nil { |
| 987 | logger.Error(ctx, "Failed to unmarshall") |
| 988 | return nil, err |
| 989 | } |
| 990 | logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem}) |
| 991 | rsrcMgr.onuGemInfoLock.Lock() |
| 992 | rsrcMgr.onuGemInfo[path] = &onugem |
| 993 | rsrcMgr.onuGemInfoLock.Unlock() |
| 994 | |
| 995 | return &onugem, nil |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 996 | } |
| 997 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 998 | // AddNewOnuGemInfoToCacheAndKvStore function adds a new onu gem info to cache and kvstore |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 999 | func (rsrcMgr *OpenOltResourceMgr) AddNewOnuGemInfoToCacheAndKvStore(ctx context.Context, onuID uint32, serialNum string) error { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1000 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1001 | Path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1002 | |
| 1003 | rsrcMgr.onuGemInfoLock.Lock() |
| 1004 | _, ok := rsrcMgr.onuGemInfo[Path] |
| 1005 | rsrcMgr.onuGemInfoLock.Unlock() |
| 1006 | |
| 1007 | // If the ONU already exists in onuGemInfo list, nothing to do |
| 1008 | if ok { |
| 1009 | logger.Debugw(ctx, "onu-id-already-exists-in-cache", log.Fields{"onuID": onuID, "serialNum": serialNum}) |
| 1010 | return nil |
| 1011 | } |
| 1012 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1013 | onuGemInfo := OnuGemInfo{OnuID: onuID, SerialNumber: serialNum, IntfID: rsrcMgr.PonIntfID} |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1014 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1015 | if err := rsrcMgr.AddOnuGemInfo(ctx, onuID, onuGemInfo); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1016 | return err |
| 1017 | } |
| 1018 | logger.Infow(ctx, "added-onuinfo", |
| 1019 | log.Fields{ |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1020 | "intf-id": rsrcMgr.PonIntfID, |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1021 | "onu-id": onuID, |
| 1022 | "serial-num": serialNum, |
| 1023 | "onu": onuGemInfo, |
| 1024 | "device-id": rsrcMgr.DeviceID}) |
| 1025 | return nil |
| 1026 | } |
| 1027 | |
Chaitrashree G S | 1a55b88 | 2020-02-04 17:35:35 -0500 | [diff] [blame] | 1028 | // AddOnuGemInfo adds onu info on to the kvstore per interface |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1029 | func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, onuID uint32, onuGem OnuGemInfo) error { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1030 | |
| 1031 | var Value []byte |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1032 | var err error |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1033 | Path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1034 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1035 | Value, err = json.Marshal(onuGem) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1036 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1037 | logger.Error(ctx, "failed to Marshal") |
| 1038 | return err |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1039 | } |
| 1040 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1041 | if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil { |
| 1042 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
| 1043 | return err |
| 1044 | } |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1045 | logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem, "Path": Path}) |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1046 | |
| 1047 | //update cache |
| 1048 | rsrcMgr.onuGemInfoLock.Lock() |
| 1049 | rsrcMgr.onuGemInfo[Path] = &onuGem |
| 1050 | rsrcMgr.onuGemInfoLock.Unlock() |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1051 | return err |
| 1052 | } |
| 1053 | |
| 1054 | // DelOnuGemInfo deletes the onugem info from kvstore per ONU |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1055 | func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, onuID uint32) error { |
| 1056 | path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1057 | |
| 1058 | if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil { |
| 1059 | logger.Errorf(ctx, "failed to remove resource %s", path) |
| 1060 | return err |
| 1061 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1062 | |
| 1063 | //update cache |
| 1064 | rsrcMgr.onuGemInfoLock.Lock() |
| 1065 | logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]}) |
| 1066 | delete(rsrcMgr.onuGemInfo, path) |
| 1067 | rsrcMgr.onuGemInfoLock.Unlock() |
Andrea Campanella | b83b39d | 2020-03-30 11:41:16 +0200 | [diff] [blame] | 1068 | return nil |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1069 | } |
| 1070 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1071 | // DeleteAllOnuGemInfoForIntf deletes all the all onu gem info on the given pon interface |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1072 | func (rsrcMgr *OpenOltResourceMgr) DeleteAllOnuGemInfoForIntf(ctx context.Context) error { |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1073 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1074 | path := fmt.Sprintf(OnuGemInfoPathPathPrefix, rsrcMgr.PonIntfID) |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1075 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1076 | logger.Debugw(ctx, "delete-all-onu-gem-info-for-pon-intf", log.Fields{"intfID": rsrcMgr.PonIntfID}) |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1077 | if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil { |
| 1078 | logger.Errorf(ctx, "failed-to-remove-resource-%s", path) |
| 1079 | return err |
| 1080 | } |
| 1081 | |
| 1082 | // Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked. |
| 1083 | rsrcMgr.onuGemInfoLock.Lock() |
| 1084 | rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo) |
| 1085 | rsrcMgr.onuGemInfoLock.Unlock() |
| 1086 | return nil |
| 1087 | } |
| 1088 | |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1089 | // AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store. |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1090 | func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, onuID uint32, portNo uint32) { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1091 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1092 | onugem, err := rsrcMgr.GetOnuGemInfo(ctx, onuID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1093 | if err != nil || onugem == nil || onugem.SerialNumber == "" { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1094 | logger.Warnf(ctx, "failed to get onuifo for intfid %d", rsrcMgr.PonIntfID) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1095 | return |
| 1096 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1097 | |
| 1098 | if onugem.OnuID == onuID { |
| 1099 | for _, uni := range onugem.UniPorts { |
| 1100 | if uni == portNo { |
| 1101 | logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo}) |
| 1102 | return |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1103 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1104 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1105 | onugem.UniPorts = append(onugem.UniPorts, portNo) |
| 1106 | } else { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1107 | logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": rsrcMgr.PonIntfID, "onuID": onuID}) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1108 | return |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1109 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1110 | err = rsrcMgr.AddOnuGemInfo(ctx, onuID, *onugem) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1111 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1112 | logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo}) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1113 | return |
| 1114 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1115 | } |
| 1116 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1117 | // UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1118 | func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1119 | |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1120 | path := fmt.Sprintf(OnuPacketInPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1121 | |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1122 | Value, err := json.Marshal(gemPort) |
| 1123 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1124 | logger.Error(ctx, "Failed to marshal data") |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1125 | return |
| 1126 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1127 | if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1128 | logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort}) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1129 | return |
| 1130 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1131 | |
| 1132 | // update cache |
| 1133 | rsrcMgr.gemPortForPacketInInfoLock.Lock() |
| 1134 | rsrcMgr.gemPortForPacketInInfo[path] = gemPort |
| 1135 | rsrcMgr.gemPortForPacketInInfoLock.Unlock() |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1136 | logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort}) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1137 | } |
| 1138 | |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 1139 | // GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1140 | func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1141 | |
| 1142 | var Val []byte |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1143 | |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1144 | path := fmt.Sprintf(OnuPacketInPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort, |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 1145 | packetInInfoKey.VlanID, packetInInfoKey.Priority) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1146 | // get from cache |
| 1147 | rsrcMgr.gemPortForPacketInInfoLock.RLock() |
| 1148 | gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path] |
| 1149 | rsrcMgr.gemPortForPacketInInfoLock.RUnlock() |
| 1150 | if ok { |
| 1151 | logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort}) |
| 1152 | return gemPort, nil |
| 1153 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1154 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1155 | value, err := rsrcMgr.KVStore.Get(ctx, path) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1156 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1157 | logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path}) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1158 | return uint32(0), err |
| 1159 | } else if value == nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1160 | logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path}) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1161 | return uint32(0), nil |
| 1162 | } |
| 1163 | |
| 1164 | if Val, err = kvstore.ToByte(value.Value); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1165 | logger.Error(ctx, "Failed to convert to byte array") |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1166 | return uint32(0), err |
| 1167 | } |
| 1168 | if err = json.Unmarshal(Val, &gemPort); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1169 | logger.Error(ctx, "Failed to unmarshall") |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1170 | return uint32(0), err |
| 1171 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1172 | logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort}) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1173 | // update cache |
| 1174 | rsrcMgr.gemPortForPacketInInfoLock.Lock() |
| 1175 | rsrcMgr.gemPortForPacketInInfo[path] = gemPort |
| 1176 | rsrcMgr.gemPortForPacketInInfoLock.Unlock() |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1177 | |
| 1178 | return gemPort, nil |
| 1179 | } |
| 1180 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1181 | // DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1182 | func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, onuID uint32, logicalPort uint32) error { |
| 1183 | path := fmt.Sprintf(OnuPacketInPathPrefix, rsrcMgr.PonIntfID, onuID, logicalPort) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1184 | value, err := rsrcMgr.KVStore.List(ctx, path) |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 1185 | if err != nil { |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 1186 | logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path) |
| 1187 | return errors.New("failed-to-read-value-from-path-" + path) |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 1188 | } |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 1189 | |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1190 | logger.Debugw(ctx, "delete-packetin-gem-port", log.Fields{"realPath": path}) |
| 1191 | if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil { |
| 1192 | logger.Errorf(ctx, "failed-to-remove-resource-%s", path) |
| 1193 | return err |
| 1194 | } |
| 1195 | |
Esin Karaman | 7fb80c2 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 1196 | //remove them one by one |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 1197 | for key := range value { |
serkant.uluderya | 4f5ed59 | 2020-12-17 20:57:59 +0300 | [diff] [blame] | 1198 | // Remove the PathPrefix from the path on KV key. |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1199 | // gemPortForPacketInInfo cache uses OnuPacketInPath as the key |
serkant.uluderya | 4f5ed59 | 2020-12-17 20:57:59 +0300 | [diff] [blame] | 1200 | stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 1201 | replacedWith := "" |
| 1202 | key = strings.Replace(key, stringToBeReplaced, replacedWith, 1) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1203 | // update cache |
| 1204 | rsrcMgr.gemPortForPacketInInfoLock.Lock() |
| 1205 | delete(rsrcMgr.gemPortForPacketInInfo, key) |
| 1206 | rsrcMgr.gemPortForPacketInInfoLock.Unlock() |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 1207 | |
serkant.uluderya | 4f5ed59 | 2020-12-17 20:57:59 +0300 | [diff] [blame] | 1208 | logger.Debugw(ctx, "removed-key-from-packetin-gem-port-cache", log.Fields{"key": key, "cache-len": len(rsrcMgr.gemPortForPacketInInfo)}) |
| 1209 | } |
| 1210 | |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1211 | return nil |
| 1212 | } |
| 1213 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1214 | // GetFlowIDsForGem gets the list of FlowIDs for the given gemport |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1215 | func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, gem uint32) ([]uint64, error) { |
| 1216 | path := fmt.Sprintf(FlowIDsForGem, rsrcMgr.PonIntfID, gem) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1217 | |
| 1218 | // get from cache |
| 1219 | rsrcMgr.flowIDsForGemLock.RLock() |
| 1220 | flowIDs, ok := rsrcMgr.flowIDsForGem[gem] |
| 1221 | rsrcMgr.flowIDsForGemLock.RUnlock() |
| 1222 | if ok { |
| 1223 | return flowIDs, nil |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1224 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1225 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1226 | value, err := rsrcMgr.KVStore.Get(ctx, path) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1227 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1228 | logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path}) |
| 1229 | return nil, err |
yasin sapli | d056627 | 2021-12-21 09:10:30 +0000 | [diff] [blame] | 1230 | } else if value == nil || value.Value == nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1231 | logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path}) |
| 1232 | return nil, nil |
| 1233 | } |
| 1234 | Val, err := kvstore.ToByte(value.Value) |
| 1235 | if err != nil { |
| 1236 | logger.Error(ctx, "Failed to convert to byte array") |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1237 | return nil, err |
| 1238 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1239 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1240 | if err = json.Unmarshal(Val, &flowIDs); err != nil { |
| 1241 | logger.Error(ctx, "Failed to unmarshall") |
| 1242 | return nil, err |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1243 | } |
| 1244 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1245 | // update cache |
| 1246 | rsrcMgr.flowIDsForGemLock.Lock() |
| 1247 | rsrcMgr.flowIDsForGem[gem] = flowIDs |
| 1248 | rsrcMgr.flowIDsForGemLock.Unlock() |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1249 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1250 | return flowIDs, nil |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1251 | } |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1252 | |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1253 | // IsGemPortUsedByAnotherFlow returns true if given gem is used by another flow |
Girish Gowdra | b4c3330 | 2022-03-18 15:07:38 -0700 | [diff] [blame] | 1254 | func (rsrcMgr *OpenOltResourceMgr) IsGemPortUsedByAnotherFlow(ctx context.Context, gemPortID uint32, flowID uint64) (bool, error) { |
| 1255 | flowIDList, err := rsrcMgr.GetFlowIDsForGem(ctx, gemPortID) |
| 1256 | if err != nil { |
| 1257 | return false, err |
| 1258 | } |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1259 | for _, id := range flowIDList { |
| 1260 | if flowID != id { |
Girish Gowdra | b4c3330 | 2022-03-18 15:07:38 -0700 | [diff] [blame] | 1261 | return true, nil |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1262 | } |
| 1263 | } |
Girish Gowdra | b4c3330 | 2022-03-18 15:07:38 -0700 | [diff] [blame] | 1264 | return false, nil |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // RegisterFlowIDForGem updates both cache and KV store for flowIDsForGem map |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1268 | func (rsrcMgr *OpenOltResourceMgr) RegisterFlowIDForGem(ctx context.Context, gemPortID uint32, flowFromCore *ofp.OfpFlowStats) error { |
Girish Gowdra | b4c3330 | 2022-03-18 15:07:38 -0700 | [diff] [blame] | 1269 | flowIDs, err := rsrcMgr.GetFlowIDsForGem(ctx, gemPortID) |
| 1270 | if err != nil { |
| 1271 | return err |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1272 | } |
Girish Gowdra | b4c3330 | 2022-03-18 15:07:38 -0700 | [diff] [blame] | 1273 | |
| 1274 | flowIDs = appendUnique64bit(flowIDs, flowFromCore.Id) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1275 | // update the flowids for a gem to the KVstore |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1276 | return rsrcMgr.UpdateFlowIDsForGem(ctx, gemPortID, flowIDs) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1279 | // UpdateFlowIDsForGem updates flow id per gemport |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1280 | func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, gem uint32, flowIDs []uint64) error { |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1281 | var val []byte |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1282 | path := fmt.Sprintf(FlowIDsForGem, rsrcMgr.PonIntfID, gem) |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1283 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1284 | if flowIDs == nil { |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1285 | if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil { |
| 1286 | logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path}) |
| 1287 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1288 | return nil |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1289 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1290 | val, err := json.Marshal(flowIDs) |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1291 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1292 | logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err}) |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1293 | return err |
| 1294 | } |
Girish Gowdra | b77ded9 | 2020-04-08 11:45:05 -0700 | [diff] [blame] | 1295 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1296 | if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil { |
| 1297 | logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val}) |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1298 | return err |
| 1299 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1300 | logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs}) |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1301 | |
| 1302 | // update cache |
| 1303 | rsrcMgr.flowIDsForGemLock.Lock() |
| 1304 | rsrcMgr.flowIDsForGem[gem] = flowIDs |
| 1305 | rsrcMgr.flowIDsForGemLock.Unlock() |
Abhilash Laxmeshwar | 275c074 | 2019-11-25 16:47:02 +0530 | [diff] [blame] | 1306 | return nil |
| 1307 | } |
| 1308 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1309 | // DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore. |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1310 | func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, gem uint32) error { |
| 1311 | path := fmt.Sprintf(FlowIDsForGem, rsrcMgr.PonIntfID, gem) |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1312 | if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil { |
| 1313 | logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path}) |
| 1314 | return err |
| 1315 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1316 | // update cache |
| 1317 | rsrcMgr.flowIDsForGemLock.Lock() |
| 1318 | delete(rsrcMgr.flowIDsForGem, gem) |
| 1319 | rsrcMgr.flowIDsForGemLock.Unlock() |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1320 | return nil |
Abhilash Laxmeshwar | 6d1acb9 | 2020-01-17 15:43:03 +0530 | [diff] [blame] | 1321 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1322 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1323 | // DeleteAllFlowIDsForGemForIntf deletes all the flow ids associated for all the gems on the given pon interface |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1324 | func (rsrcMgr *OpenOltResourceMgr) DeleteAllFlowIDsForGemForIntf(ctx context.Context) error { |
| 1325 | path := fmt.Sprintf(FlowIDsForGemPathPrefix, rsrcMgr.PonIntfID) |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1326 | |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1327 | logger.Debugw(ctx, "delete-flow-ids-for-gem-for-pon-intf", log.Fields{"intfID": rsrcMgr.PonIntfID}) |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 1328 | if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil { |
| 1329 | logger.Errorf(ctx, "failed-to-remove-resource-%s", path) |
| 1330 | return err |
| 1331 | } |
| 1332 | |
| 1333 | // Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked. |
| 1334 | rsrcMgr.flowIDsForGemLock.Lock() |
| 1335 | rsrcMgr.flowIDsForGem = make(map[uint32][]uint64) |
| 1336 | rsrcMgr.flowIDsForGemLock.Unlock() |
| 1337 | return nil |
| 1338 | } |
| 1339 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1340 | // GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1341 | func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1342 | path := McastQueuesForIntf |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1343 | var val []byte |
| 1344 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1345 | rsrcMgr.mcastQueueForIntfLock.RLock() |
| 1346 | if rsrcMgr.mcastQueueForIntfLoadedFromKvStore { |
| 1347 | rsrcMgr.mcastQueueForIntfLock.RUnlock() |
| 1348 | return rsrcMgr.mcastQueueForIntf, nil |
| 1349 | } |
| 1350 | rsrcMgr.mcastQueueForIntfLock.RUnlock() |
| 1351 | |
| 1352 | kvPair, err := rsrcMgr.KVStore.Get(ctx, path) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1353 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1354 | logger.Error(ctx, "failed to get data from kv store") |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1355 | return nil, err |
| 1356 | } |
| 1357 | if kvPair != nil && kvPair.Value != nil { |
| 1358 | if val, err = kvstore.ToByte(kvPair.Value); err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1359 | logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1360 | return nil, err |
| 1361 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1362 | rsrcMgr.mcastQueueForIntfLock.Lock() |
| 1363 | defer rsrcMgr.mcastQueueForIntfLock.Unlock() |
| 1364 | if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil { |
| 1365 | logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1366 | return nil, err |
| 1367 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1368 | rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1369 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1370 | return rsrcMgr.mcastQueueForIntf, nil |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1373 | // AddMcastQueueForIntf adds multicast queue for pon interface |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1374 | func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, gem uint32, servicePriority uint32) error { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1375 | var val []byte |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1376 | path := McastQueuesForIntf |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1377 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1378 | // Load local cache from kv store the first time |
| 1379 | rsrcMgr.mcastQueueForIntfLock.RLock() |
| 1380 | if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore { |
| 1381 | rsrcMgr.mcastQueueForIntfLock.RUnlock() |
| 1382 | _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx) |
| 1383 | if err != nil { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1384 | logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": rsrcMgr.PonIntfID}) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1385 | return err |
| 1386 | } |
| 1387 | } else { |
| 1388 | rsrcMgr.mcastQueueForIntfLock.RUnlock() |
| 1389 | } |
| 1390 | |
| 1391 | // Update KV store |
| 1392 | rsrcMgr.mcastQueueForIntfLock.Lock() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1393 | rsrcMgr.mcastQueueForIntf[rsrcMgr.PonIntfID] = []uint32{gem, servicePriority} |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1394 | val, err := json.Marshal(rsrcMgr.mcastQueueForIntf) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1395 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1396 | rsrcMgr.mcastQueueForIntfLock.Unlock() |
| 1397 | logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1398 | return err |
| 1399 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1400 | rsrcMgr.mcastQueueForIntfLock.Unlock() |
| 1401 | |
| 1402 | if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil { |
| 1403 | logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1404 | return err |
| 1405 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 1406 | logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "interfaceId": rsrcMgr.PonIntfID, "gem": gem, "svcPrior": servicePriority}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1407 | return nil |
| 1408 | } |
| 1409 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1410 | // DeleteMcastQueueForIntf deletes multicast queue info for the current pon interface from kvstore |
Gustavo Silva | 41af912 | 2022-10-11 11:05:13 -0300 | [diff] [blame] | 1411 | func (rsrcMgr *OpenOltResourceMgr) DeleteMcastQueueForIntf(ctx context.Context) error { |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 1412 | path := McastQueuesForIntf |
| 1413 | |
| 1414 | if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil { |
| 1415 | logger.Errorw(ctx, "Failed to delete multicast queue info from kvstore", log.Fields{"err": err, "interfaceId": rsrcMgr.PonIntfID}) |
Gustavo Silva | 41af912 | 2022-10-11 11:05:13 -0300 | [diff] [blame] | 1416 | return err |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 1417 | } |
| 1418 | logger.Debugw(ctx, "deleted multicast queue info from KV store successfully", log.Fields{"interfaceId": rsrcMgr.PonIntfID}) |
Gustavo Silva | 41af912 | 2022-10-11 11:05:13 -0300 | [diff] [blame] | 1419 | return nil |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1422 | // AddFlowGroupToKVStore adds flow group into KV store |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1423 | func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1424 | var Value []byte |
| 1425 | var err error |
| 1426 | var path string |
| 1427 | if cached { |
| 1428 | path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId) |
| 1429 | } else { |
| 1430 | path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId) |
| 1431 | } |
| 1432 | //build group info object |
| 1433 | var outPorts []uint32 |
| 1434 | for _, ofBucket := range groupEntry.Desc.Buckets { |
| 1435 | for _, ofAction := range ofBucket.Actions { |
| 1436 | if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT { |
| 1437 | outPorts = append(outPorts, ofAction.GetOutput().Port) |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | groupInfo := GroupInfo{ |
| 1442 | GroupID: groupEntry.Desc.GroupId, |
| 1443 | OutPorts: outPorts, |
| 1444 | } |
| 1445 | |
| 1446 | Value, err = json.Marshal(groupInfo) |
| 1447 | |
| 1448 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1449 | logger.Error(ctx, "failed to Marshal flow group object") |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1450 | return err |
| 1451 | } |
| 1452 | |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1453 | if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1454 | logger.Errorf(ctx, "Failed to update resource %s", path) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1455 | return err |
| 1456 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1457 | |
| 1458 | // update cache |
| 1459 | rsrcMgr.groupInfoLock.Lock() |
| 1460 | rsrcMgr.groupInfo[path] = &groupInfo |
| 1461 | rsrcMgr.groupInfoLock.Unlock() |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1462 | return nil |
| 1463 | } |
| 1464 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1465 | // RemoveFlowGroupFromKVStore removes flow group from KV store |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1466 | func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1467 | var path string |
| 1468 | if cached { |
| 1469 | path = fmt.Sprintf(FlowGroupCached, groupID) |
| 1470 | } else { |
| 1471 | path = fmt.Sprintf(FlowGroup, groupID) |
| 1472 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1473 | |
| 1474 | if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1475 | logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err) |
Esin Karaman | d519bbf | 2020-07-01 11:16:03 +0000 | [diff] [blame] | 1476 | return err |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1477 | } |
Gamze Abaka | 745ccb7 | 2021-11-18 11:29:58 +0000 | [diff] [blame] | 1478 | |
| 1479 | // update cache |
| 1480 | rsrcMgr.groupInfoLock.Lock() |
| 1481 | delete(rsrcMgr.groupInfo, path) |
| 1482 | rsrcMgr.groupInfoLock.Unlock() |
Esin Karaman | d519bbf | 2020-07-01 11:16:03 +0000 | [diff] [blame] | 1483 | return nil |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 1486 | // GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during |
| 1487 | // fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully. |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1488 | // Returns (false, {}, nil) if the group does not exists in the KV store. |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1489 | func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1490 | var groupInfo GroupInfo |
| 1491 | var path string |
| 1492 | if cached { |
| 1493 | path = fmt.Sprintf(FlowGroupCached, groupID) |
| 1494 | } else { |
| 1495 | path = fmt.Sprintf(FlowGroup, groupID) |
| 1496 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1497 | |
| 1498 | // read from cache |
| 1499 | rsrcMgr.groupInfoLock.RLock() |
| 1500 | gi, ok := rsrcMgr.groupInfo[path] |
| 1501 | rsrcMgr.groupInfoLock.RUnlock() |
| 1502 | if ok { |
| 1503 | return true, *gi, nil |
| 1504 | } |
| 1505 | |
| 1506 | kvPair, err := rsrcMgr.KVStore.Get(ctx, path) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1507 | if err != nil { |
| 1508 | return false, groupInfo, err |
| 1509 | } |
| 1510 | if kvPair != nil && kvPair.Value != nil { |
| 1511 | Val, err := kvstore.ToByte(kvPair.Value) |
| 1512 | if err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1513 | logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1514 | return false, groupInfo, err |
| 1515 | } |
| 1516 | if err = json.Unmarshal(Val, &groupInfo); err != nil { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1517 | logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err}) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1518 | return false, groupInfo, err |
| 1519 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 1520 | // update cache |
| 1521 | rsrcMgr.groupInfoLock.Lock() |
| 1522 | rsrcMgr.groupInfo[path] = &groupInfo |
| 1523 | rsrcMgr.groupInfoLock.Unlock() |
| 1524 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1525 | return true, groupInfo, nil |
| 1526 | } |
| 1527 | return false, groupInfo, nil |
| 1528 | } |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 1529 | |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1530 | // GetOnuGemInfoList returns all gems in the onuGemInfo map |
| 1531 | func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfoList(ctx context.Context) []OnuGemInfo { |
| 1532 | var onuGemInfoLst []OnuGemInfo |
| 1533 | rsrcMgr.onuGemInfoLock.RLock() |
| 1534 | defer rsrcMgr.onuGemInfoLock.RUnlock() |
| 1535 | for _, v := range rsrcMgr.onuGemInfo { |
| 1536 | onuGemInfoLst = append(onuGemInfoLst, *v) |
| 1537 | } |
| 1538 | return onuGemInfoLst |
| 1539 | } |
| 1540 | |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 1541 | func appendUnique64bit(slice []uint64, item uint64) []uint64 { |
| 1542 | for _, sliceElement := range slice { |
| 1543 | if sliceElement == item { |
| 1544 | return slice |
| 1545 | } |
| 1546 | } |
| 1547 | return append(slice, item) |
| 1548 | } |