Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 16 | package org.onosproject.cpman.impl; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 17 | |
| 18 | import com.codahale.metrics.Meter; |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 20 | import org.onlab.metrics.MetricsComponent; |
| 21 | import org.onlab.metrics.MetricsFeature; |
| 22 | import org.onlab.metrics.MetricsService; |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 23 | import org.onosproject.cpman.ControlMetricType; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | |
| 26 | import java.util.Optional; |
| 27 | |
| 28 | import 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 | */ |
| 33 | public 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 Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 46 | * Constructs a new metrics aggregator for aggregating a metric. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 47 | * Instantiates the metrics service |
| 48 | * Initializes all the general metrics for that object |
| 49 | * |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 50 | * @param metricsService metric service reference |
| 51 | * @param type control metric type |
| 52 | * @param deviceId device identification |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 53 | */ |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 54 | MetricsAggregator(MetricsService metricsService, ControlMetricType type, |
| 55 | Optional<DeviceId> deviceId) { |
| 56 | init(metricsService, type, deviceId, null); |
| 57 | } |
| 58 | |
| 59 | /** |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 60 | * Constructs a new metrics aggregator for aggregating a metric. |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 61 | * Instantiates the metrics service |
| 62 | * Initializes all the general metrics for that object |
| 63 | * |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 64 | * @param metricsService metric service reference |
| 65 | * @param type control metric type |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 66 | * @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 Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 75 | * Constructs a new metrics aggregator for aggregating a metric. |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 76 | * Instantiates the metrics service |
| 77 | * Initializes all the general metrics for that object |
| 78 | * |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 79 | * @param metricsService metrics service reference |
| 80 | * @param type control metric type |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 81 | */ |
| 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 Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 89 | * @param metricsService metrics service reference |
| 90 | * @param type control metric type |
| 91 | * @param deviceId device identification |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 92 | * @param resourceName resource name |
| 93 | */ |
| 94 | private void init(MetricsService metricsService, ControlMetricType type, |
| 95 | Optional<DeviceId> deviceId, String resourceName) { |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 96 | String primitiveName = type.toString(); |
| 97 | String objName = "all"; |
| 98 | if (deviceId.isPresent()) { |
| 99 | objName = deviceId.toString(); |
| 100 | } |
| 101 | |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 102 | if (StringUtils.isNotEmpty(resourceName)) { |
| 103 | objName = resourceName; |
| 104 | } |
| 105 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 106 | 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 Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Returns control metrics type. |
| 121 | * |
| 122 | * @return control metrics type |
| 123 | */ |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 124 | public ControlMetricType getMetricsType() { |
| 125 | return metricsType; |
| 126 | } |
| 127 | |
| 128 | /** |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 129 | * Increments the meter rate by n, and the meter counter by 1. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 130 | * |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 131 | * @param n increment rate. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 132 | */ |
| 133 | public void increment(long n) { |
| 134 | rateMeter.mark(n); |
| 135 | countMeter.mark(1); |
| 136 | } |
| 137 | |
| 138 | /** |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 139 | * Returns the average load value. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 140 | * |
| 141 | * @return load value |
| 142 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 143 | public long getLoad() { |
| 144 | return (long) rateMeter.getOneMinuteRate() / (long) countMeter.getOneMinuteRate(); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 148 | * Returns the average meter rate within recent 1 minute. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 149 | * |
| 150 | * @return rate value |
| 151 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 152 | public long getRate() { |
| 153 | return (long) rateMeter.getOneMinuteRate(); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 157 | * Returns the average meter count within recent 1 minute. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 158 | * |
| 159 | * @return count value |
| 160 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 161 | public long getCount() { |
| 162 | return (long) countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 163 | } |
| 164 | } |