blob: 2c0edae58e9e82081e291e73d121bb43fa622ffe [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 (
165 defaultAESEncryption = "True"
166 defaultPriorityQueue = 0
167 defaultQueueWeight = 0
168 defaultMaxQueueSize = "auto"
169 defaultdropPolicy = DiscardPolicy_TailDrop
170 defaultSchedulePolicy = SchedulingPolicy_WRR
171)
172
173type GemPortAttribute struct {
174 MaxQueueSize string `json:"max_q_size"`
175 PbitMap string `json:"pbit_map"`
176 AesEncryption string `json:"aes_encryption"`
177 SchedulingPolicy string `json:"scheduling_policy"`
178 PriorityQueue uint32 `json:"priority_q"`
179 Weight uint32 `json:"weight"`
180 DiscardPolicy string `json:"discard_policy"`
181 DiscardConfig DiscardConfig `json:"discard_config"`
182}
183
184type iScheduler struct {
185 AllocID uint32 `json:"alloc_id"`
186 Direction string `json:"direction"`
187 AdditionalBw string `json:"additional_bw"`
188 Priority uint32 `json:"priority"`
189 Weight uint32 `json:"weight"`
190 QSchedPolicy string `json:"q_sched_policy"`
191}
192type iGemPortAttribute struct {
193 GemportID uint32 `json:"gemport_id"`
194 MaxQueueSize string `json:"max_q_size"`
195 PbitMap string `json:"pbit_map"`
196 AesEncryption string `json:"aes_encryption"`
197 SchedulingPolicy string `json:"scheduling_policy"`
198 PriorityQueue uint32 `json:"priority_q"`
199 Weight uint32 `json:"weight"`
200 DiscardPolicy string `json:"discard_policy"`
201 DiscardConfig DiscardConfig `json:"discard_config"`
202}
203
204type TechProfileMgr struct {
205 config *TechProfileFlags
206 resourceMgr iPonResourceMgr
207}
208type DefaultTechProfile struct {
209 Name string `json:"name"`
210 ProfileType string `json:"profile_type"`
211 Version int `json:"version"`
212 NumGemPorts uint32 `json:"num_gem_ports"`
213 InstanceCtrl InstanceControl `json:"instance_control"`
214 UsScheduler Scheduler `json:"us_scheduler"`
215 DsScheduler Scheduler `json:"ds_scheduler"`
216 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
217 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
218}
219type TechProfile struct {
220 Name string `json:"name"`
221 SubscriberIdentifier string `json:"subscriber_identifier"`
222 ProfileType string `json:"profile_type"`
223 Version int `json:"version"`
224 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700225 InstanceCtrl InstanceControl `json:"instance_control"`
226 UsScheduler iScheduler `json:"us_scheduler"`
227 DsScheduler iScheduler `json:"ds_scheduler"`
228 UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"`
229 DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
230}
231
sbarbari1e3e29c2019-11-05 10:06:50 -0500232func (t *TechProfileMgr) SetKVClient() *db.Backend {
Scott Baker2c1c4822019-10-16 11:02:41 -0700233 addr := t.config.KVStoreHost + ":" + strconv.Itoa(t.config.KVStorePort)
234 kvClient, err := newKVClient(t.config.KVStoreType, addr, t.config.KVStoreTimeout)
235 if err != nil {
236 log.Errorw("failed-to-create-kv-client",
237 log.Fields{
238 "type": t.config.KVStoreType, "host": t.config.KVStoreHost, "port": t.config.KVStorePort,
239 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
240 "error": err.Error(),
241 })
242 return nil
243 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500244 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700245 Client: kvClient,
246 StoreType: t.config.KVStoreType,
247 Host: t.config.KVStoreHost,
248 Port: t.config.KVStorePort,
249 Timeout: t.config.KVStoreTimeout,
250 PathPrefix: t.config.TPKVPathPrefix}
251
252 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
253 issue between kv store and backend , core is not calling NewBackend directly
254 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
255 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
256 */
257}
258
259func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
260
261 log.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
262 switch storeType {
263 case "consul":
264 return kvstore.NewConsulClient(address, timeout)
265 case "etcd":
266 return kvstore.NewEtcdClient(address, timeout)
267 }
268 return nil, errors.New("unsupported-kv-store")
269}
270
271func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreHost string, KVStorePort int) (*TechProfileMgr, error) {
272 var techprofileObj TechProfileMgr
273 log.Debug("Initializing techprofile Manager")
274 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreHost, KVStorePort)
275 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
276 if techprofileObj.config.KVBackend == nil {
277 log.Error("Failed to initialize KV backend\n")
278 return nil, errors.New("KV backend init failed")
279 }
280 techprofileObj.resourceMgr = resourceMgr
281 log.Debug("Initializing techprofile object instance success")
282 return &techprofileObj, nil
283}
284
285func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
286 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
287}
288
289func (t *TechProfileMgr) GetTPInstanceFromKVStore(techProfiletblID uint32, path string) (*TechProfile, error) {
290 var KvTpIns TechProfile
291 var resPtr *TechProfile = &KvTpIns
292 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530293 var kvResult *kvstore.KVPair
Girish Gowdra9447baf2019-11-05 16:42:37 +0530294
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530295 kvResult, _ = t.config.KVBackend.Get(path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530296 if kvResult == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530297 log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
298 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700299 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530300 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700301 if err = json.Unmarshal(value, resPtr); err != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530302 log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
303 return nil, errors.New("error-unmarshal-kv-result")
304 } else {
305 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700306 }
307 }
308 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530309 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700310}
311
312func (t *TechProfileMgr) addTechProfInstanceToKVStore(techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
313 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
314 log.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
315 tpInstanceJson, err := json.Marshal(*tpInstance)
316 if err == nil {
317 // Backend will convert JSON byte array into string format
318 log.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
319 err = t.config.KVBackend.Put(path, tpInstanceJson)
320 } else {
321 log.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
322 }
323 return err
324}
325func (t *TechProfileMgr) getTPFromKVStore(techProfiletblID uint32) *DefaultTechProfile {
326 var kvtechprofile DefaultTechProfile
327 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
328 log.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
329 kvresult, err := t.config.KVBackend.Get(key)
330 if err != nil {
331 log.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
332 return nil
333 }
334 if kvresult != nil {
335 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
336 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
337 if err = json.Unmarshal(value, &kvtechprofile); err == nil {
338 log.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
339 return &kvtechprofile
340 }
341 }
342 }
343 return nil
344}
345func (t *TechProfileMgr) CreateTechProfInstance(techProfiletblID uint32, uniPortName string, intfId uint32) *TechProfile {
346 var tpInstance *TechProfile
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530347 log.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530348
349 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
350 if !uniPortNameFormat.Match([]byte(uniPortName)) {
351 log.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
352 return nil
353 }
354
Scott Baker2c1c4822019-10-16 11:02:41 -0700355 tp := t.getTPFromKVStore(techProfiletblID)
356 if tp != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530357 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
358 log.Error("invalid-instance-ctrl-attr--using-default-tp")
359 tp = t.getDefaultTechProfile()
360 } else {
361 log.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
362 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700363 } else {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530364 log.Info("tp-not-found-on-kv--creating-default-tp")
Scott Baker2c1c4822019-10-16 11:02:41 -0700365 tp = t.getDefaultTechProfile()
Scott Baker2c1c4822019-10-16 11:02:41 -0700366 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530367 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
368 if tpInstance = t.allocateTPInstance(uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
369 log.Error("tp-intance-allocation-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700370 return nil
371 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530372 if err := t.addTechProfInstanceToKVStore(techProfiletblID, uniPortName, tpInstance); err != nil {
373 log.Errorw("error-adding-tp-to-kv-store ", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
374 return nil
375 }
376 log.Infow("tp-added-to-kv-store-successfully",
Scott Baker2c1c4822019-10-16 11:02:41 -0700377 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
378 return tpInstance
379}
380
381func (t *TechProfileMgr) DeleteTechProfileInstance(techProfiletblID uint32, uniPortName string) error {
382 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
383 return t.config.KVBackend.Delete(path)
384}
385
Girish Gowdra9447baf2019-11-05 16:42:37 +0530386func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
387 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
388 log.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
389 return errors.New("invalid-onu-instance-ctl-attr")
390 }
391
392 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
393 log.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
394 return errors.New("invalid-uni-instance-ctl-attr")
395 }
396
397 if instCtl.Uni == "multi-instance" {
398 log.Error("uni-multi-instance-tp-not-supported")
399 return errors.New("uni-multi-instance-tp-not-supported")
400 }
401
402 return nil
403}
404
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530405func (t *TechProfileMgr) allocateTPInstance(uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700406
407 var usGemPortAttributeList []iGemPortAttribute
408 var dsGemPortAttributeList []iGemPortAttribute
409 var tcontIDs []uint32
410 var gemPorts []uint32
411 var err error
412
Girish Gowdra9447baf2019-11-05 16:42:37 +0530413 log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
414
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530415 if tp.InstanceCtrl.Onu == "multi-instance" {
416 if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
417 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
418 return nil
419 }
420 } else { // "single-instance"
421 tpInst, err := t.getSingleInstanceTp(tpInstPath)
422 if tpInst == nil {
423 // No "single-instance" tp found on one any uni port for the given TP ID
424 // Allocate a new TcontID or AllocID
425 if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
426 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
427 return nil
428 }
429 } else {
430 // Use the alloc-id from the existing TpInstance
431 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
432 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700433 }
434 log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
435 if gemPorts, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
436 log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
437 return nil
438 }
439 log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
440 for index := 0; index < int(tp.NumGemPorts); index++ {
441 usGemPortAttributeList = append(usGemPortAttributeList,
442 iGemPortAttribute{GemportID: gemPorts[index],
443 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
444 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
445 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
446 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
447 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
448 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
449 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
450 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
451 dsGemPortAttributeList = append(dsGemPortAttributeList,
452 iGemPortAttribute{GemportID: gemPorts[index],
453 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
454 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
455 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
456 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
457 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
458 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
459 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
460 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
461 }
462 return &TechProfile{
463 SubscriberIdentifier: uniPortName,
464 Name: tp.Name,
465 ProfileType: tp.ProfileType,
466 Version: tp.Version,
467 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700468 InstanceCtrl: tp.InstanceCtrl,
469 UsScheduler: iScheduler{
470 AllocID: tcontIDs[0],
471 Direction: tp.UsScheduler.Direction,
472 AdditionalBw: tp.UsScheduler.AdditionalBw,
473 Priority: tp.UsScheduler.Priority,
474 Weight: tp.UsScheduler.Weight,
475 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
476 DsScheduler: iScheduler{
477 AllocID: tcontIDs[0],
478 Direction: tp.DsScheduler.Direction,
479 AdditionalBw: tp.DsScheduler.AdditionalBw,
480 Priority: tp.DsScheduler.Priority,
481 Weight: tp.DsScheduler.Weight,
482 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
483 UpstreamGemPortAttributeList: usGemPortAttributeList,
484 DownstreamGemPortAttributeList: dsGemPortAttributeList}
485}
486
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530487// getSingleInstanceTp returns another TpInstance for an ONU on a different
488// uni port for the same TP ID, if it finds one, else nil.
489func (t *TechProfileMgr) getSingleInstanceTp(tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530490 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530491
492 // For example:
493 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
494 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
495 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
496 kvPairs, _ := t.config.KVBackend.List(uniPathSlice[0])
497
Girish Gowdra9447baf2019-11-05 16:42:37 +0530498 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
499 for keyPath, kvPair := range kvPairs {
500 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
501 if err = json.Unmarshal(value, &tpInst); err != nil {
502 log.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
503 return nil, errors.New("error-unmarshal-kv-pair")
504 } else {
505 log.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530506 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530507 }
508 }
509 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530510 return nil, nil
511}
512
Scott Baker2c1c4822019-10-16 11:02:41 -0700513func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
514
515 var usGemPortAttributeList []GemPortAttribute
516 var dsGemPortAttributeList []GemPortAttribute
517
518 for _, pbit := range t.config.DefaultPbits {
519 log.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
520 usGemPortAttributeList = append(usGemPortAttributeList,
521 GemPortAttribute{
522 MaxQueueSize: defaultMaxQueueSize,
523 PbitMap: pbit,
524 AesEncryption: defaultAESEncryption,
525 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
526 PriorityQueue: defaultPriorityQueue,
527 Weight: defaultQueueWeight,
528 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
529 DiscardConfig: DiscardConfig{
530 MinThreshold: defaultMinThreshold,
531 MaxThreshold: defaultMaxThreshold,
532 MaxProbability: defaultMaxProbability}})
533 dsGemPortAttributeList = append(dsGemPortAttributeList,
534 GemPortAttribute{
535 MaxQueueSize: defaultMaxQueueSize,
536 PbitMap: pbit,
537 AesEncryption: defaultAESEncryption,
538 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
539 PriorityQueue: defaultPriorityQueue,
540 Weight: defaultQueueWeight,
541 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
542 DiscardConfig: DiscardConfig{
543 MinThreshold: defaultMinThreshold,
544 MaxThreshold: defaultMaxThreshold,
545 MaxProbability: defaultMaxProbability}})
546 }
547 return &DefaultTechProfile{
548 Name: t.config.DefaultTPName,
549 ProfileType: t.resourceMgr.GetTechnology(),
550 Version: t.config.TPVersion,
551 NumGemPorts: uint32(len(usGemPortAttributeList)),
552 InstanceCtrl: InstanceControl{
553 Onu: defaultOnuInstance,
554 Uni: defaultUniInstance,
555 MaxGemPayloadSize: defaultGemPayloadSize},
556 UsScheduler: Scheduler{
557 Direction: Direction_name[Direction_UPSTREAM],
558 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
559 Priority: defaultPriority,
560 Weight: defaultWeight,
561 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
562 DsScheduler: Scheduler{
563 Direction: Direction_name[Direction_DOWNSTREAM],
564 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
565 Priority: defaultPriority,
566 Weight: defaultWeight,
567 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
568 UpstreamGemPortAttributeList: usGemPortAttributeList,
569 DownstreamGemPortAttributeList: dsGemPortAttributeList}
570}
571
572func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
573 var result int32 = -1
574
575 if paramType == "direction" {
576 for key, val := range tp_pb.Direction_value {
577 if key == paramKey {
578 result = val
579 }
580 }
581 } else if paramType == "discard_policy" {
582 for key, val := range tp_pb.DiscardPolicy_value {
583 if key == paramKey {
584 result = val
585 }
586 }
587 } else if paramType == "sched_policy" {
588 for key, val := range tp_pb.SchedulingPolicy_value {
589 if key == paramKey {
590 log.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
591 result = val
592 }
593 }
594 } else if paramType == "additional_bw" {
595 for key, val := range tp_pb.AdditionalBW_value {
596 if key == paramKey {
597 result = val
598 }
599 }
600 } else {
601 log.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
602 return -1
603 }
604 log.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
605 return result
606}
607
608func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) *tp_pb.SchedulerConfig {
609 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
610 if dir == -1 {
611 log.Fatal("Error in getting Proto for direction for upstream scheduler")
612 return nil
613 }
614 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
615 if bw == -1 {
616 log.Fatal("Error in getting Proto for bandwidth for upstream scheduler")
617 return nil
618 }
619 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
620 if policy == -1 {
621 log.Fatal("Error in getting Proto for scheduling policy for upstream scheduler")
622 return nil
623 }
624 return &tp_pb.SchedulerConfig{
625 Direction: dir,
626 AdditionalBw: bw,
627 Priority: tpInstance.UsScheduler.Priority,
628 Weight: tpInstance.UsScheduler.Weight,
629 SchedPolicy: policy}
630}
631
632func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) *tp_pb.SchedulerConfig {
633
634 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
635 if dir == -1 {
636 log.Fatal("Error in getting Proto for direction for downstream scheduler")
637 return nil
638 }
639 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
640 if bw == -1 {
641 log.Fatal("Error in getting Proto for bandwidth for downstream scheduler")
642 return nil
643 }
644 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
645 if policy == -1 {
646 log.Fatal("Error in getting Proto for scheduling policy for downstream scheduler")
647 return nil
648 }
649
650 return &tp_pb.SchedulerConfig{
651 Direction: dir,
652 AdditionalBw: bw,
653 Priority: tpInstance.DsScheduler.Priority,
654 Weight: tpInstance.DsScheduler.Weight,
655 SchedPolicy: policy}
656}
657
658func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
659 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
660
661 tSched := &tp_pb.TrafficScheduler{
662 Direction: SchedCfg.Direction,
663 AllocId: tpInstance.UsScheduler.AllocID,
664 TrafficShapingInfo: ShapingCfg,
665 Scheduler: SchedCfg}
666
667 return tSched
668}
669
670func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) []*tp_pb.TrafficQueue {
671
672 var encryp bool
673 if Dir == tp_pb.Direction_UPSTREAM {
674 // upstream GEM ports
675 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
676 GemPorts := make([]*tp_pb.TrafficQueue, 0)
677 for Count := 0; Count < NumGemPorts; Count++ {
678 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
679 encryp = true
680 } else {
681 encryp = false
682 }
683 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
684 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
685 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
686 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
687 AesEncryption: encryp,
688 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)),
689 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
690 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
691 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)),
692 })
693 }
694 log.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
695 return GemPorts
696 } else if Dir == tp_pb.Direction_DOWNSTREAM {
697 //downstream GEM ports
698 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
699 GemPorts := make([]*tp_pb.TrafficQueue, 0)
700 for Count := 0; Count < NumGemPorts; Count++ {
701 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
702 encryp = true
703 } else {
704 encryp = false
705 }
706 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
707 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
708 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
709 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
710 AesEncryption: encryp,
711 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
712 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
713 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
714 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
715 })
716 }
717 log.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
718 return GemPorts
719 }
720 return nil
721}
722
723func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
724 UsScheduler := tpm.GetUsScheduler(tp)
725
726 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
727 AllocId: tp.UsScheduler.AllocID,
728 Scheduler: UsScheduler}
729}
730
731func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 {
732 /*
733 Function to get the Gemport ID mapped to a pbit.
734 */
735 if Dir == tp_pb.Direction_UPSTREAM {
736 // upstream GEM ports
737 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
738 for Count := 0; Count < NumGemPorts; Count++ {
739 NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap)
740 for ICount := 2; ICount < NumPbitMaps; ICount++ {
741 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
742 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
743 log.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID})
744 return tp.UpstreamGemPortAttributeList[Count].GemportID
745 }
746 }
747 }
748 }
749 } else if Dir == tp_pb.Direction_DOWNSTREAM {
750 //downstream GEM ports
751 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
752 for Count := 0; Count < NumGemPorts; Count++ {
753 NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap)
754 for ICount := 2; ICount < NumPbitMaps; ICount++ {
755 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
756 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
757 log.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID})
758 return tp.DownstreamGemPortAttributeList[Count].GemportID
759 }
760 }
761 }
762 }
763 }
764 log.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
765 return 0
766}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530767
768// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
769func (t *TechProfileMgr) FindAllTpInstances(techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
770 var tp TechProfile
771 onuTpInstancePath := fmt.Sprintf("%d/%s/pon-{%d}/onu-{%d}", techProfiletblID, t.resourceMgr.GetTechnology(), ponIntf, onuID)
772
773 if kvPairs, _ := t.config.KVBackend.List(onuTpInstancePath); kvPairs != nil {
774 tpInstances := make([]TechProfile, 0, len(kvPairs))
775 for kvPath, kvPair := range kvPairs {
776 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
777 if err = json.Unmarshal(value, &tp); err != nil {
778 log.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
779 continue
780 } else {
781 tpInstances = append(tpInstances, tp)
782 }
783 }
784 }
785 return tpInstances
786 }
787 return nil
788}