blob: bdcc56e06408b0c500bc4d03ba98f66ddc095b8d [file] [log] [blame]
Jian Li60804322015-12-02 14:46:31 -08001/*
Jian Lie044d1a2016-01-25 09:01:20 -08002 * Copyright 2015-2016 Open Networking Laboratory
Jian Li60804322015-12-02 14:46:31 -08003 *
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 */
Jian Li6b86a762016-01-29 09:30:40 -080016package org.onosproject.cpman.impl;
Jian Li60804322015-12-02 14:46:31 -080017
18import com.codahale.metrics.Meter;
Jian Lie044d1a2016-01-25 09:01:20 -080019import org.apache.commons.lang3.StringUtils;
Jian Li60804322015-12-02 14:46:31 -080020import org.onlab.metrics.MetricsComponent;
21import org.onlab.metrics.MetricsFeature;
22import org.onlab.metrics.MetricsService;
Jian Li6b86a762016-01-29 09:30:40 -080023import org.onosproject.cpman.ControlMetricType;
Jian Li60804322015-12-02 14:46:31 -080024import org.onosproject.net.DeviceId;
25
26import java.util.Optional;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * An aggregator that aggregates a specific network or performance metrics via the metrics service.
32 */
33public class MetricsAggregator {
34
35 private Meter rateMeter;
36 private Meter countMeter;
37 private MetricsService metricsService;
38 private MetricsComponent metricsComponent;
39 private MetricsFeature metricsFeature;
40 private ControlMetricType metricsType;
41 private static final int EXECUTE_PERIOD_IN_SECOND = 60;
42 private static final String RATE_NAME = "rate";
43 private static final String COUNT_NAME = "count";
44
45 /**
Jian Lic5cb4a12016-02-03 23:24:42 -080046 * Constructs a new metrics aggregator for aggregating a metric.
Jian Li60804322015-12-02 14:46:31 -080047 * Instantiates the metrics service
48 * Initializes all the general metrics for that object
49 *
Jian Lic5cb4a12016-02-03 23:24:42 -080050 * @param metricsService metric service reference
51 * @param type control metric type
52 * @param deviceId device identification
Jian Li60804322015-12-02 14:46:31 -080053 */
Jian Lie044d1a2016-01-25 09:01:20 -080054 MetricsAggregator(MetricsService metricsService, ControlMetricType type,
55 Optional<DeviceId> deviceId) {
56 init(metricsService, type, deviceId, null);
57 }
58
59 /**
Jian Lic5cb4a12016-02-03 23:24:42 -080060 * Constructs a new metrics aggregator for aggregating a metric.
Jian Lie044d1a2016-01-25 09:01:20 -080061 * Instantiates the metrics service
62 * Initializes all the general metrics for that object
63 *
Jian Lic5cb4a12016-02-03 23:24:42 -080064 * @param metricsService metric service reference
65 * @param type control metric type
Jian Lie044d1a2016-01-25 09:01:20 -080066 * @param resourceName resource name (e.g., ethernet interface name)
67 */
68 MetricsAggregator(MetricsService metricsService, ControlMetricType type,
69 String resourceName) {
70 init(metricsService, type, Optional.ofNullable(null), resourceName);
71
72 }
73
74 /**
Jian Lic5cb4a12016-02-03 23:24:42 -080075 * Constructs a new metrics aggregator for aggregating a metric.
Jian Lie044d1a2016-01-25 09:01:20 -080076 * Instantiates the metrics service
77 * Initializes all the general metrics for that object
78 *
Jian Lic5cb4a12016-02-03 23:24:42 -080079 * @param metricsService metrics service reference
80 * @param type control metric type
Jian Lie044d1a2016-01-25 09:01:20 -080081 */
82 MetricsAggregator(MetricsService metricsService, ControlMetricType type) {
83 init(metricsService, type, Optional.ofNullable(null), null);
84 }
85
86 /**
87 * Base method of the constructor of this class.
88 *
Jian Lic5cb4a12016-02-03 23:24:42 -080089 * @param metricsService metrics service reference
90 * @param type control metric type
91 * @param deviceId device identification
Jian Lie044d1a2016-01-25 09:01:20 -080092 * @param resourceName resource name
93 */
94 private void init(MetricsService metricsService, ControlMetricType type,
95 Optional<DeviceId> deviceId, String resourceName) {
Jian Li60804322015-12-02 14:46:31 -080096 String primitiveName = type.toString();
97 String objName = "all";
98 if (deviceId.isPresent()) {
99 objName = deviceId.toString();
100 }
101
Jian Lie044d1a2016-01-25 09:01:20 -0800102 if (StringUtils.isNotEmpty(resourceName)) {
103 objName = resourceName;
104 }
105
Jian Li60804322015-12-02 14:46:31 -0800106 checkNotNull(primitiveName, "Component name cannot be null");
107 checkNotNull(objName, "Feature name cannot be null");
108
109 this.metricsType = type;
110
111 this.metricsService = metricsService;
112 this.metricsComponent = metricsService.registerComponent(primitiveName);
113 this.metricsFeature = metricsComponent.registerFeature(objName);
114
115 this.rateMeter = metricsService.createMeter(metricsComponent, metricsFeature, RATE_NAME);
116 this.countMeter = metricsService.createMeter(metricsComponent, metricsFeature, COUNT_NAME);
117 }
118
Jian Lic5cb4a12016-02-03 23:24:42 -0800119 /**
120 * Returns control metrics type.
121 *
122 * @return control metrics type
123 */
Jian Li60804322015-12-02 14:46:31 -0800124 public ControlMetricType getMetricsType() {
125 return metricsType;
126 }
127
128 /**
Jian Lic5cb4a12016-02-03 23:24:42 -0800129 * Increments the meter rate by n, and the meter counter by 1.
Jian Li60804322015-12-02 14:46:31 -0800130 *
Jian Lic5cb4a12016-02-03 23:24:42 -0800131 * @param n increment rate.
Jian Li60804322015-12-02 14:46:31 -0800132 */
133 public void increment(long n) {
134 rateMeter.mark(n);
135 countMeter.mark(1);
136 }
137
138 /**
Jian Lic5cb4a12016-02-03 23:24:42 -0800139 * Returns the average load value.
Jian Li60804322015-12-02 14:46:31 -0800140 *
141 * @return load value
142 */
Jian Lic132c112016-01-28 20:27:34 -0800143 public long getLoad() {
144 return (long) rateMeter.getOneMinuteRate() / (long) countMeter.getOneMinuteRate();
Jian Li60804322015-12-02 14:46:31 -0800145 }
146
147 /**
Jian Lic5cb4a12016-02-03 23:24:42 -0800148 * Returns the average meter rate within recent 1 minute.
Jian Li60804322015-12-02 14:46:31 -0800149 *
150 * @return rate value
151 */
Jian Lic132c112016-01-28 20:27:34 -0800152 public long getRate() {
153 return (long) rateMeter.getOneMinuteRate();
Jian Li60804322015-12-02 14:46:31 -0800154 }
155
156 /**
Jian Lic5cb4a12016-02-03 23:24:42 -0800157 * Returns the average meter count within recent 1 minute.
Jian Li60804322015-12-02 14:46:31 -0800158 *
159 * @return count value
160 */
Jian Lic132c112016-01-28 20:27:34 -0800161 public long getCount() {
162 return (long) countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
Jian Li60804322015-12-02 14:46:31 -0800163 }
164}