blob: abd5b1c44ac166082c03af06a6d9f8d2a5a57c04 [file] [log] [blame]
Jian Li60804322015-12-02 14:46:31 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.cpman;
17
18import com.codahale.metrics.Meter;
19import org.onlab.metrics.MetricsComponent;
20import org.onlab.metrics.MetricsFeature;
21import org.onlab.metrics.MetricsService;
22import org.onosproject.net.DeviceId;
23
24import java.util.Optional;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * An aggregator that aggregates a specific network or performance metrics via the metrics service.
30 */
31public class MetricsAggregator {
32
33 private Meter rateMeter;
34 private Meter countMeter;
35 private MetricsService metricsService;
36 private MetricsComponent metricsComponent;
37 private MetricsFeature metricsFeature;
38 private ControlMetricType metricsType;
39 private static final int EXECUTE_PERIOD_IN_SECOND = 60;
40 private static final String RATE_NAME = "rate";
41 private static final String COUNT_NAME = "count";
42
43 /**
44 * Constructs a new MetricsAggregator for aggregating a metric.
45 * Instantiates the metrics service
46 * Initializes all the general metrics for that object
47 *
Jian Lidfba7392016-01-22 16:46:58 -080048 * @param metricsService MetricsService reference
49 * @param type Control metric type
50 * @param deviceId DeviceId
Jian Li60804322015-12-02 14:46:31 -080051 */
52 MetricsAggregator(MetricsService metricsService, ControlMetricType type, Optional<DeviceId> deviceId) {
53 String primitiveName = type.toString();
54 String objName = "all";
55 if (deviceId.isPresent()) {
56 objName = deviceId.toString();
57 }
58
59 checkNotNull(primitiveName, "Component name cannot be null");
60 checkNotNull(objName, "Feature name cannot be null");
61
62 this.metricsType = type;
63
64 this.metricsService = metricsService;
65 this.metricsComponent = metricsService.registerComponent(primitiveName);
66 this.metricsFeature = metricsComponent.registerFeature(objName);
67
68 this.rateMeter = metricsService.createMeter(metricsComponent, metricsFeature, RATE_NAME);
69 this.countMeter = metricsService.createMeter(metricsComponent, metricsFeature, COUNT_NAME);
70 }
71
72 public ControlMetricType getMetricsType() {
73 return metricsType;
74 }
75
76 /**
77 * Removes both rate and count metrics.
78 */
79 protected void removeMetrics() {
80 metricsService.removeMetric(metricsComponent, metricsFeature, RATE_NAME);
81 metricsService.removeMetric(metricsComponent, metricsFeature, COUNT_NAME);
82 }
83
84 /**
85 * Increments the meter rate by {@code n}, and the meter counter by 1.
86 *
87 * @param n Increment the meter rate by {@code n}.
88 */
89 public void increment(long n) {
90 rateMeter.mark(n);
91 countMeter.mark(1);
92 }
93
94 /**
95 * Obtains the average load value.
96 *
97 * @return load value
98 */
99 public double getLoad() {
100 return rateMeter.getOneMinuteRate() / countMeter.getOneMinuteRate();
101 }
102
103 /**
104 * Obtains the average meter rate within recent 1 minute.
105 *
106 * @return rate value
107 */
108 public double getRate() {
109 return rateMeter.getOneMinuteRate();
110 }
111
112 /**
113 * Obtains the average meter count within recent 1 minute.
114 *
115 * @return count value
116 */
117 public double getCount() {
118 return countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
119 }
120}