blob: 60e4daec7190a3957a67ff10ff64ccbe3a5e605a [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/*
108type InferredAdditionBWIndication int32
109
110const (
111 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
112 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
113 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
114)
115
116var InferredAdditionBWIndication_name = map[int32]string{
117 0: "InferredAdditionBWIndication_None",
118 1: "InferredAdditionBWIndication_Assured",
119 2: "InferredAdditionBWIndication_BestEffort",
120}
121*/
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
sbarbaria8910ba2019-11-05 10:12:23 -0500248func (t *TechProfileMgr) SetKVClient() *db.Backend {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000249 kvClient, err := newKVClient(t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400250 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700251 logger.Errorw("failed-to-create-kv-client",
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400252 log.Fields{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000253 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400254 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
255 "error": err.Error(),
256 })
257 return nil
258 }
sbarbaria8910ba2019-11-05 10:12:23 -0500259 return &db.Backend{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400260 Client: kvClient,
261 StoreType: t.config.KVStoreType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000262 Address: t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400263 Timeout: t.config.KVStoreTimeout,
264 PathPrefix: t.config.TPKVPathPrefix}
265
266 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
267 issue between kv store and backend , core is not calling NewBackend directly
268 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
269 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
270 */
271}
272
Neha Sharmacc656962020-04-14 14:26:11 +0000273func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400274
Scott Baker24f83e22020-03-30 16:14:28 -0700275 logger.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400276 switch storeType {
277 case "consul":
278 return kvstore.NewConsulClient(address, timeout)
279 case "etcd":
Scott Bakered4a8e72020-04-17 11:10:20 -0700280 return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400281 }
282 return nil, errors.New("unsupported-kv-store")
283}
284
Neha Sharma3f221ae2020-04-29 19:02:12 +0000285func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400286 var techprofileObj TechProfileMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700287 logger.Debug("Initializing techprofile Manager")
Neha Sharma3f221ae2020-04-29 19:02:12 +0000288 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400289 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
290 if techprofileObj.config.KVBackend == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700291 logger.Error("Failed to initialize KV backend\n")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400292 return nil, errors.New("KV backend init failed")
293 }
294 techprofileObj.resourceMgr = resourceMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700295 logger.Debug("Initializing techprofile object instance success")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400296 return &techprofileObj, nil
297}
298
299func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700300 logger.Debugw("get-tp-instance-kv-path", log.Fields{
301 "uniPortName": uniPortName,
302 "tpId": techProfiletblID,
303 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400304 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
305}
306
npujarec5762e2020-01-01 14:08:48 +0530307func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400308 var KvTpIns TechProfile
309 var resPtr *TechProfile = &KvTpIns
310 var err error
Girish Gowdra54934262019-11-13 14:19:55 +0530311 var kvResult *kvstore.KVPair
312
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700313 logger.Infow("get-tp-instance-form-kv-store", log.Fields{"path": path, "tpid": techProfiletblID})
314
npujarec5762e2020-01-01 14:08:48 +0530315 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra54934262019-11-13 14:19:55 +0530316 if kvResult == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700317 logger.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra54934262019-11-13 14:19:55 +0530318 return nil, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400319 } else {
Girish Gowdra54934262019-11-13 14:19:55 +0530320 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400321 if err = json.Unmarshal(value, resPtr); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700322 logger.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530323 return nil, errors.New("error-unmarshal-kv-result")
324 } else {
325 return resPtr, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400326 }
327 }
328 }
Girish Gowdra54934262019-11-13 14:19:55 +0530329 return nil, err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400330}
331
npujarec5762e2020-01-01 14:08:48 +0530332func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400333 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Scott Baker24f83e22020-03-30 16:14:28 -0700334 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400335 tpInstanceJson, err := json.Marshal(*tpInstance)
336 if err == nil {
337 // Backend will convert JSON byte array into string format
Scott Baker24f83e22020-03-30 16:14:28 -0700338 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujarec5762e2020-01-01 14:08:48 +0530339 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400340 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700341 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400342 }
343 return err
344}
npujarec5762e2020-01-01 14:08:48 +0530345func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400346 var kvtechprofile DefaultTechProfile
347 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Scott Baker24f83e22020-03-30 16:14:28 -0700348 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujarec5762e2020-01-01 14:08:48 +0530349 kvresult, err := t.config.KVBackend.Get(ctx, key)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400350 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700351 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400352 return nil
353 }
354 if kvresult != nil {
355 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
356 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumar8f73fe02019-12-09 13:19:37 +0000357 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700358 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 +0000359 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400360 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000361
Scott Baker24f83e22020-03-30 16:14:28 -0700362 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000363 return &kvtechprofile
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400364 }
365 }
366 return nil
367}
Girish Kumar8f73fe02019-12-09 13:19:37 +0000368
npujarec5762e2020-01-01 14:08:48 +0530369func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400370 var tpInstance *TechProfile
Scott Baker24f83e22020-03-30 16:14:28 -0700371 logger.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530372
373 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
374 if !uniPortNameFormat.Match([]byte(uniPortName)) {
Scott Baker24f83e22020-03-30 16:14:28 -0700375 logger.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000376 return nil, errors.New("uni-port-name-not-confirming-to-format")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400377 }
Girish Gowdra54934262019-11-13 14:19:55 +0530378
npujarec5762e2020-01-01 14:08:48 +0530379 tp := t.getTPFromKVStore(ctx, techProfiletblID)
Girish Gowdra54934262019-11-13 14:19:55 +0530380 if tp != nil {
381 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700382 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
Girish Gowdra54934262019-11-13 14:19:55 +0530383 tp = t.getDefaultTechProfile()
384 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700385 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Girish Gowdra54934262019-11-13 14:19:55 +0530386 }
387 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700388 logger.Info("tp-not-found-on-kv--creating-default-tp")
Girish Gowdra54934262019-11-13 14:19:55 +0530389 tp = t.getDefaultTechProfile()
390 }
391 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujarec5762e2020-01-01 14:08:48 +0530392 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700393 logger.Error("tp-intance-allocation-failed")
Girish Kumar8f73fe02019-12-09 13:19:37 +0000394 return nil, errors.New("tp-intance-allocation-failed")
Girish Gowdra54934262019-11-13 14:19:55 +0530395 }
npujarec5762e2020-01-01 14:08:48 +0530396 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700397 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000398 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra54934262019-11-13 14:19:55 +0530399 }
Scott Baker24f83e22020-03-30 16:14:28 -0700400 logger.Infow("tp-added-to-kv-store-successfully",
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400401 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000402 return tpInstance, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400403}
404
npujarec5762e2020-01-01 14:08:48 +0530405func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400406 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujarec5762e2020-01-01 14:08:48 +0530407 return t.config.KVBackend.Delete(ctx, path)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400408}
409
Girish Gowdra54934262019-11-13 14:19:55 +0530410func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
411 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700412 logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra54934262019-11-13 14:19:55 +0530413 return errors.New("invalid-onu-instance-ctl-attr")
414 }
415
416 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700417 logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra54934262019-11-13 14:19:55 +0530418 return errors.New("invalid-uni-instance-ctl-attr")
419 }
420
421 if instCtl.Uni == "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700422 logger.Error("uni-multi-instance-tp-not-supported")
Girish Gowdra54934262019-11-13 14:19:55 +0530423 return errors.New("uni-multi-instance-tp-not-supported")
424 }
425
426 return nil
427}
428
npujarec5762e2020-01-01 14:08:48 +0530429func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400430
431 var usGemPortAttributeList []iGemPortAttribute
432 var dsGemPortAttributeList []iGemPortAttribute
Scott Bakeree7c0a02020-01-07 11:12:26 -0800433 var dsMulticastGemAttributeList []iGemPortAttribute
434 var dsUnicastGemAttributeList []iGemPortAttribute
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400435 var tcontIDs []uint32
436 var gemPorts []uint32
437 var err error
438
Scott Baker24f83e22020-03-30 16:14:28 -0700439 logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra54934262019-11-13 14:19:55 +0530440
441 if tp.InstanceCtrl.Onu == "multi-instance" {
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700442 t.AllocIDMgmtLock.Lock()
443 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
444 t.AllocIDMgmtLock.Unlock()
445 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700446 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530447 return nil
448 }
449 } else { // "single-instance"
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000450 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700451 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000452 return nil
453 } else if tpInst == nil {
Girish Gowdra54934262019-11-13 14:19:55 +0530454 // No "single-instance" tp found on one any uni port for the given TP ID
455 // Allocate a new TcontID or AllocID
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700456 t.AllocIDMgmtLock.Lock()
457 tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1)
458 t.AllocIDMgmtLock.Unlock()
459 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700460 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530461 return nil
462 }
463 } else {
464 // Use the alloc-id from the existing TpInstance
465 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
466 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400467 }
Scott Baker24f83e22020-03-30 16:14:28 -0700468 logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
Girish Gowdra631ef3d2020-06-15 10:45:52 -0700469 t.GemPortIDMgmtLock.Lock()
470 gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts)
471 t.GemPortIDMgmtLock.Unlock()
472 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700473 logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400474 return nil
475 }
Scott Baker24f83e22020-03-30 16:14:28 -0700476 logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400477 for index := 0; index < int(tp.NumGemPorts); index++ {
478 usGemPortAttributeList = append(usGemPortAttributeList,
479 iGemPortAttribute{GemportID: gemPorts[index],
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400480 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
481 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
482 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
483 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
484 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
485 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
486 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
487 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Scott Bakeree7c0a02020-01-07 11:12:26 -0800488 }
489
Scott Baker24f83e22020-03-30 16:14:28 -0700490 logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Scott Bakeree7c0a02020-01-07 11:12:26 -0800491 //put multicast and unicast downstream GEM port attributes in different lists first
492 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
493 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
494 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
495 iGemPortAttribute{
496 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
497 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
498 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
499 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
500 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
501 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
502 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
503 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
504 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
505 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
506 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
507 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
508 } else {
509 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
510 iGemPortAttribute{
511 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
512 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
513 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
514 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
515 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
516 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
517 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
518 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
519 }
520 }
521 //add unicast downstream GEM ports to dsGemPortAttributeList
522 for index := 0; index < int(tp.NumGemPorts); index++ {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400523 dsGemPortAttributeList = append(dsGemPortAttributeList,
524 iGemPortAttribute{GemportID: gemPorts[index],
Scott Bakeree7c0a02020-01-07 11:12:26 -0800525 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
526 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
527 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
528 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
529 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
530 Weight: dsUnicastGemAttributeList[index].Weight,
531 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
532 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400533 }
Scott Bakeree7c0a02020-01-07 11:12:26 -0800534 //add multicast GEM ports to dsGemPortAttributeList afterwards
535 for k := range dsMulticastGemAttributeList {
536 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
537 }
538
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400539 return &TechProfile{
540 SubscriberIdentifier: uniPortName,
541 Name: tp.Name,
542 ProfileType: tp.ProfileType,
543 Version: tp.Version,
544 NumGemPorts: tp.NumGemPorts,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400545 InstanceCtrl: tp.InstanceCtrl,
546 UsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400547 AllocID: tcontIDs[0],
548 Direction: tp.UsScheduler.Direction,
549 AdditionalBw: tp.UsScheduler.AdditionalBw,
550 Priority: tp.UsScheduler.Priority,
551 Weight: tp.UsScheduler.Weight,
552 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400553 DsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400554 AllocID: tcontIDs[0],
555 Direction: tp.DsScheduler.Direction,
556 AdditionalBw: tp.DsScheduler.AdditionalBw,
557 Priority: tp.DsScheduler.Priority,
558 Weight: tp.DsScheduler.Weight,
559 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400560 UpstreamGemPortAttributeList: usGemPortAttributeList,
561 DownstreamGemPortAttributeList: dsGemPortAttributeList}
562}
563
Girish Gowdra54934262019-11-13 14:19:55 +0530564// getSingleInstanceTp returns another TpInstance for an ONU on a different
565// uni port for the same TP ID, if it finds one, else nil.
npujarec5762e2020-01-01 14:08:48 +0530566func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra54934262019-11-13 14:19:55 +0530567 var tpInst TechProfile
568
569 // For example:
570 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
571 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
572 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujarec5762e2020-01-01 14:08:48 +0530573 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra54934262019-11-13 14:19:55 +0530574
575 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
576 for keyPath, kvPair := range kvPairs {
577 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
578 if err = json.Unmarshal(value, &tpInst); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700579 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530580 return nil, errors.New("error-unmarshal-kv-pair")
581 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700582 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra54934262019-11-13 14:19:55 +0530583 return &tpInst, nil
584 }
585 }
586 }
587 return nil, nil
588}
589
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400590func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
591
592 var usGemPortAttributeList []GemPortAttribute
593 var dsGemPortAttributeList []GemPortAttribute
594
595 for _, pbit := range t.config.DefaultPbits {
Scott Baker24f83e22020-03-30 16:14:28 -0700596 logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400597 usGemPortAttributeList = append(usGemPortAttributeList,
598 GemPortAttribute{
599 MaxQueueSize: defaultMaxQueueSize,
600 PbitMap: pbit,
601 AesEncryption: defaultAESEncryption,
602 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
603 PriorityQueue: defaultPriorityQueue,
604 Weight: defaultQueueWeight,
605 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
606 DiscardConfig: DiscardConfig{
607 MinThreshold: defaultMinThreshold,
608 MaxThreshold: defaultMaxThreshold,
609 MaxProbability: defaultMaxProbability}})
610 dsGemPortAttributeList = append(dsGemPortAttributeList,
611 GemPortAttribute{
612 MaxQueueSize: defaultMaxQueueSize,
613 PbitMap: pbit,
614 AesEncryption: defaultAESEncryption,
615 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
616 PriorityQueue: defaultPriorityQueue,
617 Weight: defaultQueueWeight,
618 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
619 DiscardConfig: DiscardConfig{
620 MinThreshold: defaultMinThreshold,
621 MaxThreshold: defaultMaxThreshold,
Scott Bakeree7c0a02020-01-07 11:12:26 -0800622 MaxProbability: defaultMaxProbability},
623 IsMulticast: defaultIsMulticast,
624 DControlList: defaultAccessControlList,
625 SControlList: defaultAccessControlList,
626 McastGemID: defaultMcastGemID})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400627 }
628 return &DefaultTechProfile{
629 Name: t.config.DefaultTPName,
630 ProfileType: t.resourceMgr.GetTechnology(),
631 Version: t.config.TPVersion,
632 NumGemPorts: uint32(len(usGemPortAttributeList)),
633 InstanceCtrl: InstanceControl{
634 Onu: defaultOnuInstance,
635 Uni: defaultUniInstance,
636 MaxGemPayloadSize: defaultGemPayloadSize},
637 UsScheduler: Scheduler{
638 Direction: Direction_name[Direction_UPSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530639 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400640 Priority: defaultPriority,
641 Weight: defaultWeight,
642 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
643 DsScheduler: Scheduler{
644 Direction: Direction_name[Direction_DOWNSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530645 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400646 Priority: defaultPriority,
647 Weight: defaultWeight,
648 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
649 UpstreamGemPortAttributeList: usGemPortAttributeList,
650 DownstreamGemPortAttributeList: dsGemPortAttributeList}
651}
652
653func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
654 var result int32 = -1
655
656 if paramType == "direction" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400657 for key, val := range tp_pb.Direction_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400658 if key == paramKey {
659 result = val
660 }
661 }
662 } else if paramType == "discard_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400663 for key, val := range tp_pb.DiscardPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400664 if key == paramKey {
665 result = val
666 }
667 }
668 } else if paramType == "sched_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400669 for key, val := range tp_pb.SchedulingPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400670 if key == paramKey {
Scott Baker24f83e22020-03-30 16:14:28 -0700671 logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400672 result = val
673 }
674 }
675 } else if paramType == "additional_bw" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400676 for key, val := range tp_pb.AdditionalBW_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400677 if key == paramKey {
678 result = val
679 }
680 }
681 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700682 logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400683 return -1
684 }
Scott Baker24f83e22020-03-30 16:14:28 -0700685 logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400686 return result
687}
688
Girish Kumar8f73fe02019-12-09 13:19:37 +0000689func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400690 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400691 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700692 logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000693 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 -0400694 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000695
Manikkaraj kb1d51442019-07-23 10:41:02 -0400696 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400697 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700698 logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000699 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 -0400700 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000701
Manikkaraj kb1d51442019-07-23 10:41:02 -0400702 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400703 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700704 logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000705 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 -0400706 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000707
Manikkaraj kb1d51442019-07-23 10:41:02 -0400708 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400709 Direction: dir,
710 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400711 Priority: tpInstance.UsScheduler.Priority,
712 Weight: tpInstance.UsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000713 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400714}
715
Girish Kumar8f73fe02019-12-09 13:19:37 +0000716func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400717
Manikkaraj kb1d51442019-07-23 10:41:02 -0400718 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400719 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700720 logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000721 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 -0400722 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000723
Manikkaraj kb1d51442019-07-23 10:41:02 -0400724 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400725 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700726 logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000727 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 -0400728 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000729
Manikkaraj kb1d51442019-07-23 10:41:02 -0400730 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400731 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700732 logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000733 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 -0400734 }
735
Manikkaraj kb1d51442019-07-23 10:41:02 -0400736 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400737 Direction: dir,
738 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400739 Priority: tpInstance.DsScheduler.Priority,
740 Weight: tpInstance.DsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000741 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400742}
743
Manikkaraj kb1d51442019-07-23 10:41:02 -0400744func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
745 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
746
747 tSched := &tp_pb.TrafficScheduler{
748 Direction: SchedCfg.Direction,
749 AllocId: tpInstance.UsScheduler.AllocID,
750 TrafficShapingInfo: ShapingCfg,
751 Scheduler: SchedCfg}
752
753 return tSched
754}
755
Girish Kumar8f73fe02019-12-09 13:19:37 +0000756func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400757
758 var encryp bool
759 if Dir == tp_pb.Direction_UPSTREAM {
760 // upstream GEM ports
761 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
762 GemPorts := make([]*tp_pb.TrafficQueue, 0)
763 for Count := 0; Count < NumGemPorts; Count++ {
764 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
765 encryp = true
766 } else {
767 encryp = false
768 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000769
770 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
771 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700772 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 +0000773 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
774 }
775
776 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
777 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700778 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 +0000779 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
780 }
781
Manikkaraj kb1d51442019-07-23 10:41:02 -0400782 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
783 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
784 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
785 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
786 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000787 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400788 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
789 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000790 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400791 })
792 }
Scott Baker24f83e22020-03-30 16:14:28 -0700793 logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000794 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -0400795 } else if Dir == tp_pb.Direction_DOWNSTREAM {
796 //downstream GEM ports
797 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
798 GemPorts := make([]*tp_pb.TrafficQueue, 0)
799 for Count := 0; Count < NumGemPorts; Count++ {
Scott Bakeree7c0a02020-01-07 11:12:26 -0800800 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
801 //do not take multicast GEM ports. They are handled separately.
802 continue
803 }
Manikkaraj kb1d51442019-07-23 10:41:02 -0400804 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
805 encryp = true
806 } else {
807 encryp = false
808 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000809
810 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
811 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700812 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 +0000813 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
814 }
815
816 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
817 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700818 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 +0000819 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
820 }
821
Manikkaraj kb1d51442019-07-23 10:41:02 -0400822 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
823 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
824 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
825 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
826 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000827 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400828 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
829 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000830 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400831 })
832 }
Scott Baker24f83e22020-03-30 16:14:28 -0700833 logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000834 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -0400835 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000836
Scott Baker24f83e22020-03-30 16:14:28 -0700837 logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000838 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400839}
840
Scott Bakeree7c0a02020-01-07 11:12:26 -0800841//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
842func isMulticastGem(isMulticastAttrValue string) bool {
843 return isMulticastAttrValue != "" &&
844 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
845}
846
847func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
848 var encryp bool
849 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
850 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
851 for Count := 0; Count < NumGemPorts; Count++ {
852 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
853 continue
854 }
855 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
856 encryp = true
857 } else {
858 encryp = false
859 }
860 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
861 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
862 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
863 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
864 AesEncryption: encryp,
865 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
866 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
867 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
868 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
869 })
870 }
Scott Baker24f83e22020-03-30 16:14:28 -0700871 logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Scott Bakeree7c0a02020-01-07 11:12:26 -0800872 return mcastTrafficQueues
873}
874
Manikkaraj kb1d51442019-07-23 10:41:02 -0400875func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumar8f73fe02019-12-09 13:19:37 +0000876 UsScheduler, _ := tpm.GetUsScheduler(tp)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400877
878 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
879 AllocId: tp.UsScheduler.AllocID,
880 Scheduler: UsScheduler}
881}
882
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700883func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, dir tp_pb.Direction, pbit uint32) uint32 {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400884 /*
885 Function to get the Gemport ID mapped to a pbit.
886 */
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700887 if dir == tp_pb.Direction_UPSTREAM {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400888 // upstream GEM ports
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700889 numGemPorts := len(tp.UpstreamGemPortAttributeList)
890 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
891 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
892 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
893 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
894 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
895 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
896 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
897 logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
898 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
Manikkaraj kb1d51442019-07-23 10:41:02 -0400899 }
900 }
901 }
902 }
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700903 } else if dir == tp_pb.Direction_DOWNSTREAM {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400904 //downstream GEM ports
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700905 numGemPorts := len(tp.DownstreamGemPortAttributeList)
906 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
907 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
908 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
909 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
910 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
911 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
912 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
913 logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
914 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
Manikkaraj kb1d51442019-07-23 10:41:02 -0400915 }
916 }
917 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400918 }
919 }
Scott Baker24f83e22020-03-30 16:14:28 -0700920 logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400921 return 0
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400922}
Girish Gowdra54934262019-11-13 14:19:55 +0530923
924// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
npujarec5762e2020-01-01 14:08:48 +0530925func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
Girish Gowdra54934262019-11-13 14:19:55 +0530926 var tp TechProfile
Girish Gowdra6b130582019-11-20 16:45:20 +0530927 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra54934262019-11-13 14:19:55 +0530928
npujarec5762e2020-01-01 14:08:48 +0530929 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Girish Gowdra54934262019-11-13 14:19:55 +0530930 tpInstances := make([]TechProfile, 0, len(kvPairs))
931 for kvPath, kvPair := range kvPairs {
932 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
933 if err = json.Unmarshal(value, &tp); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700934 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530935 continue
936 } else {
937 tpInstances = append(tpInstances, tp)
938 }
939 }
940 }
941 return tpInstances
942 }
943 return nil
944}