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