blob: 3ac7cbb7bb3bfea94c4d8a8647cb02856ef61cd8 [file] [log] [blame]
Naga Manjunath86e9d2e2019-10-25 15:21:49 +05301/*
Joey Armstrong7f8436c2023-07-09 20:23:27 -04002 * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors
Naga Manjunath86e9d2e2019-10-25 15:21:49 +05303
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 (
khenaidoo26721882021-08-11 17:42:52 -040020 "github.com/opencord/voltha-protos/v5/go/voltha"
Naga Manjunath86e9d2e2019-10-25 15:21:49 +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
Rohan Agrawale66289c2020-05-20 11:47:21 +000061// UpdateFrequency will update the frequency.
62func (pm *PmMetrics) UpdateFrequency(frequency uint32) {
63 pm.frequency = frequency
64}
65
Naga Manjunath86e9d2e2019-10-25 15:21:49 +053066// Metrics will store the PMMetric params
67func 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
81func 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
91func (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}