Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package techprofile |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "errors" |
| 22 | "fmt" |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 23 | "regexp" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | "strconv" |
| 25 | |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v2/pkg/db" |
Scott Baker | ce76700 | 2019-10-23 13:30:24 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
Scott Baker | ce76700 | 2019-10-23 13:30:24 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 29 | tp_pb "github.com/opencord/voltha-protos/v2/go/tech_profile" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // Interface to pon resource manager APIs |
| 33 | type iPonResourceMgr interface { |
| 34 | GetResourceID(IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error) |
| 35 | GetResourceTypeAllocID() string |
| 36 | GetResourceTypeGemPortID() string |
| 37 | GetTechnology() string |
| 38 | } |
| 39 | |
| 40 | type Direction int32 |
| 41 | |
| 42 | const ( |
| 43 | Direction_UPSTREAM Direction = 0 |
| 44 | Direction_DOWNSTREAM Direction = 1 |
| 45 | Direction_BIDIRECTIONAL Direction = 2 |
| 46 | ) |
| 47 | |
| 48 | var Direction_name = map[Direction]string{ |
| 49 | 0: "UPSTREAM", |
| 50 | 1: "DOWNSTREAM", |
| 51 | 2: "BIDIRECTIONAL", |
| 52 | } |
| 53 | |
| 54 | type SchedulingPolicy int32 |
| 55 | |
| 56 | const ( |
| 57 | SchedulingPolicy_WRR SchedulingPolicy = 0 |
| 58 | SchedulingPolicy_StrictPriority SchedulingPolicy = 1 |
| 59 | SchedulingPolicy_Hybrid SchedulingPolicy = 2 |
| 60 | ) |
| 61 | |
| 62 | var SchedulingPolicy_name = map[SchedulingPolicy]string{ |
| 63 | 0: "WRR", |
| 64 | 1: "StrictPriority", |
| 65 | 2: "Hybrid", |
| 66 | } |
| 67 | |
| 68 | type AdditionalBW int32 |
| 69 | |
| 70 | const ( |
| 71 | AdditionalBW_AdditionalBW_None AdditionalBW = 0 |
| 72 | AdditionalBW_AdditionalBW_NA AdditionalBW = 1 |
| 73 | AdditionalBW_AdditionalBW_BestEffort AdditionalBW = 2 |
| 74 | AdditionalBW_AdditionalBW_Auto AdditionalBW = 3 |
| 75 | ) |
| 76 | |
| 77 | var AdditionalBW_name = map[AdditionalBW]string{ |
| 78 | 0: "AdditionalBW_None", |
| 79 | 1: "AdditionalBW_NA", |
| 80 | 2: "AdditionalBW_BestEffort", |
| 81 | 3: "AdditionalBW_Auto", |
| 82 | } |
| 83 | |
| 84 | type DiscardPolicy int32 |
| 85 | |
| 86 | const ( |
| 87 | DiscardPolicy_TailDrop DiscardPolicy = 0 |
| 88 | DiscardPolicy_WTailDrop DiscardPolicy = 1 |
| 89 | DiscardPolicy_Red DiscardPolicy = 2 |
| 90 | DiscardPolicy_WRed DiscardPolicy = 3 |
| 91 | ) |
| 92 | |
| 93 | var DiscardPolicy_name = map[DiscardPolicy]string{ |
| 94 | 0: "TailDrop", |
| 95 | 1: "WTailDrop", |
| 96 | 2: "Red", |
| 97 | 3: "WRed", |
| 98 | } |
| 99 | |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 100 | // Required uniPortName format |
| 101 | var uniPortNameFormat = regexp.MustCompile(`^pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`) |
| 102 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 103 | /* |
| 104 | type InferredAdditionBWIndication int32 |
| 105 | |
| 106 | const ( |
| 107 | InferredAdditionBWIndication_InferredAdditionBWIndication_None InferredAdditionBWIndication = 0 |
| 108 | InferredAdditionBWIndication_InferredAdditionBWIndication_Assured InferredAdditionBWIndication = 1 |
| 109 | InferredAdditionBWIndication_InferredAdditionBWIndication_BestEffort InferredAdditionBWIndication = 2 |
| 110 | ) |
| 111 | |
| 112 | var InferredAdditionBWIndication_name = map[int32]string{ |
| 113 | 0: "InferredAdditionBWIndication_None", |
| 114 | 1: "InferredAdditionBWIndication_Assured", |
| 115 | 2: "InferredAdditionBWIndication_BestEffort", |
| 116 | } |
| 117 | */ |
| 118 | // instance control defaults |
| 119 | const ( |
| 120 | defaultOnuInstance = "multi-instance" |
| 121 | defaultUniInstance = "single-instance" |
| 122 | defaultNumGemPorts = 1 |
| 123 | defaultGemPayloadSize = "auto" |
| 124 | ) |
| 125 | |
| 126 | const MAX_GEM_PAYLOAD = "max_gem_payload_size" |
| 127 | |
| 128 | type InstanceControl struct { |
| 129 | Onu string `json:"ONU"` |
| 130 | Uni string `json:"uni"` |
| 131 | MaxGemPayloadSize string `json:"max_gem_payload_size"` |
| 132 | } |
| 133 | |
| 134 | // default discard config constants |
| 135 | const ( |
| 136 | defaultMinThreshold = 0 |
| 137 | defaultMaxThreshold = 0 |
| 138 | defaultMaxProbability = 0 |
| 139 | ) |
| 140 | |
| 141 | type DiscardConfig struct { |
| 142 | MinThreshold int `json:"min_threshold"` |
| 143 | MaxThreshold int `json:"max_threshold"` |
| 144 | MaxProbability int `json:"max_probability"` |
| 145 | } |
| 146 | |
| 147 | // default scheduler contants |
| 148 | const ( |
| 149 | defaultAdditionalBw = AdditionalBW_AdditionalBW_BestEffort |
| 150 | defaultPriority = 0 |
| 151 | defaultWeight = 0 |
| 152 | defaultQueueSchedPolicy = SchedulingPolicy_Hybrid |
| 153 | ) |
| 154 | |
| 155 | type Scheduler struct { |
| 156 | Direction string `json:"direction"` |
| 157 | AdditionalBw string `json:"additional_bw"` |
| 158 | Priority uint32 `json:"priority"` |
| 159 | Weight uint32 `json:"weight"` |
| 160 | QSchedPolicy string `json:"q_sched_policy"` |
| 161 | } |
| 162 | |
| 163 | // default GEM attribute constants |
| 164 | const ( |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 165 | defaultAESEncryption = "True" |
| 166 | defaultPriorityQueue = 0 |
| 167 | defaultQueueWeight = 0 |
| 168 | defaultMaxQueueSize = "auto" |
| 169 | defaultdropPolicy = DiscardPolicy_TailDrop |
| 170 | defaultSchedulePolicy = SchedulingPolicy_WRR |
| 171 | defaultIsMulticast = "False" |
| 172 | defaultAccessControlList = "224.0.0.0-239.255.255.255" |
| 173 | defaultMcastGemID = 4069 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 174 | ) |
| 175 | |
| 176 | type GemPortAttribute struct { |
| 177 | MaxQueueSize string `json:"max_q_size"` |
| 178 | PbitMap string `json:"pbit_map"` |
| 179 | AesEncryption string `json:"aes_encryption"` |
| 180 | SchedulingPolicy string `json:"scheduling_policy"` |
| 181 | PriorityQueue uint32 `json:"priority_q"` |
| 182 | Weight uint32 `json:"weight"` |
| 183 | DiscardPolicy string `json:"discard_policy"` |
| 184 | DiscardConfig DiscardConfig `json:"discard_config"` |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 185 | IsMulticast string `json:"is_multicast"` |
| 186 | DControlList string `json:"dynamic_access_control_list"` |
| 187 | SControlList string `json:"static_access_control_list"` |
| 188 | McastGemID uint32 `json:"multicast_gem_id"` |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | type iScheduler struct { |
| 192 | AllocID uint32 `json:"alloc_id"` |
| 193 | Direction string `json:"direction"` |
| 194 | AdditionalBw string `json:"additional_bw"` |
| 195 | Priority uint32 `json:"priority"` |
| 196 | Weight uint32 `json:"weight"` |
| 197 | QSchedPolicy string `json:"q_sched_policy"` |
| 198 | } |
| 199 | type iGemPortAttribute struct { |
| 200 | GemportID uint32 `json:"gemport_id"` |
| 201 | MaxQueueSize string `json:"max_q_size"` |
| 202 | PbitMap string `json:"pbit_map"` |
| 203 | AesEncryption string `json:"aes_encryption"` |
| 204 | SchedulingPolicy string `json:"scheduling_policy"` |
| 205 | PriorityQueue uint32 `json:"priority_q"` |
| 206 | Weight uint32 `json:"weight"` |
| 207 | DiscardPolicy string `json:"discard_policy"` |
| 208 | DiscardConfig DiscardConfig `json:"discard_config"` |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 209 | IsMulticast string `json:"is_multicast"` |
| 210 | DControlList string `json:"dynamic_access_control_list"` |
| 211 | SControlList string `json:"static_access_control_list"` |
| 212 | McastGemID uint32 `json:"multicast_gem_id"` |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | type TechProfileMgr struct { |
| 216 | config *TechProfileFlags |
| 217 | resourceMgr iPonResourceMgr |
| 218 | } |
| 219 | type DefaultTechProfile struct { |
| 220 | Name string `json:"name"` |
| 221 | ProfileType string `json:"profile_type"` |
| 222 | Version int `json:"version"` |
| 223 | NumGemPorts uint32 `json:"num_gem_ports"` |
| 224 | InstanceCtrl InstanceControl `json:"instance_control"` |
| 225 | UsScheduler Scheduler `json:"us_scheduler"` |
| 226 | DsScheduler Scheduler `json:"ds_scheduler"` |
| 227 | UpstreamGemPortAttributeList []GemPortAttribute `json:"upstream_gem_port_attribute_list"` |
| 228 | DownstreamGemPortAttributeList []GemPortAttribute `json:"downstream_gem_port_attribute_list"` |
| 229 | } |
| 230 | type TechProfile struct { |
| 231 | Name string `json:"name"` |
| 232 | SubscriberIdentifier string `json:"subscriber_identifier"` |
| 233 | ProfileType string `json:"profile_type"` |
| 234 | Version int `json:"version"` |
| 235 | NumGemPorts uint32 `json:"num_gem_ports"` |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 236 | InstanceCtrl InstanceControl `json:"instance_control"` |
| 237 | UsScheduler iScheduler `json:"us_scheduler"` |
| 238 | DsScheduler iScheduler `json:"ds_scheduler"` |
| 239 | UpstreamGemPortAttributeList []iGemPortAttribute `json:"upstream_gem_port_attribute_list"` |
| 240 | DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"` |
| 241 | } |
| 242 | |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 243 | func (t *TechProfileMgr) SetKVClient() *db.Backend { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 244 | addr := t.config.KVStoreHost + ":" + strconv.Itoa(t.config.KVStorePort) |
| 245 | kvClient, err := newKVClient(t.config.KVStoreType, addr, t.config.KVStoreTimeout) |
| 246 | if err != nil { |
| 247 | log.Errorw("failed-to-create-kv-client", |
| 248 | log.Fields{ |
| 249 | "type": t.config.KVStoreType, "host": t.config.KVStoreHost, "port": t.config.KVStorePort, |
| 250 | "timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix, |
| 251 | "error": err.Error(), |
| 252 | }) |
| 253 | return nil |
| 254 | } |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 255 | return &db.Backend{ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 256 | Client: kvClient, |
| 257 | StoreType: t.config.KVStoreType, |
| 258 | Host: t.config.KVStoreHost, |
| 259 | Port: t.config.KVStorePort, |
| 260 | 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 | |
| 270 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
| 271 | |
| 272 | log.Infow("kv-store", log.Fields{"storeType": storeType, "address": address}) |
| 273 | switch storeType { |
| 274 | case "consul": |
| 275 | return kvstore.NewConsulClient(address, timeout) |
| 276 | case "etcd": |
| 277 | return kvstore.NewEtcdClient(address, timeout) |
| 278 | } |
| 279 | return nil, errors.New("unsupported-kv-store") |
| 280 | } |
| 281 | |
| 282 | func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreHost string, KVStorePort int) (*TechProfileMgr, error) { |
| 283 | var techprofileObj TechProfileMgr |
| 284 | log.Debug("Initializing techprofile Manager") |
| 285 | techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreHost, KVStorePort) |
| 286 | techprofileObj.config.KVBackend = techprofileObj.SetKVClient() |
| 287 | if techprofileObj.config.KVBackend == nil { |
| 288 | log.Error("Failed to initialize KV backend\n") |
| 289 | return nil, errors.New("KV backend init failed") |
| 290 | } |
| 291 | techprofileObj.resourceMgr = resourceMgr |
| 292 | log.Debug("Initializing techprofile object instance success") |
| 293 | return &techprofileObj, nil |
| 294 | } |
| 295 | |
| 296 | func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string { |
| 297 | return fmt.Sprintf(t.config.TPInstanceKVPath, t.resourceMgr.GetTechnology(), techProfiletblID, uniPortName) |
| 298 | } |
| 299 | |
| 300 | func (t *TechProfileMgr) GetTPInstanceFromKVStore(techProfiletblID uint32, path string) (*TechProfile, error) { |
| 301 | var KvTpIns TechProfile |
| 302 | var resPtr *TechProfile = &KvTpIns |
| 303 | var err error |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 304 | var kvResult *kvstore.KVPair |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 305 | |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 306 | kvResult, _ = t.config.KVBackend.Get(path) |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 307 | if kvResult == nil { |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 308 | log.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path}) |
| 309 | return nil, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 310 | } else { |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 311 | if value, err := kvstore.ToByte(kvResult.Value); err == nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 312 | if err = json.Unmarshal(value, resPtr); err != nil { |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 313 | log.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value}) |
| 314 | return nil, errors.New("error-unmarshal-kv-result") |
| 315 | } else { |
| 316 | return resPtr, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 320 | return nil, err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | func (t *TechProfileMgr) addTechProfInstanceToKVStore(techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error { |
| 324 | path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName) |
| 325 | log.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance}) |
| 326 | tpInstanceJson, err := json.Marshal(*tpInstance) |
| 327 | if err == nil { |
| 328 | // Backend will convert JSON byte array into string format |
| 329 | log.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson}) |
| 330 | err = t.config.KVBackend.Put(path, tpInstanceJson) |
| 331 | } else { |
| 332 | log.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance}) |
| 333 | } |
| 334 | return err |
| 335 | } |
| 336 | func (t *TechProfileMgr) getTPFromKVStore(techProfiletblID uint32) *DefaultTechProfile { |
| 337 | var kvtechprofile DefaultTechProfile |
| 338 | key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID) |
| 339 | log.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key}) |
| 340 | kvresult, err := t.config.KVBackend.Get(key) |
| 341 | if err != nil { |
| 342 | log.Errorw("Error while fetching value from KV store", log.Fields{"key": key}) |
| 343 | return nil |
| 344 | } |
| 345 | if kvresult != nil { |
| 346 | /* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/ |
| 347 | if value, err := kvstore.ToByte(kvresult.Value); err == nil { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 348 | if err = json.Unmarshal(value, &kvtechprofile); err != nil { |
| 349 | log.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value}) |
| 350 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 351 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 352 | |
| 353 | log.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile}) |
| 354 | return &kvtechprofile |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | return nil |
| 358 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 359 | |
| 360 | func (t *TechProfileMgr) CreateTechProfInstance(techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 361 | var tpInstance *TechProfile |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 362 | log.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId}) |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 363 | |
| 364 | // Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+} |
| 365 | if !uniPortNameFormat.Match([]byte(uniPortName)) { |
| 366 | log.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName}) |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 367 | return nil, errors.New("uni-port-name-not-confirming-to-format") |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 368 | } |
| 369 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 370 | tp := t.getTPFromKVStore(techProfiletblID) |
| 371 | if tp != nil { |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 372 | if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil { |
| 373 | log.Error("invalid-instance-ctrl-attr--using-default-tp") |
| 374 | tp = t.getDefaultTechProfile() |
| 375 | } else { |
| 376 | log.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID}) |
| 377 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 378 | } else { |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 379 | log.Info("tp-not-found-on-kv--creating-default-tp") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 380 | tp = t.getDefaultTechProfile() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 381 | } |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 382 | tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName) |
| 383 | if tpInstance = t.allocateTPInstance(uniPortName, tp, intfId, tpInstancePath); tpInstance == nil { |
| 384 | log.Error("tp-intance-allocation-failed") |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 385 | return nil, errors.New("tp-intance-allocation-failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 386 | } |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 387 | if err := t.addTechProfInstanceToKVStore(techProfiletblID, uniPortName, tpInstance); err != nil { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 388 | log.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName}) |
| 389 | return nil, errors.New("error-adding-tp-to-kv-store") |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 390 | } |
| 391 | log.Infow("tp-added-to-kv-store-successfully", |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 392 | log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId}) |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 393 | return tpInstance, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | func (t *TechProfileMgr) DeleteTechProfileInstance(techProfiletblID uint32, uniPortName string) error { |
| 397 | path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName) |
| 398 | return t.config.KVBackend.Delete(path) |
| 399 | } |
| 400 | |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 401 | func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error { |
| 402 | if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" { |
| 403 | log.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu}) |
| 404 | return errors.New("invalid-onu-instance-ctl-attr") |
| 405 | } |
| 406 | |
| 407 | if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" { |
| 408 | log.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni}) |
| 409 | return errors.New("invalid-uni-instance-ctl-attr") |
| 410 | } |
| 411 | |
| 412 | if instCtl.Uni == "multi-instance" { |
| 413 | log.Error("uni-multi-instance-tp-not-supported") |
| 414 | return errors.New("uni-multi-instance-tp-not-supported") |
| 415 | } |
| 416 | |
| 417 | return nil |
| 418 | } |
| 419 | |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 420 | func (t *TechProfileMgr) allocateTPInstance(uniPortName string, tp *DefaultTechProfile, intfId uint32, tpInstPath string) *TechProfile { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 421 | |
| 422 | var usGemPortAttributeList []iGemPortAttribute |
| 423 | var dsGemPortAttributeList []iGemPortAttribute |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 424 | var dsMulticastGemAttributeList []iGemPortAttribute |
| 425 | var dsUnicastGemAttributeList []iGemPortAttribute |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 426 | var tcontIDs []uint32 |
| 427 | var gemPorts []uint32 |
| 428 | var err error |
| 429 | |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 430 | log.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts}) |
| 431 | |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 432 | if tp.InstanceCtrl.Onu == "multi-instance" { |
| 433 | if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil { |
| 434 | log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId}) |
| 435 | return nil |
| 436 | } |
| 437 | } else { // "single-instance" |
| 438 | tpInst, err := t.getSingleInstanceTp(tpInstPath) |
| 439 | if tpInst == nil { |
| 440 | // No "single-instance" tp found on one any uni port for the given TP ID |
| 441 | // Allocate a new TcontID or AllocID |
| 442 | if tcontIDs, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil { |
| 443 | log.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId}) |
| 444 | return nil |
| 445 | } |
| 446 | } else { |
| 447 | // Use the alloc-id from the existing TpInstance |
| 448 | tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID) |
| 449 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 450 | } |
| 451 | log.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts}) |
| 452 | if gemPorts, err = t.resourceMgr.GetResourceID(intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil { |
| 453 | log.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts}) |
| 454 | return nil |
| 455 | } |
| 456 | log.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts}) |
| 457 | for index := 0; index < int(tp.NumGemPorts); index++ { |
| 458 | usGemPortAttributeList = append(usGemPortAttributeList, |
| 459 | iGemPortAttribute{GemportID: gemPorts[index], |
| 460 | MaxQueueSize: tp.UpstreamGemPortAttributeList[index].MaxQueueSize, |
| 461 | PbitMap: tp.UpstreamGemPortAttributeList[index].PbitMap, |
| 462 | AesEncryption: tp.UpstreamGemPortAttributeList[index].AesEncryption, |
| 463 | SchedulingPolicy: tp.UpstreamGemPortAttributeList[index].SchedulingPolicy, |
| 464 | PriorityQueue: tp.UpstreamGemPortAttributeList[index].PriorityQueue, |
| 465 | Weight: tp.UpstreamGemPortAttributeList[index].Weight, |
| 466 | DiscardPolicy: tp.UpstreamGemPortAttributeList[index].DiscardPolicy, |
| 467 | DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig}) |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | log.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList)) |
| 471 | //put multicast and unicast downstream GEM port attributes in different lists first |
| 472 | for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ { |
| 473 | if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) { |
| 474 | dsMulticastGemAttributeList = append(dsMulticastGemAttributeList, |
| 475 | iGemPortAttribute{ |
| 476 | McastGemID: tp.DownstreamGemPortAttributeList[index].McastGemID, |
| 477 | MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize, |
| 478 | PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap, |
| 479 | AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption, |
| 480 | SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy, |
| 481 | PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue, |
| 482 | Weight: tp.DownstreamGemPortAttributeList[index].Weight, |
| 483 | DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy, |
| 484 | DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig, |
| 485 | IsMulticast: tp.DownstreamGemPortAttributeList[index].IsMulticast, |
| 486 | DControlList: tp.DownstreamGemPortAttributeList[index].DControlList, |
| 487 | SControlList: tp.DownstreamGemPortAttributeList[index].SControlList}) |
| 488 | } else { |
| 489 | dsUnicastGemAttributeList = append(dsUnicastGemAttributeList, |
| 490 | iGemPortAttribute{ |
| 491 | MaxQueueSize: tp.DownstreamGemPortAttributeList[index].MaxQueueSize, |
| 492 | PbitMap: tp.DownstreamGemPortAttributeList[index].PbitMap, |
| 493 | AesEncryption: tp.DownstreamGemPortAttributeList[index].AesEncryption, |
| 494 | SchedulingPolicy: tp.DownstreamGemPortAttributeList[index].SchedulingPolicy, |
| 495 | PriorityQueue: tp.DownstreamGemPortAttributeList[index].PriorityQueue, |
| 496 | Weight: tp.DownstreamGemPortAttributeList[index].Weight, |
| 497 | DiscardPolicy: tp.DownstreamGemPortAttributeList[index].DiscardPolicy, |
| 498 | DiscardConfig: tp.DownstreamGemPortAttributeList[index].DiscardConfig}) |
| 499 | } |
| 500 | } |
| 501 | //add unicast downstream GEM ports to dsGemPortAttributeList |
| 502 | for index := 0; index < int(tp.NumGemPorts); index++ { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 503 | dsGemPortAttributeList = append(dsGemPortAttributeList, |
| 504 | iGemPortAttribute{GemportID: gemPorts[index], |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 505 | MaxQueueSize: dsUnicastGemAttributeList[index].MaxQueueSize, |
| 506 | PbitMap: dsUnicastGemAttributeList[index].PbitMap, |
| 507 | AesEncryption: dsUnicastGemAttributeList[index].AesEncryption, |
| 508 | SchedulingPolicy: dsUnicastGemAttributeList[index].SchedulingPolicy, |
| 509 | PriorityQueue: dsUnicastGemAttributeList[index].PriorityQueue, |
| 510 | Weight: dsUnicastGemAttributeList[index].Weight, |
| 511 | DiscardPolicy: dsUnicastGemAttributeList[index].DiscardPolicy, |
| 512 | DiscardConfig: dsUnicastGemAttributeList[index].DiscardConfig}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 513 | } |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 514 | //add multicast GEM ports to dsGemPortAttributeList afterwards |
| 515 | for k := range dsMulticastGemAttributeList { |
| 516 | dsGemPortAttributeList = append(dsGemPortAttributeList, dsMulticastGemAttributeList[k]) |
| 517 | } |
| 518 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 519 | return &TechProfile{ |
| 520 | SubscriberIdentifier: uniPortName, |
| 521 | Name: tp.Name, |
| 522 | ProfileType: tp.ProfileType, |
| 523 | Version: tp.Version, |
| 524 | NumGemPorts: tp.NumGemPorts, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 525 | InstanceCtrl: tp.InstanceCtrl, |
| 526 | UsScheduler: iScheduler{ |
| 527 | AllocID: tcontIDs[0], |
| 528 | Direction: tp.UsScheduler.Direction, |
| 529 | AdditionalBw: tp.UsScheduler.AdditionalBw, |
| 530 | Priority: tp.UsScheduler.Priority, |
| 531 | Weight: tp.UsScheduler.Weight, |
| 532 | QSchedPolicy: tp.UsScheduler.QSchedPolicy}, |
| 533 | DsScheduler: iScheduler{ |
| 534 | AllocID: tcontIDs[0], |
| 535 | Direction: tp.DsScheduler.Direction, |
| 536 | AdditionalBw: tp.DsScheduler.AdditionalBw, |
| 537 | Priority: tp.DsScheduler.Priority, |
| 538 | Weight: tp.DsScheduler.Weight, |
| 539 | QSchedPolicy: tp.DsScheduler.QSchedPolicy}, |
| 540 | UpstreamGemPortAttributeList: usGemPortAttributeList, |
| 541 | DownstreamGemPortAttributeList: dsGemPortAttributeList} |
| 542 | } |
| 543 | |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 544 | // getSingleInstanceTp returns another TpInstance for an ONU on a different |
| 545 | // uni port for the same TP ID, if it finds one, else nil. |
| 546 | func (t *TechProfileMgr) getSingleInstanceTp(tpPath string) (*TechProfile, error) { |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 547 | var tpInst TechProfile |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 548 | |
| 549 | // For example: |
| 550 | // tpPath like "service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}/uni-{1}" |
| 551 | // is broken into ["service/voltha/technology_profiles/xgspon/64/pon-{0}/onu-{1}" ""] |
| 552 | uniPathSlice := regexp.MustCompile(`/uni-{[0-9]+}$`).Split(tpPath, 2) |
| 553 | kvPairs, _ := t.config.KVBackend.List(uniPathSlice[0]) |
| 554 | |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 555 | // Find a valid TP Instance among all the UNIs of that ONU for the given TP ID |
| 556 | for keyPath, kvPair := range kvPairs { |
| 557 | if value, err := kvstore.ToByte(kvPair.Value); err == nil { |
| 558 | if err = json.Unmarshal(value, &tpInst); err != nil { |
| 559 | log.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value}) |
| 560 | return nil, errors.New("error-unmarshal-kv-pair") |
| 561 | } else { |
| 562 | log.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath}) |
Girish Gowdra | 32f0eff | 2019-11-17 09:53:29 +0530 | [diff] [blame] | 563 | return &tpInst, nil |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 567 | return nil, nil |
| 568 | } |
| 569 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 570 | func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile { |
| 571 | |
| 572 | var usGemPortAttributeList []GemPortAttribute |
| 573 | var dsGemPortAttributeList []GemPortAttribute |
| 574 | |
| 575 | for _, pbit := range t.config.DefaultPbits { |
| 576 | log.Debugw("Creating GEM port", log.Fields{"pbit": pbit}) |
| 577 | usGemPortAttributeList = append(usGemPortAttributeList, |
| 578 | GemPortAttribute{ |
| 579 | MaxQueueSize: defaultMaxQueueSize, |
| 580 | PbitMap: pbit, |
| 581 | AesEncryption: defaultAESEncryption, |
| 582 | SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy], |
| 583 | PriorityQueue: defaultPriorityQueue, |
| 584 | Weight: defaultQueueWeight, |
| 585 | DiscardPolicy: DiscardPolicy_name[defaultdropPolicy], |
| 586 | DiscardConfig: DiscardConfig{ |
| 587 | MinThreshold: defaultMinThreshold, |
| 588 | MaxThreshold: defaultMaxThreshold, |
| 589 | MaxProbability: defaultMaxProbability}}) |
| 590 | dsGemPortAttributeList = append(dsGemPortAttributeList, |
| 591 | GemPortAttribute{ |
| 592 | MaxQueueSize: defaultMaxQueueSize, |
| 593 | PbitMap: pbit, |
| 594 | AesEncryption: defaultAESEncryption, |
| 595 | SchedulingPolicy: SchedulingPolicy_name[defaultSchedulePolicy], |
| 596 | PriorityQueue: defaultPriorityQueue, |
| 597 | Weight: defaultQueueWeight, |
| 598 | DiscardPolicy: DiscardPolicy_name[defaultdropPolicy], |
| 599 | DiscardConfig: DiscardConfig{ |
| 600 | MinThreshold: defaultMinThreshold, |
| 601 | MaxThreshold: defaultMaxThreshold, |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 602 | MaxProbability: defaultMaxProbability}, |
| 603 | IsMulticast: defaultIsMulticast, |
| 604 | DControlList: defaultAccessControlList, |
| 605 | SControlList: defaultAccessControlList, |
| 606 | McastGemID: defaultMcastGemID}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 607 | } |
| 608 | return &DefaultTechProfile{ |
| 609 | Name: t.config.DefaultTPName, |
| 610 | ProfileType: t.resourceMgr.GetTechnology(), |
| 611 | Version: t.config.TPVersion, |
| 612 | NumGemPorts: uint32(len(usGemPortAttributeList)), |
| 613 | InstanceCtrl: InstanceControl{ |
| 614 | Onu: defaultOnuInstance, |
| 615 | Uni: defaultUniInstance, |
| 616 | MaxGemPayloadSize: defaultGemPayloadSize}, |
| 617 | UsScheduler: Scheduler{ |
| 618 | Direction: Direction_name[Direction_UPSTREAM], |
| 619 | AdditionalBw: AdditionalBW_name[defaultAdditionalBw], |
| 620 | Priority: defaultPriority, |
| 621 | Weight: defaultWeight, |
| 622 | QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]}, |
| 623 | DsScheduler: Scheduler{ |
| 624 | Direction: Direction_name[Direction_DOWNSTREAM], |
| 625 | AdditionalBw: AdditionalBW_name[defaultAdditionalBw], |
| 626 | Priority: defaultPriority, |
| 627 | Weight: defaultWeight, |
| 628 | QSchedPolicy: SchedulingPolicy_name[defaultQueueSchedPolicy]}, |
| 629 | UpstreamGemPortAttributeList: usGemPortAttributeList, |
| 630 | DownstreamGemPortAttributeList: dsGemPortAttributeList} |
| 631 | } |
| 632 | |
| 633 | func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 { |
| 634 | var result int32 = -1 |
| 635 | |
| 636 | if paramType == "direction" { |
| 637 | for key, val := range tp_pb.Direction_value { |
| 638 | if key == paramKey { |
| 639 | result = val |
| 640 | } |
| 641 | } |
| 642 | } else if paramType == "discard_policy" { |
| 643 | for key, val := range tp_pb.DiscardPolicy_value { |
| 644 | if key == paramKey { |
| 645 | result = val |
| 646 | } |
| 647 | } |
| 648 | } else if paramType == "sched_policy" { |
| 649 | for key, val := range tp_pb.SchedulingPolicy_value { |
| 650 | if key == paramKey { |
| 651 | log.Debugw("Got value in proto", log.Fields{"key": key, "value": val}) |
| 652 | result = val |
| 653 | } |
| 654 | } |
| 655 | } else if paramType == "additional_bw" { |
| 656 | for key, val := range tp_pb.AdditionalBW_value { |
| 657 | if key == paramKey { |
| 658 | result = val |
| 659 | } |
| 660 | } |
| 661 | } else { |
| 662 | log.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey}) |
| 663 | return -1 |
| 664 | } |
| 665 | log.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result}) |
| 666 | return result |
| 667 | } |
| 668 | |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 669 | func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 670 | dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction)) |
| 671 | if dir == -1 { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 672 | log.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction) |
| 673 | return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 674 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 675 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 676 | bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw)) |
| 677 | if bw == -1 { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 678 | log.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw) |
| 679 | return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 680 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 681 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 682 | policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy)) |
| 683 | if policy == -1 { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 684 | log.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy) |
| 685 | return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 686 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 687 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 688 | return &tp_pb.SchedulerConfig{ |
| 689 | Direction: dir, |
| 690 | AdditionalBw: bw, |
| 691 | Priority: tpInstance.UsScheduler.Priority, |
| 692 | Weight: tpInstance.UsScheduler.Weight, |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 693 | SchedPolicy: policy}, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 696 | func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 697 | |
| 698 | dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction)) |
| 699 | if dir == -1 { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 700 | log.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction) |
| 701 | return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 702 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 703 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 704 | bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw)) |
| 705 | if bw == -1 { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 706 | log.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw) |
| 707 | return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 708 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 709 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 710 | policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy)) |
| 711 | if policy == -1 { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 712 | log.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy) |
| 713 | return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | return &tp_pb.SchedulerConfig{ |
| 717 | Direction: dir, |
| 718 | AdditionalBw: bw, |
| 719 | Priority: tpInstance.DsScheduler.Priority, |
| 720 | Weight: tpInstance.DsScheduler.Weight, |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 721 | SchedPolicy: policy}, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | func (t *TechProfileMgr) GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig, |
| 725 | ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler { |
| 726 | |
| 727 | tSched := &tp_pb.TrafficScheduler{ |
| 728 | Direction: SchedCfg.Direction, |
| 729 | AllocId: tpInstance.UsScheduler.AllocID, |
| 730 | TrafficShapingInfo: ShapingCfg, |
| 731 | Scheduler: SchedCfg} |
| 732 | |
| 733 | return tSched |
| 734 | } |
| 735 | |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 736 | func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 737 | |
| 738 | var encryp bool |
| 739 | if Dir == tp_pb.Direction_UPSTREAM { |
| 740 | // upstream GEM ports |
| 741 | NumGemPorts := len(tp.UpstreamGemPortAttributeList) |
| 742 | GemPorts := make([]*tp_pb.TrafficQueue, 0) |
| 743 | for Count := 0; Count < NumGemPorts; Count++ { |
| 744 | if tp.UpstreamGemPortAttributeList[Count].AesEncryption == "True" { |
| 745 | encryp = true |
| 746 | } else { |
| 747 | encryp = false |
| 748 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 749 | |
| 750 | schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy) |
| 751 | if schedPolicy == -1 { |
| 752 | log.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count) |
| 753 | return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy) |
| 754 | } |
| 755 | |
| 756 | discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy) |
| 757 | if discardPolicy == -1 { |
| 758 | log.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count) |
| 759 | return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy) |
| 760 | } |
| 761 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 762 | GemPorts = append(GemPorts, &tp_pb.TrafficQueue{ |
| 763 | Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)), |
| 764 | GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID, |
| 765 | PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap, |
| 766 | AesEncryption: encryp, |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 767 | SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 768 | Priority: tp.UpstreamGemPortAttributeList[Count].PriorityQueue, |
| 769 | Weight: tp.UpstreamGemPortAttributeList[Count].Weight, |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 770 | DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 771 | }) |
| 772 | } |
| 773 | log.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts}) |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 774 | return GemPorts, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 775 | } else if Dir == tp_pb.Direction_DOWNSTREAM { |
| 776 | //downstream GEM ports |
| 777 | NumGemPorts := len(tp.DownstreamGemPortAttributeList) |
| 778 | GemPorts := make([]*tp_pb.TrafficQueue, 0) |
| 779 | for Count := 0; Count < NumGemPorts; Count++ { |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 780 | if isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) { |
| 781 | //do not take multicast GEM ports. They are handled separately. |
| 782 | continue |
| 783 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 784 | if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" { |
| 785 | encryp = true |
| 786 | } else { |
| 787 | encryp = false |
| 788 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 789 | |
| 790 | schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy) |
| 791 | if schedPolicy == -1 { |
| 792 | log.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count) |
| 793 | return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy) |
| 794 | } |
| 795 | |
| 796 | discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy) |
| 797 | if discardPolicy == -1 { |
| 798 | log.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count) |
| 799 | return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy) |
| 800 | } |
| 801 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 802 | GemPorts = append(GemPorts, &tp_pb.TrafficQueue{ |
| 803 | Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)), |
| 804 | GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID, |
| 805 | PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap, |
| 806 | AesEncryption: encryp, |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 807 | SchedPolicy: tp_pb.SchedulingPolicy(schedPolicy), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 808 | Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue, |
| 809 | Weight: tp.DownstreamGemPortAttributeList[Count].Weight, |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 810 | DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 811 | }) |
| 812 | } |
| 813 | log.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts}) |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 814 | return GemPorts, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 815 | } |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 816 | |
| 817 | log.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir) |
| 818 | return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Esin Karaman | 8aa75a7 | 2019-12-20 13:11:59 +0000 | [diff] [blame] | 821 | //isMulticastGem returns true if isMulticast attribute value of a GEM port is true; false otherwise |
| 822 | func isMulticastGem(isMulticastAttrValue string) bool { |
| 823 | return isMulticastAttrValue != "" && |
| 824 | (isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE") |
| 825 | } |
| 826 | |
| 827 | func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue { |
| 828 | var encryp bool |
| 829 | NumGemPorts := len(tp.DownstreamGemPortAttributeList) |
| 830 | mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0) |
| 831 | for Count := 0; Count < NumGemPorts; Count++ { |
| 832 | if !isMulticastGem(tp.DownstreamGemPortAttributeList[Count].IsMulticast) { |
| 833 | continue |
| 834 | } |
| 835 | if tp.DownstreamGemPortAttributeList[Count].AesEncryption == "True" { |
| 836 | encryp = true |
| 837 | } else { |
| 838 | encryp = false |
| 839 | } |
| 840 | mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{ |
| 841 | Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)), |
| 842 | GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID, |
| 843 | PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap, |
| 844 | AesEncryption: encryp, |
| 845 | SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)), |
| 846 | Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue, |
| 847 | Weight: tp.DownstreamGemPortAttributeList[Count].Weight, |
| 848 | DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)), |
| 849 | }) |
| 850 | } |
| 851 | log.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues}) |
| 852 | return mcastTrafficQueues |
| 853 | } |
| 854 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 855 | func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler { |
Girish Kumar | b3c52d5 | 2019-12-06 12:14:14 +0000 | [diff] [blame] | 856 | UsScheduler, _ := tpm.GetUsScheduler(tp) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 857 | |
| 858 | return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction, |
| 859 | AllocId: tp.UsScheduler.AllocID, |
| 860 | Scheduler: UsScheduler} |
| 861 | } |
| 862 | |
| 863 | func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32 { |
| 864 | /* |
| 865 | Function to get the Gemport ID mapped to a pbit. |
| 866 | */ |
| 867 | if Dir == tp_pb.Direction_UPSTREAM { |
| 868 | // upstream GEM ports |
| 869 | NumGemPorts := len(tp.UpstreamGemPortAttributeList) |
| 870 | for Count := 0; Count < NumGemPorts; Count++ { |
| 871 | NumPbitMaps := len(tp.UpstreamGemPortAttributeList[Count].PbitMap) |
| 872 | for ICount := 2; ICount < NumPbitMaps; ICount++ { |
| 873 | if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil { |
| 874 | if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set |
| 875 | log.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[Count].GemportID}) |
| 876 | return tp.UpstreamGemPortAttributeList[Count].GemportID |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | } else if Dir == tp_pb.Direction_DOWNSTREAM { |
| 882 | //downstream GEM ports |
| 883 | NumGemPorts := len(tp.DownstreamGemPortAttributeList) |
| 884 | for Count := 0; Count < NumGemPorts; Count++ { |
| 885 | NumPbitMaps := len(tp.DownstreamGemPortAttributeList[Count].PbitMap) |
| 886 | for ICount := 2; ICount < NumPbitMaps; ICount++ { |
| 887 | if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[Count].PbitMap[ICount])); err == nil { |
| 888 | if uint32(ICount-2) == pbit && p == 1 { // Check this p-bit is set |
| 889 | log.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[Count].GemportID}) |
| 890 | return tp.DownstreamGemPortAttributeList[Count].GemportID |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | log.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit}) |
| 897 | return 0 |
| 898 | } |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 899 | |
| 900 | // FindAllTpInstances returns all TechProfile instances for a given TechProfile table-id, pon interface ID and onu ID. |
| 901 | func (t *TechProfileMgr) FindAllTpInstances(techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile { |
| 902 | var tp TechProfile |
Girish Gowdra | 6172a2e | 2019-11-27 14:37:58 +0530 | [diff] [blame] | 903 | onuTpInstancePath := fmt.Sprintf("%s/%d/pon-{%d}/onu-{%d}", t.resourceMgr.GetTechnology(), techProfiletblID, ponIntf, onuID) |
Girish Gowdra | 9447baf | 2019-11-05 16:42:37 +0530 | [diff] [blame] | 904 | |
| 905 | if kvPairs, _ := t.config.KVBackend.List(onuTpInstancePath); kvPairs != nil { |
| 906 | tpInstances := make([]TechProfile, 0, len(kvPairs)) |
| 907 | for kvPath, kvPair := range kvPairs { |
| 908 | if value, err := kvstore.ToByte(kvPair.Value); err == nil { |
| 909 | if err = json.Unmarshal(value, &tp); err != nil { |
| 910 | log.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value}) |
| 911 | continue |
| 912 | } else { |
| 913 | tpInstances = append(tpInstances, tp) |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | return tpInstances |
| 918 | } |
| 919 | return nil |
| 920 | } |