khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [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 common |
| 18 | |
| 19 | import ( |
| 20 | "github.com/opencord/voltha-protos/go/voltha" |
| 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 | |
| 51 | func Metrics(pmNames []string) PmMetricsOption { |
| 52 | return func(args *PmMetrics) { |
| 53 | args.metrics = make(map[string]*voltha.PmConfig) |
| 54 | for _, name := range pmNames { |
| 55 | args.metrics[name] = &voltha.PmConfig{ |
| 56 | Name: name, |
| 57 | Type: voltha.PmConfig_COUNTER, |
| 58 | Enabled: true, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func NewPmMetrics(deviceId string, opts ...PmMetricsOption) *PmMetrics { |
| 65 | pm := &PmMetrics{deviceId: deviceId} |
| 66 | for _, option := range opts { |
| 67 | option(pm) |
| 68 | } |
| 69 | return pm |
| 70 | } |
| 71 | |
| 72 | func (pm *PmMetrics) ToPmConfigs() *voltha.PmConfigs { |
| 73 | pmConfigs := &voltha.PmConfigs{ |
| 74 | Id: pm.deviceId, |
| 75 | DefaultFreq: pm.frequency, |
| 76 | Grouped: pm.grouped, |
| 77 | FreqOverride: pm.frequencyOverride, |
| 78 | } |
| 79 | for _, v := range pm.metrics { |
| 80 | pmConfigs.Metrics = append(pmConfigs.Metrics, &voltha.PmConfig{Name: v.Name, Type: v.Type, Enabled: v.Enabled}) |
| 81 | } |
| 82 | return pmConfigs |
| 83 | } |