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 ( |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 20 | "github.com/opencord/voltha-protos/v2/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 | |
| 61 | // Metrics will store the PMMetric params |
| 62 | func Metrics(pmNames []string) PmMetricsOption { |
| 63 | return func(args *PmMetrics) { |
| 64 | args.metrics = make(map[string]*voltha.PmConfig) |
| 65 | for _, name := range pmNames { |
| 66 | args.metrics[name] = &voltha.PmConfig{ |
| 67 | Name: name, |
| 68 | Type: voltha.PmConfig_COUNTER, |
| 69 | Enabled: true, |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // NewPmMetrics will return the pmmetric object |
| 76 | func NewPmMetrics(deviceID string, opts ...PmMetricsOption) *PmMetrics { |
| 77 | pm := &PmMetrics{} |
| 78 | pm.deviceID = deviceID |
| 79 | for _, option := range opts { |
| 80 | option(pm) |
| 81 | } |
| 82 | return pm |
| 83 | } |
| 84 | |
| 85 | // ToPmConfigs will enable the defined pmmetric |
| 86 | func (pm *PmMetrics) ToPmConfigs() *voltha.PmConfigs { |
| 87 | pmConfigs := &voltha.PmConfigs{ |
| 88 | Id: pm.deviceID, |
| 89 | DefaultFreq: pm.frequency, |
| 90 | Grouped: pm.grouped, |
| 91 | FreqOverride: pm.frequencyOverride, |
| 92 | } |
| 93 | for _, v := range pm.metrics { |
| 94 | pmConfigs.Metrics = append(pmConfigs.Metrics, &voltha.PmConfig{Name: v.Name, Type: v.Type, Enabled: v.Enabled}) |
| 95 | } |
| 96 | return pmConfigs |
| 97 | } |