blob: 492c9e8cf5faf34215b88f88da629468ae01b09f [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 (
377 xgspon = "xgspon"
378 xgsponBbsim = "XGS-PON"
379 gpon = "gpon"
380 epon = "EPON"
381)
382
sbarbaria8910ba2019-11-05 10:12:23 -0500383func (t *TechProfileMgr) SetKVClient() *db.Backend {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000384 kvClient, err := newKVClient(t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400385 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700386 logger.Errorw("failed-to-create-kv-client",
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400387 log.Fields{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000388 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400389 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
390 "error": err.Error(),
391 })
392 return nil
393 }
sbarbaria8910ba2019-11-05 10:12:23 -0500394 return &db.Backend{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400395 Client: kvClient,
396 StoreType: t.config.KVStoreType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000397 Address: t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400398 Timeout: t.config.KVStoreTimeout,
399 PathPrefix: t.config.TPKVPathPrefix}
400
401 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700402 issue between kv store and backend , core is not calling NewBackend directly
403 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
404 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400405 */
406}
407
Neha Sharmacc656962020-04-14 14:26:11 +0000408func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400409
Scott Baker24f83e22020-03-30 16:14:28 -0700410 logger.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400411 switch storeType {
412 case "consul":
413 return kvstore.NewConsulClient(address, timeout)
414 case "etcd":
Scott Bakered4a8e72020-04-17 11:10:20 -0700415 return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400416 }
417 return nil, errors.New("unsupported-kv-store")
418}
419
Neha Sharma3f221ae2020-04-29 19:02:12 +0000420func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400421 var techprofileObj TechProfileMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700422 logger.Debug("Initializing techprofile Manager")
Neha Sharma3f221ae2020-04-29 19:02:12 +0000423 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400424 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
425 if techprofileObj.config.KVBackend == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700426 logger.Error("Failed to initialize KV backend\n")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400427 return nil, errors.New("KV backend init failed")
428 }
429 techprofileObj.resourceMgr = resourceMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700430 logger.Debug("Initializing techprofile object instance success")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400431 return &techprofileObj, nil
432}
433
434func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700435 logger.Debugw("get-tp-instance-kv-path", log.Fields{
436 "uniPortName": uniPortName,
437 "tpId": techProfiletblID,
438 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400439 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
440}
441
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700442func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (interface{}, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400443 var err error
Girish Gowdra54934262019-11-13 14:19:55 +0530444 var kvResult *kvstore.KVPair
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700445 var KvTpIns TechProfile
446 var KvEponIns EponProfile
447 var resPtr interface{}
448 // For example:
449 // tpInstPath like "XGS-PON/64/uni_port_name"
450 // is broken into ["XGS-PON" "64" ...]
451 pathSlice := regexp.MustCompile(`/`).Split(path, -1)
452 switch pathSlice[0] {
453 case xgspon, xgsponBbsim, gpon:
454 resPtr = &KvTpIns
455 case epon:
456 resPtr = &KvEponIns
457 default:
458 log.Errorw("unknown-tech", log.Fields{"tech": pathSlice[0]})
459 return nil, fmt.Errorf("unknown-tech-%s", pathSlice[0])
460 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700461
npujarec5762e2020-01-01 14:08:48 +0530462 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra54934262019-11-13 14:19:55 +0530463 if kvResult == nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700464 log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra54934262019-11-13 14:19:55 +0530465 return nil, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400466 } else {
Girish Gowdra54934262019-11-13 14:19:55 +0530467 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400468 if err = json.Unmarshal(value, resPtr); err != nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700469 log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530470 return nil, errors.New("error-unmarshal-kv-result")
471 } else {
472 return resPtr, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400473 }
474 }
475 }
Girish Gowdra54934262019-11-13 14:19:55 +0530476 return nil, err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400477}
478
npujarec5762e2020-01-01 14:08:48 +0530479func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400480 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Scott Baker24f83e22020-03-30 16:14:28 -0700481 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400482 tpInstanceJson, err := json.Marshal(*tpInstance)
483 if err == nil {
484 // Backend will convert JSON byte array into string format
Scott Baker24f83e22020-03-30 16:14:28 -0700485 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujarec5762e2020-01-01 14:08:48 +0530486 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400487 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700488 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400489 }
490 return err
491}
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700492
493func (t *TechProfileMgr) addEponProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *EponProfile) error {
494 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
495 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
496 tpInstanceJson, err := json.Marshal(*tpInstance)
497 if err == nil {
498 // Backend will convert JSON byte array into string format
499 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
500 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
501 } else {
502 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
503 }
504 return err
505}
506
npujarec5762e2020-01-01 14:08:48 +0530507func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400508 var kvtechprofile DefaultTechProfile
509 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Scott Baker24f83e22020-03-30 16:14:28 -0700510 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujarec5762e2020-01-01 14:08:48 +0530511 kvresult, err := t.config.KVBackend.Get(ctx, key)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400512 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700513 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400514 return nil
515 }
516 if kvresult != nil {
517 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
518 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumar8f73fe02019-12-09 13:19:37 +0000519 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700520 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 +0000521 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400522 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000523
Scott Baker24f83e22020-03-30 16:14:28 -0700524 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000525 return &kvtechprofile
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400526 }
527 }
528 return nil
529}
Girish Kumar8f73fe02019-12-09 13:19:37 +0000530
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700531func (t *TechProfileMgr) getEponTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultEponProfile {
532 var kvtechprofile DefaultEponProfile
533 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
534 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
535 kvresult, err := t.config.KVBackend.Get(ctx, key)
536 if err != nil {
537 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
538 return nil
539 }
540 if kvresult != nil {
541 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
542 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
543 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
544 logger.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
545 return nil
546 }
547
548 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
549 return &kvtechprofile
550 }
551 }
552 return nil
553}
554
555func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (interface{}, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400556 var tpInstance *TechProfile
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700557 var tpEponInstance *EponProfile
558
Scott Baker24f83e22020-03-30 16:14:28 -0700559 logger.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530560
561 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
562 if !uniPortNameFormat.Match([]byte(uniPortName)) {
Scott Baker24f83e22020-03-30 16:14:28 -0700563 logger.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000564 return nil, errors.New("uni-port-name-not-confirming-to-format")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400565 }
Girish Gowdra54934262019-11-13 14:19:55 +0530566 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700567 // For example:
568 // tpInstPath like "XGS-PON/64/uni_port_name"
569 // is broken into ["XGS-PON" "64" ...]
570 pathSlice := regexp.MustCompile(`/`).Split(tpInstancePath, -1)
571 if pathSlice[0] == epon {
572 tp := t.getEponTPFromKVStore(ctx, techProfiletblID)
573 if tp != nil {
574 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
575 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
576 tp = t.getDefaultEponProfile()
577 } else {
578 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
579 }
580 } else {
581 logger.Info("tp-not-found-on-kv--creating-default-tp")
582 tp = t.getDefaultEponProfile()
583 }
584
585 if tpEponInstance = t.allocateEponTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpEponInstance == nil {
586 logger.Error("tp-intance-allocation-failed")
587 return nil, errors.New("tp-intance-allocation-failed")
588 }
589 if err := t.addEponProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpEponInstance); err != nil {
590 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
591 return nil, errors.New("error-adding-tp-to-kv-store")
592 }
593 logger.Infow("tp-added-to-kv-store-successfully",
594 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
595 return tpEponInstance, nil
596 } else {
597 tp := t.getTPFromKVStore(ctx, techProfiletblID)
598 if tp != nil {
599 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
600 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
601 tp = t.getDefaultTechProfile()
602 } else {
603 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
604 }
605 } else {
606 logger.Info("tp-not-found-on-kv--creating-default-tp")
607 tp = t.getDefaultTechProfile()
608 }
609
610 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
611 logger.Error("tp-intance-allocation-failed")
612 return nil, errors.New("tp-intance-allocation-failed")
613 }
614 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
615 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
616 return nil, errors.New("error-adding-tp-to-kv-store")
617 }
618 logger.Infow("tp-added-to-kv-store-successfully",
619 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
620 return tpInstance, nil
Girish Gowdra54934262019-11-13 14:19:55 +0530621 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400622}
623
npujarec5762e2020-01-01 14:08:48 +0530624func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400625 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujarec5762e2020-01-01 14:08:48 +0530626 return t.config.KVBackend.Delete(ctx, path)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400627}
628
Girish Gowdra54934262019-11-13 14:19:55 +0530629func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
630 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700631 logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra54934262019-11-13 14:19:55 +0530632 return errors.New("invalid-onu-instance-ctl-attr")
633 }
634
635 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700636 logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra54934262019-11-13 14:19:55 +0530637 return errors.New("invalid-uni-instance-ctl-attr")
638 }
639
640 if instCtl.Uni == "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700641 logger.Error("uni-multi-instance-tp-not-supported")
Girish Gowdra54934262019-11-13 14:19:55 +0530642 return errors.New("uni-multi-instance-tp-not-supported")
643 }
644
645 return nil
646}
647
npujarec5762e2020-01-01 14:08:48 +0530648func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400649
650 var usGemPortAttributeList []iGemPortAttribute
651 var dsGemPortAttributeList []iGemPortAttribute
Scott Bakeree7c0a02020-01-07 11:12:26 -0800652 var dsMulticastGemAttributeList []iGemPortAttribute
653 var dsUnicastGemAttributeList []iGemPortAttribute
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400654 var tcontIDs []uint32
655 var gemPorts []uint32
656 var err error
657
Scott Baker24f83e22020-03-30 16:14:28 -0700658 logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra54934262019-11-13 14:19:55 +0530659
660 if tp.InstanceCtrl.Onu == "multi-instance" {
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700661 t.AllocIDMgmtLock.Lock()
662 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
663 t.AllocIDMgmtLock.Unlock()
664 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700665 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530666 return nil
667 }
668 } else { // "single-instance"
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000669 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700670 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000671 return nil
672 } else if tpInst == nil {
Girish Gowdra54934262019-11-13 14:19:55 +0530673 // No "single-instance" tp found on one any uni port for the given TP ID
674 // Allocate a new TcontID or AllocID
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700675 t.AllocIDMgmtLock.Lock()
676 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
677 t.AllocIDMgmtLock.Unlock()
678 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700679 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530680 return nil
681 }
682 } else {
683 // Use the alloc-id from the existing TpInstance
684 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
685 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400686 }
Scott Baker24f83e22020-03-30 16:14:28 -0700687 logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700688 t.GemPortIDMgmtLock.Lock()
689 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
690 t.GemPortIDMgmtLock.Unlock()
691 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700692 logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400693 return nil
694 }
Scott Baker24f83e22020-03-30 16:14:28 -0700695 logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400696 for index := 0; index < int(tp.NumGemPorts); index++ {
697 usGemPortAttributeList = append(usGemPortAttributeList,
698 iGemPortAttribute{GemportID: gemPorts[index],
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400699 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
700 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
701 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
702 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
703 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
704 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
705 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
706 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Scott Bakeree7c0a02020-01-07 11:12:26 -0800707 }
708
Scott Baker24f83e22020-03-30 16:14:28 -0700709 logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Scott Bakeree7c0a02020-01-07 11:12:26 -0800710 //put multicast and unicast downstream GEM port attributes in different lists first
711 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
712 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
713 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
714 iGemPortAttribute{
715 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
716 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
717 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
718 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
719 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
720 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
721 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
722 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
723 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
724 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
725 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
726 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
727 } else {
728 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
729 iGemPortAttribute{
730 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
731 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
732 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
733 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
734 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
735 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
736 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
737 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
738 }
739 }
740 //add unicast downstream GEM ports to dsGemPortAttributeList
741 for index := 0; index < int(tp.NumGemPorts); index++ {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400742 dsGemPortAttributeList = append(dsGemPortAttributeList,
743 iGemPortAttribute{GemportID: gemPorts[index],
Scott Bakeree7c0a02020-01-07 11:12:26 -0800744 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
745 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
746 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
747 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
748 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
749 Weight: dsUnicastGemAttributeList[index].Weight,
750 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
751 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400752 }
Scott Bakeree7c0a02020-01-07 11:12:26 -0800753 //add multicast GEM ports to dsGemPortAttributeList afterwards
754 for k := range dsMulticastGemAttributeList {
755 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
756 }
757
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400758 return &TechProfile{
759 SubscriberIdentifier: uniPortName,
760 Name: tp.Name,
761 ProfileType: tp.ProfileType,
762 Version: tp.Version,
763 NumGemPorts: tp.NumGemPorts,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400764 InstanceCtrl: tp.InstanceCtrl,
765 UsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400766 AllocID: tcontIDs[0],
767 Direction: tp.UsScheduler.Direction,
768 AdditionalBw: tp.UsScheduler.AdditionalBw,
769 Priority: tp.UsScheduler.Priority,
770 Weight: tp.UsScheduler.Weight,
771 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400772 DsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400773 AllocID: tcontIDs[0],
774 Direction: tp.DsScheduler.Direction,
775 AdditionalBw: tp.DsScheduler.AdditionalBw,
776 Priority: tp.DsScheduler.Priority,
777 Weight: tp.DsScheduler.Weight,
778 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400779 UpstreamGemPortAttributeList: usGemPortAttributeList,
780 DownstreamGemPortAttributeList: dsGemPortAttributeList}
781}
782
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700783// allocateTPInstance function for EPON
784func (t *TechProfileMgr) allocateEponTPInstance(ctx context.Context, uniPortName string, tp *DefaultEponProfile, intfId uint32, tpInstPath string) *EponProfile {
785
786 var usQueueAttributeList []iUpstreamQueueAttribute
787 var dsQueueAttributeList []iDownstreamQueueAttribute
788 var tcontIDs []uint32
789 var gemPorts []uint32
790 var err error
791
792 log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
793
794 if tp.InstanceCtrl.Onu == "multi-instance" {
795 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
796 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
797 return nil
798 }
799 } else { // "single-instance"
800 if tpInst, err := t.getSingleInstanceEponTp(ctx, tpInstPath); err != nil {
801 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
802 return nil
803 } else if tpInst == nil {
804 // No "single-instance" tp found on one any uni port for the given TP ID
805 // Allocate a new TcontID or AllocID
806 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
807 log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
808 return nil
809 }
810 } else {
811 // Use the alloc-id from the existing TpInstance
812 tcontIDs = append(tcontIDs, tpInst.AllocID)
813 }
814 }
815 log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
816 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
817 log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
818 return nil
819 }
820 log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
821 for index := 0; index < int(tp.NumGemPorts); index++ {
822 usQueueAttributeList = append(usQueueAttributeList,
823 iUpstreamQueueAttribute{GemportID: gemPorts[index],
824 MaxQueueSize: tp.UpstreamQueueAttributeList[index].MaxQueueSize,
825 PbitMap: tp.UpstreamQueueAttributeList[index].PbitMap,
826 AesEncryption: tp.UpstreamQueueAttributeList[index].AesEncryption,
827 TrafficType: tp.UpstreamQueueAttributeList[index].TrafficType,
828 UnsolicitedGrantSize: tp.UpstreamQueueAttributeList[index].UnsolicitedGrantSize,
829 NominalInterval: tp.UpstreamQueueAttributeList[index].NominalInterval,
830 ToleratedPollJitter: tp.UpstreamQueueAttributeList[index].ToleratedPollJitter,
831 RequestTransmissionPolicy: tp.UpstreamQueueAttributeList[index].RequestTransmissionPolicy,
832 NumQueueSet: tp.UpstreamQueueAttributeList[index].NumQueueSet,
833 QThresholds: tp.UpstreamQueueAttributeList[index].QThresholds,
834 SchedulingPolicy: tp.UpstreamQueueAttributeList[index].SchedulingPolicy,
835 PriorityQueue: tp.UpstreamQueueAttributeList[index].PriorityQueue,
836 Weight: tp.UpstreamQueueAttributeList[index].Weight,
837 DiscardPolicy: tp.UpstreamQueueAttributeList[index].DiscardPolicy,
838 DiscardConfig: tp.UpstreamQueueAttributeList[index].DiscardConfig})
839 }
840
841 log.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamQueueAttributeList))
842 for index := 0; index < int(tp.NumGemPorts); index++ {
843 dsQueueAttributeList = append(dsQueueAttributeList,
844 iDownstreamQueueAttribute{GemportID: gemPorts[index],
845 MaxQueueSize: tp.DownstreamQueueAttributeList[index].MaxQueueSize,
846 PbitMap: tp.DownstreamQueueAttributeList[index].PbitMap,
847 AesEncryption: tp.DownstreamQueueAttributeList[index].AesEncryption,
848 SchedulingPolicy: tp.DownstreamQueueAttributeList[index].SchedulingPolicy,
849 PriorityQueue: tp.DownstreamQueueAttributeList[index].PriorityQueue,
850 Weight: tp.DownstreamQueueAttributeList[index].Weight,
851 DiscardPolicy: tp.DownstreamQueueAttributeList[index].DiscardPolicy,
852 DiscardConfig: tp.DownstreamQueueAttributeList[index].DiscardConfig})
853 }
854
855 return &EponProfile{
856 SubscriberIdentifier: uniPortName,
857 Name: tp.Name,
858 ProfileType: tp.ProfileType,
859 Version: tp.Version,
860 NumGemPorts: tp.NumGemPorts,
861 InstanceCtrl: tp.InstanceCtrl,
862 EponAttribute: tp.EponAttribute,
863 AllocID: tcontIDs[0],
864 UpstreamQueueAttributeList: usQueueAttributeList,
865 DownstreamQueueAttributeList: dsQueueAttributeList}
866}
867
Girish Gowdra54934262019-11-13 14:19:55 +0530868// getSingleInstanceTp returns another TpInstance for an ONU on a different
869// uni port for the same TP ID, if it finds one, else nil.
npujarec5762e2020-01-01 14:08:48 +0530870func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra54934262019-11-13 14:19:55 +0530871 var tpInst TechProfile
872
873 // For example:
874 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
875 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
876 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujarec5762e2020-01-01 14:08:48 +0530877 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra54934262019-11-13 14:19:55 +0530878
879 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
880 for keyPath, kvPair := range kvPairs {
881 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
882 if err = json.Unmarshal(value, &tpInst); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700883 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530884 return nil, errors.New("error-unmarshal-kv-pair")
885 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700886 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra54934262019-11-13 14:19:55 +0530887 return &tpInst, nil
888 }
889 }
890 }
891 return nil, nil
892}
893
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700894func (t *TechProfileMgr) getSingleInstanceEponTp(ctx context.Context, tpPath string) (*EponProfile, error) {
895 var tpInst EponProfile
896
897 // For example:
898 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
899 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
900 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
901 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
902
903 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
904 for keyPath, kvPair := range kvPairs {
905 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
906 if err = json.Unmarshal(value, &tpInst); err != nil {
907 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
908 return nil, errors.New("error-unmarshal-kv-pair")
909 } else {
910 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
911 return &tpInst, nil
912 }
913 }
914 }
915 return nil, nil
916}
917
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400918func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
919
920 var usGemPortAttributeList []GemPortAttribute
921 var dsGemPortAttributeList []GemPortAttribute
922
923 for _, pbit := range t.config.DefaultPbits {
Scott Baker24f83e22020-03-30 16:14:28 -0700924 logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400925 usGemPortAttributeList = append(usGemPortAttributeList,
926 GemPortAttribute{
927 MaxQueueSize: defaultMaxQueueSize,
928 PbitMap: pbit,
929 AesEncryption: defaultAESEncryption,
930 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
931 PriorityQueue: defaultPriorityQueue,
932 Weight: defaultQueueWeight,
933 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
934 DiscardConfig: DiscardConfig{
935 MinThreshold: defaultMinThreshold,
936 MaxThreshold: defaultMaxThreshold,
937 MaxProbability: defaultMaxProbability}})
938 dsGemPortAttributeList = append(dsGemPortAttributeList,
939 GemPortAttribute{
940 MaxQueueSize: defaultMaxQueueSize,
941 PbitMap: pbit,
942 AesEncryption: defaultAESEncryption,
943 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
944 PriorityQueue: defaultPriorityQueue,
945 Weight: defaultQueueWeight,
946 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
947 DiscardConfig: DiscardConfig{
948 MinThreshold: defaultMinThreshold,
949 MaxThreshold: defaultMaxThreshold,
Scott Bakeree7c0a02020-01-07 11:12:26 -0800950 MaxProbability: defaultMaxProbability},
951 IsMulticast: defaultIsMulticast,
952 DControlList: defaultAccessControlList,
953 SControlList: defaultAccessControlList,
954 McastGemID: defaultMcastGemID})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400955 }
956 return &DefaultTechProfile{
957 Name: t.config.DefaultTPName,
958 ProfileType: t.resourceMgr.GetTechnology(),
959 Version: t.config.TPVersion,
960 NumGemPorts: uint32(len(usGemPortAttributeList)),
961 InstanceCtrl: InstanceControl{
962 Onu: defaultOnuInstance,
963 Uni: defaultUniInstance,
964 MaxGemPayloadSize: defaultGemPayloadSize},
965 UsScheduler: Scheduler{
966 Direction: Direction_name[Direction_UPSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530967 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400968 Priority: defaultPriority,
969 Weight: defaultWeight,
970 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
971 DsScheduler: Scheduler{
972 Direction: Direction_name[Direction_DOWNSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530973 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400974 Priority: defaultPriority,
975 Weight: defaultWeight,
976 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
977 UpstreamGemPortAttributeList: usGemPortAttributeList,
978 DownstreamGemPortAttributeList: dsGemPortAttributeList}
979}
980
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -0700981// getDefaultTechProfile function for EPON
982func (t *TechProfileMgr) getDefaultEponProfile() *DefaultEponProfile {
983
984 var usQueueAttributeList []UpstreamQueueAttribute
985 var dsQueueAttributeList []DownstreamQueueAttribute
986
987 for _, pbit := range t.config.DefaultPbits {
988 log.Debugw("Creating Queue", log.Fields{"pbit": pbit})
989 usQueueAttributeList = append(usQueueAttributeList,
990 UpstreamQueueAttribute{
991 MaxQueueSize: defaultMaxQueueSize,
992 PbitMap: pbit,
993 AesEncryption: defaultAESEncryption,
994 TrafficType: defaultTrafficType,
995 UnsolicitedGrantSize: defaultUnsolicitedGrantSize,
996 NominalInterval: defaultNominalInterval,
997 ToleratedPollJitter: defaultToleratedPollJitter,
998 RequestTransmissionPolicy: defaultRequestTransmissionPolicy,
999 NumQueueSet: defaultNumQueueSet,
1000 QThresholds: QThresholds{
1001 QThreshold1: defaultQThreshold1,
1002 QThreshold2: defaultQThreshold2,
1003 QThreshold3: defaultQThreshold3,
1004 QThreshold4: defaultQThreshold4,
1005 QThreshold5: defaultQThreshold5,
1006 QThreshold6: defaultQThreshold6,
1007 QThreshold7: defaultQThreshold7},
1008 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1009 PriorityQueue: defaultPriorityQueue,
1010 Weight: defaultQueueWeight,
1011 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1012 DiscardConfig: DiscardConfig{
1013 MinThreshold: defaultMinThreshold,
1014 MaxThreshold: defaultMaxThreshold,
1015 MaxProbability: defaultMaxProbability}})
1016 dsQueueAttributeList = append(dsQueueAttributeList,
1017 DownstreamQueueAttribute{
1018 MaxQueueSize: defaultMaxQueueSize,
1019 PbitMap: pbit,
1020 AesEncryption: defaultAESEncryption,
1021 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
1022 PriorityQueue: defaultPriorityQueue,
1023 Weight: defaultQueueWeight,
1024 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
1025 DiscardConfig: DiscardConfig{
1026 MinThreshold: defaultMinThreshold,
1027 MaxThreshold: defaultMaxThreshold,
1028 MaxProbability: defaultMaxProbability}})
1029 }
1030 return &DefaultEponProfile{
1031 Name: t.config.DefaultTPName,
1032 ProfileType: t.resourceMgr.GetTechnology(),
1033 Version: t.config.TPVersion,
1034 NumGemPorts: uint32(len(usQueueAttributeList)),
1035 InstanceCtrl: InstanceControl{
1036 Onu: defaultOnuInstance,
1037 Uni: defaultUniInstance,
1038 MaxGemPayloadSize: defaultGemPayloadSize},
1039 EponAttribute: EponAttribute{
1040 PackageType: defaultPakageType},
1041 UpstreamQueueAttributeList: usQueueAttributeList,
1042 DownstreamQueueAttributeList: dsQueueAttributeList}
1043}
1044
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001045func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
1046 var result int32 = -1
1047
1048 if paramType == "direction" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001049 for key, val := range tp_pb.Direction_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001050 if key == paramKey {
1051 result = val
1052 }
1053 }
1054 } else if paramType == "discard_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001055 for key, val := range tp_pb.DiscardPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001056 if key == paramKey {
1057 result = val
1058 }
1059 }
1060 } else if paramType == "sched_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001061 for key, val := range tp_pb.SchedulingPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001062 if key == paramKey {
Scott Baker24f83e22020-03-30 16:14:28 -07001063 logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001064 result = val
1065 }
1066 }
1067 } else if paramType == "additional_bw" {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001068 for key, val := range tp_pb.AdditionalBW_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001069 if key == paramKey {
1070 result = val
1071 }
1072 }
1073 } else {
Scott Baker24f83e22020-03-30 16:14:28 -07001074 logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001075 return -1
1076 }
Scott Baker24f83e22020-03-30 16:14:28 -07001077 logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001078 return result
1079}
1080
Girish Kumar8f73fe02019-12-09 13:19:37 +00001081func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001082 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001083 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001084 logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001085 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 -04001086 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001087
Manikkaraj kb1d51442019-07-23 10:41:02 -04001088 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001089 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001090 logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001091 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 -04001092 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001093
Manikkaraj kb1d51442019-07-23 10:41:02 -04001094 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001095 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001096 logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001097 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 -04001098 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001099
Manikkaraj kb1d51442019-07-23 10:41:02 -04001100 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001101 Direction: dir,
1102 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001103 Priority: tpInstance.UsScheduler.Priority,
1104 Weight: tpInstance.UsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001105 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001106}
1107
Girish Kumar8f73fe02019-12-09 13:19:37 +00001108func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001109
Manikkaraj kb1d51442019-07-23 10:41:02 -04001110 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001111 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001112 logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001113 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 -04001114 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001115
Manikkaraj kb1d51442019-07-23 10:41:02 -04001116 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001117 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001118 logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001119 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 -04001120 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001121
Manikkaraj kb1d51442019-07-23 10:41:02 -04001122 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001123 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001124 logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001125 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 -04001126 }
1127
Manikkaraj kb1d51442019-07-23 10:41:02 -04001128 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001129 Direction: dir,
1130 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001131 Priority: tpInstance.DsScheduler.Priority,
1132 Weight: tpInstance.DsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001133 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001134}
1135
Manikkaraj kb1d51442019-07-23 10:41:02 -04001136func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
1137 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
1138
1139 tSched := &tp_pb.TrafficScheduler{
1140 Direction: SchedCfg.Direction,
1141 AllocId: tpInstance.UsScheduler.AllocID,
1142 TrafficShapingInfo: ShapingCfg,
1143 Scheduler: SchedCfg}
1144
1145 return tSched
1146}
1147
Girish Kumar8f73fe02019-12-09 13:19:37 +00001148func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001149
1150 var encryp bool
1151 if Dir == tp_pb.Direction_UPSTREAM {
1152 // upstream GEM ports
1153 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
1154 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1155 for Count := 0; Count < NumGemPorts; Count++ {
1156 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
1157 encryp = true
1158 } else {
1159 encryp = false
1160 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001161
1162 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1163 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001164 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 +00001165 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
1166 }
1167
1168 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1169 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001170 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 +00001171 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
1172 }
1173
Manikkaraj kb1d51442019-07-23 10:41:02 -04001174 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
1175 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
1176 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
1177 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
1178 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001179 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001180 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
1181 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001182 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001183 })
1184 }
Scott Baker24f83e22020-03-30 16:14:28 -07001185 logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +00001186 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -04001187 } else if Dir == tp_pb.Direction_DOWNSTREAM {
1188 //downstream GEM ports
1189 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1190 GemPorts := make([]*tp_pb.TrafficQueue, 0)
1191 for Count := 0; Count < NumGemPorts; Count++ {
Scott Bakeree7c0a02020-01-07 11:12:26 -08001192 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1193 //do not take multicast GEM ports. They are handled separately.
1194 continue
1195 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001196 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1197 encryp = true
1198 } else {
1199 encryp = false
1200 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001201
1202 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1203 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001204 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 +00001205 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
1206 }
1207
1208 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1209 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -07001210 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 +00001211 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
1212 }
1213
Manikkaraj kb1d51442019-07-23 10:41:02 -04001214 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
1215 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
1216 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
1217 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1218 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001219 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001220 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1221 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +00001222 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -04001223 })
1224 }
Scott Baker24f83e22020-03-30 16:14:28 -07001225 logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +00001226 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -04001227 }
Girish Kumar8f73fe02019-12-09 13:19:37 +00001228
Scott Baker24f83e22020-03-30 16:14:28 -07001229 logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumar8f73fe02019-12-09 13:19:37 +00001230 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001231}
1232
Scott Bakeree7c0a02020-01-07 11:12:26 -08001233//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
1234func isMulticastGem(isMulticastAttrValue string) bool {
1235 return isMulticastAttrValue != "" &&
1236 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
1237}
1238
1239func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
1240 var encryp bool
1241 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
1242 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
1243 for Count := 0; Count < NumGemPorts; Count++ {
1244 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
1245 continue
1246 }
1247 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
1248 encryp = true
1249 } else {
1250 encryp = false
1251 }
1252 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
1253 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
1254 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
1255 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
1256 AesEncryption: encryp,
1257 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
1258 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
1259 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
1260 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
1261 })
1262 }
Scott Baker24f83e22020-03-30 16:14:28 -07001263 logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Scott Bakeree7c0a02020-01-07 11:12:26 -08001264 return mcastTrafficQueues
1265}
1266
Manikkaraj kb1d51442019-07-23 10:41:02 -04001267func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumar8f73fe02019-12-09 13:19:37 +00001268 UsScheduler, _ := tpm.GetUsScheduler(tp)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001269
1270 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
1271 AllocId: tp.UsScheduler.AllocID,
1272 Scheduler: UsScheduler}
1273}
1274
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001275func (t *TechProfileMgr) GetGemportIDForPbit(tp interface{}, dir tp_pb.Direction, pbit uint32) uint32 {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001276 /*
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001277 Function to get the Gemport ID mapped to a pbit.
Manikkaraj kb1d51442019-07-23 10:41:02 -04001278 */
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001279 switch tp := tp.(type) {
1280 case *TechProfile:
1281 if dir == tp_pb.Direction_UPSTREAM {
1282 // upstream GEM ports
1283 numGemPorts := len(tp.UpstreamGemPortAttributeList)
1284 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1285 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
1286 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1287 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1288 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1289 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1290 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1291 logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
1292 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
1293 }
1294 }
1295 }
1296 }
1297 } else if dir == tp_pb.Direction_DOWNSTREAM {
1298 //downstream GEM ports
1299 numGemPorts := len(tp.DownstreamGemPortAttributeList)
1300 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1301 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
1302 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1303 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1304 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1305 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1306 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1307 logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
1308 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
1309 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001310 }
1311 }
1312 }
1313 }
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001314 logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
1315 case *EponProfile:
1316 if dir == tp_pb.Direction_UPSTREAM {
1317 // upstream GEM ports
1318 numGemPorts := len(tp.UpstreamQueueAttributeList)
1319 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1320 lenOfPbitMap := len(tp.UpstreamQueueAttributeList[gemCnt].PbitMap)
1321 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1322 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1323 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1324 if p, err := strconv.Atoi(string(tp.UpstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1325 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1326 logger.Debugw("Found-US-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.UpstreamQueueAttributeList[gemCnt].GemportID})
1327 return tp.UpstreamQueueAttributeList[gemCnt].GemportID
1328 }
1329 }
1330 }
1331 }
1332 } else if dir == tp_pb.Direction_DOWNSTREAM {
1333 //downstream GEM ports
1334 numGemPorts := len(tp.DownstreamQueueAttributeList)
1335 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
1336 lenOfPbitMap := len(tp.DownstreamQueueAttributeList[gemCnt].PbitMap)
1337 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
1338 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
1339 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
1340 if p, err := strconv.Atoi(string(tp.DownstreamQueueAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
1341 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
1342 logger.Debugw("Found-DS-Queue-for-Pcp", log.Fields{"pbit": pbit, "Queue": tp.DownstreamQueueAttributeList[gemCnt].GemportID})
1343 return tp.DownstreamQueueAttributeList[gemCnt].GemportID
1344 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001345 }
1346 }
1347 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001348 }
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001349 logger.Errorw("No-QueueId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
1350 default:
1351 logger.Errorw("unknown-tech", log.Fields{"tp": tp})
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001352 }
Manikkaraj kb1d51442019-07-23 10:41:02 -04001353 return 0
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001354}
Girish Gowdra54934262019-11-13 14:19:55 +05301355
1356// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001357func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) interface{} {
1358 var tpTech TechProfile
1359 var tpEpon EponProfile
1360
Girish Gowdra6b130582019-11-20 16:45:20 +05301361 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra54934262019-11-13 14:19:55 +05301362
npujarec5762e2020-01-01 14:08:48 +05301363 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001364 tech := t.resourceMgr.GetTechnology()
1365 tpInstancesTech := make([]TechProfile, 0, len(kvPairs))
1366 tpInstancesEpon := make([]EponProfile, 0, len(kvPairs))
1367
Girish Gowdra54934262019-11-13 14:19:55 +05301368 for kvPath, kvPair := range kvPairs {
1369 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001370 if tech == xgspon || tech == xgsponBbsim || tech == gpon {
1371 if err = json.Unmarshal(value, &tpTech); err != nil {
1372 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
1373 continue
1374 } else {
1375 tpInstancesTech = append(tpInstancesTech, tpTech)
1376 }
1377 } else if tech == epon {
1378 if err = json.Unmarshal(value, &tpEpon); err != nil {
1379 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
1380 continue
1381 } else {
1382 tpInstancesEpon = append(tpInstancesEpon, tpEpon)
1383 }
Girish Gowdra54934262019-11-13 14:19:55 +05301384 }
1385 }
1386 }
Takahiro Suzuki2ba0e0b2020-06-05 14:23:03 -07001387
1388 switch tech {
1389 case xgspon, xgsponBbsim, gpon:
1390 return tpInstancesTech
1391 case epon:
1392 return tpInstancesEpon
1393 default:
1394 log.Errorw("unknown-technology", log.Fields{"tech": tech})
1395 return nil
1396 }
Girish Gowdra54934262019-11-13 14:19:55 +05301397 }
1398 return nil
1399}