blob: d876588a8fe0428387a2f5e50dda2812de8a5529 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package techprofile
18
19import (
npujar5bf737f2020-01-16 19:35:25 +053020 "context"
Scott Baker2c1c4822019-10-16 11:02:41 -070021 "encoding/json"
22 "errors"
23 "fmt"
Girish Gowdra9447baf2019-11-05 16:42:37 +053024 "regexp"
Scott Baker2c1c4822019-10-16 11:02:41 -070025 "strconv"
Neha Sharma130ac6d2020-04-08 08:46:32 +000026 "time"
Scott Baker2c1c4822019-10-16 11:02:41 -070027
serkant.uluderyab38671c2019-11-01 09:35:38 -070028 "github.com/opencord/voltha-lib-go/v3/pkg/db"
29
30 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
31 "github.com/opencord/voltha-lib-go/v3/pkg/log"
32 tp_pb "github.com/opencord/voltha-protos/v3/go/tech_profile"
Scott Baker2c1c4822019-10-16 11:02:41 -070033)
34
35// Interface to pon resource manager APIs
36type iPonResourceMgr interface {
npujar5bf737f2020-01-16 19:35:25 +053037 GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
Scott Baker2c1c4822019-10-16 11:02:41 -070038 GetResourceTypeAllocID() string
39 GetResourceTypeGemPortID() string
40 GetTechnology() string
41}
42
43type Direction int32
44
45const (
46 Direction_UPSTREAM Direction = 0
47 Direction_DOWNSTREAM Direction = 1
48 Direction_BIDIRECTIONAL Direction = 2
49)
50
51var Direction_name = map[Direction]string{
52 0: "UPSTREAM",
53 1: "DOWNSTREAM",
54 2: "BIDIRECTIONAL",
55}
56
57type SchedulingPolicy int32
58
59const (
60 SchedulingPolicy_WRR SchedulingPolicy = 0
61 SchedulingPolicy_StrictPriority SchedulingPolicy = 1
62 SchedulingPolicy_Hybrid SchedulingPolicy = 2
63)
64
65var SchedulingPolicy_name = map[SchedulingPolicy]string{
66 0: "WRR",
67 1: "StrictPriority",
68 2: "Hybrid",
69}
70
71type AdditionalBW int32
72
73const (
74 AdditionalBW_AdditionalBW_None AdditionalBW = 0
75 AdditionalBW_AdditionalBW_NA AdditionalBW = 1
76 AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2
77 AdditionalBW_AdditionalBW_Auto AdditionalBW = 3
78)
79
80var AdditionalBW_name = map[AdditionalBW]string{
81 0: "AdditionalBW_None",
82 1: "AdditionalBW_NA",
83 2: "AdditionalBW_BestEffort",
84 3: "AdditionalBW_Auto",
85}
86
87type DiscardPolicy int32
88
89const (
90 DiscardPolicy_TailDrop DiscardPolicy = 0
91 DiscardPolicy_WTailDrop DiscardPolicy = 1
92 DiscardPolicy_Red DiscardPolicy = 2
93 DiscardPolicy_WRed DiscardPolicy = 3
94)
95
96var DiscardPolicy_name = map[DiscardPolicy]string{
97 0: "TailDrop",
98 1: "WTailDrop",
99 2: "Red",
100 3: "WRed",
101}
102
Girish Gowdra9447baf2019-11-05 16:42:37 +0530103// Required uniPortName format
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700104var uniPortNameFormat = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530105
Scott Baker2c1c4822019-10-16 11:02:41 -0700106/*
107type InferredAdditionBWIndication int32
108
109const (
110 InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0
111 InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1
112 InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2
113)
114
115var InferredAdditionBWIndication_name = map[int32]string{
116 0: "InferredAdditionBWIndication_None",
117 1: "InferredAdditionBWIndication_Assured",
118 2: "InferredAdditionBWIndication_BestEffort",
119}
120*/
121// instance control defaults
122const (
123 defaultOnuInstance = "multi-instance"
124 defaultUniInstance = "single-instance"
Scott Baker2c1c4822019-10-16 11:02:41 -0700125 defaultGemPayloadSize = "auto"
126)
127
128const MAX_GEM_PAYLOAD = "max_gem_payload_size"
129
130type InstanceControl struct {
131 Onu string `json:"ONU"`
132 Uni string `json:"uni"`
133 MaxGemPayloadSize string `json:"max_gem_payload_size"`
134}
135
136// default discard config constants
137const (
138 defaultMinThreshold = 0
139 defaultMaxThreshold = 0
140 defaultMaxProbability = 0
141)
142
143type DiscardConfig struct {
144 MinThreshold int `json:"min_threshold"`
145 MaxThreshold int `json:"max_threshold"`
146 MaxProbability int `json:"max_probability"`
147}
148
149// default scheduler contants
150const (
151 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
152 defaultPriority = 0
153 defaultWeight = 0
154 defaultQueueSchedPolicy = SchedulingPolicy_Hybrid
155)
156
157type Scheduler struct {
158 Direction string `json:"direction"`
159 AdditionalBw string `json:"additional_bw"`
160 Priority uint32 `json:"priority"`
161 Weight uint32 `json:"weight"`
162 QSchedPolicy string `json:"q_sched_policy"`
163}
164
165// default GEM attribute constants
166const (
Esin Karaman8aa75a72019-12-20 13:11:59 +0000167 defaultAESEncryption = "True"
168 defaultPriorityQueue = 0
169 defaultQueueWeight = 0
170 defaultMaxQueueSize = "auto"
171 defaultdropPolicy = DiscardPolicy_TailDrop
172 defaultSchedulePolicy = SchedulingPolicy_WRR
173 defaultIsMulticast = "False"
174 defaultAccessControlList = "224.0.0.0-239.255.255.255"
175 defaultMcastGemID = 4069
Scott Baker2c1c4822019-10-16 11:02:41 -0700176)
177
178type GemPortAttribute struct {
179 MaxQueueSize string `json:"max_q_size"`
180 PbitMap string `json:"pbit_map"`
181 AesEncryption string `json:"aes_encryption"`
182 SchedulingPolicy string `json:"scheduling_policy"`
183 PriorityQueue uint32 `json:"priority_q"`
184 Weight uint32 `json:"weight"`
185 DiscardPolicy string `json:"discard_policy"`
186 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000187 IsMulticast string `json:"is_multicast"`
188 DControlList string `json:"dynamic_access_control_list"`
189 SControlList string `json:"static_access_control_list"`
190 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700191}
192
193type iScheduler struct {
194 AllocID uint32 `json:"alloc_id"`
195 Direction string `json:"direction"`
196 AdditionalBw string `json:"additional_bw"`
197 Priority uint32 `json:"priority"`
198 Weight uint32 `json:"weight"`
199 QSchedPolicy string `json:"q_sched_policy"`
200}
201type iGemPortAttribute struct {
202 GemportID uint32 `json:"gemport_id"`
203 MaxQueueSize string `json:"max_q_size"`
204 PbitMap string `json:"pbit_map"`
205 AesEncryption string `json:"aes_encryption"`
206 SchedulingPolicy string `json:"scheduling_policy"`
207 PriorityQueue uint32 `json:"priority_q"`
208 Weight uint32 `json:"weight"`
209 DiscardPolicy string `json:"discard_policy"`
210 DiscardConfig DiscardConfig `json:"discard_config"`
Esin Karaman8aa75a72019-12-20 13:11:59 +0000211 IsMulticast string `json:"is_multicast"`
212 DControlList string `json:"dynamic_access_control_list"`
213 SControlList string `json:"static_access_control_list"`
214 McastGemID uint32 `json:"multicast_gem_id"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700215}
216
217type TechProfileMgr struct {
218 config *TechProfileFlags
219 resourceMgr iPonResourceMgr
220}
221type DefaultTechProfile struct {
222 Name string `json:"name"`
223 ProfileType string `json:"profile_type"`
224 Version int `json:"version"`
225 NumGemPorts uint32 `json:"num_gem_ports"`
226 InstanceCtrl InstanceControl `json:"instance_control"`
227 UsScheduler Scheduler `json:"us_scheduler"`
228 DsScheduler Scheduler `json:"ds_scheduler"`
229 UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"`
230 DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"`
231}
232type TechProfile struct {
233 Name string `json:"name"`
234 SubscriberIdentifier string `json:"subscriber_identifier"`
235 ProfileType string `json:"profile_type"`
236 Version int `json:"version"`
237 NumGemPorts uint32 `json:"num_gem_ports"`
Scott Baker2c1c4822019-10-16 11:02:41 -0700238 InstanceCtrl InstanceControl `json:"instance_control"`
239 UsScheduler iScheduler `json:"us_scheduler"`
240 DsScheduler iScheduler `json:"ds_scheduler"`
241 UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"`
242 DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
243}
244
Neha Sharma3c425fb2020-06-08 16:42:32 +0000245func (t *TechProfileMgr) SetKVClient(ctx context.Context) *db.Backend {
246 kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700247 if err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000248 logger.Errorw(ctx, "failed-to-create-kv-client",
Scott Baker2c1c4822019-10-16 11:02:41 -0700249 log.Fields{
Neha Sharmadd9af392020-04-28 09:03:57 +0000250 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700251 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
252 "error": err.Error(),
253 })
254 return nil
255 }
sbarbari1e3e29c2019-11-05 10:06:50 -0500256 return &db.Backend{
Scott Baker2c1c4822019-10-16 11:02:41 -0700257 Client: kvClient,
258 StoreType: t.config.KVStoreType,
Neha Sharmadd9af392020-04-28 09:03:57 +0000259 Address: t.config.KVStoreAddress,
Scott Baker2c1c4822019-10-16 11:02:41 -0700260 Timeout: t.config.KVStoreTimeout,
261 PathPrefix: t.config.TPKVPathPrefix}
262
263 /* TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
264 issue between kv store and backend , core is not calling NewBackend directly
265 kv := model.NewBackend(t.config.KVStoreType, t.config.KVStoreHost, t.config.KVStorePort,
266 t.config.KVStoreTimeout, kvStoreTechProfilePathPrefix)
267 */
268}
269
Neha Sharma3c425fb2020-06-08 16:42:32 +0000270func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700271
Neha Sharma3c425fb2020-06-08 16:42:32 +0000272 logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
Scott Baker2c1c4822019-10-16 11:02:41 -0700273 switch storeType {
274 case "consul":
Neha Sharma3c425fb2020-06-08 16:42:32 +0000275 return kvstore.NewConsulClient(ctx, address, timeout)
Scott Baker2c1c4822019-10-16 11:02:41 -0700276 case "etcd":
Neha Sharma3c425fb2020-06-08 16:42:32 +0000277 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
Scott Baker2c1c4822019-10-16 11:02:41 -0700278 }
279 return nil, errors.New("unsupported-kv-store")
280}
281
Neha Sharma3c425fb2020-06-08 16:42:32 +0000282func NewTechProfile(ctx context.Context, resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700283 var techprofileObj TechProfileMgr
Neha Sharma3c425fb2020-06-08 16:42:32 +0000284 logger.Debug(ctx, "Initializing techprofile Manager")
Neha Sharmadd9af392020-04-28 09:03:57 +0000285 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
Neha Sharma3c425fb2020-06-08 16:42:32 +0000286 techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx)
Scott Baker2c1c4822019-10-16 11:02:41 -0700287 if techprofileObj.config.KVBackend == nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000288 logger.Error(ctx, "Failed to initialize KV backend\n")
Scott Baker2c1c4822019-10-16 11:02:41 -0700289 return nil, errors.New("KV backend init failed")
290 }
291 techprofileObj.resourceMgr = resourceMgr
Neha Sharma3c425fb2020-06-08 16:42:32 +0000292 logger.Debug(ctx, "Initializing techprofile object instance success")
Scott Baker2c1c4822019-10-16 11:02:41 -0700293 return &techprofileObj, nil
294}
295
Neha Sharma3c425fb2020-06-08 16:42:32 +0000296func (t *TechProfileMgr) GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string {
297 logger.Debugw(ctx, "get-tp-instance-kv-path", log.Fields{
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700298 "uniPortName": uniPortName,
299 "tpId": techProfiletblID,
300 })
Scott Baker2c1c4822019-10-16 11:02:41 -0700301 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
302}
303
npujar5bf737f2020-01-16 19:35:25 +0530304func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700305 var KvTpIns TechProfile
306 var resPtr *TechProfile = &KvTpIns
307 var err error
Girish Gowdra9447baf2019-11-05 16:42:37 +0530308 var kvResult *kvstore.KVPair
Girish Gowdra9447baf2019-11-05 16:42:37 +0530309
Neha Sharma3c425fb2020-06-08 16:42:32 +0000310 logger.Infow(ctx, "get-tp-instance-form-kv-store", log.Fields{"path": path, "tpid": techProfiletblID})
Matteo Scandolo4fca23a2020-04-07 07:55:08 -0700311
npujar5bf737f2020-01-16 19:35:25 +0530312 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530313 if kvResult == nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000314 logger.Infow(ctx, "tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530315 return nil, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700316 } else {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530317 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700318 if err = json.Unmarshal(value, resPtr); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000319 logger.Errorw(ctx, "error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530320 return nil, errors.New("error-unmarshal-kv-result")
321 } else {
322 return resPtr, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700323 }
324 }
325 }
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530326 return nil, err
Scott Baker2c1c4822019-10-16 11:02:41 -0700327}
328
npujar5bf737f2020-01-16 19:35:25 +0530329func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000330 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
331 logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700332 tpInstanceJson, err := json.Marshal(*tpInstance)
333 if err == nil {
334 // Backend will convert JSON byte array into string format
Neha Sharma3c425fb2020-06-08 16:42:32 +0000335 logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujar5bf737f2020-01-16 19:35:25 +0530336 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Scott Baker2c1c4822019-10-16 11:02:41 -0700337 } else {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000338 logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Scott Baker2c1c4822019-10-16 11:02:41 -0700339 }
340 return err
341}
npujar5bf737f2020-01-16 19:35:25 +0530342func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700343 var kvtechprofile DefaultTechProfile
344 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Neha Sharma3c425fb2020-06-08 16:42:32 +0000345 logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujar5bf737f2020-01-16 19:35:25 +0530346 kvresult, err := t.config.KVBackend.Get(ctx, key)
Scott Baker2c1c4822019-10-16 11:02:41 -0700347 if err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000348 logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
Scott Baker2c1c4822019-10-16 11:02:41 -0700349 return nil
350 }
351 if kvresult != nil {
352 /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
353 if value, err := kvstore.ToByte(kvresult.Value); err == nil {
Girish Kumarb3c52d52019-12-06 12:14:14 +0000354 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000355 logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000356 return nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700357 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000358
Neha Sharma3c425fb2020-06-08 16:42:32 +0000359 logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000360 return &kvtechprofile
Scott Baker2c1c4822019-10-16 11:02:41 -0700361 }
362 }
363 return nil
364}
Girish Kumarb3c52d52019-12-06 12:14:14 +0000365
npujar5bf737f2020-01-16 19:35:25 +0530366func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700367 var tpInstance *TechProfile
Neha Sharma3c425fb2020-06-08 16:42:32 +0000368 logger.Infow(ctx, "creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530369
370 // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
371 if !uniPortNameFormat.Match([]byte(uniPortName)) {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000372 logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000373 return nil, errors.New("uni-port-name-not-confirming-to-format")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530374 }
375
npujar5bf737f2020-01-16 19:35:25 +0530376 tp := t.getTPFromKVStore(ctx, techProfiletblID)
Scott Baker2c1c4822019-10-16 11:02:41 -0700377 if tp != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000378 if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
379 logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
380 tp = t.getDefaultTechProfile(ctx)
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530381 } else {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000382 logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530383 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700384 } else {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000385 logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
386 tp = t.getDefaultTechProfile(ctx)
Scott Baker2c1c4822019-10-16 11:02:41 -0700387 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000388 tpInstancePath := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530389 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000390 logger.Error(ctx, "tp-intance-allocation-failed")
Girish Kumarb3c52d52019-12-06 12:14:14 +0000391 return nil, errors.New("tp-intance-allocation-failed")
Scott Baker2c1c4822019-10-16 11:02:41 -0700392 }
npujar5bf737f2020-01-16 19:35:25 +0530393 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000394 logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000395 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530396 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000397 logger.Infow(ctx, "tp-added-to-kv-store-successfully",
Scott Baker2c1c4822019-10-16 11:02:41 -0700398 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000399 return tpInstance, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700400}
401
npujar5bf737f2020-01-16 19:35:25 +0530402func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000403 path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
npujar5bf737f2020-01-16 19:35:25 +0530404 return t.config.KVBackend.Delete(ctx, path)
Scott Baker2c1c4822019-10-16 11:02:41 -0700405}
406
Neha Sharma3c425fb2020-06-08 16:42:32 +0000407func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl InstanceControl) error {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530408 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000409 logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530410 return errors.New("invalid-onu-instance-ctl-attr")
411 }
412
413 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000414 logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530415 return errors.New("invalid-uni-instance-ctl-attr")
416 }
417
418 if instCtl.Uni == "multi-instance" {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000419 logger.Error(ctx, "uni-multi-instance-tp-not-supported")
Girish Gowdra9447baf2019-11-05 16:42:37 +0530420 return errors.New("uni-multi-instance-tp-not-supported")
421 }
422
423 return nil
424}
425
npujar5bf737f2020-01-16 19:35:25 +0530426func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700427
428 var usGemPortAttributeList []iGemPortAttribute
429 var dsGemPortAttributeList []iGemPortAttribute
Esin Karaman8aa75a72019-12-20 13:11:59 +0000430 var dsMulticastGemAttributeList []iGemPortAttribute
431 var dsUnicastGemAttributeList []iGemPortAttribute
Scott Baker2c1c4822019-10-16 11:02:41 -0700432 var tcontIDs []uint32
433 var gemPorts []uint32
434 var err error
435
Neha Sharma3c425fb2020-06-08 16:42:32 +0000436 logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530437
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530438 if tp.InstanceCtrl.Onu == "multi-instance" {
npujar5bf737f2020-01-16 19:35:25 +0530439 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000440 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530441 return nil
442 }
443 } else { // "single-instance"
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800444 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000445 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
David K. Bainbridge7c75cac2020-02-19 08:53:46 -0800446 return nil
447 } else if tpInst == nil {
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530448 // No "single-instance" tp found on one any uni port for the given TP ID
449 // Allocate a new TcontID or AllocID
npujar5bf737f2020-01-16 19:35:25 +0530450 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000451 logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530452 return nil
453 }
454 } else {
455 // Use the alloc-id from the existing TpInstance
456 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
457 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700458 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000459 logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
npujar5bf737f2020-01-16 19:35:25 +0530460 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000461 logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700462 return nil
463 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000464 logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Scott Baker2c1c4822019-10-16 11:02:41 -0700465 for index := 0; index < int(tp.NumGemPorts); index++ {
466 usGemPortAttributeList = append(usGemPortAttributeList,
467 iGemPortAttribute{GemportID: gemPorts[index],
468 MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize,
469 PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap,
470 AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption,
471 SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy,
472 PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue,
473 Weight: tp.UpstreamGemPortAttributeList[index].Weight,
474 DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy,
475 DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000476 }
477
Neha Sharma3c425fb2020-06-08 16:42:32 +0000478 logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Esin Karaman8aa75a72019-12-20 13:11:59 +0000479 //put multicast and unicast downstream GEM port attributes in different lists first
480 for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
481 if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
482 dsMulticastGemAttributeList = append(dsMulticastGemAttributeList,
483 iGemPortAttribute{
484 McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID,
485 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
486 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
487 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
488 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
489 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
490 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
491 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
492 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig,
493 IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast,
494 DControlList: tp.DownstreamGemPortAttributeList[index].DControlList,
495 SControlList: tp.DownstreamGemPortAttributeList[index].SControlList})
496 } else {
497 dsUnicastGemAttributeList = append(dsUnicastGemAttributeList,
498 iGemPortAttribute{
499 MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize,
500 PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap,
501 AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption,
502 SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy,
503 PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue,
504 Weight: tp.DownstreamGemPortAttributeList[index].Weight,
505 DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy,
506 DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig})
507 }
508 }
509 //add unicast downstream GEM ports to dsGemPortAttributeList
510 for index := 0; index < int(tp.NumGemPorts); index++ {
Scott Baker2c1c4822019-10-16 11:02:41 -0700511 dsGemPortAttributeList = append(dsGemPortAttributeList,
512 iGemPortAttribute{GemportID: gemPorts[index],
Esin Karaman8aa75a72019-12-20 13:11:59 +0000513 MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize,
514 PbitMap: dsUnicastGemAttributeList[index].PbitMap,
515 AesEncryption: dsUnicastGemAttributeList[index].AesEncryption,
516 SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy,
517 PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue,
518 Weight: dsUnicastGemAttributeList[index].Weight,
519 DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy,
520 DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig})
Scott Baker2c1c4822019-10-16 11:02:41 -0700521 }
Esin Karaman8aa75a72019-12-20 13:11:59 +0000522 //add multicast GEM ports to dsGemPortAttributeList afterwards
523 for k := range dsMulticastGemAttributeList {
524 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
525 }
526
Scott Baker2c1c4822019-10-16 11:02:41 -0700527 return &TechProfile{
528 SubscriberIdentifier: uniPortName,
529 Name: tp.Name,
530 ProfileType: tp.ProfileType,
531 Version: tp.Version,
532 NumGemPorts: tp.NumGemPorts,
Scott Baker2c1c4822019-10-16 11:02:41 -0700533 InstanceCtrl: tp.InstanceCtrl,
534 UsScheduler: iScheduler{
535 AllocID: tcontIDs[0],
536 Direction: tp.UsScheduler.Direction,
537 AdditionalBw: tp.UsScheduler.AdditionalBw,
538 Priority: tp.UsScheduler.Priority,
539 Weight: tp.UsScheduler.Weight,
540 QSchedPolicy: tp.UsScheduler.QSchedPolicy},
541 DsScheduler: iScheduler{
542 AllocID: tcontIDs[0],
543 Direction: tp.DsScheduler.Direction,
544 AdditionalBw: tp.DsScheduler.AdditionalBw,
545 Priority: tp.DsScheduler.Priority,
546 Weight: tp.DsScheduler.Weight,
547 QSchedPolicy: tp.DsScheduler.QSchedPolicy},
548 UpstreamGemPortAttributeList: usGemPortAttributeList,
549 DownstreamGemPortAttributeList: dsGemPortAttributeList}
550}
551
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530552// getSingleInstanceTp returns another TpInstance for an ONU on a different
553// uni port for the same TP ID, if it finds one, else nil.
npujar5bf737f2020-01-16 19:35:25 +0530554func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530555 var tpInst TechProfile
Girish Gowdra9447baf2019-11-05 16:42:37 +0530556
557 // For example:
558 // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}"
559 // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""]
560 uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2)
npujar5bf737f2020-01-16 19:35:25 +0530561 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra9447baf2019-11-05 16:42:37 +0530562
Girish Gowdra9447baf2019-11-05 16:42:37 +0530563 // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID
564 for keyPath, kvPair := range kvPairs {
565 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
566 if err = json.Unmarshal(value, &tpInst); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000567 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530568 return nil, errors.New("error-unmarshal-kv-pair")
569 } else {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000570 logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra32f0eff2019-11-17 09:53:29 +0530571 return &tpInst, nil
Girish Gowdra9447baf2019-11-05 16:42:37 +0530572 }
573 }
574 }
Girish Gowdra9447baf2019-11-05 16:42:37 +0530575 return nil, nil
576}
577
Neha Sharma3c425fb2020-06-08 16:42:32 +0000578func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *DefaultTechProfile {
Scott Baker2c1c4822019-10-16 11:02:41 -0700579
580 var usGemPortAttributeList []GemPortAttribute
581 var dsGemPortAttributeList []GemPortAttribute
582
583 for _, pbit := range t.config.DefaultPbits {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000584 logger.Debugw(ctx, "Creating GEM port", log.Fields{"pbit": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700585 usGemPortAttributeList = append(usGemPortAttributeList,
586 GemPortAttribute{
587 MaxQueueSize: defaultMaxQueueSize,
588 PbitMap: pbit,
589 AesEncryption: defaultAESEncryption,
590 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
591 PriorityQueue: defaultPriorityQueue,
592 Weight: defaultQueueWeight,
593 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
594 DiscardConfig: DiscardConfig{
595 MinThreshold: defaultMinThreshold,
596 MaxThreshold: defaultMaxThreshold,
597 MaxProbability: defaultMaxProbability}})
598 dsGemPortAttributeList = append(dsGemPortAttributeList,
599 GemPortAttribute{
600 MaxQueueSize: defaultMaxQueueSize,
601 PbitMap: pbit,
602 AesEncryption: defaultAESEncryption,
603 SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy],
604 PriorityQueue: defaultPriorityQueue,
605 Weight: defaultQueueWeight,
606 DiscardPolicy: DiscardPolicy_name[defaultdropPolicy],
607 DiscardConfig: DiscardConfig{
608 MinThreshold: defaultMinThreshold,
609 MaxThreshold: defaultMaxThreshold,
Esin Karaman8aa75a72019-12-20 13:11:59 +0000610 MaxProbability: defaultMaxProbability},
611 IsMulticast: defaultIsMulticast,
612 DControlList: defaultAccessControlList,
613 SControlList: defaultAccessControlList,
614 McastGemID: defaultMcastGemID})
Scott Baker2c1c4822019-10-16 11:02:41 -0700615 }
616 return &DefaultTechProfile{
617 Name: t.config.DefaultTPName,
618 ProfileType: t.resourceMgr.GetTechnology(),
619 Version: t.config.TPVersion,
620 NumGemPorts: uint32(len(usGemPortAttributeList)),
621 InstanceCtrl: InstanceControl{
622 Onu: defaultOnuInstance,
623 Uni: defaultUniInstance,
624 MaxGemPayloadSize: defaultGemPayloadSize},
625 UsScheduler: Scheduler{
626 Direction: Direction_name[Direction_UPSTREAM],
627 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
628 Priority: defaultPriority,
629 Weight: defaultWeight,
630 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
631 DsScheduler: Scheduler{
632 Direction: Direction_name[Direction_DOWNSTREAM],
633 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
634 Priority: defaultPriority,
635 Weight: defaultWeight,
636 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
637 UpstreamGemPortAttributeList: usGemPortAttributeList,
638 DownstreamGemPortAttributeList: dsGemPortAttributeList}
639}
640
Neha Sharma3c425fb2020-06-08 16:42:32 +0000641func (t *TechProfileMgr) GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32 {
Scott Baker2c1c4822019-10-16 11:02:41 -0700642 var result int32 = -1
643
644 if paramType == "direction" {
645 for key, val := range tp_pb.Direction_value {
646 if key == paramKey {
647 result = val
648 }
649 }
650 } else if paramType == "discard_policy" {
651 for key, val := range tp_pb.DiscardPolicy_value {
652 if key == paramKey {
653 result = val
654 }
655 }
656 } else if paramType == "sched_policy" {
657 for key, val := range tp_pb.SchedulingPolicy_value {
658 if key == paramKey {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000659 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": key, "value": val})
Scott Baker2c1c4822019-10-16 11:02:41 -0700660 result = val
661 }
662 }
663 } else if paramType == "additional_bw" {
664 for key, val := range tp_pb.AdditionalBW_value {
665 if key == paramKey {
666 result = val
667 }
668 }
669 } else {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000670 logger.Error(ctx, "Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Scott Baker2c1c4822019-10-16 11:02:41 -0700671 return -1
672 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000673 logger.Debugw(ctx, "Got value in proto", log.Fields{"key": paramKey, "value": result})
Scott Baker2c1c4822019-10-16 11:02:41 -0700674 return result
675}
676
Neha Sharma3c425fb2020-06-08 16:42:32 +0000677func (t *TechProfileMgr) GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
678 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.UsScheduler.Direction))
Scott Baker2c1c4822019-10-16 11:02:41 -0700679 if dir == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000680 logger.Errorf(ctx, "Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000681 return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Scott Baker2c1c4822019-10-16 11:02:41 -0700682 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000683
Neha Sharma3c425fb2020-06-08 16:42:32 +0000684 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.UsScheduler.AdditionalBw))
Scott Baker2c1c4822019-10-16 11:02:41 -0700685 if bw == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000686 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000687 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Scott Baker2c1c4822019-10-16 11:02:41 -0700688 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000689
Neha Sharma3c425fb2020-06-08 16:42:32 +0000690 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Scott Baker2c1c4822019-10-16 11:02:41 -0700691 if policy == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000692 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000693 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Scott Baker2c1c4822019-10-16 11:02:41 -0700694 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000695
Scott Baker2c1c4822019-10-16 11:02:41 -0700696 return &tp_pb.SchedulerConfig{
697 Direction: dir,
698 AdditionalBw: bw,
699 Priority: tpInstance.UsScheduler.Priority,
700 Weight: tpInstance.UsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000701 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700702}
703
Neha Sharma3c425fb2020-06-08 16:42:32 +0000704func (t *TechProfileMgr) GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700705
Neha Sharma3c425fb2020-06-08 16:42:32 +0000706 dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.DsScheduler.Direction))
Scott Baker2c1c4822019-10-16 11:02:41 -0700707 if dir == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000708 logger.Errorf(ctx, "Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000709 return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Scott Baker2c1c4822019-10-16 11:02:41 -0700710 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000711
Neha Sharma3c425fb2020-06-08 16:42:32 +0000712 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.DsScheduler.AdditionalBw))
Scott Baker2c1c4822019-10-16 11:02:41 -0700713 if bw == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000714 logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000715 return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Scott Baker2c1c4822019-10-16 11:02:41 -0700716 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000717
Neha Sharma3c425fb2020-06-08 16:42:32 +0000718 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Scott Baker2c1c4822019-10-16 11:02:41 -0700719 if policy == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000720 logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000721 return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Scott Baker2c1c4822019-10-16 11:02:41 -0700722 }
723
724 return &tp_pb.SchedulerConfig{
725 Direction: dir,
726 AdditionalBw: bw,
727 Priority: tpInstance.DsScheduler.Priority,
728 Weight: tpInstance.DsScheduler.Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000729 SchedPolicy: policy}, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700730}
731
732func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
733 ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler {
734
735 tSched := &tp_pb.TrafficScheduler{
736 Direction: SchedCfg.Direction,
737 AllocId: tpInstance.UsScheduler.AllocID,
738 TrafficShapingInfo: ShapingCfg,
739 Scheduler: SchedCfg}
740
741 return tSched
742}
743
Neha Sharma3c425fb2020-06-08 16:42:32 +0000744func (tpm *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700745
746 var encryp bool
747 if Dir == tp_pb.Direction_UPSTREAM {
748 // upstream GEM ports
749 NumGemPorts := len(tp.UpstreamGemPortAttributeList)
750 GemPorts := make([]*tp_pb.TrafficQueue, 0)
751 for Count := 0; Count < NumGemPorts; Count++ {
752 if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" {
753 encryp = true
754 } else {
755 encryp = false
756 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000757
Neha Sharma3c425fb2020-06-08 16:42:32 +0000758 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000759 if schedPolicy == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000760 logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000761 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
762 }
763
Neha Sharma3c425fb2020-06-08 16:42:32 +0000764 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000765 if discardPolicy == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000766 logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000767 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
768 }
769
Scott Baker2c1c4822019-10-16 11:02:41 -0700770 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
Neha Sharma3c425fb2020-06-08 16:42:32 +0000771 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.UsScheduler.Direction)),
Scott Baker2c1c4822019-10-16 11:02:41 -0700772 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
773 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
774 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000775 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700776 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
777 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000778 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700779 })
780 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000781 logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000782 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700783 } else if Dir == tp_pb.Direction_DOWNSTREAM {
784 //downstream GEM ports
785 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
786 GemPorts := make([]*tp_pb.TrafficQueue, 0)
787 for Count := 0; Count < NumGemPorts; Count++ {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000788 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
789 //do not take multicast GEM ports. They are handled separately.
790 continue
791 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700792 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
793 encryp = true
794 } else {
795 encryp = false
796 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000797
Neha Sharma3c425fb2020-06-08 16:42:32 +0000798 schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000799 if schedPolicy == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000800 logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000801 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
802 }
803
Neha Sharma3c425fb2020-06-08 16:42:32 +0000804 discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000805 if discardPolicy == -1 {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000806 logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000807 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
808 }
809
Scott Baker2c1c4822019-10-16 11:02:41 -0700810 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
Neha Sharma3c425fb2020-06-08 16:42:32 +0000811 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
Scott Baker2c1c4822019-10-16 11:02:41 -0700812 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
813 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
814 AesEncryption: encryp,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000815 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700816 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
817 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumarb3c52d52019-12-06 12:14:14 +0000818 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Scott Baker2c1c4822019-10-16 11:02:41 -0700819 })
820 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000821 logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumarb3c52d52019-12-06 12:14:14 +0000822 return GemPorts, nil
Scott Baker2c1c4822019-10-16 11:02:41 -0700823 }
Girish Kumarb3c52d52019-12-06 12:14:14 +0000824
Neha Sharma3c425fb2020-06-08 16:42:32 +0000825 logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumarb3c52d52019-12-06 12:14:14 +0000826 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Scott Baker2c1c4822019-10-16 11:02:41 -0700827}
828
Esin Karaman8aa75a72019-12-20 13:11:59 +0000829//isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise
830func isMulticastGem(isMulticastAttrValue string) bool {
831 return isMulticastAttrValue != "" &&
832 (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
833}
834
Neha Sharma3c425fb2020-06-08 16:42:32 +0000835func (tpm *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue {
Esin Karaman8aa75a72019-12-20 13:11:59 +0000836 var encryp bool
837 NumGemPorts := len(tp.DownstreamGemPortAttributeList)
838 mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
839 for Count := 0; Count < NumGemPorts; Count++ {
840 if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
841 continue
842 }
843 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
844 encryp = true
845 } else {
846 encryp = false
847 }
848 mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
Neha Sharma3c425fb2020-06-08 16:42:32 +0000849 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
Esin Karaman8aa75a72019-12-20 13:11:59 +0000850 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
851 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
852 AesEncryption: encryp,
Neha Sharma3c425fb2020-06-08 16:42:32 +0000853 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
Esin Karaman8aa75a72019-12-20 13:11:59 +0000854 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
855 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Neha Sharma3c425fb2020-06-08 16:42:32 +0000856 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
Esin Karaman8aa75a72019-12-20 13:11:59 +0000857 })
858 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000859 logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Esin Karaman8aa75a72019-12-20 13:11:59 +0000860 return mcastTrafficQueues
861}
862
Neha Sharma3c425fb2020-06-08 16:42:32 +0000863func (tpm *TechProfileMgr) GetUsTrafficScheduler(ctx context.Context, tp *TechProfile) *tp_pb.TrafficScheduler {
864 UsScheduler, _ := tpm.GetUsScheduler(ctx, tp)
Scott Baker2c1c4822019-10-16 11:02:41 -0700865
866 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
867 AllocId: tp.UsScheduler.AllocID,
868 Scheduler: UsScheduler}
869}
870
Neha Sharma3c425fb2020-06-08 16:42:32 +0000871func (t *TechProfileMgr) GetGemportIDForPbit(ctx context.Context, tp *TechProfile, dir tp_pb.Direction, pbit uint32) uint32 {
Scott Baker2c1c4822019-10-16 11:02:41 -0700872 /*
873 Function to get the Gemport ID mapped to a pbit.
874 */
Girish Gowdra79661202020-05-12 13:42:26 -0700875 if dir == tp_pb.Direction_UPSTREAM {
Scott Baker2c1c4822019-10-16 11:02:41 -0700876 // upstream GEM ports
Girish Gowdra79661202020-05-12 13:42:26 -0700877 numGemPorts := len(tp.UpstreamGemPortAttributeList)
878 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
879 lenOfPbitMap := len(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap)
880 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
881 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
882 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
883 if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
884 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma3c425fb2020-06-08 16:42:32 +0000885 logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
Girish Gowdra79661202020-05-12 13:42:26 -0700886 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
Scott Baker2c1c4822019-10-16 11:02:41 -0700887 }
888 }
889 }
890 }
Girish Gowdra79661202020-05-12 13:42:26 -0700891 } else if dir == tp_pb.Direction_DOWNSTREAM {
Scott Baker2c1c4822019-10-16 11:02:41 -0700892 //downstream GEM ports
Girish Gowdra79661202020-05-12 13:42:26 -0700893 numGemPorts := len(tp.DownstreamGemPortAttributeList)
894 for gemCnt := 0; gemCnt < numGemPorts; gemCnt++ {
895 lenOfPbitMap := len(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap)
896 for pbitMapIdx := 2; pbitMapIdx < lenOfPbitMap; pbitMapIdx++ {
897 // Given a sample pbit map string "0b00000001", lenOfPbitMap is 10
898 // "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
899 if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
900 if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
Neha Sharma3c425fb2020-06-08 16:42:32 +0000901 logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
Girish Gowdra79661202020-05-12 13:42:26 -0700902 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
Scott Baker2c1c4822019-10-16 11:02:41 -0700903 }
904 }
905 }
906 }
907 }
Neha Sharma3c425fb2020-06-08 16:42:32 +0000908 logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Scott Baker2c1c4822019-10-16 11:02:41 -0700909 return 0
910}
Girish Gowdra9447baf2019-11-05 16:42:37 +0530911
912// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
npujar5bf737f2020-01-16 19:35:25 +0530913func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530914 var tp TechProfile
Girish Gowdra6172a2e2019-11-27 14:37:58 +0530915 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra9447baf2019-11-05 16:42:37 +0530916
npujar5bf737f2020-01-16 19:35:25 +0530917 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Girish Gowdra9447baf2019-11-05 16:42:37 +0530918 tpInstances := make([]TechProfile, 0, len(kvPairs))
919 for kvPath, kvPair := range kvPairs {
920 if value, err := kvstore.ToByte(kvPair.Value); err == nil {
921 if err = json.Unmarshal(value, &tp); err != nil {
Neha Sharma3c425fb2020-06-08 16:42:32 +0000922 logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Girish Gowdra9447baf2019-11-05 16:42:37 +0530923 continue
924 } else {
925 tpInstances = append(tpInstances, tp)
926 }
927 }
928 }
929 return tpInstances
930 }
931 return nil
932}