blob: 8951a7d2c15afd4b76017570bdb090795721cb3d [file] [log] [blame]
Naga Manjunath7615e552019-10-11 22:35:47 +05301/*
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
17package pmmetrics
18
19import (
Scott Bakerc6e54cb2019-11-04 09:31:25 -080020 "github.com/opencord/voltha-protos/v2/go/voltha"
Naga Manjunath7615e552019-10-11 22:35:47 +053021)
22
23// PmMetrics structure holds metric and device info
24type PmMetrics struct {
25 deviceID string
26 frequency uint32
27 grouped bool
28 frequencyOverride bool
29 metrics map[string]*voltha.PmConfig
30}
31
32type PmMetricsOption func(*PmMetrics)
33
34// Frequency is to poll stats at this interval
35func 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.
42func (pm *PmMetrics) GetSubscriberMetrics() map[string]*voltha.PmConfig {
43 if pm == nil {
44 return nil
45 }
46 return pm.metrics
47}
48
49func Grouped(grouped bool) PmMetricsOption {
50 return func(args *PmMetrics) {
51 args.grouped = grouped
52 }
53}
54
55func FrequencyOverride(frequencyOverride bool) PmMetricsOption {
56 return func(args *PmMetrics) {
57 args.frequencyOverride = frequencyOverride
58 }
59}
60
61// Metrics will store the PMMetric params
62func 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
76func 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
86func (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}