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