blob: 3588838a9968273f671f807bbb50c3b07810cd17 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package techprofile
18
19import (
20 "encoding/json"
21 "errors"
22 "fmt"
Girish Gowdra9447baf2019-11-05 16:42:37 +053023 "regexp"
Scott Baker2c1c4822019-10-16 11:02:41 -070024 "strconv"
25
sbarbari1e3e29c2019-11-05 10:06:50 -050026 "github.com/opencord/voltha-lib-go/v2/pkg/db"
Scott Bakerce767002019-10-23 13:30:24 -070027 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
Scott Bakerce767002019-10-23 13:30:24 -070028 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Bakerf1b096c2019-11-01 12:36:30 -070029 tp_pb "github.com/opencord/voltha-protos/v2/go/tech_profile"
Scott Baker2c1c4822019-10-16 11:02:41 -070030)
31
32// Interface to pon resource manager APIs
33type iPonResourceMgr interface {
34 GetResourceID(IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
35 GetResourceTypeAllocID() string
36 GetResourceTypeGemPortID() string
37 GetTechnology() string
38}
39
40type Direction int32
41
42const (
43 Direction_UPSTREAM Direction = 0
44 Direction_DOWNSTREAM Direction = 1
45 Direction_BIDIRECTIONAL Direction = 2
46)
47
48var Direction_name = map[Direction]string{
49 0: "UPSTREAM",
50 1: "DOWNSTREAM",
51 2: "BIDIRECTIONAL",
52}
53
54type SchedulingPolicy int32
55
56const (
57 SchedulingPolicy_WRR SchedulingPolicy = 0
58 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
59 SchedulingPolicy_Hybrid SchedulingPolicy = 2
60)
61
62var SchedulingPolicy_name = map[SchedulingPolicy]string{
63 0: "WRR",
64 1: "StrictPriority",
65 2: "Hybrid",
66}
67
68type AdditionalBW int32
69
70const (
71 AdditionalBW_AdditionalBW_None AdditionalBW = 0
72 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
73 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
74 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
75)
76
77var AdditionalBW_name = map[AdditionalBW]string{
78 0: "AdditionalBW_None",
79 1: "AdditionalBW_NA",
80 2: "AdditionalBW_BestEffort",
81 3: "AdditionalBW_Auto",
82}
83
84type DiscardPolicy int32
85
86const (
87 DiscardPolicy_TailDrop DiscardPolicy = 0
88 DiscardPolicy_WTailDrop DiscardPolicy = 1
89 DiscardPolicy_Red DiscardPolicy = 2
90 DiscardPolicy_WRed DiscardPolicy = 3
91)
92
93var DiscardPolicy_name = map[DiscardPolicy]string{
94 0: "TailDrop",
95 1: "WTailDrop",
96 2: "Red",
97 3: "WRed",
98}
99
Girish Gowdra9447baf2019-11-05 16:42:37 +0530100// Required uniPortName format
101var uniPortNameFormat = regexp.MustCompile(`^pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
102
Scott Baker2c1c4822019-10-16 11:02:41 -0700103/*
104type InferredAdditionBWIndication int32
105
106const (
107 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
108 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
109 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
110)
111
112var InferredAdditionBWIndication_name = map[int32]string{
113 0: "InferredAdditionBWIndication_None",
114 1: "InferredAdditionBWIndication_Assured",
115 2: "InferredAdditionBWIndication_BestEffort",
116}
117*/
118// instance control defaults
119const (
120 defaultOnuInstance = "multi-instance"
121 defaultUniInstance = "single-instance"
122 defaultNumGemPorts = 1
123 defaultGemPayloadSize = "auto"
124)
125
126const MAX_GEM_PAYLOAD = "max_gem_payload_size"
127
128type InstanceControl struct {
129 Onu string `json:"ONU"`
130 Uni string `json:"uni"`
131 MaxGemPayloadSize string `json:"max_gem_payload_size"`
132}
133
134// default discard config constants
135const (
136 defaultMinThreshold = 0
137 defaultMaxThreshold = 0
138 defaultMaxProbability = 0
139)
140
141type DiscardConfig struct {
142 MinThreshold int `json:"min_threshold"`
143 MaxThreshold int `json:"max_threshold"`
144 MaxProbability int `json:"max_probability"`
145}
146
147// default scheduler contants
148const (
149 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
150 defaultPriority = 0
151 defaultWeight = 0
152 defaultQueueSchedPolicy = SchedulingPolicy_Hybrid
153)
154
155type Scheduler struct {
156 Direction string `json:"direction"`
157 AdditionalBw string `json:"additional_bw"`
158 Priority uint32 `json:"priority"`
159 Weight uint32 `json:"weight"`
160 QSchedPolicy string `json:"q_sched_policy"`
161}
162
163// default GEM attribute constants
164const (
Esin Karaman8aa75a72019-12-20 13:11:59 +0000165 defaultAESEncryption = "True"
166 defaultPriorityQueue = 0
167 defaultQueueWeight = 0
168 defaultMaxQueueSize = "auto"
169 defaultdropPolicy = DiscardPolicy_TailDrop
170 defaultSchedulePolicy = SchedulingPolicy_WRR
171 defaultIsMulticast = "False"
172 defaultAccessControlList = "224.0.0.0-239.255.255.255"
173 defaultMcastGemID = 4069
Scott Baker2c1c4822019-10-16 11:02:41 -0700174)
175
176type GemPortAttribute struct {
177 MaxQueueSize string `json:"max_q_size"`
178 PbitMap string `json:"pbit_map"`
179 AesEncryption string `json:"aes_encryption"`
180 SchedulingPolicy string `json:"scheduling_policy"`
181 PriorityQueue uint32 `json:"priority_q"`
182 Weight uint32 `json:"weight"`
183 DiscardPolicy string `json:"discard_policy"`
184 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000185 IsMulticast string `json:"is_multicast"`
186 DControlList string `json:"dynamic_access_control_list"`
187 SControlList string `json:"static_access_control_list"`
188 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700189}
190
191type iScheduler struct {
192 AllocID uint32 `json:"alloc_id"`
193 Direction string `json:"direction"`
194 AdditionalBw string `json:"additional_bw"`
195 Priority uint32 `json:"priority"`
196 Weight uint32 `json:"weight"`
197 QSchedPolicy string `json:"q_sched_policy"`
198}
199type iGemPortAttribute struct {
200 GemportID uint32 `json:"gemport_id"`
201 MaxQueueSize string `json:"max_q_size"`
202 PbitMap string `json:"pbit_map"`
203 AesEncryption string `json:"aes_encryption"`
204 SchedulingPolicy string `json:"scheduling_policy"`
205 PriorityQueue uint32 `json:"priority_q"`
206 Weight uint32 `json:"weight"`
207 DiscardPolicy string `json:"discard_policy"`
208 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000209 IsMulticast string `json:"is_multicast"`
210 DControlList string `json:"dynamic_access_control_list"`
211 SControlList string `json:"static_access_control_list"`
212 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700213}
214
215type TechProfileMgr struct {
216 config *TechProfileFlags
217 resourceMgr iPonResourceMgr
218}
219type DefaultTechProfile struct {
220 Name string `json:"name"`
221 ProfileType string `json:"profile_type"`
222 Version int `json:"version"`
223 NumGemPorts uint32 `json:"num_gem_ports"`
224 InstanceCtrl InstanceControl `json:"instance_control"`
225 UsScheduler Scheduler `json:"us_scheduler"`
226 DsScheduler Scheduler `json:"ds_scheduler"`
227 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
228 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
229}
230type TechProfile struct {
231 Name string `json:"name"`
232 SubscriberIdentifier string `json:"subscriber_identifier"`
233 ProfileType string `json:"profile_type"`
234 Version int `json:"version"`
235 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700236 InstanceCtrl InstanceControl `json:"instance_control"`
237 UsScheduler iScheduler `json:"us_scheduler"`
238 DsScheduler iScheduler `json:"ds_scheduler"`
239 UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"`
240 DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
241}
242
sbarbari1e3e29c2019-11-05 10:06:50 -0500243func (t *TechProfileMgr) SetKVClient() *db.Backend {
Scott Baker2c1c4822019-10-16 11:02:41 -0700244 addr := t.config.KVStoreHost + ":" + strconv.Itoa(t.config.KVStorePort)
245 kvClient, err := newKVClient(t.config.KVStoreType, addr, t.config.KVStoreTimeout)
246 if err != nil {
247 log.Errorw("failed-to-create-kv-client",
248 log.Fields{
249 "type": t.config.KVStoreType, "host": t.config.KVStoreHost, "port": t.config.KVStorePort,
250 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
251 "error": err.Error(),
252 })
253 return nil
254 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500255 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700256 Client: kvClient,
257 StoreType: t.config.KVStoreType,
258 Host: t.config.KVStoreHost,
259 Port: t.config.KVStorePort,
260 Timeout: t.config.KVStoreTimeout,
261 PathPrefix: t.config.TPKVPathPrefix}
262
263 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
264 issue between kv store and backend , core is not calling NewBackend directly
265 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
266 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
267 */
268}
269
270func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
271
272 log.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
273 switch storeType {
274 case "consul":
275 return kvstore.NewConsulClient(address, timeout)
276 case "etcd":
277 return kvstore.NewEtcdClient(address, timeout)
278 }
279 return nil, errors.New("unsupported-kv-store")
280}
281
282func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreHost string, KVStorePort int) (*TechProfileMgr, error) {
283 var techprofileObj TechProfileMgr
284 log.Debug("Initializing techprofile Manager")
285 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreHost, KVStorePort)
286 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
287 if techprofileObj.config.KVBackend == nil {
288 log.Error("Failed to initialize KV backend\n")
289 return nil, errors.New("KV backend init failed")
290 }
291 techprofileObj.resourceMgr = resourceMgr
292 log.Debug("Initializing techprofile object instance success")
293 return &techprofileObj, nil
294}
295
296func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
297 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
298}
299
300func (t *TechProfileMgr) GetTPInstanceFromKVStore(techProfiletblID uint32, path string) (*TechProfile, error) {
301 var KvTpIns TechProfile
302 var resPtr *TechProfile = &KvTpIns
303 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530304 var kvResult *kvstore.KVPair
Girish Gowdra9447baf2019-11-05 16:42:37 +0530305
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530306 kvResult, _ = t.config.KVBackend.Get(path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530307 if kvResult == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530308 log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
309 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700310 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530311 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700312 if err = json.Unmarshal(value, resPtr); err != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530313 log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
314 return nil, errors.New("error-unmarshal-kv-result")
315 } else {
316 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700317 }
318 }
319 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530320 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700321}
322
323func (t *TechProfileMgr) addTechProfInstanceToKVStore(techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
324 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
325 log.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
326 tpInstanceJson, err := json.Marshal(*tpInstance)
327 if err == nil {
328 // Backend will convert JSON byte array into string format
329 log.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
330 err = t.config.KVBackend.Put(path, tpInstanceJson)
331 } else {
332 log.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
333 }
334 return err
335}
336func (t *TechProfileMgr) getTPFromKVStore(techProfiletblID uint32) *DefaultTechProfile {
337 var kvtechprofile DefaultTechProfile
338 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
339 log.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
340 kvresult, err := t.config.KVBackend.Get(key)
341 if err != nil {
342 log.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
343 return nil
344 }
345 if kvresult != nil {
346 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
347 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000348 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
349 log.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
350 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700351 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000352
353 log.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
354 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700355 }
356 }
357 return nil
358}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000359
360func (t *TechProfileMgr) CreateTechProfInstance(techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700361 var tpInstance *TechProfile
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530362 log.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530363
364 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
365 if !uniPortNameFormat.Match([]byte(uniPortName)) {
366 log.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000367 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530368 }
369
Scott Baker2c1c4822019-10-16 11:02:41 -0700370 tp := t.getTPFromKVStore(techProfiletblID)
371 if tp != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530372 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
373 log.Error("invalid-instance-ctrl-attr--using-default-tp")
374 tp = t.getDefaultTechProfile()
375 } else {
376 log.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
377 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700378 } else {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530379 log.Info("tp-not-found-on-kv--creating-default-tp")
Scott Baker2c1c4822019-10-16 11:02:41 -0700380 tp = t.getDefaultTechProfile()
Scott Baker2c1c4822019-10-16 11:02:41 -0700381 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530382 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
383 if tpInstance = t.allocateTPInstance(uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
384 log.Error("tp-intance-allocation-failed")
Girish Kumarb3c52d52019-12-06 12:14:14 +0000385 return nil, errors.New("tp-intance-allocation-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700386 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530387 if err := t.addTechProfInstanceToKVStore(techProfiletblID, uniPortName, tpInstance); err != nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000388 log.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
389 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530390 }
391 log.Infow("tp-added-to-kv-store-successfully",
Scott Baker2c1c4822019-10-16 11:02:41 -0700392 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000393 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700394}
395
396func (t *TechProfileMgr) DeleteTechProfileInstance(techProfiletblID uint32, uniPortName string) error {
397 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
398 return t.config.KVBackend.Delete(path)
399}
400
Girish Gowdra9447baf2019-11-05 16:42:37 +0530401func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
402 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
403 log.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
404 return errors.New("invalid-onu-instance-ctl-attr")
405 }
406
407 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
408 log.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
409 return errors.New("invalid-uni-instance-ctl-attr")
410 }
411
412 if instCtl.Uni == "multi-instance" {
413 log.Error("uni-multi-instance-tp-not-supported")
414 return errors.New("uni-multi-instance-tp-not-supported")
415 }
416
417 return nil
418}
419
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530420func (t *TechProfileMgr) allocateTPInstance(uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700421
422 var usGemPortAttributeList []iGemPortAttribute
423 var dsGemPortAttributeList []iGemPortAttribute
Esin Karaman8aa75a72019-12-20 13:11:59 +0000424 var dsMulticastGemAttributeList []iGemPortAttribute
425 var dsUnicastGemAttributeList []iGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700426 var tcontIDs []uint32
427 var gemPorts []uint32
428 var err error
429
Girish Gowdra9447baf2019-11-05 16:42:37 +0530430 log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
431
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530432 if tp.InstanceCtrl.Onu == "multi-instance" {
433 if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
434 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
435 return nil
436 }
437 } else { // "single-instance"
438 tpInst, err := t.getSingleInstanceTp(tpInstPath)
439 if tpInst == nil {
440 // No "single-instance" tp found on one any uni port for the given TP ID
441 // Allocate a new TcontID or AllocID
442 if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
443 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
444 return nil
445 }
446 } else {
447 // Use the alloc-id from the existing TpInstance
448 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
449 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700450 }
451 log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
452 if gemPorts, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
453 log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
454 return nil
455 }
456 log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
457 for index := 0; index < int(tp.NumGemPorts); index++ {
458 usGemPortAttributeList = append(usGemPortAttributeList,
459 iGemPortAttribute{GemportID: gemPorts[index],
460 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
461 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
462 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
463 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
464 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
465 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
466 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
467 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000468 }
469
470 log.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
471 //put multicast and unicast downstream GEM port attributes in different lists first
472 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
473 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
474 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
475 iGemPortAttribute{
476 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
477 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
478 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
479 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
480 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
481 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
482 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
483 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
484 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
485 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
486 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
487 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
488 } else {
489 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
490 iGemPortAttribute{
491 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
492 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
493 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
494 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
495 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
496 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
497 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
498 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
499 }
500 }
501 //add unicast downstream GEM ports to dsGemPortAttributeList
502 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700503 dsGemPortAttributeList = append(dsGemPortAttributeList,
504 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000505 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
506 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
507 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
508 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
509 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
510 Weight: dsUnicastGemAttributeList[index].Weight,
511 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
512 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700513 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000514 //add multicast GEM ports to dsGemPortAttributeList afterwards
515 for k := range dsMulticastGemAttributeList {
516 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
517 }
518
Scott Baker2c1c4822019-10-16 11:02:41 -0700519 return &TechProfile{
520 SubscriberIdentifier: uniPortName,
521 Name: tp.Name,
522 ProfileType: tp.ProfileType,
523 Version: tp.Version,
524 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700525 InstanceCtrl: tp.InstanceCtrl,
526 UsScheduler: iScheduler{
527 AllocID: tcontIDs[0],
528 Direction: tp.UsScheduler.Direction,
529 AdditionalBw: tp.UsScheduler.AdditionalBw,
530 Priority: tp.UsScheduler.Priority,
531 Weight: tp.UsScheduler.Weight,
532 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
533 DsScheduler: iScheduler{
534 AllocID: tcontIDs[0],
535 Direction: tp.DsScheduler.Direction,
536 AdditionalBw: tp.DsScheduler.AdditionalBw,
537 Priority: tp.DsScheduler.Priority,
538 Weight: tp.DsScheduler.Weight,
539 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
540 UpstreamGemPortAttributeList: usGemPortAttributeList,
541 DownstreamGemPortAttributeList: dsGemPortAttributeList}
542}
543
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530544// getSingleInstanceTp returns another TpInstance for an ONU on a different
545// uni port for the same TP ID, if it finds one, else nil.
546func (t *TechProfileMgr) getSingleInstanceTp(tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530547 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530548
549 // For example:
550 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
551 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
552 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
553 kvPairs, _ := t.config.KVBackend.List(uniPathSlice[0])
554
Girish Gowdra9447baf2019-11-05 16:42:37 +0530555 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
556 for keyPath, kvPair := range kvPairs {
557 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
558 if err = json.Unmarshal(value, &tpInst); err != nil {
559 log.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
560 return nil, errors.New("error-unmarshal-kv-pair")
561 } else {
562 log.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530563 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530564 }
565 }
566 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530567 return nil, nil
568}
569
Scott Baker2c1c4822019-10-16 11:02:41 -0700570func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
571
572 var usGemPortAttributeList []GemPortAttribute
573 var dsGemPortAttributeList []GemPortAttribute
574
575 for _, pbit := range t.config.DefaultPbits {
576 log.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
577 usGemPortAttributeList = append(usGemPortAttributeList,
578 GemPortAttribute{
579 MaxQueueSize: defaultMaxQueueSize,
580 PbitMap: pbit,
581 AesEncryption: defaultAESEncryption,
582 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
583 PriorityQueue: defaultPriorityQueue,
584 Weight: defaultQueueWeight,
585 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
586 DiscardConfig: DiscardConfig{
587 MinThreshold: defaultMinThreshold,
588 MaxThreshold: defaultMaxThreshold,
589 MaxProbability: defaultMaxProbability}})
590 dsGemPortAttributeList = append(dsGemPortAttributeList,
591 GemPortAttribute{
592 MaxQueueSize: defaultMaxQueueSize,
593 PbitMap: pbit,
594 AesEncryption: defaultAESEncryption,
595 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
596 PriorityQueue: defaultPriorityQueue,
597 Weight: defaultQueueWeight,
598 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
599 DiscardConfig: DiscardConfig{
600 MinThreshold: defaultMinThreshold,
601 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000602 MaxProbability: defaultMaxProbability},
603 IsMulticast: defaultIsMulticast,
604 DControlList: defaultAccessControlList,
605 SControlList: defaultAccessControlList,
606 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700607 }
608 return &DefaultTechProfile{
609 Name: t.config.DefaultTPName,
610 ProfileType: t.resourceMgr.GetTechnology(),
611 Version: t.config.TPVersion,
612 NumGemPorts: uint32(len(usGemPortAttributeList)),
613 InstanceCtrl: InstanceControl{
614 Onu: defaultOnuInstance,
615 Uni: defaultUniInstance,
616 MaxGemPayloadSize: defaultGemPayloadSize},
617 UsScheduler: Scheduler{
618 Direction: Direction_name[Direction_UPSTREAM],
619 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
620 Priority: defaultPriority,
621 Weight: defaultWeight,
622 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
623 DsScheduler: Scheduler{
624 Direction: Direction_name[Direction_DOWNSTREAM],
625 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
626 Priority: defaultPriority,
627 Weight: defaultWeight,
628 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
629 UpstreamGemPortAttributeList: usGemPortAttributeList,
630 DownstreamGemPortAttributeList: dsGemPortAttributeList}
631}
632
633func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
634 var result int32 = -1
635
636 if paramType == "direction" {
637 for key, val := range tp_pb.Direction_value {
638 if key == paramKey {
639 result = val
640 }
641 }
642 } else if paramType == "discard_policy" {
643 for key, val := range tp_pb.DiscardPolicy_value {
644 if key == paramKey {
645 result = val
646 }
647 }
648 } else if paramType == "sched_policy" {
649 for key, val := range tp_pb.SchedulingPolicy_value {
650 if key == paramKey {
651 log.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
652 result = val
653 }
654 }
655 } else if paramType == "additional_bw" {
656 for key, val := range tp_pb.AdditionalBW_value {
657 if key == paramKey {
658 result = val
659 }
660 }
661 } else {
662 log.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
663 return -1
664 }
665 log.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
666 return result
667}
668
Girish Kumarb3c52d52019-12-06 12:14:14 +0000669func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700670 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
671 if dir == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000672 log.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
673 return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Scott Baker2c1c4822019-10-16 11:02:41 -0700674 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000675
Scott Baker2c1c4822019-10-16 11:02:41 -0700676 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
677 if bw == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000678 log.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
679 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Scott Baker2c1c4822019-10-16 11:02:41 -0700680 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000681
Scott Baker2c1c4822019-10-16 11:02:41 -0700682 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
683 if policy == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000684 log.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
685 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Scott Baker2c1c4822019-10-16 11:02:41 -0700686 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000687
Scott Baker2c1c4822019-10-16 11:02:41 -0700688 return &tp_pb.SchedulerConfig{
689 Direction: dir,
690 AdditionalBw: bw,
691 Priority: tpInstance.UsScheduler.Priority,
692 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000693 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700694}
695
Girish Kumarb3c52d52019-12-06 12:14:14 +0000696func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700697
698 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
699 if dir == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000700 log.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
701 return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Scott Baker2c1c4822019-10-16 11:02:41 -0700702 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000703
Scott Baker2c1c4822019-10-16 11:02:41 -0700704 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
705 if bw == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000706 log.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
707 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Scott Baker2c1c4822019-10-16 11:02:41 -0700708 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000709
Scott Baker2c1c4822019-10-16 11:02:41 -0700710 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
711 if policy == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000712 log.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
713 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Scott Baker2c1c4822019-10-16 11:02:41 -0700714 }
715
716 return &tp_pb.SchedulerConfig{
717 Direction: dir,
718 AdditionalBw: bw,
719 Priority: tpInstance.DsScheduler.Priority,
720 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000721 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700722}
723
724func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
725 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
726
727 tSched := &tp_pb.TrafficScheduler{
728 Direction: SchedCfg.Direction,
729 AllocId: tpInstance.UsScheduler.AllocID,
730 TrafficShapingInfo: ShapingCfg,
731 Scheduler: SchedCfg}
732
733 return tSched
734}
735
Girish Kumarb3c52d52019-12-06 12:14:14 +0000736func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700737
738 var encryp bool
739 if Dir == tp_pb.Direction_UPSTREAM {
740 // upstream GEM ports
741 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
742 GemPorts := make([]*tp_pb.TrafficQueue, 0)
743 for Count := 0; Count < NumGemPorts; Count++ {
744 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
745 encryp = true
746 } else {
747 encryp = false
748 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000749
750 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
751 if schedPolicy == -1 {
752 log.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
753 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
754 }
755
756 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
757 if discardPolicy == -1 {
758 log.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
759 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
760 }
761
Scott Baker2c1c4822019-10-16 11:02:41 -0700762 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
763 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
764 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
765 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
766 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000767 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700768 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
769 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000770 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700771 })
772 }
773 log.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000774 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700775 } else if Dir == tp_pb.Direction_DOWNSTREAM {
776 //downstream GEM ports
777 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
778 GemPorts := make([]*tp_pb.TrafficQueue, 0)
779 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000780 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
781 //do not take multicast GEM ports. They are handled separately.
782 continue
783 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700784 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
785 encryp = true
786 } else {
787 encryp = false
788 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000789
790 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
791 if schedPolicy == -1 {
792 log.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
793 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
794 }
795
796 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
797 if discardPolicy == -1 {
798 log.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
799 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
800 }
801
Scott Baker2c1c4822019-10-16 11:02:41 -0700802 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
803 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
804 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
805 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
806 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000807 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700808 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
809 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000810 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700811 })
812 }
813 log.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000814 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700815 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000816
817 log.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
818 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -0700819}
820
Esin Karaman8aa75a72019-12-20 13:11:59 +0000821//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
822func isMulticastGem(isMulticastAttrValue string) bool {
823 return isMulticastAttrValue != "" &&
824 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
825}
826
827func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
828 var encryp bool
829 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
830 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
831 for Count := 0; Count < NumGemPorts; Count++ {
832 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
833 continue
834 }
835 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
836 encryp = true
837 } else {
838 encryp = false
839 }
840 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
841 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
842 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
843 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
844 AesEncryption: encryp,
845 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
846 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
847 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
848 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
849 })
850 }
851 log.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
852 return mcastTrafficQueues
853}
854
Scott Baker2c1c4822019-10-16 11:02:41 -0700855func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000856 UsScheduler, _ := tpm.GetUsScheduler(tp)
Scott Baker2c1c4822019-10-16 11:02:41 -0700857
858 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
859 AllocId: tp.UsScheduler.AllocID,
860 Scheduler: UsScheduler}
861}
862
863func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 {
864 /*
865 Function to get the Gemport ID mapped to a pbit.
866 */
867 if Dir == tp_pb.Direction_UPSTREAM {
868 // upstream GEM ports
869 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
870 for Count := 0; Count < NumGemPorts; Count++ {
871 NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap)
872 for ICount := 2; ICount < NumPbitMaps; ICount++ {
873 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
874 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
875 log.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID})
876 return tp.UpstreamGemPortAttributeList[Count].GemportID
877 }
878 }
879 }
880 }
881 } else if Dir == tp_pb.Direction_DOWNSTREAM {
882 //downstream GEM ports
883 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
884 for Count := 0; Count < NumGemPorts; Count++ {
885 NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap)
886 for ICount := 2; ICount < NumPbitMaps; ICount++ {
887 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
888 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
889 log.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID})
890 return tp.DownstreamGemPortAttributeList[Count].GemportID
891 }
892 }
893 }
894 }
895 }
896 log.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
897 return 0
898}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530899
900// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
901func (t *TechProfileMgr) FindAllTpInstances(techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
902 var tp TechProfile
Girish Gowdra6172a2e2019-11-27 14:37:58 +0530903 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530904
905 if kvPairs, _ := t.config.KVBackend.List(onuTpInstancePath); kvPairs != nil {
906 tpInstances := make([]TechProfile, 0, len(kvPairs))
907 for kvPath, kvPair := range kvPairs {
908 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
909 if err = json.Unmarshal(value, &tp); err != nil {
910 log.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
911 continue
912 } else {
913 tpInstances = append(tpInstances, tp)
914 }
915 }
916 }
917 return tpInstances
918 }
919 return nil
920}