Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cpman; |
| 17 | |
| 18 | import com.codahale.metrics.Meter; |
| 19 | import org.onlab.metrics.MetricsComponent; |
| 20 | import org.onlab.metrics.MetricsFeature; |
| 21 | import org.onlab.metrics.MetricsService; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | |
| 24 | import java.util.Optional; |
| 25 | |
| 26 | import 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 | */ |
| 31 | public 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 | * |
| 48 | * @param type Control metric type |
| 49 | * @param deviceId DeviceId |
| 50 | */ |
| 51 | MetricsAggregator(MetricsService metricsService, ControlMetricType type, Optional<DeviceId> deviceId) { |
| 52 | String primitiveName = type.toString(); |
| 53 | String objName = "all"; |
| 54 | if (deviceId.isPresent()) { |
| 55 | objName = deviceId.toString(); |
| 56 | } |
| 57 | |
| 58 | checkNotNull(primitiveName, "Component name cannot be null"); |
| 59 | checkNotNull(objName, "Feature name cannot be null"); |
| 60 | |
| 61 | this.metricsType = type; |
| 62 | |
| 63 | this.metricsService = metricsService; |
| 64 | this.metricsComponent = metricsService.registerComponent(primitiveName); |
| 65 | this.metricsFeature = metricsComponent.registerFeature(objName); |
| 66 | |
| 67 | this.rateMeter = metricsService.createMeter(metricsComponent, metricsFeature, RATE_NAME); |
| 68 | this.countMeter = metricsService.createMeter(metricsComponent, metricsFeature, COUNT_NAME); |
| 69 | } |
| 70 | |
| 71 | public ControlMetricType getMetricsType() { |
| 72 | return metricsType; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Removes both rate and count metrics. |
| 77 | */ |
| 78 | protected void removeMetrics() { |
| 79 | metricsService.removeMetric(metricsComponent, metricsFeature, RATE_NAME); |
| 80 | metricsService.removeMetric(metricsComponent, metricsFeature, COUNT_NAME); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Increments the meter rate by {@code n}, and the meter counter by 1. |
| 85 | * |
| 86 | * @param n Increment the meter rate by {@code n}. |
| 87 | */ |
| 88 | public void increment(long n) { |
| 89 | rateMeter.mark(n); |
| 90 | countMeter.mark(1); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Obtains the average load value. |
| 95 | * |
| 96 | * @return load value |
| 97 | */ |
| 98 | public double getLoad() { |
| 99 | return rateMeter.getOneMinuteRate() / countMeter.getOneMinuteRate(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Obtains the average meter rate within recent 1 minute. |
| 104 | * |
| 105 | * @return rate value |
| 106 | */ |
| 107 | public double getRate() { |
| 108 | return rateMeter.getOneMinuteRate(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Obtains the average meter count within recent 1 minute. |
| 113 | * |
| 114 | * @return count value |
| 115 | */ |
| 116 | public double getCount() { |
| 117 | return countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND; |
| 118 | } |
| 119 | } |