blob: a12df1dd2c8d1e0bafc6babdb2c90d5978381943 [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
serkant.uluderyab38671c2019-11-01 09:35:38 -070029 "github.com/opencord/voltha-lib-go/v3/pkg/db"
30
31 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v3/pkg/log"
33 tp_pb "github.com/opencord/voltha-protos/v3/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
194type iScheduler struct {
195 AllocID uint32 `json:"alloc_id"`
196 Direction string `json:"direction"`
197 AdditionalBw string `json:"additional_bw"`
198 Priority uint32 `json:"priority"`
199 Weight uint32 `json:"weight"`
200 QSchedPolicy string `json:"q_sched_policy"`
201}
202type iGemPortAttribute struct {
203 GemportID uint32 `json:"gemport_id"`
204 MaxQueueSize string `json:"max_q_size"`
205 PbitMap string `json:"pbit_map"`
206 AesEncryption string `json:"aes_encryption"`
207 SchedulingPolicy string `json:"scheduling_policy"`
208 PriorityQueue uint32 `json:"priority_q"`
209 Weight uint32 `json:"weight"`
210 DiscardPolicy string `json:"discard_policy"`
211 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000212 IsMulticast string `json:"is_multicast"`
213 DControlList string `json:"dynamic_access_control_list"`
214 SControlList string `json:"static_access_control_list"`
215 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700216}
217
218type TechProfileMgr struct {
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000219 config *TechProfileFlags
220 resourceMgr iPonResourceMgr
221 GemPortIDMgmtLock sync.RWMutex
222 AllocIDMgmtLock sync.RWMutex
Scott Baker2c1c4822019-10-16 11:02:41 -0700223}
224type DefaultTechProfile struct {
225 Name string `json:"name"`
226 ProfileType string `json:"profile_type"`
227 Version int `json:"version"`
228 NumGemPorts uint32 `json:"num_gem_ports"`
229 InstanceCtrl InstanceControl `json:"instance_control"`
230 UsScheduler Scheduler `json:"us_scheduler"`
231 DsScheduler Scheduler `json:"ds_scheduler"`
232 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
233 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
234}
235type TechProfile struct {
236 Name string `json:"name"`
237 SubscriberIdentifier string `json:"subscriber_identifier"`
238 ProfileType string `json:"profile_type"`
239 Version int `json:"version"`
240 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700241 InstanceCtrl InstanceControl `json:"instance_control"`
242 UsScheduler iScheduler `json:"us_scheduler"`
243 DsScheduler iScheduler `json:"ds_scheduler"`
244 UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"`
245 DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
246}
247
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700248// QThresholds struct for EPON
249type QThresholds struct {
250 QThreshold1 uint32 `json:"q_threshold1"`
251 QThreshold2 uint32 `json:"q_threshold2"`
252 QThreshold3 uint32 `json:"q_threshold3"`
253 QThreshold4 uint32 `json:"q_threshold4"`
254 QThreshold5 uint32 `json:"q_threshold5"`
255 QThreshold6 uint32 `json:"q_threshold6"`
256 QThreshold7 uint32 `json:"q_threshold7"`
257}
258
259// UpstreamQueueAttribute struct for EPON
260type UpstreamQueueAttribute struct {
261 MaxQueueSize string `json:"max_q_size"`
262 PbitMap string `json:"pbit_map"`
263 AesEncryption string `json:"aes_encryption"`
264 TrafficType string `json:"traffic_type"`
265 UnsolicitedGrantSize uint32 `json:"unsolicited_grant_size"`
266 NominalInterval uint32 `json:"nominal_interval"`
267 ToleratedPollJitter uint32 `json:"tolerated_poll_jitter"`
268 RequestTransmissionPolicy uint32 `json:"request_transmission_policy"`
269 NumQueueSet uint32 `json:"num_q_sets"`
270 QThresholds QThresholds `json:"q_thresholds"`
271 SchedulingPolicy string `json:"scheduling_policy"`
272 PriorityQueue uint32 `json:"priority_q"`
273 Weight uint32 `json:"weight"`
274 DiscardPolicy string `json:"discard_policy"`
275 DiscardConfig DiscardConfig `json:"discard_config"`
276}
277
278// Default EPON constants
279const (
280 defaultPakageType = "B"
281)
282const (
283 defaultTrafficType = "BE"
284 defaultUnsolicitedGrantSize = 0
285 defaultNominalInterval = 0
286 defaultToleratedPollJitter = 0
287 defaultRequestTransmissionPolicy = 0
288 defaultNumQueueSet = 2
289)
290const (
291 defaultQThreshold1 = 5500
292 defaultQThreshold2 = 0
293 defaultQThreshold3 = 0
294 defaultQThreshold4 = 0
295 defaultQThreshold5 = 0
296 defaultQThreshold6 = 0
297 defaultQThreshold7 = 0
298)
299
300// DownstreamQueueAttribute struct for EPON
301type DownstreamQueueAttribute struct {
302 MaxQueueSize string `json:"max_q_size"`
303 PbitMap string `json:"pbit_map"`
304 AesEncryption string `json:"aes_encryption"`
305 SchedulingPolicy string `json:"scheduling_policy"`
306 PriorityQueue uint32 `json:"priority_q"`
307 Weight uint32 `json:"weight"`
308 DiscardPolicy string `json:"discard_policy"`
309 DiscardConfig DiscardConfig `json:"discard_config"`
310}
311
312// iUpstreamQueueAttribute struct for EPON
313type iUpstreamQueueAttribute struct {
314 GemportID uint32 `json:"q_id"`
315 MaxQueueSize string `json:"max_q_size"`
316 PbitMap string `json:"pbit_map"`
317 AesEncryption string `json:"aes_encryption"`
318 TrafficType string `json:"traffic_type"`
319 UnsolicitedGrantSize uint32 `json:"unsolicited_grant_size"`
320 NominalInterval uint32 `json:"nominal_interval"`
321 ToleratedPollJitter uint32 `json:"tolerated_poll_jitter"`
322 RequestTransmissionPolicy uint32 `json:"request_transmission_policy"`
323 NumQueueSet uint32 `json:"num_q_sets"`
324 QThresholds QThresholds `json:"q_thresholds"`
325 SchedulingPolicy string `json:"scheduling_policy"`
326 PriorityQueue uint32 `json:"priority_q"`
327 Weight uint32 `json:"weight"`
328 DiscardPolicy string `json:"discard_policy"`
329 DiscardConfig DiscardConfig `json:"discard_config"`
330}
331
332// iDownstreamQueueAttribute struct for EPON
333type iDownstreamQueueAttribute struct {
334 GemportID uint32 `json:"q_id"`
335 MaxQueueSize string `json:"max_q_size"`
336 PbitMap string `json:"pbit_map"`
337 AesEncryption string `json:"aes_encryption"`
338 SchedulingPolicy string `json:"scheduling_policy"`
339 PriorityQueue uint32 `json:"priority_q"`
340 Weight uint32 `json:"weight"`
341 DiscardPolicy string `json:"discard_policy"`
342 DiscardConfig DiscardConfig `json:"discard_config"`
343}
344
345// EponAttribute struct for EPON
346type EponAttribute struct {
347 PackageType string `json:"pakage_type"`
348}
349
350// DefaultTechProfile struct for EPON
351type DefaultEponProfile struct {
352 Name string `json:"name"`
353 ProfileType string `json:"profile_type"`
354 Version int `json:"version"`
355 NumGemPorts uint32 `json:"num_gem_ports"`
356 InstanceCtrl InstanceControl `json:"instance_control"`
357 EponAttribute EponAttribute `json:"epon_attribute"`
358 UpstreamQueueAttributeList []UpstreamQueueAttribute `json:"upstream_queue_attribute_list"`
359 DownstreamQueueAttributeList []DownstreamQueueAttribute `json:"downstream_queue_attribute_list"`
360}
361
362// TechProfile struct for EPON
363type EponProfile struct {
364 Name string `json:"name"`
365 SubscriberIdentifier string `json:"subscriber_identifier"`
366 ProfileType string `json:"profile_type"`
367 Version int `json:"version"`
368 NumGemPorts uint32 `json:"num_gem_ports"`
369 InstanceCtrl InstanceControl `json:"instance_control"`
370 EponAttribute EponAttribute `json:"epon_attribute"`
371 AllocID uint32 `json:"llid"`
372 UpstreamQueueAttributeList []iUpstreamQueueAttribute `json:"upstream_queue_attribute_list"`
373 DownstreamQueueAttributeList []iDownstreamQueueAttribute `json:"downstream_queue_attribute_list"`
374}
375
376const (
Andrea Campanella904fe322020-06-26 18:56:51 +0200377 xgspon = "XGS-PON"
378 gpon = "GPON"
379 epon = "EPON"
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700380)
381
Neha Sharma94f16a92020-06-26 04:17:55 +0000382func (t *TechProfileMgr) SetKVClient(ctx context.Context) *db.Backend {
383 kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700384 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000385 logger.Errorw(ctx, "failed-to-create-kv-client",
Scott Baker2c1c4822019-10-16 11:02:41 -0700386 log.Fields{
Neha Sharmadd9af392020-04-28 09:03:57 +0000387 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700388 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
389 "error": err.Error(),
390 })
391 return nil
392 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500393 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700394 Client: kvClient,
395 StoreType: t.config.KVStoreType,
Neha Sharmadd9af392020-04-28 09:03:57 +0000396 Address: t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700397 Timeout: t.config.KVStoreTimeout,
398 PathPrefix: t.config.TPKVPathPrefix}
399
400 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700401 issue between kv store and backend , core is not calling NewBackend directly
402 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
403 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
Scott Baker2c1c4822019-10-16 11:02:41 -0700404 */
405}
406
Neha Sharma94f16a92020-06-26 04:17:55 +0000407func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700408
Neha Sharma94f16a92020-06-26 04:17:55 +0000409 logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
Scott Baker2c1c4822019-10-16 11:02:41 -0700410 switch storeType {
411 case "consul":
Neha Sharma94f16a92020-06-26 04:17:55 +0000412 return kvstore.NewConsulClient(ctx, address, timeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700413 case "etcd":
Neha Sharma94f16a92020-06-26 04:17:55 +0000414 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
Scott Baker2c1c4822019-10-16 11:02:41 -0700415 }
416 return nil, errors.New("unsupported-kv-store")
417}
418
Neha Sharma94f16a92020-06-26 04:17:55 +0000419func NewTechProfile(ctx context.Context, resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700420 var techprofileObj TechProfileMgr
Neha Sharma94f16a92020-06-26 04:17:55 +0000421 logger.Debug(ctx, "Initializing techprofile Manager")
Neha Sharmadd9af392020-04-28 09:03:57 +0000422 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
Neha Sharma94f16a92020-06-26 04:17:55 +0000423 techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx)
Scott Baker2c1c4822019-10-16 11:02:41 -0700424 if techprofileObj.config.KVBackend == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000425 logger.Error(ctx, "Failed to initialize KV backend\n")
Scott Baker2c1c4822019-10-16 11:02:41 -0700426 return nil, errors.New("KV backend init failed")
427 }
428 techprofileObj.resourceMgr = resourceMgr
Neha Sharma94f16a92020-06-26 04:17:55 +0000429 logger.Debug(ctx, "Initializing techprofile object instance success")
Scott Baker2c1c4822019-10-16 11:02:41 -0700430 return &techprofileObj, nil
431}
432
Neha Sharma94f16a92020-06-26 04:17:55 +0000433func (t *TechProfileMgr) GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string {
434 logger.Debugw(ctx, "get-tp-instance-kv-path", log.Fields{
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700435 "uniPortName": uniPortName,
436 "tpId": techProfiletblID,
437 })
Scott Baker2c1c4822019-10-16 11:02:41 -0700438 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
439}
440
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700441func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (interface{}, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700442 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530443 var kvResult *kvstore.KVPair
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700444 var KvTpIns TechProfile
445 var KvEponIns EponProfile
446 var resPtr interface{}
447 // For example:
448 // tpInstPath like "XGS-PON/64/uni_port_name"
449 // is broken into ["XGS-PON" "64" ...]
450 pathSlice := regexp.MustCompile(`/`).Split(path, -1)
451 switch pathSlice[0] {
Andrea Campanella904fe322020-06-26 18:56:51 +0200452 case xgspon, gpon:
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700453 resPtr = &KvTpIns
454 case epon:
455 resPtr = &KvEponIns
456 default:
457 log.Errorw("unknown-tech", log.Fields{"tech": pathSlice[0]})
458 return nil, fmt.Errorf("unknown-tech-%s", pathSlice[0])
459 }
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700460
npujar5bf737f2020-01-16 19:35:25 +0530461 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530462 if kvResult == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000463 logger.Infow(ctx, "tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530464 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700465 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530466 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700467 if err = json.Unmarshal(value, resPtr); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000468 logger.Errorw(ctx, "error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530469 return nil, errors.New("error-unmarshal-kv-result")
470 } else {
471 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700472 }
473 }
474 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530475 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700476}
477
npujar5bf737f2020-01-16 19:35:25 +0530478func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Neha Sharma94f16a92020-06-26 04:17:55 +0000479 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
480 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700481 tpInstanceJson, err := json.Marshal(*tpInstance)
482 if err == nil {
483 // Backend will convert JSON byte array into string format
Neha Sharma94f16a92020-06-26 04:17:55 +0000484 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujar5bf737f2020-01-16 19:35:25 +0530485 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Scott Baker2c1c4822019-10-16 11:02:41 -0700486 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000487 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700488 }
489 return err
490}
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700491
492func (t *TechProfileMgr) addEponProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *EponProfile) error {
Neha Sharma94f16a92020-06-26 04:17:55 +0000493 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
494 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700495 tpInstanceJson, err := json.Marshal(*tpInstance)
496 if err == nil {
497 // Backend will convert JSON byte array into string format
Neha Sharma94f16a92020-06-26 04:17:55 +0000498 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700499 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
500 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000501 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700502 }
503 return err
504}
505
npujar5bf737f2020-01-16 19:35:25 +0530506func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700507 var kvtechprofile DefaultTechProfile
508 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Neha Sharma94f16a92020-06-26 04:17:55 +0000509 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujar5bf737f2020-01-16 19:35:25 +0530510 kvresult, err := t.config.KVBackend.Get(ctx, key)
Scott Baker2c1c4822019-10-16 11:02:41 -0700511 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000512 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
Scott Baker2c1c4822019-10-16 11:02:41 -0700513 return nil
514 }
515 if kvresult != nil {
516 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
517 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000518 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000519 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 +0000520 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700521 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000522
Neha Sharma94f16a92020-06-26 04:17:55 +0000523 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000524 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700525 }
526 }
527 return nil
528}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000529
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700530func (t *TechProfileMgr) getEponTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultEponProfile {
531 var kvtechprofile DefaultEponProfile
532 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Neha Sharma94f16a92020-06-26 04:17:55 +0000533 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700534 kvresult, err := t.config.KVBackend.Get(ctx, key)
535 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000536 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700537 return nil
538 }
539 if kvresult != nil {
540 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
541 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
542 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000543 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 -0700544 return nil
545 }
546
Neha Sharma94f16a92020-06-26 04:17:55 +0000547 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700548 return &kvtechprofile
549 }
550 }
551 return nil
552}
553
554func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (interface{}, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700555 var tpInstance *TechProfile
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700556 var tpEponInstance *EponProfile
557
Neha Sharma94f16a92020-06-26 04:17:55 +0000558 logger.Infow(ctx, "creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530559
560 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
561 if !uniPortNameFormat.Match([]byte(uniPortName)) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000562 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000563 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530564 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000565 tpInstancePath := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700566 // For example:
567 // tpInstPath like "XGS-PON/64/uni_port_name"
568 // is broken into ["XGS-PON" "64" ...]
569 pathSlice := regexp.MustCompile(`/`).Split(tpInstancePath, -1)
570 if pathSlice[0] == epon {
571 tp := t.getEponTPFromKVStore(ctx, techProfiletblID)
572 if tp != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000573 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
574 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700575 tp = t.getDefaultEponProfile()
576 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000577 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700578 }
579 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000580 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700581 tp = t.getDefaultEponProfile()
582 }
583
584 if tpEponInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpEponInstance == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000585 logger.Error(ctx, "tp-intance-allocation-failed")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700586 return nil, errors.New("tp-intance-allocation-failed")
587 }
588 if err := t.addEponProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpEponInstance); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000589 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700590 return nil, errors.New("error-adding-tp-to-kv-store")
591 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000592 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700593 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
594 return tpEponInstance, nil
595 } else {
596 tp := t.getTPFromKVStore(ctx, techProfiletblID)
597 if tp != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000598 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
599 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
600 tp = t.getDefaultTechProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700601 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000602 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700603 }
604 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000605 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
606 tp = t.getDefaultTechProfile(ctx)
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700607 }
608
609 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000610 logger.Error(ctx, "tp-intance-allocation-failed")
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700611 return nil, errors.New("tp-intance-allocation-failed")
612 }
613 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000614 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700615 return nil, errors.New("error-adding-tp-to-kv-store")
616 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000617 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700618 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
619 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700620 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700621}
622
npujar5bf737f2020-01-16 19:35:25 +0530623func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Neha Sharma94f16a92020-06-26 04:17:55 +0000624 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530625 return t.config.KVBackend.Delete(ctx, path)
Scott Baker2c1c4822019-10-16 11:02:41 -0700626}
627
Neha Sharma94f16a92020-06-26 04:17:55 +0000628func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl InstanceControl) error {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530629 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000630 logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530631 return errors.New("invalid-onu-instance-ctl-attr")
632 }
633
634 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000635 logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530636 return errors.New("invalid-uni-instance-ctl-attr")
637 }
638
639 if instCtl.Uni == "multi-instance" {
Neha Sharma94f16a92020-06-26 04:17:55 +0000640 logger.Error(ctx, "uni-multi-instance-tp-not-supported")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530641 return errors.New("uni-multi-instance-tp-not-supported")
642 }
643
644 return nil
645}
646
npujar5bf737f2020-01-16 19:35:25 +0530647func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700648
649 var usGemPortAttributeList []iGemPortAttribute
650 var dsGemPortAttributeList []iGemPortAttribute
Esin Karaman8aa75a72019-12-20 13:11:59 +0000651 var dsMulticastGemAttributeList []iGemPortAttribute
652 var dsUnicastGemAttributeList []iGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700653 var tcontIDs []uint32
654 var gemPorts []uint32
655 var err error
656
Neha Sharma94f16a92020-06-26 04:17:55 +0000657 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 +0530658
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530659 if tp.InstanceCtrl.Onu == "multi-instance" {
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000660 t.AllocIDMgmtLock.Lock()
661 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
662 t.AllocIDMgmtLock.Unlock()
663 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000664 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530665 return nil
666 }
667 } else { // "single-instance"
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800668 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000669 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800670 return nil
671 } else if tpInst == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530672 // No "single-instance" tp found on one any uni port for the given TP ID
673 // Allocate a new TcontID or AllocID
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000674 t.AllocIDMgmtLock.Lock()
675 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
676 t.AllocIDMgmtLock.Unlock()
677 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000678 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530679 return nil
680 }
681 } else {
682 // Use the alloc-id from the existing TpInstance
683 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
684 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700685 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000686 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Gamze Abakadfdd8f82020-05-04 08:39:50 +0000687 t.GemPortIDMgmtLock.Lock()
688 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
689 t.GemPortIDMgmtLock.Unlock()
690 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000691 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700692 return nil
693 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000694 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700695 for index := 0; index < int(tp.NumGemPorts); index++ {
696 usGemPortAttributeList = append(usGemPortAttributeList,
697 iGemPortAttribute{GemportID: gemPorts[index],
698 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
699 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
700 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
701 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
702 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
703 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
704 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
705 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000706 }
707
Neha Sharma94f16a92020-06-26 04:17:55 +0000708 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Esin Karaman8aa75a72019-12-20 13:11:59 +0000709 //put multicast and unicast downstream GEM port attributes in different lists first
710 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
711 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
712 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
713 iGemPortAttribute{
714 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
715 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
716 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
717 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
718 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
719 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
720 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
721 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
722 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
723 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
724 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
725 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
726 } else {
727 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
728 iGemPortAttribute{
729 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
730 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
731 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
732 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
733 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
734 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
735 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
736 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
737 }
738 }
739 //add unicast downstream GEM ports to dsGemPortAttributeList
740 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700741 dsGemPortAttributeList = append(dsGemPortAttributeList,
742 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000743 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
744 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
745 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
746 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
747 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
748 Weight: dsUnicastGemAttributeList[index].Weight,
749 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
750 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700751 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000752 //add multicast GEM ports to dsGemPortAttributeList afterwards
753 for k := range dsMulticastGemAttributeList {
754 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
755 }
756
Scott Baker2c1c4822019-10-16 11:02:41 -0700757 return &TechProfile{
758 SubscriberIdentifier: uniPortName,
759 Name: tp.Name,
760 ProfileType: tp.ProfileType,
761 Version: tp.Version,
762 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700763 InstanceCtrl: tp.InstanceCtrl,
764 UsScheduler: iScheduler{
765 AllocID: tcontIDs[0],
766 Direction: tp.UsScheduler.Direction,
767 AdditionalBw: tp.UsScheduler.AdditionalBw,
768 Priority: tp.UsScheduler.Priority,
769 Weight: tp.UsScheduler.Weight,
770 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
771 DsScheduler: iScheduler{
772 AllocID: tcontIDs[0],
773 Direction: tp.DsScheduler.Direction,
774 AdditionalBw: tp.DsScheduler.AdditionalBw,
775 Priority: tp.DsScheduler.Priority,
776 Weight: tp.DsScheduler.Weight,
777 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
778 UpstreamGemPortAttributeList: usGemPortAttributeList,
779 DownstreamGemPortAttributeList: dsGemPortAttributeList}
780}
781
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700782// allocateTPInstance function for EPON
783func (t *TechProfileMgr) allocateEponTPInstance(ctx context.Context, uniPortName string, tp *DefaultEponProfile, intfId uint32, tpInstPath string) *EponProfile {
784
785 var usQueueAttributeList []iUpstreamQueueAttribute
786 var dsQueueAttributeList []iDownstreamQueueAttribute
787 var tcontIDs []uint32
788 var gemPorts []uint32
789 var err error
790
791 log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
792
793 if tp.InstanceCtrl.Onu == "multi-instance" {
794 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
795 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
796 return nil
797 }
798 } else { // "single-instance"
799 if tpInst, err := t.getSingleInstanceEponTp(ctx, tpInstPath); err != nil {
800 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
801 return nil
802 } else if tpInst == nil {
803 // No "single-instance" tp found on one any uni port for the given TP ID
804 // Allocate a new TcontID or AllocID
805 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
806 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
807 return nil
808 }
809 } else {
810 // Use the alloc-id from the existing TpInstance
811 tcontIDs = append(tcontIDs, tpInst.AllocID)
812 }
813 }
814 log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
815 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
816 log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
817 return nil
818 }
819 log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
820 for index := 0; index < int(tp.NumGemPorts); index++ {
821 usQueueAttributeList = append(usQueueAttributeList,
822 iUpstreamQueueAttribute{GemportID: gemPorts[index],
823 MaxQueueSize: tp.UpstreamQueueAttributeList[index].MaxQueueSize,
824 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
825 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
826 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
827 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
828 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
829 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
830 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
831 NumQueueSet: tp.UpstreamQueueAttributeList[index].NumQueueSet,
832 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
833 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
834 PriorityQueue: tp.UpstreamQueueAttributeList[index].PriorityQueue,
835 Weight: tp.UpstreamQueueAttributeList[index].Weight,
836 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
837 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
838 }
839
840 log.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamQueueAttributeList))
841 for index := 0; index < int(tp.NumGemPorts); index++ {
842 dsQueueAttributeList = append(dsQueueAttributeList,
843 iDownstreamQueueAttribute{GemportID: gemPorts[index],
844 MaxQueueSize: tp.DownstreamQueueAttributeList[index].MaxQueueSize,
845 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
846 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
847 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
848 PriorityQueue: tp.DownstreamQueueAttributeList[index].PriorityQueue,
849 Weight: tp.DownstreamQueueAttributeList[index].Weight,
850 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
851 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
852 }
853
854 return &EponProfile{
855 SubscriberIdentifier: uniPortName,
856 Name: tp.Name,
857 ProfileType: tp.ProfileType,
858 Version: tp.Version,
859 NumGemPorts: tp.NumGemPorts,
860 InstanceCtrl: tp.InstanceCtrl,
861 EponAttribute: tp.EponAttribute,
862 AllocID: tcontIDs[0],
863 UpstreamQueueAttributeList: usQueueAttributeList,
864 DownstreamQueueAttributeList: dsQueueAttributeList}
865}
866
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530867// getSingleInstanceTp returns another TpInstance for an ONU on a different
868// uni port for the same TP ID, if it finds one, else nil.
npujar5bf737f2020-01-16 19:35:25 +0530869func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530870 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530871
872 // For example:
873 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
874 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
875 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujar5bf737f2020-01-16 19:35:25 +0530876 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra9447baf2019-11-05 16:42:37 +0530877
Girish Gowdra9447baf2019-11-05 16:42:37 +0530878 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
879 for keyPath, kvPair := range kvPairs {
880 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
881 if err = json.Unmarshal(value, &tpInst); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000882 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530883 return nil, errors.New("error-unmarshal-kv-pair")
884 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000885 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530886 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530887 }
888 }
889 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530890 return nil, nil
891}
892
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700893func (t *TechProfileMgr) getSingleInstanceEponTp(ctx context.Context, tpPath string) (*EponProfile, error) {
894 var tpInst EponProfile
895
896 // For example:
897 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
898 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
899 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
900 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
901
902 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
903 for keyPath, kvPair := range kvPairs {
904 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
905 if err = json.Unmarshal(value, &tpInst); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000906 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700907 return nil, errors.New("error-unmarshal-kv-pair")
908 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000909 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700910 return &tpInst, nil
911 }
912 }
913 }
914 return nil, nil
915}
916
Neha Sharma94f16a92020-06-26 04:17:55 +0000917func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700918 var usGemPortAttributeList []GemPortAttribute
919 var dsGemPortAttributeList []GemPortAttribute
920
921 for _, pbit := range t.config.DefaultPbits {
Neha Sharma94f16a92020-06-26 04:17:55 +0000922 logger.Debugw(ctx, "Creating GEM port", log.Fields{"pbit": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700923 usGemPortAttributeList = append(usGemPortAttributeList,
924 GemPortAttribute{
925 MaxQueueSize: defaultMaxQueueSize,
926 PbitMap: pbit,
927 AesEncryption: defaultAESEncryption,
928 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
929 PriorityQueue: defaultPriorityQueue,
930 Weight: defaultQueueWeight,
931 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
932 DiscardConfig: DiscardConfig{
933 MinThreshold: defaultMinThreshold,
934 MaxThreshold: defaultMaxThreshold,
935 MaxProbability: defaultMaxProbability}})
936 dsGemPortAttributeList = append(dsGemPortAttributeList,
937 GemPortAttribute{
938 MaxQueueSize: defaultMaxQueueSize,
939 PbitMap: pbit,
940 AesEncryption: defaultAESEncryption,
941 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
942 PriorityQueue: defaultPriorityQueue,
943 Weight: defaultQueueWeight,
944 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
945 DiscardConfig: DiscardConfig{
946 MinThreshold: defaultMinThreshold,
947 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000948 MaxProbability: defaultMaxProbability},
949 IsMulticast: defaultIsMulticast,
950 DControlList: defaultAccessControlList,
951 SControlList: defaultAccessControlList,
952 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700953 }
954 return &DefaultTechProfile{
955 Name: t.config.DefaultTPName,
956 ProfileType: t.resourceMgr.GetTechnology(),
957 Version: t.config.TPVersion,
958 NumGemPorts: uint32(len(usGemPortAttributeList)),
959 InstanceCtrl: InstanceControl{
960 Onu: defaultOnuInstance,
961 Uni: defaultUniInstance,
962 MaxGemPayloadSize: defaultGemPayloadSize},
963 UsScheduler: Scheduler{
964 Direction: Direction_name[Direction_UPSTREAM],
965 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
966 Priority: defaultPriority,
967 Weight: defaultWeight,
968 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
969 DsScheduler: Scheduler{
970 Direction: Direction_name[Direction_DOWNSTREAM],
971 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
972 Priority: defaultPriority,
973 Weight: defaultWeight,
974 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
975 UpstreamGemPortAttributeList: usGemPortAttributeList,
976 DownstreamGemPortAttributeList: dsGemPortAttributeList}
977}
978
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -0700979// getDefaultTechProfile function for EPON
980func (t *TechProfileMgr) getDefaultEponProfile() *DefaultEponProfile {
981
982 var usQueueAttributeList []UpstreamQueueAttribute
983 var dsQueueAttributeList []DownstreamQueueAttribute
984
985 for _, pbit := range t.config.DefaultPbits {
986 log.Debugw("Creating Queue", log.Fields{"pbit": pbit})
987 usQueueAttributeList = append(usQueueAttributeList,
988 UpstreamQueueAttribute{
989 MaxQueueSize: defaultMaxQueueSize,
990 PbitMap: pbit,
991 AesEncryption: defaultAESEncryption,
992 TrafficType: defaultTrafficType,
993 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
994 NominalInterval: defaultNominalInterval,
995 ToleratedPollJitter: defaultToleratedPollJitter,
996 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
997 NumQueueSet: defaultNumQueueSet,
998 QThresholds: QThresholds{
999 QThreshold1: defaultQThreshold1,
1000 QThreshold2: defaultQThreshold2,
1001 QThreshold3: defaultQThreshold3,
1002 QThreshold4: defaultQThreshold4,
1003 QThreshold5: defaultQThreshold5,
1004 QThreshold6: defaultQThreshold6,
1005 QThreshold7: defaultQThreshold7},
1006 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1007 PriorityQueue: defaultPriorityQueue,
1008 Weight: defaultQueueWeight,
1009 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1010 DiscardConfig: DiscardConfig{
1011 MinThreshold: defaultMinThreshold,
1012 MaxThreshold: defaultMaxThreshold,
1013 MaxProbability: defaultMaxProbability}})
1014 dsQueueAttributeList = append(dsQueueAttributeList,
1015 DownstreamQueueAttribute{
1016 MaxQueueSize: defaultMaxQueueSize,
1017 PbitMap: pbit,
1018 AesEncryption: defaultAESEncryption,
1019 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1020 PriorityQueue: defaultPriorityQueue,
1021 Weight: defaultQueueWeight,
1022 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1023 DiscardConfig: DiscardConfig{
1024 MinThreshold: defaultMinThreshold,
1025 MaxThreshold: defaultMaxThreshold,
1026 MaxProbability: defaultMaxProbability}})
1027 }
1028 return &DefaultEponProfile{
1029 Name: t.config.DefaultTPName,
1030 ProfileType: t.resourceMgr.GetTechnology(),
1031 Version: t.config.TPVersion,
1032 NumGemPorts: uint32(len(usQueueAttributeList)),
1033 InstanceCtrl: InstanceControl{
1034 Onu: defaultOnuInstance,
1035 Uni: defaultUniInstance,
1036 MaxGemPayloadSize: defaultGemPayloadSize},
1037 EponAttribute: EponAttribute{
1038 PackageType: defaultPakageType},
1039 UpstreamQueueAttributeList: usQueueAttributeList,
1040 DownstreamQueueAttributeList: dsQueueAttributeList}
1041}
1042
Neha Sharma94f16a92020-06-26 04:17:55 +00001043func (t *TechProfileMgr) GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32 {
Scott Baker2c1c4822019-10-16 11:02:41 -07001044 var result int32 = -1
1045
1046 if paramType == "direction" {
1047 for key, val := range tp_pb.Direction_value {
1048 if key == paramKey {
1049 result = val
1050 }
1051 }
1052 } else if paramType == "discard_policy" {
1053 for key, val := range tp_pb.DiscardPolicy_value {
1054 if key == paramKey {
1055 result = val
1056 }
1057 }
1058 } else if paramType == "sched_policy" {
1059 for key, val := range tp_pb.SchedulingPolicy_value {
1060 if key == paramKey {
Neha Sharma94f16a92020-06-26 04:17:55 +00001061 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": key, "value": val})
Scott Baker2c1c4822019-10-16 11:02:41 -07001062 result = val
1063 }
1064 }
1065 } else if paramType == "additional_bw" {
1066 for key, val := range tp_pb.AdditionalBW_value {
1067 if key == paramKey {
1068 result = val
1069 }
1070 }
1071 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +00001072 logger.Error(ctx, "Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Scott Baker2c1c4822019-10-16 11:02:41 -07001073 return -1
1074 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001075 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": paramKey, "value": result})
Scott Baker2c1c4822019-10-16 11:02:41 -07001076 return result
1077}
1078
Neha Sharma94f16a92020-06-26 04:17:55 +00001079func (t *TechProfileMgr) GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
1080 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.UsScheduler.Direction))
Scott Baker2c1c4822019-10-16 11:02:41 -07001081 if dir == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001082 logger.Errorf(ctx, "Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001083 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 -07001084 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001085
Neha Sharma94f16a92020-06-26 04:17:55 +00001086 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.UsScheduler.AdditionalBw))
Scott Baker2c1c4822019-10-16 11:02:41 -07001087 if bw == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001088 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001089 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 -07001090 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001091
Neha Sharma94f16a92020-06-26 04:17:55 +00001092 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Scott Baker2c1c4822019-10-16 11:02:41 -07001093 if policy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001094 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 +00001095 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 -07001096 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001097
Scott Baker2c1c4822019-10-16 11:02:41 -07001098 return &tp_pb.SchedulerConfig{
1099 Direction: dir,
1100 AdditionalBw: bw,
1101 Priority: tpInstance.UsScheduler.Priority,
1102 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001103 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001104}
1105
Neha Sharma94f16a92020-06-26 04:17:55 +00001106func (t *TechProfileMgr) GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -07001107
Neha Sharma94f16a92020-06-26 04:17:55 +00001108 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.DsScheduler.Direction))
Scott Baker2c1c4822019-10-16 11:02:41 -07001109 if dir == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001110 logger.Errorf(ctx, "Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001111 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 -07001112 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001113
Neha Sharma94f16a92020-06-26 04:17:55 +00001114 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.DsScheduler.AdditionalBw))
Scott Baker2c1c4822019-10-16 11:02:41 -07001115 if bw == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001116 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001117 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 -07001118 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001119
Neha Sharma94f16a92020-06-26 04:17:55 +00001120 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Scott Baker2c1c4822019-10-16 11:02:41 -07001121 if policy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001122 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 +00001123 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 -07001124 }
1125
1126 return &tp_pb.SchedulerConfig{
1127 Direction: dir,
1128 AdditionalBw: bw,
1129 Priority: tpInstance.DsScheduler.Priority,
1130 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001131 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001132}
1133
1134func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
1135 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
1136
1137 tSched := &tp_pb.TrafficScheduler{
1138 Direction: SchedCfg.Direction,
1139 AllocId: tpInstance.UsScheduler.AllocID,
1140 TrafficShapingInfo: ShapingCfg,
1141 Scheduler: SchedCfg}
1142
1143 return tSched
1144}
1145
Neha Sharma94f16a92020-06-26 04:17:55 +00001146func (tpm *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -07001147
1148 var encryp bool
1149 if Dir == tp_pb.Direction_UPSTREAM {
1150 // upstream GEM ports
1151 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
1152 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1153 for Count := 0; Count < NumGemPorts; Count++ {
1154 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
1155 encryp = true
1156 } else {
1157 encryp = false
1158 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001159
Neha Sharma94f16a92020-06-26 04:17:55 +00001160 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001161 if schedPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001162 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 +00001163 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1164 }
1165
Neha Sharma94f16a92020-06-26 04:17:55 +00001166 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001167 if discardPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001168 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 +00001169 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1170 }
1171
Scott Baker2c1c4822019-10-16 11:02:41 -07001172 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
Neha Sharma94f16a92020-06-26 04:17:55 +00001173 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.UsScheduler.Direction)),
Scott Baker2c1c4822019-10-16 11:02:41 -07001174 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
1175 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
1176 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001177 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001178 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
1179 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001180 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001181 })
1182 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001183 logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +00001184 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001185 } else if Dir == tp_pb.Direction_DOWNSTREAM {
1186 //downstream GEM ports
1187 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1188 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1189 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +00001190 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1191 //do not take multicast GEM ports. They are handled separately.
1192 continue
1193 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001194 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1195 encryp = true
1196 } else {
1197 encryp = false
1198 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001199
Neha Sharma94f16a92020-06-26 04:17:55 +00001200 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001201 if schedPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001202 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 +00001203 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1204 }
1205
Neha Sharma94f16a92020-06-26 04:17:55 +00001206 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001207 if discardPolicy == -1 {
Neha Sharma94f16a92020-06-26 04:17:55 +00001208 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 +00001209 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1210 }
1211
Scott Baker2c1c4822019-10-16 11:02:41 -07001212 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
Neha Sharma94f16a92020-06-26 04:17:55 +00001213 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
Scott Baker2c1c4822019-10-16 11:02:41 -07001214 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
1215 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1216 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001217 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001218 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1219 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +00001220 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -07001221 })
1222 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001223 logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +00001224 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -07001225 }
Girish Kumarb3c52d52019-12-06 12:14:14 +00001226
Neha Sharma94f16a92020-06-26 04:17:55 +00001227 logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumarb3c52d52019-12-06 12:14:14 +00001228 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -07001229}
1230
Esin Karaman8aa75a72019-12-20 13:11:59 +00001231//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
1232func isMulticastGem(isMulticastAttrValue string) bool {
1233 return isMulticastAttrValue != "" &&
1234 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1235}
1236
Neha Sharma94f16a92020-06-26 04:17:55 +00001237func (tpm *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue {
Esin Karaman8aa75a72019-12-20 13:11:59 +00001238 var encryp bool
1239 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1240 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
1241 for Count := 0; Count < NumGemPorts; Count++ {
1242 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1243 continue
1244 }
1245 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1246 encryp = true
1247 } else {
1248 encryp = false
1249 }
1250 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
Neha Sharma94f16a92020-06-26 04:17:55 +00001251 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
Esin Karaman8aa75a72019-12-20 13:11:59 +00001252 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
1253 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1254 AesEncryption: encryp,
Neha Sharma94f16a92020-06-26 04:17:55 +00001255 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
Esin Karaman8aa75a72019-12-20 13:11:59 +00001256 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1257 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Neha Sharma94f16a92020-06-26 04:17:55 +00001258 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
Esin Karaman8aa75a72019-12-20 13:11:59 +00001259 })
1260 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001261 logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Esin Karaman8aa75a72019-12-20 13:11:59 +00001262 return mcastTrafficQueues
1263}
1264
Neha Sharma94f16a92020-06-26 04:17:55 +00001265func (tpm *TechProfileMgr) GetUsTrafficScheduler(ctx context.Context, tp *TechProfile) *tp_pb.TrafficScheduler {
1266 UsScheduler, _ := tpm.GetUsScheduler(ctx, tp)
Scott Baker2c1c4822019-10-16 11:02:41 -07001267
1268 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
1269 AllocId: tp.UsScheduler.AllocID,
1270 Scheduler: UsScheduler}
1271}
1272
Neha Sharma94f16a92020-06-26 04:17:55 +00001273func (t *TechProfileMgr) GetGemportIDForPbit(ctx context.Context, tp interface{}, dir tp_pb.Direction, pbit uint32) uint32 {
Scott Baker2c1c4822019-10-16 11:02:41 -07001274 /*
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001275 Function to get the Gemport ID mapped to a pbit.
Scott Baker2c1c4822019-10-16 11:02:41 -07001276 */
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001277 switch tp := tp.(type) {
1278 case *TechProfile:
1279 if dir == tp_pb.Direction_UPSTREAM {
1280 // upstream GEM ports
1281 numGemPorts := len(tp.UpstreamGemPortAttributeList)
1282 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1283 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
1284 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1285 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1286 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1287 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1288 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001289 logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001290 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
1291 }
1292 }
1293 }
1294 }
1295 } else if dir == tp_pb.Direction_DOWNSTREAM {
1296 //downstream GEM ports
1297 numGemPorts := len(tp.DownstreamGemPortAttributeList)
1298 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1299 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
1300 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1301 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1302 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1303 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1304 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001305 logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001306 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
1307 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001308 }
1309 }
1310 }
1311 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001312 logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001313 case *EponProfile:
1314 if dir == tp_pb.Direction_UPSTREAM {
1315 // upstream GEM ports
1316 numGemPorts := len(tp.UpstreamQueueAttributeList)
1317 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1318 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
1319 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1320 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1321 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1322 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1323 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001324 logger.Debugw(ctx, "Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001325 return tp.UpstreamQueueAttributeList[gemCnt].GemportID
1326 }
1327 }
1328 }
1329 }
1330 } else if dir == tp_pb.Direction_DOWNSTREAM {
1331 //downstream GEM ports
1332 numGemPorts := len(tp.DownstreamQueueAttributeList)
1333 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1334 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
1335 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1336 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1337 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1338 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1339 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma94f16a92020-06-26 04:17:55 +00001340 logger.Debugw(ctx, "Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportID})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001341 return tp.DownstreamQueueAttributeList[gemCnt].GemportID
1342 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001343 }
1344 }
1345 }
1346 }
Neha Sharma94f16a92020-06-26 04:17:55 +00001347 logger.Errorw(ctx, "No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001348 default:
Neha Sharma94f16a92020-06-26 04:17:55 +00001349 logger.Errorw(ctx, "unknown-tech", log.Fields{"tp": tp})
Scott Baker2c1c4822019-10-16 11:02:41 -07001350 }
Scott Baker2c1c4822019-10-16 11:02:41 -07001351 return 0
1352}
Girish Gowdra9447baf2019-11-05 16:42:37 +05301353
1354// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001355func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) interface{} {
1356 var tpTech TechProfile
1357 var tpEpon EponProfile
1358
Girish Gowdra6172a2e2019-11-27 14:37:58 +05301359 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +05301360
npujar5bf737f2020-01-16 19:35:25 +05301361 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001362 tech := t.resourceMgr.GetTechnology()
1363 tpInstancesTech := make([]TechProfile, 0, len(kvPairs))
1364 tpInstancesEpon := make([]EponProfile, 0, len(kvPairs))
1365
Girish Gowdra9447baf2019-11-05 16:42:37 +05301366 for kvPath, kvPair := range kvPairs {
1367 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
Andrea Campanella904fe322020-06-26 18:56:51 +02001368 if tech == xgspon || tech == gpon {
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001369 if err = json.Unmarshal(value, &tpTech); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +00001370 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001371 continue
1372 } else {
1373 tpInstancesTech = append(tpInstancesTech, tpTech)
1374 }
1375 } else if tech == epon {
1376 if err = json.Unmarshal(value, &tpEpon); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +00001377 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001378 continue
1379 } else {
1380 tpInstancesEpon = append(tpInstancesEpon, tpEpon)
1381 }
Girish Gowdra9447baf2019-11-05 16:42:37 +05301382 }
1383 }
1384 }
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001385
1386 switch tech {
Andrea Campanella904fe322020-06-26 18:56:51 +02001387 case xgspon, gpon:
Takahiro Suzuki98cd2b92020-03-13 14:50:08 -07001388 return tpInstancesTech
1389 case epon:
1390 return tpInstancesEpon
1391 default:
1392 log.Errorw("unknown-technology", log.Fields{"tech": tech})
1393 return nil
1394 }
Girish Gowdra9447baf2019-11-05 16:42:37 +05301395 }
1396 return nil
1397}