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