Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //Package resourcemanager provides the utility for managing resources |
| 18 | package core |
| 19 | |
| 20 | import ( |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 21 | "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 Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 25 | "golang.org/x/net/context" |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | basePathKvStore = "service/voltha" |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 31 | ) |
| 32 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 33 | // OpenOltResourceMgr holds resource related information as provided below for each field |
| 34 | type OpenOltResourceMgr struct { |
| 35 | deviceInfo *openolt.DeviceInfo |
Girish Gowdra | aeceb84 | 2020-08-21 12:10:39 -0700 | [diff] [blame] | 36 | |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 37 | intfID uint32 // pon interface id |
Girish Gowdra | aeceb84 | 2020-08-21 12:10:39 -0700 | [diff] [blame] | 38 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 39 | // array of pon resource managers per interface technology |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 40 | PonRsrMgr *ponrmgr.PONResourceManager |
| 41 | TechprofileRef techprofile.TechProfileIf |
| 42 | |
| 43 | FlowIDMgmtLock sync.RWMutex |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 44 | |
| 45 | flow_id uint64 |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 46 | } |
| 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 Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 51 | func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, ponID uint32, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr { |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 52 | var ResourceMgr OpenOltResourceMgr |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 53 | logger.Debugf(nil, "Init new resource manager") |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 54 | |
| 55 | ResourceMgr.deviceInfo = devInfo |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 56 | ResourceMgr.intfID = ponID |
| 57 | ctx := context.Background() |
| 58 | newCtx := context.WithValue(ctx, "ponIf", ponID) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 59 | // Create a separate Resource Manager instance for each range. This assumes that |
| 60 | // each technology is represented by only a single range |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 61 | for _, TechRange := range devInfo.Ranges { |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 62 | 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 Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 80 | } |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 81 | } |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 82 | 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 Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 88 | } |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 89 | |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 90 | logger.Info(nil, "Initialization of resource manager success!") |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 91 | 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 Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 98 | func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 99 | techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) { |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 100 | // 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 Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 109 | |
| 110 | // init the resource range pool according to the sharing type |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 111 | 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 Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 117 | for _, RangePool := range techRange.Pools { |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 118 | // FIXME: Remove hardcoding |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 119 | if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID { |
| 120 | ONUIDStart = RangePool.Start |
| 121 | ONUIDEnd = RangePool.End |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 122 | ONUIDShared = uint32(RangePool.Sharing) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 123 | } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID { |
| 124 | AllocIDStart = RangePool.Start |
| 125 | AllocIDEnd = RangePool.End |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 126 | AllocIDShared = uint32(RangePool.Sharing) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 127 | } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID { |
| 128 | GEMPortIDStart = RangePool.Start |
| 129 | GEMPortIDEnd = RangePool.End |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 130 | GEMPortIDShared = uint32(RangePool.Sharing) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 134 | ponRMgr.InitDefaultPONResourceRanges(nil, ONUIDStart, ONUIDEnd, ONUIDShared, |
| 135 | AllocIDStart, AllocIDEnd, AllocIDShared, |
| 136 | GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared, |
| 137 | flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd, |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 138 | devInfo.PonPorts, techRange.IntfIds) |
| 139 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Delete clears used resources for the particular olt device being deleted |
| 143 | func (RsrcMgr *OpenOltResourceMgr) Delete() error { |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 144 | if err := RsrcMgr.PonRsrMgr.ClearDeviceResourcePool(context.Background()); err != nil { |
| 145 | logger.Debug(nil, "Failed to clear device resource pool") |
| 146 | return err |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 147 | } |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 148 | logger.Debug(nil, "Cleared device resource pool") |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 149 | return nil |
| 150 | } |
| 151 | |
| 152 | // GetONUID returns the available OnuID for the given pon-port |
| 153 | func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) { |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 154 | 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 Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 158 | ponrmgr.ONU_ID, 1) |
| 159 | if err != nil { |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 160 | logger.Errorf(nil, "Failed to get resource for interface %d for type %s", |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 161 | ponIntfID, ponrmgr.ONU_ID) |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 162 | return 0, err |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 163 | } |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 164 | if len(onuID) > 0 { |
| 165 | return onuID[0], err |
| 166 | } |
| 167 | |
| 168 | return 0, err // return onuID 0 on error |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Girish Gowdra | aeceb84 | 2020-08-21 12:10:39 -0700 | [diff] [blame] | 171 | // GetFlowID return flow ID for a given pon interface id, onu id and uni id |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 172 | func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32) (uint64, error) { |
Girish Gowdra | aeceb84 | 2020-08-21 12:10:39 -0700 | [diff] [blame] | 173 | RsrcMgr.FlowIDMgmtLock.Lock() |
| 174 | defer RsrcMgr.FlowIDMgmtLock.Unlock() |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 175 | RsrcMgr.flow_id++ |
| 176 | return RsrcMgr.flow_id, nil |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 177 | } |