blob: 524eeeabd7142f7be56b55133f9cd31d08dbec24 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 common
18
19import (
khenaidoo26721882021-08-11 17:42:52 -040020 "github.com/opencord/voltha-protos/v5/go/voltha"
Scott Baker2c1c4822019-10-16 11:02:41 -070021)
22
23type PmMetrics struct {
24 deviceId string
25 frequency uint32
26 grouped bool
27 frequencyOverride bool
28 metrics map[string]*voltha.PmConfig
29}
30
31type PmMetricsOption func(*PmMetrics)
32
33func Frequency(frequency uint32) PmMetricsOption {
34 return func(args *PmMetrics) {
35 args.frequency = frequency
36 }
37}
38
39func Grouped(grouped bool) PmMetricsOption {
40 return func(args *PmMetrics) {
41 args.grouped = grouped
42 }
43}
44
45func FrequencyOverride(frequencyOverride bool) PmMetricsOption {
46 return func(args *PmMetrics) {
47 args.frequencyOverride = frequencyOverride
48 }
49}
50
Girish Gowdra59503102021-01-14 13:25:54 -080051// UpdateFrequency will update the frequency.
52func (pm *PmMetrics) UpdateFrequency(frequency uint32) {
53 pm.frequency = frequency
54}
55
Scott Baker2c1c4822019-10-16 11:02:41 -070056func Metrics(pmNames []string) PmMetricsOption {
57 return func(args *PmMetrics) {
58 args.metrics = make(map[string]*voltha.PmConfig)
59 for _, name := range pmNames {
60 args.metrics[name] = &voltha.PmConfig{
61 Name: name,
62 Type: voltha.PmConfig_COUNTER,
63 Enabled: true,
64 }
65 }
66 }
67}
68
69func NewPmMetrics(deviceId string, opts ...PmMetricsOption) *PmMetrics {
70 pm := &PmMetrics{deviceId: deviceId}
71 for _, option := range opts {
72 option(pm)
73 }
74 return pm
75}
76
77func (pm *PmMetrics) ToPmConfigs() *voltha.PmConfigs {
78 pmConfigs := &voltha.PmConfigs{
79 Id: pm.deviceId,
80 DefaultFreq: pm.frequency,
81 Grouped: pm.grouped,
82 FreqOverride: pm.frequencyOverride,
83 }
84 for _, v := range pm.metrics {
85 pmConfigs.Metrics = append(pmConfigs.Metrics, &voltha.PmConfig{Name: v.Name, Type: v.Type, Enabled: v.Enabled})
86 }
87 return pmConfigs
88}