blob: bcb45f8a87284dc326ca74f34cfaf5836d264f19 [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 (
Scott Bakerf1b096c2019-11-01 12:36:30 -070020 "github.com/opencord/voltha-protos/v2/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
51func Metrics(pmNames []string) PmMetricsOption {
52 return func(args *PmMetrics) {
53 args.metrics = make(map[string]*voltha.PmConfig)
54 for _, name := range pmNames {
55 args.metrics[name] = &voltha.PmConfig{
56 Name: name,
57 Type: voltha.PmConfig_COUNTER,
58 Enabled: true,
59 }
60 }
61 }
62}
63
64func NewPmMetrics(deviceId string, opts ...PmMetricsOption) *PmMetrics {
65 pm := &PmMetrics{deviceId: deviceId}
66 for _, option := range opts {
67 option(pm)
68 }
69 return pm
70}
71
72func (pm *PmMetrics) ToPmConfigs() *voltha.PmConfigs {
73 pmConfigs := &voltha.PmConfigs{
74 Id: pm.deviceId,
75 DefaultFreq: pm.frequency,
76 Grouped: pm.grouped,
77 FreqOverride: pm.frequencyOverride,
78 }
79 for _, v := range pm.metrics {
80 pmConfigs.Metrics = append(pmConfigs.Metrics, &voltha.PmConfig{Name: v.Name, Type: v.Type, Enabled: v.Enabled})
81 }
82 return pmConfigs
83}