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