Jian Li
Committed by Gerrit Code Review

[ONOS-3538] Implement control metrics distribution logic

- Revise getLoad method to getLocalLoad
- Add new getRemoteLoad methods
- Add the capability to query remote control load

CLI and REST will be implemented in a separated patch

Change-Id: I62d4f4ab891d6d3e95cedd5af9e4ab71949c05ec
...@@ -20,6 +20,7 @@ import org.onosproject.net.DeviceId; ...@@ -20,6 +20,7 @@ import org.onosproject.net.DeviceId;
20 20
21 import java.util.Optional; 21 import java.util.Optional;
22 import java.util.Set; 22 import java.util.Set;
23 +import java.util.concurrent.CompletableFuture;
23 24
24 import static org.onosproject.cpman.ControlResource.*; 25 import static org.onosproject.cpman.ControlResource.*;
25 26
...@@ -51,29 +52,50 @@ public interface ControlPlaneMonitorService { ...@@ -51,29 +52,50 @@ public interface ControlPlaneMonitorService {
51 String resourceName); 52 String resourceName);
52 53
53 /** 54 /**
54 - * Obtains the control plane load of a specific device. 55 + * Obtains local control plane load of a specific device.
55 * The metrics range from control messages and system metrics 56 * The metrics range from control messages and system metrics
56 * (e.g., CPU and memory info) 57 * (e.g., CPU and memory info)
57 * 58 *
58 - * @param nodeId node identifier
59 * @param type control metric type 59 * @param type control metric type
60 * @param deviceId device identifier 60 * @param deviceId device identifier
61 * @return control plane load 61 * @return control plane load
62 */ 62 */
63 - ControlLoad getLoad(NodeId nodeId, ControlMetricType type, 63 + ControlLoad getLocalLoad(ControlMetricType type, Optional<DeviceId> deviceId);
64 - Optional<DeviceId> deviceId);
65 64
66 /** 65 /**
67 - * Obtains the control plane load of a specific device. 66 + * Obtains local control plane load of a specific resource.
68 * The metrics range from I/O device metrics 67 * The metrics range from I/O device metrics
69 * (e.g., disk and network interface) 68 * (e.g., disk and network interface)
70 * 69 *
71 - * @param nodeId node identifier
72 * @param type control metric type 70 * @param type control metric type
73 * @param resourceName resource name 71 * @param resourceName resource name
74 * @return control plane load 72 * @return control plane load
75 */ 73 */
76 - ControlLoad getLoad(NodeId nodeId, ControlMetricType type, String resourceName); 74 + ControlLoad getLocalLoad(ControlMetricType type, String resourceName);
75 +
76 + /**
77 + * Obtains remote control plane load of a specific device.
78 + *
79 + * @param nodeId node identifier
80 + * @param type control metric type
81 + * @param deviceId device identifier
82 + * @return completable future object of control load
83 + */
84 + CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId,
85 + ControlMetricType type,
86 + Optional<DeviceId> deviceId);
87 +
88 + /**
89 + * Obtains remote control plane load of a specific resource.
90 + *
91 + * @param nodeId node identifier
92 + * @param type control metric type
93 + * @param resourceName resource name
94 + * @return completable future object of control load
95 + */
96 + CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId,
97 + ControlMetricType type,
98 + String resourceName);
77 99
78 /** 100 /**
79 * Obtains a list of names of available resources. 101 * Obtains a list of names of available resources.
......
...@@ -101,11 +101,11 @@ public class ControlMetricsStatsListCommand extends AbstractShellCommand { ...@@ -101,11 +101,11 @@ public class ControlMetricsStatsListCommand extends AbstractShellCommand {
101 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId, 101 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
102 Set<ControlMetricType> typeSet, String name, DeviceId did) { 102 Set<ControlMetricType> typeSet, String name, DeviceId did) {
103 if (name == null && did == null) { 103 if (name == null && did == null) {
104 - typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, Optional.ofNullable(null)))); 104 + typeSet.forEach(s -> print(s, service.getLocalLoad(s, Optional.ofNullable(null))));
105 } else if (name == null && did != null) { 105 } else if (name == null && did != null) {
106 - typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, Optional.of(did)))); 106 + typeSet.forEach(s -> print(s, service.getLocalLoad(s, Optional.of(did))));
107 } else if (name != null && did == null) { 107 } else if (name != null && did == null) {
108 - typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, name))); 108 + typeSet.forEach(s -> print(s, service.getLocalLoad(s, name)));
109 } 109 }
110 } 110 }
111 111
......
1 +/*
2 + * Copyright 2016 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.impl;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import org.onosproject.cpman.ControlMetricType;
20 +import org.onosproject.net.DeviceId;
21 +
22 +import java.util.Objects;
23 +import java.util.Optional;
24 +
25 +import static com.google.common.base.MoreObjects.toStringHelper;
26 +
27 +/**
28 + * A container class that is used to request control metric of remote node.
29 + */
30 +public class ControlMetricsRequest {
31 + private final ControlMetricType type;
32 + private Optional<DeviceId> deviceId;
33 + private String resourceName;
34 +
35 + /**
36 + * Instantiates a new control metric request with the given control metric
37 + * type and device identifier.
38 + *
39 + * @param type control metric type
40 + * @param deviceId device identifier
41 + */
42 + public ControlMetricsRequest(ControlMetricType type, Optional<DeviceId> deviceId) {
43 + this.type = type;
44 + this.deviceId = deviceId;
45 + }
46 +
47 + /**
48 + * Instantiates a new control metric request with the given control metric
49 + * type and resource name.
50 + *
51 + * @param type control metric type
52 + * @param resourceName resource name
53 + */
54 + public ControlMetricsRequest(ControlMetricType type, String resourceName) {
55 + this.type = type;
56 + this.resourceName = resourceName;
57 + }
58 +
59 + /**
60 + * Obtains control metric type.
61 + *
62 + * @return control metric type
63 + */
64 + public ControlMetricType getType() {
65 + return type;
66 + }
67 +
68 + /**
69 + * Obtains resource name.
70 + *
71 + * @return resource name
72 + */
73 + public String getResourceName() {
74 + return resourceName;
75 + }
76 +
77 + /**
78 + * Obtains device identifier.
79 + *
80 + * @return device identifier
81 + */
82 + public Optional<DeviceId> getDeviceId() {
83 + return deviceId;
84 + }
85 +
86 + @Override
87 + public int hashCode() {
88 + return Objects.hash(type, deviceId, resourceName);
89 + }
90 +
91 + @Override
92 + public boolean equals(Object obj) {
93 + if (this == obj) {
94 + return true;
95 + }
96 + if (obj instanceof ControlMetricsRequest) {
97 + final ControlMetricsRequest other = (ControlMetricsRequest) obj;
98 + return Objects.equals(this.type, other.type) &&
99 + Objects.equals(this.deviceId, other.deviceId) &&
100 + Objects.equals(this.resourceName, other.resourceName);
101 + }
102 + return false;
103 + }
104 +
105 + @Override
106 + public String toString() {
107 + MoreObjects.ToStringHelper helper;
108 + helper = toStringHelper(this)
109 + .add("type", type)
110 + .add("resourceName", resourceName);
111 + if (deviceId != null) {
112 + helper.add("deviceId", deviceId.get());
113 + }
114 + return helper.toString();
115 + }
116 +}
...@@ -24,8 +24,8 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -24,8 +24,8 @@ import org.apache.felix.scr.annotations.Deactivate;
24 import org.apache.felix.scr.annotations.Reference; 24 import org.apache.felix.scr.annotations.Reference;
25 import org.apache.felix.scr.annotations.ReferenceCardinality; 25 import org.apache.felix.scr.annotations.ReferenceCardinality;
26 import org.apache.felix.scr.annotations.Service; 26 import org.apache.felix.scr.annotations.Service;
27 +import org.onlab.util.KryoNamespace;
27 import org.onosproject.cluster.ClusterService; 28 import org.onosproject.cluster.ClusterService;
28 -import org.onosproject.cluster.ControllerNode;
29 import org.onosproject.cluster.NodeId; 29 import org.onosproject.cluster.NodeId;
30 import org.onosproject.cpman.ControlLoad; 30 import org.onosproject.cpman.ControlLoad;
31 import org.onosproject.cpman.ControlMetric; 31 import org.onosproject.cpman.ControlMetric;
...@@ -33,15 +33,29 @@ import org.onosproject.cpman.ControlMetricType; ...@@ -33,15 +33,29 @@ import org.onosproject.cpman.ControlMetricType;
33 import org.onosproject.cpman.ControlPlaneMonitorService; 33 import org.onosproject.cpman.ControlPlaneMonitorService;
34 import org.onosproject.cpman.MetricsDatabase; 34 import org.onosproject.cpman.MetricsDatabase;
35 import org.onosproject.net.DeviceId; 35 import org.onosproject.net.DeviceId;
36 +import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
37 +import org.onosproject.store.cluster.messaging.MessageSubject;
38 +import org.onosproject.store.serializers.KryoNamespaces;
39 +import org.onosproject.store.service.Serializer;
36 import org.slf4j.Logger; 40 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory; 41 import org.slf4j.LoggerFactory;
38 42
39 import java.util.Map; 43 import java.util.Map;
40 import java.util.Optional; 44 import java.util.Optional;
41 import java.util.Set; 45 import java.util.Set;
46 +import java.util.concurrent.CompletableFuture;
47 +import java.util.concurrent.ExecutorService;
48 +import java.util.concurrent.Executors;
42 import java.util.stream.Collectors; 49 import java.util.stream.Collectors;
43 50
44 -import static org.onosproject.cpman.ControlResource.*; 51 +import static com.google.common.base.Preconditions.checkArgument;
52 +import static org.onlab.util.Tools.groupedThreads;
53 +import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
54 +import static org.onosproject.cpman.ControlResource.CPU_METRICS;
55 +import static org.onosproject.cpman.ControlResource.DISK_METRICS;
56 +import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
57 +import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
58 +import static org.onosproject.cpman.ControlResource.Type;
45 59
46 /** 60 /**
47 * Control plane monitoring service class. 61 * Control plane monitoring service class.
...@@ -60,9 +74,15 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -60,9 +74,15 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected ClusterService clusterService; 75 protected ClusterService clusterService;
62 76
77 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 + protected ClusterCommunicationService communicationService;
79 +
63 private static final Set RESOURCE_TYPE_SET = 80 private static final Set RESOURCE_TYPE_SET =
64 ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK); 81 ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK);
65 82
83 + private static final MessageSubject CONTROL_STATS =
84 + new MessageSubject("control-plane-stats");
85 +
66 private Map<ControlMetricType, Double> cpuBuf; 86 private Map<ControlMetricType, Double> cpuBuf;
67 private Map<ControlMetricType, Double> memoryBuf; 87 private Map<ControlMetricType, Double> memoryBuf;
68 private Map<String, Map<ControlMetricType, Double>> diskBuf; 88 private Map<String, Map<ControlMetricType, Double>> diskBuf;
...@@ -72,6 +92,19 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -72,6 +92,19 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
72 private Map<Type, Set<String>> availableResourceMap; 92 private Map<Type, Set<String>> availableResourceMap;
73 private Set<DeviceId> availableDeviceIdSet; 93 private Set<DeviceId> availableDeviceIdSet;
74 94
95 + private ExecutorService messageHandlingExecutor;
96 +
97 + private static final String METRIC_TYPE_NULL = "Control metric type cannot be null";
98 +
99 + private static final Serializer SERIALIZER = Serializer
100 + .using(new KryoNamespace.Builder()
101 + .register(KryoNamespaces.API)
102 + .register(ControlMetricsRequest.class)
103 + .register(DefaultControlLoad.class)
104 + .register(DefaultMetricsDatabase.class)
105 + .register(ControlMetricType.class)
106 + .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build());
107 +
75 @Activate 108 @Activate
76 public void activate() { 109 public void activate() {
77 cpuMetrics = genMDbBuilder(Type.CPU, CPU_METRICS); 110 cpuMetrics = genMDbBuilder(Type.CPU, CPU_METRICS);
...@@ -89,6 +122,12 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -89,6 +122,12 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
89 availableResourceMap = Maps.newConcurrentMap(); 122 availableResourceMap = Maps.newConcurrentMap();
90 availableDeviceIdSet = Sets.newConcurrentHashSet(); 123 availableDeviceIdSet = Sets.newConcurrentHashSet();
91 124
125 + messageHandlingExecutor = Executors.newSingleThreadScheduledExecutor(
126 + groupedThreads("onos/app/cpman", "message-handlers"));
127 +
128 + communicationService.addSubscriber(CONTROL_STATS,
129 + SERIALIZER::decode, this::handleRequest, messageHandlingExecutor);
130 +
92 log.info("Started"); 131 log.info("Started");
93 } 132 }
94 133
...@@ -102,6 +141,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -102,6 +141,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
102 networkBuf.clear(); 141 networkBuf.clear();
103 ctrlMsgBuf.clear(); 142 ctrlMsgBuf.clear();
104 143
144 + communicationService.removeSubscriber(CONTROL_STATS);
145 +
105 log.info("Stopped"); 146 log.info("Stopped");
106 } 147 }
107 148
...@@ -197,11 +238,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -197,11 +238,8 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
197 } 238 }
198 239
199 @Override 240 @Override
200 - public ControlLoad getLoad(NodeId nodeId, ControlMetricType type, 241 + public ControlLoad getLocalLoad(ControlMetricType type,
201 Optional<DeviceId> deviceId) { 242 Optional<DeviceId> deviceId) {
202 - ControllerNode node = clusterService.getNode(nodeId);
203 - if (clusterService.getLocalNode().equals(node)) {
204 -
205 if (deviceId.isPresent()) { 243 if (deviceId.isPresent()) {
206 if (CONTROL_MESSAGE_METRICS.contains(type) && 244 if (CONTROL_MESSAGE_METRICS.contains(type) &&
207 availableDeviceIdSet.contains(deviceId.get())) { 245 availableDeviceIdSet.contains(deviceId.get())) {
...@@ -218,17 +256,11 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -218,17 +256,11 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
218 return new DefaultControlLoad(memoryMetrics, type); 256 return new DefaultControlLoad(memoryMetrics, type);
219 } 257 }
220 } 258 }
221 - } else {
222 - // TODO: currently only query the metrics of local node
223 - return null;
224 - }
225 return null; 259 return null;
226 } 260 }
227 261
228 @Override 262 @Override
229 - public ControlLoad getLoad(NodeId nodeId, ControlMetricType type, 263 + public ControlLoad getLocalLoad(ControlMetricType type, String resourceName) {
230 - String resourceName) {
231 - if (clusterService.getLocalNode().id().equals(nodeId)) {
232 if (DISK_METRICS.contains(type) && 264 if (DISK_METRICS.contains(type) &&
233 availableResources(Type.DISK).contains(resourceName)) { 265 availableResources(Type.DISK).contains(resourceName)) {
234 return new DefaultControlLoad(diskMetricsMap.get(resourceName), type); 266 return new DefaultControlLoad(diskMetricsMap.get(resourceName), type);
...@@ -238,14 +270,27 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -238,14 +270,27 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
238 availableResources(Type.NETWORK).contains(resourceName)) { 270 availableResources(Type.NETWORK).contains(resourceName)) {
239 return new DefaultControlLoad(networkMetricsMap.get(resourceName), type); 271 return new DefaultControlLoad(networkMetricsMap.get(resourceName), type);
240 } 272 }
241 - } else {
242 - // TODO: currently only query the metrics of local node
243 return null; 273 return null;
244 } 274 }
245 - return null; 275 +
276 + @Override
277 + public CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId,
278 + ControlMetricType type,
279 + Optional<DeviceId> deviceId) {
280 + return communicationService.sendAndReceive(createRequest(type, deviceId),
281 + CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
246 } 282 }
247 283
248 @Override 284 @Override
285 + public CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId,
286 + ControlMetricType type,
287 + String resourceName) {
288 + return communicationService.sendAndReceive(createRequest(type, resourceName),
289 + CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
290 + }
291 +
292 +
293 + @Override
249 public Set<String> availableResources(Type resourceType) { 294 public Set<String> availableResources(Type resourceType) {
250 if (RESOURCE_TYPE_SET.contains(resourceType)) { 295 if (RESOURCE_TYPE_SET.contains(resourceType)) {
251 if (Type.CONTROL_MESSAGE.equals(resourceType)) { 296 if (Type.CONTROL_MESSAGE.equals(resourceType)) {
...@@ -291,4 +336,27 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService { ...@@ -291,4 +336,27 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
291 map.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v)); 336 map.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v));
292 return newMap; 337 return newMap;
293 } 338 }
339 +
340 + private CompletableFuture<ControlLoad> handleRequest(ControlMetricsRequest request) {
341 +
342 + checkArgument(request.getType() != null, METRIC_TYPE_NULL);
343 +
344 + ControlLoad load;
345 + if (request.getResourceName() != null) {
346 + load = getLocalLoad(request.getType(), request.getResourceName());
347 + } else {
348 + load = getLocalLoad(request.getType(), request.getDeviceId());
349 + }
350 + return CompletableFuture.completedFuture(load);
351 + }
352 +
353 + private ControlMetricsRequest createRequest(ControlMetricType type,
354 + Optional<DeviceId> deviceId) {
355 + return new ControlMetricsRequest(type, deviceId);
356 + }
357 +
358 + private ControlMetricsRequest createRequest(ControlMetricType type,
359 + String resourceName) {
360 + return new ControlMetricsRequest(type, resourceName);
361 + }
294 } 362 }
......
...@@ -241,17 +241,17 @@ public class ControlMetricsWebResource extends AbstractWebResource { ...@@ -241,17 +241,17 @@ public class ControlMetricsWebResource extends AbstractWebResource {
241 if (name == null && did == null) { 241 if (name == null && did == null) {
242 typeSet.forEach(type -> { 242 typeSet.forEach(type -> {
243 ObjectNode metricNode = mapper().createObjectNode(); 243 ObjectNode metricNode = mapper().createObjectNode();
244 - ControlLoad load = service.getLoad(nodeId, type, Optional.ofNullable(null)); 244 + ControlLoad load = service.getLocalLoad(type, Optional.ofNullable(null));
245 if (load != null) { 245 if (load != null) {
246 metricNode.set(type.toString().toLowerCase(), codec(ControlLoad.class) 246 metricNode.set(type.toString().toLowerCase(), codec(ControlLoad.class)
247 - .encode(service.getLoad(nodeId, type, Optional.ofNullable(null)), this)); 247 + .encode(service.getLocalLoad(type, Optional.ofNullable(null)), this));
248 metricsNode.add(metricNode); 248 metricsNode.add(metricNode);
249 } 249 }
250 }); 250 });
251 } else if (name == null) { 251 } else if (name == null) {
252 typeSet.forEach(type -> { 252 typeSet.forEach(type -> {
253 ObjectNode metricNode = mapper().createObjectNode(); 253 ObjectNode metricNode = mapper().createObjectNode();
254 - ControlLoad load = service.getLoad(nodeId, type, Optional.of(did)); 254 + ControlLoad load = service.getLocalLoad(type, Optional.of(did));
255 if (load != null) { 255 if (load != null) {
256 metricNode.set(type.toString().toLowerCase(), 256 metricNode.set(type.toString().toLowerCase(),
257 codec(ControlLoad.class).encode(load, this)); 257 codec(ControlLoad.class).encode(load, this));
...@@ -261,7 +261,7 @@ public class ControlMetricsWebResource extends AbstractWebResource { ...@@ -261,7 +261,7 @@ public class ControlMetricsWebResource extends AbstractWebResource {
261 } else if (did == null) { 261 } else if (did == null) {
262 typeSet.forEach(type -> { 262 typeSet.forEach(type -> {
263 ObjectNode metricNode = mapper().createObjectNode(); 263 ObjectNode metricNode = mapper().createObjectNode();
264 - ControlLoad load = service.getLoad(nodeId, type, name); 264 + ControlLoad load = service.getLocalLoad(type, name);
265 if (load != null) { 265 if (load != null) {
266 metricNode.set(type.toString().toLowerCase(), 266 metricNode.set(type.toString().toLowerCase(),
267 codec(ControlLoad.class).encode(load, this)); 267 codec(ControlLoad.class).encode(load, this));
......
...@@ -26,6 +26,8 @@ import org.onosproject.cpman.ControlMetric; ...@@ -26,6 +26,8 @@ import org.onosproject.cpman.ControlMetric;
26 import org.onosproject.cpman.ControlMetricType; 26 import org.onosproject.cpman.ControlMetricType;
27 import org.onosproject.cpman.MetricValue; 27 import org.onosproject.cpman.MetricValue;
28 import org.onosproject.net.DeviceId; 28 import org.onosproject.net.DeviceId;
29 +import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
30 +import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
29 31
30 import java.util.Optional; 32 import java.util.Optional;
31 import java.util.Set; 33 import java.util.Set;
...@@ -36,8 +38,12 @@ import static org.easymock.EasyMock.expect; ...@@ -36,8 +38,12 @@ import static org.easymock.EasyMock.expect;
36 import static org.easymock.EasyMock.replay; 38 import static org.easymock.EasyMock.replay;
37 import static org.hamcrest.Matchers.is; 39 import static org.hamcrest.Matchers.is;
38 import static org.junit.Assert.assertThat; 40 import static org.junit.Assert.assertThat;
39 - 41 +import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
40 -import static org.onosproject.cpman.ControlResource.*; 42 +import static org.onosproject.cpman.ControlResource.CPU_METRICS;
43 +import static org.onosproject.cpman.ControlResource.DISK_METRICS;
44 +import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
45 +import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
46 +import static org.onosproject.cpman.ControlResource.Type;
41 47
42 /** 48 /**
43 * Unit test of control plane monitoring service. 49 * Unit test of control plane monitoring service.
...@@ -48,6 +54,7 @@ public class ControlPlaneMonitorTest { ...@@ -48,6 +54,7 @@ public class ControlPlaneMonitorTest {
48 private static final Integer UPDATE_INTERVAL = 1; 54 private static final Integer UPDATE_INTERVAL = 1;
49 private ClusterService mockClusterService; 55 private ClusterService mockClusterService;
50 private ControllerNode mockControllerNode; 56 private ControllerNode mockControllerNode;
57 + private ClusterCommunicationService mockCommunicationService;
51 private NodeId nodeId; 58 private NodeId nodeId;
52 59
53 /** 60 /**
...@@ -56,7 +63,9 @@ public class ControlPlaneMonitorTest { ...@@ -56,7 +63,9 @@ public class ControlPlaneMonitorTest {
56 @Before 63 @Before
57 public void setup() { 64 public void setup() {
58 monitor = new ControlPlaneMonitor(); 65 monitor = new ControlPlaneMonitor();
59 - monitor.activate(); 66 +
67 + mockCommunicationService = new ClusterCommunicationServiceAdapter();
68 + monitor.communicationService = mockCommunicationService;
60 69
61 nodeId = new NodeId("1"); 70 nodeId = new NodeId("1");
62 mockControllerNode = new MockControllerNode(nodeId); 71 mockControllerNode = new MockControllerNode(nodeId);
...@@ -68,6 +77,8 @@ public class ControlPlaneMonitorTest { ...@@ -68,6 +77,8 @@ public class ControlPlaneMonitorTest {
68 expect(mockClusterService.getLocalNode()) 77 expect(mockClusterService.getLocalNode())
69 .andReturn(mockControllerNode).anyTimes(); 78 .andReturn(mockControllerNode).anyTimes();
70 replay(mockClusterService); 79 replay(mockClusterService);
80 +
81 + monitor.activate();
71 } 82 }
72 83
73 /** 84 /**
...@@ -102,7 +113,7 @@ public class ControlPlaneMonitorTest { ...@@ -102,7 +113,7 @@ public class ControlPlaneMonitorTest {
102 } 113 }
103 114
104 private void testLoadMetricWithoutId(ControlMetricType cmt, MetricValue mv) { 115 private void testLoadMetricWithoutId(ControlMetricType cmt, MetricValue mv) {
105 - assertThat(monitor.getLoad(nodeId, cmt, Optional.ofNullable(null)).latest(), is(mv.getLoad())); 116 + assertThat(monitor.getLocalLoad(cmt, Optional.ofNullable(null)).latest(), is(mv.getLoad()));
106 } 117 }
107 118
108 private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) { 119 private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
...@@ -111,7 +122,7 @@ public class ControlPlaneMonitorTest { ...@@ -111,7 +122,7 @@ public class ControlPlaneMonitorTest {
111 } 122 }
112 123
113 private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) { 124 private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
114 - assertThat(monitor.getLoad(nodeId, cmt, resourceName).latest(), is(mv.getLoad())); 125 + assertThat(monitor.getLocalLoad(cmt, resourceName).latest(), is(mv.getLoad()));
115 } 126 }
116 127
117 private void testUpdateMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) { 128 private void testUpdateMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) {
...@@ -120,7 +131,7 @@ public class ControlPlaneMonitorTest { ...@@ -120,7 +131,7 @@ public class ControlPlaneMonitorTest {
120 } 131 }
121 132
122 private void testLoadMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) { 133 private void testLoadMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) {
123 - assertThat(monitor.getLoad(nodeId, cmt, Optional.of(did)).latest(), is(mv.getLoad())); 134 + assertThat(monitor.getLocalLoad(cmt, Optional.of(did)).latest(), is(mv.getLoad()));
124 } 135 }
125 136
126 /** 137 /**
......
...@@ -25,6 +25,7 @@ import org.onosproject.net.DeviceId; ...@@ -25,6 +25,7 @@ import org.onosproject.net.DeviceId;
25 25
26 import java.util.Optional; 26 import java.util.Optional;
27 import java.util.Set; 27 import java.util.Set;
28 +import java.util.concurrent.CompletableFuture;
28 29
29 /** 30 /**
30 * Test adapter control plane monitoring service. 31 * Test adapter control plane monitoring service.
...@@ -32,23 +33,38 @@ import java.util.Set; ...@@ -32,23 +33,38 @@ import java.util.Set;
32 public class ControlPlaneMonitorServiceAdaptor implements ControlPlaneMonitorService { 33 public class ControlPlaneMonitorServiceAdaptor implements ControlPlaneMonitorService {
33 @Override 34 @Override
34 public void updateMetric(ControlMetric controlMetric, 35 public void updateMetric(ControlMetric controlMetric,
35 - int updateIntervalInMinutes, Optional<DeviceId> deviceId) { 36 + int updateIntervalInMinutes,
37 + Optional<DeviceId> deviceId) {
36 } 38 }
37 39
38 @Override 40 @Override
39 public void updateMetric(ControlMetric controlMetric, 41 public void updateMetric(ControlMetric controlMetric,
40 - int updateIntervalInMinutes, String resourceName) { 42 + int updateIntervalInMinutes,
43 + String resourceName) {
41 } 44 }
42 45
43 @Override 46 @Override
44 - public ControlLoad getLoad(NodeId nodeId, 47 + public ControlLoad getLocalLoad(ControlMetricType type,
45 - ControlMetricType type, Optional<DeviceId> deviceId) { 48 + Optional<DeviceId> deviceId) {
46 return null; 49 return null;
47 } 50 }
48 51
49 @Override 52 @Override
50 - public ControlLoad getLoad(NodeId nodeId, 53 + public ControlLoad getLocalLoad(ControlMetricType type, String resourceName) {
51 - ControlMetricType type, String resourceName) { 54 + return null;
55 + }
56 +
57 + @Override
58 + public CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId,
59 + ControlMetricType type,
60 + Optional<DeviceId> deviceId) {
61 + return null;
62 + }
63 +
64 + @Override
65 + public CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId,
66 + ControlMetricType type,
67 + String resourceName) {
52 return null; 68 return null;
53 } 69 }
54 70
......
...@@ -182,7 +182,7 @@ public class ControlMetricsResourceTest extends JerseyTest { ...@@ -182,7 +182,7 @@ public class ControlMetricsResourceTest extends JerseyTest {
182 public void testResourcePopulatedArray() { 182 public void testResourcePopulatedArray() {
183 expect(mockControlPlaneMonitorService.availableResources(anyObject())) 183 expect(mockControlPlaneMonitorService.availableResources(anyObject()))
184 .andReturn(resourceSet).once(); 184 .andReturn(resourceSet).once();
185 - expect(mockControlPlaneMonitorService.getLoad(anyObject(), anyObject(), 185 + expect(mockControlPlaneMonitorService.getLocalLoad(anyObject(),
186 anyString())).andReturn(null).times(4); 186 anyString())).andReturn(null).times(4);
187 replay(mockControlPlaneMonitorService); 187 replay(mockControlPlaneMonitorService);
188 188
......