blob: c066542244b0e174b2ad7fcf5a1c789d45364829 [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"
Gamze Abakadfdd8f82020-05-04 08:39:50 +000026 "sync"
Neha Sharma130ac6d2020-04-08 08:46:32 +000027 "time"
Scott Baker2c1c4822019-10-16 11:02:41 -070028
Girish Gowdra89c985b2020-10-14 15:02:09 -070029 "github.com/opencord/voltha-lib-go/v4/pkg/db"
serkant.uluderyab38671c2019-11-01 09:35:38 -070030
Girish Gowdra89c985b2020-10-14 15:02:09 -070031 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v4/pkg/log"
33 tp_pb "github.com/opencord/voltha-protos/v4/go/tech_profile"
Scott Baker2c1c4822019-10-16 11:02:41 -070034)
35
36// Interface to pon resource manager APIs
37type iPonResourceMgr interface {
npujar5bf737f2020-01-16 19:35:25 +053038 GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
Scott Baker2c1c4822019-10-16 11:02:41 -070039 GetResourceTypeAllocID() string
40 GetResourceTypeGemPortID() string
41 GetTechnology() string
42}
43
44type Direction int32
45
46const (
47 Direction_UPSTREAM Direction = 0
48 Direction_DOWNSTREAM Direction = 1
49 Direction_BIDIRECTIONAL Direction = 2
50)
51
52var Direction_name = map[Direction]string{
53 0: "UPSTREAM",
54 1: "DOWNSTREAM",
55 2: "BIDIRECTIONAL",
56}
57
58type SchedulingPolicy int32
59
60const (
61 SchedulingPolicy_WRR SchedulingPolicy = 0
62 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
63 SchedulingPolicy_Hybrid SchedulingPolicy = 2
64)
65
66var SchedulingPolicy_name = map[SchedulingPolicy]string{
67 0: "WRR",
68 1: "StrictPriority",
69 2: "Hybrid",
70}
71
72type AdditionalBW int32
73
74const (
75 AdditionalBW_AdditionalBW_None AdditionalBW = 0
76 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
77 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
78 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
79)
80
81var AdditionalBW_name = map[AdditionalBW]string{
82 0: "AdditionalBW_None",
83 1: "AdditionalBW_NA",
84 2: "AdditionalBW_BestEffort",
85 3: "AdditionalBW_Auto",
86}
87
88type DiscardPolicy int32
89
90const (
91 DiscardPolicy_TailDrop DiscardPolicy = 0
92 DiscardPolicy_WTailDrop DiscardPolicy = 1
93 DiscardPolicy_Red DiscardPolicy = 2
94 DiscardPolicy_WRed DiscardPolicy = 3
95)
96
97var DiscardPolicy_name = map[DiscardPolicy]string{
98 0: "TailDrop",
99 1: "WTailDrop",
100 2: "Red",
101 3: "WRed",
102}
103
Girish Gowdra9447baf2019-11-05 16:42:37 +0530104// Required uniPortName format
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700105var uniPortNameFormat = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530106
Scott Baker2c1c4822019-10-16 11:02:41 -0700107/*
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700108 type InferredAdditionBWIndication int32
Scott Baker2c1c4822019-10-16 11:02:41 -0700109
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700110 const (
111 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
112 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
113 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
114 )
Scott Baker2c1c4822019-10-16 11:02:41 -0700115
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700116 var InferredAdditionBWIndication_name = map[int32]string{
117 0: "InferredAdditionBWIndication_None",
118 1: "InferredAdditionBWIndication_Assured",
119 2: "InferredAdditionBWIndication_BestEffort",
120 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700121*/
122// instance control defaults
123const (
124 defaultOnuInstance = "multi-instance"
125 defaultUniInstance = "single-instance"
Scott Baker2c1c4822019-10-16 11:02:41 -0700126 defaultGemPayloadSize = "auto"
127)
128
129const MAX_GEM_PAYLOAD = "max_gem_payload_size"
130
131type InstanceControl struct {
132 Onu string `json:"ONU"`
133 Uni string `json:"uni"`
134 MaxGemPayloadSize string `json:"max_gem_payload_size"`
135}
136
137// default discard config constants
138const (
139 defaultMinThreshold = 0
140 defaultMaxThreshold = 0
141 defaultMaxProbability = 0
142)
143
144type DiscardConfig struct {
145 MinThreshold int `json:"min_threshold"`
146 MaxThreshold int `json:"max_threshold"`
147 MaxProbability int `json:"max_probability"`
148}
149
150// default scheduler contants
151const (
152 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
153 defaultPriority = 0
154 defaultWeight = 0
155 defaultQueueSchedPolicy = SchedulingPolicy_Hybrid
156)
157
158type Scheduler struct {
159 Direction string `json:"direction"`
160 AdditionalBw string `json:"additional_bw"`
161 Priority uint32 `json:"priority"`
162 Weight uint32 `json:"weight"`
163 QSchedPolicy string `json:"q_sched_policy"`
164}
165
166// default GEM attribute constants
167const (
Esin Karaman8aa75a72019-12-20 13:11:59 +0000168 defaultAESEncryption = "True"
169 defaultPriorityQueue = 0
170 defaultQueueWeight = 0
171 defaultMaxQueueSize = "auto"
172 defaultdropPolicy = DiscardPolicy_TailDrop
173 defaultSchedulePolicy = SchedulingPolicy_WRR
174 defaultIsMulticast = "False"
175 defaultAccessControlList = "224.0.0.0-239.255.255.255"
176 defaultMcastGemID = 4069
Scott Baker2c1c4822019-10-16 11:02:41 -0700177)
178
179type GemPortAttribute struct {
180 MaxQueueSize string `json:"max_q_size"`
181 PbitMap string `json:"pbit_map"`
182 AesEncryption string `json:"aes_encryption"`
183 SchedulingPolicy string `json:"scheduling_policy"`
184 PriorityQueue uint32 `json:"priority_q"`
185 Weight uint32 `json:"weight"`
186 DiscardPolicy string `json:"discard_policy"`
187 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000188 IsMulticast string `json:"is_multicast"`
189 DControlList string `json:"dynamic_access_control_list"`
190 SControlList string `json:"static_access_control_list"`
191 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700192}
193
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700194// Instance of Scheduler
195type IScheduler struct {
Scott Baker2c1c4822019-10-16 11:02:41 -0700196 AllocID uint32 `json:"alloc_id"`
197 Direction string `json:"direction"`
198 AdditionalBw string `json:"additional_bw"`
199 Priority uint32 `json:"priority"`
200 Weight uint32 `json:"weight"`
201 QSchedPolicy string `json:"q_sched_policy"`
202}
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700203
204// Instance of GemPortAttribute
205type IGemPortAttribute struct {
Scott Baker2c1c4822019-10-16 11:02:41 -0700206 GemportID uint32 `json:"gemport_id"`
207 MaxQueueSize string `json:"max_q_size"`
208 PbitMap string `json:"pbit_map"`
209 AesEncryption string `json:"aes_encryption"`
210 SchedulingPolicy string `json:"scheduling_policy"`
211 PriorityQueue uint32 `json:"priority_q"`
212 Weight uint32 `json:"weight"`
213 DiscardPolicy string `json:"discard_policy"`
214 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000215 IsMulticast string `json:"is_multicast"`
216 DControlList string `json:"dynamic_access_control_list"`
217 SControlList string `json:"static_access_control_list"`
218 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700219}
220
221type TechProfileMgr struct {
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000222 config *TechProfileFlags
223 resourceMgr iPonResourceMgr
224 GemPortIDMgmtLock sync.RWMutex
225 AllocIDMgmtLock sync.RWMutex
Scott Baker2c1c4822019-10-16 11:02:41 -0700226}
227type DefaultTechProfile struct {
228 Name string `json:"name"`
229 ProfileType string `json:"profile_type"`
230 Version int `json:"version"`
231 NumGemPorts uint32 `json:"num_gem_ports"`
232 InstanceCtrl InstanceControl `json:"instance_control"`
233 UsScheduler Scheduler `json:"us_scheduler"`
234 DsScheduler Scheduler `json:"ds_scheduler"`
235 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
236 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
237}
238type TechProfile struct {
239 Name string `json:"name"`
240 SubscriberIdentifier string `json:"subscriber_identifier"`
241 ProfileType string `json:"profile_type"`
242 Version int `json:"version"`
243 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700244 InstanceCtrl InstanceControl `json:"instance_control"`
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700245 UsScheduler IScheduler `json:"us_scheduler"`
246 DsScheduler IScheduler `json:"ds_scheduler"`
247 UpstreamGemPortAttributeList []IGemPortAttribute `json:"upstream_gem_port_attribute_list"`
248 DownstreamGemPortAttributeList []IGemPortAttribute `json:"downstream_gem_port_attribute_list"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700249}
250
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700251// QThresholds struct for EPON
252type QThresholds struct {
253 QThreshold1 uint32 `json:"q_threshold1"`
254 QThreshold2 uint32 `json:"q_threshold2"`
255 QThreshold3 uint32 `json:"q_threshold3"`
256 QThreshold4 uint32 `json:"q_threshold4"`
257 QThreshold5 uint32 `json:"q_threshold5"`
258 QThreshold6 uint32 `json:"q_threshold6"`
259 QThreshold7 uint32 `json:"q_threshold7"`
260}
261
262// UpstreamQueueAttribute struct for EPON
263type UpstreamQueueAttribute struct {
264 MaxQueueSize string `json:"max_q_size"`
265 PbitMap string `json:"pbit_map"`
266 AesEncryption string `json:"aes_encryption"`
267 TrafficType string `json:"traffic_type"`
268 UnsolicitedGrantSize uint32 `json:"unsolicited_grant_size"`
269 NominalInterval uint32 `json:"nominal_interval"`
270 ToleratedPollJitter uint32 `json:"tolerated_poll_jitter"`
271 RequestTransmissionPolicy uint32 `json:"request_transmission_policy"`
272 NumQueueSet uint32 `json:"num_q_sets"`
273 QThresholds QThresholds `json:"q_thresholds"`
274 SchedulingPolicy string `json:"scheduling_policy"`
275 PriorityQueue uint32 `json:"priority_q"`
276 Weight uint32 `json:"weight"`
277 DiscardPolicy string `json:"discard_policy"`
278 DiscardConfig DiscardConfig `json:"discard_config"`
279}
280
281// Default EPON constants
282const (
283 defaultPakageType = "B"
284)
285const (
286 defaultTrafficType = "BE"
287 defaultUnsolicitedGrantSize = 0
288 defaultNominalInterval = 0
289 defaultToleratedPollJitter = 0
290 defaultRequestTransmissionPolicy = 0
291 defaultNumQueueSet = 2
292)
293const (
294 defaultQThreshold1 = 5500
295 defaultQThreshold2 = 0
296 defaultQThreshold3 = 0
297 defaultQThreshold4 = 0
298 defaultQThreshold5 = 0
299 defaultQThreshold6 = 0
300 defaultQThreshold7 = 0
301)
302
303// DownstreamQueueAttribute struct for EPON
304type DownstreamQueueAttribute struct {
305 MaxQueueSize string `json:"max_q_size"`
306 PbitMap string `json:"pbit_map"`
307 AesEncryption string `json:"aes_encryption"`
308 SchedulingPolicy string `json:"scheduling_policy"`
309 PriorityQueue uint32 `json:"priority_q"`
310 Weight uint32 `json:"weight"`
311 DiscardPolicy string `json:"discard_policy"`
312 DiscardConfig DiscardConfig `json:"discard_config"`
313}
314
315// iUpstreamQueueAttribute struct for EPON
316type iUpstreamQueueAttribute struct {
317 GemportID uint32 `json:"q_id"`
318 MaxQueueSize string `json:"max_q_size"`
319 PbitMap string `json:"pbit_map"`
320 AesEncryption string `json:"aes_encryption"`
321 TrafficType string `json:"traffic_type"`
322 UnsolicitedGrantSize uint32 `json:"unsolicited_grant_size"`
323 NominalInterval uint32 `json:"nominal_interval"`
324 ToleratedPollJitter uint32 `json:"tolerated_poll_jitter"`
325 RequestTransmissionPolicy uint32 `json:"request_transmission_policy"`
326 NumQueueSet uint32 `json:"num_q_sets"`
327 QThresholds QThresholds `json:"q_thresholds"`
328 SchedulingPolicy string `json:"scheduling_policy"`
329 PriorityQueue uint32 `json:"priority_q"`
330 Weight uint32 `json:"weight"`
331 DiscardPolicy string `json:"discard_policy"`
332 DiscardConfig DiscardConfig `json:"discard_config"`
333}
334
335// iDownstreamQueueAttribute struct for EPON
336type iDownstreamQueueAttribute struct {
337 GemportID uint32 `json:"q_id"`
338 MaxQueueSize string `json:"max_q_size"`
339 PbitMap string `json:"pbit_map"`
340 AesEncryption string `json:"aes_encryption"`
341 SchedulingPolicy string `json:"scheduling_policy"`
342 PriorityQueue uint32 `json:"priority_q"`
343 Weight uint32 `json:"weight"`
344 DiscardPolicy string `json:"discard_policy"`
345 DiscardConfig DiscardConfig `json:"discard_config"`
346}
347
348// EponAttribute struct for EPON
349type EponAttribute struct {
350 PackageType string `json:"pakage_type"`
351}
352
353// DefaultTechProfile struct for EPON
354type DefaultEponProfile struct {
355 Name string `json:"name"`
356 ProfileType string `json:"profile_type"`
357 Version int `json:"version"`
358 NumGemPorts uint32 `json:"num_gem_ports"`
359 InstanceCtrl InstanceControl `json:"instance_control"`
360 EponAttribute EponAttribute `json:"epon_attribute"`
361 UpstreamQueueAttributeList []UpstreamQueueAttribute `json:"upstream_queue_attribute_list"`
362 DownstreamQueueAttributeList []DownstreamQueueAttribute `json:"downstream_queue_attribute_list"`
363}
364
365// TechProfile struct for EPON
366type EponProfile struct {
367 Name string `json:"name"`
368 SubscriberIdentifier string `json:"subscriber_identifier"`
369 ProfileType string `json:"profile_type"`
370 Version int `json:"version"`
371 NumGemPorts uint32 `json:"num_gem_ports"`
372 InstanceCtrl InstanceControl `json:"instance_control"`
373 EponAttribute EponAttribute `json:"epon_attribute"`
374 AllocID uint32 `json:"llid"`
375 UpstreamQueueAttributeList []iUpstreamQueueAttribute `json:"upstream_queue_attribute_list"`
376 DownstreamQueueAttributeList []iDownstreamQueueAttribute `json:"downstream_queue_attribute_list"`
377}
378
379const (
Andrea Campanella904fe322020-06-26 18:56:51 +0200380 xgspon = "XGS-PON"
381 gpon = "GPON"
382 epon = "EPON"
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700383)
384
Neha Sharma94f16a92020-06-26 04:17:55 +0000385func (t *TechProfileMgr) SetKVClient(ctx context.Context) *db.Backend {
386 kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700387 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000388 logger.Errorw(ctx, "failed-to-create-kv-client",
Scott Baker2c1c4822019-10-16 11:02:41 -0700389 log.Fields{
Neha Sharmadd9af392020-04-28 09:03:57 +0000390 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700391 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
392 "error": err.Error(),
393 })
394 return nil
395 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500396 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700397 Client: kvClient,
398 StoreType: t.config.KVStoreType,
Neha Sharmadd9af392020-04-28 09:03:57 +0000399 Address: t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700400 Timeout: t.config.KVStoreTimeout,
401 PathPrefix: t.config.TPKVPathPrefix}
402
403 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700404 issue between kv store and backend , core is not calling NewBackend directly
405 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
406 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
Scott Baker2c1c4822019-10-16 11:02:41 -0700407 */
408}
409
Neha Sharma94f16a92020-06-26 04:17:55 +0000410func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700411
Neha Sharma94f16a92020-06-26 04:17:55 +0000412 logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
Scott Baker2c1c4822019-10-16 11:02:41 -0700413 switch storeType {
414 case "consul":
Neha Sharma94f16a92020-06-26 04:17:55 +0000415 return kvstore.NewConsulClient(ctx, address, timeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700416 case "etcd":
Neha Sharma94f16a92020-06-26 04:17:55 +0000417 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
Scott Baker2c1c4822019-10-16 11:02:41 -0700418 }
419 return nil, errors.New("unsupported-kv-store")
420}
421
Matteo Scandolo866ac582020-11-09 12:18:31 -0800422func NewTechProfile(ctx context.Context, resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string, basePathKvStore string) (*TechProfileMgr, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700423 var techprofileObj TechProfileMgr
Neha Sharma94f16a92020-06-26 04:17:55 +0000424 logger.Debug(ctx, "Initializing techprofile Manager")
Matteo Scandolo866ac582020-11-09 12:18:31 -0800425 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress, basePathKvStore)
Neha Sharma94f16a92020-06-26 04:17:55 +0000426 techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx)
Scott Baker2c1c4822019-10-16 11:02:41 -0700427 if techprofileObj.config.KVBackend == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000428 logger.Error(ctx, "Failed to initialize KV backend\n")
Scott Baker2c1c4822019-10-16 11:02:41 -0700429 return nil, errors.New("KV backend init failed")
430 }
431 techprofileObj.resourceMgr = resourceMgr
Neha Sharma94f16a92020-06-26 04:17:55 +0000432 logger.Debug(ctx, "Initializing techprofile object instance success")
Scott Baker2c1c4822019-10-16 11:02:41 -0700433 return &techprofileObj, nil
434}
435
Neha Sharma94f16a92020-06-26 04:17:55 +0000436func (t *TechProfileMgr) GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string {
437 logger.Debugw(ctx, "get-tp-instance-kv-path", log.Fields{
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700438 "uniPortName": uniPortName,
439 "tpId": techProfiletblID,
440 })
Scott Baker2c1c4822019-10-16 11:02:41 -0700441 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
442}
443
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700444func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (interface{}, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700445 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530446 var kvResult *kvstore.KVPair
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700447 var KvTpIns TechProfile
448 var KvEponIns EponProfile
449 var resPtr interface{}
450 // For example:
451 // tpInstPath like "XGS-PON/64/uni_port_name"
452 // is broken into ["XGS-PON" "64" ...]
453 pathSlice := regexp.MustCompile(`/`).Split(path, -1)
454 switch pathSlice[0] {
Andrea Campanella904fe322020-06-26 18:56:51 +0200455 case xgspon, gpon:
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700456 resPtr = &KvTpIns
457 case epon:
458 resPtr = &KvEponIns
459 default:
Girish Kumar950f21e2020-08-19 17:42:29 +0000460 logger.Errorw(ctx, "unknown-tech", log.Fields{"tech": pathSlice[0]})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700461 return nil, fmt.Errorf("unknown-tech-%s", pathSlice[0])
462 }
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700463
npujar5bf737f2020-01-16 19:35:25 +0530464 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530465 if kvResult == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000466 logger.Infow(ctx, "tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530467 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700468 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530469 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700470 if err = json.Unmarshal(value, resPtr); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000471 logger.Errorw(ctx, "error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530472 return nil, errors.New("error-unmarshal-kv-result")
473 } else {
474 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700475 }
476 }
477 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530478 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700479}
480
npujar5bf737f2020-01-16 19:35:25 +0530481func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Neha Sharma94f16a92020-06-26 04:17:55 +0000482 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
483 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700484 tpInstanceJson, err := json.Marshal(*tpInstance)
485 if err == nil {
486 // Backend will convert JSON byte array into string format
Neha Sharma94f16a92020-06-26 04:17:55 +0000487 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujar5bf737f2020-01-16 19:35:25 +0530488 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Scott Baker2c1c4822019-10-16 11:02:41 -0700489 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000490 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700491 }
492 return err
493}
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700494
495func (t *TechProfileMgr) addEponProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *EponProfile) error {
Neha Sharma94f16a92020-06-26 04:17:55 +0000496 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
497 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700498 tpInstanceJson, err := json.Marshal(*tpInstance)
499 if err == nil {
500 // Backend will convert JSON byte array into string format
Neha Sharma94f16a92020-06-26 04:17:55 +0000501 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700502 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
503 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000504 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700505 }
506 return err
507}
508
npujar5bf737f2020-01-16 19:35:25 +0530509func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700510 var kvtechprofile DefaultTechProfile
511 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Neha Sharma94f16a92020-06-26 04:17:55 +0000512 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujar5bf737f2020-01-16 19:35:25 +0530513 kvresult, err := t.config.KVBackend.Get(ctx, key)
Scott Baker2c1c4822019-10-16 11:02:41 -0700514 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000515 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
Scott Baker2c1c4822019-10-16 11:02:41 -0700516 return nil
517 }
518 if kvresult != nil {
519 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
520 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000521 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000522 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000523 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700524 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000525
Neha Sharma94f16a92020-06-26 04:17:55 +0000526 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000527 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700528 }
529 }
530 return nil
531}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000532
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700533func (t *TechProfileMgr) getEponTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultEponProfile {
534 var kvtechprofile DefaultEponProfile
535 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Neha Sharma94f16a92020-06-26 04:17:55 +0000536 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700537 kvresult, err := t.config.KVBackend.Get(ctx, key)
538 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000539 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700540 return nil
541 }
542 if kvresult != nil {
543 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
544 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
545 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000546 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700547 return nil
548 }
549
Neha Sharma94f16a92020-06-26 04:17:55 +0000550 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700551 return &kvtechprofile
552 }
553 }
554 return nil
555}
556
557func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (interface{}, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700558 var tpInstance *TechProfile
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700559 var tpEponInstance *EponProfile
560
Neha Sharma94f16a92020-06-26 04:17:55 +0000561 logger.Infow(ctx, "creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530562
563 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
564 if !uniPortNameFormat.Match([]byte(uniPortName)) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000565 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000566 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530567 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000568 tpInstancePath := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700569 // For example:
570 // tpInstPath like "XGS-PON/64/uni_port_name"
571 // is broken into ["XGS-PON" "64" ...]
572 pathSlice := regexp.MustCompile(`/`).Split(tpInstancePath, -1)
573 if pathSlice[0] == epon {
574 tp := t.getEponTPFromKVStore(ctx, techProfiletblID)
575 if tp != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000576 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
577 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
Girish Kumar950f21e2020-08-19 17:42:29 +0000578 tp = t.getDefaultEponProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700579 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000580 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700581 }
582 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000583 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
Girish Kumar950f21e2020-08-19 17:42:29 +0000584 tp = t.getDefaultEponProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700585 }
586
587 if tpEponInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpEponInstance == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000588 logger.Error(ctx, "tp-intance-allocation-failed")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700589 return nil, errors.New("tp-intance-allocation-failed")
590 }
591 if err := t.addEponProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpEponInstance); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000592 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700593 return nil, errors.New("error-adding-tp-to-kv-store")
594 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000595 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700596 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
597 return tpEponInstance, nil
598 } else {
599 tp := t.getTPFromKVStore(ctx, techProfiletblID)
600 if tp != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000601 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
602 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
603 tp = t.getDefaultTechProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700604 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000605 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700606 }
607 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000608 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
609 tp = t.getDefaultTechProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700610 }
611
612 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000613 logger.Error(ctx, "tp-intance-allocation-failed")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700614 return nil, errors.New("tp-intance-allocation-failed")
615 }
616 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000617 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700618 return nil, errors.New("error-adding-tp-to-kv-store")
619 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000620 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700621 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
622 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700623 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700624}
625
npujar5bf737f2020-01-16 19:35:25 +0530626func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Neha Sharma94f16a92020-06-26 04:17:55 +0000627 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530628 return t.config.KVBackend.Delete(ctx, path)
Scott Baker2c1c4822019-10-16 11:02:41 -0700629}
630
Neha Sharma94f16a92020-06-26 04:17:55 +0000631func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl InstanceControl) error {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530632 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000633 logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530634 return errors.New("invalid-onu-instance-ctl-attr")
635 }
636
637 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000638 logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530639 return errors.New("invalid-uni-instance-ctl-attr")
640 }
641
642 if instCtl.Uni == "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000643 logger.Error(ctx, "uni-multi-instance-tp-not-supported")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530644 return errors.New("uni-multi-instance-tp-not-supported")
645 }
646
647 return nil
648}
649
npujar5bf737f2020-01-16 19:35:25 +0530650func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700651
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700652 var usGemPortAttributeList []IGemPortAttribute
653 var dsGemPortAttributeList []IGemPortAttribute
654 var dsMulticastGemAttributeList []IGemPortAttribute
655 var dsUnicastGemAttributeList []IGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700656 var tcontIDs []uint32
657 var gemPorts []uint32
658 var err error
659
Neha Sharma94f16a92020-06-26 04:17:55 +0000660 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530661
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530662 if tp.InstanceCtrl.Onu == "multi-instance" {
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000663 t.AllocIDMgmtLock.Lock()
664 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
665 t.AllocIDMgmtLock.Unlock()
666 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000667 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530668 return nil
669 }
670 } else { // "single-instance"
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800671 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000672 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800673 return nil
674 } else if tpInst == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530675 // No "single-instance" tp found on one any uni port for the given TP ID
676 // Allocate a new TcontID or AllocID
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000677 t.AllocIDMgmtLock.Lock()
678 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
679 t.AllocIDMgmtLock.Unlock()
680 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000681 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530682 return nil
683 }
684 } else {
685 // Use the alloc-id from the existing TpInstance
686 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
687 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700688 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000689 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000690 t.GemPortIDMgmtLock.Lock()
691 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
692 t.GemPortIDMgmtLock.Unlock()
693 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000694 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700695 return nil
696 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000697 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700698 for index := 0; index < int(tp.NumGemPorts); index++ {
699 usGemPortAttributeList = append(usGemPortAttributeList,
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700700 IGemPortAttribute{GemportID: gemPorts[index],
Scott Baker2c1c4822019-10-16 11:02:41 -0700701 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
702 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
703 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
704 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
705 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
706 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
707 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
708 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000709 }
710
Neha Sharma94f16a92020-06-26 04:17:55 +0000711 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Esin Karaman8aa75a72019-12-20 13:11:59 +0000712 //put multicast and unicast downstream GEM port attributes in different lists first
713 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
714 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
715 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700716 IGemPortAttribute{
Esin Karaman8aa75a72019-12-20 13:11:59 +0000717 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
718 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
719 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
720 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
721 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
722 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
723 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
724 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
725 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
726 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
727 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
728 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
729 } else {
730 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700731 IGemPortAttribute{
Esin Karaman8aa75a72019-12-20 13:11:59 +0000732 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
733 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
734 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
735 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
736 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
737 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
738 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
739 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
740 }
741 }
742 //add unicast downstream GEM ports to dsGemPortAttributeList
743 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700744 dsGemPortAttributeList = append(dsGemPortAttributeList,
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700745 IGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000746 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
747 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
748 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
749 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
750 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
751 Weight: dsUnicastGemAttributeList[index].Weight,
752 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
753 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700754 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000755 //add multicast GEM ports to dsGemPortAttributeList afterwards
756 for k := range dsMulticastGemAttributeList {
757 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
758 }
759
Scott Baker2c1c4822019-10-16 11:02:41 -0700760 return &TechProfile{
761 SubscriberIdentifier: uniPortName,
762 Name: tp.Name,
763 ProfileType: tp.ProfileType,
764 Version: tp.Version,
765 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700766 InstanceCtrl: tp.InstanceCtrl,
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700767 UsScheduler: IScheduler{
Scott Baker2c1c4822019-10-16 11:02:41 -0700768 AllocID: tcontIDs[0],
769 Direction: tp.UsScheduler.Direction,
770 AdditionalBw: tp.UsScheduler.AdditionalBw,
771 Priority: tp.UsScheduler.Priority,
772 Weight: tp.UsScheduler.Weight,
773 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
Girish Gowdra246bd0b2020-10-08 21:57:35 -0700774 DsScheduler: IScheduler{
Scott Baker2c1c4822019-10-16 11:02:41 -0700775 AllocID: tcontIDs[0],
776 Direction: tp.DsScheduler.Direction,
777 AdditionalBw: tp.DsScheduler.AdditionalBw,
778 Priority: tp.DsScheduler.Priority,
779 Weight: tp.DsScheduler.Weight,
780 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
781 UpstreamGemPortAttributeList: usGemPortAttributeList,
782 DownstreamGemPortAttributeList: dsGemPortAttributeList}
783}
784
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700785// allocateTPInstance function for EPON
786func (t *TechProfileMgr) allocateEponTPInstance(ctx context.Context, uniPortName string, tp *DefaultEponProfile, intfId uint32, tpInstPath string) *EponProfile {
787
788 var usQueueAttributeList []iUpstreamQueueAttribute
789 var dsQueueAttributeList []iDownstreamQueueAttribute
790 var tcontIDs []uint32
791 var gemPorts []uint32
792 var err error
793
Girish Kumar950f21e2020-08-19 17:42:29 +0000794 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700795
796 if tp.InstanceCtrl.Onu == "multi-instance" {
797 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Kumar950f21e2020-08-19 17:42:29 +0000798 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700799 return nil
800 }
801 } else { // "single-instance"
802 if tpInst, err := t.getSingleInstanceEponTp(ctx, tpInstPath); err != nil {
Girish Kumar950f21e2020-08-19 17:42:29 +0000803 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700804 return nil
805 } else if tpInst == nil {
806 // No "single-instance" tp found on one any uni port for the given TP ID
807 // Allocate a new TcontID or AllocID
808 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Girish Kumar950f21e2020-08-19 17:42:29 +0000809 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700810 return nil
811 }
812 } else {
813 // Use the alloc-id from the existing TpInstance
814 tcontIDs = append(tcontIDs, tpInst.AllocID)
815 }
816 }
Girish Kumar950f21e2020-08-19 17:42:29 +0000817 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700818 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
Girish Kumar950f21e2020-08-19 17:42:29 +0000819 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700820 return nil
821 }
Girish Kumar950f21e2020-08-19 17:42:29 +0000822 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700823 for index := 0; index < int(tp.NumGemPorts); index++ {
824 usQueueAttributeList = append(usQueueAttributeList,
825 iUpstreamQueueAttribute{GemportID: gemPorts[index],
826 MaxQueueSize: tp.UpstreamQueueAttributeList[index].MaxQueueSize,
827 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
828 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
829 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
830 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
831 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
832 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
833 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
834 NumQueueSet: tp.UpstreamQueueAttributeList[index].NumQueueSet,
835 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
836 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
837 PriorityQueue: tp.UpstreamQueueAttributeList[index].PriorityQueue,
838 Weight: tp.UpstreamQueueAttributeList[index].Weight,
839 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
840 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
841 }
842
Girish Kumar950f21e2020-08-19 17:42:29 +0000843 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamQueueAttributeList))
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700844 for index := 0; index < int(tp.NumGemPorts); index++ {
845 dsQueueAttributeList = append(dsQueueAttributeList,
846 iDownstreamQueueAttribute{GemportID: gemPorts[index],
847 MaxQueueSize: tp.DownstreamQueueAttributeList[index].MaxQueueSize,
848 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
849 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
850 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
851 PriorityQueue: tp.DownstreamQueueAttributeList[index].PriorityQueue,
852 Weight: tp.DownstreamQueueAttributeList[index].Weight,
853 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
854 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
855 }
856
857 return &EponProfile{
858 SubscriberIdentifier: uniPortName,
859 Name: tp.Name,
860 ProfileType: tp.ProfileType,
861 Version: tp.Version,
862 NumGemPorts: tp.NumGemPorts,
863 InstanceCtrl: tp.InstanceCtrl,
864 EponAttribute: tp.EponAttribute,
865 AllocID: tcontIDs[0],
866 UpstreamQueueAttributeList: usQueueAttributeList,
867 DownstreamQueueAttributeList: dsQueueAttributeList}
868}
869
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530870// getSingleInstanceTp returns another TpInstance for an ONU on a different
871// uni port for the same TP ID, if it finds one, else nil.
npujar5bf737f2020-01-16 19:35:25 +0530872func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530873 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530874
875 // For example:
876 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
877 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
878 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujar5bf737f2020-01-16 19:35:25 +0530879 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra9447baf2019-11-05 16:42:37 +0530880
Girish Gowdra9447baf2019-11-05 16:42:37 +0530881 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
882 for keyPath, kvPair := range kvPairs {
883 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
884 if err = json.Unmarshal(value, &tpInst); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000885 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530886 return nil, errors.New("error-unmarshal-kv-pair")
887 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000888 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530889 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530890 }
891 }
892 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530893 return nil, nil
894}
895
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700896func (t *TechProfileMgr) getSingleInstanceEponTp(ctx context.Context, tpPath string) (*EponProfile, error) {
897 var tpInst EponProfile
898
899 // For example:
900 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
901 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
902 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
903 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
904
905 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
906 for keyPath, kvPair := range kvPairs {
907 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
908 if err = json.Unmarshal(value, &tpInst); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000909 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700910 return nil, errors.New("error-unmarshal-kv-pair")
911 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000912 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700913 return &tpInst, nil
914 }
915 }
916 }
917 return nil, nil
918}
919
Neha Sharma94f16a92020-06-26 04:17:55 +0000920func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700921 var usGemPortAttributeList []GemPortAttribute
922 var dsGemPortAttributeList []GemPortAttribute
923
924 for _, pbit := range t.config.DefaultPbits {
Neha Sharma94f16a92020-06-26 04:17:55 +0000925 logger.Debugw(ctx, "Creating GEM port", log.Fields{"pbit": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700926 usGemPortAttributeList = append(usGemPortAttributeList,
927 GemPortAttribute{
928 MaxQueueSize: defaultMaxQueueSize,
929 PbitMap: pbit,
930 AesEncryption: defaultAESEncryption,
931 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
932 PriorityQueue: defaultPriorityQueue,
933 Weight: defaultQueueWeight,
934 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
935 DiscardConfig: DiscardConfig{
936 MinThreshold: defaultMinThreshold,
937 MaxThreshold: defaultMaxThreshold,
938 MaxProbability: defaultMaxProbability}})
939 dsGemPortAttributeList = append(dsGemPortAttributeList,
940 GemPortAttribute{
941 MaxQueueSize: defaultMaxQueueSize,
942 PbitMap: pbit,
943 AesEncryption: defaultAESEncryption,
944 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
945 PriorityQueue: defaultPriorityQueue,
946 Weight: defaultQueueWeight,
947 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
948 DiscardConfig: DiscardConfig{
949 MinThreshold: defaultMinThreshold,
950 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000951 MaxProbability: defaultMaxProbability},
952 IsMulticast: defaultIsMulticast,
953 DControlList: defaultAccessControlList,
954 SControlList: defaultAccessControlList,
955 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700956 }
957 return &DefaultTechProfile{
958 Name: t.config.DefaultTPName,
959 ProfileType: t.resourceMgr.GetTechnology(),
960 Version: t.config.TPVersion,
961 NumGemPorts: uint32(len(usGemPortAttributeList)),
962 InstanceCtrl: InstanceControl{
963 Onu: defaultOnuInstance,
964 Uni: defaultUniInstance,
965 MaxGemPayloadSize: defaultGemPayloadSize},
966 UsScheduler: Scheduler{
967 Direction: Direction_name[Direction_UPSTREAM],
968 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
969 Priority: defaultPriority,
970 Weight: defaultWeight,
971 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
972 DsScheduler: Scheduler{
973 Direction: Direction_name[Direction_DOWNSTREAM],
974 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
975 Priority: defaultPriority,
976 Weight: defaultWeight,
977 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
978 UpstreamGemPortAttributeList: usGemPortAttributeList,
979 DownstreamGemPortAttributeList: dsGemPortAttributeList}
980}
981
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700982// getDefaultTechProfile function for EPON
Girish Kumar950f21e2020-08-19 17:42:29 +0000983func (t *TechProfileMgr) getDefaultEponProfile(ctx context.Context) *DefaultEponProfile {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700984
985 var usQueueAttributeList []UpstreamQueueAttribute
986 var dsQueueAttributeList []DownstreamQueueAttribute
987
988 for _, pbit := range t.config.DefaultPbits {
Girish Kumar950f21e2020-08-19 17:42:29 +0000989 logger.Debugw(ctx, "Creating Queue", log.Fields{"pbit": pbit})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700990 usQueueAttributeList = append(usQueueAttributeList,
991 UpstreamQueueAttribute{
992 MaxQueueSize: defaultMaxQueueSize,
993 PbitMap: pbit,
994 AesEncryption: defaultAESEncryption,
995 TrafficType: defaultTrafficType,
996 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
997 NominalInterval: defaultNominalInterval,
998 ToleratedPollJitter: defaultToleratedPollJitter,
999 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
1000 NumQueueSet: defaultNumQueueSet,
1001 QThresholds: QThresholds{
1002 QThreshold1: defaultQThreshold1,
1003 QThreshold2: defaultQThreshold2,
1004 QThreshold3: defaultQThreshold3,
1005 QThreshold4: defaultQThreshold4,
1006 QThreshold5: defaultQThreshold5,
1007 QThreshold6: defaultQThreshold6,
1008 QThreshold7: defaultQThreshold7},
1009 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1010 PriorityQueue: defaultPriorityQueue,
1011 Weight: defaultQueueWeight,
1012 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1013 DiscardConfig: DiscardConfig{
1014 MinThreshold: defaultMinThreshold,
1015 MaxThreshold: defaultMaxThreshold,
1016 MaxProbability: defaultMaxProbability}})
1017 dsQueueAttributeList = append(dsQueueAttributeList,
1018 DownstreamQueueAttribute{
1019 MaxQueueSize: defaultMaxQueueSize,
1020 PbitMap: pbit,
1021 AesEncryption: defaultAESEncryption,
1022 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1023 PriorityQueue: defaultPriorityQueue,
1024 Weight: defaultQueueWeight,
1025 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1026 DiscardConfig: DiscardConfig{
1027 MinThreshold: defaultMinThreshold,
1028 MaxThreshold: defaultMaxThreshold,
1029 MaxProbability: defaultMaxProbability}})
1030 }
1031 return &DefaultEponProfile{
1032 Name: t.config.DefaultTPName,
1033 ProfileType: t.resourceMgr.GetTechnology(),
1034 Version: t.config.TPVersion,
1035 NumGemPorts: uint32(len(usQueueAttributeList)),
1036 InstanceCtrl: InstanceControl{
1037 Onu: defaultOnuInstance,
1038 Uni: defaultUniInstance,
1039 MaxGemPayloadSize: defaultGemPayloadSize},
1040 EponAttribute: EponAttribute{
1041 PackageType: defaultPakageType},
1042 UpstreamQueueAttributeList: usQueueAttributeList,
1043 DownstreamQueueAttributeList: dsQueueAttributeList}
1044}
1045
Neha Sharma94f16a92020-06-26 04:17:55 +00001046func (t *TechProfileMgr) GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32 {
Scott Baker2c1c4822019-10-16 11:02:41 -07001047 var result int32 = -1
1048
1049 if paramType == "direction" {
1050 for key, val := range tp_pb.Direction_value {
1051 if key == paramKey {
1052 result = val
1053 }
1054 }
1055 } else if paramType == "discard_policy" {
1056 for key, val := range tp_pb.DiscardPolicy_value {
1057 if key == paramKey {
1058 result = val
1059 }
1060 }
1061 } else if paramType == "sched_policy" {
1062 for key, val := range tp_pb.SchedulingPolicy_value {
1063 if key == paramKey {
Neha Sharma94f16a92020-06-26 04:17:55 +00001064 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": key, "value": val})
Scott Baker2c1c4822019-10-16 11:02:41 -07001065 result = val
1066 }
1067 }
1068 } else if paramType == "additional_bw" {
1069 for key, val := range tp_pb.AdditionalBW_value {
1070 if key == paramKey {
1071 result = val
1072 }
1073 }
1074 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +00001075 logger.Error(ctx, "Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Scott Baker2c1c4822019-10-16 11:02:41 -07001076 return -1
1077 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001078 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": paramKey, "value": result})
Scott Baker2c1c4822019-10-16 11:02:41 -07001079 return result
1080}
1081
Neha Sharma94f16a92020-06-26 04:17:55 +00001082func (t *TechProfileMgr) GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
1083 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.UsScheduler.Direction))
Scott Baker2c1c4822019-10-16 11:02:41 -07001084 if dir == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001085 logger.Errorf(ctx, "Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001086 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 -07001087 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001088
Neha Sharma94f16a92020-06-26 04:17:55 +00001089 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.UsScheduler.AdditionalBw))
Scott Baker2c1c4822019-10-16 11:02:41 -07001090 if bw == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001091 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001092 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 -07001093 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001094
Neha Sharma94f16a92020-06-26 04:17:55 +00001095 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Scott Baker2c1c4822019-10-16 11:02:41 -07001096 if policy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001097 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001098 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 -07001099 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001100
Scott Baker2c1c4822019-10-16 11:02:41 -07001101 return &tp_pb.SchedulerConfig{
1102 Direction: dir,
1103 AdditionalBw: bw,
1104 Priority: tpInstance.UsScheduler.Priority,
1105 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001106 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001107}
1108
Neha Sharma94f16a92020-06-26 04:17:55 +00001109func (t *TechProfileMgr) GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -07001110
Neha Sharma94f16a92020-06-26 04:17:55 +00001111 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.DsScheduler.Direction))
Scott Baker2c1c4822019-10-16 11:02:41 -07001112 if dir == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001113 logger.Errorf(ctx, "Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001114 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 -07001115 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001116
Neha Sharma94f16a92020-06-26 04:17:55 +00001117 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.DsScheduler.AdditionalBw))
Scott Baker2c1c4822019-10-16 11:02:41 -07001118 if bw == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001119 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001120 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 -07001121 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001122
Neha Sharma94f16a92020-06-26 04:17:55 +00001123 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Scott Baker2c1c4822019-10-16 11:02:41 -07001124 if policy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001125 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001126 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 -07001127 }
1128
1129 return &tp_pb.SchedulerConfig{
1130 Direction: dir,
1131 AdditionalBw: bw,
1132 Priority: tpInstance.DsScheduler.Priority,
1133 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001134 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001135}
1136
1137func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
1138 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
1139
1140 tSched := &tp_pb.TrafficScheduler{
1141 Direction: SchedCfg.Direction,
1142 AllocId: tpInstance.UsScheduler.AllocID,
1143 TrafficShapingInfo: ShapingCfg,
1144 Scheduler: SchedCfg}
1145
1146 return tSched
1147}
1148
Neha Sharma94f16a92020-06-26 04:17:55 +00001149func (tpm *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -07001150
1151 var encryp bool
1152 if Dir == tp_pb.Direction_UPSTREAM {
1153 // upstream GEM ports
1154 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
1155 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1156 for Count := 0; Count < NumGemPorts; Count++ {
1157 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
1158 encryp = true
1159 } else {
1160 encryp = false
1161 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001162
Neha Sharma94f16a92020-06-26 04:17:55 +00001163 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001164 if schedPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001165 logger.Errorf(ctx, "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 +00001166 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1167 }
1168
Neha Sharma94f16a92020-06-26 04:17:55 +00001169 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001170 if discardPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001171 logger.Errorf(ctx, "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 +00001172 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1173 }
1174
Scott Baker2c1c4822019-10-16 11:02:41 -07001175 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
Neha Sharma94f16a92020-06-26 04:17:55 +00001176 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.UsScheduler.Direction)),
Scott Baker2c1c4822019-10-16 11:02:41 -07001177 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
1178 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
1179 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001180 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001181 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
1182 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001183 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001184 })
1185 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001186 logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +00001187 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001188 } else if Dir == tp_pb.Direction_DOWNSTREAM {
1189 //downstream GEM ports
1190 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1191 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1192 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +00001193 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1194 //do not take multicast GEM ports. They are handled separately.
1195 continue
1196 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001197 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1198 encryp = true
1199 } else {
1200 encryp = false
1201 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001202
Neha Sharma94f16a92020-06-26 04:17:55 +00001203 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001204 if schedPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001205 logger.Errorf(ctx, "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 +00001206 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1207 }
1208
Neha Sharma94f16a92020-06-26 04:17:55 +00001209 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001210 if discardPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001211 logger.Errorf(ctx, "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 +00001212 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1213 }
1214
Scott Baker2c1c4822019-10-16 11:02:41 -07001215 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
Neha Sharma94f16a92020-06-26 04:17:55 +00001216 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
Scott Baker2c1c4822019-10-16 11:02:41 -07001217 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
1218 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1219 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001220 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001221 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1222 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001223 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001224 })
1225 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001226 logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +00001227 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001228 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001229
Neha Sharma94f16a92020-06-26 04:17:55 +00001230 logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001231 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -07001232}
1233
Esin Karaman8aa75a72019-12-20 13:11:59 +00001234//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
1235func isMulticastGem(isMulticastAttrValue string) bool {
1236 return isMulticastAttrValue != "" &&
1237 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1238}
1239
Neha Sharma94f16a92020-06-26 04:17:55 +00001240func (tpm *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue {
Esin Karaman8aa75a72019-12-20 13:11:59 +00001241 var encryp bool
1242 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1243 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
1244 for Count := 0; Count < NumGemPorts; Count++ {
1245 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1246 continue
1247 }
1248 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1249 encryp = true
1250 } else {
1251 encryp = false
1252 }
1253 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
Neha Sharma94f16a92020-06-26 04:17:55 +00001254 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
Esin Karaman8aa75a72019-12-20 13:11:59 +00001255 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
1256 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1257 AesEncryption: encryp,
Neha Sharma94f16a92020-06-26 04:17:55 +00001258 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
Esin Karaman8aa75a72019-12-20 13:11:59 +00001259 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1260 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Neha Sharma94f16a92020-06-26 04:17:55 +00001261 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
Esin Karaman8aa75a72019-12-20 13:11:59 +00001262 })
1263 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001264 logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Esin Karaman8aa75a72019-12-20 13:11:59 +00001265 return mcastTrafficQueues
1266}
1267
Neha Sharma94f16a92020-06-26 04:17:55 +00001268func (tpm *TechProfileMgr) GetUsTrafficScheduler(ctx context.Context, tp *TechProfile) *tp_pb.TrafficScheduler {
1269 UsScheduler, _ := tpm.GetUsScheduler(ctx, tp)
Scott Baker2c1c4822019-10-16 11:02:41 -07001270
1271 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
1272 AllocId: tp.UsScheduler.AllocID,
1273 Scheduler: UsScheduler}
1274}
1275
Neha Sharma94f16a92020-06-26 04:17:55 +00001276func (t *TechProfileMgr) GetGemportIDForPbit(ctx context.Context, tp interface{}, dir tp_pb.Direction, pbit uint32) uint32 {
Scott Baker2c1c4822019-10-16 11:02:41 -07001277 /*
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001278 Function to get the Gemport ID mapped to a pbit.
Scott Baker2c1c4822019-10-16 11:02:41 -07001279 */
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001280 switch tp := tp.(type) {
1281 case *TechProfile:
1282 if dir == tp_pb.Direction_UPSTREAM {
1283 // upstream GEM ports
1284 numGemPorts := len(tp.UpstreamGemPortAttributeList)
1285 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1286 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
1287 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1288 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1289 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1290 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1291 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001292 logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001293 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
1294 }
1295 }
1296 }
1297 }
1298 } else if dir == tp_pb.Direction_DOWNSTREAM {
1299 //downstream GEM ports
1300 numGemPorts := len(tp.DownstreamGemPortAttributeList)
1301 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1302 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
1303 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1304 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1305 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1306 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1307 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001308 logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001309 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
1310 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001311 }
1312 }
1313 }
1314 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001315 logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001316 case *EponProfile:
1317 if dir == tp_pb.Direction_UPSTREAM {
1318 // upstream GEM ports
1319 numGemPorts := len(tp.UpstreamQueueAttributeList)
1320 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1321 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
1322 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1323 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1324 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1325 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1326 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001327 logger.Debugw(ctx, "Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001328 return tp.UpstreamQueueAttributeList[gemCnt].GemportID
1329 }
1330 }
1331 }
1332 }
1333 } else if dir == tp_pb.Direction_DOWNSTREAM {
1334 //downstream GEM ports
1335 numGemPorts := len(tp.DownstreamQueueAttributeList)
1336 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1337 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
1338 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1339 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1340 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1341 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1342 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001343 logger.Debugw(ctx, "Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001344 return tp.DownstreamQueueAttributeList[gemCnt].GemportID
1345 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001346 }
1347 }
1348 }
1349 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001350 logger.Errorw(ctx, "No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001351 default:
Neha Sharma94f16a92020-06-26 04:17:55 +00001352 logger.Errorw(ctx, "unknown-tech", log.Fields{"tp": tp})
Scott Baker2c1c4822019-10-16 11:02:41 -07001353 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001354 return 0
1355}
Girish Gowdra9447baf2019-11-05 16:42:37 +05301356
1357// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001358func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) interface{} {
1359 var tpTech TechProfile
1360 var tpEpon EponProfile
1361
Girish Gowdra6172a2e2019-11-27 14:37:58 +05301362 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +05301363
npujar5bf737f2020-01-16 19:35:25 +05301364 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001365 tech := t.resourceMgr.GetTechnology()
1366 tpInstancesTech := make([]TechProfile, 0, len(kvPairs))
1367 tpInstancesEpon := make([]EponProfile, 0, len(kvPairs))
1368
Girish Gowdra9447baf2019-11-05 16:42:37 +05301369 for kvPath, kvPair := range kvPairs {
1370 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
Andrea Campanella904fe322020-06-26 18:56:51 +02001371 if tech == xgspon || tech == gpon {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001372 if err = json.Unmarshal(value, &tpTech); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +00001373 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001374 continue
1375 } else {
1376 tpInstancesTech = append(tpInstancesTech, tpTech)
1377 }
1378 } else if tech == epon {
1379 if err = json.Unmarshal(value, &tpEpon); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +00001380 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001381 continue
1382 } else {
1383 tpInstancesEpon = append(tpInstancesEpon, tpEpon)
1384 }
Girish Gowdra9447baf2019-11-05 16:42:37 +05301385 }
1386 }
1387 }
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001388
1389 switch tech {
Andrea Campanella904fe322020-06-26 18:56:51 +02001390 case xgspon, gpon:
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001391 return tpInstancesTech
1392 case epon:
1393 return tpInstancesEpon
1394 default:
Girish Kumar950f21e2020-08-19 17:42:29 +00001395 logger.Errorw(ctx, "unknown-technology", log.Fields{"tech": tech})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001396 return nil
1397 }
Girish Gowdra9447baf2019-11-05 16:42:37 +05301398 }
1399 return nil
1400}