blob: 4473389f3611969ffe343104a86e3201ffd4d02a [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
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700103var uniPortNameFormat = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530104
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 {
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700298 logger.Debugw("get-tp-instance-kv-path", log.Fields{
299 "uniPortName": uniPortName,
300 "tpId": techProfiletblID,
301 })
Scott Baker2c1c4822019-10-16 11:02:41 -0700302 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
303}
304
npujar5bf737f2020-01-16 19:35:25 +0530305func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700306 var KvTpIns TechProfile
307 var resPtr *TechProfile = &KvTpIns
308 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530309 var kvResult *kvstore.KVPair
Girish Gowdra9447baf2019-11-05 16:42:37 +0530310
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700311 logger.Infow("get-tp-instance-form-kv-store", log.Fields{"path": path, "tpid": techProfiletblID})
312
npujar5bf737f2020-01-16 19:35:25 +0530313 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530314 if kvResult == nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000315 logger.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530316 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700317 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530318 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700319 if err = json.Unmarshal(value, resPtr); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000320 logger.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530321 return nil, errors.New("error-unmarshal-kv-result")
322 } else {
323 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700324 }
325 }
326 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530327 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700328}
329
npujar5bf737f2020-01-16 19:35:25 +0530330func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Scott Baker2c1c4822019-10-16 11:02:41 -0700331 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Girish Kumare6f45e82020-03-20 10:46:54 +0000332 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700333 tpInstanceJson, err := json.Marshal(*tpInstance)
334 if err == nil {
335 // Backend will convert JSON byte array into string format
Girish Kumare6f45e82020-03-20 10:46:54 +0000336 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujar5bf737f2020-01-16 19:35:25 +0530337 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Scott Baker2c1c4822019-10-16 11:02:41 -0700338 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000339 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700340 }
341 return err
342}
npujar5bf737f2020-01-16 19:35:25 +0530343func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700344 var kvtechprofile DefaultTechProfile
345 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Girish Kumare6f45e82020-03-20 10:46:54 +0000346 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujar5bf737f2020-01-16 19:35:25 +0530347 kvresult, err := t.config.KVBackend.Get(ctx, key)
Scott Baker2c1c4822019-10-16 11:02:41 -0700348 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000349 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
Scott Baker2c1c4822019-10-16 11:02:41 -0700350 return nil
351 }
352 if kvresult != nil {
353 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
354 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000355 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000356 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 +0000357 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700358 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000359
Girish Kumare6f45e82020-03-20 10:46:54 +0000360 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000361 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700362 }
363 }
364 return nil
365}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000366
npujar5bf737f2020-01-16 19:35:25 +0530367func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700368 var tpInstance *TechProfile
Girish Kumare6f45e82020-03-20 10:46:54 +0000369 logger.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530370
371 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
372 if !uniPortNameFormat.Match([]byte(uniPortName)) {
Girish Kumare6f45e82020-03-20 10:46:54 +0000373 logger.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000374 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530375 }
376
npujar5bf737f2020-01-16 19:35:25 +0530377 tp := t.getTPFromKVStore(ctx, techProfiletblID)
Scott Baker2c1c4822019-10-16 11:02:41 -0700378 if tp != nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530379 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000380 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530381 tp = t.getDefaultTechProfile()
382 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000383 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530384 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700385 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000386 logger.Info("tp-not-found-on-kv--creating-default-tp")
Scott Baker2c1c4822019-10-16 11:02:41 -0700387 tp = t.getDefaultTechProfile()
Scott Baker2c1c4822019-10-16 11:02:41 -0700388 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530389 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530390 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000391 logger.Error("tp-intance-allocation-failed")
Girish Kumarb3c52d52019-12-06 12:14:14 +0000392 return nil, errors.New("tp-intance-allocation-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700393 }
npujar5bf737f2020-01-16 19:35:25 +0530394 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000395 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000396 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530397 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000398 logger.Infow("tp-added-to-kv-store-successfully",
Scott Baker2c1c4822019-10-16 11:02:41 -0700399 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000400 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700401}
402
npujar5bf737f2020-01-16 19:35:25 +0530403func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Scott Baker2c1c4822019-10-16 11:02:41 -0700404 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530405 return t.config.KVBackend.Delete(ctx, path)
Scott Baker2c1c4822019-10-16 11:02:41 -0700406}
407
Girish Gowdra9447baf2019-11-05 16:42:37 +0530408func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
409 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000410 logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530411 return errors.New("invalid-onu-instance-ctl-attr")
412 }
413
414 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000415 logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530416 return errors.New("invalid-uni-instance-ctl-attr")
417 }
418
419 if instCtl.Uni == "multi-instance" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000420 logger.Error("uni-multi-instance-tp-not-supported")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530421 return errors.New("uni-multi-instance-tp-not-supported")
422 }
423
424 return nil
425}
426
npujar5bf737f2020-01-16 19:35:25 +0530427func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700428
429 var usGemPortAttributeList []iGemPortAttribute
430 var dsGemPortAttributeList []iGemPortAttribute
Esin Karaman8aa75a72019-12-20 13:11:59 +0000431 var dsMulticastGemAttributeList []iGemPortAttribute
432 var dsUnicastGemAttributeList []iGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700433 var tcontIDs []uint32
434 var gemPorts []uint32
435 var err error
436
Girish Kumare6f45e82020-03-20 10:46:54 +0000437 logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530438
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530439 if tp.InstanceCtrl.Onu == "multi-instance" {
npujar5bf737f2020-01-16 19:35:25 +0530440 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000441 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530442 return nil
443 }
444 } else { // "single-instance"
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800445 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000446 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800447 return nil
448 } else if tpInst == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530449 // No "single-instance" tp found on one any uni port for the given TP ID
450 // Allocate a new TcontID or AllocID
npujar5bf737f2020-01-16 19:35:25 +0530451 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000452 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530453 return nil
454 }
455 } else {
456 // Use the alloc-id from the existing TpInstance
457 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
458 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700459 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000460 logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
npujar5bf737f2020-01-16 19:35:25 +0530461 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000462 logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700463 return nil
464 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000465 logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700466 for index := 0; index < int(tp.NumGemPorts); index++ {
467 usGemPortAttributeList = append(usGemPortAttributeList,
468 iGemPortAttribute{GemportID: gemPorts[index],
469 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
470 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
471 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
472 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
473 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
474 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
475 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
476 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000477 }
478
Girish Kumare6f45e82020-03-20 10:46:54 +0000479 logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Esin Karaman8aa75a72019-12-20 13:11:59 +0000480 //put multicast and unicast downstream GEM port attributes in different lists first
481 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
482 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
483 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
484 iGemPortAttribute{
485 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
486 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
487 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
488 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
489 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
490 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
491 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
492 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
493 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
494 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
495 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
496 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
497 } else {
498 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
499 iGemPortAttribute{
500 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
501 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
502 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
503 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
504 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
505 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
506 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
507 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
508 }
509 }
510 //add unicast downstream GEM ports to dsGemPortAttributeList
511 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700512 dsGemPortAttributeList = append(dsGemPortAttributeList,
513 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000514 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
515 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
516 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
517 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
518 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
519 Weight: dsUnicastGemAttributeList[index].Weight,
520 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
521 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700522 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000523 //add multicast GEM ports to dsGemPortAttributeList afterwards
524 for k := range dsMulticastGemAttributeList {
525 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
526 }
527
Scott Baker2c1c4822019-10-16 11:02:41 -0700528 return &TechProfile{
529 SubscriberIdentifier: uniPortName,
530 Name: tp.Name,
531 ProfileType: tp.ProfileType,
532 Version: tp.Version,
533 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700534 InstanceCtrl: tp.InstanceCtrl,
535 UsScheduler: iScheduler{
536 AllocID: tcontIDs[0],
537 Direction: tp.UsScheduler.Direction,
538 AdditionalBw: tp.UsScheduler.AdditionalBw,
539 Priority: tp.UsScheduler.Priority,
540 Weight: tp.UsScheduler.Weight,
541 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
542 DsScheduler: iScheduler{
543 AllocID: tcontIDs[0],
544 Direction: tp.DsScheduler.Direction,
545 AdditionalBw: tp.DsScheduler.AdditionalBw,
546 Priority: tp.DsScheduler.Priority,
547 Weight: tp.DsScheduler.Weight,
548 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
549 UpstreamGemPortAttributeList: usGemPortAttributeList,
550 DownstreamGemPortAttributeList: dsGemPortAttributeList}
551}
552
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530553// getSingleInstanceTp returns another TpInstance for an ONU on a different
554// uni port for the same TP ID, if it finds one, else nil.
npujar5bf737f2020-01-16 19:35:25 +0530555func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530556 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530557
558 // For example:
559 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
560 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
561 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujar5bf737f2020-01-16 19:35:25 +0530562 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra9447baf2019-11-05 16:42:37 +0530563
Girish Gowdra9447baf2019-11-05 16:42:37 +0530564 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
565 for keyPath, kvPair := range kvPairs {
566 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
567 if err = json.Unmarshal(value, &tpInst); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000568 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530569 return nil, errors.New("error-unmarshal-kv-pair")
570 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000571 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530572 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530573 }
574 }
575 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530576 return nil, nil
577}
578
Scott Baker2c1c4822019-10-16 11:02:41 -0700579func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
580
581 var usGemPortAttributeList []GemPortAttribute
582 var dsGemPortAttributeList []GemPortAttribute
583
584 for _, pbit := range t.config.DefaultPbits {
Girish Kumare6f45e82020-03-20 10:46:54 +0000585 logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700586 usGemPortAttributeList = append(usGemPortAttributeList,
587 GemPortAttribute{
588 MaxQueueSize: defaultMaxQueueSize,
589 PbitMap: pbit,
590 AesEncryption: defaultAESEncryption,
591 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
592 PriorityQueue: defaultPriorityQueue,
593 Weight: defaultQueueWeight,
594 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
595 DiscardConfig: DiscardConfig{
596 MinThreshold: defaultMinThreshold,
597 MaxThreshold: defaultMaxThreshold,
598 MaxProbability: defaultMaxProbability}})
599 dsGemPortAttributeList = append(dsGemPortAttributeList,
600 GemPortAttribute{
601 MaxQueueSize: defaultMaxQueueSize,
602 PbitMap: pbit,
603 AesEncryption: defaultAESEncryption,
604 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
605 PriorityQueue: defaultPriorityQueue,
606 Weight: defaultQueueWeight,
607 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
608 DiscardConfig: DiscardConfig{
609 MinThreshold: defaultMinThreshold,
610 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000611 MaxProbability: defaultMaxProbability},
612 IsMulticast: defaultIsMulticast,
613 DControlList: defaultAccessControlList,
614 SControlList: defaultAccessControlList,
615 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700616 }
617 return &DefaultTechProfile{
618 Name: t.config.DefaultTPName,
619 ProfileType: t.resourceMgr.GetTechnology(),
620 Version: t.config.TPVersion,
621 NumGemPorts: uint32(len(usGemPortAttributeList)),
622 InstanceCtrl: InstanceControl{
623 Onu: defaultOnuInstance,
624 Uni: defaultUniInstance,
625 MaxGemPayloadSize: defaultGemPayloadSize},
626 UsScheduler: Scheduler{
627 Direction: Direction_name[Direction_UPSTREAM],
628 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
629 Priority: defaultPriority,
630 Weight: defaultWeight,
631 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
632 DsScheduler: Scheduler{
633 Direction: Direction_name[Direction_DOWNSTREAM],
634 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
635 Priority: defaultPriority,
636 Weight: defaultWeight,
637 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
638 UpstreamGemPortAttributeList: usGemPortAttributeList,
639 DownstreamGemPortAttributeList: dsGemPortAttributeList}
640}
641
642func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
643 var result int32 = -1
644
645 if paramType == "direction" {
646 for key, val := range tp_pb.Direction_value {
647 if key == paramKey {
648 result = val
649 }
650 }
651 } else if paramType == "discard_policy" {
652 for key, val := range tp_pb.DiscardPolicy_value {
653 if key == paramKey {
654 result = val
655 }
656 }
657 } else if paramType == "sched_policy" {
658 for key, val := range tp_pb.SchedulingPolicy_value {
659 if key == paramKey {
Girish Kumare6f45e82020-03-20 10:46:54 +0000660 logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
Scott Baker2c1c4822019-10-16 11:02:41 -0700661 result = val
662 }
663 }
664 } else if paramType == "additional_bw" {
665 for key, val := range tp_pb.AdditionalBW_value {
666 if key == paramKey {
667 result = val
668 }
669 }
670 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000671 logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Scott Baker2c1c4822019-10-16 11:02:41 -0700672 return -1
673 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000674 logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
Scott Baker2c1c4822019-10-16 11:02:41 -0700675 return result
676}
677
Girish Kumarb3c52d52019-12-06 12:14:14 +0000678func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700679 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
680 if dir == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000681 logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000682 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 -0700683 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000684
Scott Baker2c1c4822019-10-16 11:02:41 -0700685 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
686 if bw == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000687 logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000688 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 -0700689 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000690
Scott Baker2c1c4822019-10-16 11:02:41 -0700691 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
692 if policy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000693 logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000694 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 -0700695 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000696
Scott Baker2c1c4822019-10-16 11:02:41 -0700697 return &tp_pb.SchedulerConfig{
698 Direction: dir,
699 AdditionalBw: bw,
700 Priority: tpInstance.UsScheduler.Priority,
701 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000702 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700703}
704
Girish Kumarb3c52d52019-12-06 12:14:14 +0000705func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700706
707 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
708 if dir == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000709 logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000710 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 -0700711 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000712
Scott Baker2c1c4822019-10-16 11:02:41 -0700713 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
714 if bw == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000715 logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000716 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 -0700717 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000718
Scott Baker2c1c4822019-10-16 11:02:41 -0700719 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
720 if policy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000721 logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000722 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 -0700723 }
724
725 return &tp_pb.SchedulerConfig{
726 Direction: dir,
727 AdditionalBw: bw,
728 Priority: tpInstance.DsScheduler.Priority,
729 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000730 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700731}
732
733func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
734 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
735
736 tSched := &tp_pb.TrafficScheduler{
737 Direction: SchedCfg.Direction,
738 AllocId: tpInstance.UsScheduler.AllocID,
739 TrafficShapingInfo: ShapingCfg,
740 Scheduler: SchedCfg}
741
742 return tSched
743}
744
Girish Kumarb3c52d52019-12-06 12:14:14 +0000745func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700746
747 var encryp bool
748 if Dir == tp_pb.Direction_UPSTREAM {
749 // upstream GEM ports
750 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
751 GemPorts := make([]*tp_pb.TrafficQueue, 0)
752 for Count := 0; Count < NumGemPorts; Count++ {
753 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
754 encryp = true
755 } else {
756 encryp = false
757 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000758
759 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
760 if schedPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000761 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 +0000762 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
763 }
764
765 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
766 if discardPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000767 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 +0000768 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
769 }
770
Scott Baker2c1c4822019-10-16 11:02:41 -0700771 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
772 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
773 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
774 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
775 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000776 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700777 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
778 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000779 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700780 })
781 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000782 logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000783 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700784 } else if Dir == tp_pb.Direction_DOWNSTREAM {
785 //downstream GEM ports
786 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
787 GemPorts := make([]*tp_pb.TrafficQueue, 0)
788 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000789 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
790 //do not take multicast GEM ports. They are handled separately.
791 continue
792 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700793 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
794 encryp = true
795 } else {
796 encryp = false
797 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000798
799 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
800 if schedPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000801 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 +0000802 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
803 }
804
805 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
806 if discardPolicy == -1 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000807 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 +0000808 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
809 }
810
Scott Baker2c1c4822019-10-16 11:02:41 -0700811 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
812 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
813 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
814 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
815 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000816 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700817 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
818 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000819 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700820 })
821 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000822 logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000823 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700824 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000825
Girish Kumare6f45e82020-03-20 10:46:54 +0000826 logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000827 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -0700828}
829
Esin Karaman8aa75a72019-12-20 13:11:59 +0000830//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
831func isMulticastGem(isMulticastAttrValue string) bool {
832 return isMulticastAttrValue != "" &&
833 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
834}
835
836func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
837 var encryp bool
838 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
839 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
840 for Count := 0; Count < NumGemPorts; Count++ {
841 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
842 continue
843 }
844 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
845 encryp = true
846 } else {
847 encryp = false
848 }
849 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
850 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
851 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
852 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
853 AesEncryption: encryp,
854 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
855 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
856 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
857 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
858 })
859 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000860 logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000861 return mcastTrafficQueues
862}
863
Scott Baker2c1c4822019-10-16 11:02:41 -0700864func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000865 UsScheduler, _ := tpm.GetUsScheduler(tp)
Scott Baker2c1c4822019-10-16 11:02:41 -0700866
867 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
868 AllocId: tp.UsScheduler.AllocID,
869 Scheduler: UsScheduler}
870}
871
872func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 {
873 /*
874 Function to get the Gemport ID mapped to a pbit.
875 */
876 if Dir == tp_pb.Direction_UPSTREAM {
877 // upstream GEM ports
878 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
879 for Count := 0; Count < NumGemPorts; Count++ {
880 NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap)
881 for ICount := 2; ICount < NumPbitMaps; ICount++ {
882 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
883 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
Girish Kumare6f45e82020-03-20 10:46:54 +0000884 logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700885 return tp.UpstreamGemPortAttributeList[Count].GemportID
886 }
887 }
888 }
889 }
890 } else if Dir == tp_pb.Direction_DOWNSTREAM {
891 //downstream GEM ports
892 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
893 for Count := 0; Count < NumGemPorts; Count++ {
894 NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap)
895 for ICount := 2; ICount < NumPbitMaps; ICount++ {
896 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil {
897 if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set
Girish Kumare6f45e82020-03-20 10:46:54 +0000898 logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700899 return tp.DownstreamGemPortAttributeList[Count].GemportID
900 }
901 }
902 }
903 }
904 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000905 logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700906 return 0
907}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530908
909// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
npujar5bf737f2020-01-16 19:35:25 +0530910func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530911 var tp TechProfile
Girish Gowdra6172a2e2019-11-27 14:37:58 +0530912 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530913
npujar5bf737f2020-01-16 19:35:25 +0530914 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530915 tpInstances := make([]TechProfile, 0, len(kvPairs))
916 for kvPath, kvPair := range kvPairs {
917 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
918 if err = json.Unmarshal(value, &tp); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000919 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530920 continue
921 } else {
922 tpInstances = append(tpInstances, tp)
923 }
924 }
925 }
926 return tpInstances
927 }
928 return nil
929}