blob: afe5c0945ab1c1fba8387c1224a9ec2405e3fba5 [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"
Neha Sharmacc656962020-04-14 14:26:11 +000026 "time"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040027
Esin Karamanccb714b2019-11-29 15:02:06 +000028 "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"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040033)
34
35// Interface to pon resource manager APIs
36type iPonResourceMgr interface {
npujarec5762e2020-01-01 14:08:48 +053037 GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error)
Matt Jeanneretcab955f2019-04-10 15:45:57 -040038 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 Gowdra54934262019-11-13 14:19:55 +0530103// Required uniPortName format
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700104var uniPortNameFormat = regexp.MustCompile(`^olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
Girish Gowdra54934262019-11-13 14:19:55 +0530105
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400106/*
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"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400125 defaultGemPayloadSize = "auto"
126)
127
128const MAX_GEM_PAYLOAD = "max_gem_payload_size"
129
130type InstanceControl struct {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400131 Onu string `json:"ONU"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400132 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 (
kdarapub26b4502019-10-05 03:02:33 +0530151 defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400152 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 (
Scott Bakeree7c0a02020-01-07 11:12:26 -0800167 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
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400176)
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"`
Manikkaraj kb1d51442019-07-23 10:41:02 -0400183 PriorityQueue uint32 `json:"priority_q"`
184 Weight uint32 `json:"weight"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400185 DiscardPolicy string `json:"discard_policy"`
186 DiscardConfig DiscardConfig `json:"discard_config"`
Scott Bakeree7c0a02020-01-07 11:12:26 -0800187 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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400191}
192
193type iScheduler struct {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400194 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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400200}
201type iGemPortAttribute struct {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400202 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"`
Manikkaraj kb1d51442019-07-23 10:41:02 -0400207 PriorityQueue uint32 `json:"priority_q"`
208 Weight uint32 `json:"weight"`
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400209 DiscardPolicy string `json:"discard_policy"`
210 DiscardConfig DiscardConfig `json:"discard_config"`
Scott Bakeree7c0a02020-01-07 11:12:26 -0800211 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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400215}
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"`
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400238 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
sbarbaria8910ba2019-11-05 10:12:23 -0500245func (t *TechProfileMgr) SetKVClient() *db.Backend {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000246 kvClient, err := newKVClient(t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400247 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700248 logger.Errorw("failed-to-create-kv-client",
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400249 log.Fields{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000250 "type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400251 "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
252 "error": err.Error(),
253 })
254 return nil
255 }
sbarbaria8910ba2019-11-05 10:12:23 -0500256 return &db.Backend{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400257 Client: kvClient,
258 StoreType: t.config.KVStoreType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000259 Address: t.config.KVStoreAddress,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400260 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 Sharmacc656962020-04-14 14:26:11 +0000270func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400271
Scott Baker24f83e22020-03-30 16:14:28 -0700272 logger.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400273 switch storeType {
274 case "consul":
275 return kvstore.NewConsulClient(address, timeout)
276 case "etcd":
Scott Bakered4a8e72020-04-17 11:10:20 -0700277 return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400278 }
279 return nil, errors.New("unsupported-kv-store")
280}
281
Neha Sharma3f221ae2020-04-29 19:02:12 +0000282func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400283 var techprofileObj TechProfileMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700284 logger.Debug("Initializing techprofile Manager")
Neha Sharma3f221ae2020-04-29 19:02:12 +0000285 techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400286 techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
287 if techprofileObj.config.KVBackend == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700288 logger.Error("Failed to initialize KV backend\n")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400289 return nil, errors.New("KV backend init failed")
290 }
291 techprofileObj.resourceMgr = resourceMgr
Scott Baker24f83e22020-03-30 16:14:28 -0700292 logger.Debug("Initializing techprofile object instance success")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400293 return &techprofileObj, nil
294}
295
296func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700297 logger.Debugw("get-tp-instance-kv-path", log.Fields{
298 "uniPortName": uniPortName,
299 "tpId": techProfiletblID,
300 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400301 return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName)
302}
303
npujarec5762e2020-01-01 14:08:48 +0530304func (t *TechProfileMgr) GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400305 var KvTpIns TechProfile
306 var resPtr *TechProfile = &KvTpIns
307 var err error
Girish Gowdra54934262019-11-13 14:19:55 +0530308 var kvResult *kvstore.KVPair
309
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700310 logger.Infow("get-tp-instance-form-kv-store", log.Fields{"path": path, "tpid": techProfiletblID})
311
npujarec5762e2020-01-01 14:08:48 +0530312 kvResult, _ = t.config.KVBackend.Get(ctx, path)
Girish Gowdra54934262019-11-13 14:19:55 +0530313 if kvResult == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700314 logger.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
Girish Gowdra54934262019-11-13 14:19:55 +0530315 return nil, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400316 } else {
Girish Gowdra54934262019-11-13 14:19:55 +0530317 if value, err := kvstore.ToByte(kvResult.Value); err == nil {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400318 if err = json.Unmarshal(value, resPtr); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700319 logger.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530320 return nil, errors.New("error-unmarshal-kv-result")
321 } else {
322 return resPtr, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400323 }
324 }
325 }
Girish Gowdra54934262019-11-13 14:19:55 +0530326 return nil, err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400327}
328
npujarec5762e2020-01-01 14:08:48 +0530329func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400330 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
Scott Baker24f83e22020-03-30 16:14:28 -0700331 logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400332 tpInstanceJson, err := json.Marshal(*tpInstance)
333 if err == nil {
334 // Backend will convert JSON byte array into string format
Scott Baker24f83e22020-03-30 16:14:28 -0700335 logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
npujarec5762e2020-01-01 14:08:48 +0530336 err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400337 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700338 logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400339 }
340 return err
341}
npujarec5762e2020-01-01 14:08:48 +0530342func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400343 var kvtechprofile DefaultTechProfile
344 key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
Scott Baker24f83e22020-03-30 16:14:28 -0700345 logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
npujarec5762e2020-01-01 14:08:48 +0530346 kvresult, err := t.config.KVBackend.Get(ctx, key)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400347 if err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700348 logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400349 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 Kumar8f73fe02019-12-09 13:19:37 +0000354 if err = json.Unmarshal(value, &kvtechprofile); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700355 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 +0000356 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400357 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000358
Scott Baker24f83e22020-03-30 16:14:28 -0700359 logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000360 return &kvtechprofile
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400361 }
362 }
363 return nil
364}
Girish Kumar8f73fe02019-12-09 13:19:37 +0000365
npujarec5762e2020-01-01 14:08:48 +0530366func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400367 var tpInstance *TechProfile
Scott Baker24f83e22020-03-30 16:14:28 -0700368 logger.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +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)) {
Scott Baker24f83e22020-03-30 16:14:28 -0700372 logger.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000373 return nil, errors.New("uni-port-name-not-confirming-to-format")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400374 }
Girish Gowdra54934262019-11-13 14:19:55 +0530375
npujarec5762e2020-01-01 14:08:48 +0530376 tp := t.getTPFromKVStore(ctx, techProfiletblID)
Girish Gowdra54934262019-11-13 14:19:55 +0530377 if tp != nil {
378 if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700379 logger.Error("invalid-instance-ctrl-attr--using-default-tp")
Girish Gowdra54934262019-11-13 14:19:55 +0530380 tp = t.getDefaultTechProfile()
381 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700382 logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
Girish Gowdra54934262019-11-13 14:19:55 +0530383 }
384 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700385 logger.Info("tp-not-found-on-kv--creating-default-tp")
Girish Gowdra54934262019-11-13 14:19:55 +0530386 tp = t.getDefaultTechProfile()
387 }
388 tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujarec5762e2020-01-01 14:08:48 +0530389 if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700390 logger.Error("tp-intance-allocation-failed")
Girish Kumar8f73fe02019-12-09 13:19:37 +0000391 return nil, errors.New("tp-intance-allocation-failed")
Girish Gowdra54934262019-11-13 14:19:55 +0530392 }
npujarec5762e2020-01-01 14:08:48 +0530393 if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700394 logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000395 return nil, errors.New("error-adding-tp-to-kv-store")
Girish Gowdra54934262019-11-13 14:19:55 +0530396 }
Scott Baker24f83e22020-03-30 16:14:28 -0700397 logger.Infow("tp-added-to-kv-store-successfully",
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400398 log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000399 return tpInstance, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400400}
401
npujarec5762e2020-01-01 14:08:48 +0530402func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400403 path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
npujarec5762e2020-01-01 14:08:48 +0530404 return t.config.KVBackend.Delete(ctx, path)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400405}
406
Girish Gowdra54934262019-11-13 14:19:55 +0530407func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
408 if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700409 logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
Girish Gowdra54934262019-11-13 14:19:55 +0530410 return errors.New("invalid-onu-instance-ctl-attr")
411 }
412
413 if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700414 logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
Girish Gowdra54934262019-11-13 14:19:55 +0530415 return errors.New("invalid-uni-instance-ctl-attr")
416 }
417
418 if instCtl.Uni == "multi-instance" {
Scott Baker24f83e22020-03-30 16:14:28 -0700419 logger.Error("uni-multi-instance-tp-not-supported")
Girish Gowdra54934262019-11-13 14:19:55 +0530420 return errors.New("uni-multi-instance-tp-not-supported")
421 }
422
423 return nil
424}
425
npujarec5762e2020-01-01 14:08:48 +0530426func (t *TechProfileMgr) allocateTPInstance(ctx context.Context, uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400427
428 var usGemPortAttributeList []iGemPortAttribute
429 var dsGemPortAttributeList []iGemPortAttribute
Scott Bakeree7c0a02020-01-07 11:12:26 -0800430 var dsMulticastGemAttributeList []iGemPortAttribute
431 var dsUnicastGemAttributeList []iGemPortAttribute
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400432 var tcontIDs []uint32
433 var gemPorts []uint32
434 var err error
435
Scott Baker24f83e22020-03-30 16:14:28 -0700436 logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
Girish Gowdra54934262019-11-13 14:19:55 +0530437
438 if tp.InstanceCtrl.Onu == "multi-instance" {
npujarec5762e2020-01-01 14:08:48 +0530439 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700440 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530441 return nil
442 }
443 } else { // "single-instance"
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000444 if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700445 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000446 return nil
447 } else if tpInst == nil {
Girish Gowdra54934262019-11-13 14:19:55 +0530448 // No "single-instance" tp found on one any uni port for the given TP ID
449 // Allocate a new TcontID or AllocID
npujarec5762e2020-01-01 14:08:48 +0530450 if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700451 logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
Girish Gowdra54934262019-11-13 14:19:55 +0530452 return nil
453 }
454 } else {
455 // Use the alloc-id from the existing TpInstance
456 tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
457 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400458 }
Scott Baker24f83e22020-03-30 16:14:28 -0700459 logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
npujarec5762e2020-01-01 14:08:48 +0530460 if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
Scott Baker24f83e22020-03-30 16:14:28 -0700461 logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400462 return nil
463 }
Scott Baker24f83e22020-03-30 16:14:28 -0700464 logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400465 for index := 0; index < int(tp.NumGemPorts); index++ {
466 usGemPortAttributeList = append(usGemPortAttributeList,
467 iGemPortAttribute{GemportID: gemPorts[index],
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400468 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})
Scott Bakeree7c0a02020-01-07 11:12:26 -0800476 }
477
Scott Baker24f83e22020-03-30 16:14:28 -0700478 logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
Scott Bakeree7c0a02020-01-07 11:12:26 -0800479 //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++ {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400511 dsGemPortAttributeList = append(dsGemPortAttributeList,
512 iGemPortAttribute{GemportID: gemPorts[index],
Scott Bakeree7c0a02020-01-07 11:12:26 -0800513 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})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400521 }
Scott Bakeree7c0a02020-01-07 11:12:26 -0800522 //add multicast GEM ports to dsGemPortAttributeList afterwards
523 for k := range dsMulticastGemAttributeList {
524 dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k])
525 }
526
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400527 return &TechProfile{
528 SubscriberIdentifier: uniPortName,
529 Name: tp.Name,
530 ProfileType: tp.ProfileType,
531 Version: tp.Version,
532 NumGemPorts: tp.NumGemPorts,
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400533 InstanceCtrl: tp.InstanceCtrl,
534 UsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400535 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},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400541 DsScheduler: iScheduler{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400542 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},
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400548 UpstreamGemPortAttributeList: usGemPortAttributeList,
549 DownstreamGemPortAttributeList: dsGemPortAttributeList}
550}
551
Girish Gowdra54934262019-11-13 14:19:55 +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.
npujarec5762e2020-01-01 14:08:48 +0530554func (t *TechProfileMgr) getSingleInstanceTp(ctx context.Context, tpPath string) (*TechProfile, error) {
Girish Gowdra54934262019-11-13 14:19:55 +0530555 var tpInst TechProfile
556
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)
npujarec5762e2020-01-01 14:08:48 +0530561 kvPairs, _ := t.config.KVBackend.List(ctx, uniPathSlice[0])
Girish Gowdra54934262019-11-13 14:19:55 +0530562
563 // 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 {
Scott Baker24f83e22020-03-30 16:14:28 -0700567 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530568 return nil, errors.New("error-unmarshal-kv-pair")
569 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700570 logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
Girish Gowdra54934262019-11-13 14:19:55 +0530571 return &tpInst, nil
572 }
573 }
574 }
575 return nil, nil
576}
577
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400578func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
579
580 var usGemPortAttributeList []GemPortAttribute
581 var dsGemPortAttributeList []GemPortAttribute
582
583 for _, pbit := range t.config.DefaultPbits {
Scott Baker24f83e22020-03-30 16:14:28 -0700584 logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400585 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,
Scott Bakeree7c0a02020-01-07 11:12:26 -0800610 MaxProbability: defaultMaxProbability},
611 IsMulticast: defaultIsMulticast,
612 DControlList: defaultAccessControlList,
613 SControlList: defaultAccessControlList,
614 McastGemID: defaultMcastGemID})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400615 }
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],
kdarapub26b4502019-10-05 03:02:33 +0530627 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400628 Priority: defaultPriority,
629 Weight: defaultWeight,
630 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
631 DsScheduler: Scheduler{
632 Direction: Direction_name[Direction_DOWNSTREAM],
kdarapub26b4502019-10-05 03:02:33 +0530633 AdditionalBw: AdditionalBW_name[defaultAdditionalBw],
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400634 Priority: defaultPriority,
635 Weight: defaultWeight,
636 QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]},
637 UpstreamGemPortAttributeList: usGemPortAttributeList,
638 DownstreamGemPortAttributeList: dsGemPortAttributeList}
639}
640
641func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
642 var result int32 = -1
643
644 if paramType == "direction" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400645 for key, val := range tp_pb.Direction_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400646 if key == paramKey {
647 result = val
648 }
649 }
650 } else if paramType == "discard_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400651 for key, val := range tp_pb.DiscardPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400652 if key == paramKey {
653 result = val
654 }
655 }
656 } else if paramType == "sched_policy" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400657 for key, val := range tp_pb.SchedulingPolicy_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400658 if key == paramKey {
Scott Baker24f83e22020-03-30 16:14:28 -0700659 logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400660 result = val
661 }
662 }
663 } else if paramType == "additional_bw" {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400664 for key, val := range tp_pb.AdditionalBW_value {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400665 if key == paramKey {
666 result = val
667 }
668 }
669 } else {
Scott Baker24f83e22020-03-30 16:14:28 -0700670 logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400671 return -1
672 }
Scott Baker24f83e22020-03-30 16:14:28 -0700673 logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400674 return result
675}
676
Girish Kumar8f73fe02019-12-09 13:19:37 +0000677func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400678 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400679 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700680 logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000681 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 -0400682 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000683
Manikkaraj kb1d51442019-07-23 10:41:02 -0400684 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400685 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700686 logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000687 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 -0400688 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000689
Manikkaraj kb1d51442019-07-23 10:41:02 -0400690 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400691 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700692 logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000693 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 -0400694 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000695
Manikkaraj kb1d51442019-07-23 10:41:02 -0400696 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400697 Direction: dir,
698 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400699 Priority: tpInstance.UsScheduler.Priority,
700 Weight: tpInstance.UsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000701 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400702}
703
Girish Kumar8f73fe02019-12-09 13:19:37 +0000704func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400705
Manikkaraj kb1d51442019-07-23 10:41:02 -0400706 dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400707 if dir == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700708 logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000709 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 -0400710 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000711
Manikkaraj kb1d51442019-07-23 10:41:02 -0400712 bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400713 if bw == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700714 logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000715 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 -0400716 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000717
Manikkaraj kb1d51442019-07-23 10:41:02 -0400718 policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400719 if policy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700720 logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000721 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 -0400722 }
723
Manikkaraj kb1d51442019-07-23 10:41:02 -0400724 return &tp_pb.SchedulerConfig{
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400725 Direction: dir,
726 AdditionalBw: bw,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400727 Priority: tpInstance.DsScheduler.Priority,
728 Weight: tpInstance.DsScheduler.Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000729 SchedPolicy: policy}, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400730}
731
Manikkaraj kb1d51442019-07-23 10:41:02 -0400732func (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
Girish Kumar8f73fe02019-12-09 13:19:37 +0000744func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400745
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 Kumar8f73fe02019-12-09 13:19:37 +0000757
758 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
759 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700760 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 +0000761 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
762 }
763
764 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
765 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700766 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 +0000767 return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
768 }
769
Manikkaraj kb1d51442019-07-23 10:41:02 -0400770 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
771 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
772 GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
773 PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
774 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000775 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400776 Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue,
777 Weight: tp.UpstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000778 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400779 })
780 }
Scott Baker24f83e22020-03-30 16:14:28 -0700781 logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000782 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -0400783 } 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++ {
Scott Bakeree7c0a02020-01-07 11:12:26 -0800788 if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) {
789 //do not take multicast GEM ports. They are handled separately.
790 continue
791 }
Manikkaraj kb1d51442019-07-23 10:41:02 -0400792 if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" {
793 encryp = true
794 } else {
795 encryp = false
796 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000797
798 schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
799 if schedPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700800 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 +0000801 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
802 }
803
804 discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
805 if discardPolicy == -1 {
Scott Baker24f83e22020-03-30 16:14:28 -0700806 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 +0000807 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
808 }
809
Manikkaraj kb1d51442019-07-23 10:41:02 -0400810 GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
811 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
812 GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
813 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
814 AesEncryption: encryp,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000815 SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400816 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
817 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
Girish Kumar8f73fe02019-12-09 13:19:37 +0000818 DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400819 })
820 }
Scott Baker24f83e22020-03-30 16:14:28 -0700821 logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
Girish Kumar8f73fe02019-12-09 13:19:37 +0000822 return GemPorts, nil
Manikkaraj kb1d51442019-07-23 10:41:02 -0400823 }
Girish Kumar8f73fe02019-12-09 13:19:37 +0000824
Scott Baker24f83e22020-03-30 16:14:28 -0700825 logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
Girish Kumar8f73fe02019-12-09 13:19:37 +0000826 return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400827}
828
Scott Bakeree7c0a02020-01-07 11:12:26 -0800829//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
835func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
836 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{
849 Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
850 GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
851 PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
852 AesEncryption: encryp,
853 SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
854 Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
855 Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
856 DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
857 })
858 }
Scott Baker24f83e22020-03-30 16:14:28 -0700859 logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
Scott Bakeree7c0a02020-01-07 11:12:26 -0800860 return mcastTrafficQueues
861}
862
Manikkaraj kb1d51442019-07-23 10:41:02 -0400863func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
Girish Kumar8f73fe02019-12-09 13:19:37 +0000864 UsScheduler, _ := tpm.GetUsScheduler(tp)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400865
866 return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
867 AllocId: tp.UsScheduler.AllocID,
868 Scheduler: UsScheduler}
869}
870
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700871func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, dir tp_pb.Direction, pbit uint32) uint32 {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400872 /*
873 Function to get the Gemport ID mapped to a pbit.
874 */
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700875 if dir == tp_pb.Direction_UPSTREAM {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400876 // upstream GEM ports
Girish Gowdraf369c0a2020-05-15 13:27:38 -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
885 logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
886 return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
Manikkaraj kb1d51442019-07-23 10:41:02 -0400887 }
888 }
889 }
890 }
Girish Gowdraf369c0a2020-05-15 13:27:38 -0700891 } else if dir == tp_pb.Direction_DOWNSTREAM {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400892 //downstream GEM ports
Girish Gowdraf369c0a2020-05-15 13:27:38 -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
901 logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
902 return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
Manikkaraj kb1d51442019-07-23 10:41:02 -0400903 }
904 }
905 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400906 }
907 }
Scott Baker24f83e22020-03-30 16:14:28 -0700908 logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400909 return 0
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400910}
Girish Gowdra54934262019-11-13 14:19:55 +0530911
912// FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID.
npujarec5762e2020-01-01 14:08:48 +0530913func (t *TechProfileMgr) FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile {
Girish Gowdra54934262019-11-13 14:19:55 +0530914 var tp TechProfile
Girish Gowdra6b130582019-11-20 16:45:20 +0530915 onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID)
Girish Gowdra54934262019-11-13 14:19:55 +0530916
npujarec5762e2020-01-01 14:08:48 +0530917 if kvPairs, _ := t.config.KVBackend.List(ctx, onuTpInstancePath); kvPairs != nil {
Girish Gowdra54934262019-11-13 14:19:55 +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 {
Scott Baker24f83e22020-03-30 16:14:28 -0700922 logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
Girish Gowdra54934262019-11-13 14:19:55 +0530923 continue
924 } else {
925 tpInstances = append(tpInstances, tp)
926 }
927 }
928 }
929 return tpInstances
930 }
931 return nil
932}