blob: b268a4c13dd41fd42ca4f392030e55dc38852891 [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 (
npujar5bf737f2020-01-16 19:35:25 +053020 "context"
Scott Baker2c1c4822019-10-16 11:02:41 -070021 "encoding/json"
22 "errors"
23 "fmt"
Girish Gowdra9447baf2019-11-05 16:42:37 +053024 "regexp"
Scott Baker2c1c4822019-10-16 11:02:41 -070025 "strconv"
26
serkant.uluderyab38671c2019-11-01 09:35:38 -070027 "github.com/opencord/voltha-lib-go/v3/pkg/db"
28
29 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 tp_pb "github.com/opencord/voltha-protos/v3/go/tech_profile"
Scott Baker2c1c4822019-10-16 11:02:41 -070032)
33
34// Interface to pon resource manager APIs
35type iPonResourceMgr interface {
npujar5bf737f2020-01-16 19:35:25 +053036 GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
Scott Baker2c1c4822019-10-16 11:02:41 -070037 GetResourceTypeAllocID() string
38 GetResourceTypeGemPortID() string
39 GetTechnology() string
40}
41
42type Direction int32
43
44const (
45 Direction_UPSTREAM Direction = 0
46 Direction_DOWNSTREAM Direction = 1
47 Direction_BIDIRECTIONAL Direction = 2
48)
49
50var Direction_name = map[Direction]string{
51 0: "UPSTREAM",
52 1: "DOWNSTREAM",
53 2: "BIDIRECTIONAL",
54}
55
56type SchedulingPolicy int32
57
58const (
59 SchedulingPolicy_WRR SchedulingPolicy = 0
60 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
61 SchedulingPolicy_Hybrid SchedulingPolicy = 2
62)
63
64var SchedulingPolicy_name = map[SchedulingPolicy]string{
65 0: "WRR",
66 1: "StrictPriority",
67 2: "Hybrid",
68}
69
70type AdditionalBW int32
71
72const (
73 AdditionalBW_AdditionalBW_None AdditionalBW = 0
74 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
75 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
76 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
77)
78
79var AdditionalBW_name = map[AdditionalBW]string{
80 0: "AdditionalBW_None",
81 1: "AdditionalBW_NA",
82 2: "AdditionalBW_BestEffort",
83 3: "AdditionalBW_Auto",
84}
85
86type DiscardPolicy int32
87
88const (
89 DiscardPolicy_TailDrop DiscardPolicy = 0
90 DiscardPolicy_WTailDrop DiscardPolicy = 1
91 DiscardPolicy_Red DiscardPolicy = 2
92 DiscardPolicy_WRed DiscardPolicy = 3
93)
94
95var DiscardPolicy_name = map[DiscardPolicy]string{
96 0: "TailDrop",
97 1: "WTailDrop",
98 2: "Red",
99 3: "WRed",
100}
101
Girish Gowdra9447baf2019-11-05 16:42:37 +0530102// Required uniPortName format
103var uniPortNameFormat = regexp.MustCompile(`^pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
104
Scott Baker2c1c4822019-10-16 11:02:41 -0700105/*
106type InferredAdditionBWIndication int32
107
108const (
109 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
110 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
111 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
112)
113
114var InferredAdditionBWIndication_name = map[int32]string{
115 0: "InferredAdditionBWIndication_None",
116 1: "InferredAdditionBWIndication_Assured",
117 2: "InferredAdditionBWIndication_BestEffort",
118}
119*/
120// instance control defaults
121const (
122 defaultOnuInstance = "multi-instance"
123 defaultUniInstance = "single-instance"
124 defaultNumGemPorts = 1
125 defaultGemPayloadSize = "auto"
126)
127
128const MAX_GEM_PAYLOAD = "max_gem_payload_size"
129
130type InstanceControl struct {
131 Onu string `json:"ONU"`
132 Uni string `json:"uni"`
133 MaxGemPayloadSize string `json:"max_gem_payload_size"`
134}
135
136// default discard config constants
137const (
138 defaultMinThreshold = 0
139 defaultMaxThreshold = 0
140 defaultMaxProbability = 0
141)
142
143type DiscardConfig struct {
144 MinThreshold int `json:"min_threshold"`
145 MaxThreshold int `json:"max_threshold"`
146 MaxProbability int `json:"max_probability"`
147}
148
149// default scheduler contants
150const (
151 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
152 defaultPriority = 0
153 defaultWeight = 0
154 defaultQueueSchedPolicy = SchedulingPolicy_Hybrid
155)
156
157type Scheduler struct {
158 Direction string `json:"direction"`
159 AdditionalBw string `json:"additional_bw"`
160 Priority uint32 `json:"priority"`
161 Weight uint32 `json:"weight"`
162 QSchedPolicy string `json:"q_sched_policy"`
163}
164
165// default GEM attribute constants
166const (
Esin Karaman8aa75a72019-12-20 13:11:59 +0000167 defaultAESEncryption = "True"
168 defaultPriorityQueue = 0
169 defaultQueueWeight = 0
170 defaultMaxQueueSize = "auto"
171 defaultdropPolicy = DiscardPolicy_TailDrop
172 defaultSchedulePolicy = SchedulingPolicy_WRR
173 defaultIsMulticast = "False"
174 defaultAccessControlList = "224.0.0.0-239.255.255.255"
175 defaultMcastGemID = 4069
Scott Baker2c1c4822019-10-16 11:02:41 -0700176)
177
178type GemPortAttribute struct {
179 MaxQueueSize string `json:"max_q_size"`
180 PbitMap string `json:"pbit_map"`
181 AesEncryption string `json:"aes_encryption"`
182 SchedulingPolicy string `json:"scheduling_policy"`
183 PriorityQueue uint32 `json:"priority_q"`
184 Weight uint32 `json:"weight"`
185 DiscardPolicy string `json:"discard_policy"`
186 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000187 IsMulticast string `json:"is_multicast"`
188 DControlList string `json:"dynamic_access_control_list"`
189 SControlList string `json:"static_access_control_list"`
190 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700191}
192
193type iScheduler struct {
194 AllocID uint32 `json:"alloc_id"`
195 Direction string `json:"direction"`
196 AdditionalBw string `json:"additional_bw"`
197 Priority uint32 `json:"priority"`
198 Weight uint32 `json:"weight"`
199 QSchedPolicy string `json:"q_sched_policy"`
200}
201type iGemPortAttribute struct {
202 GemportID uint32 `json:"gemport_id"`
203 MaxQueueSize string `json:"max_q_size"`
204 PbitMap string `json:"pbit_map"`
205 AesEncryption string `json:"aes_encryption"`
206 SchedulingPolicy string `json:"scheduling_policy"`
207 PriorityQueue uint32 `json:"priority_q"`
208 Weight uint32 `json:"weight"`
209 DiscardPolicy string `json:"discard_policy"`
210 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000211 IsMulticast string `json:"is_multicast"`
212 DControlList string `json:"dynamic_access_control_list"`
213 SControlList string `json:"static_access_control_list"`
214 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700215}
216
217type TechProfileMgr struct {
218 config *TechProfileFlags
219 resourceMgr iPonResourceMgr
220}
221type DefaultTechProfile struct {
222 Name string `json:"name"`
223 ProfileType string `json:"profile_type"`
224 Version int `json:"version"`
225 NumGemPorts uint32 `json:"num_gem_ports"`
226 InstanceCtrl InstanceControl `json:"instance_control"`
227 UsScheduler Scheduler `json:"us_scheduler"`
228 DsScheduler Scheduler `json:"ds_scheduler"`
229 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
230 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
231}
232type TechProfile struct {
233 Name string `json:"name"`
234 SubscriberIdentifier string `json:"subscriber_identifier"`
235 ProfileType string `json:"profile_type"`
236 Version int `json:"version"`
237 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700238 InstanceCtrl InstanceControl `json:"instance_control"`
239 UsScheduler iScheduler `json:"us_scheduler"`
240 DsScheduler iScheduler `json:"ds_scheduler"`
241 UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"`
242 DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
243}
244
sbarbari1e3e29c2019-11-05 10:06:50 -0500245func (t *TechProfileMgr) SetKVClient() *db.Backend {
Scott Baker2c1c4822019-10-16 11:02:41 -0700246 addr := t.config.KVStoreHost + ":" + strconv.Itoa(t.config.KVStorePort)
247 kvClient, err := newKVClient(t.config.KVStoreType, addr, t.config.KVStoreTimeout)
248 if err != nil {
249 log.Errorw("failed-to-create-kv-client",
250 log.Fields{
251 "type": t.config.KVStoreType, "host": t.config.KVStoreHost, "port": t.config.KVStorePort,
252 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
253 "error": err.Error(),
254 })
255 return nil
256 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500257 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700258 Client: kvClient,
259 StoreType: t.config.KVStoreType,
260 Host: t.config.KVStoreHost,
261 Port: t.config.KVStorePort,
262 Timeout: t.config.KVStoreTimeout,
263 PathPrefix: t.config.TPKVPathPrefix}
264
265 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
266 issue between kv store and backend , core is not calling NewBackend directly
267 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
268 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
269 */
270}
271
272func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
273
274 log.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
275 switch storeType {
276 case "consul":
277 return kvstore.NewConsulClient(address, timeout)
278 case "etcd":
279 return kvstore.NewEtcdClient(address, timeout)
280 }
281 return nil, errors.New("unsupported-kv-store")
282}
283
284func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreHost string, KVStorePort int) (*TechProfileMgr, error) {
285 var techprofileObj TechProfileMgr
286 log.Debug("Initializing techprofile Manager")
287 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreHost, KVStorePort)
288 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
289 if techprofileObj.config.KVBackend == nil {
290 log.Error("Failed to initialize KV backend\n")
291 return nil, errors.New("KV backend init failed")
292 }
293 techprofileObj.resourceMgr = resourceMgr
294 log.Debug("Initializing techprofile object instance success")
295 return &techprofileObj, nil
296}
297
298func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
299 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
300}
301
npujar5bf737f2020-01-16 19:35:25 +0530302func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700303 var KvTpIns TechProfile
304 var resPtr *TechProfile = &KvTpIns
305 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530306 var kvResult *kvstore.KVPair
Girish Gowdra9447baf2019-11-05 16:42:37 +0530307
npujar5bf737f2020-01-16 19:35:25 +0530308 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530309 if kvResult == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530310 log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
311 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700312 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530313 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700314 if err = json.Unmarshal(value, resPtr); err != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530315 log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
316 return nil, errors.New("error-unmarshal-kv-result")
317 } else {
318 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700319 }
320 }
321 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530322 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700323}
324
npujar5bf737f2020-01-16 19:35:25 +0530325func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Scott Baker2c1c4822019-10-16 11:02:41 -0700326 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
327 log.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
328 tpInstanceJson, err := json.Marshal(*tpInstance)
329 if err == nil {
330 // Backend will convert JSON byte array into string format
331 log.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujar5bf737f2020-01-16 19:35:25 +0530332 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Scott Baker2c1c4822019-10-16 11:02:41 -0700333 } else {
334 log.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
335 }
336 return err
337}
npujar5bf737f2020-01-16 19:35:25 +0530338func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700339 var kvtechprofile DefaultTechProfile
340 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
341 log.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujar5bf737f2020-01-16 19:35:25 +0530342 kvresult, err := t.config.KVBackend.Get(ctx, key)
Scott Baker2c1c4822019-10-16 11:02:41 -0700343 if err != nil {
344 log.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
345 return nil
346 }
347 if kvresult != nil {
348 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
349 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000350 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
351 log.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
352 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700353 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000354
355 log.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
356 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700357 }
358 }
359 return nil
360}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000361
npujar5bf737f2020-01-16 19:35:25 +0530362func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700363 var tpInstance *TechProfile
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530364 log.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530365
366 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
367 if !uniPortNameFormat.Match([]byte(uniPortName)) {
368 log.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000369 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530370 }
371
npujar5bf737f2020-01-16 19:35:25 +0530372 tp := t.getTPFromKVStore(ctx, techProfiletblID)
Scott Baker2c1c4822019-10-16 11:02:41 -0700373 if tp != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530374 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
375 log.Error("invalid-instance-ctrl-attr--using-default-tp")
376 tp = t.getDefaultTechProfile()
377 } else {
378 log.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
379 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700380 } else {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530381 log.Info("tp-not-found-on-kv--creating-default-tp")
Scott Baker2c1c4822019-10-16 11:02:41 -0700382 tp = t.getDefaultTechProfile()
Scott Baker2c1c4822019-10-16 11:02:41 -0700383 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530384 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530385 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530386 log.Error("tp-intance-allocation-failed")
Girish Kumarb3c52d52019-12-06 12:14:14 +0000387 return nil, errors.New("tp-intance-allocation-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700388 }
npujar5bf737f2020-01-16 19:35:25 +0530389 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000390 log.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
391 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530392 }
393 log.Infow("tp-added-to-kv-store-successfully",
Scott Baker2c1c4822019-10-16 11:02:41 -0700394 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000395 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700396}
397
npujar5bf737f2020-01-16 19:35:25 +0530398func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Scott Baker2c1c4822019-10-16 11:02:41 -0700399 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530400 return t.config.KVBackend.Delete(ctx, path)
Scott Baker2c1c4822019-10-16 11:02:41 -0700401}
402
Girish Gowdra9447baf2019-11-05 16:42:37 +0530403func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
404 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
405 log.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
406 return errors.New("invalid-onu-instance-ctl-attr")
407 }
408
409 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
410 log.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
411 return errors.New("invalid-uni-instance-ctl-attr")
412 }
413
414 if instCtl.Uni == "multi-instance" {
415 log.Error("uni-multi-instance-tp-not-supported")
416 return errors.New("uni-multi-instance-tp-not-supported")
417 }
418
419 return nil
420}
421
npujar5bf737f2020-01-16 19:35:25 +0530422func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700423
424 var usGemPortAttributeList []iGemPortAttribute
425 var dsGemPortAttributeList []iGemPortAttribute
Esin Karaman8aa75a72019-12-20 13:11:59 +0000426 var dsMulticastGemAttributeList []iGemPortAttribute
427 var dsUnicastGemAttributeList []iGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700428 var tcontIDs []uint32
429 var gemPorts []uint32
430 var err error
431
Girish Gowdra9447baf2019-11-05 16:42:37 +0530432 log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
433
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530434 if tp.InstanceCtrl.Onu == "multi-instance" {
npujar5bf737f2020-01-16 19:35:25 +0530435 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530436 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
437 return nil
438 }
439 } else { // "single-instance"
npujar5bf737f2020-01-16 19:35:25 +0530440 tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath)
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530441 if tpInst == nil {
442 // No "single-instance" tp found on one any uni port for the given TP ID
443 // Allocate a new TcontID or AllocID
npujar5bf737f2020-01-16 19:35:25 +0530444 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530445 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
446 return nil
447 }
448 } else {
449 // Use the alloc-id from the existing TpInstance
450 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
451 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700452 }
453 log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
npujar5bf737f2020-01-16 19:35:25 +0530454 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700455 log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
456 return nil
457 }
458 log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
459 for index := 0; index < int(tp.NumGemPorts); index++ {
460 usGemPortAttributeList = append(usGemPortAttributeList,
461 iGemPortAttribute{GemportID: gemPorts[index],
462 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
463 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
464 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
465 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
466 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
467 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
468 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
469 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000470 }
471
472 log.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
473 //put multicast and unicast downstream GEM port attributes in different lists first
474 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
475 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
476 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
477 iGemPortAttribute{
478 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
479 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
480 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
481 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
482 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
483 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
484 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
485 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
486 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
487 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
488 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
489 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
490 } else {
491 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
492 iGemPortAttribute{
493 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
494 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
495 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
496 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
497 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
498 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
499 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
500 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
501 }
502 }
503 //add unicast downstream GEM ports to dsGemPortAttributeList
504 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700505 dsGemPortAttributeList = append(dsGemPortAttributeList,
506 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000507 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
508 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
509 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
510 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
511 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
512 Weight: dsUnicastGemAttributeList[index].Weight,
513 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
514 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700515 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000516 //add multicast GEM ports to dsGemPortAttributeList afterwards
517 for k := range dsMulticastGemAttributeList {
518 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
519 }
520
Scott Baker2c1c4822019-10-16 11:02:41 -0700521 return &TechProfile{
522 SubscriberIdentifier: uniPortName,
523 Name: tp.Name,
524 ProfileType: tp.ProfileType,
525 Version: tp.Version,
526 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700527 InstanceCtrl: tp.InstanceCtrl,
528 UsScheduler: iScheduler{
529 AllocID: tcontIDs[0],
530 Direction: tp.UsScheduler.Direction,
531 AdditionalBw: tp.UsScheduler.AdditionalBw,
532 Priority: tp.UsScheduler.Priority,
533 Weight: tp.UsScheduler.Weight,
534 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
535 DsScheduler: iScheduler{
536 AllocID: tcontIDs[0],
537 Direction: tp.DsScheduler.Direction,
538 AdditionalBw: tp.DsScheduler.AdditionalBw,
539 Priority: tp.DsScheduler.Priority,
540 Weight: tp.DsScheduler.Weight,
541 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
542 UpstreamGemPortAttributeList: usGemPortAttributeList,
543 DownstreamGemPortAttributeList: dsGemPortAttributeList}
544}
545
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530546// getSingleInstanceTp returns another TpInstance for an ONU on a different
547// uni port for the same TP ID, if it finds one, else nil.
npujar5bf737f2020-01-16 19:35:25 +0530548func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530549 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530550
551 // For example:
552 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
553 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
554 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujar5bf737f2020-01-16 19:35:25 +0530555 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra9447baf2019-11-05 16:42:37 +0530556
Girish Gowdra9447baf2019-11-05 16:42:37 +0530557 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
558 for keyPath, kvPair := range kvPairs {
559 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
560 if err = json.Unmarshal(value, &tpInst); err != nil {
561 log.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
562 return nil, errors.New("error-unmarshal-kv-pair")
563 } else {
564 log.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530565 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530566 }
567 }
568 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530569 return nil, nil
570}
571
Scott Baker2c1c4822019-10-16 11:02:41 -0700572func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
573
574 var usGemPortAttributeList []GemPortAttribute
575 var dsGemPortAttributeList []GemPortAttribute
576
577 for _, pbit := range t.config.DefaultPbits {
578 log.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
579 usGemPortAttributeList = append(usGemPortAttributeList,
580 GemPortAttribute{
581 MaxQueueSize: defaultMaxQueueSize,
582 PbitMap: pbit,
583 AesEncryption: defaultAESEncryption,
584 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
585 PriorityQueue: defaultPriorityQueue,
586 Weight: defaultQueueWeight,
587 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
588 DiscardConfig: DiscardConfig{
589 MinThreshold: defaultMinThreshold,
590 MaxThreshold: defaultMaxThreshold,
591 MaxProbability: defaultMaxProbability}})
592 dsGemPortAttributeList = append(dsGemPortAttributeList,
593 GemPortAttribute{
594 MaxQueueSize: defaultMaxQueueSize,
595 PbitMap: pbit,
596 AesEncryption: defaultAESEncryption,
597 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
598 PriorityQueue: defaultPriorityQueue,
599 Weight: defaultQueueWeight,
600 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
601 DiscardConfig: DiscardConfig{
602 MinThreshold: defaultMinThreshold,
603 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000604 MaxProbability: defaultMaxProbability},
605 IsMulticast: defaultIsMulticast,
606 DControlList: defaultAccessControlList,
607 SControlList: defaultAccessControlList,
608 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700609 }
610 return &DefaultTechProfile{
611 Name: t.config.DefaultTPName,
612 ProfileType: t.resourceMgr.GetTechnology(),
613 Version: t.config.TPVersion,
614 NumGemPorts: uint32(len(usGemPortAttributeList)),
615 InstanceCtrl: InstanceControl{
616 Onu: defaultOnuInstance,
617 Uni: defaultUniInstance,
618 MaxGemPayloadSize: defaultGemPayloadSize},
619 UsScheduler: Scheduler{
620 Direction: Direction_name[Direction_UPSTREAM],
621 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
622 Priority: defaultPriority,
623 Weight: defaultWeight,
624 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
625 DsScheduler: Scheduler{
626 Direction: Direction_name[Direction_DOWNSTREAM],
627 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
628 Priority: defaultPriority,
629 Weight: defaultWeight,
630 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
631 UpstreamGemPortAttributeList: usGemPortAttributeList,
632 DownstreamGemPortAttributeList: dsGemPortAttributeList}
633}
634
635func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
636 var result int32 = -1
637
638 if paramType == "direction" {
639 for key, val := range tp_pb.Direction_value {
640 if key == paramKey {
641 result = val
642 }
643 }
644 } else if paramType == "discard_policy" {
645 for key, val := range tp_pb.DiscardPolicy_value {
646 if key == paramKey {
647 result = val
648 }
649 }
650 } else if paramType == "sched_policy" {
651 for key, val := range tp_pb.SchedulingPolicy_value {
652 if key == paramKey {
653 log.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
654 result = val
655 }
656 }
657 } else if paramType == "additional_bw" {
658 for key, val := range tp_pb.AdditionalBW_value {
659 if key == paramKey {
660 result = val
661 }
662 }
663 } else {
664 log.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
665 return -1
666 }
667 log.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
668 return result
669}
670
Girish Kumarb3c52d52019-12-06 12:14:14 +0000671func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700672 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
673 if dir == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000674 log.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
675 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 -0700676 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000677
Scott Baker2c1c4822019-10-16 11:02:41 -0700678 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
679 if bw == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000680 log.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
681 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 -0700682 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000683
Scott Baker2c1c4822019-10-16 11:02:41 -0700684 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
685 if policy == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000686 log.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
687 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 -0700688 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000689
Scott Baker2c1c4822019-10-16 11:02:41 -0700690 return &tp_pb.SchedulerConfig{
691 Direction: dir,
692 AdditionalBw: bw,
693 Priority: tpInstance.UsScheduler.Priority,
694 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000695 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700696}
697
Girish Kumarb3c52d52019-12-06 12:14:14 +0000698func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700699
700 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
701 if dir == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000702 log.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
703 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 -0700704 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000705
Scott Baker2c1c4822019-10-16 11:02:41 -0700706 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
707 if bw == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000708 log.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
709 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 -0700710 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000711
Scott Baker2c1c4822019-10-16 11:02:41 -0700712 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
713 if policy == -1 {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000714 log.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
715 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 -0700716 }
717
718 return &tp_pb.SchedulerConfig{
719 Direction: dir,
720 AdditionalBw: bw,
721 Priority: tpInstance.DsScheduler.Priority,
722 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000723 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700724}
725
726func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
727 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
728
729 tSched := &tp_pb.TrafficScheduler{
730 Direction: SchedCfg.Direction,
731 AllocId: tpInstance.UsScheduler.AllocID,
732 TrafficShapingInfo: ShapingCfg,
733 Scheduler: SchedCfg}
734
735 return tSched
736}
737
Girish Kumarb3c52d52019-12-06 12:14:14 +0000738func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700739
740 var encryp bool
741 if Dir == tp_pb.Direction_UPSTREAM {
742 // upstream GEM ports
743 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
744 GemPorts := make([]*tp_pb.TrafficQueue, 0)
745 for Count := 0; Count < NumGemPorts; Count++ {
746 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
747 encryp = true
748 } else {
749 encryp = false
750 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000751
752 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
753 if schedPolicy == -1 {
754 log.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
755 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
756 }
757
758 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
759 if discardPolicy == -1 {
760 log.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
761 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
762 }
763
Scott Baker2c1c4822019-10-16 11:02:41 -0700764 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
765 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
766 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
767 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
768 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000769 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700770 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
771 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000772 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700773 })
774 }
775 log.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000776 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700777 } else if Dir == tp_pb.Direction_DOWNSTREAM {
778 //downstream GEM ports
779 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
780 GemPorts := make([]*tp_pb.TrafficQueue, 0)
781 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000782 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
783 //do not take multicast GEM ports. They are handled separately.
784 continue
785 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700786 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
787 encryp = true
788 } else {
789 encryp = false
790 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000791
792 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
793 if schedPolicy == -1 {
794 log.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
795 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
796 }
797
798 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
799 if discardPolicy == -1 {
800 log.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
801 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
802 }
803
Scott Baker2c1c4822019-10-16 11:02:41 -0700804 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
805 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
806 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
807 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
808 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000809 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700810 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
811 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000812 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700813 })
814 }
815 log.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000816 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700817 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000818
819 log.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
820 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -0700821}
822
Esin Karaman8aa75a72019-12-20 13:11:59 +0000823//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
824func isMulticastGem(isMulticastAttrValue string) bool {
825 return isMulticastAttrValue != "" &&
826 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
827}
828
829func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
830 var encryp bool
831 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
832 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
833 for Count := 0; Count < NumGemPorts; Count++ {
834 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
835 continue
836 }
837 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
838 encryp = true
839 } else {
840 encryp = false
841 }
842 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
843 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
844 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
845 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
846 AesEncryption: encryp,
847 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
848 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
849 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
850 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
851 })
852 }
853 log.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
854 return mcastTrafficQueues
855}
856
Scott Baker2c1c4822019-10-16 11:02:41 -0700857func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000858 UsScheduler, _ := tpm.GetUsScheduler(tp)
Scott Baker2c1c4822019-10-16 11:02:41 -0700859
860 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
861 AllocId: tp.UsScheduler.AllocID,
862 Scheduler: UsScheduler}
863}
864
865func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 {
866 /*
867 Function to get the Gemport ID mapped to a pbit.
868 */
869 if Dir == tp_pb.Direction_UPSTREAM {
870 // upstream GEM ports
871 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
872 for Count := 0; Count < NumGemPorts; Count++ {
873 NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap)
874 for ICount := 2; ICount < NumPbitMaps; ICount++ {
875 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
876 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
877 log.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID})
878 return tp.UpstreamGemPortAttributeList[Count].GemportID
879 }
880 }
881 }
882 }
883 } else if Dir == tp_pb.Direction_DOWNSTREAM {
884 //downstream GEM ports
885 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
886 for Count := 0; Count < NumGemPorts; Count++ {
887 NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap)
888 for ICount := 2; ICount < NumPbitMaps; ICount++ {
889 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
890 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
891 log.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID})
892 return tp.DownstreamGemPortAttributeList[Count].GemportID
893 }
894 }
895 }
896 }
897 }
898 log.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
899 return 0
900}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530901
902// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
npujar5bf737f2020-01-16 19:35:25 +0530903func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530904 var tp TechProfile
Girish Gowdra6172a2e2019-11-27 14:37:58 +0530905 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530906
npujar5bf737f2020-01-16 19:35:25 +0530907 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530908 tpInstances := make([]TechProfile, 0, len(kvPairs))
909 for kvPath, kvPair := range kvPairs {
910 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
911 if err = json.Unmarshal(value, &tp); err != nil {
912 log.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
913 continue
914 } else {
915 tpInstances = append(tpInstances, tp)
916 }
917 }
918 }
919 return tpInstances
920 }
921 return nil
922}