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