blob: b80f136136e4e8b57defff8563f2ca205b9e15ad [file] [log] [blame]
Girish Gowdra5d7d6442020-09-08 17:03:11 -07001/*
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
17package techprofile
18
19import (
20 "context"
21 "encoding/json"
22 "errors"
23 "fmt"
24 "regexp"
25 "strconv"
26 "sync"
27 "time"
28
29 "github.com/opencord/voltha-lib-go/v4/pkg/db"
30
31 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v4/pkg/log"
33 tp_pb "github.com/opencord/voltha-protos/v4/go/tech_profile"
34)
35
36// Interface to pon resource manager APIs
37type iPonResourceMgr interface {
38 GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
39 GetResourceTypeAllocID() string
40 GetResourceTypeGemPortID() string
41 GetTechnology() string
42}
43
44type Direction int32
45
46const (
47 Direction_UPSTREAM Direction = 0
48 Direction_DOWNSTREAM Direction = 1
49 Direction_BIDIRECTIONAL Direction = 2
50)
51
52var Direction_name = map[Direction]string{
53 0: "UPSTREAM",
54 1: "DOWNSTREAM",
55 2: "BIDIRECTIONAL",
56}
57
58type SchedulingPolicy int32
59
60const (
61 SchedulingPolicy_WRR SchedulingPolicy = 0
62 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
63 SchedulingPolicy_Hybrid SchedulingPolicy = 2
64)
65
66var SchedulingPolicy_name = map[SchedulingPolicy]string{
67 0: "WRR",
68 1: "StrictPriority",
69 2: "Hybrid",
70}
71
72type AdditionalBW int32
73
74const (
75 AdditionalBW_AdditionalBW_None AdditionalBW = 0
76 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
77 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
78 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
79)
80
81var AdditionalBW_name = map[AdditionalBW]string{
82 0: "AdditionalBW_None",
83 1: "AdditionalBW_NA",
84 2: "AdditionalBW_BestEffort",
85 3: "AdditionalBW_Auto",
86}
87
88type DiscardPolicy int32
89
90const (
91 DiscardPolicy_TailDrop DiscardPolicy = 0
92 DiscardPolicy_WTailDrop DiscardPolicy = 1
93 DiscardPolicy_Red DiscardPolicy = 2
94 DiscardPolicy_WRed DiscardPolicy = 3
95)
96
97var DiscardPolicy_name = map[DiscardPolicy]string{
98 0: "TailDrop",
99 1: "WTailDrop",
100 2: "Red",
101 3: "WRed",
102}
103
104// Required uniPortName format
105var uniPortNameFormat = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
106
107/*
108 type InferredAdditionBWIndication int32
109
110 const (
111 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
112 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
113 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
114 )
115
116 var InferredAdditionBWIndication_name = map[int32]string{
117 0: "InferredAdditionBWIndication_None",
118 1: "InferredAdditionBWIndication_Assured",
119 2: "InferredAdditionBWIndication_BestEffort",
120 }
121*/
122// instance control defaults
123const (
124 defaultOnuInstance = "multi-instance"
125 defaultUniInstance = "single-instance"
126 defaultGemPayloadSize = "auto"
127)
128
129const MAX_GEM_PAYLOAD = "max_gem_payload_size"
130
131type InstanceControl struct {
132 Onu string `json:"ONU"`
133 Uni string `json:"uni"`
134 MaxGemPayloadSize string `json:"max_gem_payload_size"`
135}
136
137// default discard config constants
138const (
139 defaultMinThreshold = 0
140 defaultMaxThreshold = 0
141 defaultMaxProbability = 0
142)
143
144type DiscardConfig struct {
145 MinThreshold int `json:"min_threshold"`
146 MaxThreshold int `json:"max_threshold"`
147 MaxProbability int `json:"max_probability"`
148}
149
150// default scheduler contants
151const (
152 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
153 defaultPriority = 0
154 defaultWeight = 0
155 defaultQueueSchedPolicy = SchedulingPolicy_Hybrid
156)
157
158type Scheduler struct {
159 Direction string `json:"direction"`
160 AdditionalBw string `json:"additional_bw"`
161 Priority uint32 `json:"priority"`
162 Weight uint32 `json:"weight"`
163 QSchedPolicy string `json:"q_sched_policy"`
164}
165
166// default GEM attribute constants
167const (
168 defaultAESEncryption = "True"
169 defaultPriorityQueue = 0
170 defaultQueueWeight = 0
171 defaultMaxQueueSize = "auto"
172 defaultdropPolicy = DiscardPolicy_TailDrop
173 defaultSchedulePolicy = SchedulingPolicy_WRR
174 defaultIsMulticast = "False"
175 defaultAccessControlList = "224.0.0.0-239.255.255.255"
176 defaultMcastGemID = 4069
177)
178
179type GemPortAttribute struct {
180 MaxQueueSize string `json:"max_q_size"`
181 PbitMap string `json:"pbit_map"`
182 AesEncryption string `json:"aes_encryption"`
183 SchedulingPolicy string `json:"scheduling_policy"`
184 PriorityQueue uint32 `json:"priority_q"`
185 Weight uint32 `json:"weight"`
186 DiscardPolicy string `json:"discard_policy"`
187 DiscardConfig DiscardConfig `json:"discard_config"`
188 IsMulticast string `json:"is_multicast"`
189 DControlList string `json:"dynamic_access_control_list"`
190 SControlList string `json:"static_access_control_list"`
191 McastGemID uint32 `json:"multicast_gem_id"`
192}
193
194// Instance of Scheduler
195type IScheduler struct {
196 AllocID uint32 `json:"alloc_id"`
197 Direction string `json:"direction"`
198 AdditionalBw string `json:"additional_bw"`
199 Priority uint32 `json:"priority"`
200 Weight uint32 `json:"weight"`
201 QSchedPolicy string `json:"q_sched_policy"`
202}
203
204// Instance of GemPortAttribute
205type IGemPortAttribute struct {
206 GemportID uint32 `json:"gemport_id"`
207 MaxQueueSize string `json:"max_q_size"`
208 PbitMap string `json:"pbit_map"`
209 AesEncryption string `json:"aes_encryption"`
210 SchedulingPolicy string `json:"scheduling_policy"`
211 PriorityQueue uint32 `json:"priority_q"`
212 Weight uint32 `json:"weight"`
213 DiscardPolicy string `json:"discard_policy"`
214 DiscardConfig DiscardConfig `json:"discard_config"`
215 IsMulticast string `json:"is_multicast"`
216 DControlList string `json:"dynamic_access_control_list"`
217 SControlList string `json:"static_access_control_list"`
218 McastGemID uint32 `json:"multicast_gem_id"`
219}
220
221type TechProfileMgr struct {
222 config *TechProfileFlags
223 resourceMgr iPonResourceMgr
224 GemPortIDMgmtLock sync.RWMutex
225 AllocIDMgmtLock sync.RWMutex
226}
227type DefaultTechProfile struct {
228 Name string `json:"name"`
229 ProfileType string `json:"profile_type"`
230 Version int `json:"version"`
231 NumGemPorts uint32 `json:"num_gem_ports"`
232 InstanceCtrl InstanceControl `json:"instance_control"`
233 UsScheduler Scheduler `json:"us_scheduler"`
234 DsScheduler Scheduler `json:"ds_scheduler"`
235 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
236 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
237}
238type TechProfile struct {
239 Name string `json:"name"`
240 SubscriberIdentifier string `json:"subscriber_identifier"`
241 ProfileType string `json:"profile_type"`
242 Version int `json:"version"`
243 NumGemPorts uint32 `json:"num_gem_ports"`
244 InstanceCtrl InstanceControl `json:"instance_control"`
245 UsScheduler IScheduler `json:"us_scheduler"`
246 DsScheduler IScheduler `json:"ds_scheduler"`
247 UpstreamGemPortAttributeList []IGemPortAttribute `json:"upstream_gem_port_attribute_list"`
248 DownstreamGemPortAttributeList []IGemPortAttribute `json:"downstream_gem_port_attribute_list"`
249}
250
251// QThresholds struct for EPON
252type QThresholds struct {
253 QThreshold1 uint32 `json:"q_threshold1"`
254 QThreshold2 uint32 `json:"q_threshold2"`
255 QThreshold3 uint32 `json:"q_threshold3"`
256 QThreshold4 uint32 `json:"q_threshold4"`
257 QThreshold5 uint32 `json:"q_threshold5"`
258 QThreshold6 uint32 `json:"q_threshold6"`
259 QThreshold7 uint32 `json:"q_threshold7"`
260}
261
262// UpstreamQueueAttribute struct for EPON
263type UpstreamQueueAttribute struct {
264 MaxQueueSize string `json:"max_q_size"`
265 PbitMap string `json:"pbit_map"`
266 AesEncryption string `json:"aes_encryption"`
267 TrafficType string `json:"traffic_type"`
268 UnsolicitedGrantSize uint32 `json:"unsolicited_grant_size"`
269 NominalInterval uint32 `json:"nominal_interval"`
270 ToleratedPollJitter uint32 `json:"tolerated_poll_jitter"`
271 RequestTransmissionPolicy uint32 `json:"request_transmission_policy"`
272 NumQueueSet uint32 `json:"num_q_sets"`
273 QThresholds QThresholds `json:"q_thresholds"`
274 SchedulingPolicy string `json:"scheduling_policy"`
275 PriorityQueue uint32 `json:"priority_q"`
276 Weight uint32 `json:"weight"`
277 DiscardPolicy string `json:"discard_policy"`
278 DiscardConfig DiscardConfig `json:"discard_config"`
279}
280
281// Default EPON constants
282const (
283 defaultPakageType = "B"
284)
285const (
286 defaultTrafficType = "BE"
287 defaultUnsolicitedGrantSize = 0
288 defaultNominalInterval = 0
289 defaultToleratedPollJitter = 0
290 defaultRequestTransmissionPolicy = 0
291 defaultNumQueueSet = 2
292)
293const (
294 defaultQThreshold1 = 5500
295 defaultQThreshold2 = 0
296 defaultQThreshold3 = 0
297 defaultQThreshold4 = 0
298 defaultQThreshold5 = 0
299 defaultQThreshold6 = 0
300 defaultQThreshold7 = 0
301)
302
303// DownstreamQueueAttribute struct for EPON
304type DownstreamQueueAttribute struct {
305 MaxQueueSize string `json:"max_q_size"`
306 PbitMap string `json:"pbit_map"`
307 AesEncryption string `json:"aes_encryption"`
308 SchedulingPolicy string `json:"scheduling_policy"`
309 PriorityQueue uint32 `json:"priority_q"`
310 Weight uint32 `json:"weight"`
311 DiscardPolicy string `json:"discard_policy"`
312 DiscardConfig DiscardConfig `json:"discard_config"`
313}
314
315// iUpstreamQueueAttribute struct for EPON
316type iUpstreamQueueAttribute struct {
317 GemportID uint32 `json:"q_id"`
318 MaxQueueSize string `json:"max_q_size"`
319 PbitMap string `json:"pbit_map"`
320 AesEncryption string `json:"aes_encryption"`
321 TrafficType string `json:"traffic_type"`
322 UnsolicitedGrantSize uint32 `json:"unsolicited_grant_size"`
323 NominalInterval uint32 `json:"nominal_interval"`
324 ToleratedPollJitter uint32 `json:"tolerated_poll_jitter"`
325 RequestTransmissionPolicy uint32 `json:"request_transmission_policy"`
326 NumQueueSet uint32 `json:"num_q_sets"`
327 QThresholds QThresholds `json:"q_thresholds"`
328 SchedulingPolicy string `json:"scheduling_policy"`
329 PriorityQueue uint32 `json:"priority_q"`
330 Weight uint32 `json:"weight"`
331 DiscardPolicy string `json:"discard_policy"`
332 DiscardConfig DiscardConfig `json:"discard_config"`
333}
334
335// iDownstreamQueueAttribute struct for EPON
336type iDownstreamQueueAttribute struct {
337 GemportID uint32 `json:"q_id"`
338 MaxQueueSize string `json:"max_q_size"`
339 PbitMap string `json:"pbit_map"`
340 AesEncryption string `json:"aes_encryption"`
341 SchedulingPolicy string `json:"scheduling_policy"`
342 PriorityQueue uint32 `json:"priority_q"`
343 Weight uint32 `json:"weight"`
344 DiscardPolicy string `json:"discard_policy"`
345 DiscardConfig DiscardConfig `json:"discard_config"`
346}
347
348// EponAttribute struct for EPON
349type EponAttribute struct {
350 PackageType string `json:"pakage_type"`
351}
352
353// DefaultTechProfile struct for EPON
354type DefaultEponProfile struct {
355 Name string `json:"name"`
356 ProfileType string `json:"profile_type"`
357 Version int `json:"version"`
358 NumGemPorts uint32 `json:"num_gem_ports"`
359 InstanceCtrl InstanceControl `json:"instance_control"`
360 EponAttribute EponAttribute `json:"epon_attribute"`
361 UpstreamQueueAttributeList []UpstreamQueueAttribute `json:"upstream_queue_attribute_list"`
362 DownstreamQueueAttributeList []DownstreamQueueAttribute `json:"downstream_queue_attribute_list"`
363}
364
365// TechProfile struct for EPON
366type EponProfile struct {
367 Name string `json:"name"`
368 SubscriberIdentifier string `json:"subscriber_identifier"`
369 ProfileType string `json:"profile_type"`
370 Version int `json:"version"`
371 NumGemPorts uint32 `json:"num_gem_ports"`
372 InstanceCtrl InstanceControl `json:"instance_control"`
373 EponAttribute EponAttribute `json:"epon_attribute"`
374 AllocID uint32 `json:"llid"`
375 UpstreamQueueAttributeList []iUpstreamQueueAttribute `json:"upstream_queue_attribute_list"`
376 DownstreamQueueAttributeList []iDownstreamQueueAttribute `json:"downstream_queue_attribute_list"`
377}
378
379const (
380 xgspon = "XGS-PON"
381 gpon = "GPON"
382 epon = "EPON"
383)
384
385func (t *TechProfileMgr) SetKVClient(ctx context.Context) *db.Backend {
386 kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
387 if err != nil {
388 logger.Errorw(ctx, "failed-to-create-kv-client",
389 log.Fields{
390 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
391 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
392 "error": err.Error(),
393 })
394 return nil
395 }
396 return &db.Backend{
397 Client: kvClient,
398 StoreType: t.config.KVStoreType,
399 Address: t.config.KVStoreAddress,
400 Timeout: t.config.KVStoreTimeout,
401 PathPrefix: t.config.TPKVPathPrefix}
402
403 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
404 issue between kv store and backend , core is not calling NewBackend directly
405 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
406 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
407 */
408}
409
410func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
411
412 logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
413 switch storeType {
414 case "consul":
415 return kvstore.NewConsulClient(ctx, address, timeout)
416 case "etcd":
417 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
418 }
419 return nil, errors.New("unsupported-kv-store")
420}
421
422func NewTechProfile(ctx context.Context, resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
423 var techprofileObj TechProfileMgr
424 logger.Debug(ctx, "Initializing techprofile Manager")
425 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
426 techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx)
427 if techprofileObj.config.KVBackend == nil {
428 logger.Error(ctx, "Failed to initialize KV backend\n")
429 return nil, errors.New("KV backend init failed")
430 }
431 techprofileObj.resourceMgr = resourceMgr
432 logger.Debug(ctx, "Initializing techprofile object instance success")
433 return &techprofileObj, nil
434}
435
436func (t *TechProfileMgr) GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string {
437 logger.Debugw(ctx, "get-tp-instance-kv-path", log.Fields{
438 "uniPortName": uniPortName,
439 "tpId": techProfiletblID,
440 })
441 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
442}
443
444func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (interface{}, error) {
445 var err error
446 var kvResult *kvstore.KVPair
447 var KvTpIns TechProfile
448 var KvEponIns EponProfile
449 var resPtr interface{}
450 // For example:
451 // tpInstPath like "XGS-PON/64/uni_port_name"
452 // is broken into ["XGS-PON" "64" ...]
453 pathSlice := regexp.MustCompile(`/`).Split(path, -1)
454 switch pathSlice[0] {
455 case xgspon, gpon:
456 resPtr = &KvTpIns
457 case epon:
458 resPtr = &KvEponIns
459 default:
460 logger.Errorw(ctx, "unknown-tech", log.Fields{"tech": pathSlice[0]})
461 return nil, fmt.Errorf("unknown-tech-%s", pathSlice[0])
462 }
463
464 kvResult, _ = t.config.KVBackend.Get(ctx, path)
465 if kvResult == nil {
466 logger.Infow(ctx, "tp-instance-not-found-on-kv", log.Fields{"key": path})
467 return nil, nil
468 } else {
469 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
470 if err = json.Unmarshal(value, resPtr); err != nil {
471 logger.Errorw(ctx, "error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
472 return nil, errors.New("error-unmarshal-kv-result")
473 } else {
474 return resPtr, nil
475 }
476 }
477 }
478 return nil, err
479}
480
481func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
482 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
483 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
484 tpInstanceJson, err := json.Marshal(*tpInstance)
485 if err == nil {
486 // Backend will convert JSON byte array into string format
487 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
488 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
489 } else {
490 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
491 }
492 return err
493}
494
495func (t *TechProfileMgr) addEponProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *EponProfile) error {
496 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
497 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
498 tpInstanceJson, err := json.Marshal(*tpInstance)
499 if err == nil {
500 // Backend will convert JSON byte array into string format
501 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
502 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
503 } else {
504 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
505 }
506 return err
507}
508
509func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
510 var kvtechprofile DefaultTechProfile
511 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
512 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
513 kvresult, err := t.config.KVBackend.Get(ctx, key)
514 if err != nil {
515 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
516 return nil
517 }
518 if kvresult != nil {
519 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
520 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
521 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
522 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
523 return nil
524 }
525
526 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
527 return &kvtechprofile
528 }
529 }
530 return nil
531}
532
533func (t *TechProfileMgr) getEponTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultEponProfile {
534 var kvtechprofile DefaultEponProfile
535 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
536 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
537 kvresult, err := t.config.KVBackend.Get(ctx, key)
538 if err != nil {
539 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
540 return nil
541 }
542 if kvresult != nil {
543 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
544 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
545 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
546 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
547 return nil
548 }
549
550 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
551 return &kvtechprofile
552 }
553 }
554 return nil
555}
556
557func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (interface{}, error) {
558 var tpInstance *TechProfile
559 var tpEponInstance *EponProfile
560
561 logger.Infow(ctx, "creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
562
563 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
564 if !uniPortNameFormat.Match([]byte(uniPortName)) {
565 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
566 return nil, errors.New("uni-port-name-not-confirming-to-format")
567 }
568 tpInstancePath := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
569 // For example:
570 // tpInstPath like "XGS-PON/64/uni_port_name"
571 // is broken into ["XGS-PON" "64" ...]
572 pathSlice := regexp.MustCompile(`/`).Split(tpInstancePath, -1)
573 if pathSlice[0] == epon {
574 tp := t.getEponTPFromKVStore(ctx, techProfiletblID)
575 if tp != nil {
576 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
577 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
578 tp = t.getDefaultEponProfile(ctx)
579 } else {
580 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
581 }
582 } else {
583 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
584 tp = t.getDefaultEponProfile(ctx)
585 }
586
587 if tpEponInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpEponInstance == nil {
588 logger.Error(ctx, "tp-intance-allocation-failed")
589 return nil, errors.New("tp-intance-allocation-failed")
590 }
591 if err := t.addEponProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpEponInstance); err != nil {
592 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
593 return nil, errors.New("error-adding-tp-to-kv-store")
594 }
595 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
596 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
597 return tpEponInstance, nil
598 } else {
599 tp := t.getTPFromKVStore(ctx, techProfiletblID)
600 if tp != nil {
601 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
602 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
603 tp = t.getDefaultTechProfile(ctx)
604 } else {
605 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
606 }
607 } else {
608 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
609 tp = t.getDefaultTechProfile(ctx)
610 }
611
612 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
613 logger.Error(ctx, "tp-intance-allocation-failed")
614 return nil, errors.New("tp-intance-allocation-failed")
615 }
616 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
617 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
618 return nil, errors.New("error-adding-tp-to-kv-store")
619 }
620 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
621 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
622 return tpInstance, nil
623 }
624}
625
626func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
627 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
628 return t.config.KVBackend.Delete(ctx, path)
629}
630
631func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl InstanceControl) error {
632 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
633 logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
634 return errors.New("invalid-onu-instance-ctl-attr")
635 }
636
637 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
638 logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
639 return errors.New("invalid-uni-instance-ctl-attr")
640 }
641
642 if instCtl.Uni == "multi-instance" {
643 logger.Error(ctx, "uni-multi-instance-tp-not-supported")
644 return errors.New("uni-multi-instance-tp-not-supported")
645 }
646
647 return nil
648}
649
650func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
651
652 var usGemPortAttributeList []IGemPortAttribute
653 var dsGemPortAttributeList []IGemPortAttribute
654 var dsMulticastGemAttributeList []IGemPortAttribute
655 var dsUnicastGemAttributeList []IGemPortAttribute
656 var tcontIDs []uint32
657 var gemPorts []uint32
658 var err error
659
660 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
661
662 if tp.InstanceCtrl.Onu == "multi-instance" {
663 t.AllocIDMgmtLock.Lock()
664 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
665 t.AllocIDMgmtLock.Unlock()
666 if err != nil {
667 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
668 return nil
669 }
670 } else { // "single-instance"
671 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
672 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
673 return nil
674 } else if tpInst == nil {
675 // No "single-instance" tp found on one any uni port for the given TP ID
676 // Allocate a new TcontID or AllocID
677 t.AllocIDMgmtLock.Lock()
678 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
679 t.AllocIDMgmtLock.Unlock()
680 if err != nil {
681 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
682 return nil
683 }
684 } else {
685 // Use the alloc-id from the existing TpInstance
686 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
687 }
688 }
689 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
690 t.GemPortIDMgmtLock.Lock()
691 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
692 t.GemPortIDMgmtLock.Unlock()
693 if err != nil {
694 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
695 return nil
696 }
697 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
698 for index := 0; index < int(tp.NumGemPorts); index++ {
699 usGemPortAttributeList = append(usGemPortAttributeList,
700 IGemPortAttribute{GemportID: gemPorts[index],
701 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
702 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
703 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
704 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
705 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
706 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
707 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
708 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
709 }
710
711 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
712 //put multicast and unicast downstream GEM port attributes in different lists first
713 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
714 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
715 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
716 IGemPortAttribute{
717 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
718 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
719 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
720 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
721 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
722 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
723 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
724 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
725 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
726 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
727 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
728 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
729 } else {
730 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
731 IGemPortAttribute{
732 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
733 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
734 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
735 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
736 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
737 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
738 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
739 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
740 }
741 }
742 //add unicast downstream GEM ports to dsGemPortAttributeList
743 for index := 0; index < int(tp.NumGemPorts); index++ {
744 dsGemPortAttributeList = append(dsGemPortAttributeList,
745 IGemPortAttribute{GemportID: gemPorts[index],
746 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
747 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
748 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
749 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
750 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
751 Weight: dsUnicastGemAttributeList[index].Weight,
752 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
753 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
754 }
755 //add multicast GEM ports to dsGemPortAttributeList afterwards
756 for k := range dsMulticastGemAttributeList {
757 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
758 }
759
760 return &TechProfile{
761 SubscriberIdentifier: uniPortName,
762 Name: tp.Name,
763 ProfileType: tp.ProfileType,
764 Version: tp.Version,
765 NumGemPorts: tp.NumGemPorts,
766 InstanceCtrl: tp.InstanceCtrl,
767 UsScheduler: IScheduler{
768 AllocID: tcontIDs[0],
769 Direction: tp.UsScheduler.Direction,
770 AdditionalBw: tp.UsScheduler.AdditionalBw,
771 Priority: tp.UsScheduler.Priority,
772 Weight: tp.UsScheduler.Weight,
773 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
774 DsScheduler: IScheduler{
775 AllocID: tcontIDs[0],
776 Direction: tp.DsScheduler.Direction,
777 AdditionalBw: tp.DsScheduler.AdditionalBw,
778 Priority: tp.DsScheduler.Priority,
779 Weight: tp.DsScheduler.Weight,
780 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
781 UpstreamGemPortAttributeList: usGemPortAttributeList,
782 DownstreamGemPortAttributeList: dsGemPortAttributeList}
783}
784
785// allocateTPInstance function for EPON
786func (t *TechProfileMgr) allocateEponTPInstance(ctx context.Context, uniPortName string, tp *DefaultEponProfile, intfId uint32, tpInstPath string) *EponProfile {
787
788 var usQueueAttributeList []iUpstreamQueueAttribute
789 var dsQueueAttributeList []iDownstreamQueueAttribute
790 var tcontIDs []uint32
791 var gemPorts []uint32
792 var err error
793
794 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
795
796 if tp.InstanceCtrl.Onu == "multi-instance" {
797 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
798 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
799 return nil
800 }
801 } else { // "single-instance"
802 if tpInst, err := t.getSingleInstanceEponTp(ctx, tpInstPath); err != nil {
803 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
804 return nil
805 } else if tpInst == nil {
806 // No "single-instance" tp found on one any uni port for the given TP ID
807 // Allocate a new TcontID or AllocID
808 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
809 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
810 return nil
811 }
812 } else {
813 // Use the alloc-id from the existing TpInstance
814 tcontIDs = append(tcontIDs, tpInst.AllocID)
815 }
816 }
817 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
818 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
819 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
820 return nil
821 }
822 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
823 for index := 0; index < int(tp.NumGemPorts); index++ {
824 usQueueAttributeList = append(usQueueAttributeList,
825 iUpstreamQueueAttribute{GemportID: gemPorts[index],
826 MaxQueueSize: tp.UpstreamQueueAttributeList[index].MaxQueueSize,
827 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
828 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
829 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
830 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
831 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
832 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
833 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
834 NumQueueSet: tp.UpstreamQueueAttributeList[index].NumQueueSet,
835 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
836 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
837 PriorityQueue: tp.UpstreamQueueAttributeList[index].PriorityQueue,
838 Weight: tp.UpstreamQueueAttributeList[index].Weight,
839 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
840 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
841 }
842
843 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamQueueAttributeList))
844 for index := 0; index < int(tp.NumGemPorts); index++ {
845 dsQueueAttributeList = append(dsQueueAttributeList,
846 iDownstreamQueueAttribute{GemportID: gemPorts[index],
847 MaxQueueSize: tp.DownstreamQueueAttributeList[index].MaxQueueSize,
848 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
849 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
850 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
851 PriorityQueue: tp.DownstreamQueueAttributeList[index].PriorityQueue,
852 Weight: tp.DownstreamQueueAttributeList[index].Weight,
853 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
854 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
855 }
856
857 return &EponProfile{
858 SubscriberIdentifier: uniPortName,
859 Name: tp.Name,
860 ProfileType: tp.ProfileType,
861 Version: tp.Version,
862 NumGemPorts: tp.NumGemPorts,
863 InstanceCtrl: tp.InstanceCtrl,
864 EponAttribute: tp.EponAttribute,
865 AllocID: tcontIDs[0],
866 UpstreamQueueAttributeList: usQueueAttributeList,
867 DownstreamQueueAttributeList: dsQueueAttributeList}
868}
869
870// getSingleInstanceTp returns another TpInstance for an ONU on a different
871// uni port for the same TP ID, if it finds one, else nil.
872func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
873 var tpInst TechProfile
874
875 // For example:
876 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
877 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
878 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
879 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
880
881 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
882 for keyPath, kvPair := range kvPairs {
883 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
884 if err = json.Unmarshal(value, &tpInst); err != nil {
885 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
886 return nil, errors.New("error-unmarshal-kv-pair")
887 } else {
888 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
889 return &tpInst, nil
890 }
891 }
892 }
893 return nil, nil
894}
895
896func (t *TechProfileMgr) getSingleInstanceEponTp(ctx context.Context, tpPath string) (*EponProfile, error) {
897 var tpInst EponProfile
898
899 // For example:
900 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
901 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
902 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
903 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
904
905 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
906 for keyPath, kvPair := range kvPairs {
907 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
908 if err = json.Unmarshal(value, &tpInst); err != nil {
909 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
910 return nil, errors.New("error-unmarshal-kv-pair")
911 } else {
912 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
913 return &tpInst, nil
914 }
915 }
916 }
917 return nil, nil
918}
919
920func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *DefaultTechProfile {
921 var usGemPortAttributeList []GemPortAttribute
922 var dsGemPortAttributeList []GemPortAttribute
923
924 for _, pbit := range t.config.DefaultPbits {
925 logger.Debugw(ctx, "Creating GEM port", log.Fields{"pbit": pbit})
926 usGemPortAttributeList = append(usGemPortAttributeList,
927 GemPortAttribute{
928 MaxQueueSize: defaultMaxQueueSize,
929 PbitMap: pbit,
930 AesEncryption: defaultAESEncryption,
931 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
932 PriorityQueue: defaultPriorityQueue,
933 Weight: defaultQueueWeight,
934 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
935 DiscardConfig: DiscardConfig{
936 MinThreshold: defaultMinThreshold,
937 MaxThreshold: defaultMaxThreshold,
938 MaxProbability: defaultMaxProbability}})
939 dsGemPortAttributeList = append(dsGemPortAttributeList,
940 GemPortAttribute{
941 MaxQueueSize: defaultMaxQueueSize,
942 PbitMap: pbit,
943 AesEncryption: defaultAESEncryption,
944 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
945 PriorityQueue: defaultPriorityQueue,
946 Weight: defaultQueueWeight,
947 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
948 DiscardConfig: DiscardConfig{
949 MinThreshold: defaultMinThreshold,
950 MaxThreshold: defaultMaxThreshold,
951 MaxProbability: defaultMaxProbability},
952 IsMulticast: defaultIsMulticast,
953 DControlList: defaultAccessControlList,
954 SControlList: defaultAccessControlList,
955 McastGemID: defaultMcastGemID})
956 }
957 return &DefaultTechProfile{
958 Name: t.config.DefaultTPName,
959 ProfileType: t.resourceMgr.GetTechnology(),
960 Version: t.config.TPVersion,
961 NumGemPorts: uint32(len(usGemPortAttributeList)),
962 InstanceCtrl: InstanceControl{
963 Onu: defaultOnuInstance,
964 Uni: defaultUniInstance,
965 MaxGemPayloadSize: defaultGemPayloadSize},
966 UsScheduler: Scheduler{
967 Direction: Direction_name[Direction_UPSTREAM],
968 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
969 Priority: defaultPriority,
970 Weight: defaultWeight,
971 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
972 DsScheduler: Scheduler{
973 Direction: Direction_name[Direction_DOWNSTREAM],
974 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
975 Priority: defaultPriority,
976 Weight: defaultWeight,
977 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
978 UpstreamGemPortAttributeList: usGemPortAttributeList,
979 DownstreamGemPortAttributeList: dsGemPortAttributeList}
980}
981
982// getDefaultTechProfile function for EPON
983func (t *TechProfileMgr) getDefaultEponProfile(ctx context.Context) *DefaultEponProfile {
984
985 var usQueueAttributeList []UpstreamQueueAttribute
986 var dsQueueAttributeList []DownstreamQueueAttribute
987
988 for _, pbit := range t.config.DefaultPbits {
989 logger.Debugw(ctx, "Creating Queue", log.Fields{"pbit": pbit})
990 usQueueAttributeList = append(usQueueAttributeList,
991 UpstreamQueueAttribute{
992 MaxQueueSize: defaultMaxQueueSize,
993 PbitMap: pbit,
994 AesEncryption: defaultAESEncryption,
995 TrafficType: defaultTrafficType,
996 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
997 NominalInterval: defaultNominalInterval,
998 ToleratedPollJitter: defaultToleratedPollJitter,
999 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
1000 NumQueueSet: defaultNumQueueSet,
1001 QThresholds: QThresholds{
1002 QThreshold1: defaultQThreshold1,
1003 QThreshold2: defaultQThreshold2,
1004 QThreshold3: defaultQThreshold3,
1005 QThreshold4: defaultQThreshold4,
1006 QThreshold5: defaultQThreshold5,
1007 QThreshold6: defaultQThreshold6,
1008 QThreshold7: defaultQThreshold7},
1009 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1010 PriorityQueue: defaultPriorityQueue,
1011 Weight: defaultQueueWeight,
1012 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1013 DiscardConfig: DiscardConfig{
1014 MinThreshold: defaultMinThreshold,
1015 MaxThreshold: defaultMaxThreshold,
1016 MaxProbability: defaultMaxProbability}})
1017 dsQueueAttributeList = append(dsQueueAttributeList,
1018 DownstreamQueueAttribute{
1019 MaxQueueSize: defaultMaxQueueSize,
1020 PbitMap: pbit,
1021 AesEncryption: defaultAESEncryption,
1022 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1023 PriorityQueue: defaultPriorityQueue,
1024 Weight: defaultQueueWeight,
1025 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1026 DiscardConfig: DiscardConfig{
1027 MinThreshold: defaultMinThreshold,
1028 MaxThreshold: defaultMaxThreshold,
1029 MaxProbability: defaultMaxProbability}})
1030 }
1031 return &DefaultEponProfile{
1032 Name: t.config.DefaultTPName,
1033 ProfileType: t.resourceMgr.GetTechnology(),
1034 Version: t.config.TPVersion,
1035 NumGemPorts: uint32(len(usQueueAttributeList)),
1036 InstanceCtrl: InstanceControl{
1037 Onu: defaultOnuInstance,
1038 Uni: defaultUniInstance,
1039 MaxGemPayloadSize: defaultGemPayloadSize},
1040 EponAttribute: EponAttribute{
1041 PackageType: defaultPakageType},
1042 UpstreamQueueAttributeList: usQueueAttributeList,
1043 DownstreamQueueAttributeList: dsQueueAttributeList}
1044}
1045
1046func (t *TechProfileMgr) GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32 {
1047 var result int32 = -1
1048
1049 if paramType == "direction" {
1050 for key, val := range tp_pb.Direction_value {
1051 if key == paramKey {
1052 result = val
1053 }
1054 }
1055 } else if paramType == "discard_policy" {
1056 for key, val := range tp_pb.DiscardPolicy_value {
1057 if key == paramKey {
1058 result = val
1059 }
1060 }
1061 } else if paramType == "sched_policy" {
1062 for key, val := range tp_pb.SchedulingPolicy_value {
1063 if key == paramKey {
1064 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": key, "value": val})
1065 result = val
1066 }
1067 }
1068 } else if paramType == "additional_bw" {
1069 for key, val := range tp_pb.AdditionalBW_value {
1070 if key == paramKey {
1071 result = val
1072 }
1073 }
1074 } else {
1075 logger.Error(ctx, "Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
1076 return -1
1077 }
1078 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": paramKey, "value": result})
1079 return result
1080}
1081
1082func (t *TechProfileMgr) GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
1083 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.UsScheduler.Direction))
1084 if dir == -1 {
1085 logger.Errorf(ctx, "Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
1086 return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
1087 }
1088
1089 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.UsScheduler.AdditionalBw))
1090 if bw == -1 {
1091 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
1092 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
1093 }
1094
1095 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.UsScheduler.QSchedPolicy))
1096 if policy == -1 {
1097 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
1098 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
1099 }
1100
1101 return &tp_pb.SchedulerConfig{
1102 Direction: dir,
1103 AdditionalBw: bw,
1104 Priority: tpInstance.UsScheduler.Priority,
1105 Weight: tpInstance.UsScheduler.Weight,
1106 SchedPolicy: policy}, nil
1107}
1108
1109func (t *TechProfileMgr) GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
1110
1111 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.DsScheduler.Direction))
1112 if dir == -1 {
1113 logger.Errorf(ctx, "Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
1114 return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
1115 }
1116
1117 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.DsScheduler.AdditionalBw))
1118 if bw == -1 {
1119 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
1120 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
1121 }
1122
1123 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.DsScheduler.QSchedPolicy))
1124 if policy == -1 {
1125 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
1126 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
1127 }
1128
1129 return &tp_pb.SchedulerConfig{
1130 Direction: dir,
1131 AdditionalBw: bw,
1132 Priority: tpInstance.DsScheduler.Priority,
1133 Weight: tpInstance.DsScheduler.Weight,
1134 SchedPolicy: policy}, nil
1135}
1136
1137func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
1138 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
1139
1140 tSched := &tp_pb.TrafficScheduler{
1141 Direction: SchedCfg.Direction,
1142 AllocId: tpInstance.UsScheduler.AllocID,
1143 TrafficShapingInfo: ShapingCfg,
1144 Scheduler: SchedCfg}
1145
1146 return tSched
1147}
1148
1149func (tpm *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
1150
1151 var encryp bool
1152 if Dir == tp_pb.Direction_UPSTREAM {
1153 // upstream GEM ports
1154 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
1155 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1156 for Count := 0; Count < NumGemPorts; Count++ {
1157 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
1158 encryp = true
1159 } else {
1160 encryp = false
1161 }
1162
1163 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1164 if schedPolicy == -1 {
1165 logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
1166 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1167 }
1168
1169 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1170 if discardPolicy == -1 {
1171 logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
1172 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1173 }
1174
1175 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
1176 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.UsScheduler.Direction)),
1177 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
1178 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
1179 AesEncryption: encryp,
1180 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
1181 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
1182 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
1183 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
1184 })
1185 }
1186 logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
1187 return GemPorts, nil
1188 } else if Dir == tp_pb.Direction_DOWNSTREAM {
1189 //downstream GEM ports
1190 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1191 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1192 for Count := 0; Count < NumGemPorts; Count++ {
1193 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1194 //do not take multicast GEM ports. They are handled separately.
1195 continue
1196 }
1197 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1198 encryp = true
1199 } else {
1200 encryp = false
1201 }
1202
1203 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1204 if schedPolicy == -1 {
1205 logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
1206 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1207 }
1208
1209 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1210 if discardPolicy == -1 {
1211 logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
1212 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1213 }
1214
1215 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
1216 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
1217 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
1218 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1219 AesEncryption: encryp,
1220 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
1221 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1222 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
1223 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
1224 })
1225 }
1226 logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
1227 return GemPorts, nil
1228 }
1229
1230 logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", Dir)
1231 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
1232}
1233
1234//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
1235func isMulticastGem(isMulticastAttrValue string) bool {
1236 return isMulticastAttrValue != "" &&
1237 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1238}
1239
1240func (tpm *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue {
1241 var encryp bool
1242 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1243 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
1244 for Count := 0; Count < NumGemPorts; Count++ {
1245 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1246 continue
1247 }
1248 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1249 encryp = true
1250 } else {
1251 encryp = false
1252 }
1253 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
1254 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
1255 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
1256 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1257 AesEncryption: encryp,
1258 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
1259 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1260 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
1261 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
1262 })
1263 }
1264 logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
1265 return mcastTrafficQueues
1266}
1267
1268func (tpm *TechProfileMgr) GetUsTrafficScheduler(ctx context.Context, tp *TechProfile) *tp_pb.TrafficScheduler {
1269 UsScheduler, _ := tpm.GetUsScheduler(ctx, tp)
1270
1271 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
1272 AllocId: tp.UsScheduler.AllocID,
1273 Scheduler: UsScheduler}
1274}
1275
1276func (t *TechProfileMgr) GetGemportIDForPbit(ctx context.Context, tp interface{}, dir tp_pb.Direction, pbit uint32) uint32 {
1277 /*
1278 Function to get the Gemport ID mapped to a pbit.
1279 */
1280 switch tp := tp.(type) {
1281 case *TechProfile:
1282 if dir == tp_pb.Direction_UPSTREAM {
1283 // upstream GEM ports
1284 numGemPorts := len(tp.UpstreamGemPortAttributeList)
1285 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1286 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
1287 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1288 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1289 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1290 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1291 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1292 logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
1293 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
1294 }
1295 }
1296 }
1297 }
1298 } else if dir == tp_pb.Direction_DOWNSTREAM {
1299 //downstream GEM ports
1300 numGemPorts := len(tp.DownstreamGemPortAttributeList)
1301 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1302 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
1303 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1304 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1305 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1306 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1307 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1308 logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
1309 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
1310 }
1311 }
1312 }
1313 }
1314 }
1315 logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
1316 case *EponProfile:
1317 if dir == tp_pb.Direction_UPSTREAM {
1318 // upstream GEM ports
1319 numGemPorts := len(tp.UpstreamQueueAttributeList)
1320 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1321 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
1322 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1323 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1324 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1325 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1326 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1327 logger.Debugw(ctx, "Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportID})
1328 return tp.UpstreamQueueAttributeList[gemCnt].GemportID
1329 }
1330 }
1331 }
1332 }
1333 } else if dir == tp_pb.Direction_DOWNSTREAM {
1334 //downstream GEM ports
1335 numGemPorts := len(tp.DownstreamQueueAttributeList)
1336 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1337 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
1338 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1339 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1340 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1341 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1342 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1343 logger.Debugw(ctx, "Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportID})
1344 return tp.DownstreamQueueAttributeList[gemCnt].GemportID
1345 }
1346 }
1347 }
1348 }
1349 }
1350 logger.Errorw(ctx, "No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
1351 default:
1352 logger.Errorw(ctx, "unknown-tech", log.Fields{"tp": tp})
1353 }
1354 return 0
1355}
1356
1357// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
1358func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) interface{} {
1359 var tpTech TechProfile
1360 var tpEpon EponProfile
1361
1362 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
1363
1364 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
1365 tech := t.resourceMgr.GetTechnology()
1366 tpInstancesTech := make([]TechProfile, 0, len(kvPairs))
1367 tpInstancesEpon := make([]EponProfile, 0, len(kvPairs))
1368
1369 for kvPath, kvPair := range kvPairs {
1370 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
1371 if tech == xgspon || tech == gpon {
1372 if err = json.Unmarshal(value, &tpTech); err != nil {
1373 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
1374 continue
1375 } else {
1376 tpInstancesTech = append(tpInstancesTech, tpTech)
1377 }
1378 } else if tech == epon {
1379 if err = json.Unmarshal(value, &tpEpon); err != nil {
1380 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
1381 continue
1382 } else {
1383 tpInstancesEpon = append(tpInstancesEpon, tpEpon)
1384 }
1385 }
1386 }
1387 }
1388
1389 switch tech {
1390 case xgspon, gpon:
1391 return tpInstancesTech
1392 case epon:
1393 return tpInstancesEpon
1394 default:
1395 logger.Errorw(ctx, "unknown-technology", log.Fields{"tech": tech})
1396 return nil
1397 }
1398 }
1399 return nil
1400}