Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 common |
| 18 | |
| 19 | import ( |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 20 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | type PmMetrics struct { |
| 24 | deviceId string |
| 25 | frequency uint32 |
| 26 | grouped bool |
| 27 | frequencyOverride bool |
| 28 | metrics map[string]*voltha.PmConfig |
| 29 | } |
| 30 | |
| 31 | type PmMetricsOption func(*PmMetrics) |
| 32 | |
| 33 | func Frequency(frequency uint32) PmMetricsOption { |
| 34 | return func(args *PmMetrics) { |
| 35 | args.frequency = frequency |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func Grouped(grouped bool) PmMetricsOption { |
| 40 | return func(args *PmMetrics) { |
| 41 | args.grouped = grouped |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func FrequencyOverride(frequencyOverride bool) PmMetricsOption { |
| 46 | return func(args *PmMetrics) { |
| 47 | args.frequencyOverride = frequencyOverride |
| 48 | } |
| 49 | } |
| 50 | |
Girish Gowdra | 5950310 | 2021-01-14 13:25:54 -0800 | [diff] [blame] | 51 | // UpdateFrequency will update the frequency. |
| 52 | func (pm *PmMetrics) UpdateFrequency(frequency uint32) { |
| 53 | pm.frequency = frequency |
| 54 | } |
| 55 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 56 | func Metrics(pmNames []string) PmMetricsOption { |
| 57 | return func(args *PmMetrics) { |
| 58 | args.metrics = make(map[string]*voltha.PmConfig) |
| 59 | for _, name := range pmNames { |
| 60 | args.metrics[name] = &voltha.PmConfig{ |
| 61 | Name: name, |
| 62 | Type: voltha.PmConfig_COUNTER, |
| 63 | Enabled: true, |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func NewPmMetrics(deviceId string, opts ...PmMetricsOption) *PmMetrics { |
| 70 | pm := &PmMetrics{deviceId: deviceId} |
| 71 | for _, option := range opts { |
| 72 | option(pm) |
| 73 | } |
| 74 | return pm |
| 75 | } |
| 76 | |
| 77 | func (pm *PmMetrics) ToPmConfigs() *voltha.PmConfigs { |
| 78 | pmConfigs := &voltha.PmConfigs{ |
| 79 | Id: pm.deviceId, |
| 80 | DefaultFreq: pm.frequency, |
| 81 | Grouped: pm.grouped, |
| 82 | FreqOverride: pm.frequencyOverride, |
| 83 | } |
| 84 | for _, v := range pm.metrics { |
| 85 | pmConfigs.Metrics = append(pmConfigs.Metrics, &voltha.PmConfig{Name: v.Name, Type: v.Type, Enabled: v.Enabled}) |
| 86 | } |
| 87 | return pmConfigs |
| 88 | } |