[VOL-1349] EPON OLT adapter (package B)
Change-Id: I634ef62c53813dcf4456f54948f13e06358e263c
diff --git a/internal/pkg/resourcemanager/common.go b/internal/pkg/resourcemanager/common.go
new file mode 100644
index 0000000..f21d7b2
--- /dev/null
+++ b/internal/pkg/resourcemanager/common.go
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package resourcemanager Common Logger initialization
+package resourcemanager
+
+import (
+ "github.com/opencord/voltha-lib-go/v3/pkg/log"
+)
+
+var logger log.CLogger
+
+func init() {
+ var err error
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{})
+ if err != nil {
+ panic(err)
+ }
+}
diff --git a/internal/pkg/resourcemanager/resourcemanager.go b/internal/pkg/resourcemanager/resourcemanager.go
new file mode 100644
index 0000000..389e09e
--- /dev/null
+++ b/internal/pkg/resourcemanager/resourcemanager.go
@@ -0,0 +1,1488 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//Package resourcemanager provides the utility for managing resources
+package resourcemanager
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "strconv"
+ "sync"
+ "time"
+
+ "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
+
+ "github.com/opencord/voltha-lib-go/v3/pkg/db"
+ "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
+ "github.com/opencord/voltha-lib-go/v3/pkg/log"
+ ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
+ ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
+ "github.com/opencord/voltha-protos/v3/go/openolt"
+)
+
+const (
+ KvstoreTimeout = 5 * time.Second
+ BasePathKvStore = "service/voltha/openolt/{%s}"
+ TpIDPathSuffix = "{%d,%d,%d}/tp_id"
+ MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
+ NnniIntfID = "nniintfids"
+ OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d"
+ OnuPacketINPath = OnuPacketINPathPrefix + ",%d,%d}"
+ FlowIDsForGem = "flowids_per_gem/{%d}"
+ McastQueuesForIntf = "mcast_qs_for_int"
+ FlowGroup = "flow_groups/{%d}"
+ FlowGroupCached = "flow_groups_cached/{%d}"
+)
+
+// FlowInfo holds the flow information
+type FlowInfo struct {
+ Flow *openolt.Flow
+ FlowStoreCookie uint64
+ FlowCategory string
+ LogicalFlowID uint64
+}
+
+// OnuGemInfo holds onu information along with gem port list and uni port list
+type OnuGemInfo struct {
+ OnuID uint32
+ SerialNumber string
+ IntfID uint32
+ GemPorts []uint32
+ UniPorts []uint32
+}
+
+// PacketInInfoKey is the key for packet in gemport
+type PacketInInfoKey struct {
+ IntfID uint32
+ OnuID uint32
+ LogicalPort uint32
+ VlanID uint16
+ Priority uint8
+}
+
+// GroupInfo holds group information
+type GroupInfo struct {
+ GroupID uint32
+ OutPorts []uint32
+}
+
+// OpenOltResourceMgr holds resource related information as provided below for each field
+type OpenOltResourceMgr struct {
+ DeviceID string // OLT device id
+ Address string // Host and port of the kv store to connect to
+ Args string // args
+ KVStore *db.Backend // backend kv store connection handle
+ DeviceType string
+ DevInfo *openolt.DeviceInfo // device information
+ ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
+
+ GemPortIDMgmtLock []sync.RWMutex
+ AllocIDMgmtLock []sync.RWMutex
+ OnuIDMgmtLock []sync.RWMutex
+ FlowIDMgmtLock sync.RWMutex
+
+ flowIDToGemInfoLock sync.RWMutex
+}
+
+func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
+ logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
+ switch storeType {
+ case "consul":
+ return kvstore.NewConsulClient(ctx, address, timeout)
+ case "etcd":
+ return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
+ }
+ return nil, errors.New("unsupported-kv-store")
+}
+
+// SetKVClient sets the KV client and return a kv backend
+func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string) *db.Backend {
+ kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
+ if err != nil {
+ logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
+ return nil
+ }
+
+ kvbackend := &db.Backend{
+ Client: kvClient,
+ StoreType: backend,
+ Address: addr,
+ Timeout: KvstoreTimeout,
+ PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
+
+ return kvbackend
+}
+
+// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
+// instances according to technology. Initializes the default resource ranges for all
+// the resources.
+func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
+ var ResourceMgr OpenOltResourceMgr
+ logger.Debugf(ctx, "Init new resource manager , address: %s, device-id: %s", KVStoreAddress, deviceID)
+ ResourceMgr.Address = KVStoreAddress
+ ResourceMgr.DeviceType = deviceType
+ ResourceMgr.DevInfo = devInfo
+ NumPONPorts := devInfo.GetPonPorts()
+
+ Backend := kvStoreType
+ ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID)
+ if ResourceMgr.KVStore == nil {
+ logger.Error(ctx, "Failed to setup KV store")
+ }
+ Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
+ RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
+ ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
+
+ ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
+ ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
+ ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
+
+
+ /*
+ If a legacy driver returns protobuf without any ranges,s synthesize one from
+ the legacy global per-device information. This, in theory, is temporary until
+ the legacy drivers are upgrade to support pool ranges.
+ */
+ if devInfo.Ranges == nil {
+ var ranges openolt.DeviceInfo_DeviceResourceRanges
+ ranges.Technology = devInfo.GetTechnology()
+
+ var index uint32
+ for index = 0; index < NumPONPorts; index++ {
+ ranges.IntfIds = append(ranges.IntfIds, index)
+ }
+
+ var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
+ Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
+ Pool.Start = devInfo.OnuIdStart
+ Pool.End = devInfo.OnuIdEnd
+ Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
+ onuPool := Pool
+ ranges.Pools = append(ranges.Pools, &onuPool)
+
+ Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
+ Pool.Start = devInfo.AllocIdStart
+ Pool.End = devInfo.AllocIdEnd
+ Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
+ allocPool := Pool
+ ranges.Pools = append(ranges.Pools, &allocPool)
+
+ Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
+ Pool.Start = devInfo.GemportIdStart
+ Pool.End = devInfo.GemportIdEnd
+ Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
+ gemPool := Pool
+ ranges.Pools = append(ranges.Pools, &gemPool)
+
+ Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
+ Pool.Start = devInfo.FlowIdStart
+ Pool.End = devInfo.FlowIdEnd
+ Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
+ ranges.Pools = append(ranges.Pools, &Pool)
+ // Add to device info
+ devInfo.Ranges = append(devInfo.Ranges, &ranges)
+ }
+
+ var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
+ var err error
+ for _, TechRange := range devInfo.Ranges {
+ technology := TechRange.Technology
+ logger.Debugf(ctx, "Device info technology %s", technology)
+ Ranges[technology] = TechRange
+
+ RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
+ Backend, ResourceMgr.Address)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
+ return nil
+ }
+ // resource_mgrs_by_tech[technology] = resource_mgr
+ if GlobalPONRsrcMgr == nil {
+ GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
+ }
+ for _, IntfID := range TechRange.IntfIds {
+ ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
+ }
+ // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
+ InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
+ TechRange, devInfo)
+ }
+ for _, PONRMgr := range RsrcMgrsByTech {
+ _ = PONRMgr.InitDeviceResourcePool(ctx)
+ }
+ logger.Info(ctx, "Initialization of resource manager success!")
+ return &ResourceMgr
+}
+
+// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
+// device specific information. If KV doesn't exist
+// or is broader than the device, the device's information will
+// dictate the range limits
+func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
+ techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
+
+
+ logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
+ status := ponRMgr.InitResourceRangesFromKVStore(ctx)
+ if !status {
+ logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
+ }
+
+ /*
+ Then apply device specific information. If KV doesn't exist
+ or is broader than the device, the device's information will
+ dictate the range limits
+ */
+ logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
+
+ ONUIDStart := devInfo.OnuIdStart
+ ONUIDEnd := devInfo.OnuIdEnd
+ ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
+ ONUIDSharedPoolID := uint32(0)
+ AllocIDStart := devInfo.AllocIdStart
+ AllocIDEnd := devInfo.AllocIdEnd
+ AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
+ AllocIDSharedPoolID := uint32(0)
+ GEMPortIDStart := devInfo.GemportIdStart
+ GEMPortIDEnd := devInfo.GemportIdEnd
+ GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
+ GEMPortIDSharedPoolID := uint32(0)
+ FlowIDStart := devInfo.FlowIdStart
+ FlowIDEnd := devInfo.FlowIdEnd
+ FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
+ FlowIDSharedPoolID := uint32(0)
+
+ var FirstIntfPoolID uint32
+ var SharedPoolID uint32
+
+ /*
+ * As a zero check is made against SharedPoolID to check whether the resources are shared across all intfs
+ * if resources are shared across interfaces then SharedPoolID is given a positive number.
+ */
+ for _, FirstIntfPoolID = range techRange.IntfIds {
+ // skip the intf id 0
+ if FirstIntfPoolID == 0 {
+ continue
+ }
+ break
+ }
+
+ for _, RangePool := range techRange.Pools {
+ if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
+ SharedPoolID = FirstIntfPoolID
+ } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
+ SharedPoolID = FirstIntfPoolID
+ } else {
+ SharedPoolID = 0
+ }
+ if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
+ ONUIDStart = RangePool.Start
+ ONUIDEnd = RangePool.End
+ ONUIDShared = RangePool.Sharing
+ ONUIDSharedPoolID = SharedPoolID
+ } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
+ AllocIDStart = RangePool.Start
+ AllocIDEnd = RangePool.End
+ AllocIDShared = RangePool.Sharing
+ AllocIDSharedPoolID = SharedPoolID
+ } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
+ GEMPortIDStart = RangePool.Start
+ GEMPortIDEnd = RangePool.End
+ GEMPortIDShared = RangePool.Sharing
+ GEMPortIDSharedPoolID = SharedPoolID
+ } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
+ FlowIDStart = RangePool.Start
+ FlowIDEnd = RangePool.End
+ FlowIDShared = RangePool.Sharing
+ FlowIDSharedPoolID = SharedPoolID
+ }
+ }
+
+ logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
+ "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
+ "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
+ "alloc_id_shared_pool_id": AllocIDSharedPoolID,
+ "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
+ "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
+ "flow_id_start": FlowIDStart,
+ "flow_id_end_idx": FlowIDEnd,
+ "flow_id_shared_pool_id": FlowIDSharedPoolID,
+ "intf_ids": techRange.IntfIds,
+ "uni_id_start": 0,
+ "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
+ })
+
+ ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
+ AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
+ GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
+ FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
+ devInfo.PonPorts, techRange.IntfIds)
+
+
+ if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
+ globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
+ "", 0, nil)
+ ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
+ "", 0, globalPONRMgr)
+ }
+ if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
+ globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
+ "", 0, nil)
+
+ ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
+ "", 0, globalPONRMgr)
+ }
+ if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
+ globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
+ "", 0, nil)
+ ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
+ "", 0, globalPONRMgr)
+ }
+ if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
+ globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
+ "", 0, nil)
+ ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
+ "", 0, globalPONRMgr)
+ }
+
+ ponRMgr.UpdateRanges(ctx, ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
+}
+
+// Delete clears used resources for the particular olt device being deleted
+func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
+ /* TODO
+ def __del__(self):
+ self.log.info("clearing-device-resource-pool")
+ for key, resource_mgr in self.resource_mgrs.iteritems():
+ resource_mgr.clear_device_resource_pool()
+
+ def assert_pon_id_limit(self, pon_intf_id):
+ assert pon_intf_id in self.resource_mgrs
+
+ def assert_onu_id_limit(self, pon_intf_id, onu_id):
+ self.assert_pon_id_limit(pon_intf_id)
+ self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
+
+ @property
+ def max_uni_id_per_onu(self):
+ return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
+
+ def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
+ self.assert_onu_id_limit(pon_intf_id, onu_id)
+ self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
+ */
+ for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
+ if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
+ logger.Debug(ctx, "Failed to clear device resource pool")
+ return err
+ }
+ }
+ logger.Debug(ctx, "Cleared device resource pool")
+ return nil
+}
+
+// GetONUID returns the available OnuID for the given pon-port
+func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
+ RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
+ defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
+
+ if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
+ err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
+ return 0, err
+ }
+ ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
+ ponrmgr.ONU_ID, 1)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
+ ponIntfID, ponrmgr.ONU_ID)
+ return 0, err
+ }
+ if ONUID != nil {
+ RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
+ return ONUID[0], err
+ }
+
+ return 0, err // return OnuID 0 on error
+}
+
+// GetFlowIDInfo returns the slice of flow info of the given pon-port
+// Note: For flows which trap from the NNI and not really associated with any particular
+// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
+func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
+ var flows []FlowInfo
+
+ FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
+ if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
+ logger.Errorw(ctx, "Error while getting flows from KV store", log.Fields{"flowId": flowID})
+ return nil
+ }
+ if len(flows) == 0 {
+ logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
+ return nil
+ }
+ return &flows
+}
+
+// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
+// Note: For flows which trap from the NNI and not really associated with any particular
+// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
+func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
+
+ FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
+ if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
+ return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
+ }
+ return nil
+}
+
+// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
+// Note: For flows which trap from the NNI and not really associated with any particular
+// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
+func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
+ flowID uint32, flowData *[]FlowInfo) error {
+ FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
+ return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
+}
+
+// GetFlowID return flow ID for a given pon interface id, onu id and uni id
+func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
+ gemportID uint32,
+ flowStoreCookie uint64,
+ flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
+
+ var err error
+ FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
+
+ RsrcMgr.FlowIDMgmtLock.Lock()
+ defer RsrcMgr.FlowIDMgmtLock.Unlock()
+
+ FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
+ if FlowIDs != nil {
+ logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
+ for _, flowID := range FlowIDs {
+ FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
+ er := getFlowIDFromFlowInfo(ctx, FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
+ if er == nil {
+ logger.Debugw(ctx, "Found flowid for the vlan, pcp, and gem",
+ log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
+ return flowID, er
+ }
+ }
+ }
+ logger.Debug(ctx, "No matching flows with flow cookie or flow category, allocating new flowid")
+ FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
+ ponrmgr.FLOW_ID, 1)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
+ ponIntfID, ponrmgr.FLOW_ID)
+ return uint32(0), err
+ }
+ if FlowIDs != nil {
+ _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
+ return FlowIDs[0], err
+ }
+
+ return 0, err
+}
+
+// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
+// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
+// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
+func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
+
+ var err error
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
+
+ RsrcMgr.AllocIDMgmtLock[intfID].Lock()
+ defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
+
+ AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
+ if AllocID != nil {
+ // Since we support only one alloc_id for the ONU at the moment,
+ // return the first alloc_id in the list, if available, for that
+ // ONU.
+ logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
+ return AllocID[0]
+ }
+ AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
+ ponrmgr.ALLOC_ID, 1)
+
+ if AllocID == nil || err != nil {
+ logger.Error(ctx, "Failed to allocate alloc id")
+ return 0
+ }
+ err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
+ if err != nil {
+ logger.Error(ctx, "Failed to update Alloc ID")
+ return 0
+ }
+ logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
+ return AllocID[0]
+}
+
+// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
+func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
+
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
+ return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
+ allocID)
+}
+
+// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
+func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
+ uniID uint32) []uint32 {
+
+ /* Get gem ports for given pon interface , onu id and uni id. */
+
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
+ return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
+}
+
+// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
+func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
+
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
+ AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
+ if AllocID != nil {
+ return AllocID
+ }
+ return []uint32{}
+}
+
+// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
+func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
+ allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
+ for i := 0; i < len(allocIDs); i++ {
+ if allocIDs[i] == allocID {
+ allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
+ break
+ }
+ }
+ err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
+ intfID, onuID, uniID, allocID)
+ }
+}
+
+// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
+func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
+ gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
+ for i := 0; i < len(gemPortIDs); i++ {
+ if gemPortIDs[i] == gemPortID {
+ gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
+ break
+ }
+ }
+ err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
+ intfID, onuID, uniID, gemPortID)
+ }
+}
+
+// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
+// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
+// the packet arrived based on the pon_intf and gemport available in the packet_indication
+func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
+ onuID uint32, uniID uint32) error {
+
+ /* Update onu and uni id associated with the gem port to the kv store. */
+ var IntfGEMPortPath string
+ Data := fmt.Sprintf("%d %d", onuID, uniID)
+ for _, GEM := range gemPorts {
+ IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
+ Val, err := json.Marshal(Data)
+ if err != nil {
+ logger.Error(ctx, "failed to Marshal")
+ return err
+ }
+
+ if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
+ logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
+ return err
+ }
+ }
+ return nil
+}
+
+// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
+func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
+ IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
+ err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
+ }
+}
+
+// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
+// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
+func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
+ uniID uint32, NumOfPorts uint32) ([]uint32, error) {
+
+ /* Get gem port id for a particular pon port, onu id
+ and uni id.
+ */
+
+ var err error
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
+
+ RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
+ defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
+
+ GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
+ if GEMPortList != nil {
+ return GEMPortList, nil
+ }
+
+ GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
+ ponrmgr.GEMPORT_ID, NumOfPorts)
+ if err != nil && GEMPortList == nil {
+ logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
+ return nil, err
+ }
+
+ err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
+ GEMPortList)
+ if err != nil {
+ logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
+ return nil, err
+ }
+ _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
+ onuID, uniID)
+ return GEMPortList, err
+}
+
+// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
+func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
+ uniID uint32, GEMPortList []uint32) error {
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
+ return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
+ GEMPortList)
+
+}
+
+// FreeonuID releases(make free) onu id for a particular pon-port
+func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
+
+ RsrcMgr.OnuIDMgmtLock[intfID].Lock()
+ defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
+
+ RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
+
+ /* Free onu id for a particular interface.*/
+ var IntfonuID string
+ for _, onu := range onuID {
+ IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
+ RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
+ }
+}
+
+// FreeFlowID returns the free flow id for a given interface, onu id and uni id
+func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
+ uniID int32, FlowID uint32) {
+ var IntfONUID string
+ var err error
+
+ RsrcMgr.FlowIDMgmtLock.Lock()
+ defer RsrcMgr.FlowIDMgmtLock.Unlock()
+
+ FlowIds := make([]uint32, 0)
+ FlowIds = append(FlowIds, FlowID)
+ IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
+ err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfONUID})
+ }
+ RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
+
+ RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
+}
+
+// FreeFlowIDs releases the flow Ids
+func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
+ uniID uint32, FlowID []uint32) {
+ RsrcMgr.FlowIDMgmtLock.Lock()
+ defer RsrcMgr.FlowIDMgmtLock.Unlock()
+
+ RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
+
+ var IntfOnuIDUniID string
+ var err error
+ for _, flow := range FlowID {
+ IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
+ err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
+ }
+ RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
+ }
+}
+
+// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
+// for the given OLT device.
+func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
+ uniID uint32, allocID uint32) {
+ RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
+ defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
+
+ RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
+ allocIDs := make([]uint32, 0)
+ allocIDs = append(allocIDs, allocID)
+ RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
+}
+
+// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
+// for the given OLT device.
+func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
+ uniID uint32, gemPortID uint32) {
+ RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
+ defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
+
+ RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
+ gemPortIDs := make([]uint32, 0)
+ gemPortIDs = append(gemPortIDs, gemPortID)
+ RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
+}
+
+// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
+// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
+func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
+
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
+
+ RsrcMgr.AllocIDMgmtLock[intfID].Lock()
+ AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
+
+ RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
+ ponrmgr.ALLOC_ID,
+ AllocIDs)
+ RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
+
+ RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
+ GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
+ RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
+ ponrmgr.GEMPORT_ID,
+ GEMPortIDs)
+ RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
+
+ RsrcMgr.FlowIDMgmtLock.Lock()
+ FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
+ RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
+ ponrmgr.FLOW_ID,
+ FlowIDs)
+ RsrcMgr.FlowIDMgmtLock.Unlock()
+
+ RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
+ for _, GEM := range GEMPortIDs {
+ _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
+ }
+}
+
+// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
+// Returns true if the flow cookie is found, otherwise it returns false
+func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
+ flowStoreCookie uint64) bool {
+
+ FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
+ FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
+ if FlowIDs != nil {
+ logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
+ for _, flowID := range FlowIDs {
+ FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
+ if FlowInfo != nil {
+ logger.Debugw(ctx, "Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
+ for _, Info := range *FlowInfo {
+ if Info.FlowStoreCookie == flowStoreCookie {
+ logger.Debug(ctx, "Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
+ return true
+ }
+ }
+ }
+ }
+ }
+ return false
+}
+
+// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
+// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
+func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
+ Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
+ var Data []uint32
+ Value, err := RsrcMgr.KVStore.Get(ctx, Path)
+ if err == nil {
+ if Value != nil {
+ Val, err := kvstore.ToByte(Value.Value)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
+ return Data
+ }
+ if err = json.Unmarshal(Val, &Data); err != nil {
+ logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
+ return Data
+ }
+ }
+ } else {
+ logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
+ }
+ logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
+ return Data
+
+}
+
+// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
+// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
+func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
+ IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
+ if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
+ logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
+ return err
+ }
+ return nil
+}
+
+// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
+// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
+func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
+ tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
+ for i, tpIDInList := range tpIDList {
+ if tpIDInList == TpID {
+ tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
+ }
+ }
+ IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
+ Value, err := json.Marshal(tpIDList)
+ if err != nil {
+ logger.Error(ctx, "failed to Marshal")
+ return err
+ }
+ if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
+ logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
+ return err
+ }
+ return err
+}
+
+// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
+// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
+func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
+ UniID uint32, TpID uint32) error {
+ var Value []byte
+ var err error
+
+ IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
+
+ tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
+ for _, value := range tpIDList {
+ if value == TpID {
+ logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
+ return err
+ }
+ }
+ logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
+ tpIDList = append(tpIDList, TpID)
+ Value, err = json.Marshal(tpIDList)
+ if err != nil {
+ logger.Error(ctx, "failed to Marshal")
+ return err
+ }
+ if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
+ logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
+ return err
+ }
+ return err
+}
+
+// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
+// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
+func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
+ UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
+ var Value []byte
+ var err error
+
+ IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
+ Value, err = json.Marshal(*MeterConfig)
+ if err != nil {
+ logger.Error(ctx, "failed to Marshal meter config")
+ return err
+ }
+ if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
+ logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
+ return err
+ }
+ return err
+}
+
+// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
+// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
+func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
+ UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
+ Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
+ var meterConfig ofp.OfpMeterConfig
+ Value, err := RsrcMgr.KVStore.Get(ctx, Path)
+ if err == nil {
+ if Value != nil {
+ logger.Debug(ctx, "Found meter in KV store", log.Fields{"Direction": Direction})
+ Val, er := kvstore.ToByte(Value.Value)
+ if er != nil {
+ logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
+ return nil, er
+ }
+ if er = json.Unmarshal(Val, &meterConfig); er != nil {
+ logger.Error(ctx, "Failed to unmarshal meterconfig", log.Fields{"error": er})
+ return nil, er
+ }
+ } else {
+ logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
+ return nil, err
+ }
+ } else {
+ logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
+
+ }
+ return &meterConfig, err
+}
+
+// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
+// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
+func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
+ UniID uint32, TpID uint32) error {
+ Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
+ if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
+ logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
+ return err
+ }
+ return nil
+}
+
+func getFlowIDFromFlowInfo(ctx context.Context, FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
+ vlanVid uint32, vlanPcp ...uint32) error {
+ if FlowInfo != nil {
+ for _, Info := range *FlowInfo {
+ if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
+ logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
+ if Info.FlowCategory == "HSIA_FLOW" {
+ if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
+ return nil
+ }
+ }
+ }
+ if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
+ if flowCategory != "" && Info.FlowCategory == flowCategory {
+ logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
+ return nil
+ }
+ }
+ }
+ }
+ logger.Debugw(ctx, "the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
+ return errors.New("invalid flow-info")
+}
+
+func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
+ if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
+ return err
+ }
+
+ if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
+ if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
+ return nil
+ }
+ } else if vlanPcp == Info.Flow.Classifier.OPbits {
+ //no remark action but flow has pbits
+ return nil
+ } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
+ // no pbit found
+ return nil
+ }
+ return errors.New("not found in terms of pbit equality")
+}
+
+func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
+ if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
+ return nil
+ }
+ return errors.New("not found in terms of vlan_id equality")
+}
+
+//AddGemToOnuGemInfo adds gemport to onugem info kvstore
+func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
+ var onuGemData []OnuGemInfo
+ var err error
+
+ if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
+ logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
+ return err
+ }
+ if len(onuGemData) == 0 {
+ logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
+ return err
+ }
+
+ for idx, onugem := range onuGemData {
+ if onugem.OnuID == onuID {
+ for _, gem := range onuGemData[idx].GemPorts {
+ if gem == gemPort {
+ logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
+ return nil
+ }
+ }
+ logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
+ onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
+ break
+ }
+ }
+ err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
+ if err != nil {
+ logger.Error(ctx, "Failed to add onugem to kv store")
+ return err
+ }
+ return err
+}
+
+//GetOnuGemInfo gets onu gem info from the kvstore per interface
+func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
+ var onuGemData []OnuGemInfo
+
+ if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
+ logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
+ return nil, err
+ }
+
+ return onuGemData, nil
+}
+
+// AddOnuGemInfo adds onu info on to the kvstore per interface
+func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
+ var onuGemData []OnuGemInfo
+ var err error
+
+ if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
+ logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
+ return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
+ log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
+ }
+ onuGemData = append(onuGemData, onuGem)
+ err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
+ if err != nil {
+ logger.Error(ctx, "Failed to add onugem to kv store")
+ return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
+ log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
+ }
+
+ logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
+ return nil
+}
+
+// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
+func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
+ var onuGemData []OnuGemInfo
+ var err error
+
+ if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
+ logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
+ return
+ }
+ for idx, onu := range onuGemData {
+ if onu.OnuID == onuID {
+ for _, uni := range onu.UniPorts {
+ if uni == portNo {
+ logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
+ return
+ }
+ }
+ onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
+ break
+ }
+ }
+ err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
+ return
+ }
+}
+
+//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
+func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
+
+ path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
+ Value, err := json.Marshal(gemPort)
+ if err != nil {
+ logger.Error(ctx, "Failed to marshal data")
+ return
+ }
+ if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
+ logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
+ return
+ }
+ logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
+}
+
+// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
+func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
+
+ var Val []byte
+ var gemPort uint32
+
+ path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
+ packetInInfoKey.VlanID, packetInInfoKey.Priority)
+
+ value, err := RsrcMgr.KVStore.Get(ctx, path)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
+ return uint32(0), err
+ } else if value == nil {
+ logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
+ return uint32(0), nil
+ }
+
+ if Val, err = kvstore.ToByte(value.Value); err != nil {
+ logger.Error(ctx, "Failed to convert to byte array")
+ return uint32(0), err
+ }
+ if err = json.Unmarshal(Val, &gemPort); err != nil {
+ logger.Error(ctx, "Failed to unmarshall")
+ return uint32(0), err
+ }
+ logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
+
+ return gemPort, nil
+}
+
+//DelGemPortPktInOfAllServices deletes the gemports from pkt in path for all services
+func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktInOfAllServices(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
+
+ Path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
+ logger.Debugf(ctx, "getting flows from the path:%s", Path)
+ Value, err := RsrcMgr.KVStore.List(ctx, Path)
+ if err != nil {
+ logger.Errorf(ctx, "failed to get flows from kvstore for path %s", Path)
+ return errors.New("failed to get flows from kvstore for path " + Path)
+ }
+ logger.Debugf(ctx, "%d flows retrieved from the path:%s", len(Value), Path)
+
+ for key := range Value {
+ if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
+ logger.Errorf(ctx, "Falied to remove resource %s", key)
+ return err
+ }
+ }
+ return nil
+}
+
+// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
+func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
+ if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
+ logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
+ return err
+ }
+ return nil
+}
+
+//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
+func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
+
+ var nni []uint32
+ var Val []byte
+
+ path := NnniIntfID
+ value, err := RsrcMgr.KVStore.Get(ctx, path)
+ if err != nil {
+ logger.Error(ctx, "failed to get data from kv store")
+ return nil, err
+ }
+ if value != nil {
+ if Val, err = kvstore.ToByte(value.Value); err != nil {
+ logger.Error(ctx, "Failed to convert to byte array")
+ return nil, err
+ }
+ if err = json.Unmarshal(Val, &nni); err != nil {
+ logger.Error(ctx, "Failed to unmarshall")
+ return nil, err
+ }
+ }
+ return nni, err
+}
+
+// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
+func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
+ var Value []byte
+
+ nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
+ if err != nil {
+ logger.Error(ctx, "failed to fetch nni interfaces from kv store")
+ return err
+ }
+
+ path := NnniIntfID
+ nni = append(nni, nniIntf)
+ Value, err = json.Marshal(nni)
+ if err != nil {
+ logger.Error(ctx, "Failed to marshal data")
+ }
+ if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
+ logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
+ return err
+ }
+ logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
+ return nil
+}
+
+// DelNNiFromKVStore deletes nni interface list from kv store.
+func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
+
+ path := NnniIntfID
+
+ if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
+ logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
+ return err
+ }
+ return nil
+}
+
+//UpdateFlowIDsForGem updates flow id per gemport
+func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
+ var val []byte
+ path := fmt.Sprintf(FlowIDsForGem, intf)
+
+ flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
+ if err != nil {
+ logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
+ return err
+ }
+ if flowsForGem == nil {
+ flowsForGem = make(map[uint32][]uint32)
+ }
+ flowsForGem[gem] = flowIDs
+ val, err = json.Marshal(flowsForGem)
+ if err != nil {
+ logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
+ return err
+ }
+
+ RsrcMgr.flowIDToGemInfoLock.Lock()
+ defer RsrcMgr.flowIDToGemInfoLock.Unlock()
+ if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
+ logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
+ return err
+ }
+ logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
+ return nil
+}
+
+//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
+func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
+ path := fmt.Sprintf(FlowIDsForGem, intf)
+ var val []byte
+
+ flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
+ if err != nil {
+ logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
+ return
+ }
+ if flowsForGem == nil {
+ logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
+ return
+ }
+ delete(flowsForGem, gem)
+ val, err = json.Marshal(flowsForGem)
+ if err != nil {
+ logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
+ return
+ }
+
+ RsrcMgr.flowIDToGemInfoLock.Lock()
+ defer RsrcMgr.flowIDToGemInfoLock.Unlock()
+ if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
+ logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
+ }
+}
+
+//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
+func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
+ path := fmt.Sprintf(FlowIDsForGem, intf)
+ var flowsForGem map[uint32][]uint32
+ var val []byte
+ RsrcMgr.flowIDToGemInfoLock.RLock()
+ value, err := RsrcMgr.KVStore.Get(ctx, path)
+ RsrcMgr.flowIDToGemInfoLock.RUnlock()
+ if err != nil {
+ logger.Error(ctx, "failed to get data from kv store")
+ return nil, err
+ }
+ if value != nil && value.Value != nil {
+ if val, err = kvstore.ToByte(value.Value); err != nil {
+ logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
+ return nil, err
+ }
+ if err = json.Unmarshal(val, &flowsForGem); err != nil {
+ logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
+ return nil, err
+ }
+ }
+ return flowsForGem, nil
+}
+
+//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
+func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
+ path := fmt.Sprintf(FlowIDsForGem, intf)
+ RsrcMgr.flowIDToGemInfoLock.Lock()
+ defer RsrcMgr.flowIDToGemInfoLock.Unlock()
+ if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
+ logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
+ }
+}
+
+// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
+func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
+ IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
+ RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
+}
+
+//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
+func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
+ path := McastQueuesForIntf
+ var mcastQueueToIntfMap map[uint32][]uint32
+ var val []byte
+
+ kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
+ if err != nil {
+ logger.Error(ctx, "failed to get data from kv store")
+ return nil, err
+ }
+ if kvPair != nil && kvPair.Value != nil {
+ if val, err = kvstore.ToByte(kvPair.Value); err != nil {
+ logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
+ return nil, err
+ }
+ if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
+ logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
+ return nil, err
+ }
+ }
+ return mcastQueueToIntfMap, nil
+}
+
+//AddMcastQueueForIntf adds multicast queue for pon interface
+func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
+ var val []byte
+ path := McastQueuesForIntf
+
+ mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
+ return err
+ }
+ if mcastQueues == nil {
+ mcastQueues = make(map[uint32][]uint32)
+ }
+ mcastQueues[intf] = []uint32{gem, servicePriority}
+ if val, err = json.Marshal(mcastQueues); err != nil {
+ logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
+ return err
+ }
+ if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
+ logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
+ return err
+ }
+ logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
+ return nil
+}
+
+//AddFlowGroupToKVStore adds flow group into KV store
+func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
+ var Value []byte
+ var err error
+ var path string
+ if cached {
+ path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
+ } else {
+ path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
+ }
+ var outPorts []uint32
+ for _, ofBucket := range groupEntry.Desc.Buckets {
+ for _, ofAction := range ofBucket.Actions {
+ if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
+ outPorts = append(outPorts, ofAction.GetOutput().Port)
+ }
+ }
+ }
+ groupInfo := GroupInfo{
+ GroupID: groupEntry.Desc.GroupId,
+ OutPorts: outPorts,
+ }
+
+ Value, err = json.Marshal(groupInfo)
+
+ if err != nil {
+ logger.Error(ctx, "failed to Marshal flow group object")
+ return err
+ }
+
+ if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
+ logger.Errorf(ctx, "Failed to update resource %s", path)
+ return err
+ }
+ return nil
+}
+
+//RemoveFlowGroupFromKVStore removes flow group from KV store
+func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
+ var path string
+ if cached {
+ path = fmt.Sprintf(FlowGroupCached, groupID)
+ } else {
+ path = fmt.Sprintf(FlowGroup, groupID)
+ }
+ if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
+ logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
+ return err
+ }
+ return nil
+}
+
+//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
+//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
+// Returns (false, {}, nil) if the group does not exists in the KV store.
+func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
+ var groupInfo GroupInfo
+ var path string
+ if cached {
+ path = fmt.Sprintf(FlowGroupCached, groupID)
+ } else {
+ path = fmt.Sprintf(FlowGroup, groupID)
+ }
+ kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
+ if err != nil {
+ return false, groupInfo, err
+ }
+ if kvPair != nil && kvPair.Value != nil {
+ Val, err := kvstore.ToByte(kvPair.Value)
+ if err != nil {
+ logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
+ return false, groupInfo, err
+ }
+ if err = json.Unmarshal(Val, &groupInfo); err != nil {
+ logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
+ return false, groupInfo, err
+ }
+ return true, groupInfo, nil
+ }
+ return false, groupInfo, nil
+}
diff --git a/internal/pkg/resourcemanager/resourcemanager_test.go b/internal/pkg/resourcemanager/resourcemanager_test.go
new file mode 100644
index 0000000..81e0fa4
--- /dev/null
+++ b/internal/pkg/resourcemanager/resourcemanager_test.go
@@ -0,0 +1,1188 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+This file contains unit test cases for functions in the file resourcemanager.go.
+This file also implements the Client interface to mock the kv-client, fields struct to mock OpenOltResourceMgr
+and few utility functions.
+*/
+
+//Package adaptercore provides the utility for olt devices, flows and statistics
+package resourcemanager
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "github.com/opencord/voltha-lib-go/v3/pkg/db"
+ "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
+ fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
+ "github.com/opencord/voltha-lib-go/v3/pkg/log"
+ ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
+ ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
+ "github.com/opencord/voltha-protos/v3/go/openolt"
+ "reflect"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+)
+
+func init() {
+ _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
+}
+
+const (
+ MeterConfig = "meter_id"
+ FlowIDInfo = "flow_id_info"
+ FlowIDs = "flow_ids"
+ GemportIDs = "gemport_ids"
+ AllocIDs = "alloc_ids"
+ GemportIDPool = "gemport_id_pool"
+ AllocIDPool = "alloc_id_pool"
+ FlowIDpool = "flow_id_pool"
+)
+
+// fields mocks OpenOltResourceMgr struct.
+type fields struct {
+ DeviceID string
+ Address string
+ Args string
+ KVStore *db.Backend
+ DeviceType string
+ DevInfo *openolt.DeviceInfo
+ ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
+ NumOfPonPorts uint32
+}
+
+// MockKVClient mocks the AdapterProxy interface.
+type MockResKVClient struct {
+}
+
+// getResMgr mocks OpenOltResourceMgr struct.
+func getResMgr() *fields {
+ var resMgr fields
+ resMgr.KVStore = &db.Backend{
+ Client: &MockResKVClient{},
+ }
+ resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
+ ranges := make(map[string]interface{})
+ sharedIdxByType := make(map[string]string)
+ sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
+ sharedIdxByType["ONU_ID"] = "ONU_ID"
+ sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
+ sharedIdxByType["FLOW_ID"] = "FLOW_ID"
+ ranges["ONU_ID"] = uint32(0)
+ ranges["GEMPORT_ID"] = uint32(0)
+ ranges["ALLOC_ID"] = uint32(0)
+ ranges["FLOW_ID"] = uint32(0)
+ ranges["onu_id_shared"] = uint32(0)
+ ranges["alloc_id_shared"] = uint32(0)
+ ranges["gemport_id_shared"] = uint32(0)
+ ranges["flow_id_shared"] = uint32(0)
+ resMgr.NumOfPonPorts = 2
+ ponMgr := &ponrmgr.PONResourceManager{
+ DeviceID: "onu-1",
+ IntfIDs: []uint32{1, 2},
+ KVStore: &db.Backend{
+ Client: &MockResKVClient{},
+ },
+ PonResourceRanges: ranges,
+ SharedIdxByType: sharedIdxByType,
+ }
+ resMgr.ResourceMgrs[1] = ponMgr
+ resMgr.ResourceMgrs[2] = ponMgr
+
+ return &resMgr
+}
+
+// List function implemented for KVClient.
+func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
+ return nil, errors.New("key didn't find")
+}
+
+// Get mock function implementation for KVClient
+func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
+ logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
+ if key != "" {
+ if strings.Contains(key, MeterConfig) {
+ var bands []*ofp.OfpMeterBandHeader
+ bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
+ Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
+
+ bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
+ Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
+
+ sep := strings.Split(key, "/")[1]
+ val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
+ if uint32(val) > 1 {
+ meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
+ str, _ := json.Marshal(meterConfig)
+
+ return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
+ }
+ return nil, errors.New("invalid meter")
+ }
+ if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
+ logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
+ data := make(map[string]interface{})
+ data["pool"] = "1024"
+ data["start_idx"] = 1
+ data["end_idx"] = 1024
+ str, _ := json.Marshal(data)
+ return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
+ }
+ if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
+ logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
+ str, _ := json.Marshal([]uint32{1, 2})
+ return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
+ }
+ if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
+ logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
+ str, _ := json.Marshal(1)
+ return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
+ }
+ if strings.Contains(key, McastQueuesForIntf) {
+ logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
+ mcastQueues := make(map[uint32][]uint32)
+ mcastQueues[10] = []uint32{4000, 0}
+ str, _ := json.Marshal(mcastQueues)
+ return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
+ }
+ if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
+ groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
+ str, _ := json.Marshal(groupInfo)
+ return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
+ }
+
+ maps := make(map[string]*kvstore.KVPair)
+ maps[key] = &kvstore.KVPair{Key: key}
+ return maps[key], nil
+ }
+ return nil, errors.New("key didn't find")
+}
+
+// Put mock function implementation for KVClient
+func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
+ if key != "" {
+ return nil
+ }
+ return errors.New("key didn't find")
+}
+
+// Delete mock function implementation for KVClient
+func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
+ return nil
+}
+
+// Reserve mock function implementation for KVClient
+func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
+ return nil, errors.New("key didn't find")
+}
+
+// ReleaseReservation mock function implementation for KVClient
+func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
+ return nil
+}
+
+// ReleaseAllReservations mock function implementation for KVClient
+func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
+ return nil
+}
+
+// RenewReservation mock function implementation for KVClient
+func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
+ return nil
+}
+
+// Watch mock function implementation for KVClient
+func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
+ return nil
+}
+
+// AcquireLock mock function implementation for KVClient
+func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
+ return nil
+}
+
+// ReleaseLock mock function implementation for KVClient
+func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
+ return nil
+}
+
+// IsConnectionUp mock function implementation for KVClient
+func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
+ return true
+}
+
+// CloseWatch mock function implementation for KVClient
+func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
+}
+
+// Close mock function implementation for KVClient
+func (kvclient *MockResKVClient) Close(ctx context.Context) {
+}
+
+// testResMgrObject maps fields type to OpenOltResourceMgr type.
+func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
+ var rsrMgr = OpenOltResourceMgr{
+ DeviceID: testResMgr.DeviceID,
+ Args: testResMgr.Args,
+ KVStore: testResMgr.KVStore,
+ DeviceType: testResMgr.DeviceType,
+ Address: testResMgr.Address,
+ DevInfo: testResMgr.DevInfo,
+ ResourceMgrs: testResMgr.ResourceMgrs,
+ }
+
+ rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
+ rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
+ rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
+
+ return &rsrMgr
+}
+
+func TestNewResourceMgr(t *testing.T) {
+ type args struct {
+ deviceID string
+ KVStoreAddress string
+ kvStoreType string
+ deviceType string
+ devInfo *openolt.DeviceInfo
+ }
+ tests := []struct {
+ name string
+ args args
+ want *OpenOltResourceMgr
+ }{
+ {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
+ "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
+ {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
+ "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := NewResourceMgr(ctx, tt.args.deviceID, tt.args.KVStoreAddress, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_Delete(t *testing.T) {
+ tests := []struct {
+ name string
+ fields *fields
+ wantErr error
+ }{
+ {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) {
+ type args struct {
+ IntfID uint32
+ onuID int32
+ uniID int32
+ FlowID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ }{
+ {"FreeFlowID-1", getResMgr(), args{1, 2, 2, 2}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ RsrcMgr.FreeFlowID(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) {
+
+ type args struct {
+ IntfID uint32
+ onuID uint32
+ uniID uint32
+ FlowID []uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ }{
+ {"FreeFlowIDs-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ RsrcMgr.FreeFlowIDs(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
+ type args struct {
+ intfID uint32
+ onuID uint32
+ uniID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ }{
+ {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
+ type args struct {
+ intfID uint32
+ onuID []uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ }{
+ {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
+
+ type args struct {
+ intfID uint32
+ onuID uint32
+ uniID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want uint32
+ }{
+ {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := RsrcMgr.GetAllocID(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
+ type args struct {
+ intfID uint32
+ onuID uint32
+ uniID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want []uint32
+ }{
+ {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
+
+ type args struct {
+ PONIntfID uint32
+ ONUID int32
+ UNIID int32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want []uint32
+ }{
+ {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
+ type args struct {
+ intfID uint32
+ onuID uint32
+ uniID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want []uint32
+ }{
+ {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetFlowID(t *testing.T) {
+
+ type args struct {
+ ponIntfID uint32
+ ONUID int32
+ uniID int32
+ gemportID uint32
+ flowStoreCookie uint64
+ flowCategory string
+ vlanVid uint32
+ vlanPcp []uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want uint32
+ wantErr error
+ }{
+ {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2,
+ "HSIA", 33, nil}, 0, errors.New("failed to get flows")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ got, err := RsrcMgr.GetFlowID(ctx, tt.args.ponIntfID, tt.args.ONUID, tt.args.uniID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...)
+ if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
+ type args struct {
+ ponPort uint32
+ onuID uint32
+ uniID uint32
+ NumOfPorts uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want []uint32
+ wantErr error
+ }{
+ {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
+ errors.New("failed to get gem port")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
+ if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
+ t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
+ type args struct {
+ Direction string
+ IntfID uint32
+ OnuID uint32
+ UniID uint32
+ tpID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want *ofp.OfpMeterConfig
+ wantErr error
+ }{
+ {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
+ &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
+ {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
+ &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ got, err := RsrcMgr.GetMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
+ if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
+ t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
+ type args struct {
+ ponIntfID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want uint32
+ wantErr error
+ }{
+ {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
+ if got != tt.want && err != nil {
+ t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
+
+ type args struct {
+ IntfID uint32
+ OnuID uint32
+ UniID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want []uint32
+ }{
+ {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
+ []uint32{1}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
+ type args struct {
+ ponIntfID uint32
+ onuID int32
+ uniID int32
+ flowStoreCookie uint64
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ want bool
+ }{
+ {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if got := RsrcMgr.IsFlowCookieOnKVStore(ctx, tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowStoreCookie); got != tt.want {
+ t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
+
+ type args struct {
+ Direction string
+ IntfID uint32
+ OnuID uint32
+ UniID uint32
+ tpID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
+ errors.New("failed to delete meter id %s from kvstore")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
+ tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
+ t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
+ type args struct {
+ IntfID uint32
+ OnuID uint32
+ UniID uint32
+ tpID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
+ errors.New("failed to delete techprofile id resource %s in KV store")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
+ tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
+ t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
+ type args struct {
+ ponPort uint32
+ onuID uint32
+ uniID uint32
+ allocID []uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
+ errors.New("")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.UpdateAllocIdsForOnu(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.allocID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
+ type args struct {
+ ponIntfID int32
+ onuID int32
+ uniID int32
+ flowID uint32
+ flowData *[]FlowInfo
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.UpdateFlowIDInfo(ctx, tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowID, tt.args.flowData); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
+
+ type args struct {
+ ponPort uint32
+ onuID uint32
+ uniID uint32
+ GEMPortList []uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
+ []uint32{1, 2}}, errors.New("failed to update resource")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.GEMPortList); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
+ type args struct {
+ gemPorts []uint32
+ PonPort uint32
+ onuID uint32
+ uniID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
+ 1, 2, 2}, errors.New("failed to update resource")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
+ tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
+ type args struct {
+ Direction string
+ IntfID uint32
+ OnuID uint32
+ UniID uint32
+ tpID uint32
+ MeterConfig *ofp.OfpMeterConfig
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
+ 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
+ tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
+ t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
+ type args struct {
+ IntfID uint32
+ OnuID uint32
+ UniID uint32
+ TpID uint32
+ }
+ tests := []struct {
+ name string
+ fields *fields
+ args args
+ wantErr error
+ }{
+ {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
+ 2}, errors.New("failed to update resource")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := RsrcMgr.UpdateTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.TpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
+ t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestSetKVClient(t *testing.T) {
+ type args struct {
+ backend string
+ address string
+ DeviceID string
+ }
+ tests := []struct {
+ name string
+ args args
+ want *db.Backend
+ }{
+ {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1"}, &db.Backend{}},
+ {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2"}, &db.Backend{}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := SetKVClient(context.Background(), tt.args.backend, tt.args.address, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func Test_getFlowIDFromFlowInfo(t *testing.T) {
+ type args struct {
+ FlowInfo *[]FlowInfo
+ flowID uint32
+ gemportID uint32
+ flowStoreCookie uint64
+ flowCategory string
+ vlanVid uint32
+ vlanPcp []uint32
+ }
+ flowInfo := &[]FlowInfo{
+ {
+ &openolt.Flow{
+ FlowId: 1,
+ GemportId: 1,
+ Classifier: &openolt.Classifier{
+ OPbits: 1,
+ OVid: 33,
+ },
+ Action: &openolt.Action{
+ Cmd: &openolt.ActionCmd{
+ AddOuterTag: true,
+ },
+ OVid: 7,
+ },
+ },
+ 1,
+ "HSIA_FLOW",
+ 2000,
+ },
+ {
+ &openolt.Flow{
+ GemportId: 1,
+ Classifier: &openolt.Classifier{
+ OVid: 0,
+ },
+ Action: &openolt.Action{
+ Cmd: &openolt.ActionCmd{
+ TrapToHost: true,
+ },
+ },
+ },
+ 1,
+ "EAPOL",
+ 3000,
+ },
+ }
+ tests := []struct {
+ name string
+ args args
+ wantErr error
+ }{
+ {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
+ {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
+ "HSIA_FLOW", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
+ {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
+ "EAPOL", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := getFlowIDFromFlowInfo(context.Background(), tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...)
+ if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
+ t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ if err == nil {
+ t.Log("return'd nil")
+ }
+ })
+ }
+}
+
+func Test_newKVClient(t *testing.T) {
+ type args struct {
+ storeType string
+ address string
+ timeout time.Duration
+ }
+ var kvClient kvstore.Client
+ tests := []struct {
+ name string
+ args args
+ want kvstore.Client
+ wantErr error
+ }{
+ {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
+ if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
+ t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
+ }
+ if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
+ t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
+ type args struct {
+ intf uint32
+ gem uint32
+ servicePriority uint32
+ }
+ tests := []struct {
+ name string
+ args args
+ fields *fields
+ }{
+ {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
+ {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
+ {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
+ if err != nil {
+ t.Errorf("%s got err= %s wants nil", tt.name, err)
+ return
+ }
+ })
+ }
+}
+
+func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
+ groupDesc := ofp.OfpGroupDesc{
+ Type: ofp.OfpGroupType_OFPGT_ALL,
+ GroupId: groupID,
+ }
+ groupEntry := ofp.OfpGroupEntry{
+ Desc: &groupDesc,
+ }
+ for i := 0; i < len(outPorts); i++ {
+ var acts []*ofp.OfpAction
+ acts = append(acts, fu.Output(outPorts[i]))
+ bucket := ofp.OfpBucket{
+ Actions: acts,
+ }
+ groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
+ }
+ return &groupEntry
+}
+
+func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
+ type args struct {
+ group *ofp.OfpGroupEntry
+ cached bool
+ }
+ group1 := newGroup(1, []uint32{1})
+ group2 := newGroup(2, []uint32{2})
+ tests := []struct {
+ name string
+ args args
+ fields *fields
+ }{
+ {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
+ {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
+ if err != nil {
+ t.Errorf("%s got err= %s wants nil", tt.name, err)
+ return
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
+ type args struct {
+ groupID uint32
+ cached bool
+ }
+ tests := []struct {
+ name string
+ args args
+ fields *fields
+ }{
+ {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
+ {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
+ if err != nil {
+ t.Errorf("%s got false but wants true", tt.name)
+ return
+ }
+ })
+ }
+}
+
+func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
+ type args struct {
+ groupID uint32
+ cached bool
+ }
+ tests := []struct {
+ name string
+ args args
+ fields *fields
+ }{
+ {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
+ {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
+ {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ RsrcMgr := testResMgrObject(tt.fields)
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
+ if err != nil {
+ t.Errorf("%s got error but wants nil error", tt.name)
+ return
+ } else if exists && (groupInfo.GroupID == 0) {
+ t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
+ return
+ } else if tt.args.groupID == 3 && exists {
+ t.Errorf("%s got true but wants false", tt.name)
+ return
+ }
+ })
+ }
+}