blob: 0358291ceff1d9be6dfeba27949348e822641851 [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
serkant.uluderyab38671c2019-11-01 09:35:38 -070026 "github.com/opencord/voltha-lib-go/v3/pkg/db"
27
28 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 tp_pb "github.com/opencord/voltha-protos/v3/go/tech_profile"
Scott Baker2c1c4822019-10-16 11:02:41 -070031)
32
33// Interface to pon resource manager APIs
34type iPonResourceMgr interface {
35 GetResourceID(IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
36 GetResourceTypeAllocID() string
37 GetResourceTypeGemPortID() string
38 GetTechnology() string
39}
40
41type Direction int32
42
43const (
44 Direction_UPSTREAM Direction = 0
45 Direction_DOWNSTREAM Direction = 1
46 Direction_BIDIRECTIONAL Direction = 2
47)
48
49var Direction_name = map[Direction]string{
50 0: "UPSTREAM",
51 1: "DOWNSTREAM",
52 2: "BIDIRECTIONAL",
53}
54
55type SchedulingPolicy int32
56
57const (
58 SchedulingPolicy_WRR SchedulingPolicy = 0
59 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
60 SchedulingPolicy_Hybrid SchedulingPolicy = 2
61)
62
63var SchedulingPolicy_name = map[SchedulingPolicy]string{
64 0: "WRR",
65 1: "StrictPriority",
66 2: "Hybrid",
67}
68
69type AdditionalBW int32
70
71const (
72 AdditionalBW_AdditionalBW_None AdditionalBW = 0
73 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
74 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
75 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
76)
77
78var AdditionalBW_name = map[AdditionalBW]string{
79 0: "AdditionalBW_None",
80 1: "AdditionalBW_NA",
81 2: "AdditionalBW_BestEffort",
82 3: "AdditionalBW_Auto",
83}
84
85type DiscardPolicy int32
86
87const (
88 DiscardPolicy_TailDrop DiscardPolicy = 0
89 DiscardPolicy_WTailDrop DiscardPolicy = 1
90 DiscardPolicy_Red DiscardPolicy = 2
91 DiscardPolicy_WRed DiscardPolicy = 3
92)
93
94var DiscardPolicy_name = map[DiscardPolicy]string{
95 0: "TailDrop",
96 1: "WTailDrop",
97 2: "Red",
98 3: "WRed",
99}
100
Girish Gowdra9447baf2019-11-05 16:42:37 +0530101// Required uniPortName format
102var uniPortNameFormat = regexp.MustCompile(`^pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
103
Scott Baker2c1c4822019-10-16 11:02:41 -0700104/*
105type InferredAdditionBWIndication int32
106
107const (
108 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
109 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
110 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
111)
112
113var InferredAdditionBWIndication_name = map[int32]string{
114 0: "InferredAdditionBWIndication_None",
115 1: "InferredAdditionBWIndication_Assured",
116 2: "InferredAdditionBWIndication_BestEffort",
117}
118*/
119// instance control defaults
120const (
121 defaultOnuInstance = "multi-instance"
122 defaultUniInstance = "single-instance"
123 defaultNumGemPorts = 1
124 defaultGemPayloadSize = "auto"
125)
126
127const MAX_GEM_PAYLOAD = "max_gem_payload_size"
128
129type InstanceControl struct {
130 Onu string `json:"ONU"`
131 Uni string `json:"uni"`
132 MaxGemPayloadSize string `json:"max_gem_payload_size"`
133}
134
135// default discard config constants
136const (
137 defaultMinThreshold = 0
138 defaultMaxThreshold = 0
139 defaultMaxProbability = 0
140)
141
142type DiscardConfig struct {
143 MinThreshold int `json:"min_threshold"`
144 MaxThreshold int `json:"max_threshold"`
145 MaxProbability int `json:"max_probability"`
146}
147
148// default scheduler contants
149const (
150 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
151 defaultPriority = 0
152 defaultWeight = 0
153 defaultQueueSchedPolicy = SchedulingPolicy_Hybrid
154)
155
156type Scheduler struct {
157 Direction string `json:"direction"`
158 AdditionalBw string `json:"additional_bw"`
159 Priority uint32 `json:"priority"`
160 Weight uint32 `json:"weight"`
161 QSchedPolicy string `json:"q_sched_policy"`
162}
163
164// default GEM attribute constants
165const (
Esin Karaman8aa75a72019-12-20 13:11:59 +0000166 defaultAESEncryption = "True"
167 defaultPriorityQueue = 0
168 defaultQueueWeight = 0
169 defaultMaxQueueSize = "auto"
170 defaultdropPolicy = DiscardPolicy_TailDrop
171 defaultSchedulePolicy = SchedulingPolicy_WRR
172 defaultIsMulticast = "False"
173 defaultAccessControlList = "224.0.0.0-239.255.255.255"
174 defaultMcastGemID = 4069
Scott Baker2c1c4822019-10-16 11:02:41 -0700175)
176
177type GemPortAttribute struct {
178 MaxQueueSize string `json:"max_q_size"`
179 PbitMap string `json:"pbit_map"`
180 AesEncryption string `json:"aes_encryption"`
181 SchedulingPolicy string `json:"scheduling_policy"`
182 PriorityQueue uint32 `json:"priority_q"`
183 Weight uint32 `json:"weight"`
184 DiscardPolicy string `json:"discard_policy"`
185 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000186 IsMulticast string `json:"is_multicast"`
187 DControlList string `json:"dynamic_access_control_list"`
188 SControlList string `json:"static_access_control_list"`
189 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700190}
191
192type iScheduler struct {
193 AllocID uint32 `json:"alloc_id"`
194 Direction string `json:"direction"`
195 AdditionalBw string `json:"additional_bw"`
196 Priority uint32 `json:"priority"`
197 Weight uint32 `json:"weight"`
198 QSchedPolicy string `json:"q_sched_policy"`
199}
200type iGemPortAttribute struct {
201 GemportID uint32 `json:"gemport_id"`
202 MaxQueueSize string `json:"max_q_size"`
203 PbitMap string `json:"pbit_map"`
204 AesEncryption string `json:"aes_encryption"`
205 SchedulingPolicy string `json:"scheduling_policy"`
206 PriorityQueue uint32 `json:"priority_q"`
207 Weight uint32 `json:"weight"`
208 DiscardPolicy string `json:"discard_policy"`
209 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000210 IsMulticast string `json:"is_multicast"`
211 DControlList string `json:"dynamic_access_control_list"`
212 SControlList string `json:"static_access_control_list"`
213 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700214}
215
216type TechProfileMgr struct {
217 config *TechProfileFlags
218 resourceMgr iPonResourceMgr
219}
220type DefaultTechProfile struct {
221 Name string `json:"name"`
222 ProfileType string `json:"profile_type"`
223 Version int `json:"version"`
224 NumGemPorts uint32 `json:"num_gem_ports"`
225 InstanceCtrl InstanceControl `json:"instance_control"`
226 UsScheduler Scheduler `json:"us_scheduler"`
227 DsScheduler Scheduler `json:"ds_scheduler"`
228 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
229 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
230}
231type TechProfile struct {
232 Name string `json:"name"`
233 SubscriberIdentifier string `json:"subscriber_identifier"`
234 ProfileType string `json:"profile_type"`
235 Version int `json:"version"`
236 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700237 InstanceCtrl InstanceControl `json:"instance_control"`
238 UsScheduler iScheduler `json:"us_scheduler"`
239 DsScheduler iScheduler `json:"ds_scheduler"`
240 UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"`
241 DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
242}
243
sbarbari1e3e29c2019-11-05 10:06:50 -0500244func (t *TechProfileMgr) SetKVClient() *db.Backend {
Scott Baker2c1c4822019-10-16 11:02:41 -0700245 addr := t.config.KVStoreHost + ":" + strconv.Itoa(t.config.KVStorePort)
246 kvClient, err := newKVClient(t.config.KVStoreType, addr, t.config.KVStoreTimeout)
247 if err != nil {
248 log.Errorw("failed-to-create-kv-client",
249 log.Fields{
250 "type": t.config.KVStoreType, "host": t.config.KVStoreHost, "port": t.config.KVStorePort,
251 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
252 "error": err.Error(),
253 })
254 return nil
255 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500256 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700257 Client: kvClient,
258 StoreType: t.config.KVStoreType,
259 Host: t.config.KVStoreHost,
260 Port: t.config.KVStorePort,
261 Timeout: t.config.KVStoreTimeout,
262 PathPrefix: t.config.TPKVPathPrefix}
263
264 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
265 issue between kv store and backend , core is not calling NewBackend directly
266 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
267 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
268 */
269}
270
271func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
272
273 log.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
274 switch storeType {
275 case "consul":
276 return kvstore.NewConsulClient(address, timeout)
277 case "etcd":
278 return kvstore.NewEtcdClient(address, timeout)
279 }
280 return nil, errors.New("unsupported-kv-store")
281}
282
283func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreHost string, KVStorePort int) (*TechProfileMgr, error) {
284 var techprofileObj TechProfileMgr
285 log.Debug("Initializing techprofile Manager")
286 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreHost, KVStorePort)
287 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
288 if techprofileObj.config.KVBackend == nil {
289 log.Error("Failed to initialize KV backend\n")
290 return nil, errors.New("KV backend init failed")
291 }
292 techprofileObj.resourceMgr = resourceMgr
293 log.Debug("Initializing techprofile object instance success")
294 return &techprofileObj, nil
295}
296
297func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
298 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
299}
300
301func (t *TechProfileMgr) GetTPInstanceFromKVStore(techProfiletblID uint32, path string) (*TechProfile, error) {
302 var KvTpIns TechProfile
303 var resPtr *TechProfile = &KvTpIns
304 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530305 var kvResult *kvstore.KVPair
Girish Gowdra9447baf2019-11-05 16:42:37 +0530306
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530307 kvResult, _ = t.config.KVBackend.Get(path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530308 if kvResult == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530309 log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
310 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700311 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530312 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700313 if err = json.Unmarshal(value, resPtr); err != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530314 log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
315 return nil, errors.New("error-unmarshal-kv-result")
316 } else {
317 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700318 }
319 }
320 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530321 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700322}
323
324func (t *TechProfileMgr) addTechProfInstanceToKVStore(techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
325 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
326 log.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
327 tpInstanceJson, err := json.Marshal(*tpInstance)
328 if err == nil {
329 // Backend will convert JSON byte array into string format
330 log.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
331 err = t.config.KVBackend.Put(path, tpInstanceJson)
332 } else {
333 log.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
334 }
335 return err
336}
337func (t *TechProfileMgr) getTPFromKVStore(techProfiletblID uint32) *DefaultTechProfile {
338 var kvtechprofile DefaultTechProfile
339 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
340 log.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
341 kvresult, err := t.config.KVBackend.Get(key)
342 if err != nil {
343 log.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
344 return nil
345 }
346 if kvresult != nil {
347 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
348 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000349 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
350 log.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
351 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700352 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000353
354 log.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
355 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700356 }
357 }
358 return nil
359}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000360
361func (t *TechProfileMgr) CreateTechProfInstance(techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700362 var tpInstance *TechProfile
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530363 log.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530364
365 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
366 if !uniPortNameFormat.Match([]byte(uniPortName)) {
367 log.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000368 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530369 }
370
Scott Baker2c1c4822019-10-16 11:02:41 -0700371 tp := t.getTPFromKVStore(techProfiletblID)
372 if tp != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530373 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
374 log.Error("invalid-instance-ctrl-attr--using-default-tp")
375 tp = t.getDefaultTechProfile()
376 } else {
377 log.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
378 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700379 } else {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530380 log.Info("tp-not-found-on-kv--creating-default-tp")
Scott Baker2c1c4822019-10-16 11:02:41 -0700381 tp = t.getDefaultTechProfile()
Scott Baker2c1c4822019-10-16 11:02:41 -0700382 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530383 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
384 if tpInstance = t.allocateTPInstance(uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
385 log.Error("tp-intance-allocation-failed")
Girish Kumarb3c52d52019-12-06 12:14:14 +0000386 return nil, errors.New("tp-intance-allocation-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700387 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530388 if err := t.addTechProfInstanceToKVStore(techProfiletblID, uniPortName, tpInstance); err != nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000389 log.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
390 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530391 }
392 log.Infow("tp-added-to-kv-store-successfully",
Scott Baker2c1c4822019-10-16 11:02:41 -0700393 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000394 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700395}
396
397func (t *TechProfileMgr) DeleteTechProfileInstance(techProfiletblID uint32, uniPortName string) error {
398 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
399 return t.config.KVBackend.Delete(path)
400}
401
Girish Gowdra9447baf2019-11-05 16:42:37 +0530402func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
403 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
404 log.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
405 return errors.New("invalid-onu-instance-ctl-attr")
406 }
407
408 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
409 log.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
410 return errors.New("invalid-uni-instance-ctl-attr")
411 }
412
413 if instCtl.Uni == "multi-instance" {
414 log.Error("uni-multi-instance-tp-not-supported")
415 return errors.New("uni-multi-instance-tp-not-supported")
416 }
417
418 return nil
419}
420
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530421func (t *TechProfileMgr) allocateTPInstance(uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700422
423 var usGemPortAttributeList []iGemPortAttribute
424 var dsGemPortAttributeList []iGemPortAttribute
Esin Karaman8aa75a72019-12-20 13:11:59 +0000425 var dsMulticastGemAttributeList []iGemPortAttribute
426 var dsUnicastGemAttributeList []iGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700427 var tcontIDs []uint32
428 var gemPorts []uint32
429 var err error
430
Girish Gowdra9447baf2019-11-05 16:42:37 +0530431 log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
432
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530433 if tp.InstanceCtrl.Onu == "multi-instance" {
434 if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
435 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
436 return nil
437 }
438 } else { // "single-instance"
439 tpInst, err := t.getSingleInstanceTp(tpInstPath)
440 if tpInst == nil {
441 // No "single-instance" tp found on one any uni port for the given TP ID
442 // Allocate a new TcontID or AllocID
443 if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
444 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
445 return nil
446 }
447 } else {
448 // Use the alloc-id from the existing TpInstance
449 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
450 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700451 }
452 log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
453 if gemPorts, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
454 log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
455 return nil
456 }
457 log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
458 for index := 0; index < int(tp.NumGemPorts); index++ {
459 usGemPortAttributeList = append(usGemPortAttributeList,
460 iGemPortAttribute{GemportID: gemPorts[index],
461 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
462 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
463 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
464 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
465 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
466 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
467 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
468 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000469 }
470
471 log.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
472 //put multicast and unicast downstream GEM port attributes in different lists first
473 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
474 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
475 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
476 iGemPortAttribute{
477 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
478 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
479 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
480 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
481 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
482 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
483 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
484 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
485 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
486 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
487 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
488 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
489 } else {
490 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
491 iGemPortAttribute{
492 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
493 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
494 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
495 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
496 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
497 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
498 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
499 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
500 }
501 }
502 //add unicast downstream GEM ports to dsGemPortAttributeList
503 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700504 dsGemPortAttributeList = append(dsGemPortAttributeList,
505 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000506 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
507 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
508 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
509 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
510 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
511 Weight: dsUnicastGemAttributeList[index].Weight,
512 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
513 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700514 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000515 //add multicast GEM ports to dsGemPortAttributeList afterwards
516 for k := range dsMulticastGemAttributeList {
517 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
518 }
519
Scott Baker2c1c4822019-10-16 11:02:41 -0700520 return &TechProfile{
521 SubscriberIdentifier: uniPortName,
522 Name: tp.Name,
523 ProfileType: tp.ProfileType,
524 Version: tp.Version,
525 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700526 InstanceCtrl: tp.InstanceCtrl,
527 UsScheduler: iScheduler{
528 AllocID: tcontIDs[0],
529 Direction: tp.UsScheduler.Direction,
530 AdditionalBw: tp.UsScheduler.AdditionalBw,
531 Priority: tp.UsScheduler.Priority,
532 Weight: tp.UsScheduler.Weight,
533 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
534 DsScheduler: iScheduler{
535 AllocID: tcontIDs[0],
536 Direction: tp.DsScheduler.Direction,
537 AdditionalBw: tp.DsScheduler.AdditionalBw,
538 Priority: tp.DsScheduler.Priority,
539 Weight: tp.DsScheduler.Weight,
540 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
541 UpstreamGemPortAttributeList: usGemPortAttributeList,
542 DownstreamGemPortAttributeList: dsGemPortAttributeList}
543}
544
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530545// getSingleInstanceTp returns another TpInstance for an ONU on a different
546// uni port for the same TP ID, if it finds one, else nil.
547func (t *TechProfileMgr) getSingleInstanceTp(tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530548 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530549
550 // For example:
551 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
552 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
553 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
554 kvPairs, _ := t.config.KVBackend.List(uniPathSlice[0])
555
Girish Gowdra9447baf2019-11-05 16:42:37 +0530556 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
557 for keyPath, kvPair := range kvPairs {
558 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
559 if err = json.Unmarshal(value, &tpInst); err != nil {
560 log.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
561 return nil, errors.New("error-unmarshal-kv-pair")
562 } else {
563 log.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530564 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530565 }
566 }
567 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530568 return nil, nil
569}
570
Scott Baker2c1c4822019-10-16 11:02:41 -0700571func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
572
573 var usGemPortAttributeList []GemPortAttribute
574 var dsGemPortAttributeList []GemPortAttribute
575
576 for _, pbit := range t.config.DefaultPbits {
577 log.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
578 usGemPortAttributeList = append(usGemPortAttributeList,
579 GemPortAttribute{
580 MaxQueueSize: defaultMaxQueueSize,
581 PbitMap: pbit,
582 AesEncryption: defaultAESEncryption,
583 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
584 PriorityQueue: defaultPriorityQueue,
585 Weight: defaultQueueWeight,
586 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
587 DiscardConfig: DiscardConfig{
588 MinThreshold: defaultMinThreshold,
589 MaxThreshold: defaultMaxThreshold,
590 MaxProbability: defaultMaxProbability}})
591 dsGemPortAttributeList = append(dsGemPortAttributeList,
592 GemPortAttribute{
593 MaxQueueSize: defaultMaxQueueSize,
594 PbitMap: pbit,
595 AesEncryption: defaultAESEncryption,
596 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
597 PriorityQueue: defaultPriorityQueue,
598 Weight: defaultQueueWeight,
599 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
600 DiscardConfig: DiscardConfig{
601 MinThreshold: defaultMinThreshold,
602 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000603 MaxProbability: defaultMaxProbability},
604 IsMulticast: defaultIsMulticast,
605 DControlList: defaultAccessControlList,
606 SControlList: defaultAccessControlList,
607 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700608 }
609 return &DefaultTechProfile{
610 Name: t.config.DefaultTPName,
611 ProfileType: t.resourceMgr.GetTechnology(),
612 Version: t.config.TPVersion,
613 NumGemPorts: uint32(len(usGemPortAttributeList)),
614 InstanceCtrl: InstanceControl{
615 Onu: defaultOnuInstance,
616 Uni: defaultUniInstance,
617 MaxGemPayloadSize: defaultGemPayloadSize},
618 UsScheduler: Scheduler{
619 Direction: Direction_name[Direction_UPSTREAM],
620 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
621 Priority: defaultPriority,
622 Weight: defaultWeight,
623 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
624 DsScheduler: Scheduler{
625 Direction: Direction_name[Direction_DOWNSTREAM],
626 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
627 Priority: defaultPriority,
628 Weight: defaultWeight,
629 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
630 UpstreamGemPortAttributeList: usGemPortAttributeList,
631 DownstreamGemPortAttributeList: dsGemPortAttributeList}
632}
633
634func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
635 var result int32 = -1
636
637 if paramType == "direction" {
638 for key, val := range tp_pb.Direction_value {
639 if key == paramKey {
640 result = val
641 }
642 }
643 } else if paramType == "discard_policy" {
644 for key, val := range tp_pb.DiscardPolicy_value {
645 if key == paramKey {
646 result = val
647 }
648 }
649 } else if paramType == "sched_policy" {
650 for key, val := range tp_pb.SchedulingPolicy_value {
651 if key == paramKey {
652 log.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
653 result = val
654 }
655 }
656 } else if paramType == "additional_bw" {
657 for key, val := range tp_pb.AdditionalBW_value {
658 if key == paramKey {
659 result = val
660 }
661 }
662 } else {
663 log.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
664 return -1
665 }
666 log.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
667 return result
668}
669
Girish Kumarb3c52d52019-12-06 12:14:14 +0000670func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700671 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
672 if dir == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000673 log.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
674 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 -0700675 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000676
Scott Baker2c1c4822019-10-16 11:02:41 -0700677 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
678 if bw == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000679 log.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
680 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 -0700681 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000682
Scott Baker2c1c4822019-10-16 11:02:41 -0700683 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
684 if policy == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000685 log.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
686 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 -0700687 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000688
Scott Baker2c1c4822019-10-16 11:02:41 -0700689 return &tp_pb.SchedulerConfig{
690 Direction: dir,
691 AdditionalBw: bw,
692 Priority: tpInstance.UsScheduler.Priority,
693 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000694 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700695}
696
Girish Kumarb3c52d52019-12-06 12:14:14 +0000697func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700698
699 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
700 if dir == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000701 log.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
702 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 -0700703 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000704
Scott Baker2c1c4822019-10-16 11:02:41 -0700705 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
706 if bw == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000707 log.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
708 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 -0700709 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000710
Scott Baker2c1c4822019-10-16 11:02:41 -0700711 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
712 if policy == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000713 log.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
714 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 -0700715 }
716
717 return &tp_pb.SchedulerConfig{
718 Direction: dir,
719 AdditionalBw: bw,
720 Priority: tpInstance.DsScheduler.Priority,
721 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000722 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700723}
724
725func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
726 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
727
728 tSched := &tp_pb.TrafficScheduler{
729 Direction: SchedCfg.Direction,
730 AllocId: tpInstance.UsScheduler.AllocID,
731 TrafficShapingInfo: ShapingCfg,
732 Scheduler: SchedCfg}
733
734 return tSched
735}
736
Girish Kumarb3c52d52019-12-06 12:14:14 +0000737func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700738
739 var encryp bool
740 if Dir == tp_pb.Direction_UPSTREAM {
741 // upstream GEM ports
742 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
743 GemPorts := make([]*tp_pb.TrafficQueue, 0)
744 for Count := 0; Count < NumGemPorts; Count++ {
745 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
746 encryp = true
747 } else {
748 encryp = false
749 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000750
751 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
752 if schedPolicy == -1 {
753 log.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
754 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
755 }
756
757 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
758 if discardPolicy == -1 {
759 log.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
760 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
761 }
762
Scott Baker2c1c4822019-10-16 11:02:41 -0700763 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
764 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
765 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
766 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
767 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000768 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700769 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
770 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000771 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700772 })
773 }
774 log.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000775 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700776 } else if Dir == tp_pb.Direction_DOWNSTREAM {
777 //downstream GEM ports
778 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
779 GemPorts := make([]*tp_pb.TrafficQueue, 0)
780 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000781 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
782 //do not take multicast GEM ports. They are handled separately.
783 continue
784 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700785 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
786 encryp = true
787 } else {
788 encryp = false
789 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000790
791 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
792 if schedPolicy == -1 {
793 log.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
794 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
795 }
796
797 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
798 if discardPolicy == -1 {
799 log.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
800 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
801 }
802
Scott Baker2c1c4822019-10-16 11:02:41 -0700803 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
804 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
805 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
806 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
807 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000808 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700809 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
810 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000811 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700812 })
813 }
814 log.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000815 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700816 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000817
818 log.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
819 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -0700820}
821
Esin Karaman8aa75a72019-12-20 13:11:59 +0000822//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
823func isMulticastGem(isMulticastAttrValue string) bool {
824 return isMulticastAttrValue != "" &&
825 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
826}
827
828func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
829 var encryp bool
830 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
831 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
832 for Count := 0; Count < NumGemPorts; Count++ {
833 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
834 continue
835 }
836 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
837 encryp = true
838 } else {
839 encryp = false
840 }
841 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
842 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
843 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
844 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
845 AesEncryption: encryp,
846 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
847 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
848 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
849 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
850 })
851 }
852 log.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
853 return mcastTrafficQueues
854}
855
Scott Baker2c1c4822019-10-16 11:02:41 -0700856func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000857 UsScheduler, _ := tpm.GetUsScheduler(tp)
Scott Baker2c1c4822019-10-16 11:02:41 -0700858
859 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
860 AllocId: tp.UsScheduler.AllocID,
861 Scheduler: UsScheduler}
862}
863
864func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 {
865 /*
866 Function to get the Gemport ID mapped to a pbit.
867 */
868 if Dir == tp_pb.Direction_UPSTREAM {
869 // upstream GEM ports
870 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
871 for Count := 0; Count < NumGemPorts; Count++ {
872 NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap)
873 for ICount := 2; ICount < NumPbitMaps; ICount++ {
874 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
875 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
876 log.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID})
877 return tp.UpstreamGemPortAttributeList[Count].GemportID
878 }
879 }
880 }
881 }
882 } else if Dir == tp_pb.Direction_DOWNSTREAM {
883 //downstream GEM ports
884 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
885 for Count := 0; Count < NumGemPorts; Count++ {
886 NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap)
887 for ICount := 2; ICount < NumPbitMaps; ICount++ {
888 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
889 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
890 log.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID})
891 return tp.DownstreamGemPortAttributeList[Count].GemportID
892 }
893 }
894 }
895 }
896 }
897 log.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
898 return 0
899}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530900
901// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
902func (t *TechProfileMgr) FindAllTpInstances(techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
903 var tp TechProfile
Girish Gowdra6172a2e2019-11-27 14:37:58 +0530904 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530905
906 if kvPairs, _ := t.config.KVBackend.List(onuTpInstancePath); kvPairs != nil {
907 tpInstances := make([]TechProfile, 0, len(kvPairs))
908 for kvPath, kvPair := range kvPairs {
909 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
910 if err = json.Unmarshal(value, &tp); err != nil {
911 log.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
912 continue
913 } else {
914 tpInstances = append(tpInstances, tp)
915 }
916 }
917 }
918 return tpInstances
919 }
920 return nil
921}