Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [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 pmmetrics |
| 18 | |
| 19 | import ( |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 20 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | // PmMetrics structure holds metric and device info |
| 24 | type PmMetrics struct { |
| 25 | deviceID string |
| 26 | frequency uint32 |
| 27 | grouped bool |
| 28 | frequencyOverride bool |
| 29 | metrics map[string]*voltha.PmConfig |
| 30 | } |
| 31 | |
| 32 | type PmMetricsOption func(*PmMetrics) |
| 33 | |
| 34 | // Frequency is to poll stats at this interval |
| 35 | func Frequency(frequency uint32) PmMetricsOption { |
| 36 | return func(args *PmMetrics) { |
| 37 | args.frequency = frequency |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // GetSubscriberMetrics will return the metrics subscribed for the device. |
| 42 | func (pm *PmMetrics) GetSubscriberMetrics() map[string]*voltha.PmConfig { |
| 43 | if pm == nil { |
| 44 | return nil |
| 45 | } |
| 46 | return pm.metrics |
| 47 | } |
| 48 | |
| 49 | func Grouped(grouped bool) PmMetricsOption { |
| 50 | return func(args *PmMetrics) { |
| 51 | args.grouped = grouped |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func FrequencyOverride(frequencyOverride bool) PmMetricsOption { |
| 56 | return func(args *PmMetrics) { |
| 57 | args.frequencyOverride = frequencyOverride |
| 58 | } |
| 59 | } |
| 60 | |
Rohan Agrawal | e66289c | 2020-05-20 11:47:21 +0000 | [diff] [blame] | 61 | // UpdateFrequency will update the frequency. |
| 62 | func (pm *PmMetrics) UpdateFrequency(frequency uint32) { |
| 63 | pm.frequency = frequency |
| 64 | } |
| 65 | |
Naga Manjunath | 86e9d2e | 2019-10-25 15:21:49 +0530 | [diff] [blame] | 66 | // Metrics will store the PMMetric params |
| 67 | func Metrics(pmNames []string) PmMetricsOption { |
| 68 | return func(args *PmMetrics) { |
| 69 | args.metrics = make(map[string]*voltha.PmConfig) |
| 70 | for _, name := range pmNames { |
| 71 | args.metrics[name] = &voltha.PmConfig{ |
| 72 | Name: name, |
| 73 | Type: voltha.PmConfig_COUNTER, |
| 74 | Enabled: true, |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // NewPmMetrics will return the pmmetric object |
| 81 | func NewPmMetrics(deviceID string, opts ...PmMetricsOption) *PmMetrics { |
| 82 | pm := &PmMetrics{} |
| 83 | pm.deviceID = deviceID |
| 84 | for _, option := range opts { |
| 85 | option(pm) |
| 86 | } |
| 87 | return pm |
| 88 | } |
| 89 | |
| 90 | // ToPmConfigs will enable the defined pmmetric |
| 91 | func (pm *PmMetrics) ToPmConfigs() *voltha.PmConfigs { |
| 92 | pmConfigs := &voltha.PmConfigs{ |
| 93 | Id: pm.deviceID, |
| 94 | DefaultFreq: pm.frequency, |
| 95 | Grouped: pm.grouped, |
| 96 | FreqOverride: pm.frequencyOverride, |
| 97 | } |
| 98 | for _, v := range pm.metrics { |
| 99 | pmConfigs.Metrics = append(pmConfigs.Metrics, &voltha.PmConfig{Name: v.Name, Type: v.Type, Enabled: v.Enabled}) |
| 100 | } |
| 101 | return pmConfigs |
| 102 | } |