blob: ff3732678d9db8d7c60d39aedfa1cfc7479a119f [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001/*
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 (
npujarec5762e2020-01-01 14:08:48 +053020 "context"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040021 "encoding/json"
22 "errors"
23 "fmt"
Girish Gowdra54934262019-11-13 14:19:55 +053024 "regexp"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040025 "strconv"
Girish Gowdra631ef3d2020-06-15 10:45:52 -070026 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000027 "time"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040028
Esin Karamanccb714b2019-11-29 15:02:06 +000029 "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"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040034)
35
36// Interface to pon resource manager APIs
37type iPonResourceMgr interface {
npujarec5762e2020-01-01 14:08:48 +053038 GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
Matt Jeanneretcab955f2019-04-10 15:45:57 -040039 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 Gowdra54934262019-11-13 14:19:55 +0530104// Required uniPortName format
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700105var uniPortNameFormat = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
Girish Gowdra54934262019-11-13 14:19:55 +0530106
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400107/*
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700108 type InferredAdditionBWIndication int32
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400109
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700110 const (
111 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
112 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
113 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
114 )
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400115
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700116 var InferredAdditionBWIndication_name = map[int32]string{
117 0: "InferredAdditionBWIndication_None",
118 1: "InferredAdditionBWIndication_Assured",
119 2: "InferredAdditionBWIndication_BestEffort",
120 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400121*/
122// instance control defaults
123const (
124 defaultOnuInstance = "multi-instance"
125 defaultUniInstance = "single-instance"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400126 defaultGemPayloadSize = "auto"
127)
128
129const MAX_GEM_PAYLOAD = "max_gem_payload_size"
130
131type InstanceControl struct {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400132 Onu string `json:"ONU"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400133 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 (
kdarapub26b4502019-10-05 03:02:33 +0530152 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400153 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 (
Scott Bakeree7c0a02020-01-07 11:12:26 -0800168 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
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400177)
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"`
Manikkaraj kb1d51442019-07-23 10:41:02 -0400184 PriorityQueue uint32 `json:"priority_q"`
185 Weight uint32 `json:"weight"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400186 DiscardPolicy string `json:"discard_policy"`
187 DiscardConfig DiscardConfig `json:"discard_config"`
Scott Bakeree7c0a02020-01-07 11:12:26 -0800188 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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400192}
193
194type iScheduler struct {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400195 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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400201}
202type iGemPortAttribute struct {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400203 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"`
Manikkaraj kb1d51442019-07-23 10:41:02 -0400208 PriorityQueue uint32 `json:"priority_q"`
209 Weight uint32 `json:"weight"`
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400210 DiscardPolicy string `json:"discard_policy"`
211 DiscardConfig DiscardConfig `json:"discard_config"`
Scott Bakeree7c0a02020-01-07 11:12:26 -0800212 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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400216}
217
218type TechProfileMgr struct {
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700219 config *TechProfileFlags
220 resourceMgr iPonResourceMgr
221 GemPortIDMgmtLock sync.RWMutex
222 AllocIDMgmtLock sync.RWMutex
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400223}
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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400241 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 Suzuki2ba0e0b2020-06-05 14:23:03 -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 Campanella974d7452020-06-26 19:32:30 +0200377 xgspon = "XGS-PON"
378 gpon = "GPON"
379 epon = "EPON"
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700380)
381
sbarbaria8910ba2019-11-05 10:12:23 -0500382func (t *TechProfileMgr) SetKVClient() *db.Backend {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000383 kvClient, err := newKVClient(t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400384 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700385 logger.Errorw("failed-to-create-kv-client",
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400386 log.Fields{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000387 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400388 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
389 "error": err.Error(),
390 })
391 return nil
392 }
sbarbaria8910ba2019-11-05 10:12:23 -0500393 return &db.Backend{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400394 Client: kvClient,
395 StoreType: t.config.KVStoreType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000396 Address: t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400397 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 Suzuki2ba0e0b2020-06-05 14:23:03 -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)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400404 */
405}
406
Neha Sharmacc656962020-04-14 14:26:11 +0000407func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400408
Scott Baker24f83e22020-03-30 16:14:28 -0700409 logger.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400410 switch storeType {
411 case "consul":
412 return kvstore.NewConsulClient(address, timeout)
413 case "etcd":
Scott Bakered4a8e72020-04-17 11:10:20 -0700414 return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400415 }
416 return nil, errors.New("unsupported-kv-store")
417}
418
Neha Sharma3f221ae2020-04-29 19:02:12 +0000419func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400420 var techprofileObj TechProfileMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700421 logger.Debug("Initializing techprofile Manager")
Neha Sharma3f221ae2020-04-29 19:02:12 +0000422 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400423 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
424 if techprofileObj.config.KVBackend == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700425 logger.Error("Failed to initialize KV backend\n")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400426 return nil, errors.New("KV backend init failed")
427 }
428 techprofileObj.resourceMgr = resourceMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700429 logger.Debug("Initializing techprofile object instance success")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400430 return &techprofileObj, nil
431}
432
433func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700434 logger.Debugw("get-tp-instance-kv-path", log.Fields{
435 "uniPortName": uniPortName,
436 "tpId": techProfiletblID,
437 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400438 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
439}
440
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700441func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (interface{}, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400442 var err error
Girish Gowdra54934262019-11-13 14:19:55 +0530443 var kvResult *kvstore.KVPair
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -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 Campanella974d7452020-06-26 19:32:30 +0200452 case xgspon, gpon:
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -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 Scandolod625b4c2020-04-02 16:16:01 -0700460
npujarec5762e2020-01-01 14:08:48 +0530461 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra54934262019-11-13 14:19:55 +0530462 if kvResult == nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700463 log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra54934262019-11-13 14:19:55 +0530464 return nil, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400465 } else {
Girish Gowdra54934262019-11-13 14:19:55 +0530466 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400467 if err = json.Unmarshal(value, resPtr); err != nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700468 log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530469 return nil, errors.New("error-unmarshal-kv-result")
470 } else {
471 return resPtr, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400472 }
473 }
474 }
Girish Gowdra54934262019-11-13 14:19:55 +0530475 return nil, err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400476}
477
npujarec5762e2020-01-01 14:08:48 +0530478func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400479 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Scott Baker24f83e22020-03-30 16:14:28 -0700480 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400481 tpInstanceJson, err := json.Marshal(*tpInstance)
482 if err == nil {
483 // Backend will convert JSON byte array into string format
Scott Baker24f83e22020-03-30 16:14:28 -0700484 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujarec5762e2020-01-01 14:08:48 +0530485 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400486 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700487 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400488 }
489 return err
490}
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700491
492func (t *TechProfileMgr) addEponProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *EponProfile) error {
493 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
494 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
495 tpInstanceJson, err := json.Marshal(*tpInstance)
496 if err == nil {
497 // Backend will convert JSON byte array into string format
498 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
499 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
500 } else {
501 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
502 }
503 return err
504}
505
npujarec5762e2020-01-01 14:08:48 +0530506func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400507 var kvtechprofile DefaultTechProfile
508 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Scott Baker24f83e22020-03-30 16:14:28 -0700509 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujarec5762e2020-01-01 14:08:48 +0530510 kvresult, err := t.config.KVBackend.Get(ctx, key)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400511 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700512 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400513 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 Kumar8f73fe02019-12-09 13:19:37 +0000518 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700519 logger.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000520 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400521 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000522
Scott Baker24f83e22020-03-30 16:14:28 -0700523 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000524 return &kvtechprofile
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400525 }
526 }
527 return nil
528}
Girish Kumar8f73fe02019-12-09 13:19:37 +0000529
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -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)
533 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
534 kvresult, err := t.config.KVBackend.Get(ctx, key)
535 if err != nil {
536 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
537 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 {
543 logger.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
544 return nil
545 }
546
547 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
548 return &kvtechprofile
549 }
550 }
551 return nil
552}
553
554func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (interface{}, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400555 var tpInstance *TechProfile
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700556 var tpEponInstance *EponProfile
557
Scott Baker24f83e22020-03-30 16:14:28 -0700558 logger.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +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)) {
Scott Baker24f83e22020-03-30 16:14:28 -0700562 logger.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000563 return nil, errors.New("uni-port-name-not-confirming-to-format")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400564 }
Girish Gowdra54934262019-11-13 14:19:55 +0530565 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -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 {
573 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
574 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
575 tp = t.getDefaultEponProfile()
576 } else {
577 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
578 }
579 } else {
580 logger.Info("tp-not-found-on-kv--creating-default-tp")
581 tp = t.getDefaultEponProfile()
582 }
583
584 if tpEponInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpEponInstance == nil {
585 logger.Error("tp-intance-allocation-failed")
586 return nil, errors.New("tp-intance-allocation-failed")
587 }
588 if err := t.addEponProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpEponInstance); err != nil {
589 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
590 return nil, errors.New("error-adding-tp-to-kv-store")
591 }
592 logger.Infow("tp-added-to-kv-store-successfully",
593 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
594 return tpEponInstance, nil
595 } else {
596 tp := t.getTPFromKVStore(ctx, techProfiletblID)
597 if tp != nil {
598 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
599 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
600 tp = t.getDefaultTechProfile()
601 } else {
602 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
603 }
604 } else {
605 logger.Info("tp-not-found-on-kv--creating-default-tp")
606 tp = t.getDefaultTechProfile()
607 }
608
609 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
610 logger.Error("tp-intance-allocation-failed")
611 return nil, errors.New("tp-intance-allocation-failed")
612 }
613 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
614 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
615 return nil, errors.New("error-adding-tp-to-kv-store")
616 }
617 logger.Infow("tp-added-to-kv-store-successfully",
618 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
619 return tpInstance, nil
Girish Gowdra54934262019-11-13 14:19:55 +0530620 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400621}
622
npujarec5762e2020-01-01 14:08:48 +0530623func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400624 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujarec5762e2020-01-01 14:08:48 +0530625 return t.config.KVBackend.Delete(ctx, path)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400626}
627
Girish Gowdra54934262019-11-13 14:19:55 +0530628func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
629 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700630 logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra54934262019-11-13 14:19:55 +0530631 return errors.New("invalid-onu-instance-ctl-attr")
632 }
633
634 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700635 logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra54934262019-11-13 14:19:55 +0530636 return errors.New("invalid-uni-instance-ctl-attr")
637 }
638
639 if instCtl.Uni == "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700640 logger.Error("uni-multi-instance-tp-not-supported")
Girish Gowdra54934262019-11-13 14:19:55 +0530641 return errors.New("uni-multi-instance-tp-not-supported")
642 }
643
644 return nil
645}
646
npujarec5762e2020-01-01 14:08:48 +0530647func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400648
649 var usGemPortAttributeList []iGemPortAttribute
650 var dsGemPortAttributeList []iGemPortAttribute
Scott Bakeree7c0a02020-01-07 11:12:26 -0800651 var dsMulticastGemAttributeList []iGemPortAttribute
652 var dsUnicastGemAttributeList []iGemPortAttribute
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400653 var tcontIDs []uint32
654 var gemPorts []uint32
655 var err error
656
Scott Baker24f83e22020-03-30 16:14:28 -0700657 logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra54934262019-11-13 14:19:55 +0530658
659 if tp.InstanceCtrl.Onu == "multi-instance" {
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700660 t.AllocIDMgmtLock.Lock()
661 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
662 t.AllocIDMgmtLock.Unlock()
663 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700664 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530665 return nil
666 }
667 } else { // "single-instance"
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000668 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700669 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000670 return nil
671 } else if tpInst == nil {
Girish Gowdra54934262019-11-13 14:19:55 +0530672 // No "single-instance" tp found on one any uni port for the given TP ID
673 // Allocate a new TcontID or AllocID
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700674 t.AllocIDMgmtLock.Lock()
675 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
676 t.AllocIDMgmtLock.Unlock()
677 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700678 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530679 return nil
680 }
681 } else {
682 // Use the alloc-id from the existing TpInstance
683 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
684 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400685 }
Scott Baker24f83e22020-03-30 16:14:28 -0700686 logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700687 t.GemPortIDMgmtLock.Lock()
688 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
689 t.GemPortIDMgmtLock.Unlock()
690 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700691 logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400692 return nil
693 }
Scott Baker24f83e22020-03-30 16:14:28 -0700694 logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400695 for index := 0; index < int(tp.NumGemPorts); index++ {
696 usGemPortAttributeList = append(usGemPortAttributeList,
697 iGemPortAttribute{GemportID: gemPorts[index],
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400698 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})
Scott Bakeree7c0a02020-01-07 11:12:26 -0800706 }
707
Scott Baker24f83e22020-03-30 16:14:28 -0700708 logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Scott Bakeree7c0a02020-01-07 11:12:26 -0800709 //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++ {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400741 dsGemPortAttributeList = append(dsGemPortAttributeList,
742 iGemPortAttribute{GemportID: gemPorts[index],
Scott Bakeree7c0a02020-01-07 11:12:26 -0800743 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})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400751 }
Scott Bakeree7c0a02020-01-07 11:12:26 -0800752 //add multicast GEM ports to dsGemPortAttributeList afterwards
753 for k := range dsMulticastGemAttributeList {
754 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
755 }
756
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400757 return &TechProfile{
758 SubscriberIdentifier: uniPortName,
759 Name: tp.Name,
760 ProfileType: tp.ProfileType,
761 Version: tp.Version,
762 NumGemPorts: tp.NumGemPorts,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400763 InstanceCtrl: tp.InstanceCtrl,
764 UsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400765 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},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400771 DsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400772 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},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400778 UpstreamGemPortAttributeList: usGemPortAttributeList,
779 DownstreamGemPortAttributeList: dsGemPortAttributeList}
780}
781
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -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 Gowdra54934262019-11-13 14:19:55 +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.
npujarec5762e2020-01-01 14:08:48 +0530869func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra54934262019-11-13 14:19:55 +0530870 var tpInst TechProfile
871
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)
npujarec5762e2020-01-01 14:08:48 +0530876 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra54934262019-11-13 14:19:55 +0530877
878 // 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 {
Scott Baker24f83e22020-03-30 16:14:28 -0700882 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530883 return nil, errors.New("error-unmarshal-kv-pair")
884 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700885 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra54934262019-11-13 14:19:55 +0530886 return &tpInst, nil
887 }
888 }
889 }
890 return nil, nil
891}
892
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -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 {
906 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
907 return nil, errors.New("error-unmarshal-kv-pair")
908 } else {
909 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
910 return &tpInst, nil
911 }
912 }
913 }
914 return nil, nil
915}
916
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400917func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
918
919 var usGemPortAttributeList []GemPortAttribute
920 var dsGemPortAttributeList []GemPortAttribute
921
922 for _, pbit := range t.config.DefaultPbits {
Scott Baker24f83e22020-03-30 16:14:28 -0700923 logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400924 usGemPortAttributeList = append(usGemPortAttributeList,
925 GemPortAttribute{
926 MaxQueueSize: defaultMaxQueueSize,
927 PbitMap: pbit,
928 AesEncryption: defaultAESEncryption,
929 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
930 PriorityQueue: defaultPriorityQueue,
931 Weight: defaultQueueWeight,
932 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
933 DiscardConfig: DiscardConfig{
934 MinThreshold: defaultMinThreshold,
935 MaxThreshold: defaultMaxThreshold,
936 MaxProbability: defaultMaxProbability}})
937 dsGemPortAttributeList = append(dsGemPortAttributeList,
938 GemPortAttribute{
939 MaxQueueSize: defaultMaxQueueSize,
940 PbitMap: pbit,
941 AesEncryption: defaultAESEncryption,
942 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
943 PriorityQueue: defaultPriorityQueue,
944 Weight: defaultQueueWeight,
945 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
946 DiscardConfig: DiscardConfig{
947 MinThreshold: defaultMinThreshold,
948 MaxThreshold: defaultMaxThreshold,
Scott Bakeree7c0a02020-01-07 11:12:26 -0800949 MaxProbability: defaultMaxProbability},
950 IsMulticast: defaultIsMulticast,
951 DControlList: defaultAccessControlList,
952 SControlList: defaultAccessControlList,
953 McastGemID: defaultMcastGemID})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400954 }
955 return &DefaultTechProfile{
956 Name: t.config.DefaultTPName,
957 ProfileType: t.resourceMgr.GetTechnology(),
958 Version: t.config.TPVersion,
959 NumGemPorts: uint32(len(usGemPortAttributeList)),
960 InstanceCtrl: InstanceControl{
961 Onu: defaultOnuInstance,
962 Uni: defaultUniInstance,
963 MaxGemPayloadSize: defaultGemPayloadSize},
964 UsScheduler: Scheduler{
965 Direction: Direction_name[Direction_UPSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530966 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400967 Priority: defaultPriority,
968 Weight: defaultWeight,
969 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
970 DsScheduler: Scheduler{
971 Direction: Direction_name[Direction_DOWNSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530972 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400973 Priority: defaultPriority,
974 Weight: defaultWeight,
975 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
976 UpstreamGemPortAttributeList: usGemPortAttributeList,
977 DownstreamGemPortAttributeList: dsGemPortAttributeList}
978}
979
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700980// getDefaultTechProfile function for EPON
981func (t *TechProfileMgr) getDefaultEponProfile() *DefaultEponProfile {
982
983 var usQueueAttributeList []UpstreamQueueAttribute
984 var dsQueueAttributeList []DownstreamQueueAttribute
985
986 for _, pbit := range t.config.DefaultPbits {
987 log.Debugw("Creating Queue", log.Fields{"pbit": pbit})
988 usQueueAttributeList = append(usQueueAttributeList,
989 UpstreamQueueAttribute{
990 MaxQueueSize: defaultMaxQueueSize,
991 PbitMap: pbit,
992 AesEncryption: defaultAESEncryption,
993 TrafficType: defaultTrafficType,
994 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
995 NominalInterval: defaultNominalInterval,
996 ToleratedPollJitter: defaultToleratedPollJitter,
997 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
998 NumQueueSet: defaultNumQueueSet,
999 QThresholds: QThresholds{
1000 QThreshold1: defaultQThreshold1,
1001 QThreshold2: defaultQThreshold2,
1002 QThreshold3: defaultQThreshold3,
1003 QThreshold4: defaultQThreshold4,
1004 QThreshold5: defaultQThreshold5,
1005 QThreshold6: defaultQThreshold6,
1006 QThreshold7: defaultQThreshold7},
1007 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1008 PriorityQueue: defaultPriorityQueue,
1009 Weight: defaultQueueWeight,
1010 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1011 DiscardConfig: DiscardConfig{
1012 MinThreshold: defaultMinThreshold,
1013 MaxThreshold: defaultMaxThreshold,
1014 MaxProbability: defaultMaxProbability}})
1015 dsQueueAttributeList = append(dsQueueAttributeList,
1016 DownstreamQueueAttribute{
1017 MaxQueueSize: defaultMaxQueueSize,
1018 PbitMap: pbit,
1019 AesEncryption: defaultAESEncryption,
1020 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1021 PriorityQueue: defaultPriorityQueue,
1022 Weight: defaultQueueWeight,
1023 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1024 DiscardConfig: DiscardConfig{
1025 MinThreshold: defaultMinThreshold,
1026 MaxThreshold: defaultMaxThreshold,
1027 MaxProbability: defaultMaxProbability}})
1028 }
1029 return &DefaultEponProfile{
1030 Name: t.config.DefaultTPName,
1031 ProfileType: t.resourceMgr.GetTechnology(),
1032 Version: t.config.TPVersion,
1033 NumGemPorts: uint32(len(usQueueAttributeList)),
1034 InstanceCtrl: InstanceControl{
1035 Onu: defaultOnuInstance,
1036 Uni: defaultUniInstance,
1037 MaxGemPayloadSize: defaultGemPayloadSize},
1038 EponAttribute: EponAttribute{
1039 PackageType: defaultPakageType},
1040 UpstreamQueueAttributeList: usQueueAttributeList,
1041 DownstreamQueueAttributeList: dsQueueAttributeList}
1042}
1043
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001044func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
1045 var result int32 = -1
1046
1047 if paramType == "direction" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001048 for key, val := range tp_pb.Direction_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001049 if key == paramKey {
1050 result = val
1051 }
1052 }
1053 } else if paramType == "discard_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001054 for key, val := range tp_pb.DiscardPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001055 if key == paramKey {
1056 result = val
1057 }
1058 }
1059 } else if paramType == "sched_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001060 for key, val := range tp_pb.SchedulingPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001061 if key == paramKey {
Scott Baker24f83e22020-03-30 16:14:28 -07001062 logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001063 result = val
1064 }
1065 }
1066 } else if paramType == "additional_bw" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001067 for key, val := range tp_pb.AdditionalBW_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001068 if key == paramKey {
1069 result = val
1070 }
1071 }
1072 } else {
Scott Baker24f83e22020-03-30 16:14:28 -07001073 logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001074 return -1
1075 }
Scott Baker24f83e22020-03-30 16:14:28 -07001076 logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001077 return result
1078}
1079
Girish Kumar8f73fe02019-12-09 13:19:37 +00001080func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001081 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001082 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001083 logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001084 return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001085 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001086
Manikkaraj kb1d51442019-07-23 10:41:02 -04001087 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001088 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001089 logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001090 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001091 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001092
Manikkaraj kb1d51442019-07-23 10:41:02 -04001093 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001094 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001095 logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001096 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001097 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001098
Manikkaraj kb1d51442019-07-23 10:41:02 -04001099 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001100 Direction: dir,
1101 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001102 Priority: tpInstance.UsScheduler.Priority,
1103 Weight: tpInstance.UsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001104 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001105}
1106
Girish Kumar8f73fe02019-12-09 13:19:37 +00001107func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001108
Manikkaraj kb1d51442019-07-23 10:41:02 -04001109 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001110 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001111 logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001112 return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001113 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001114
Manikkaraj kb1d51442019-07-23 10:41:02 -04001115 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001116 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001117 logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001118 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001119 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001120
Manikkaraj kb1d51442019-07-23 10:41:02 -04001121 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001122 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001123 logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001124 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001125 }
1126
Manikkaraj kb1d51442019-07-23 10:41:02 -04001127 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001128 Direction: dir,
1129 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001130 Priority: tpInstance.DsScheduler.Priority,
1131 Weight: tpInstance.DsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001132 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001133}
1134
Manikkaraj kb1d51442019-07-23 10:41:02 -04001135func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
1136 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
1137
1138 tSched := &tp_pb.TrafficScheduler{
1139 Direction: SchedCfg.Direction,
1140 AllocId: tpInstance.UsScheduler.AllocID,
1141 TrafficShapingInfo: ShapingCfg,
1142 Scheduler: SchedCfg}
1143
1144 return tSched
1145}
1146
Girish Kumar8f73fe02019-12-09 13:19:37 +00001147func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001148
1149 var encryp bool
1150 if Dir == tp_pb.Direction_UPSTREAM {
1151 // upstream GEM ports
1152 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
1153 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1154 for Count := 0; Count < NumGemPorts; Count++ {
1155 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
1156 encryp = true
1157 } else {
1158 encryp = false
1159 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001160
1161 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1162 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001163 logger.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001164 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1165 }
1166
1167 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1168 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001169 logger.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001170 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1171 }
1172
Manikkaraj kb1d51442019-07-23 10:41:02 -04001173 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
1174 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
1175 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
1176 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
1177 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001178 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001179 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
1180 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001181 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001182 })
1183 }
Scott Baker24f83e22020-03-30 16:14:28 -07001184 logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +00001185 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -04001186 } else if Dir == tp_pb.Direction_DOWNSTREAM {
1187 //downstream GEM ports
1188 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1189 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1190 for Count := 0; Count < NumGemPorts; Count++ {
Scott Bakeree7c0a02020-01-07 11:12:26 -08001191 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1192 //do not take multicast GEM ports. They are handled separately.
1193 continue
1194 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001195 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1196 encryp = true
1197 } else {
1198 encryp = false
1199 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001200
1201 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1202 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001203 logger.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001204 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1205 }
1206
1207 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1208 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001209 logger.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001210 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1211 }
1212
Manikkaraj kb1d51442019-07-23 10:41:02 -04001213 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
1214 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
1215 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
1216 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1217 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001218 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001219 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1220 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001221 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001222 })
1223 }
Scott Baker24f83e22020-03-30 16:14:28 -07001224 logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +00001225 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -04001226 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001227
Scott Baker24f83e22020-03-30 16:14:28 -07001228 logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001229 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001230}
1231
Scott Bakeree7c0a02020-01-07 11:12:26 -08001232//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
1233func isMulticastGem(isMulticastAttrValue string) bool {
1234 return isMulticastAttrValue != "" &&
1235 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1236}
1237
1238func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
1239 var encryp bool
1240 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1241 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
1242 for Count := 0; Count < NumGemPorts; Count++ {
1243 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1244 continue
1245 }
1246 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1247 encryp = true
1248 } else {
1249 encryp = false
1250 }
1251 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
1252 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
1253 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
1254 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1255 AesEncryption: encryp,
1256 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
1257 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1258 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
1259 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
1260 })
1261 }
Scott Baker24f83e22020-03-30 16:14:28 -07001262 logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Scott Bakeree7c0a02020-01-07 11:12:26 -08001263 return mcastTrafficQueues
1264}
1265
Manikkaraj kb1d51442019-07-23 10:41:02 -04001266func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumar8f73fe02019-12-09 13:19:37 +00001267 UsScheduler, _ := tpm.GetUsScheduler(tp)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001268
1269 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
1270 AllocId: tp.UsScheduler.AllocID,
1271 Scheduler: UsScheduler}
1272}
1273
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001274func (t *TechProfileMgr) GetGemportIDForPbit(tp interface{}, dir tp_pb.Direction, pbit uint32) uint32 {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001275 /*
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001276 Function to get the Gemport ID mapped to a pbit.
Manikkaraj kb1d51442019-07-23 10:41:02 -04001277 */
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001278 switch tp := tp.(type) {
1279 case *TechProfile:
1280 if dir == tp_pb.Direction_UPSTREAM {
1281 // upstream GEM ports
1282 numGemPorts := len(tp.UpstreamGemPortAttributeList)
1283 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1284 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
1285 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1286 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1287 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1288 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1289 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1290 logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
1291 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
1292 }
1293 }
1294 }
1295 }
1296 } else if dir == tp_pb.Direction_DOWNSTREAM {
1297 //downstream GEM ports
1298 numGemPorts := len(tp.DownstreamGemPortAttributeList)
1299 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1300 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
1301 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1302 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1303 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1304 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1305 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1306 logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
1307 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
1308 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001309 }
1310 }
1311 }
1312 }
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001313 logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
1314 case *EponProfile:
1315 if dir == tp_pb.Direction_UPSTREAM {
1316 // upstream GEM ports
1317 numGemPorts := len(tp.UpstreamQueueAttributeList)
1318 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1319 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
1320 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1321 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1322 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1323 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1324 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1325 logger.Debugw("Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportID})
1326 return tp.UpstreamQueueAttributeList[gemCnt].GemportID
1327 }
1328 }
1329 }
1330 }
1331 } else if dir == tp_pb.Direction_DOWNSTREAM {
1332 //downstream GEM ports
1333 numGemPorts := len(tp.DownstreamQueueAttributeList)
1334 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1335 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
1336 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1337 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1338 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1339 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1340 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1341 logger.Debugw("Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportID})
1342 return tp.DownstreamQueueAttributeList[gemCnt].GemportID
1343 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001344 }
1345 }
1346 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001347 }
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001348 logger.Errorw("No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
1349 default:
1350 logger.Errorw("unknown-tech", log.Fields{"tp": tp})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001351 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001352 return 0
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001353}
Girish Gowdra54934262019-11-13 14:19:55 +05301354
1355// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001356func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) interface{} {
1357 var tpTech TechProfile
1358 var tpEpon EponProfile
1359
Girish Gowdra6b130582019-11-20 16:45:20 +05301360 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra54934262019-11-13 14:19:55 +05301361
npujarec5762e2020-01-01 14:08:48 +05301362 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001363 tech := t.resourceMgr.GetTechnology()
1364 tpInstancesTech := make([]TechProfile, 0, len(kvPairs))
1365 tpInstancesEpon := make([]EponProfile, 0, len(kvPairs))
1366
Girish Gowdra54934262019-11-13 14:19:55 +05301367 for kvPath, kvPair := range kvPairs {
1368 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
Andrea Campanella974d7452020-06-26 19:32:30 +02001369 if tech == xgspon || tech == gpon {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001370 if err = json.Unmarshal(value, &tpTech); err != nil {
1371 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
1372 continue
1373 } else {
1374 tpInstancesTech = append(tpInstancesTech, tpTech)
1375 }
1376 } else if tech == epon {
1377 if err = json.Unmarshal(value, &tpEpon); err != nil {
1378 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
1379 continue
1380 } else {
1381 tpInstancesEpon = append(tpInstancesEpon, tpEpon)
1382 }
Girish Gowdra54934262019-11-13 14:19:55 +05301383 }
1384 }
1385 }
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001386
1387 switch tech {
Andrea Campanella974d7452020-06-26 19:32:30 +02001388 case xgspon, gpon:
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001389 return tpInstancesTech
1390 case epon:
1391 return tpInstancesEpon
1392 default:
1393 log.Errorw("unknown-technology", log.Fields{"tech": tech})
1394 return nil
1395 }
Girish Gowdra54934262019-11-13 14:19:55 +05301396 }
1397 return nil
1398}