Jian Li

Add resource name into MetricsDatabase, handle exception gracefully

Change-Id: Icf19965a0fcbfd9696c42b12c32441fd3b161734
...@@ -30,6 +30,13 @@ public interface MetricsDatabase { ...@@ -30,6 +30,13 @@ public interface MetricsDatabase {
30 String metricName(); 30 String metricName();
31 31
32 /** 32 /**
33 + * Returns the resource name of this database.
34 + *
35 + * @return resource name
36 + */
37 + String resourceName();
38 +
39 + /**
33 * Update metric value by specifying metric type. 40 * Update metric value by specifying metric type.
34 * 41 *
35 * @param metricType metric type (e.g., load, usage, etc.) 42 * @param metricType metric type (e.g., load, usage, etc.)
...@@ -138,6 +145,14 @@ public interface MetricsDatabase { ...@@ -138,6 +145,14 @@ public interface MetricsDatabase {
138 Builder withMetricName(String metricName); 145 Builder withMetricName(String metricName);
139 146
140 /** 147 /**
148 + * Sets the resource name.
149 + *
150 + * @param resourceName resource name
151 + * @return builder object
152 + */
153 + Builder withResourceName(String resourceName);
154 +
155 + /**
141 * Add a new metric to be monitored. 156 * Add a new metric to be monitored.
142 * 157 *
143 * @param metricType control metric type 158 * @param metricType control metric type
......
...@@ -77,6 +77,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -77,6 +77,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected ClusterCommunicationService communicationService; 78 protected ClusterCommunicationService communicationService;
79 79
80 + private static final String DEFAULT_RESOURCE = "default";
81 +
80 private static final Set RESOURCE_TYPE_SET = 82 private static final Set RESOURCE_TYPE_SET =
81 ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK); 83 ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK);
82 84
...@@ -96,6 +98,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -96,6 +98,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
96 98
97 private static final String METRIC_TYPE_NULL = "Control metric type cannot be null"; 99 private static final String METRIC_TYPE_NULL = "Control metric type cannot be null";
98 100
101 + Set<Map<ControlMetricType, Double>> debugSets = Sets.newHashSet();
102 +
99 private static final Serializer SERIALIZER = Serializer 103 private static final Serializer SERIALIZER = Serializer
100 .using(new KryoNamespace.Builder() 104 .using(new KryoNamespace.Builder()
101 .register(KryoNamespaces.API) 105 .register(KryoNamespaces.API)
...@@ -107,8 +111,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -107,8 +111,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
107 111
108 @Activate 112 @Activate
109 public void activate() { 113 public void activate() {
110 - cpuMetrics = genMDbBuilder(Type.CPU, CPU_METRICS); 114 + cpuMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.CPU, CPU_METRICS);
111 - memoryMetrics = genMDbBuilder(Type.MEMORY, MEMORY_METRICS); 115 + memoryMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.MEMORY, MEMORY_METRICS);
112 controlMessageMap = Maps.newConcurrentMap(); 116 controlMessageMap = Maps.newConcurrentMap();
113 diskMetricsMap = Maps.newConcurrentMap(); 117 diskMetricsMap = Maps.newConcurrentMap();
114 networkMetricsMap = Maps.newConcurrentMap(); 118 networkMetricsMap = Maps.newConcurrentMap();
...@@ -170,7 +174,7 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -170,7 +174,7 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
170 if (ctrlMsgBuf.get(deviceId.get()).keySet() 174 if (ctrlMsgBuf.get(deviceId.get()).keySet()
171 .containsAll(CONTROL_MESSAGE_METRICS)) { 175 .containsAll(CONTROL_MESSAGE_METRICS)) {
172 updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get()); 176 updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get());
173 - ctrlMsgBuf.get(deviceId.get()).clear(); 177 + ctrlMsgBuf.get(deviceId.get());
174 } 178 }
175 } 179 }
176 } else { 180 } else {
...@@ -304,31 +308,34 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -304,31 +308,34 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
304 return ImmutableSet.of(); 308 return ImmutableSet.of();
305 } 309 }
306 310
307 - private MetricsDatabase genMDbBuilder(Type resourceType, 311 + private MetricsDatabase genMDbBuilder(String resourceName,
312 + Type resourceType,
308 Set<ControlMetricType> metricTypes) { 313 Set<ControlMetricType> metricTypes) {
309 MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder(); 314 MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder();
310 builder.withMetricName(resourceType.toString()); 315 builder.withMetricName(resourceType.toString());
316 + builder.withResourceName(resourceName);
311 metricTypes.forEach(type -> builder.addMetricType(type.toString())); 317 metricTypes.forEach(type -> builder.addMetricType(type.toString()));
312 return builder.build(); 318 return builder.build();
313 } 319 }
314 320
315 private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap, 321 private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap,
316 String resName) { 322 String resName) {
317 - networkMetricsMap.putIfAbsent(resName, 323 + networkMetricsMap.putIfAbsent(resName, genMDbBuilder(resName,
318 - genMDbBuilder(Type.NETWORK, NETWORK_METRICS)); 324 + Type.NETWORK, NETWORK_METRICS));
319 networkMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); 325 networkMetricsMap.get(resName).updateMetrics(convertMap(metricMap));
320 } 326 }
321 327
322 private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap, 328 private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap,
323 String resName) { 329 String resName) {
324 - diskMetricsMap.putIfAbsent(resName, genMDbBuilder(Type.DISK, DISK_METRICS)); 330 + diskMetricsMap.putIfAbsent(resName, genMDbBuilder(resName,
331 + Type.DISK, DISK_METRICS));
325 diskMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); 332 diskMetricsMap.get(resName).updateMetrics(convertMap(metricMap));
326 } 333 }
327 334
328 private void updateControlMessages(Map<ControlMetricType, Double> metricMap, 335 private void updateControlMessages(Map<ControlMetricType, Double> metricMap,
329 DeviceId devId) { 336 DeviceId devId) {
330 - controlMessageMap.putIfAbsent(devId, 337 + controlMessageMap.putIfAbsent(devId, genMDbBuilder(devId.toString(),
331 - genMDbBuilder(Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS)); 338 + Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS));
332 controlMessageMap.get(devId).updateMetrics(convertMap(metricMap)); 339 controlMessageMap.get(devId).updateMetrics(convertMap(metricMap));
333 } 340 }
334 341
......
...@@ -26,6 +26,8 @@ import org.rrd4j.core.RrdBackendFactory; ...@@ -26,6 +26,8 @@ import org.rrd4j.core.RrdBackendFactory;
26 import org.rrd4j.core.RrdDb; 26 import org.rrd4j.core.RrdDb;
27 import org.rrd4j.core.RrdDef; 27 import org.rrd4j.core.RrdDef;
28 import org.rrd4j.core.Sample; 28 import org.rrd4j.core.Sample;
29 +import org.slf4j.Logger;
30 +import org.slf4j.LoggerFactory;
29 31
30 import java.io.IOException; 32 import java.io.IOException;
31 import java.util.ArrayList; 33 import java.util.ArrayList;
...@@ -43,7 +45,10 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -43,7 +45,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
43 * An implementation of control plane metrics back-end database. 45 * An implementation of control plane metrics back-end database.
44 */ 46 */
45 public final class DefaultMetricsDatabase implements MetricsDatabase { 47 public final class DefaultMetricsDatabase implements MetricsDatabase {
48 + private final Logger log = LoggerFactory.getLogger(getClass());
49 +
46 private String metricName; 50 private String metricName;
51 + private String resourceName;
47 private RrdDb rrdDb; 52 private RrdDb rrdDb;
48 private Sample sample; 53 private Sample sample;
49 private static final long SECONDS_OF_DAY = 60L * 60L * 24L; 54 private static final long SECONDS_OF_DAY = 60L * 60L * 24L;
...@@ -60,8 +65,9 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -60,8 +65,9 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
60 * @param metricName metric name 65 * @param metricName metric name
61 * @param rrdDb round robin database 66 * @param rrdDb round robin database
62 */ 67 */
63 - private DefaultMetricsDatabase(String metricName, RrdDb rrdDb) { 68 + private DefaultMetricsDatabase(String metricName, String resourceName, RrdDb rrdDb) {
64 this.metricName = metricName; 69 this.metricName = metricName;
70 + this.resourceName = resourceName;
65 this.rrdDb = rrdDb; 71 this.rrdDb = rrdDb;
66 } 72 }
67 73
...@@ -71,6 +77,11 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -71,6 +77,11 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
71 } 77 }
72 78
73 @Override 79 @Override
80 + public String resourceName() {
81 + return this.resourceName;
82 + }
83 +
84 + @Override
74 public void updateMetric(String metricType, double value) { 85 public void updateMetric(String metricType, double value) {
75 updateMetric(metricType, value, System.currentTimeMillis() / 1000L); 86 updateMetric(metricType, value, System.currentTimeMillis() / 1000L);
76 } 87 }
...@@ -83,7 +94,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -83,7 +94,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
83 sample.setValue(metricType, value); 94 sample.setValue(metricType, value);
84 sample.update(); 95 sample.update();
85 } catch (IOException e) { 96 } catch (IOException e) {
86 - e.printStackTrace(); 97 + log.error("Failed to update metric value due to {}", e.getMessage());
87 } 98 }
88 } 99 }
89 100
...@@ -106,7 +117,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -106,7 +117,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
106 }); 117 });
107 sample.update(); 118 sample.update();
108 } catch (IOException e) { 119 } catch (IOException e) {
109 - e.printStackTrace(); 120 + log.error("Failed to update metric values due to {}", e.getMessage());
110 } 121 }
111 } 122 }
112 123
...@@ -116,10 +127,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -116,10 +127,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
116 checkArgument(rrdDb.containsDs(metricType), NON_EXIST_METRIC); 127 checkArgument(rrdDb.containsDs(metricType), NON_EXIST_METRIC);
117 return rrdDb.getDatasource(metricType).getLastValue(); 128 return rrdDb.getDatasource(metricType).getLastValue();
118 } catch (IOException e) { 129 } catch (IOException e) {
119 - e.printStackTrace(); 130 + log.error("Failed to obtain metric value due to {}", e.getMessage());
120 - }
121 return 0D; 131 return 0D;
122 } 132 }
133 + }
123 134
124 @Override 135 @Override
125 public double[] recentMetrics(String metricType, int duration, TimeUnit unit) { 136 public double[] recentMetrics(String metricType, int duration, TimeUnit unit) {
...@@ -130,12 +141,15 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -130,12 +141,15 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
130 if (checkTimeRange(startTime, endTime)) { 141 if (checkTimeRange(startTime, endTime)) {
131 FetchRequest fr = rrdDb.createFetchRequest(CONSOL_FUNCTION, startTime, endTime); 142 FetchRequest fr = rrdDb.createFetchRequest(CONSOL_FUNCTION, startTime, endTime);
132 return arrangeDataPoints(fr.fetchData().getValues(metricType)); 143 return arrangeDataPoints(fr.fetchData().getValues(metricType));
144 + } else {
145 + log.warn("Data projection is out-of-range");
146 + return new double[0];
133 } 147 }
134 } catch (IOException e) { 148 } catch (IOException e) {
135 - e.printStackTrace(); 149 + log.error("Failed to obtain metric values due to {}", e.getMessage());
136 - }
137 return new double[0]; 150 return new double[0];
138 } 151 }
152 + }
139 153
140 @Override 154 @Override
141 public double minMetric(String metricType) { 155 public double minMetric(String metricType) {
...@@ -145,10 +159,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -145,10 +159,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
145 long startTime = endTime - SECONDS_OF_DAY + 1; 159 long startTime = endTime - SECONDS_OF_DAY + 1;
146 return minMetric(metricType, startTime, endTime); 160 return minMetric(metricType, startTime, endTime);
147 } catch (IOException e) { 161 } catch (IOException e) {
148 - e.printStackTrace(); 162 + log.error("Failed to obtain metric value due to {}", e.getMessage());
149 - }
150 return 0D; 163 return 0D;
151 } 164 }
165 + }
152 166
153 @Override 167 @Override
154 public double maxMetric(String metricType) { 168 public double maxMetric(String metricType) {
...@@ -158,10 +172,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -158,10 +172,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
158 long startTime = endTime - SECONDS_OF_DAY; 172 long startTime = endTime - SECONDS_OF_DAY;
159 return maxMetric(metricType, startTime, endTime); 173 return maxMetric(metricType, startTime, endTime);
160 } catch (IOException e) { 174 } catch (IOException e) {
161 - e.printStackTrace(); 175 + log.error("Failed to obtain metric value due to {}", e.getMessage());
162 - }
163 return 0D; 176 return 0D;
164 } 177 }
178 + }
165 179
166 @Override 180 @Override
167 public double[] metrics(String metricType) { 181 public double[] metrics(String metricType) {
...@@ -171,10 +185,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -171,10 +185,10 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
171 long startTime = endTime - SECONDS_OF_DAY; 185 long startTime = endTime - SECONDS_OF_DAY;
172 return metrics(metricType, startTime, endTime); 186 return metrics(metricType, startTime, endTime);
173 } catch (IOException e) { 187 } catch (IOException e) {
174 - e.printStackTrace(); 188 + log.error("Failed to obtain metric values due to {}", e.getMessage());
175 - }
176 return new double[0]; 189 return new double[0];
177 } 190 }
191 + }
178 192
179 @Override 193 @Override
180 public double[] metrics(String metricType, long startTime, long endTime) { 194 public double[] metrics(String metricType, long startTime, long endTime) {
...@@ -183,23 +197,26 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -183,23 +197,26 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
183 if (checkTimeRange(startTime, endTime)) { 197 if (checkTimeRange(startTime, endTime)) {
184 FetchRequest fr = rrdDb.createFetchRequest(CONSOL_FUNCTION, startTime, endTime); 198 FetchRequest fr = rrdDb.createFetchRequest(CONSOL_FUNCTION, startTime, endTime);
185 return arrangeDataPoints(fr.fetchData().getValues(metricType)); 199 return arrangeDataPoints(fr.fetchData().getValues(metricType));
200 + } else {
201 + log.warn("Data projection is out-of-range");
202 + return new double[0];
186 } 203 }
187 } catch (IOException e) { 204 } catch (IOException e) {
188 - e.printStackTrace(); 205 + log.error("Failed to obtain metric values due to {}", e.getMessage());
189 - }
190 return new double[0]; 206 return new double[0];
191 } 207 }
208 + }
192 209
193 @Override 210 @Override
194 public long lastUpdate(String metricType) { 211 public long lastUpdate(String metricType) {
195 try { 212 try {
196 checkArgument(rrdDb.containsDs(metricType), NON_EXIST_METRIC); 213 checkArgument(rrdDb.containsDs(metricType), NON_EXIST_METRIC);
197 - rrdDb.getLastUpdateTime(); 214 + return rrdDb.getLastUpdateTime();
198 } catch (IOException e) { 215 } catch (IOException e) {
199 - e.printStackTrace(); 216 + log.error("Failed to obtain last update time due to {}", e.getMessage());
200 - }
201 return 0L; 217 return 0L;
202 } 218 }
219 + }
203 220
204 // try to check whether projected time range is within a day 221 // try to check whether projected time range is within a day
205 private boolean checkTimeRange(long startTime, long endTime) { 222 private boolean checkTimeRange(long startTime, long endTime) {
...@@ -242,12 +259,15 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -242,12 +259,15 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
242 private static final int STEP_VALUE = 1; 259 private static final int STEP_VALUE = 1;
243 private static final int ROW_VALUE = 60 * 24; 260 private static final int ROW_VALUE = 60 * 24;
244 private static final String METRIC_NAME_MSG = "Must specify a metric name."; 261 private static final String METRIC_NAME_MSG = "Must specify a metric name.";
262 + private static final String RESOURCE_NAME_MSG = "Must specify a resource name.";
245 private static final String METRIC_TYPE_MSG = "Must supply at least a metric type."; 263 private static final String METRIC_TYPE_MSG = "Must supply at least a metric type.";
264 + private static final String SPLITTER = "_";
246 265
247 private RrdDb rrdDb; 266 private RrdDb rrdDb;
248 private RrdDef rrdDef; 267 private RrdDef rrdDef;
249 private List<DsDef> dsDefs; 268 private List<DsDef> dsDefs;
250 private String metricName; 269 private String metricName;
270 + private String resourceName;
251 271
252 public Builder() { 272 public Builder() {
253 // initialize data source definition list 273 // initialize data source definition list
...@@ -255,11 +275,14 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -255,11 +275,14 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
255 } 275 }
256 276
257 @Override 277 @Override
258 - public Builder withMetricName(String metricName) { 278 + public Builder withMetricName(String metric) {
259 - this.metricName = metricName; 279 + this.metricName = metric;
280 + return this;
281 + }
260 282
261 - // define the resolution of monitored metrics 283 + @Override
262 - rrdDef = new RrdDef(DB_PATH + "_" + metricName, RESOLUTION_IN_SECOND); 284 + public MetricsDatabase.Builder withResourceName(String resource) {
285 + this.resourceName = resource;
263 return this; 286 return this;
264 } 287 }
265 288
...@@ -272,8 +295,13 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -272,8 +295,13 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
272 @Override 295 @Override
273 public MetricsDatabase build() { 296 public MetricsDatabase build() {
274 checkNotNull(metricName, METRIC_NAME_MSG); 297 checkNotNull(metricName, METRIC_NAME_MSG);
298 + checkNotNull(resourceName, RESOURCE_NAME_MSG);
275 checkArgument(dsDefs.size() != 0, METRIC_TYPE_MSG); 299 checkArgument(dsDefs.size() != 0, METRIC_TYPE_MSG);
276 300
301 + // define the resolution of monitored metrics
302 + rrdDef = new RrdDef(DB_PATH + SPLITTER + metricName +
303 + SPLITTER + resourceName, RESOLUTION_IN_SECOND);
304 +
277 try { 305 try {
278 DsDef[] dsDefArray = new DsDef[dsDefs.size()]; 306 DsDef[] dsDefArray = new DsDef[dsDefs.size()];
279 IntStream.range(0, dsDefs.size()).forEach(i -> dsDefArray[i] = dsDefs.get(i)); 307 IntStream.range(0, dsDefs.size()).forEach(i -> dsDefArray[i] = dsDefs.get(i));
...@@ -292,7 +320,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { ...@@ -292,7 +320,7 @@ public final class DefaultMetricsDatabase implements MetricsDatabase {
292 e.printStackTrace(); 320 e.printStackTrace();
293 } 321 }
294 322
295 - return new DefaultMetricsDatabase(metricName, rrdDb); 323 + return new DefaultMetricsDatabase(metricName, resourceName, rrdDb);
296 } 324 }
297 325
298 private DsDef defineSchema(String metricType) { 326 private DsDef defineSchema(String metricType) {
......
...@@ -15,12 +15,18 @@ ...@@ -15,12 +15,18 @@
15 */ 15 */
16 package org.onosproject.cpman.impl; 16 package org.onosproject.cpman.impl;
17 17
18 +import com.google.common.collect.ImmutableSet;
19 +import com.google.common.collect.Maps;
18 import org.junit.Before; 20 import org.junit.Before;
19 import org.junit.Test; 21 import org.junit.Test;
22 +import org.onosproject.cpman.ControlMetricType;
23 +import org.onosproject.cpman.ControlResource;
20 import org.onosproject.cpman.MetricsDatabase; 24 import org.onosproject.cpman.MetricsDatabase;
25 +import org.onosproject.net.DeviceId;
21 26
22 import java.util.HashMap; 27 import java.util.HashMap;
23 import java.util.Map; 28 import java.util.Map;
29 +import java.util.Set;
24 import java.util.concurrent.TimeUnit; 30 import java.util.concurrent.TimeUnit;
25 31
26 import static org.hamcrest.Matchers.is; 32 import static org.hamcrest.Matchers.is;
...@@ -34,9 +40,11 @@ public class MetricsDatabaseTest { ...@@ -34,9 +40,11 @@ public class MetricsDatabaseTest {
34 private MetricsDatabase mdb; 40 private MetricsDatabase mdb;
35 private static final String CPU_METRIC = "cpu"; 41 private static final String CPU_METRIC = "cpu";
36 private static final String CPU_LOAD = "load"; 42 private static final String CPU_LOAD = "load";
43 + private static final String DEFAULT_RES = "resource";
37 private static final String MEMORY_METRIC = "memory"; 44 private static final String MEMORY_METRIC = "memory";
38 private static final String MEMORY_FREE_PERC = "freePerc"; 45 private static final String MEMORY_FREE_PERC = "freePerc";
39 private static final String MEMORY_USED_PERC = "usedPerc"; 46 private static final String MEMORY_USED_PERC = "usedPerc";
47 + private Map<DeviceId, MetricsDatabase> devMetricsMap;
40 48
41 /** 49 /**
42 * Initializes metrics database instance. 50 * Initializes metrics database instance.
...@@ -45,6 +53,7 @@ public class MetricsDatabaseTest { ...@@ -45,6 +53,7 @@ public class MetricsDatabaseTest {
45 public void setUp() { 53 public void setUp() {
46 mdb = new DefaultMetricsDatabase.Builder() 54 mdb = new DefaultMetricsDatabase.Builder()
47 .withMetricName(CPU_METRIC) 55 .withMetricName(CPU_METRIC)
56 + .withResourceName(DEFAULT_RES)
48 .addMetricType(CPU_LOAD) 57 .addMetricType(CPU_LOAD)
49 .build(); 58 .build();
50 } 59 }
...@@ -114,6 +123,7 @@ public class MetricsDatabaseTest { ...@@ -114,6 +123,7 @@ public class MetricsDatabaseTest {
114 public void testMultipleMetrics() { 123 public void testMultipleMetrics() {
115 MetricsDatabase multiMdb = new DefaultMetricsDatabase.Builder() 124 MetricsDatabase multiMdb = new DefaultMetricsDatabase.Builder()
116 .withMetricName(MEMORY_METRIC) 125 .withMetricName(MEMORY_METRIC)
126 + .withResourceName(DEFAULT_RES)
117 .addMetricType(MEMORY_FREE_PERC) 127 .addMetricType(MEMORY_FREE_PERC)
118 .addMetricType(MEMORY_USED_PERC) 128 .addMetricType(MEMORY_USED_PERC)
119 .build(); 129 .build();
...@@ -126,4 +136,51 @@ public class MetricsDatabaseTest { ...@@ -126,4 +136,51 @@ public class MetricsDatabaseTest {
126 assertThat(30D, is(multiMdb.recentMetric(MEMORY_FREE_PERC))); 136 assertThat(30D, is(multiMdb.recentMetric(MEMORY_FREE_PERC)));
127 assertThat(70D, is(multiMdb.recentMetric(MEMORY_USED_PERC))); 137 assertThat(70D, is(multiMdb.recentMetric(MEMORY_USED_PERC)));
128 } 138 }
139 +
140 + /**
141 + * Tests device metrics map update and query.
142 + */
143 + @Test
144 + public void testDeviceMetricsMap() {
145 + ControlResource.Type type = ControlResource.Type.CONTROL_MESSAGE;
146 + DeviceId devId1 = DeviceId.deviceId("of:0000000000000101");
147 + DeviceId devId2 = DeviceId.deviceId("of:0000000000000102");
148 +
149 + devMetricsMap = Maps.newHashMap();
150 +
151 + Set<DeviceId> devices = ImmutableSet.of(devId1, devId2);
152 + devices.forEach(dev ->
153 + devMetricsMap.putIfAbsent(dev,
154 + genMDbBuilder(type, ControlResource.CONTROL_MESSAGE_METRICS)
155 + .withResourceName(dev.toString())
156 + .build()));
157 +
158 + Map<String, Double> metrics1 = new HashMap<>();
159 + ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType ->
160 + metrics1.putIfAbsent(msgType.toString(), 10D));
161 +
162 + Map<String, Double> metrics2 = new HashMap<>();
163 + ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType ->
164 + metrics2.putIfAbsent(msgType.toString(), 20D));
165 +
166 +
167 + devMetricsMap.get(devId1).updateMetrics(metrics1);
168 + devMetricsMap.get(devId2).updateMetrics(metrics2);
169 +
170 + ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType ->
171 + assertThat(10D, is(devMetricsMap.get(devId1).recentMetric(msgType.toString())))
172 + );
173 +
174 + ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType ->
175 + assertThat(20D, is(devMetricsMap.get(devId2).recentMetric(msgType.toString())))
176 + );
177 + }
178 +
179 + private MetricsDatabase.Builder genMDbBuilder(ControlResource.Type resourceType,
180 + Set<ControlMetricType> metricTypes) {
181 + MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder();
182 + builder.withMetricName(resourceType.toString());
183 + metricTypes.forEach(type -> builder.addMetricType(type.toString()));
184 + return builder;
185 + }
129 } 186 }
......
...@@ -141,7 +141,7 @@ public class OpenFlowControlMessageAggregator implements Runnable { ...@@ -141,7 +141,7 @@ public class OpenFlowControlMessageAggregator implements Runnable {
141 * @return count value 141 * @return count value
142 */ 142 */
143 private long getCount(OFType type) { 143 private long getCount(OFType type) {
144 - return (long) countMeterMap.get(type).getOneMinuteRate() * 144 + return (long) (countMeterMap.get(type).getOneMinuteRate()
145 - EXECUTE_PERIOD_IN_SECOND; 145 + * EXECUTE_PERIOD_IN_SECOND);
146 } 146 }
147 } 147 }
......