blob: d32d2e07ddfdd04b29f568b1b460ecf9f32de528 [file] [log] [blame]
Jian Li60804322015-12-02 14:46:31 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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
Jian Li9f3a8852016-04-07 13:37:39 -070018import com.google.common.collect.ImmutableMap;
Jian Li7d180c52016-02-01 21:53:08 -080019import com.google.common.collect.ImmutableSet;
Jian Li85060ac2016-02-04 09:58:56 -080020import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
Jian Li60804322015-12-02 14:46:31 -080022import org.apache.felix.scr.annotations.Activate;
Jian Lic132c112016-01-28 20:27:34 -080023import org.apache.felix.scr.annotations.Component;
Jian Li60804322015-12-02 14:46:31 -080024import org.apache.felix.scr.annotations.Deactivate;
Jian Li7d180c52016-02-01 21:53:08 -080025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li60804322015-12-02 14:46:31 -080027import org.apache.felix.scr.annotations.Service;
Jian Li23906cc2016-03-31 11:16:44 -070028import org.onlab.util.KryoNamespace;
Jian Li7d180c52016-02-01 21:53:08 -080029import org.onosproject.cluster.ClusterService;
Jian Li60804322015-12-02 14:46:31 -080030import org.onosproject.cluster.NodeId;
Jian Li6b86a762016-01-29 09:30:40 -080031import org.onosproject.cpman.ControlLoad;
Jian Li67e1e152016-04-18 17:52:58 -070032import org.onosproject.cpman.ControlLoadSnapshot;
Jian Li6b86a762016-01-29 09:30:40 -080033import org.onosproject.cpman.ControlMetric;
34import org.onosproject.cpman.ControlMetricType;
Jian Li67e1e152016-04-18 17:52:58 -070035import org.onosproject.cpman.ControlMetricsRequest;
Jian Li6b86a762016-01-29 09:30:40 -080036import org.onosproject.cpman.ControlPlaneMonitorService;
Jian Li7d180c52016-02-01 21:53:08 -080037import org.onosproject.cpman.MetricsDatabase;
Jian Li60804322015-12-02 14:46:31 -080038import org.onosproject.net.DeviceId;
Jian Li23906cc2016-03-31 11:16:44 -070039import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
40import org.onosproject.store.cluster.messaging.MessageSubject;
41import org.onosproject.store.serializers.KryoNamespaces;
42import org.onosproject.store.service.Serializer;
Jian Li60804322015-12-02 14:46:31 -080043import org.slf4j.Logger;
Jian Li7d180c52016-02-01 21:53:08 -080044import org.slf4j.LoggerFactory;
Jian Li60804322015-12-02 14:46:31 -080045
Jian Li7d180c52016-02-01 21:53:08 -080046import java.util.Map;
Jian Li60804322015-12-02 14:46:31 -080047import java.util.Optional;
Jian Lic5cb4a12016-02-03 23:24:42 -080048import java.util.Set;
Jian Li23906cc2016-03-31 11:16:44 -070049import java.util.concurrent.CompletableFuture;
Jian Li67e1e152016-04-18 17:52:58 -070050import java.util.concurrent.TimeUnit;
Jian Li85060ac2016-02-04 09:58:56 -080051import java.util.stream.Collectors;
Jian Li60804322015-12-02 14:46:31 -080052
Jian Li23906cc2016-03-31 11:16:44 -070053import static com.google.common.base.Preconditions.checkArgument;
Jian Li23906cc2016-03-31 11:16:44 -070054import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
55import static org.onosproject.cpman.ControlResource.CPU_METRICS;
56import static org.onosproject.cpman.ControlResource.DISK_METRICS;
57import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
58import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
59import static org.onosproject.cpman.ControlResource.Type;
Jian Li60804322015-12-02 14:46:31 -080060
61/**
62 * Control plane monitoring service class.
63 */
64@Component(immediate = true)
65@Service
66public class ControlPlaneMonitor implements ControlPlaneMonitorService {
67
Jian Li7d180c52016-02-01 21:53:08 -080068 private final Logger log = LoggerFactory.getLogger(getClass());
69 private MetricsDatabase cpuMetrics;
70 private MetricsDatabase memoryMetrics;
71 private Map<DeviceId, MetricsDatabase> controlMessageMap;
72 private Map<String, MetricsDatabase> diskMetricsMap;
73 private Map<String, MetricsDatabase> networkMetricsMap;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected ClusterService clusterService;
77
Jian Li23906cc2016-03-31 11:16:44 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected ClusterCommunicationService communicationService;
80
Jian Lidaf55ea2016-04-04 20:38:30 -070081 private static final String DEFAULT_RESOURCE = "default";
82
Jian Li85060ac2016-02-04 09:58:56 -080083 private static final Set RESOURCE_TYPE_SET =
84 ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK);
Jian Li7d180c52016-02-01 21:53:08 -080085
Jian Li23906cc2016-03-31 11:16:44 -070086 private static final MessageSubject CONTROL_STATS =
Jian Li67e1e152016-04-18 17:52:58 -070087 new MessageSubject("control-plane-stats");
Jian Li23906cc2016-03-31 11:16:44 -070088
Jian Li7d180c52016-02-01 21:53:08 -080089 private Map<ControlMetricType, Double> cpuBuf;
90 private Map<ControlMetricType, Double> memoryBuf;
91 private Map<String, Map<ControlMetricType, Double>> diskBuf;
92 private Map<String, Map<ControlMetricType, Double>> networkBuf;
93 private Map<DeviceId, Map<ControlMetricType, Double>> ctrlMsgBuf;
Jian Li60804322015-12-02 14:46:31 -080094
Jian Li85060ac2016-02-04 09:58:56 -080095 private Map<Type, Set<String>> availableResourceMap;
96 private Set<DeviceId> availableDeviceIdSet;
97
Jian Li23906cc2016-03-31 11:16:44 -070098 private static final String METRIC_TYPE_NULL = "Control metric type cannot be null";
99
100 private static final Serializer SERIALIZER = Serializer
101 .using(new KryoNamespace.Builder()
102 .register(KryoNamespaces.API)
Jian Li67e1e152016-04-18 17:52:58 -0700103 .register(KryoNamespaces.BASIC)
Jian Li23906cc2016-03-31 11:16:44 -0700104 .register(ControlMetricsRequest.class)
Jian Li67e1e152016-04-18 17:52:58 -0700105 .register(ControlLoadSnapshot.class)
Jian Li23906cc2016-03-31 11:16:44 -0700106 .register(ControlMetricType.class)
Jian Li67e1e152016-04-18 17:52:58 -0700107 .register(TimeUnit.class)
Jian Li23906cc2016-03-31 11:16:44 -0700108 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build());
109
Jian Li60804322015-12-02 14:46:31 -0800110 @Activate
111 public void activate() {
Jian Lidaf55ea2016-04-04 20:38:30 -0700112 cpuMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.CPU, CPU_METRICS);
113 memoryMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.MEMORY, MEMORY_METRICS);
Jian Li85060ac2016-02-04 09:58:56 -0800114 controlMessageMap = Maps.newConcurrentMap();
115 diskMetricsMap = Maps.newConcurrentMap();
116 networkMetricsMap = Maps.newConcurrentMap();
Jian Li7d180c52016-02-01 21:53:08 -0800117
Jian Li85060ac2016-02-04 09:58:56 -0800118 cpuBuf = Maps.newConcurrentMap();
119 memoryBuf = Maps.newConcurrentMap();
120 diskBuf = Maps.newConcurrentMap();
121 networkBuf = Maps.newConcurrentMap();
122 ctrlMsgBuf = Maps.newConcurrentMap();
123
124 availableResourceMap = Maps.newConcurrentMap();
125 availableDeviceIdSet = Sets.newConcurrentHashSet();
Jian Li7d180c52016-02-01 21:53:08 -0800126
Jian Li67e1e152016-04-18 17:52:58 -0700127 communicationService.<ControlMetricsRequest, ControlLoadSnapshot>addSubscriber(CONTROL_STATS,
128 SERIALIZER::decode, this::handleRequest, SERIALIZER::encode);
Jian Li23906cc2016-03-31 11:16:44 -0700129
Jian Li7d180c52016-02-01 21:53:08 -0800130 log.info("Started");
Jian Li60804322015-12-02 14:46:31 -0800131 }
132
133 @Deactivate
134 public void deactivate() {
Jian Li7d180c52016-02-01 21:53:08 -0800135
136 // TODO: need to handle the mdb close.
137 cpuBuf.clear();
138 memoryBuf.clear();
139 diskBuf.clear();
140 networkBuf.clear();
141 ctrlMsgBuf.clear();
142
Jian Li23906cc2016-03-31 11:16:44 -0700143 communicationService.removeSubscriber(CONTROL_STATS);
144
Jian Li7d180c52016-02-01 21:53:08 -0800145 log.info("Stopped");
Jian Li60804322015-12-02 14:46:31 -0800146 }
147
Jian Li60804322015-12-02 14:46:31 -0800148 @Override
Jian Lic5cb4a12016-02-03 23:24:42 -0800149 public void updateMetric(ControlMetric cm, int updateIntervalInMinutes,
Jian Li60804322015-12-02 14:46:31 -0800150 Optional<DeviceId> deviceId) {
Jian Li7d180c52016-02-01 21:53:08 -0800151 if (deviceId.isPresent()) {
Jian Li46148902016-01-29 13:33:50 -0800152
Jian Li7d180c52016-02-01 21:53:08 -0800153 // insert a new device entry if we cannot find any
Jian Li85060ac2016-02-04 09:58:56 -0800154 ctrlMsgBuf.putIfAbsent(deviceId.get(), Maps.newConcurrentMap());
Jian Li7d180c52016-02-01 21:53:08 -0800155
156 // update control message metrics
Jian Li85060ac2016-02-04 09:58:56 -0800157 if (CONTROL_MESSAGE_METRICS.contains(cm.metricType())) {
158
159 if (!availableDeviceIdSet.contains(deviceId.get())) {
160 availableDeviceIdSet.add(deviceId.get());
161 }
Jian Li7d180c52016-02-01 21:53:08 -0800162
163 // we will accumulate the metric value into buffer first
164 ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(),
165 (double) cm.metricValue().getLoad());
166
167 // if buffer contains all control message metrics,
168 // we simply set and update the values into MetricsDatabase.
Jian Li85060ac2016-02-04 09:58:56 -0800169 if (ctrlMsgBuf.get(deviceId.get()).keySet()
170 .containsAll(CONTROL_MESSAGE_METRICS)) {
Jian Li7d180c52016-02-01 21:53:08 -0800171 updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get());
Jian Lidaf55ea2016-04-04 20:38:30 -0700172 ctrlMsgBuf.get(deviceId.get());
Jian Li7d180c52016-02-01 21:53:08 -0800173 }
174 }
175 } else {
176
177 // update cpu metrics
178 if (CPU_METRICS.contains(cm.metricType())) {
179 cpuBuf.putIfAbsent(cm.metricType(),
180 (double) cm.metricValue().getLoad());
181 if (cpuBuf.keySet().containsAll(CPU_METRICS)) {
182 cpuMetrics.updateMetrics(convertMap(cpuBuf));
183 cpuBuf.clear();
184 }
185 }
186
187 // update memory metrics
188 if (MEMORY_METRICS.contains(cm.metricType())) {
189 memoryBuf.putIfAbsent(cm.metricType(),
190 (double) cm.metricValue().getLoad());
191 if (memoryBuf.keySet().containsAll(MEMORY_METRICS)) {
192 memoryMetrics.updateMetrics(convertMap(memoryBuf));
193 memoryBuf.clear();
194 }
195 }
196 }
Jian Li60804322015-12-02 14:46:31 -0800197 }
198
199 @Override
Jian Lic5cb4a12016-02-03 23:24:42 -0800200 public void updateMetric(ControlMetric cm, int updateIntervalInMinutes,
Jian Lie044d1a2016-01-25 09:01:20 -0800201 String resourceName) {
Jian Li7d180c52016-02-01 21:53:08 -0800202 // update disk metrics
203 if (DISK_METRICS.contains(cm.metricType())) {
Jian Li85060ac2016-02-04 09:58:56 -0800204 diskBuf.putIfAbsent(resourceName, Maps.newConcurrentMap());
205
206 availableResourceMap.putIfAbsent(Type.DISK, Sets.newHashSet());
207 availableResourceMap.computeIfPresent(Type.DISK, (k, v) -> {
208 v.add(resourceName);
209 return v;
210 });
211
Jian Li7d180c52016-02-01 21:53:08 -0800212 diskBuf.get(resourceName).putIfAbsent(cm.metricType(),
213 (double) cm.metricValue().getLoad());
214 if (diskBuf.get(resourceName).keySet().containsAll(DISK_METRICS)) {
215 updateDiskMetrics(diskBuf.get(resourceName), resourceName);
216 diskBuf.clear();
217 }
218 }
Jian Lie044d1a2016-01-25 09:01:20 -0800219
Jian Li7d180c52016-02-01 21:53:08 -0800220 // update network metrics
221 if (NETWORK_METRICS.contains(cm.metricType())) {
Jian Li85060ac2016-02-04 09:58:56 -0800222 networkBuf.putIfAbsent(resourceName, Maps.newConcurrentMap());
223
224 availableResourceMap.putIfAbsent(Type.NETWORK, Sets.newHashSet());
225 availableResourceMap.computeIfPresent(Type.NETWORK, (k, v) -> {
226 v.add(resourceName);
227 return v;
228 });
229
Jian Li7d180c52016-02-01 21:53:08 -0800230 networkBuf.get(resourceName).putIfAbsent(cm.metricType(),
231 (double) cm.metricValue().getLoad());
232 if (networkBuf.get(resourceName).keySet().containsAll(NETWORK_METRICS)) {
233 updateNetworkMetrics(networkBuf.get(resourceName), resourceName);
234 networkBuf.clear();
235 }
236 }
Jian Lie044d1a2016-01-25 09:01:20 -0800237 }
238
239 @Override
Jian Li67e1e152016-04-18 17:52:58 -0700240 public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId,
241 ControlMetricType type,
242 Optional<DeviceId> deviceId) {
243 if (clusterService.getLocalNode().id().equals(nodeId)) {
244 return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, deviceId)));
245 } else {
246 return communicationService.sendAndReceive(createRequest(type, deviceId),
247 CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
248 }
249 }
250
251 @Override
252 public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId,
253 ControlMetricType type,
254 String resourceName) {
255 if (clusterService.getLocalNode().id().equals(nodeId)) {
256 return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, resourceName)));
257 } else {
258 return communicationService.sendAndReceive(createRequest(type, resourceName),
259 CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
260 }
261 }
262
263 @Override
264 public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId,
265 ControlMetricType type,
266 int duration, TimeUnit unit,
267 Optional<DeviceId> deviceId) {
268 if (clusterService.getLocalNode().id().equals(nodeId)) {
269 return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, deviceId), duration, unit));
270 } else {
271 return communicationService.sendAndReceive(createRequest(type, duration, unit, deviceId),
272 CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
273 }
274 }
275
276 @Override
277 public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId,
278 ControlMetricType type,
279 int duration, TimeUnit unit,
280 String resourceName) {
281 if (clusterService.getLocalNode().id().equals(nodeId)) {
282 return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, resourceName), duration, unit));
283 } else {
284 return communicationService.sendAndReceive(createRequest(type, duration, unit, resourceName),
285 CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
286 }
287 }
288
289 @Override
290 public Set<String> availableResources(Type resourceType) {
291 if (RESOURCE_TYPE_SET.contains(resourceType)) {
292 if (Type.CONTROL_MESSAGE.equals(resourceType)) {
293 return availableDeviceIdSet.stream().map(id ->
294 id.toString()).collect(Collectors.toSet());
295 } else {
296 Set<String> res = availableResourceMap.get(resourceType);
297 return res == null ? ImmutableSet.of() : res;
298 }
299 }
300 return ImmutableSet.of();
301 }
302
303 /**
304 * Builds and returns metric database instance with given resource name,
305 * resource type and metric type.
306 *
307 * @param resourceName resource name
308 * @param resourceType resource type
309 * @param metricTypes metric type
310 * @return metric database instance
311 */
312 private MetricsDatabase genMDbBuilder(String resourceName,
313 Type resourceType,
314 Set<ControlMetricType> metricTypes) {
315 MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder();
316 builder.withMetricName(resourceType.toString());
317 builder.withResourceName(resourceName);
318 metricTypes.forEach(type -> builder.addMetricType(type.toString()));
319 return builder.build();
320 }
321
322 /**
323 * Updates network metrics with given metric map and resource name.
324 *
325 * @param metricMap a metric map which is comprised of metric type and value
326 * @param resourceName resource name
327 */
328 private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap,
329 String resourceName) {
330 networkMetricsMap.putIfAbsent(resourceName, genMDbBuilder(resourceName,
331 Type.NETWORK, NETWORK_METRICS));
332 networkMetricsMap.get(resourceName).updateMetrics(convertMap(metricMap));
333 }
334
335 /**
336 * Updates disk metrics with given metric map and resource name.
337 *
338 * @param metricMap a metric map which is comprised of metric type and value
339 * @param resourceName resource name
340 */
341 private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap,
342 String resourceName) {
343 diskMetricsMap.putIfAbsent(resourceName, genMDbBuilder(resourceName,
344 Type.DISK, DISK_METRICS));
345 diskMetricsMap.get(resourceName).updateMetrics(convertMap(metricMap));
346 }
347
348 /**
349 * Updates control message metrics with given metric map and device identifier.
350 *
351 * @param metricMap a metric map which is comprised of metric type and value
352 * @param deviceId device identifier
353 */
354 private void updateControlMessages(Map<ControlMetricType, Double> metricMap,
355 DeviceId deviceId) {
356 controlMessageMap.putIfAbsent(deviceId, genMDbBuilder(deviceId.toString(),
357 Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS));
358 controlMessageMap.get(deviceId).updateMetrics(convertMap(metricMap));
359 }
360
361 /**
362 * Converts metric map into a new map which contains string formatted metric type as key.
363 *
364 * @param metricMap metric map in which ControlMetricType is key
365 * @return a new map in which string formatted metric type is key
366 */
367 private Map<String, Double> convertMap(Map<ControlMetricType, Double> metricMap) {
368 if (metricMap == null) {
369 return ImmutableMap.of();
370 }
371 Map newMap = Maps.newConcurrentMap();
372 metricMap.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v));
373 return newMap;
374 }
375
376 /**
377 * Handles control metric request from remote node.
378 *
379 * @param request control metric request
380 * @return completable future object of control load snapshot
381 */
382 private CompletableFuture<ControlLoadSnapshot> handleRequest(ControlMetricsRequest request) {
383
384 checkArgument(request.getType() != null, METRIC_TYPE_NULL);
385
386 ControlLoad load;
387 if (request.getResourceName() != null && request.getUnit() != null) {
388 load = getLocalLoad(request.getType(), request.getResourceName());
389 } else {
390 load = getLocalLoad(request.getType(), request.getDeviceId());
391 }
392
393 long average;
394 if (request.getUnit() != null) {
395 average = load.average(request.getDuration(), request.getUnit());
396 } else {
397 average = load.average();
398 }
399 ControlLoadSnapshot resp =
400 new ControlLoadSnapshot(load.latest(), average, load.time());
401 return CompletableFuture.completedFuture(resp);
402 }
403
404 /**
405 * Generates a control metric request.
406 *
407 * @param type control metric type
408 * @param deviceId device identifier
409 * @return control metric request instance
410 */
411 private ControlMetricsRequest createRequest(ControlMetricType type,
412 Optional<DeviceId> deviceId) {
413 return new ControlMetricsRequest(type, deviceId);
414 }
415
416 /**
417 * Generates a control metric request with given projected time range.
418 *
419 * @param type control metric type
420 * @param duration projected time duration
421 * @param unit projected time unit
422 * @param deviceId device identifier
423 * @return control metric request instance
424 */
425 private ControlMetricsRequest createRequest(ControlMetricType type,
426 int duration, TimeUnit unit,
427 Optional<DeviceId> deviceId) {
428 return new ControlMetricsRequest(type, duration, unit, deviceId);
429 }
430
431 /**
432 * Generates a control metric request.
433 *
434 * @param type control metric type
435 * @param resourceName resource name
436 * @return control metric request instance
437 */
438 private ControlMetricsRequest createRequest(ControlMetricType type,
439 String resourceName) {
440 return new ControlMetricsRequest(type, resourceName);
441 }
442
443 /**
444 * Generates a control metric request with given projected time range.
445 *
446 * @param type control metric type
447 * @param duration projected time duration
448 * @param unit projected time unit
449 * @param resourceName resource name
450 * @return control metric request instance
451 */
452 private ControlMetricsRequest createRequest(ControlMetricType type,
453 int duration, TimeUnit unit,
454 String resourceName) {
455 return new ControlMetricsRequest(type, duration, unit, resourceName);
456 }
457
458 /**
459 * Returns a snapshot of control load.
460 *
461 * @param cl control load
462 * @return a snapshot of control load
463 */
464 private ControlLoadSnapshot snapshot(ControlLoad cl) {
465 if (cl != null) {
466 return new ControlLoadSnapshot(cl.latest(), cl.average(), cl.time());
467 }
468 return null;
469 }
470
471 /**
472 * Returns a snapshot of control load with given projected time range.
473 *
474 * @param cl control load
475 * @param duration projected time duration
476 * @param unit projected time unit
477 * @return a snapshot of control load
478 */
479 private ControlLoadSnapshot snapshot(ControlLoad cl, int duration, TimeUnit unit) {
480 if (cl != null) {
481 return new ControlLoadSnapshot(cl.latest(), cl.average(duration, unit), cl.time());
482 }
483 return null;
484 }
485
486 /**
487 * Returns local control load.
488 *
489 * @param type metric type
490 * @param deviceId device identifier
491 * @return control load
492 */
493 private ControlLoad getLocalLoad(ControlMetricType type,
494 Optional<DeviceId> deviceId) {
Jian Li23906cc2016-03-31 11:16:44 -0700495 if (deviceId.isPresent()) {
Jian Li67e1e152016-04-18 17:52:58 -0700496 // returns control message stats
Jian Li23906cc2016-03-31 11:16:44 -0700497 if (CONTROL_MESSAGE_METRICS.contains(type) &&
498 availableDeviceIdSet.contains(deviceId.get())) {
499 return new DefaultControlLoad(controlMessageMap.get(deviceId.get()), type);
Jian Li7d180c52016-02-01 21:53:08 -0800500 }
501 } else {
Jian Li23906cc2016-03-31 11:16:44 -0700502 // returns controlLoad of CPU metrics
503 if (CPU_METRICS.contains(type)) {
504 return new DefaultControlLoad(cpuMetrics, type);
505 }
506
507 // returns memoryLoad of memory metrics
508 if (MEMORY_METRICS.contains(type)) {
509 return new DefaultControlLoad(memoryMetrics, type);
510 }
Jian Li7d180c52016-02-01 21:53:08 -0800511 }
Jian Li60804322015-12-02 14:46:31 -0800512 return null;
513 }
514
Jian Li67e1e152016-04-18 17:52:58 -0700515 /**
516 * Returns local control load.
517 *
518 * @param type metric type
519 * @param resourceName resource name
520 * @return control load
521 */
522 private ControlLoad getLocalLoad(ControlMetricType type, String resourceName) {
523 // returns disk I/O stats
Jian Li23906cc2016-03-31 11:16:44 -0700524 if (DISK_METRICS.contains(type) &&
525 availableResources(Type.DISK).contains(resourceName)) {
526 return new DefaultControlLoad(diskMetricsMap.get(resourceName), type);
527 }
Jian Li7d180c52016-02-01 21:53:08 -0800528
Jian Li67e1e152016-04-18 17:52:58 -0700529 // returns network I/O stats
Jian Li23906cc2016-03-31 11:16:44 -0700530 if (NETWORK_METRICS.contains(type) &&
531 availableResources(Type.NETWORK).contains(resourceName)) {
532 return new DefaultControlLoad(networkMetricsMap.get(resourceName), type);
Jian Li7d180c52016-02-01 21:53:08 -0800533 }
Jian Li60804322015-12-02 14:46:31 -0800534 return null;
535 }
Jian Li23906cc2016-03-31 11:16:44 -0700536}