blob: bfadf5f9d138f1694b9b3adee8b10ede5ff2ee8a [file] [log] [blame]
Girish Gowdra64503432020-01-07 10:59:10 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//Package resourcemanager provides the utility for managing resources
18package core
19
20import (
Girish Gowdra390f12f2021-07-01 15:53:49 -070021 "github.com/opencord/voltha-lib-go/v7/pkg/log"
22 ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager"
23 "github.com/opencord/voltha-lib-go/v7/pkg/techprofile"
24 "github.com/opencord/voltha-protos/v5/go/openolt"
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030025 "golang.org/x/net/context"
Girish Gowdra390f12f2021-07-01 15:53:49 -070026 "sync"
27)
28
29const (
30 basePathKvStore = "service/voltha"
Girish Gowdra64503432020-01-07 10:59:10 +053031)
32
Girish Gowdra64503432020-01-07 10:59:10 +053033// OpenOltResourceMgr holds resource related information as provided below for each field
34type OpenOltResourceMgr struct {
35 deviceInfo *openolt.DeviceInfo
Girish Gowdraaeceb842020-08-21 12:10:39 -070036
Girish Gowdra390f12f2021-07-01 15:53:49 -070037 intfID uint32 // pon interface id
Girish Gowdraaeceb842020-08-21 12:10:39 -070038
Girish Gowdra64503432020-01-07 10:59:10 +053039 // array of pon resource managers per interface technology
Girish Gowdra390f12f2021-07-01 15:53:49 -070040 PonRsrMgr *ponrmgr.PONResourceManager
41 TechprofileRef techprofile.TechProfileIf
42
43 FlowIDMgmtLock sync.RWMutex
Girish Gowdra5d7d6442020-09-08 17:03:11 -070044
45 flow_id uint64
Girish Gowdra64503432020-01-07 10:59:10 +053046}
47
48// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
49// instances according to technology. Initializes the default resource ranges for all
50// the resources.
Girish Gowdra390f12f2021-07-01 15:53:49 -070051func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, ponID uint32, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdra64503432020-01-07 10:59:10 +053052 var ResourceMgr OpenOltResourceMgr
Girish Gowdra5d7d6442020-09-08 17:03:11 -070053 logger.Debugf(nil, "Init new resource manager")
Girish Gowdra64503432020-01-07 10:59:10 +053054
55 ResourceMgr.deviceInfo = devInfo
Girish Gowdra390f12f2021-07-01 15:53:49 -070056 ResourceMgr.intfID = ponID
57 ctx := context.Background()
58 newCtx := context.WithValue(ctx, "ponIf", ponID)
Girish Gowdra64503432020-01-07 10:59:10 +053059 // Create a separate Resource Manager instance for each range. This assumes that
60 // each technology is represented by only a single range
Girish Gowdra64503432020-01-07 10:59:10 +053061 for _, TechRange := range devInfo.Ranges {
Girish Gowdra390f12f2021-07-01 15:53:49 -070062 for _, intfID := range TechRange.IntfIds {
63 if intfID == ponID {
64 technology := TechRange.Technology
65 logger.Debugf(context.Background(), "Device info technology %s, intf-id %v", technology, ponID)
66 rsrMgr, err := ponrmgr.NewPONResourceManager(newCtx, technology, deviceType, deviceID,
67 kvStoreType, KVStoreHostPort, basePathKvStore)
68 if err != nil {
69 logger.Errorf(context.Background(), "Failed to create pon resource manager instance for technology %s", technology)
70 return nil
71 }
72 ResourceMgr.PonRsrMgr = rsrMgr
73 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
74 InitializeDeviceResourceRangeAndPool(rsrMgr, TechRange, devInfo)
75 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(context.Background(), intfID); err != nil {
76 logger.Fatal(context.Background(), "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.intfID)
77 return nil
78 }
79 }
Girish Gowdra64503432020-01-07 10:59:10 +053080 }
Girish Gowdra64503432020-01-07 10:59:10 +053081 }
Girish Gowdra390f12f2021-07-01 15:53:49 -070082 var err error
83 ResourceMgr.TechprofileRef, err = techprofile.NewTechProfile(newCtx, ResourceMgr.PonRsrMgr, ResourceMgr.PonRsrMgr.Backend,
84 ResourceMgr.PonRsrMgr.Address, basePathKvStore)
85 if err != nil || ResourceMgr.TechprofileRef == nil {
86 logger.Errorw(nil, "failed-to-allocate-to-techprofile-for-pon-port", log.Fields{"intfID": ponID, "err": err})
87 return nil
Girish Gowdra64503432020-01-07 10:59:10 +053088 }
Girish Gowdra390f12f2021-07-01 15:53:49 -070089
Girish Gowdra5d7d6442020-09-08 17:03:11 -070090 logger.Info(nil, "Initialization of resource manager success!")
Girish Gowdra64503432020-01-07 10:59:10 +053091 return &ResourceMgr
92}
93
94// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
95// device specific information. If KV doesn't exist
96// or is broader than the device, the device's information will
97// dictate the range limits
Girish Gowdra390f12f2021-07-01 15:53:49 -070098func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdra64503432020-01-07 10:59:10 +053099 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra390f12f2021-07-01 15:53:49 -0700100 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
101 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
102 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
103
104 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
105 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
106 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
107 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
108 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Girish Gowdra64503432020-01-07 10:59:10 +0530109
110 // init the resource range pool according to the sharing type
Girish Gowdra390f12f2021-07-01 15:53:49 -0700111 logger.Debugw(nil, "Device info init", log.Fields{"technology": techRange.Technology,
112 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
113 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
114 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
115 "intf_ids": techRange.IntfIds,
116 })
Girish Gowdra64503432020-01-07 10:59:10 +0530117 for _, RangePool := range techRange.Pools {
Girish Gowdra390f12f2021-07-01 15:53:49 -0700118 // FIXME: Remove hardcoding
Girish Gowdra64503432020-01-07 10:59:10 +0530119 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
120 ONUIDStart = RangePool.Start
121 ONUIDEnd = RangePool.End
Girish Gowdra390f12f2021-07-01 15:53:49 -0700122 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdra64503432020-01-07 10:59:10 +0530123 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
124 AllocIDStart = RangePool.Start
125 AllocIDEnd = RangePool.End
Girish Gowdra390f12f2021-07-01 15:53:49 -0700126 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdra64503432020-01-07 10:59:10 +0530127 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
128 GEMPortIDStart = RangePool.Start
129 GEMPortIDEnd = RangePool.End
Girish Gowdra390f12f2021-07-01 15:53:49 -0700130 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdra64503432020-01-07 10:59:10 +0530131 }
132 }
133
Girish Gowdra390f12f2021-07-01 15:53:49 -0700134 ponRMgr.InitDefaultPONResourceRanges(nil, ONUIDStart, ONUIDEnd, ONUIDShared,
135 AllocIDStart, AllocIDEnd, AllocIDShared,
136 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
137 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdra64503432020-01-07 10:59:10 +0530138 devInfo.PonPorts, techRange.IntfIds)
139
Girish Gowdra64503432020-01-07 10:59:10 +0530140}
141
142// Delete clears used resources for the particular olt device being deleted
143func (RsrcMgr *OpenOltResourceMgr) Delete() error {
Girish Gowdra390f12f2021-07-01 15:53:49 -0700144 if err := RsrcMgr.PonRsrMgr.ClearDeviceResourcePool(context.Background()); err != nil {
145 logger.Debug(nil, "Failed to clear device resource pool")
146 return err
Girish Gowdra64503432020-01-07 10:59:10 +0530147 }
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700148 logger.Debug(nil, "Cleared device resource pool")
Girish Gowdra64503432020-01-07 10:59:10 +0530149 return nil
150}
151
152// GetONUID returns the available OnuID for the given pon-port
153func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
Girish Gowdra390f12f2021-07-01 15:53:49 -0700154 ctx := context.Background()
155 newCtx := context.WithValue(ctx, "ponIf", ponIntfID)
156 // Get ONU id for a provided pon interface ID.
157 onuID, err := RsrcMgr.TechprofileRef.GetResourceID(newCtx, ponIntfID,
Girish Gowdra64503432020-01-07 10:59:10 +0530158 ponrmgr.ONU_ID, 1)
159 if err != nil {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700160 logger.Errorf(nil, "Failed to get resource for interface %d for type %s",
Girish Gowdra64503432020-01-07 10:59:10 +0530161 ponIntfID, ponrmgr.ONU_ID)
Girish Gowdra390f12f2021-07-01 15:53:49 -0700162 return 0, err
Girish Gowdra64503432020-01-07 10:59:10 +0530163 }
Girish Gowdra390f12f2021-07-01 15:53:49 -0700164 if len(onuID) > 0 {
165 return onuID[0], err
166 }
167
168 return 0, err // return onuID 0 on error
Girish Gowdra64503432020-01-07 10:59:10 +0530169}
170
Girish Gowdraaeceb842020-08-21 12:10:39 -0700171// GetFlowID return flow ID for a given pon interface id, onu id and uni id
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700172func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32) (uint64, error) {
Girish Gowdraaeceb842020-08-21 12:10:39 -0700173 RsrcMgr.FlowIDMgmtLock.Lock()
174 defer RsrcMgr.FlowIDMgmtLock.Unlock()
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700175 RsrcMgr.flow_id++
176 return RsrcMgr.flow_id, nil
Girish Gowdra64503432020-01-07 10:59:10 +0530177}