blob: ed284b908e9160c7c3241cc565c2a87416c5081d [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Scott Baker2c1c4822019-10-16 11:02:41 -07003
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
17package techprofile
18
19import (
Girish Gowdra248971a2021-06-01 15:14:15 -070020 "bytes"
npujar5bf737f2020-01-16 19:35:25 +053021 "context"
Scott Baker2c1c4822019-10-16 11:02:41 -070022 "errors"
23 "fmt"
Girish Gowdra9447baf2019-11-05 16:42:37 +053024 "regexp"
Scott Baker2c1c4822019-10-16 11:02:41 -070025 "strconv"
pnalmas23a77d02025-01-15 11:52:48 +053026 "strings"
Gamze Abakadfdd8f82020-05-04 08:39:50 +000027 "sync"
Neha Sharma130ac6d2020-04-08 08:46:32 +000028 "time"
Scott Baker2c1c4822019-10-16 11:02:41 -070029
khenaidoo26721882021-08-11 17:42:52 -040030 "github.com/golang/protobuf/jsonpb"
31 "github.com/golang/protobuf/proto"
32 "github.com/opencord/voltha-lib-go/v7/pkg/db"
33 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
34 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo26721882021-08-11 17:42:52 -040035 tp_pb "github.com/opencord/voltha-protos/v5/go/tech_profile"
Scott Baker2c1c4822019-10-16 11:02:41 -070036)
37
38// Interface to pon resource manager APIs
39type iPonResourceMgr interface {
Girish Gowdra248971a2021-06-01 15:14:15 -070040 GetResourceID(ctx context.Context, intfID uint32, resourceType string, numIDs uint32) ([]uint32, error)
41 FreeResourceID(ctx context.Context, intfID uint32, resourceType string, ReleaseContent []uint32) error
Scott Baker2c1c4822019-10-16 11:02:41 -070042 GetResourceTypeAllocID() string
43 GetResourceTypeGemPortID() string
Matteo Scandolob85b2f02021-03-18 14:44:41 -070044 GetResourceTypeOnuID() string
Scott Baker2c1c4822019-10-16 11:02:41 -070045 GetTechnology() string
46}
47
Scott Baker2c1c4822019-10-16 11:02:41 -070048type SchedulingPolicy int32
49
50const (
51 SchedulingPolicy_WRR SchedulingPolicy = 0
52 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
53 SchedulingPolicy_Hybrid SchedulingPolicy = 2
54)
55
Scott Baker2c1c4822019-10-16 11:02:41 -070056type AdditionalBW int32
57
58const (
59 AdditionalBW_AdditionalBW_None AdditionalBW = 0
60 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
61 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
62 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
63)
64
Scott Baker2c1c4822019-10-16 11:02:41 -070065type DiscardPolicy int32
66
67const (
68 DiscardPolicy_TailDrop DiscardPolicy = 0
69 DiscardPolicy_WTailDrop DiscardPolicy = 1
70 DiscardPolicy_Red DiscardPolicy = 2
71 DiscardPolicy_WRed DiscardPolicy = 3
72)
73
Girish Gowdra9447baf2019-11-05 16:42:37 +053074// Required uniPortName format
Girish Gowdra248971a2021-06-01 15:14:15 -070075var uniPortNameFormatRegexp = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
Girish Gowdra9447baf2019-11-05 16:42:37 +053076
Scott Baker2c1c4822019-10-16 11:02:41 -070077// instance control defaults
78const (
79 defaultOnuInstance = "multi-instance"
80 defaultUniInstance = "single-instance"
Scott Baker2c1c4822019-10-16 11:02:41 -070081 defaultGemPayloadSize = "auto"
82)
83
Scott Baker2c1c4822019-10-16 11:02:41 -070084// default discard config constants
85const (
86 defaultMinThreshold = 0
87 defaultMaxThreshold = 0
88 defaultMaxProbability = 0
89)
90
Scott Baker2c1c4822019-10-16 11:02:41 -070091// default scheduler contants
92const (
Girish Gowdra248971a2021-06-01 15:14:15 -070093 defaultPriority = 0
94 defaultWeight = 0
Scott Baker2c1c4822019-10-16 11:02:41 -070095)
96
Scott Baker2c1c4822019-10-16 11:02:41 -070097// default GEM attribute constants
98const (
Esin Karaman8aa75a72019-12-20 13:11:59 +000099 defaultAESEncryption = "True"
100 defaultPriorityQueue = 0
101 defaultQueueWeight = 0
102 defaultMaxQueueSize = "auto"
Esin Karaman8aa75a72019-12-20 13:11:59 +0000103 defaultIsMulticast = "False"
104 defaultAccessControlList = "224.0.0.0-239.255.255.255"
105 defaultMcastGemID = 4069
Scott Baker2c1c4822019-10-16 11:02:41 -0700106)
107
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700108// Default EPON constants
109const (
110 defaultPakageType = "B"
111)
112const (
113 defaultTrafficType = "BE"
114 defaultUnsolicitedGrantSize = 0
115 defaultNominalInterval = 0
116 defaultToleratedPollJitter = 0
117 defaultRequestTransmissionPolicy = 0
118 defaultNumQueueSet = 2
119)
120const (
121 defaultQThreshold1 = 5500
122 defaultQThreshold2 = 0
123 defaultQThreshold3 = 0
124 defaultQThreshold4 = 0
125 defaultQThreshold5 = 0
126 defaultQThreshold6 = 0
127 defaultQThreshold7 = 0
128)
129
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700130const (
Andrea Campanella904fe322020-06-26 18:56:51 +0200131 xgspon = "XGS-PON"
Girish Gowdra248971a2021-06-01 15:14:15 -0700132 xgpon = "XGPON"
Andrea Campanella904fe322020-06-26 18:56:51 +0200133 gpon = "GPON"
134 epon = "EPON"
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700135)
136
Girish Gowdra248971a2021-06-01 15:14:15 -0700137const (
138 MaxUniPortPerOnu = 16 // TODO: Adapter uses its own constant for MaxUniPort. How to synchronize this and have a single source of truth?
139)
140
141type TechProfileMgr struct {
142 config *TechProfileFlags
143 resourceMgr iPonResourceMgr
144 OnuIDMgmtLock sync.RWMutex
145 GemPortIDMgmtLock sync.RWMutex
146 AllocIDMgmtLock sync.RWMutex
147 tpInstanceMap map[string]*tp_pb.TechProfileInstance // Map of tp path to tp instance
148 tpInstanceMapLock sync.RWMutex
149 eponTpInstanceMap map[string]*tp_pb.EponTechProfileInstance // Map of tp path to epon tp instance
150 epontpInstanceMapLock sync.RWMutex
151 tpMap map[uint32]*tp_pb.TechProfile // Map of tp id to tp
152 tpMapLock sync.RWMutex
153 eponTpMap map[uint32]*tp_pb.EponTechProfile // map of tp id to epon tp
154 eponTpMapLock sync.RWMutex
155}
156
Matteo Scandolof34d9082020-11-24 13:56:34 -0800157func (t *TechProfileMgr) SetKVClient(ctx context.Context, pathPrefix string) *db.Backend {
Neha Sharma94f16a92020-06-26 04:17:55 +0000158 kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700159 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000160 logger.Errorw(ctx, "failed-to-create-kv-client",
Scott Baker2c1c4822019-10-16 11:02:41 -0700161 log.Fields{
Neha Sharmadd9af392020-04-28 09:03:57 +0000162 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Matteo Scandolof34d9082020-11-24 13:56:34 -0800163 "timeout": t.config.KVStoreTimeout, "prefix": pathPrefix,
Scott Baker2c1c4822019-10-16 11:02:41 -0700164 "error": err.Error(),
165 })
166 return nil
167 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500168 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700169 Client: kvClient,
170 StoreType: t.config.KVStoreType,
Neha Sharmadd9af392020-04-28 09:03:57 +0000171 Address: t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700172 Timeout: t.config.KVStoreTimeout,
Matteo Scandolof34d9082020-11-24 13:56:34 -0800173 PathPrefix: pathPrefix}
Scott Baker2c1c4822019-10-16 11:02:41 -0700174
175 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700176 issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdra248971a2021-06-01 15:14:15 -0700177 kv := model.NewBackend(t.config.kvStoreType, t.config.KVStoreHost, t.config.KVStorePort,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700178 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
Scott Baker2c1c4822019-10-16 11:02:41 -0700179 */
180}
181
Holger Hildebrandt40bd2672023-02-10 08:14:26 +0000182func (t *TechProfileMgr) CloseKVClient(ctx context.Context) {
183 if t.config.KVBackend != nil {
184 t.config.KVBackend.Client.Close(ctx)
185 t.config.KVBackend = nil
186 }
187 if t.config.DefaultTpKVBackend != nil {
188 t.config.DefaultTpKVBackend.Client.Close(ctx)
189 t.config.DefaultTpKVBackend = nil
190 }
191 if t.config.ResourceInstanceKVBacked != nil {
192 t.config.ResourceInstanceKVBacked.Client.Close(ctx)
193 t.config.ResourceInstanceKVBacked = nil
194 }
195}
196
pnalmas23a77d02025-01-15 11:52:48 +0530197func NewTechProfile(ctx context.Context, IntfId uint32, deviceId string, resourceMgr iPonResourceMgr, kvStoreType string, kvStoreAddress string, basePathKvStore string) (*TechProfileMgr, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700198 var techprofileObj TechProfileMgr
pnalmas23a77d02025-01-15 11:52:48 +0530199 logger.Debug(ctx, "initializing-techprofile-mananger ", log.Fields{"IntId": IntfId, "device-id": deviceId})
Girish Gowdra248971a2021-06-01 15:14:15 -0700200 techprofileObj.config = NewTechProfileFlags(kvStoreType, kvStoreAddress, basePathKvStore)
Matteo Scandolof34d9082020-11-24 13:56:34 -0800201 techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx, techprofileObj.config.TPKVPathPrefix)
202 techprofileObj.config.DefaultTpKVBackend = techprofileObj.SetKVClient(ctx, techprofileObj.config.defaultTpKvPathPrefix)
Scott Baker2c1c4822019-10-16 11:02:41 -0700203 if techprofileObj.config.KVBackend == nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700204 logger.Error(ctx, "failed-to-initialize-backend")
205 return nil, errors.New("kv-backend-init-failed")
206 }
207 techprofileObj.config.ResourceInstanceKVBacked = techprofileObj.SetKVClient(ctx, techprofileObj.config.ResourceInstanceKVPathPrefix)
208 if techprofileObj.config.ResourceInstanceKVBacked == nil {
209 logger.Error(ctx, "failed-to-initialize-resource-instance-kv-backend")
210 return nil, errors.New("resource-instance-kv-backend-init-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700211 }
212 techprofileObj.resourceMgr = resourceMgr
Girish Gowdra248971a2021-06-01 15:14:15 -0700213 techprofileObj.tpInstanceMap = make(map[string]*tp_pb.TechProfileInstance)
214 techprofileObj.eponTpInstanceMap = make(map[string]*tp_pb.EponTechProfileInstance)
215 techprofileObj.tpMap = make(map[uint32]*tp_pb.TechProfile)
216 techprofileObj.eponTpMap = make(map[uint32]*tp_pb.EponTechProfile)
217 logger.Debug(ctx, "reconcile-tp-instance-cache-start")
pnalmas23a77d02025-01-15 11:52:48 +0530218 if err := techprofileObj.reconcileTpInstancesToCache(ctx, IntfId, deviceId); err != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700219 logger.Errorw(ctx, "failed-to-reconcile-tp-instances", log.Fields{"err": err})
220 return nil, err
221 }
222 logger.Debug(ctx, "reconcile-tp-instance-cache-end")
223 logger.Debug(ctx, "initializing-tech-profile-manager-object-success")
Scott Baker2c1c4822019-10-16 11:02:41 -0700224 return &techprofileObj, nil
225}
226
Girish Gowdra248971a2021-06-01 15:14:15 -0700227// GetTechProfileInstanceKey returns the tp instance key that is used to reference TP Instance Map
228func (t *TechProfileMgr) GetTechProfileInstanceKey(ctx context.Context, tpID uint32, uniPortName string) string {
229 logger.Debugw(ctx, "get-tp-instance-kv-key", log.Fields{
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700230 "uniPortName": uniPortName,
Girish Gowdra248971a2021-06-01 15:14:15 -0700231 "tpId": tpID,
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700232 })
Girish Gowdra248971a2021-06-01 15:14:15 -0700233 // Make sure the uniPortName is as per format olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
234 if !uniPortNameFormatRegexp.Match([]byte(uniPortName)) {
235 logger.Warnw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
236 }
237 // The key path prefix (like service/voltha/technology_profiles or service/voltha_voltha/technology_profiles)
238 // is expected to be attached by the components that use this path as part of the KVBackend configuration.
239 resourceInstanceKvPathSuffix := "%s/%d/%s" // <technology>/<tpID>/<uni-port-name>
240 // <uni-port-name> must be of the format pon-{\d+}/onu-{\d+}/uni-{\d+}
241 return fmt.Sprintf(resourceInstanceKvPathSuffix, t.resourceMgr.GetTechnology(), tpID, uniPortName)
Scott Baker2c1c4822019-10-16 11:02:41 -0700242}
243
Girish Gowdra248971a2021-06-01 15:14:15 -0700244// GetTPInstance gets TP instance from cache if found
245func (t *TechProfileMgr) GetTPInstance(ctx context.Context, path string) (interface{}, error) {
246 tech := t.resourceMgr.GetTechnology()
247 switch tech {
248 case xgspon, xgpon, gpon:
249 t.tpInstanceMapLock.RLock()
250 defer t.tpInstanceMapLock.RUnlock()
251 tpInst, ok := t.tpInstanceMap[path]
252 if !ok {
253 return nil, fmt.Errorf("tp-instance-not-found-tp-path-%v", path)
254 }
255 return tpInst, nil
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700256 case epon:
Girish Gowdra248971a2021-06-01 15:14:15 -0700257 t.epontpInstanceMapLock.RLock()
258 defer t.epontpInstanceMapLock.RUnlock()
259 tpInst, ok := t.eponTpInstanceMap[path]
260 if !ok {
261 return nil, fmt.Errorf("tp-instance-not-found-tp-path-%v", path)
262 }
263 return tpInst, nil
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700264 default:
Girish Gowdra248971a2021-06-01 15:14:15 -0700265 logger.Errorw(ctx, "unknown-tech", log.Fields{"tech": tech})
266 return nil, fmt.Errorf("unknown-tech-%s-tp-path-%v", tech, path)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700267 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700268}
269
Girish Gowdra248971a2021-06-01 15:14:15 -0700270// CreateTechProfileInstance creates a new TP instance.
271func (t *TechProfileMgr) CreateTechProfileInstance(ctx context.Context, tpID uint32, uniPortName string, intfID uint32) (interface{}, error) {
272 var tpInstance *tp_pb.TechProfileInstance
273 var eponTpInstance *tp_pb.EponTechProfileInstance
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700274
Girish Gowdra248971a2021-06-01 15:14:15 -0700275 logger.Infow(ctx, "creating-tp-instance", log.Fields{"tpID": tpID, "uni": uniPortName, "intId": intfID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700276
Girish Gowdra248971a2021-06-01 15:14:15 -0700277 // Make sure the uniPortName is as per format olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
278 if !uniPortNameFormatRegexp.Match([]byte(uniPortName)) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000279 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Gowdra248971a2021-06-01 15:14:15 -0700280 return nil, fmt.Errorf("uni-port-name-not-confirming-to-format-%s", uniPortName)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530281 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700282 tpInstancePathSuffix := t.GetTechProfileInstanceKey(ctx, tpID, uniPortName)
283
284 if t.resourceMgr.GetTechnology() == epon {
285 tp := t.getEponTPFromKVStore(ctx, tpID)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700286 if tp != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700287 if err := t.validateInstanceControlAttr(ctx, *tp.InstanceControl); err != nil {
288 logger.Error(ctx, "invalid-instance-ctrl-attr-using-default-tp")
Girish Kumar950f21e2020-08-19 17:42:29 +0000289 tp = t.getDefaultEponProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700290 } else {
Girish Gowdra248971a2021-06-01 15:14:15 -0700291 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpID": tpID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700292 }
293 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000294 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
Girish Kumar950f21e2020-08-19 17:42:29 +0000295 tp = t.getDefaultEponProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700296 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700297 // Store TP in cache
298 t.eponTpMapLock.Lock()
299 t.eponTpMap[tpID] = tp
300 t.eponTpMapLock.Unlock()
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700301
Girish Gowdra248971a2021-06-01 15:14:15 -0700302 if eponTpInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfID, tpInstancePathSuffix); eponTpInstance == nil {
303 logger.Error(ctx, "tp-instance-allocation-failed")
304 return nil, errors.New("tp-instance-allocation-failed")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700305 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700306 t.epontpInstanceMapLock.Lock()
307 t.eponTpInstanceMap[tpInstancePathSuffix] = eponTpInstance
308 t.epontpInstanceMapLock.Unlock()
309 resInst := tp_pb.ResourceInstance{
310 TpId: tpID,
311 ProfileType: eponTpInstance.ProfileType,
312 SubscriberIdentifier: eponTpInstance.SubscriberIdentifier,
313 AllocId: eponTpInstance.AllocId,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700314 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700315 for _, usQAttr := range eponTpInstance.UpstreamQueueAttributeList {
316 resInst.GemportIds = append(resInst.GemportIds, usQAttr.GemportId)
317 }
318
319 logger.Infow(ctx, "epon-tp-instance-created-successfully",
320 log.Fields{"tpID": tpID, "uni": uniPortName, "intfID": intfID})
321 if err := t.addResourceInstanceToKVStore(ctx, tpID, uniPortName, resInst); err != nil {
322 logger.Errorw(ctx, "failed-to-update-resource-instance-to-kv-store--freeing-up-resources", log.Fields{"err": err, "tpID": tpID, "uniPortName": uniPortName})
323 allocIDs := make([]uint32, 0)
324 allocIDs = append(allocIDs, resInst.AllocId)
325 errList := make([]error, 0)
326 errList = append(errList, t.FreeResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeAllocID(), allocIDs))
327 errList = append(errList, t.FreeResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeGemPortID(), resInst.GemportIds))
328 if len(errList) > 0 {
329 logger.Errorw(ctx, "failed-to-free-up-resources-on-kv-store--system-behavior-has-become-erratic", log.Fields{"tpID": tpID, "uniPortName": uniPortName, "errList": errList})
330 }
331 return nil, err
332 }
333 return eponTpInstance, nil
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700334 } else {
Girish Gowdra248971a2021-06-01 15:14:15 -0700335 tp := t.getTPFromKVStore(ctx, tpID)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700336 if tp != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700337 if err := t.validateInstanceControlAttr(ctx, *tp.InstanceControl); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000338 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
339 tp = t.getDefaultTechProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700340 } else {
Girish Gowdra248971a2021-06-01 15:14:15 -0700341 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpID": tpID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700342 }
343 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000344 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
345 tp = t.getDefaultTechProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700346 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700347 // Store TP in cache
348 t.tpMapLock.Lock()
349 t.tpMap[tpID] = tp
350 t.tpMapLock.Unlock()
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700351
Girish Gowdra248971a2021-06-01 15:14:15 -0700352 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfID, tpInstancePathSuffix); tpInstance == nil {
353 logger.Error(ctx, "tp-instance-allocation-failed")
354 return nil, errors.New("tp-instance-allocation-failed")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700355 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700356 t.tpInstanceMapLock.Lock()
357 t.tpInstanceMap[tpInstancePathSuffix] = tpInstance
358 t.tpInstanceMapLock.Unlock()
359
360 resInst := tp_pb.ResourceInstance{
361 TpId: tpID,
362 ProfileType: tpInstance.ProfileType,
363 SubscriberIdentifier: tpInstance.SubscriberIdentifier,
364 AllocId: tpInstance.UsScheduler.AllocId,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700365 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700366 for _, usQAttr := range tpInstance.UpstreamGemPortAttributeList {
367 resInst.GemportIds = append(resInst.GemportIds, usQAttr.GemportId)
368 }
369
370 logger.Infow(ctx, "tp-instance-created-successfully",
371 log.Fields{"tpID": tpID, "uni": uniPortName, "intfID": intfID})
372 if err := t.addResourceInstanceToKVStore(ctx, tpID, uniPortName, resInst); err != nil {
373 logger.Errorw(ctx, "failed-to-update-resource-instance-to-kv-store--freeing-up-resources", log.Fields{"err": err, "tpID": tpID, "uniPortName": uniPortName})
374 allocIDs := make([]uint32, 0)
375 allocIDs = append(allocIDs, resInst.AllocId)
376 errList := make([]error, 0)
377 errList = append(errList, t.FreeResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeAllocID(), allocIDs))
378 errList = append(errList, t.FreeResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeGemPortID(), resInst.GemportIds))
379 if len(errList) > 0 {
380 logger.Fatalw(ctx, "failed-to-free-up-resources-on-kv-store--system-behavior-has-become-erratic", log.Fields{"err": err, "tpID": tpID, "uniPortName": uniPortName})
381 }
382 return nil, err
383 }
384
385 logger.Infow(ctx, "resource-instance-added-to-kv-store-successfully",
386 log.Fields{"tpID": tpID, "uni": uniPortName, "intfID": intfID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700387 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700388 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700389}
390
Girish Gowdra248971a2021-06-01 15:14:15 -0700391// DeleteTechProfileInstance deletes the TP instance from the local cache as well as deletes the corresponding
392// resource instance from the KV store.
393func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, tpID uint32, uniPortName string) error {
394 // Make sure the uniPortName is as per format olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
395 if !uniPortNameFormatRegexp.Match([]byte(uniPortName)) {
396 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
397 return fmt.Errorf("uni-port-name-not-confirming-to-format--%s", uniPortName)
398 }
399 path := t.GetTechProfileInstanceKey(ctx, tpID, uniPortName)
400 logger.Infow(ctx, "delete-tp-instance-from-cache", log.Fields{"key": path})
401 t.tpInstanceMapLock.Lock()
402 delete(t.tpInstanceMap, path)
403 t.tpInstanceMapLock.Unlock()
404 if err := t.removeResourceInstanceFromKVStore(ctx, tpID, uniPortName); err != nil {
405 return err
406 }
407 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700408}
409
Girish Gowdra248971a2021-06-01 15:14:15 -0700410func (t *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *tp_pb.TechProfileInstance) []*tp_pb.TrafficQueue {
411 var encryp bool
412 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
413 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
414 for Count := 0; Count < NumGemPorts; Count++ {
415 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
416 continue
417 }
418 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
419 encryp = true
420 } else {
421 encryp = false
422 }
423 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
424 Direction: tp_pb.Direction_DOWNSTREAM,
425 GemportId: tp.DownstreamGemPortAttributeList[Count].MulticastGemId,
426 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
427 AesEncryption: encryp,
428 SchedPolicy: tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy,
429 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQ,
430 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
431 DiscardPolicy: tp.DownstreamGemPortAttributeList[Count].DiscardPolicy,
432 })
433 }
434 logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
435 return mcastTrafficQueues
436}
437
438func (t *TechProfileMgr) GetGemportForPbit(ctx context.Context, tp interface{}, dir tp_pb.Direction, pbit uint32) interface{} {
439 /*
440 Function to get the Gemport mapped to a pbit.
441 */
442 switch tp := tp.(type) {
443 case *tp_pb.TechProfileInstance:
444 if dir == tp_pb.Direction_UPSTREAM {
445 // upstream GEM ports
446 numGemPorts := len(tp.UpstreamGemPortAttributeList)
447 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
448 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
449 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
450 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
451 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
452 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
453 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
454 logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportId})
455 return tp.UpstreamGemPortAttributeList[gemCnt]
456 }
457 }
458 }
459 }
460 } else if dir == tp_pb.Direction_DOWNSTREAM {
461 //downstream GEM ports
462 numGemPorts := len(tp.DownstreamGemPortAttributeList)
463 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
464 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
465 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
466 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
467 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
468 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
469 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
470 logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportId})
471 return tp.DownstreamGemPortAttributeList[gemCnt]
472 }
473 }
474 }
475 }
476 }
477 logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
khenaidooa5feb8e2021-10-19 17:29:22 -0400478 case *tp_pb.EponTechProfileInstance:
Girish Gowdra248971a2021-06-01 15:14:15 -0700479 if dir == tp_pb.Direction_UPSTREAM {
480 // upstream GEM ports
481 numGemPorts := len(tp.UpstreamQueueAttributeList)
482 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
483 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
484 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
485 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
486 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
487 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
488 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
489 logger.Debugw(ctx, "Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportId})
490 return tp.UpstreamQueueAttributeList[gemCnt]
491 }
492 }
493 }
494 }
495 } else if dir == tp_pb.Direction_DOWNSTREAM {
496 //downstream GEM ports
497 numGemPorts := len(tp.DownstreamQueueAttributeList)
498 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
499 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
500 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
501 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
502 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
503 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
504 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
505 logger.Debugw(ctx, "Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportId})
506 return tp.DownstreamQueueAttributeList[gemCnt]
507 }
508 }
509 }
510 }
511 }
512 logger.Errorw(ctx, "No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
513 default:
514 logger.Errorw(ctx, "unknown-tech", log.Fields{"tp": tp})
515 }
516 return nil
517}
518
519// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
520func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, oltDeviceID string, tpID uint32, intfID uint32, onuID uint32) interface{} {
521 onuTpInstancePathSuffix := fmt.Sprintf("%s/%d/olt-{%s}/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), tpID, oltDeviceID, intfID, onuID)
522 tech := t.resourceMgr.GetTechnology()
523 if tech == xgspon || tech == xgpon || tech == gpon {
524 t.tpInstanceMapLock.RLock()
525 defer t.tpInstanceMapLock.RUnlock()
526 tpInstancesTech := make([]tp_pb.TechProfileInstance, 0)
527 for i := 0; i < MaxUniPortPerOnu; i++ {
528 key := onuTpInstancePathSuffix + fmt.Sprintf("/uni-{%d}", i)
529 if tpInst, ok := t.tpInstanceMap[key]; ok {
530 tpInstancesTech = append(tpInstancesTech, *tpInst)
531 }
532 }
533 return tpInstancesTech
534 } else if tech == epon {
535 t.epontpInstanceMapLock.RLock()
536 defer t.epontpInstanceMapLock.RUnlock()
537 tpInstancesTech := make([]tp_pb.EponTechProfileInstance, 0)
538 for i := 0; i < MaxUniPortPerOnu; i++ {
539 key := onuTpInstancePathSuffix + fmt.Sprintf("/uni-{%d}", i)
540 if tpInst, ok := t.eponTpInstanceMap[key]; ok {
541 tpInstancesTech = append(tpInstancesTech, *tpInst)
542 }
543 }
544 return tpInstancesTech
545 } else {
546 logger.Errorw(ctx, "unknown-tech", log.Fields{"tech": tech, "tpID": tpID, "onuID": onuID, "intfID": intfID})
547 }
548 return nil
549}
550
551func (t *TechProfileMgr) GetResourceID(ctx context.Context, intfID uint32, resourceType string, numIDs uint32) ([]uint32, error) {
552 logger.Debugw(ctx, "getting-resource-id", log.Fields{
553 "intf-id": intfID,
554 "resource-type": resourceType,
555 "num": numIDs,
556 })
557 var err error
558 var ids []uint32
559 switch resourceType {
560 case t.resourceMgr.GetResourceTypeAllocID():
561 t.AllocIDMgmtLock.Lock()
562 ids, err = t.resourceMgr.GetResourceID(ctx, intfID, resourceType, numIDs)
563 t.AllocIDMgmtLock.Unlock()
564 case t.resourceMgr.GetResourceTypeGemPortID():
565 t.GemPortIDMgmtLock.Lock()
566 ids, err = t.resourceMgr.GetResourceID(ctx, intfID, resourceType, numIDs)
567 t.GemPortIDMgmtLock.Unlock()
568 case t.resourceMgr.GetResourceTypeOnuID():
569 t.OnuIDMgmtLock.Lock()
570 ids, err = t.resourceMgr.GetResourceID(ctx, intfID, resourceType, numIDs)
571 t.OnuIDMgmtLock.Unlock()
572 default:
573 return nil, fmt.Errorf("resourceType %s not supported", resourceType)
574 }
575 if err != nil {
576 return nil, err
577 }
578 return ids, nil
579}
580
581func (t *TechProfileMgr) FreeResourceID(ctx context.Context, intfID uint32, resourceType string, ReleaseContent []uint32) error {
582 logger.Debugw(ctx, "freeing-resource-id", log.Fields{
583 "intf-id": intfID,
584 "resource-type": resourceType,
585 "release-content": ReleaseContent,
586 })
587 var err error
588 switch resourceType {
589 case t.resourceMgr.GetResourceTypeAllocID():
590 t.AllocIDMgmtLock.Lock()
591 err = t.resourceMgr.FreeResourceID(ctx, intfID, resourceType, ReleaseContent)
592 t.AllocIDMgmtLock.Unlock()
593 case t.resourceMgr.GetResourceTypeGemPortID():
594 t.GemPortIDMgmtLock.Lock()
595 err = t.resourceMgr.FreeResourceID(ctx, intfID, resourceType, ReleaseContent)
596 t.GemPortIDMgmtLock.Unlock()
597 case t.resourceMgr.GetResourceTypeOnuID():
598 t.OnuIDMgmtLock.Lock()
599 err = t.resourceMgr.FreeResourceID(ctx, intfID, resourceType, ReleaseContent)
600 t.OnuIDMgmtLock.Unlock()
601 default:
602 return fmt.Errorf("resourceType %s not supported", resourceType)
603 }
604 if err != nil {
605 return err
606 }
607 return nil
608}
609
610func (t *TechProfileMgr) GetUsScheduler(tpInstance *tp_pb.TechProfileInstance) *tp_pb.SchedulerConfig {
611 return &tp_pb.SchedulerConfig{
612 Direction: tpInstance.UsScheduler.Direction,
613 AdditionalBw: tpInstance.UsScheduler.AdditionalBw,
614 Priority: tpInstance.UsScheduler.Priority,
615 Weight: tpInstance.UsScheduler.Weight,
616 SchedPolicy: tpInstance.UsScheduler.QSchedPolicy}
617}
618
619func (t *TechProfileMgr) GetDsScheduler(tpInstance *tp_pb.TechProfileInstance) *tp_pb.SchedulerConfig {
620 return &tp_pb.SchedulerConfig{
621 Direction: tpInstance.DsScheduler.Direction,
622 AdditionalBw: tpInstance.DsScheduler.AdditionalBw,
623 Priority: tpInstance.DsScheduler.Priority,
624 Weight: tpInstance.DsScheduler.Weight,
625 SchedPolicy: tpInstance.DsScheduler.QSchedPolicy}
626}
627
628func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *tp_pb.TechProfileInstance, SchedCfg *tp_pb.SchedulerConfig,
629 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
630
631 tSched := &tp_pb.TrafficScheduler{
632 Direction: SchedCfg.Direction,
633 AllocId: tpInstance.UsScheduler.AllocId,
634 TrafficShapingInfo: ShapingCfg,
635 Scheduler: SchedCfg}
636
637 return tSched
638}
639
640func (t *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *tp_pb.TechProfileInstance, direction tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
641
642 var encryp bool
643 if direction == tp_pb.Direction_UPSTREAM {
644 // upstream GEM ports
645 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
646 GemPorts := make([]*tp_pb.TrafficQueue, 0)
647 for Count := 0; Count < NumGemPorts; Count++ {
648 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
649 encryp = true
650 } else {
651 encryp = false
652 }
653
654 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
655 Direction: direction,
656 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportId,
657 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
658 AesEncryption: encryp,
659 SchedPolicy: tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy,
660 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQ,
661 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
662 DiscardPolicy: tp.UpstreamGemPortAttributeList[Count].DiscardPolicy,
663 })
664 }
665 logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
666 return GemPorts, nil
667 } else if direction == tp_pb.Direction_DOWNSTREAM {
668 //downstream GEM ports
669 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
670 GemPorts := make([]*tp_pb.TrafficQueue, 0)
671 for Count := 0; Count < NumGemPorts; Count++ {
672 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
673 //do not take multicast GEM ports. They are handled separately.
674 continue
675 }
676 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
677 encryp = true
678 } else {
679 encryp = false
680 }
681
682 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
683 Direction: direction,
684 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportId,
685 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
686 AesEncryption: encryp,
687 SchedPolicy: tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy,
688 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQ,
689 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
690 DiscardPolicy: tp.DownstreamGemPortAttributeList[Count].DiscardPolicy,
691 })
692 }
693 logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
694 return GemPorts, nil
695 }
696
697 logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", direction)
698 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", direction)
699}
700
701func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl tp_pb.InstanceControl) error {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530702 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000703 logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530704 return errors.New("invalid-onu-instance-ctl-attr")
705 }
706
707 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000708 logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530709 return errors.New("invalid-uni-instance-ctl-attr")
710 }
711
712 if instCtl.Uni == "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000713 logger.Error(ctx, "uni-multi-instance-tp-not-supported")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530714 return errors.New("uni-multi-instance-tp-not-supported")
715 }
716
717 return nil
718}
719
Girish Gowdra248971a2021-06-01 15:14:15 -0700720// allocateTPInstance for GPON, XGPON and XGS-PON technology
721func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *tp_pb.TechProfile, intfID uint32, tpInstPathSuffix string) *tp_pb.TechProfileInstance {
Scott Baker2c1c4822019-10-16 11:02:41 -0700722
Girish Gowdra248971a2021-06-01 15:14:15 -0700723 var usGemPortAttributeList []*tp_pb.GemPortAttributes
724 var dsGemPortAttributeList []*tp_pb.GemPortAttributes
725 var dsMulticastGemAttributeList []*tp_pb.GemPortAttributes
726 var dsUnicastGemAttributeList []*tp_pb.GemPortAttributes
Scott Baker2c1c4822019-10-16 11:02:41 -0700727 var tcontIDs []uint32
728 var gemPorts []uint32
729 var err error
730
Girish Gowdra248971a2021-06-01 15:14:15 -0700731 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfID": intfID, "numGem": tp.NumGemPorts})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530732
Girish Gowdra248971a2021-06-01 15:14:15 -0700733 if tp.InstanceControl.Onu == "multi-instance" {
734 tcontIDs, err = t.GetResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeAllocID(), 1)
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000735 if err != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700736 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"err": err, "intfID": intfID})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530737 return nil
738 }
739 } else { // "single-instance"
Girish Gowdra248971a2021-06-01 15:14:15 -0700740 if tpInst := t.getSingleInstanceTp(ctx, tpInstPathSuffix); tpInst == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530741 // No "single-instance" tp found on one any uni port for the given TP ID
742 // Allocate a new TcontID or AllocID
Girish Gowdra248971a2021-06-01 15:14:15 -0700743 tcontIDs, err = t.GetResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeAllocID(), 1)
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000744 if err != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700745 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"err": err, "intfID": intfID})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530746 return nil
747 }
748 } else {
749 // Use the alloc-id from the existing TpInstance
Girish Gowdra248971a2021-06-01 15:14:15 -0700750 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocId)
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530751 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700752 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000753 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Girish Gowdra248971a2021-06-01 15:14:15 -0700754 gemPorts, err = t.GetResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000755 if err != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -0700756 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"err": err, "intfID": intfID, "numGemports": tp.NumGemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700757 return nil
758 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000759 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700760 for index := 0; index < int(tp.NumGemPorts); index++ {
761 usGemPortAttributeList = append(usGemPortAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -0700762 &tp_pb.GemPortAttributes{GemportId: gemPorts[index],
763 MaxQSize: tp.UpstreamGemPortAttributeList[index].MaxQSize,
Scott Baker2c1c4822019-10-16 11:02:41 -0700764 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
765 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
766 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
Girish Gowdra248971a2021-06-01 15:14:15 -0700767 PriorityQ: tp.UpstreamGemPortAttributeList[index].PriorityQ,
Scott Baker2c1c4822019-10-16 11:02:41 -0700768 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
769 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
770 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000771 }
772
Neha Sharma94f16a92020-06-26 04:17:55 +0000773 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Esin Karaman8aa75a72019-12-20 13:11:59 +0000774 //put multicast and unicast downstream GEM port attributes in different lists first
Girish Gowdra248971a2021-06-01 15:14:15 -0700775 for index := 0; index < len(tp.DownstreamGemPortAttributeList); index++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000776 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
777 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -0700778 &tp_pb.GemPortAttributes{
779 MulticastGemId: tp.DownstreamGemPortAttributeList[index].MulticastGemId,
780 MaxQSize: tp.DownstreamGemPortAttributeList[index].MaxQSize,
781 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
782 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
783 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
784 PriorityQ: tp.DownstreamGemPortAttributeList[index].PriorityQ,
785 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
786 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
787 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
788 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
789 DynamicAccessControlList: tp.DownstreamGemPortAttributeList[index].DynamicAccessControlList,
790 StaticAccessControlList: tp.DownstreamGemPortAttributeList[index].StaticAccessControlList})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000791 } else {
792 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -0700793 &tp_pb.GemPortAttributes{
794 MaxQSize: tp.DownstreamGemPortAttributeList[index].MaxQSize,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000795 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
796 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
797 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
Girish Gowdra248971a2021-06-01 15:14:15 -0700798 PriorityQ: tp.DownstreamGemPortAttributeList[index].PriorityQ,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000799 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
800 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
801 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
802 }
803 }
804 //add unicast downstream GEM ports to dsGemPortAttributeList
Girish Gowdra248971a2021-06-01 15:14:15 -0700805 if dsUnicastGemAttributeList != nil {
806 for index := 0; index < int(tp.NumGemPorts); index++ {
807 dsGemPortAttributeList = append(dsGemPortAttributeList,
808 &tp_pb.GemPortAttributes{GemportId: gemPorts[index],
809 MaxQSize: dsUnicastGemAttributeList[index].MaxQSize,
810 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
811 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
812 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
813 PriorityQ: dsUnicastGemAttributeList[index].PriorityQ,
814 Weight: dsUnicastGemAttributeList[index].Weight,
815 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
816 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
817 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700818 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000819 //add multicast GEM ports to dsGemPortAttributeList afterwards
820 for k := range dsMulticastGemAttributeList {
821 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
822 }
823
Girish Gowdra248971a2021-06-01 15:14:15 -0700824 return &tp_pb.TechProfileInstance{
Scott Baker2c1c4822019-10-16 11:02:41 -0700825 SubscriberIdentifier: uniPortName,
826 Name: tp.Name,
827 ProfileType: tp.ProfileType,
828 Version: tp.Version,
829 NumGemPorts: tp.NumGemPorts,
Girish Gowdra248971a2021-06-01 15:14:15 -0700830 InstanceControl: tp.InstanceControl,
831 UsScheduler: &tp_pb.SchedulerAttributes{
832 AllocId: tcontIDs[0],
Scott Baker2c1c4822019-10-16 11:02:41 -0700833 Direction: tp.UsScheduler.Direction,
834 AdditionalBw: tp.UsScheduler.AdditionalBw,
835 Priority: tp.UsScheduler.Priority,
836 Weight: tp.UsScheduler.Weight,
837 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
Girish Gowdra248971a2021-06-01 15:14:15 -0700838 DsScheduler: &tp_pb.SchedulerAttributes{
839 AllocId: tcontIDs[0],
Scott Baker2c1c4822019-10-16 11:02:41 -0700840 Direction: tp.DsScheduler.Direction,
841 AdditionalBw: tp.DsScheduler.AdditionalBw,
842 Priority: tp.DsScheduler.Priority,
843 Weight: tp.DsScheduler.Weight,
844 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
845 UpstreamGemPortAttributeList: usGemPortAttributeList,
846 DownstreamGemPortAttributeList: dsGemPortAttributeList}
847}
848
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700849// allocateTPInstance function for EPON
Girish Gowdra248971a2021-06-01 15:14:15 -0700850func (t *TechProfileMgr) allocateEponTPInstance(ctx context.Context, uniPortName string, tp *tp_pb.EponTechProfile, intfID uint32, tpInstPath string) *tp_pb.EponTechProfileInstance {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700851
Girish Gowdra248971a2021-06-01 15:14:15 -0700852 var usQueueAttributeList []*tp_pb.EPONQueueAttributes
853 var dsQueueAttributeList []*tp_pb.EPONQueueAttributes
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700854 var tcontIDs []uint32
855 var gemPorts []uint32
856 var err error
857
Girish Gowdra248971a2021-06-01 15:14:15 -0700858 logger.Infow(ctx, "allocating-tp-instance-from-tp-template", log.Fields{"uniPortName": uniPortName, "intfID": intfID, "numGem": tp.NumGemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700859
Girish Gowdra248971a2021-06-01 15:14:15 -0700860 if tp.InstanceControl.Onu == "multi-instance" {
861 if tcontIDs, err = t.GetResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
862 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"err": err, "intfID": intfID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700863 return nil
864 }
865 } else { // "single-instance"
Girish Gowdra248971a2021-06-01 15:14:15 -0700866 if tpInst := t.getSingleInstanceEponTp(ctx, tpInstPath); tpInst == nil {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700867 // No "single-instance" tp found on one any uni port for the given TP ID
868 // Allocate a new TcontID or AllocID
Girish Gowdra248971a2021-06-01 15:14:15 -0700869 if tcontIDs, err = t.GetResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
870 logger.Errorw(ctx, "error-getting-alloc-id-from-resource-mgr", log.Fields{"err": err, "intfID": intfID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700871 return nil
872 }
873 } else {
874 // Use the alloc-id from the existing TpInstance
Girish Gowdra248971a2021-06-01 15:14:15 -0700875 tcontIDs = append(tcontIDs, tpInst.AllocId)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700876 }
877 }
Girish Kumar950f21e2020-08-19 17:42:29 +0000878 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Girish Gowdra248971a2021-06-01 15:14:15 -0700879 if gemPorts, err = t.GetResourceID(ctx, intfID, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
880 logger.Errorw(ctx, "error-getting-gemport-id-from-resource-mgr", log.Fields{"err": err, "intfID": intfID, "numGemports": tp.NumGemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700881 return nil
882 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700883 logger.Infow(ctx, "allocated-alloc-id-and-gemport-successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700884 for index := 0; index < int(tp.NumGemPorts); index++ {
885 usQueueAttributeList = append(usQueueAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -0700886 &tp_pb.EPONQueueAttributes{GemportId: gemPorts[index],
887 MaxQSize: tp.UpstreamQueueAttributeList[index].MaxQSize,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700888 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
889 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
890 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
891 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
892 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
893 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
894 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
Girish Gowdra248971a2021-06-01 15:14:15 -0700895 NumQSets: tp.UpstreamQueueAttributeList[index].NumQSets,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700896 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
897 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
Girish Gowdra248971a2021-06-01 15:14:15 -0700898 PriorityQ: tp.UpstreamQueueAttributeList[index].PriorityQ,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700899 Weight: tp.UpstreamQueueAttributeList[index].Weight,
900 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
901 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
902 }
903
Girish Gowdra248971a2021-06-01 15:14:15 -0700904 logger.Info(ctx, "length-of-downstream-gemport-attribute-list", len(tp.DownstreamQueueAttributeList))
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700905 for index := 0; index < int(tp.NumGemPorts); index++ {
906 dsQueueAttributeList = append(dsQueueAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -0700907 &tp_pb.EPONQueueAttributes{GemportId: gemPorts[index],
908 MaxQSize: tp.DownstreamQueueAttributeList[index].MaxQSize,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700909 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
910 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
911 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
Girish Gowdra248971a2021-06-01 15:14:15 -0700912 PriorityQ: tp.DownstreamQueueAttributeList[index].PriorityQ,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700913 Weight: tp.DownstreamQueueAttributeList[index].Weight,
914 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
915 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
916 }
917
Girish Gowdra248971a2021-06-01 15:14:15 -0700918 return &tp_pb.EponTechProfileInstance{
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700919 SubscriberIdentifier: uniPortName,
920 Name: tp.Name,
921 ProfileType: tp.ProfileType,
922 Version: tp.Version,
923 NumGemPorts: tp.NumGemPorts,
Girish Gowdra248971a2021-06-01 15:14:15 -0700924 InstanceControl: tp.InstanceControl,
925 PackageType: tp.PackageType,
926 AllocId: tcontIDs[0],
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700927 UpstreamQueueAttributeList: usQueueAttributeList,
928 DownstreamQueueAttributeList: dsQueueAttributeList}
929}
930
Girish Gowdra248971a2021-06-01 15:14:15 -0700931// getSingleInstanceTp returns another TpInstance (GPON, XGPON, XGS-PON) for an ONU on a different
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530932// uni port for the same TP ID, if it finds one, else nil.
Girish Gowdra248971a2021-06-01 15:14:15 -0700933func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPathSuffix string) *tp_pb.TechProfileInstance {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530934
935 // For example:
Girish Gowdra248971a2021-06-01 15:14:15 -0700936 // tpPathSuffix like "XGS-PON/64/olt-{1234}/pon-{0}/onu-{1}/uni-{1}"
937 // is broken into ["XGS-PON/64/olt-{1234}/pon-{0}/onu-{1}" ""]
938 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPathSuffix, 2)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530939
Girish Gowdra248971a2021-06-01 15:14:15 -0700940 t.tpInstanceMapLock.RLock()
941 defer t.tpInstanceMapLock.RUnlock()
942 for i := 0; i < MaxUniPortPerOnu; i++ {
943 key := fmt.Sprintf(uniPathSlice[0]+"/uni-{%d}", i)
944 if tpInst, ok := t.tpInstanceMap[key]; ok {
945 logger.Debugw(ctx, "found-single-instance-tp", log.Fields{"key": key})
946 return tpInst
Girish Gowdra9447baf2019-11-05 16:42:37 +0530947 }
948 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700949 return nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530950}
951
Girish Gowdra248971a2021-06-01 15:14:15 -0700952// getSingleInstanceTp returns another TpInstance (EPON) for an ONU on a different
953// uni port for the same TP ID, if it finds one, else nil.
954func (t *TechProfileMgr) getSingleInstanceEponTp(ctx context.Context, tpPathSuffix string) *tp_pb.EponTechProfileInstance {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700955 // For example:
Girish Gowdra248971a2021-06-01 15:14:15 -0700956 // tpPathSuffix like "EPON/64/olt-{1234}/pon-{0}/onu-{1}/uni-{1}"
957 // is broken into ["EPON/64/-{1234}/pon-{0}/onu-{1}" ""]
958 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPathSuffix, 2)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700959
Girish Gowdra248971a2021-06-01 15:14:15 -0700960 t.epontpInstanceMapLock.RLock()
961 defer t.epontpInstanceMapLock.RUnlock()
962 for i := 0; i < MaxUniPortPerOnu; i++ {
963 key := fmt.Sprintf(uniPathSlice[0]+"/uni-{%d}", i)
964 if tpInst, ok := t.eponTpInstanceMap[key]; ok {
965 logger.Debugw(ctx, "found-single-instance-tp", log.Fields{"key": key})
966 return tpInst
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700967 }
968 }
Girish Gowdra248971a2021-06-01 15:14:15 -0700969 return nil
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700970}
971
Girish Gowdra248971a2021-06-01 15:14:15 -0700972// getDefaultTechProfile returns a default TechProfile for GPON, XGPON, XGS-PON
973func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *tp_pb.TechProfile {
974 var usGemPortAttributeList []*tp_pb.GemPortAttributes
975 var dsGemPortAttributeList []*tp_pb.GemPortAttributes
Scott Baker2c1c4822019-10-16 11:02:41 -0700976
977 for _, pbit := range t.config.DefaultPbits {
Girish Gowdra248971a2021-06-01 15:14:15 -0700978 logger.Debugw(ctx, "creating-gem-port-profile-profile", log.Fields{"pbit": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700979 usGemPortAttributeList = append(usGemPortAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -0700980 &tp_pb.GemPortAttributes{
981 MaxQSize: defaultMaxQueueSize,
Scott Baker2c1c4822019-10-16 11:02:41 -0700982 PbitMap: pbit,
983 AesEncryption: defaultAESEncryption,
Girish Gowdra248971a2021-06-01 15:14:15 -0700984 SchedulingPolicy: tp_pb.SchedulingPolicy_WRR,
985 PriorityQ: defaultPriorityQueue,
Scott Baker2c1c4822019-10-16 11:02:41 -0700986 Weight: defaultQueueWeight,
Girish Gowdra248971a2021-06-01 15:14:15 -0700987 DiscardPolicy: tp_pb.DiscardPolicy_TailDrop,
988 DiscardConfigV2: &tp_pb.DiscardConfig{
989 DiscardPolicy: tp_pb.DiscardPolicy_Red,
990 DiscardConfig: &tp_pb.DiscardConfig_RedDiscardConfig{
991 RedDiscardConfig: &tp_pb.RedDiscardConfig{
992 MinThreshold: defaultMinThreshold,
993 MaxThreshold: defaultMaxThreshold,
994 MaxProbability: defaultMaxProbability,
995 },
996 },
997 },
998 DiscardConfig: &tp_pb.RedDiscardConfig{
Scott Baker2c1c4822019-10-16 11:02:41 -0700999 MinThreshold: defaultMinThreshold,
1000 MaxThreshold: defaultMaxThreshold,
Girish Gowdra248971a2021-06-01 15:14:15 -07001001 MaxProbability: defaultMaxProbability,
1002 },
1003 })
Scott Baker2c1c4822019-10-16 11:02:41 -07001004 dsGemPortAttributeList = append(dsGemPortAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -07001005 &tp_pb.GemPortAttributes{
1006 MaxQSize: defaultMaxQueueSize,
Scott Baker2c1c4822019-10-16 11:02:41 -07001007 PbitMap: pbit,
1008 AesEncryption: defaultAESEncryption,
Girish Gowdra248971a2021-06-01 15:14:15 -07001009 SchedulingPolicy: tp_pb.SchedulingPolicy_WRR,
1010 PriorityQ: defaultPriorityQueue,
Scott Baker2c1c4822019-10-16 11:02:41 -07001011 Weight: defaultQueueWeight,
Girish Gowdra248971a2021-06-01 15:14:15 -07001012 DiscardPolicy: tp_pb.DiscardPolicy_TailDrop,
1013 DiscardConfigV2: &tp_pb.DiscardConfig{
1014 DiscardPolicy: tp_pb.DiscardPolicy_Red,
1015 DiscardConfig: &tp_pb.DiscardConfig_RedDiscardConfig{
1016 RedDiscardConfig: &tp_pb.RedDiscardConfig{
1017 MinThreshold: defaultMinThreshold,
1018 MaxThreshold: defaultMaxThreshold,
1019 MaxProbability: defaultMaxProbability,
1020 },
1021 },
1022 },
1023 DiscardConfig: &tp_pb.RedDiscardConfig{
Scott Baker2c1c4822019-10-16 11:02:41 -07001024 MinThreshold: defaultMinThreshold,
1025 MaxThreshold: defaultMaxThreshold,
Girish Gowdra248971a2021-06-01 15:14:15 -07001026 MaxProbability: defaultMaxProbability,
1027 },
1028 IsMulticast: defaultIsMulticast,
1029 DynamicAccessControlList: defaultAccessControlList,
1030 StaticAccessControlList: defaultAccessControlList,
1031 MulticastGemId: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -07001032 }
Girish Gowdra248971a2021-06-01 15:14:15 -07001033 return &tp_pb.TechProfile{
Scott Baker2c1c4822019-10-16 11:02:41 -07001034 Name: t.config.DefaultTPName,
1035 ProfileType: t.resourceMgr.GetTechnology(),
1036 Version: t.config.TPVersion,
1037 NumGemPorts: uint32(len(usGemPortAttributeList)),
Girish Gowdra248971a2021-06-01 15:14:15 -07001038 InstanceControl: &tp_pb.InstanceControl{
Scott Baker2c1c4822019-10-16 11:02:41 -07001039 Onu: defaultOnuInstance,
1040 Uni: defaultUniInstance,
1041 MaxGemPayloadSize: defaultGemPayloadSize},
Girish Gowdra248971a2021-06-01 15:14:15 -07001042 UsScheduler: &tp_pb.SchedulerAttributes{
1043 Direction: tp_pb.Direction_UPSTREAM,
1044 AdditionalBw: tp_pb.AdditionalBW_AdditionalBW_BestEffort,
Scott Baker2c1c4822019-10-16 11:02:41 -07001045 Priority: defaultPriority,
1046 Weight: defaultWeight,
Girish Gowdra248971a2021-06-01 15:14:15 -07001047 QSchedPolicy: tp_pb.SchedulingPolicy_Hybrid},
1048 DsScheduler: &tp_pb.SchedulerAttributes{
1049 Direction: tp_pb.Direction_DOWNSTREAM,
1050 AdditionalBw: tp_pb.AdditionalBW_AdditionalBW_BestEffort,
Scott Baker2c1c4822019-10-16 11:02:41 -07001051 Priority: defaultPriority,
1052 Weight: defaultWeight,
Girish Gowdra248971a2021-06-01 15:14:15 -07001053 QSchedPolicy: tp_pb.SchedulingPolicy_Hybrid},
Scott Baker2c1c4822019-10-16 11:02:41 -07001054 UpstreamGemPortAttributeList: usGemPortAttributeList,
1055 DownstreamGemPortAttributeList: dsGemPortAttributeList}
1056}
1057
Girish Gowdra248971a2021-06-01 15:14:15 -07001058// getDefaultEponProfile returns a default TechProfile for EPON
1059func (t *TechProfileMgr) getDefaultEponProfile(ctx context.Context) *tp_pb.EponTechProfile {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001060
Girish Gowdra248971a2021-06-01 15:14:15 -07001061 var usQueueAttributeList []*tp_pb.EPONQueueAttributes
1062 var dsQueueAttributeList []*tp_pb.EPONQueueAttributes
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001063
1064 for _, pbit := range t.config.DefaultPbits {
Girish Kumar950f21e2020-08-19 17:42:29 +00001065 logger.Debugw(ctx, "Creating Queue", log.Fields{"pbit": pbit})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001066 usQueueAttributeList = append(usQueueAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -07001067 &tp_pb.EPONQueueAttributes{
1068 MaxQSize: defaultMaxQueueSize,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001069 PbitMap: pbit,
1070 AesEncryption: defaultAESEncryption,
1071 TrafficType: defaultTrafficType,
1072 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
1073 NominalInterval: defaultNominalInterval,
1074 ToleratedPollJitter: defaultToleratedPollJitter,
1075 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
Girish Gowdra248971a2021-06-01 15:14:15 -07001076 NumQSets: defaultNumQueueSet,
1077 QThresholds: &tp_pb.QThresholds{
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001078 QThreshold1: defaultQThreshold1,
1079 QThreshold2: defaultQThreshold2,
1080 QThreshold3: defaultQThreshold3,
1081 QThreshold4: defaultQThreshold4,
1082 QThreshold5: defaultQThreshold5,
1083 QThreshold6: defaultQThreshold6,
1084 QThreshold7: defaultQThreshold7},
Girish Gowdra248971a2021-06-01 15:14:15 -07001085 SchedulingPolicy: tp_pb.SchedulingPolicy_WRR,
1086 PriorityQ: defaultPriorityQueue,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001087 Weight: defaultQueueWeight,
Girish Gowdra248971a2021-06-01 15:14:15 -07001088 DiscardPolicy: tp_pb.DiscardPolicy_TailDrop,
1089 DiscardConfigV2: &tp_pb.DiscardConfig{
1090 DiscardPolicy: tp_pb.DiscardPolicy_Red,
1091 DiscardConfig: &tp_pb.DiscardConfig_RedDiscardConfig{
1092 RedDiscardConfig: &tp_pb.RedDiscardConfig{
1093 MinThreshold: defaultMinThreshold,
1094 MaxThreshold: defaultMaxThreshold,
1095 MaxProbability: defaultMaxProbability,
1096 },
1097 },
1098 },
1099 DiscardConfig: &tp_pb.RedDiscardConfig{
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001100 MinThreshold: defaultMinThreshold,
1101 MaxThreshold: defaultMaxThreshold,
Girish Gowdra248971a2021-06-01 15:14:15 -07001102 MaxProbability: defaultMaxProbability,
1103 }})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001104 dsQueueAttributeList = append(dsQueueAttributeList,
Girish Gowdra248971a2021-06-01 15:14:15 -07001105 &tp_pb.EPONQueueAttributes{
1106 MaxQSize: defaultMaxQueueSize,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001107 PbitMap: pbit,
1108 AesEncryption: defaultAESEncryption,
Girish Gowdra248971a2021-06-01 15:14:15 -07001109 SchedulingPolicy: tp_pb.SchedulingPolicy_WRR,
1110 PriorityQ: defaultPriorityQueue,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001111 Weight: defaultQueueWeight,
Girish Gowdra248971a2021-06-01 15:14:15 -07001112 DiscardPolicy: tp_pb.DiscardPolicy_TailDrop,
1113 DiscardConfigV2: &tp_pb.DiscardConfig{
1114 DiscardPolicy: tp_pb.DiscardPolicy_Red,
1115 DiscardConfig: &tp_pb.DiscardConfig_RedDiscardConfig{
1116 RedDiscardConfig: &tp_pb.RedDiscardConfig{
1117 MinThreshold: defaultMinThreshold,
1118 MaxThreshold: defaultMaxThreshold,
1119 MaxProbability: defaultMaxProbability,
1120 },
1121 },
1122 },
1123 DiscardConfig: &tp_pb.RedDiscardConfig{
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001124 MinThreshold: defaultMinThreshold,
1125 MaxThreshold: defaultMaxThreshold,
Girish Gowdra248971a2021-06-01 15:14:15 -07001126 MaxProbability: defaultMaxProbability,
1127 }})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001128 }
Girish Gowdra248971a2021-06-01 15:14:15 -07001129 return &tp_pb.EponTechProfile{
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001130 Name: t.config.DefaultTPName,
1131 ProfileType: t.resourceMgr.GetTechnology(),
1132 Version: t.config.TPVersion,
1133 NumGemPorts: uint32(len(usQueueAttributeList)),
Girish Gowdra248971a2021-06-01 15:14:15 -07001134 InstanceControl: &tp_pb.InstanceControl{
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001135 Onu: defaultOnuInstance,
1136 Uni: defaultUniInstance,
1137 MaxGemPayloadSize: defaultGemPayloadSize},
Girish Gowdra248971a2021-06-01 15:14:15 -07001138 PackageType: defaultPakageType,
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001139 UpstreamQueueAttributeList: usQueueAttributeList,
1140 DownstreamQueueAttributeList: dsQueueAttributeList}
1141}
1142
Joey Armstrong7f8436c2023-07-09 20:23:27 -04001143// isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
Esin Karaman8aa75a72019-12-20 13:11:59 +00001144func isMulticastGem(isMulticastAttrValue string) bool {
1145 return isMulticastAttrValue != "" &&
1146 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1147}
1148
Girish Gowdra248971a2021-06-01 15:14:15 -07001149func (t *TechProfileMgr) addResourceInstanceToKVStore(ctx context.Context, tpID uint32, uniPortName string, resInst tp_pb.ResourceInstance) error {
1150 logger.Debugw(ctx, "adding-resource-instance-to-kv-store", log.Fields{"tpID": tpID, "uniPortName": uniPortName, "resInst": resInst})
1151 val, err := proto.Marshal(&resInst)
Matteo Scandolob85b2f02021-03-18 14:44:41 -07001152 if err != nil {
Girish Gowdra248971a2021-06-01 15:14:15 -07001153 logger.Errorw(ctx, "failed-to-marshall-resource-instance", log.Fields{"err": err, "tpID": tpID, "uniPortName": uniPortName, "resInst": resInst})
Matteo Scandolob85b2f02021-03-18 14:44:41 -07001154 return err
1155 }
Girish Gowdra248971a2021-06-01 15:14:15 -07001156 err = t.config.ResourceInstanceKVBacked.Put(ctx, fmt.Sprintf("%s/%d/%s", t.resourceMgr.GetTechnology(), tpID, uniPortName), val)
1157 return err
1158}
1159
1160func (t *TechProfileMgr) removeResourceInstanceFromKVStore(ctx context.Context, tpID uint32, uniPortName string) error {
1161 logger.Debugw(ctx, "removing-resource-instance-to-kv-store", log.Fields{"tpID": tpID, "uniPortName": uniPortName})
1162 if err := t.config.ResourceInstanceKVBacked.Delete(ctx, fmt.Sprintf("%s/%d/%s", t.resourceMgr.GetTechnology(), tpID, uniPortName)); err != nil {
1163 logger.Errorw(ctx, "error-removing-resource-instance-to-kv-store", log.Fields{"err": err, "tpID": tpID, "uniPortName": uniPortName})
1164 return err
1165 }
1166 return nil
1167}
1168
1169func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, tpID uint32) *tp_pb.TechProfile {
1170 var tp *tp_pb.TechProfile
1171 t.tpMapLock.RLock()
1172 tp, ok := t.tpMap[tpID]
1173 t.tpMapLock.RUnlock()
1174 if ok {
1175 logger.Debugw(ctx, "found-tp-in-cache", log.Fields{"tpID": tpID})
1176 return tp
1177 }
1178 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), tpID)
1179 logger.Debugw(ctx, "getting-tp-from-kv-store", log.Fields{"tpID": tpID, "Key": key})
1180 kvresult, err := t.config.DefaultTpKVBackend.Get(ctx, key)
1181 if err != nil {
1182 logger.Errorw(ctx, "error-fetching-from-kv-store", log.Fields{"err": err, "key": key})
1183 return nil
1184 }
1185 if kvresult != nil {
1186 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
1187 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
1188 lTp := &tp_pb.TechProfile{}
1189 reader := bytes.NewReader(value)
1190 if err = jsonpb.Unmarshal(reader, lTp); err != nil {
1191 logger.Errorw(ctx, "error-unmarshalling-tp-from-kv-store", log.Fields{"err": err, "tpID": tpID, "error": err})
1192 return nil
1193 }
1194
1195 logger.Debugw(ctx, "success-fetched-tp-from-kv-store", log.Fields{"tpID": tpID, "value": *lTp})
1196 return lTp
1197 } else {
1198 logger.Errorw(ctx, "error-decoding-tp", log.Fields{"err": err, "tpID": tpID})
1199 // We we create a default profile in this case.
1200 }
1201 }
1202
1203 return nil
1204}
1205
1206func (t *TechProfileMgr) getEponTPFromKVStore(ctx context.Context, tpID uint32) *tp_pb.EponTechProfile {
1207 var eponTp *tp_pb.EponTechProfile
1208 t.eponTpMapLock.RLock()
1209 eponTp, ok := t.eponTpMap[tpID]
1210 t.eponTpMapLock.RUnlock()
1211 if ok {
1212 logger.Debugw(ctx, "found-tp-in-cache", log.Fields{"tpID": tpID})
1213 return eponTp
1214 }
1215 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), tpID)
1216 logger.Debugw(ctx, "getting-epon-tp-from-kv-store", log.Fields{"tpID": tpID, "Key": key})
1217 kvresult, err := t.config.DefaultTpKVBackend.Get(ctx, key)
1218 if err != nil {
1219 logger.Errorw(ctx, "error-fetching-from-kv-store", log.Fields{"err": err, "key": key})
1220 return nil
1221 }
1222 if kvresult != nil {
1223 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
1224 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
1225 lEponTp := &tp_pb.EponTechProfile{}
1226 reader := bytes.NewReader(value)
1227 if err = jsonpb.Unmarshal(reader, lEponTp); err != nil {
1228 logger.Errorw(ctx, "error-unmarshalling-epon-tp-from-kv-store", log.Fields{"err": err, "tpID": tpID, "error": err})
1229 return nil
1230 }
1231
1232 logger.Debugw(ctx, "success-fetching-epon-tp-from-kv-store", log.Fields{"tpID": tpID, "value": *lEponTp})
1233 return lEponTp
1234 }
1235 }
1236 return nil
1237}
1238
1239func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
1240
1241 logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
1242 switch storeType {
1243 case "etcd":
1244 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
serkant.uluderyae5afeff2021-02-23 18:00:23 +03001245 case "redis":
1246 return kvstore.NewRedisClient(address, timeout, false)
1247 case "redis-sentinel":
1248 return kvstore.NewRedisClient(address, timeout, true)
Girish Gowdra248971a2021-06-01 15:14:15 -07001249 }
serkant.uluderyae5afeff2021-02-23 18:00:23 +03001250
Girish Gowdra248971a2021-06-01 15:14:15 -07001251 return nil, errors.New("unsupported-kv-store")
1252}
1253
1254// buildTpInstanceFromResourceInstance for GPON, XGPON and XGS-PON technology - build TpInstance from TechProfile template and ResourceInstance
1255func (t *TechProfileMgr) buildTpInstanceFromResourceInstance(ctx context.Context, tp *tp_pb.TechProfile, resInst *tp_pb.ResourceInstance) *tp_pb.TechProfileInstance {
1256
Girish Gowdra248971a2021-06-01 15:14:15 -07001257 if len(resInst.GemportIds) != int(tp.NumGemPorts) {
1258 logger.Errorw(ctx, "mismatch-in-number-of-gemports-between-template-and-resource-instance",
1259 log.Fields{"tpID": resInst.TpId, "totalResInstGemPortIDs": len(resInst.GemportIds), "totalTpTemplateGemPorts": tp.NumGemPorts})
1260 return nil
1261 }
Girish Gowdra248971a2021-06-01 15:14:15 -07001262
pnalmas23a77d02025-01-15 11:52:48 +05301263 usGemPortAttributeList := make([]*tp_pb.GemPortAttributes, 0, tp.NumGemPorts)
1264 dsGemPortAttributeList := make([]*tp_pb.GemPortAttributes, 0, tp.NumGemPorts)
1265 dsMulticastGemAttributeList := make([]*tp_pb.GemPortAttributes, 0)
1266 dsUnicastGemAttributeList := make([]*tp_pb.GemPortAttributes, 0)
1267
1268 logger.Debugw(ctx, "Building TP Instance",
1269 log.Fields{"tpID": resInst.TpId, "totalResInstGemPortIDs": len(resInst.GemportIds), "totalTpTemplateGemPorts": tp.NumGemPorts})
1270
1271 usGemPortAttributeList = t.buildUpstreamGemPortAttributes(ctx, tp, resInst, usGemPortAttributeList)
1272 dsUnicastGemAttributeList, dsMulticastGemAttributeList = t.separateDownstreamGemPortAttributes(ctx, tp, dsUnicastGemAttributeList, dsMulticastGemAttributeList)
1273 dsGemPortAttributeList = t.buildDownstreamGemPortAttributes(ctx, tp, resInst, dsUnicastGemAttributeList, dsMulticastGemAttributeList, dsGemPortAttributeList)
Girish Gowdra248971a2021-06-01 15:14:15 -07001274
1275 return &tp_pb.TechProfileInstance{
1276 SubscriberIdentifier: resInst.SubscriberIdentifier,
1277 Name: tp.Name,
1278 ProfileType: tp.ProfileType,
1279 Version: tp.Version,
1280 NumGemPorts: tp.NumGemPorts,
1281 InstanceControl: tp.InstanceControl,
1282 UsScheduler: &tp_pb.SchedulerAttributes{
1283 AllocId: resInst.AllocId,
1284 Direction: tp.UsScheduler.Direction,
1285 AdditionalBw: tp.UsScheduler.AdditionalBw,
1286 Priority: tp.UsScheduler.Priority,
1287 Weight: tp.UsScheduler.Weight,
1288 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
1289 DsScheduler: &tp_pb.SchedulerAttributes{
1290 AllocId: resInst.AllocId,
1291 Direction: tp.DsScheduler.Direction,
1292 AdditionalBw: tp.DsScheduler.AdditionalBw,
1293 Priority: tp.DsScheduler.Priority,
1294 Weight: tp.DsScheduler.Weight,
1295 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
1296 UpstreamGemPortAttributeList: usGemPortAttributeList,
1297 DownstreamGemPortAttributeList: dsGemPortAttributeList}
1298}
1299
1300// buildEponTpInstanceFromResourceInstance for EPON technology - build EponTpInstance from EponTechProfile template and ResourceInstance
1301func (t *TechProfileMgr) buildEponTpInstanceFromResourceInstance(ctx context.Context, tp *tp_pb.EponTechProfile, resInst *tp_pb.ResourceInstance) *tp_pb.EponTechProfileInstance {
1302
1303 var usQueueAttributeList []*tp_pb.EPONQueueAttributes
1304 var dsQueueAttributeList []*tp_pb.EPONQueueAttributes
1305
1306 if len(resInst.GemportIds) != int(tp.NumGemPorts) {
1307 logger.Errorw(ctx, "mismatch-in-number-of-gemports-between-epon-tp-template-and-resource-instance",
1308 log.Fields{"tpID": resInst.TpId, "totalResInstGemPortIDs": len(resInst.GemportIds), "totalTpTemplateGemPorts": tp.NumGemPorts})
1309 return nil
1310 }
1311
1312 for index := 0; index < int(tp.NumGemPorts); index++ {
1313 usQueueAttributeList = append(usQueueAttributeList,
1314 &tp_pb.EPONQueueAttributes{GemportId: resInst.GemportIds[index],
1315 MaxQSize: tp.UpstreamQueueAttributeList[index].MaxQSize,
1316 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
1317 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
1318 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
1319 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
1320 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
1321 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
1322 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
1323 NumQSets: tp.UpstreamQueueAttributeList[index].NumQSets,
1324 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
1325 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
1326 PriorityQ: tp.UpstreamQueueAttributeList[index].PriorityQ,
1327 Weight: tp.UpstreamQueueAttributeList[index].Weight,
1328 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
1329 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
1330 }
1331
1332 for index := 0; index < int(tp.NumGemPorts); index++ {
1333 dsQueueAttributeList = append(dsQueueAttributeList,
1334 &tp_pb.EPONQueueAttributes{GemportId: resInst.GemportIds[index],
1335 MaxQSize: tp.DownstreamQueueAttributeList[index].MaxQSize,
1336 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
1337 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
1338 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
1339 PriorityQ: tp.DownstreamQueueAttributeList[index].PriorityQ,
1340 Weight: tp.DownstreamQueueAttributeList[index].Weight,
1341 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
1342 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
1343 }
1344
1345 return &tp_pb.EponTechProfileInstance{
1346 SubscriberIdentifier: resInst.SubscriberIdentifier,
1347 Name: tp.Name,
1348 ProfileType: tp.ProfileType,
1349 Version: tp.Version,
1350 NumGemPorts: tp.NumGemPorts,
1351 InstanceControl: tp.InstanceControl,
1352 PackageType: tp.PackageType,
1353 AllocId: resInst.AllocId,
1354 UpstreamQueueAttributeList: usQueueAttributeList,
1355 DownstreamQueueAttributeList: dsQueueAttributeList}
1356}
1357
1358func (t *TechProfileMgr) getTpInstanceFromResourceInstance(ctx context.Context, resInst *tp_pb.ResourceInstance) *tp_pb.TechProfileInstance {
1359 if resInst == nil {
1360 logger.Error(ctx, "resource-instance-nil")
1361 return nil
1362 }
1363 tp := t.getTPFromKVStore(ctx, resInst.TpId)
1364 if tp == nil {
1365 logger.Warnw(ctx, "tp-not-found-on-kv--creating-default-tp", log.Fields{"tpID": resInst.TpId})
1366 tp = t.getDefaultTechProfile(ctx)
1367 }
1368 return t.buildTpInstanceFromResourceInstance(ctx, tp, resInst)
1369}
1370
1371func (t *TechProfileMgr) getEponTpInstanceFromResourceInstance(ctx context.Context, resInst *tp_pb.ResourceInstance) *tp_pb.EponTechProfileInstance {
1372 if resInst == nil {
1373 logger.Error(ctx, "resource-instance-nil")
1374 return nil
1375 }
1376 eponTp := t.getEponTPFromKVStore(ctx, resInst.TpId)
1377 if eponTp == nil {
1378 logger.Warnw(ctx, "tp-not-found-on-kv--creating-default-tp", log.Fields{"tpID": resInst.TpId})
1379 eponTp = t.getDefaultEponProfile(ctx)
1380 }
1381 return t.buildEponTpInstanceFromResourceInstance(ctx, eponTp, resInst)
1382}
1383
pnalmas23a77d02025-01-15 11:52:48 +05301384func (t *TechProfileMgr) reconcileTpInstancesToCache(ctx context.Context, IntfId uint32, deviceId string) error {
Girish Gowdra248971a2021-06-01 15:14:15 -07001385
1386 tech := t.resourceMgr.GetTechnology()
1387 newCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
1388 defer cancel()
Girish Gowdra248971a2021-06-01 15:14:15 -07001389
pnalmas23a77d02025-01-15 11:52:48 +05301390 //VOL-5417:Only reconcile the tech profiles for the subscribers associated with this PON port on this device.
1391 //Getting the list of supported tech profile IDs and then reconciling the tech profiles for these IDs associated
1392 //with the subscribers on this device for this PON port.
1393
1394 //Fetching the techprofile Keys from the KV Store.
1395 tpkeys, _ := t.config.DefaultTpKVBackend.GetWithPrefixKeysOnly(ctx, tech)
1396
1397 // Extract the techprofile Ids from the keys
1398 // The tpkeys will be of the format "service/voltha/technology_profiles/GPON/65"
1399 // where 65 is the techprofile Id
1400 tpIds := make([]uint32, 0)
1401
1402 for _, key := range tpkeys {
1403 parts := strings.Split(key, "/")
1404 // Ensure the key has the expected format
1405 if len(parts) < 5 {
1406 logger.Errorw(ctx, "Key does not match expected format", log.Fields{"key": key})
1407 continue
Girish Gowdra248971a2021-06-01 15:14:15 -07001408 }
pnalmas23a77d02025-01-15 11:52:48 +05301409 // Convert the last part of the key to uint32 (techprofile Id)
1410 tpId, err := strconv.Atoi(parts[len(parts)-1])
1411 if err != nil {
1412 logger.Errorw(ctx, "Error converting techprofile Id to int", log.Fields{"key": key, "error": err})
1413 continue
Girish Gowdra248971a2021-06-01 15:14:15 -07001414 }
pnalmas23a77d02025-01-15 11:52:48 +05301415 tpIds = append(tpIds, uint32(tpId))
Girish Gowdra248971a2021-06-01 15:14:15 -07001416 }
1417
pnalmas23a77d02025-01-15 11:52:48 +05301418 //for each tpid form a prefix and get the resource instance
1419 for _, tpId := range tpIds {
1420 prefix := fmt.Sprintf("%s/%d/olt-{%s}/pon-{%d}", tech, tpId, deviceId, IntfId)
1421 kvPairs, _ := t.config.ResourceInstanceKVBacked.GetWithPrefix(newCtx, prefix)
1422 //check if KvPairs is empty and if not then reconcile the techprofile instance
1423 if len(kvPairs) > 0 {
1424 for keyPath, kvPair := range kvPairs {
1425 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
1426 var resInst tp_pb.ResourceInstance
1427 if err = proto.Unmarshal(value, &resInst); err != nil {
1428 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"err": err, "keyPath": keyPath, "value": value})
1429 continue
1430 } else {
1431 if tech == xgspon || tech == xgpon || tech == gpon {
1432 if tpInst := t.getTpInstanceFromResourceInstance(ctx, &resInst); tpInst != nil {
1433 keySuffixSlice := regexp.MustCompile(t.config.ResourceInstanceKVPathPrefix+"/").Split(keyPath, 2)
1434 if len(keySuffixSlice) == 2 {
1435 keySuffixFormatRegexp := regexp.MustCompile(`^[a-zA-Z\-]+/[0-9]+/olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
1436 if !keySuffixFormatRegexp.Match([]byte(keySuffixSlice[1])) {
1437 logger.Errorw(ctx, "kv-path-not-confirming-to-format", log.Fields{"kvPath": keySuffixSlice[1]})
1438 continue
1439 }
1440 } else {
1441 logger.Errorw(ctx, "kv-instance-key-path-not-in-the-expected-format", log.Fields{"kvPath": keyPath})
1442 continue
1443 }
1444 t.tpInstanceMapLock.Lock()
1445 t.tpInstanceMap[keySuffixSlice[1]] = tpInst
1446 t.tpInstanceMapLock.Unlock()
1447 logger.Infow(ctx, "reconciled-tp-success", log.Fields{"keyPath": keyPath})
1448 }
1449 } else if tech == epon {
1450 if eponTpInst := t.getEponTpInstanceFromResourceInstance(ctx, &resInst); eponTpInst != nil {
1451 keySuffixSlice := regexp.MustCompile(t.config.ResourceInstanceKVPathPrefix+"/").Split(keyPath, 2)
1452 if len(keySuffixSlice) == 2 {
1453 keySuffixFormatRegexp := regexp.MustCompile(`^[a-zA-Z\-]+/[0-9]+/olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
1454 if !keySuffixFormatRegexp.Match([]byte(keySuffixSlice[1])) {
1455 logger.Errorw(ctx, "kv-path-not-confirming-to-format", log.Fields{"kvPath": keySuffixSlice[1]})
1456 continue
1457 }
1458 } else {
1459 logger.Errorw(ctx, "kv-instance-key-path-not-in-the-expected-format", log.Fields{"kvPath": keyPath})
1460 continue
1461 }
1462 t.epontpInstanceMapLock.Lock()
1463 t.eponTpInstanceMap[keySuffixSlice[1]] = eponTpInst
1464 t.epontpInstanceMapLock.Unlock()
1465 logger.Debugw(ctx, "reconciled-epon-tp-success", log.Fields{"keyPath": keyPath})
1466 }
1467 } else {
1468 logger.Errorw(ctx, "unknown-tech", log.Fields{"tech": tech})
1469 return fmt.Errorf("unknown-tech-%v", tech)
1470 }
1471 }
1472 } else {
1473 logger.Errorw(ctx, "error-converting-kv-pair-value-to-byte", log.Fields{"err": err})
1474 }
1475
1476 }
1477
1478 return nil
1479 }
1480 }
Matteo Scandolob85b2f02021-03-18 14:44:41 -07001481 return nil
1482}
pnalmas23a77d02025-01-15 11:52:48 +05301483
1484func (t *TechProfileMgr) buildUpstreamGemPortAttributes(ctx context.Context, tp *tp_pb.TechProfile, resInst *tp_pb.ResourceInstance, usGemPortAttributeList []*tp_pb.GemPortAttributes) []*tp_pb.GemPortAttributes {
1485 for index := 0; index < int(tp.NumGemPorts); index++ {
1486 usGemPortAttributeList = append(usGemPortAttributeList, &tp_pb.GemPortAttributes{
1487 GemportId: resInst.GemportIds[index],
1488 MaxQSize: tp.UpstreamGemPortAttributeList[index].MaxQSize,
1489 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
1490 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
1491 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
1492 PriorityQ: tp.UpstreamGemPortAttributeList[index].PriorityQ,
1493 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
1494 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
1495 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig,
1496 })
1497 }
1498 logger.Debugw(ctx, "Processed upstream GEM port attributes", log.Fields{"count": len(usGemPortAttributeList)})
1499 return usGemPortAttributeList
1500}
1501
1502func (t *TechProfileMgr) separateDownstreamGemPortAttributes(ctx context.Context, tp *tp_pb.TechProfile, dsUnicastGemAttributeList, dsMulticastGemAttributeList []*tp_pb.GemPortAttributes) ([]*tp_pb.GemPortAttributes, []*tp_pb.GemPortAttributes) {
1503 for _, attr := range tp.DownstreamGemPortAttributeList {
1504 if isMulticastGem(attr.IsMulticast) {
1505 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList, &tp_pb.GemPortAttributes{
1506 MulticastGemId: attr.MulticastGemId,
1507 MaxQSize: attr.MaxQSize,
1508 PbitMap: attr.PbitMap,
1509 AesEncryption: attr.AesEncryption,
1510 SchedulingPolicy: attr.SchedulingPolicy,
1511 PriorityQ: attr.PriorityQ,
1512 Weight: attr.Weight,
1513 DiscardPolicy: attr.DiscardPolicy,
1514 DiscardConfig: attr.DiscardConfig,
1515 IsMulticast: attr.IsMulticast,
1516 DynamicAccessControlList: attr.DynamicAccessControlList,
1517 StaticAccessControlList: attr.StaticAccessControlList,
1518 })
1519 } else {
1520 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList, &tp_pb.GemPortAttributes{
1521 MaxQSize: attr.MaxQSize,
1522 PbitMap: attr.PbitMap,
1523 AesEncryption: attr.AesEncryption,
1524 SchedulingPolicy: attr.SchedulingPolicy,
1525 PriorityQ: attr.PriorityQ,
1526 Weight: attr.Weight,
1527 DiscardPolicy: attr.DiscardPolicy,
1528 DiscardConfig: attr.DiscardConfig,
1529 })
1530 }
1531 }
1532 logger.Debugw(ctx, "Processed downstream GEM port attributes", log.Fields{
1533 "unicastCount": len(dsUnicastGemAttributeList), "multicastCount": len(dsMulticastGemAttributeList)})
1534 return dsUnicastGemAttributeList, dsMulticastGemAttributeList
1535}
1536
1537func (t *TechProfileMgr) buildDownstreamGemPortAttributes(ctx context.Context, tp *tp_pb.TechProfile, resInst *tp_pb.ResourceInstance, dsUnicastGemAttributeList, dsMulticastGemAttributeList, dsGemPortAttributeList []*tp_pb.GemPortAttributes) []*tp_pb.GemPortAttributes {
1538 for index := 0; index < int(tp.NumGemPorts) && index < len(dsUnicastGemAttributeList); index++ {
1539 dsGemPortAttributeList = append(dsGemPortAttributeList, &tp_pb.GemPortAttributes{
1540 GemportId: resInst.GemportIds[index],
1541 MaxQSize: dsUnicastGemAttributeList[index].MaxQSize,
1542 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
1543 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
1544 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
1545 PriorityQ: dsUnicastGemAttributeList[index].PriorityQ,
1546 Weight: dsUnicastGemAttributeList[index].Weight,
1547 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
1548 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig,
1549 })
1550 }
1551 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList...)
1552 logger.Debugw(ctx, "Processed downstream GEM port attributes for final list", log.Fields{"count": len(dsGemPortAttributeList)})
1553 return dsGemPortAttributeList
1554}