blob: 2c6e67b3013570d25f2bbbc29d76b5da28d70511 [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"
Scott Baker2c1c4822019-10-16 11:02:41 -0700124 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 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000248 logger.Errorw("failed-to-create-kv-client",
Scott Baker2c1c4822019-10-16 11:02:41 -0700249 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
Girish Kumare6f45e82020-03-20 10:46:54 +0000273 logger.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
Scott Baker2c1c4822019-10-16 11:02:41 -0700274 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
Girish Kumare6f45e82020-03-20 10:46:54 +0000285 logger.Debug("Initializing techprofile Manager")
Scott Baker2c1c4822019-10-16 11:02:41 -0700286 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreHost, KVStorePort)
287 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
288 if techprofileObj.config.KVBackend == nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000289 logger.Error("Failed to initialize KV backend\n")
Scott Baker2c1c4822019-10-16 11:02:41 -0700290 return nil, errors.New("KV backend init failed")
291 }
292 techprofileObj.resourceMgr = resourceMgr
Girish Kumare6f45e82020-03-20 10:46:54 +0000293 logger.Debug("Initializing techprofile object instance success")
Scott Baker2c1c4822019-10-16 11:02:41 -0700294 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
npujar5bf737f2020-01-16 19:35:25 +0530301func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700302 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
npujar5bf737f2020-01-16 19:35:25 +0530307 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530308 if kvResult == nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000309 logger.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530310 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 Kumare6f45e82020-03-20 10:46:54 +0000314 logger.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530315 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
npujar5bf737f2020-01-16 19:35:25 +0530324func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Scott Baker2c1c4822019-10-16 11:02:41 -0700325 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Girish Kumare6f45e82020-03-20 10:46:54 +0000326 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700327 tpInstanceJson, err := json.Marshal(*tpInstance)
328 if err == nil {
329 // Backend will convert JSON byte array into string format
Girish Kumare6f45e82020-03-20 10:46:54 +0000330 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujar5bf737f2020-01-16 19:35:25 +0530331 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Scott Baker2c1c4822019-10-16 11:02:41 -0700332 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000333 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700334 }
335 return err
336}
npujar5bf737f2020-01-16 19:35:25 +0530337func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700338 var kvtechprofile DefaultTechProfile
339 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Girish Kumare6f45e82020-03-20 10:46:54 +0000340 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujar5bf737f2020-01-16 19:35:25 +0530341 kvresult, err := t.config.KVBackend.Get(ctx, key)
Scott Baker2c1c4822019-10-16 11:02:41 -0700342 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000343 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
Scott Baker2c1c4822019-10-16 11:02:41 -0700344 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 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000350 logger.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000351 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700352 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000353
Girish Kumare6f45e82020-03-20 10:46:54 +0000354 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000355 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700356 }
357 }
358 return nil
359}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000360
npujar5bf737f2020-01-16 19:35:25 +0530361func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700362 var tpInstance *TechProfile
Girish Kumare6f45e82020-03-20 10:46:54 +0000363 logger.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)) {
Girish Kumare6f45e82020-03-20 10:46:54 +0000367 logger.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
npujar5bf737f2020-01-16 19:35:25 +0530371 tp := t.getTPFromKVStore(ctx, techProfiletblID)
Scott Baker2c1c4822019-10-16 11:02:41 -0700372 if tp != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530373 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000374 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530375 tp = t.getDefaultTechProfile()
376 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000377 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530378 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700379 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000380 logger.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)
npujar5bf737f2020-01-16 19:35:25 +0530384 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000385 logger.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 }
npujar5bf737f2020-01-16 19:35:25 +0530388 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000389 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000390 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530391 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000392 logger.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
npujar5bf737f2020-01-16 19:35:25 +0530397func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Scott Baker2c1c4822019-10-16 11:02:41 -0700398 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530399 return t.config.KVBackend.Delete(ctx, path)
Scott Baker2c1c4822019-10-16 11:02:41 -0700400}
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" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000404 logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530405 return errors.New("invalid-onu-instance-ctl-attr")
406 }
407
408 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000409 logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530410 return errors.New("invalid-uni-instance-ctl-attr")
411 }
412
413 if instCtl.Uni == "multi-instance" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000414 logger.Error("uni-multi-instance-tp-not-supported")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530415 return errors.New("uni-multi-instance-tp-not-supported")
416 }
417
418 return nil
419}
420
npujar5bf737f2020-01-16 19:35:25 +0530421func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, 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 Kumare6f45e82020-03-20 10:46:54 +0000431 logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530432
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530433 if tp.InstanceCtrl.Onu == "multi-instance" {
npujar5bf737f2020-01-16 19:35:25 +0530434 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000435 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530436 return nil
437 }
438 } else { // "single-instance"
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800439 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000440 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800441 return nil
442 } else if tpInst == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530443 // No "single-instance" tp found on one any uni port for the given TP ID
444 // Allocate a new TcontID or AllocID
npujar5bf737f2020-01-16 19:35:25 +0530445 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000446 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530447 return nil
448 }
449 } else {
450 // Use the alloc-id from the existing TpInstance
451 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
452 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700453 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000454 logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
npujar5bf737f2020-01-16 19:35:25 +0530455 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000456 logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700457 return nil
458 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000459 logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700460 for index := 0; index < int(tp.NumGemPorts); index++ {
461 usGemPortAttributeList = append(usGemPortAttributeList,
462 iGemPortAttribute{GemportID: gemPorts[index],
463 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
464 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
465 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
466 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
467 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
468 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
469 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
470 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000471 }
472
Girish Kumare6f45e82020-03-20 10:46:54 +0000473 logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Esin Karaman8aa75a72019-12-20 13:11:59 +0000474 //put multicast and unicast downstream GEM port attributes in different lists first
475 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
476 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
477 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
478 iGemPortAttribute{
479 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
480 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
481 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
482 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
483 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
484 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
485 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
486 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
487 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
488 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
489 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
490 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
491 } else {
492 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
493 iGemPortAttribute{
494 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
495 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
496 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
497 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
498 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
499 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
500 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
501 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
502 }
503 }
504 //add unicast downstream GEM ports to dsGemPortAttributeList
505 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700506 dsGemPortAttributeList = append(dsGemPortAttributeList,
507 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000508 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
509 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
510 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
511 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
512 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
513 Weight: dsUnicastGemAttributeList[index].Weight,
514 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
515 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700516 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000517 //add multicast GEM ports to dsGemPortAttributeList afterwards
518 for k := range dsMulticastGemAttributeList {
519 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
520 }
521
Scott Baker2c1c4822019-10-16 11:02:41 -0700522 return &TechProfile{
523 SubscriberIdentifier: uniPortName,
524 Name: tp.Name,
525 ProfileType: tp.ProfileType,
526 Version: tp.Version,
527 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700528 InstanceCtrl: tp.InstanceCtrl,
529 UsScheduler: iScheduler{
530 AllocID: tcontIDs[0],
531 Direction: tp.UsScheduler.Direction,
532 AdditionalBw: tp.UsScheduler.AdditionalBw,
533 Priority: tp.UsScheduler.Priority,
534 Weight: tp.UsScheduler.Weight,
535 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
536 DsScheduler: iScheduler{
537 AllocID: tcontIDs[0],
538 Direction: tp.DsScheduler.Direction,
539 AdditionalBw: tp.DsScheduler.AdditionalBw,
540 Priority: tp.DsScheduler.Priority,
541 Weight: tp.DsScheduler.Weight,
542 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
543 UpstreamGemPortAttributeList: usGemPortAttributeList,
544 DownstreamGemPortAttributeList: dsGemPortAttributeList}
545}
546
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530547// getSingleInstanceTp returns another TpInstance for an ONU on a different
548// uni port for the same TP ID, if it finds one, else nil.
npujar5bf737f2020-01-16 19:35:25 +0530549func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530550 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530551
552 // For example:
553 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
554 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
555 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujar5bf737f2020-01-16 19:35:25 +0530556 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra9447baf2019-11-05 16:42:37 +0530557
Girish Gowdra9447baf2019-11-05 16:42:37 +0530558 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
559 for keyPath, kvPair := range kvPairs {
560 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
561 if err = json.Unmarshal(value, &tpInst); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000562 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530563 return nil, errors.New("error-unmarshal-kv-pair")
564 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000565 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530566 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530567 }
568 }
569 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530570 return nil, nil
571}
572
Scott Baker2c1c4822019-10-16 11:02:41 -0700573func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
574
575 var usGemPortAttributeList []GemPortAttribute
576 var dsGemPortAttributeList []GemPortAttribute
577
578 for _, pbit := range t.config.DefaultPbits {
Girish Kumare6f45e82020-03-20 10:46:54 +0000579 logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700580 usGemPortAttributeList = append(usGemPortAttributeList,
581 GemPortAttribute{
582 MaxQueueSize: defaultMaxQueueSize,
583 PbitMap: pbit,
584 AesEncryption: defaultAESEncryption,
585 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
586 PriorityQueue: defaultPriorityQueue,
587 Weight: defaultQueueWeight,
588 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
589 DiscardConfig: DiscardConfig{
590 MinThreshold: defaultMinThreshold,
591 MaxThreshold: defaultMaxThreshold,
592 MaxProbability: defaultMaxProbability}})
593 dsGemPortAttributeList = append(dsGemPortAttributeList,
594 GemPortAttribute{
595 MaxQueueSize: defaultMaxQueueSize,
596 PbitMap: pbit,
597 AesEncryption: defaultAESEncryption,
598 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
599 PriorityQueue: defaultPriorityQueue,
600 Weight: defaultQueueWeight,
601 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
602 DiscardConfig: DiscardConfig{
603 MinThreshold: defaultMinThreshold,
604 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000605 MaxProbability: defaultMaxProbability},
606 IsMulticast: defaultIsMulticast,
607 DControlList: defaultAccessControlList,
608 SControlList: defaultAccessControlList,
609 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700610 }
611 return &DefaultTechProfile{
612 Name: t.config.DefaultTPName,
613 ProfileType: t.resourceMgr.GetTechnology(),
614 Version: t.config.TPVersion,
615 NumGemPorts: uint32(len(usGemPortAttributeList)),
616 InstanceCtrl: InstanceControl{
617 Onu: defaultOnuInstance,
618 Uni: defaultUniInstance,
619 MaxGemPayloadSize: defaultGemPayloadSize},
620 UsScheduler: Scheduler{
621 Direction: Direction_name[Direction_UPSTREAM],
622 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
623 Priority: defaultPriority,
624 Weight: defaultWeight,
625 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
626 DsScheduler: Scheduler{
627 Direction: Direction_name[Direction_DOWNSTREAM],
628 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
629 Priority: defaultPriority,
630 Weight: defaultWeight,
631 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
632 UpstreamGemPortAttributeList: usGemPortAttributeList,
633 DownstreamGemPortAttributeList: dsGemPortAttributeList}
634}
635
636func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
637 var result int32 = -1
638
639 if paramType == "direction" {
640 for key, val := range tp_pb.Direction_value {
641 if key == paramKey {
642 result = val
643 }
644 }
645 } else if paramType == "discard_policy" {
646 for key, val := range tp_pb.DiscardPolicy_value {
647 if key == paramKey {
648 result = val
649 }
650 }
651 } else if paramType == "sched_policy" {
652 for key, val := range tp_pb.SchedulingPolicy_value {
653 if key == paramKey {
Girish Kumare6f45e82020-03-20 10:46:54 +0000654 logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
Scott Baker2c1c4822019-10-16 11:02:41 -0700655 result = val
656 }
657 }
658 } else if paramType == "additional_bw" {
659 for key, val := range tp_pb.AdditionalBW_value {
660 if key == paramKey {
661 result = val
662 }
663 }
664 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000665 logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Scott Baker2c1c4822019-10-16 11:02:41 -0700666 return -1
667 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000668 logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
Scott Baker2c1c4822019-10-16 11:02:41 -0700669 return result
670}
671
Girish Kumarb3c52d52019-12-06 12:14:14 +0000672func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700673 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
674 if dir == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000675 logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000676 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 -0700677 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000678
Scott Baker2c1c4822019-10-16 11:02:41 -0700679 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
680 if bw == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000681 logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000682 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 -0700683 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000684
Scott Baker2c1c4822019-10-16 11:02:41 -0700685 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
686 if policy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000687 logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000688 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 -0700689 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000690
Scott Baker2c1c4822019-10-16 11:02:41 -0700691 return &tp_pb.SchedulerConfig{
692 Direction: dir,
693 AdditionalBw: bw,
694 Priority: tpInstance.UsScheduler.Priority,
695 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000696 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700697}
698
Girish Kumarb3c52d52019-12-06 12:14:14 +0000699func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700700
701 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
702 if dir == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000703 logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000704 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 -0700705 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000706
Scott Baker2c1c4822019-10-16 11:02:41 -0700707 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
708 if bw == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000709 logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000710 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 -0700711 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000712
Scott Baker2c1c4822019-10-16 11:02:41 -0700713 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
714 if policy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000715 logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000716 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 -0700717 }
718
719 return &tp_pb.SchedulerConfig{
720 Direction: dir,
721 AdditionalBw: bw,
722 Priority: tpInstance.DsScheduler.Priority,
723 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000724 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700725}
726
727func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
728 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
729
730 tSched := &tp_pb.TrafficScheduler{
731 Direction: SchedCfg.Direction,
732 AllocId: tpInstance.UsScheduler.AllocID,
733 TrafficShapingInfo: ShapingCfg,
734 Scheduler: SchedCfg}
735
736 return tSched
737}
738
Girish Kumarb3c52d52019-12-06 12:14:14 +0000739func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700740
741 var encryp bool
742 if Dir == tp_pb.Direction_UPSTREAM {
743 // upstream GEM ports
744 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
745 GemPorts := make([]*tp_pb.TrafficQueue, 0)
746 for Count := 0; Count < NumGemPorts; Count++ {
747 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
748 encryp = true
749 } else {
750 encryp = false
751 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000752
753 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
754 if schedPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000755 logger.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000756 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
757 }
758
759 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
760 if discardPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000761 logger.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000762 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
763 }
764
Scott Baker2c1c4822019-10-16 11:02:41 -0700765 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
766 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
767 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
768 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
769 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000770 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700771 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
772 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000773 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700774 })
775 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000776 logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000777 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700778 } else if Dir == tp_pb.Direction_DOWNSTREAM {
779 //downstream GEM ports
780 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
781 GemPorts := make([]*tp_pb.TrafficQueue, 0)
782 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000783 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
784 //do not take multicast GEM ports. They are handled separately.
785 continue
786 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700787 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
788 encryp = true
789 } else {
790 encryp = false
791 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000792
793 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
794 if schedPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000795 logger.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000796 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
797 }
798
799 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
800 if discardPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000801 logger.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000802 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
803 }
804
Scott Baker2c1c4822019-10-16 11:02:41 -0700805 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
806 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
807 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
808 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
809 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000810 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700811 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
812 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000813 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700814 })
815 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000816 logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000817 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700818 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000819
Girish Kumare6f45e82020-03-20 10:46:54 +0000820 logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000821 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -0700822}
823
Esin Karaman8aa75a72019-12-20 13:11:59 +0000824//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
825func isMulticastGem(isMulticastAttrValue string) bool {
826 return isMulticastAttrValue != "" &&
827 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
828}
829
830func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
831 var encryp bool
832 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
833 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
834 for Count := 0; Count < NumGemPorts; Count++ {
835 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
836 continue
837 }
838 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
839 encryp = true
840 } else {
841 encryp = false
842 }
843 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
844 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
845 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
846 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
847 AesEncryption: encryp,
848 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
849 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
850 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
851 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
852 })
853 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000854 logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000855 return mcastTrafficQueues
856}
857
Scott Baker2c1c4822019-10-16 11:02:41 -0700858func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000859 UsScheduler, _ := tpm.GetUsScheduler(tp)
Scott Baker2c1c4822019-10-16 11:02:41 -0700860
861 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
862 AllocId: tp.UsScheduler.AllocID,
863 Scheduler: UsScheduler}
864}
865
866func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 {
867 /*
868 Function to get the Gemport ID mapped to a pbit.
869 */
870 if Dir == tp_pb.Direction_UPSTREAM {
871 // upstream GEM ports
872 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
873 for Count := 0; Count < NumGemPorts; Count++ {
874 NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap)
875 for ICount := 2; ICount < NumPbitMaps; ICount++ {
876 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
877 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
Girish Kumare6f45e82020-03-20 10:46:54 +0000878 logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700879 return tp.UpstreamGemPortAttributeList[Count].GemportID
880 }
881 }
882 }
883 }
884 } else if Dir == tp_pb.Direction_DOWNSTREAM {
885 //downstream GEM ports
886 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
887 for Count := 0; Count < NumGemPorts; Count++ {
888 NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap)
889 for ICount := 2; ICount < NumPbitMaps; ICount++ {
890 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
891 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
Girish Kumare6f45e82020-03-20 10:46:54 +0000892 logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700893 return tp.DownstreamGemPortAttributeList[Count].GemportID
894 }
895 }
896 }
897 }
898 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000899 logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700900 return 0
901}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530902
903// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
npujar5bf737f2020-01-16 19:35:25 +0530904func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530905 var tp TechProfile
Girish Gowdra6172a2e2019-11-27 14:37:58 +0530906 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530907
npujar5bf737f2020-01-16 19:35:25 +0530908 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530909 tpInstances := make([]TechProfile, 0, len(kvPairs))
910 for kvPath, kvPair := range kvPairs {
911 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
912 if err = json.Unmarshal(value, &tp); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000913 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530914 continue
915 } else {
916 tpInstances = append(tpInstances, tp)
917 }
918 }
919 }
920 return tpInstances
921 }
922 return nil
923}