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