blob: e21de4520e262d623e7fe7336a36546fa23d0113 [file] [log] [blame]
mpagenkoaf801632020-07-03 10:00:42 +00001/*
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
dbainbri4d3a0dc2020-12-02 00:33:42 +000029 "github.com/opencord/voltha-lib-go/v4/pkg/db"
mpagenkoaf801632020-07-03 10:00:42 +000030
dbainbri4d3a0dc2020-12-02 00:33:42 +000031 "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"
mpagenkoaf801632020-07-03 10:00:42 +000034)
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
dbainbri4d3a0dc2020-12-02 00:33:42 +0000194// Instance of Scheduler
195type IScheduler struct {
mpagenkoaf801632020-07-03 10:00:42 +0000196 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}
dbainbri4d3a0dc2020-12-02 00:33:42 +0000203
204// Instance of GemPortAttribute
205type IGemPortAttribute struct {
mpagenkoaf801632020-07-03 10:00:42 +0000206 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"`
dbainbri4d3a0dc2020-12-02 00:33:42 +0000245 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"`
mpagenkoaf801632020-07-03 10:00:42 +0000249}
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
dbainbri4d3a0dc2020-12-02 00:33:42 +0000385func (t *TechProfileMgr) SetKVClient(ctx context.Context, pathPrefix string) *db.Backend {
386 kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
mpagenkoaf801632020-07-03 10:00:42 +0000387 if err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000388 logger.Errorw(ctx, "failed-to-create-kv-client",
mpagenkoaf801632020-07-03 10:00:42 +0000389 log.Fields{
390 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000391 "timeout": t.config.KVStoreTimeout, "prefix": pathPrefix,
mpagenkoaf801632020-07-03 10:00:42 +0000392 "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,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000401 PathPrefix: pathPrefix}
mpagenkoaf801632020-07-03 10:00:42 +0000402
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
dbainbri4d3a0dc2020-12-02 00:33:42 +0000410func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
mpagenkoaf801632020-07-03 10:00:42 +0000411
dbainbri4d3a0dc2020-12-02 00:33:42 +0000412 logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
mpagenkoaf801632020-07-03 10:00:42 +0000413 switch storeType {
414 case "consul":
dbainbri4d3a0dc2020-12-02 00:33:42 +0000415 return kvstore.NewConsulClient(ctx, address, timeout)
mpagenkoaf801632020-07-03 10:00:42 +0000416 case "etcd":
dbainbri4d3a0dc2020-12-02 00:33:42 +0000417 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
mpagenkoaf801632020-07-03 10:00:42 +0000418 }
419 return nil, errors.New("unsupported-kv-store")
420}
421
dbainbri4d3a0dc2020-12-02 00:33:42 +0000422func NewTechProfile(ctx context.Context, resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string, basePathKvStore string) (*TechProfileMgr, error) {
mpagenkoaf801632020-07-03 10:00:42 +0000423 var techprofileObj TechProfileMgr
dbainbri4d3a0dc2020-12-02 00:33:42 +0000424 logger.Debug(ctx, "Initializing techprofile Manager")
425 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress, basePathKvStore)
426 techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx, techprofileObj.config.TPKVPathPrefix)
427 techprofileObj.config.DefaultTpKVBackend = techprofileObj.SetKVClient(ctx, techprofileObj.config.defaultTpKvPathPrefix)
mpagenkoaf801632020-07-03 10:00:42 +0000428 if techprofileObj.config.KVBackend == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000429 logger.Error(ctx, "Failed to initialize KV backend\n")
mpagenkoaf801632020-07-03 10:00:42 +0000430 return nil, errors.New("KV backend init failed")
431 }
432 techprofileObj.resourceMgr = resourceMgr
dbainbri4d3a0dc2020-12-02 00:33:42 +0000433 logger.Debug(ctx, "Initializing techprofile object instance success")
mpagenkoaf801632020-07-03 10:00:42 +0000434 return &techprofileObj, nil
435}
436
dbainbri4d3a0dc2020-12-02 00:33:42 +0000437func (t *TechProfileMgr) GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string {
438 logger.Debugw(ctx, "get-tp-instance-kv-path", log.Fields{
mpagenkoaf801632020-07-03 10:00:42 +0000439 "uniPortName": uniPortName,
440 "tpId": techProfiletblID,
441 })
442 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
443}
444
445func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (interface{}, error) {
446 var err error
447 var kvResult *kvstore.KVPair
448 var KvTpIns TechProfile
449 var KvEponIns EponProfile
450 var resPtr interface{}
451 // For example:
452 // tpInstPath like "XGS-PON/64/uni_port_name"
453 // is broken into ["XGS-PON" "64" ...]
454 pathSlice := regexp.MustCompile(`/`).Split(path, -1)
455 switch pathSlice[0] {
456 case xgspon, gpon:
457 resPtr = &KvTpIns
458 case epon:
459 resPtr = &KvEponIns
460 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000461 logger.Errorw(ctx, "unknown-tech", log.Fields{"tech": pathSlice[0]})
mpagenkoaf801632020-07-03 10:00:42 +0000462 return nil, fmt.Errorf("unknown-tech-%s", pathSlice[0])
463 }
464
465 kvResult, _ = t.config.KVBackend.Get(ctx, path)
466 if kvResult == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000467 logger.Infow(ctx, "tp-instance-not-found-on-kv", log.Fields{"key": path})
mpagenkoaf801632020-07-03 10:00:42 +0000468 return nil, nil
469 } else {
470 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
471 if err = json.Unmarshal(value, resPtr); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000472 logger.Errorw(ctx, "error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
mpagenkoaf801632020-07-03 10:00:42 +0000473 return nil, errors.New("error-unmarshal-kv-result")
474 } else {
475 return resPtr, nil
476 }
477 }
478 }
479 return nil, err
480}
481
482func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000483 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
484 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
mpagenkoaf801632020-07-03 10:00:42 +0000485 tpInstanceJson, err := json.Marshal(*tpInstance)
486 if err == nil {
487 // Backend will convert JSON byte array into string format
dbainbri4d3a0dc2020-12-02 00:33:42 +0000488 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
mpagenkoaf801632020-07-03 10:00:42 +0000489 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
490 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000491 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
mpagenkoaf801632020-07-03 10:00:42 +0000492 }
493 return err
494}
495
496func (t *TechProfileMgr) addEponProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *EponProfile) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000497 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
498 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
mpagenkoaf801632020-07-03 10:00:42 +0000499 tpInstanceJson, err := json.Marshal(*tpInstance)
500 if err == nil {
501 // Backend will convert JSON byte array into string format
dbainbri4d3a0dc2020-12-02 00:33:42 +0000502 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
mpagenkoaf801632020-07-03 10:00:42 +0000503 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
504 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000505 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
mpagenkoaf801632020-07-03 10:00:42 +0000506 }
507 return err
508}
509
510func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
511 var kvtechprofile DefaultTechProfile
512 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000513 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
514 kvresult, err := t.config.DefaultTpKVBackend.Get(ctx, key)
mpagenkoaf801632020-07-03 10:00:42 +0000515 if err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000516 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
mpagenkoaf801632020-07-03 10:00:42 +0000517 return nil
518 }
519 if kvresult != nil {
520 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
521 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
522 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000523 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
mpagenkoaf801632020-07-03 10:00:42 +0000524 return nil
525 }
526
dbainbri4d3a0dc2020-12-02 00:33:42 +0000527 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
mpagenkoaf801632020-07-03 10:00:42 +0000528 return &kvtechprofile
529 }
530 }
531 return nil
532}
533
534func (t *TechProfileMgr) getEponTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultEponProfile {
535 var kvtechprofile DefaultEponProfile
536 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000537 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
538 kvresult, err := t.config.DefaultTpKVBackend.Get(ctx, key)
mpagenkoaf801632020-07-03 10:00:42 +0000539 if err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000540 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
mpagenkoaf801632020-07-03 10:00:42 +0000541 return nil
542 }
543 if kvresult != nil {
544 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
545 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
546 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000547 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
mpagenkoaf801632020-07-03 10:00:42 +0000548 return nil
549 }
550
dbainbri4d3a0dc2020-12-02 00:33:42 +0000551 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
mpagenkoaf801632020-07-03 10:00:42 +0000552 return &kvtechprofile
553 }
554 }
555 return nil
556}
557
558func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (interface{}, error) {
559 var tpInstance *TechProfile
560 var tpEponInstance *EponProfile
561
dbainbri4d3a0dc2020-12-02 00:33:42 +0000562 logger.Infow(ctx, "creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000563
564 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
565 if !uniPortNameFormat.Match([]byte(uniPortName)) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000566 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
mpagenkoaf801632020-07-03 10:00:42 +0000567 return nil, errors.New("uni-port-name-not-confirming-to-format")
568 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000569 tpInstancePath := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
mpagenkoaf801632020-07-03 10:00:42 +0000570 // For example:
571 // tpInstPath like "XGS-PON/64/uni_port_name"
572 // is broken into ["XGS-PON" "64" ...]
573 pathSlice := regexp.MustCompile(`/`).Split(tpInstancePath, -1)
574 if pathSlice[0] == epon {
575 tp := t.getEponTPFromKVStore(ctx, techProfiletblID)
576 if tp != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000577 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
578 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
579 tp = t.getDefaultEponProfile(ctx)
mpagenkoaf801632020-07-03 10:00:42 +0000580 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000581 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
mpagenkoaf801632020-07-03 10:00:42 +0000582 }
583 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000584 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
585 tp = t.getDefaultEponProfile(ctx)
mpagenkoaf801632020-07-03 10:00:42 +0000586 }
587
588 if tpEponInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpEponInstance == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000589 logger.Error(ctx, "tp-intance-allocation-failed")
mpagenkoaf801632020-07-03 10:00:42 +0000590 return nil, errors.New("tp-intance-allocation-failed")
591 }
592 if err := t.addEponProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpEponInstance); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000593 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
mpagenkoaf801632020-07-03 10:00:42 +0000594 return nil, errors.New("error-adding-tp-to-kv-store")
595 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000596 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
mpagenkoaf801632020-07-03 10:00:42 +0000597 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
598 return tpEponInstance, nil
599 } else {
600 tp := t.getTPFromKVStore(ctx, techProfiletblID)
601 if tp != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000602 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
603 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
604 tp = t.getDefaultTechProfile(ctx)
mpagenkoaf801632020-07-03 10:00:42 +0000605 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000606 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
mpagenkoaf801632020-07-03 10:00:42 +0000607 }
608 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000609 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
610 tp = t.getDefaultTechProfile(ctx)
mpagenkoaf801632020-07-03 10:00:42 +0000611 }
612
613 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000614 logger.Error(ctx, "tp-intance-allocation-failed")
mpagenkoaf801632020-07-03 10:00:42 +0000615 return nil, errors.New("tp-intance-allocation-failed")
616 }
617 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000618 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
mpagenkoaf801632020-07-03 10:00:42 +0000619 return nil, errors.New("error-adding-tp-to-kv-store")
620 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000621 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
mpagenkoaf801632020-07-03 10:00:42 +0000622 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
623 return tpInstance, nil
624 }
625}
626
627func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000628 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
mpagenkoaf801632020-07-03 10:00:42 +0000629 return t.config.KVBackend.Delete(ctx, path)
630}
631
dbainbri4d3a0dc2020-12-02 00:33:42 +0000632func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl InstanceControl) error {
mpagenkoaf801632020-07-03 10:00:42 +0000633 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000634 logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
mpagenkoaf801632020-07-03 10:00:42 +0000635 return errors.New("invalid-onu-instance-ctl-attr")
636 }
637
638 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000639 logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
mpagenkoaf801632020-07-03 10:00:42 +0000640 return errors.New("invalid-uni-instance-ctl-attr")
641 }
642
643 if instCtl.Uni == "multi-instance" {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000644 logger.Error(ctx, "uni-multi-instance-tp-not-supported")
mpagenkoaf801632020-07-03 10:00:42 +0000645 return errors.New("uni-multi-instance-tp-not-supported")
646 }
647
648 return nil
649}
650
651func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
652
dbainbri4d3a0dc2020-12-02 00:33:42 +0000653 var usGemPortAttributeList []IGemPortAttribute
654 var dsGemPortAttributeList []IGemPortAttribute
655 var dsMulticastGemAttributeList []IGemPortAttribute
656 var dsUnicastGemAttributeList []IGemPortAttribute
mpagenkoaf801632020-07-03 10:00:42 +0000657 var tcontIDs []uint32
658 var gemPorts []uint32
659 var err error
660
dbainbri4d3a0dc2020-12-02 00:33:42 +0000661 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000662
663 if tp.InstanceCtrl.Onu == "multi-instance" {
664 t.AllocIDMgmtLock.Lock()
665 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
666 t.AllocIDMgmtLock.Unlock()
667 if err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000668 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000669 return nil
670 }
671 } else { // "single-instance"
672 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000673 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000674 return nil
675 } else if tpInst == nil {
676 // No "single-instance" tp found on one any uni port for the given TP ID
677 // Allocate a new TcontID or AllocID
678 t.AllocIDMgmtLock.Lock()
679 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
680 t.AllocIDMgmtLock.Unlock()
681 if err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000682 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000683 return nil
684 }
685 } else {
686 // Use the alloc-id from the existing TpInstance
687 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
688 }
689 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000690 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000691 t.GemPortIDMgmtLock.Lock()
692 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
693 t.GemPortIDMgmtLock.Unlock()
694 if err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000695 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000696 return nil
697 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000698 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000699 for index := 0; index < int(tp.NumGemPorts); index++ {
700 usGemPortAttributeList = append(usGemPortAttributeList,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000701 IGemPortAttribute{GemportID: gemPorts[index],
mpagenkoaf801632020-07-03 10:00:42 +0000702 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
703 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
704 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
705 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
706 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
707 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
708 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
709 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
710 }
711
dbainbri4d3a0dc2020-12-02 00:33:42 +0000712 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
mpagenkoaf801632020-07-03 10:00:42 +0000713 //put multicast and unicast downstream GEM port attributes in different lists first
714 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
715 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
716 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000717 IGemPortAttribute{
mpagenkoaf801632020-07-03 10:00:42 +0000718 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
719 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
720 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
721 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
722 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
723 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
724 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
725 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
726 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
727 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
728 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
729 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
730 } else {
731 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000732 IGemPortAttribute{
mpagenkoaf801632020-07-03 10:00:42 +0000733 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
734 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
735 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
736 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
737 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
738 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
739 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
740 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
741 }
742 }
743 //add unicast downstream GEM ports to dsGemPortAttributeList
744 for index := 0; index < int(tp.NumGemPorts); index++ {
745 dsGemPortAttributeList = append(dsGemPortAttributeList,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000746 IGemPortAttribute{GemportID: gemPorts[index],
mpagenkoaf801632020-07-03 10:00:42 +0000747 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
748 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
749 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
750 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
751 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
752 Weight: dsUnicastGemAttributeList[index].Weight,
753 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
754 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
755 }
756 //add multicast GEM ports to dsGemPortAttributeList afterwards
757 for k := range dsMulticastGemAttributeList {
758 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
759 }
760
761 return &TechProfile{
762 SubscriberIdentifier: uniPortName,
763 Name: tp.Name,
764 ProfileType: tp.ProfileType,
765 Version: tp.Version,
766 NumGemPorts: tp.NumGemPorts,
767 InstanceCtrl: tp.InstanceCtrl,
dbainbri4d3a0dc2020-12-02 00:33:42 +0000768 UsScheduler: IScheduler{
mpagenkoaf801632020-07-03 10:00:42 +0000769 AllocID: tcontIDs[0],
770 Direction: tp.UsScheduler.Direction,
771 AdditionalBw: tp.UsScheduler.AdditionalBw,
772 Priority: tp.UsScheduler.Priority,
773 Weight: tp.UsScheduler.Weight,
774 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
dbainbri4d3a0dc2020-12-02 00:33:42 +0000775 DsScheduler: IScheduler{
mpagenkoaf801632020-07-03 10:00:42 +0000776 AllocID: tcontIDs[0],
777 Direction: tp.DsScheduler.Direction,
778 AdditionalBw: tp.DsScheduler.AdditionalBw,
779 Priority: tp.DsScheduler.Priority,
780 Weight: tp.DsScheduler.Weight,
781 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
782 UpstreamGemPortAttributeList: usGemPortAttributeList,
783 DownstreamGemPortAttributeList: dsGemPortAttributeList}
784}
785
786// allocateTPInstance function for EPON
787func (t *TechProfileMgr) allocateEponTPInstance(ctx context.Context, uniPortName string, tp *DefaultEponProfile, intfId uint32, tpInstPath string) *EponProfile {
788
789 var usQueueAttributeList []iUpstreamQueueAttribute
790 var dsQueueAttributeList []iDownstreamQueueAttribute
791 var tcontIDs []uint32
792 var gemPorts []uint32
793 var err error
794
dbainbri4d3a0dc2020-12-02 00:33:42 +0000795 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000796
797 if tp.InstanceCtrl.Onu == "multi-instance" {
798 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000799 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000800 return nil
801 }
802 } else { // "single-instance"
803 if tpInst, err := t.getSingleInstanceEponTp(ctx, tpInstPath); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000804 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000805 return nil
806 } else if tpInst == nil {
807 // No "single-instance" tp found on one any uni port for the given TP ID
808 // Allocate a new TcontID or AllocID
809 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000810 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
mpagenkoaf801632020-07-03 10:00:42 +0000811 return nil
812 }
813 } else {
814 // Use the alloc-id from the existing TpInstance
815 tcontIDs = append(tcontIDs, tpInst.AllocID)
816 }
817 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000818 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000819 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000820 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000821 return nil
822 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000823 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
mpagenkoaf801632020-07-03 10:00:42 +0000824 for index := 0; index < int(tp.NumGemPorts); index++ {
825 usQueueAttributeList = append(usQueueAttributeList,
826 iUpstreamQueueAttribute{GemportID: gemPorts[index],
827 MaxQueueSize: tp.UpstreamQueueAttributeList[index].MaxQueueSize,
828 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
829 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
830 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
831 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
832 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
833 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
834 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
835 NumQueueSet: tp.UpstreamQueueAttributeList[index].NumQueueSet,
836 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
837 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
838 PriorityQueue: tp.UpstreamQueueAttributeList[index].PriorityQueue,
839 Weight: tp.UpstreamQueueAttributeList[index].Weight,
840 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
841 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
842 }
843
dbainbri4d3a0dc2020-12-02 00:33:42 +0000844 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamQueueAttributeList))
mpagenkoaf801632020-07-03 10:00:42 +0000845 for index := 0; index < int(tp.NumGemPorts); index++ {
846 dsQueueAttributeList = append(dsQueueAttributeList,
847 iDownstreamQueueAttribute{GemportID: gemPorts[index],
848 MaxQueueSize: tp.DownstreamQueueAttributeList[index].MaxQueueSize,
849 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
850 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
851 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
852 PriorityQueue: tp.DownstreamQueueAttributeList[index].PriorityQueue,
853 Weight: tp.DownstreamQueueAttributeList[index].Weight,
854 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
855 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
856 }
857
858 return &EponProfile{
859 SubscriberIdentifier: uniPortName,
860 Name: tp.Name,
861 ProfileType: tp.ProfileType,
862 Version: tp.Version,
863 NumGemPorts: tp.NumGemPorts,
864 InstanceCtrl: tp.InstanceCtrl,
865 EponAttribute: tp.EponAttribute,
866 AllocID: tcontIDs[0],
867 UpstreamQueueAttributeList: usQueueAttributeList,
868 DownstreamQueueAttributeList: dsQueueAttributeList}
869}
870
871// getSingleInstanceTp returns another TpInstance for an ONU on a different
872// uni port for the same TP ID, if it finds one, else nil.
873func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
874 var tpInst TechProfile
875
876 // For example:
877 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
878 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
879 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
880 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
881
882 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
883 for keyPath, kvPair := range kvPairs {
884 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
885 if err = json.Unmarshal(value, &tpInst); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000886 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
mpagenkoaf801632020-07-03 10:00:42 +0000887 return nil, errors.New("error-unmarshal-kv-pair")
888 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000889 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
mpagenkoaf801632020-07-03 10:00:42 +0000890 return &tpInst, nil
891 }
892 }
893 }
894 return nil, nil
895}
896
897func (t *TechProfileMgr) getSingleInstanceEponTp(ctx context.Context, tpPath string) (*EponProfile, error) {
898 var tpInst EponProfile
899
900 // For example:
901 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
902 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
903 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
904 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
905
906 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
907 for keyPath, kvPair := range kvPairs {
908 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
909 if err = json.Unmarshal(value, &tpInst); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000910 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
mpagenkoaf801632020-07-03 10:00:42 +0000911 return nil, errors.New("error-unmarshal-kv-pair")
912 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000913 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
mpagenkoaf801632020-07-03 10:00:42 +0000914 return &tpInst, nil
915 }
916 }
917 }
918 return nil, nil
919}
920
dbainbri4d3a0dc2020-12-02 00:33:42 +0000921func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *DefaultTechProfile {
mpagenkoaf801632020-07-03 10:00:42 +0000922 var usGemPortAttributeList []GemPortAttribute
923 var dsGemPortAttributeList []GemPortAttribute
924
925 for _, pbit := range t.config.DefaultPbits {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000926 logger.Debugw(ctx, "Creating GEM port", log.Fields{"pbit": pbit})
mpagenkoaf801632020-07-03 10:00:42 +0000927 usGemPortAttributeList = append(usGemPortAttributeList,
928 GemPortAttribute{
929 MaxQueueSize: defaultMaxQueueSize,
930 PbitMap: pbit,
931 AesEncryption: defaultAESEncryption,
932 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
933 PriorityQueue: defaultPriorityQueue,
934 Weight: defaultQueueWeight,
935 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
936 DiscardConfig: DiscardConfig{
937 MinThreshold: defaultMinThreshold,
938 MaxThreshold: defaultMaxThreshold,
939 MaxProbability: defaultMaxProbability}})
940 dsGemPortAttributeList = append(dsGemPortAttributeList,
941 GemPortAttribute{
942 MaxQueueSize: defaultMaxQueueSize,
943 PbitMap: pbit,
944 AesEncryption: defaultAESEncryption,
945 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
946 PriorityQueue: defaultPriorityQueue,
947 Weight: defaultQueueWeight,
948 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
949 DiscardConfig: DiscardConfig{
950 MinThreshold: defaultMinThreshold,
951 MaxThreshold: defaultMaxThreshold,
952 MaxProbability: defaultMaxProbability},
953 IsMulticast: defaultIsMulticast,
954 DControlList: defaultAccessControlList,
955 SControlList: defaultAccessControlList,
956 McastGemID: defaultMcastGemID})
957 }
958 return &DefaultTechProfile{
959 Name: t.config.DefaultTPName,
960 ProfileType: t.resourceMgr.GetTechnology(),
961 Version: t.config.TPVersion,
962 NumGemPorts: uint32(len(usGemPortAttributeList)),
963 InstanceCtrl: InstanceControl{
964 Onu: defaultOnuInstance,
965 Uni: defaultUniInstance,
966 MaxGemPayloadSize: defaultGemPayloadSize},
967 UsScheduler: Scheduler{
968 Direction: Direction_name[Direction_UPSTREAM],
969 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
970 Priority: defaultPriority,
971 Weight: defaultWeight,
972 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
973 DsScheduler: Scheduler{
974 Direction: Direction_name[Direction_DOWNSTREAM],
975 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
976 Priority: defaultPriority,
977 Weight: defaultWeight,
978 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
979 UpstreamGemPortAttributeList: usGemPortAttributeList,
980 DownstreamGemPortAttributeList: dsGemPortAttributeList}
981}
982
983// getDefaultTechProfile function for EPON
dbainbri4d3a0dc2020-12-02 00:33:42 +0000984func (t *TechProfileMgr) getDefaultEponProfile(ctx context.Context) *DefaultEponProfile {
mpagenkoaf801632020-07-03 10:00:42 +0000985
986 var usQueueAttributeList []UpstreamQueueAttribute
987 var dsQueueAttributeList []DownstreamQueueAttribute
988
989 for _, pbit := range t.config.DefaultPbits {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000990 logger.Debugw(ctx, "Creating Queue", log.Fields{"pbit": pbit})
mpagenkoaf801632020-07-03 10:00:42 +0000991 usQueueAttributeList = append(usQueueAttributeList,
992 UpstreamQueueAttribute{
993 MaxQueueSize: defaultMaxQueueSize,
994 PbitMap: pbit,
995 AesEncryption: defaultAESEncryption,
996 TrafficType: defaultTrafficType,
997 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
998 NominalInterval: defaultNominalInterval,
999 ToleratedPollJitter: defaultToleratedPollJitter,
1000 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
1001 NumQueueSet: defaultNumQueueSet,
1002 QThresholds: QThresholds{
1003 QThreshold1: defaultQThreshold1,
1004 QThreshold2: defaultQThreshold2,
1005 QThreshold3: defaultQThreshold3,
1006 QThreshold4: defaultQThreshold4,
1007 QThreshold5: defaultQThreshold5,
1008 QThreshold6: defaultQThreshold6,
1009 QThreshold7: defaultQThreshold7},
1010 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1011 PriorityQueue: defaultPriorityQueue,
1012 Weight: defaultQueueWeight,
1013 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1014 DiscardConfig: DiscardConfig{
1015 MinThreshold: defaultMinThreshold,
1016 MaxThreshold: defaultMaxThreshold,
1017 MaxProbability: defaultMaxProbability}})
1018 dsQueueAttributeList = append(dsQueueAttributeList,
1019 DownstreamQueueAttribute{
1020 MaxQueueSize: defaultMaxQueueSize,
1021 PbitMap: pbit,
1022 AesEncryption: defaultAESEncryption,
1023 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1024 PriorityQueue: defaultPriorityQueue,
1025 Weight: defaultQueueWeight,
1026 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1027 DiscardConfig: DiscardConfig{
1028 MinThreshold: defaultMinThreshold,
1029 MaxThreshold: defaultMaxThreshold,
1030 MaxProbability: defaultMaxProbability}})
1031 }
1032 return &DefaultEponProfile{
1033 Name: t.config.DefaultTPName,
1034 ProfileType: t.resourceMgr.GetTechnology(),
1035 Version: t.config.TPVersion,
1036 NumGemPorts: uint32(len(usQueueAttributeList)),
1037 InstanceCtrl: InstanceControl{
1038 Onu: defaultOnuInstance,
1039 Uni: defaultUniInstance,
1040 MaxGemPayloadSize: defaultGemPayloadSize},
1041 EponAttribute: EponAttribute{
1042 PackageType: defaultPakageType},
1043 UpstreamQueueAttributeList: usQueueAttributeList,
1044 DownstreamQueueAttributeList: dsQueueAttributeList}
1045}
1046
dbainbri4d3a0dc2020-12-02 00:33:42 +00001047func (t *TechProfileMgr) GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32 {
mpagenkoaf801632020-07-03 10:00:42 +00001048 var result int32 = -1
1049
1050 if paramType == "direction" {
1051 for key, val := range tp_pb.Direction_value {
1052 if key == paramKey {
1053 result = val
1054 }
1055 }
1056 } else if paramType == "discard_policy" {
1057 for key, val := range tp_pb.DiscardPolicy_value {
1058 if key == paramKey {
1059 result = val
1060 }
1061 }
1062 } else if paramType == "sched_policy" {
1063 for key, val := range tp_pb.SchedulingPolicy_value {
1064 if key == paramKey {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001065 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": key, "value": val})
mpagenkoaf801632020-07-03 10:00:42 +00001066 result = val
1067 }
1068 }
1069 } else if paramType == "additional_bw" {
1070 for key, val := range tp_pb.AdditionalBW_value {
1071 if key == paramKey {
1072 result = val
1073 }
1074 }
1075 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001076 logger.Error(ctx, "Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
mpagenkoaf801632020-07-03 10:00:42 +00001077 return -1
1078 }
dbainbri4d3a0dc2020-12-02 00:33:42 +00001079 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": paramKey, "value": result})
mpagenkoaf801632020-07-03 10:00:42 +00001080 return result
1081}
1082
dbainbri4d3a0dc2020-12-02 00:33:42 +00001083func (t *TechProfileMgr) GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
1084 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.UsScheduler.Direction))
mpagenkoaf801632020-07-03 10:00:42 +00001085 if dir == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001086 logger.Errorf(ctx, "Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
mpagenkoaf801632020-07-03 10:00:42 +00001087 return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
1088 }
1089
dbainbri4d3a0dc2020-12-02 00:33:42 +00001090 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.UsScheduler.AdditionalBw))
mpagenkoaf801632020-07-03 10:00:42 +00001091 if bw == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001092 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
mpagenkoaf801632020-07-03 10:00:42 +00001093 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
1094 }
1095
dbainbri4d3a0dc2020-12-02 00:33:42 +00001096 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.UsScheduler.QSchedPolicy))
mpagenkoaf801632020-07-03 10:00:42 +00001097 if policy == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001098 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
mpagenkoaf801632020-07-03 10:00:42 +00001099 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
1100 }
1101
1102 return &tp_pb.SchedulerConfig{
1103 Direction: dir,
1104 AdditionalBw: bw,
1105 Priority: tpInstance.UsScheduler.Priority,
1106 Weight: tpInstance.UsScheduler.Weight,
1107 SchedPolicy: policy}, nil
1108}
1109
dbainbri4d3a0dc2020-12-02 00:33:42 +00001110func (t *TechProfileMgr) GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
mpagenkoaf801632020-07-03 10:00:42 +00001111
dbainbri4d3a0dc2020-12-02 00:33:42 +00001112 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.DsScheduler.Direction))
mpagenkoaf801632020-07-03 10:00:42 +00001113 if dir == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001114 logger.Errorf(ctx, "Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
mpagenkoaf801632020-07-03 10:00:42 +00001115 return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
1116 }
1117
dbainbri4d3a0dc2020-12-02 00:33:42 +00001118 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.DsScheduler.AdditionalBw))
mpagenkoaf801632020-07-03 10:00:42 +00001119 if bw == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001120 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
mpagenkoaf801632020-07-03 10:00:42 +00001121 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
1122 }
1123
dbainbri4d3a0dc2020-12-02 00:33:42 +00001124 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.DsScheduler.QSchedPolicy))
mpagenkoaf801632020-07-03 10:00:42 +00001125 if policy == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001126 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
mpagenkoaf801632020-07-03 10:00:42 +00001127 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
1128 }
1129
1130 return &tp_pb.SchedulerConfig{
1131 Direction: dir,
1132 AdditionalBw: bw,
1133 Priority: tpInstance.DsScheduler.Priority,
1134 Weight: tpInstance.DsScheduler.Weight,
1135 SchedPolicy: policy}, nil
1136}
1137
1138func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
1139 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
1140
1141 tSched := &tp_pb.TrafficScheduler{
1142 Direction: SchedCfg.Direction,
1143 AllocId: tpInstance.UsScheduler.AllocID,
1144 TrafficShapingInfo: ShapingCfg,
1145 Scheduler: SchedCfg}
1146
1147 return tSched
1148}
1149
dbainbri4d3a0dc2020-12-02 00:33:42 +00001150func (tpm *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
mpagenkoaf801632020-07-03 10:00:42 +00001151
1152 var encryp bool
1153 if Dir == tp_pb.Direction_UPSTREAM {
1154 // upstream GEM ports
1155 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
1156 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1157 for Count := 0; Count < NumGemPorts; Count++ {
1158 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
1159 encryp = true
1160 } else {
1161 encryp = false
1162 }
1163
dbainbri4d3a0dc2020-12-02 00:33:42 +00001164 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
mpagenkoaf801632020-07-03 10:00:42 +00001165 if schedPolicy == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001166 logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
mpagenkoaf801632020-07-03 10:00:42 +00001167 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1168 }
1169
dbainbri4d3a0dc2020-12-02 00:33:42 +00001170 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
mpagenkoaf801632020-07-03 10:00:42 +00001171 if discardPolicy == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001172 logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
mpagenkoaf801632020-07-03 10:00:42 +00001173 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1174 }
1175
1176 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
dbainbri4d3a0dc2020-12-02 00:33:42 +00001177 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.UsScheduler.Direction)),
mpagenkoaf801632020-07-03 10:00:42 +00001178 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
1179 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
1180 AesEncryption: encryp,
1181 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
1182 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
1183 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
1184 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
1185 })
1186 }
dbainbri4d3a0dc2020-12-02 00:33:42 +00001187 logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
mpagenkoaf801632020-07-03 10:00:42 +00001188 return GemPorts, nil
1189 } else if Dir == tp_pb.Direction_DOWNSTREAM {
1190 //downstream GEM ports
1191 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1192 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1193 for Count := 0; Count < NumGemPorts; Count++ {
1194 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1195 //do not take multicast GEM ports. They are handled separately.
1196 continue
1197 }
1198 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1199 encryp = true
1200 } else {
1201 encryp = false
1202 }
1203
dbainbri4d3a0dc2020-12-02 00:33:42 +00001204 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
mpagenkoaf801632020-07-03 10:00:42 +00001205 if schedPolicy == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001206 logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
mpagenkoaf801632020-07-03 10:00:42 +00001207 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1208 }
1209
dbainbri4d3a0dc2020-12-02 00:33:42 +00001210 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
mpagenkoaf801632020-07-03 10:00:42 +00001211 if discardPolicy == -1 {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001212 logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
mpagenkoaf801632020-07-03 10:00:42 +00001213 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1214 }
1215
1216 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
dbainbri4d3a0dc2020-12-02 00:33:42 +00001217 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
mpagenkoaf801632020-07-03 10:00:42 +00001218 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
1219 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1220 AesEncryption: encryp,
1221 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
1222 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1223 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
1224 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
1225 })
1226 }
dbainbri4d3a0dc2020-12-02 00:33:42 +00001227 logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
mpagenkoaf801632020-07-03 10:00:42 +00001228 return GemPorts, nil
1229 }
1230
dbainbri4d3a0dc2020-12-02 00:33:42 +00001231 logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", Dir)
mpagenkoaf801632020-07-03 10:00:42 +00001232 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
1233}
1234
1235//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
1236func isMulticastGem(isMulticastAttrValue string) bool {
1237 return isMulticastAttrValue != "" &&
1238 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1239}
1240
dbainbri4d3a0dc2020-12-02 00:33:42 +00001241func (tpm *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue {
mpagenkoaf801632020-07-03 10:00:42 +00001242 var encryp bool
1243 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1244 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
1245 for Count := 0; Count < NumGemPorts; Count++ {
1246 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1247 continue
1248 }
1249 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1250 encryp = true
1251 } else {
1252 encryp = false
1253 }
1254 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
dbainbri4d3a0dc2020-12-02 00:33:42 +00001255 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
mpagenkoaf801632020-07-03 10:00:42 +00001256 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
1257 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1258 AesEncryption: encryp,
dbainbri4d3a0dc2020-12-02 00:33:42 +00001259 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
mpagenkoaf801632020-07-03 10:00:42 +00001260 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1261 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
dbainbri4d3a0dc2020-12-02 00:33:42 +00001262 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
mpagenkoaf801632020-07-03 10:00:42 +00001263 })
1264 }
dbainbri4d3a0dc2020-12-02 00:33:42 +00001265 logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
mpagenkoaf801632020-07-03 10:00:42 +00001266 return mcastTrafficQueues
1267}
1268
dbainbri4d3a0dc2020-12-02 00:33:42 +00001269func (tpm *TechProfileMgr) GetUsTrafficScheduler(ctx context.Context, tp *TechProfile) *tp_pb.TrafficScheduler {
1270 UsScheduler, _ := tpm.GetUsScheduler(ctx, tp)
mpagenkoaf801632020-07-03 10:00:42 +00001271
1272 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
1273 AllocId: tp.UsScheduler.AllocID,
1274 Scheduler: UsScheduler}
1275}
1276
dbainbri4d3a0dc2020-12-02 00:33:42 +00001277func (t *TechProfileMgr) GetGemportIDForPbit(ctx context.Context, tp interface{}, dir tp_pb.Direction, pbit uint32) uint32 {
mpagenkoaf801632020-07-03 10:00:42 +00001278 /*
1279 Function to get the Gemport ID mapped to a pbit.
1280 */
1281 switch tp := tp.(type) {
1282 case *TechProfile:
1283 if dir == tp_pb.Direction_UPSTREAM {
1284 // upstream GEM ports
1285 numGemPorts := len(tp.UpstreamGemPortAttributeList)
1286 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1287 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
1288 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1289 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1290 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1291 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1292 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
dbainbri4d3a0dc2020-12-02 00:33:42 +00001293 logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
mpagenkoaf801632020-07-03 10:00:42 +00001294 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
1295 }
1296 }
1297 }
1298 }
1299 } else if dir == tp_pb.Direction_DOWNSTREAM {
1300 //downstream GEM ports
1301 numGemPorts := len(tp.DownstreamGemPortAttributeList)
1302 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1303 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
1304 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1305 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1306 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1307 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1308 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
dbainbri4d3a0dc2020-12-02 00:33:42 +00001309 logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
mpagenkoaf801632020-07-03 10:00:42 +00001310 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
1311 }
1312 }
1313 }
1314 }
1315 }
dbainbri4d3a0dc2020-12-02 00:33:42 +00001316 logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
mpagenkoaf801632020-07-03 10:00:42 +00001317 case *EponProfile:
1318 if dir == tp_pb.Direction_UPSTREAM {
1319 // upstream GEM ports
1320 numGemPorts := len(tp.UpstreamQueueAttributeList)
1321 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1322 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
1323 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1324 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1325 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1326 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1327 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
dbainbri4d3a0dc2020-12-02 00:33:42 +00001328 logger.Debugw(ctx, "Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportID})
mpagenkoaf801632020-07-03 10:00:42 +00001329 return tp.UpstreamQueueAttributeList[gemCnt].GemportID
1330 }
1331 }
1332 }
1333 }
1334 } else if dir == tp_pb.Direction_DOWNSTREAM {
1335 //downstream GEM ports
1336 numGemPorts := len(tp.DownstreamQueueAttributeList)
1337 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1338 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
1339 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1340 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1341 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1342 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1343 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
dbainbri4d3a0dc2020-12-02 00:33:42 +00001344 logger.Debugw(ctx, "Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportID})
mpagenkoaf801632020-07-03 10:00:42 +00001345 return tp.DownstreamQueueAttributeList[gemCnt].GemportID
1346 }
1347 }
1348 }
1349 }
1350 }
dbainbri4d3a0dc2020-12-02 00:33:42 +00001351 logger.Errorw(ctx, "No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
mpagenkoaf801632020-07-03 10:00:42 +00001352 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +00001353 logger.Errorw(ctx, "unknown-tech", log.Fields{"tp": tp})
mpagenkoaf801632020-07-03 10:00:42 +00001354 }
1355 return 0
1356}
1357
1358// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
1359func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) interface{} {
1360 var tpTech TechProfile
1361 var tpEpon EponProfile
1362
1363 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
1364
1365 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
1366 tech := t.resourceMgr.GetTechnology()
1367 tpInstancesTech := make([]TechProfile, 0, len(kvPairs))
1368 tpInstancesEpon := make([]EponProfile, 0, len(kvPairs))
1369
1370 for kvPath, kvPair := range kvPairs {
1371 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
1372 if tech == xgspon || tech == gpon {
1373 if err = json.Unmarshal(value, &tpTech); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001374 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
mpagenkoaf801632020-07-03 10:00:42 +00001375 continue
1376 } else {
1377 tpInstancesTech = append(tpInstancesTech, tpTech)
1378 }
1379 } else if tech == epon {
1380 if err = json.Unmarshal(value, &tpEpon); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +00001381 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
mpagenkoaf801632020-07-03 10:00:42 +00001382 continue
1383 } else {
1384 tpInstancesEpon = append(tpInstancesEpon, tpEpon)
1385 }
1386 }
1387 }
1388 }
1389
1390 switch tech {
1391 case xgspon, gpon:
1392 return tpInstancesTech
1393 case epon:
1394 return tpInstancesEpon
1395 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +00001396 logger.Errorw(ctx, "unknown-technology", log.Fields{"tech": tech})
mpagenkoaf801632020-07-03 10:00:42 +00001397 return nil
1398 }
1399 }
1400 return nil
1401}