Support to query available resources from remote node
Change-Id: I465327143b5959b9e18daac9481ffea332f889c8
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java
index fe0aae2..07e669e 100644
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java
@@ -21,6 +21,7 @@
import org.apache.karaf.shell.console.completer.StringsCompleter;
import org.onosproject.cli.AbstractCompleter;
import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cluster.NodeId;
import org.onosproject.cpman.ControlPlaneMonitorService;
import org.onosproject.cpman.ControlResource;
import org.slf4j.Logger;
@@ -40,7 +41,7 @@
private static final String NETWORK = "network";
private static final String DISK = "disk";
private static final String CONTROL_MESSAGE = "control_message";
- Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE);
+ private final Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE);
private static final String INVALID_MSG = "Invalid type name";
@@ -51,7 +52,8 @@
// Resource type is the second argument.
ArgumentCompleter.ArgumentList list = getArgumentList();
- String type = list.getArguments()[1];
+ String nodeId = list.getArguments()[1];
+ String type = list.getArguments()[2];
if (resourceTypes.contains(type)) {
ControlPlaneMonitorService monitorService =
@@ -60,13 +62,16 @@
Set<String> set = Sets.newHashSet();
switch (type) {
case NETWORK:
- set = monitorService.availableResources(ControlResource.Type.NETWORK);
+ set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId),
+ ControlResource.Type.NETWORK);
break;
case DISK:
- set = monitorService.availableResources(ControlResource.Type.DISK);
+ set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId),
+ ControlResource.Type.DISK);
break;
case CONTROL_MESSAGE:
- set = monitorService.availableResources(ControlResource.Type.CONTROL_MESSAGE);
+ set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId),
+ ControlResource.Type.CONTROL_MESSAGE);
break;
default:
log.warn(INVALID_MSG);
@@ -76,7 +81,7 @@
SortedSet<String> strings = delegate.getStrings();
if (set.size() != 0) {
- set.forEach(s -> strings.add(s));
+ set.forEach(strings::add);
}
}
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/gui/CpmanViewMessageHandler.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/gui/CpmanViewMessageHandler.java
index 0ba1f83..22ad36c 100644
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/gui/CpmanViewMessageHandler.java
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/gui/CpmanViewMessageHandler.java
@@ -25,6 +25,7 @@
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDateTime;
import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
import org.onosproject.cpman.ControlLoadSnapshot;
import org.onosproject.cpman.ControlMetricType;
import org.onosproject.cpman.ControlPlaneMonitorService;
@@ -96,10 +97,11 @@
ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
ClusterService cs = get(ClusterService.class);
DeviceService ds = get(DeviceService.class);
+ NodeId localNodeId = cs.getLocalNode().id();
if (!Strings.isNullOrEmpty(uri)) {
DeviceId deviceId = DeviceId.deviceId(uri);
- if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) {
+ if (cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE).contains(deviceId.toString())) {
Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId);
LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT);
@@ -110,7 +112,7 @@
attachDeviceList(cm, deviceIds);
}
} else {
- Set<String> deviceIds = cpms.availableResources(CONTROL_MESSAGE);
+ Set<String> deviceIds = cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE);
for (String deviceId : deviceIds) {
Map<ControlMetricType, Long> data =
populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlPlaneMonitor.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlPlaneMonitor.java
index dff72db..37d542d 100644
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlPlaneMonitor.java
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlPlaneMonitor.java
@@ -34,6 +34,8 @@
import org.onosproject.cpman.ControlMetricType;
import org.onosproject.cpman.ControlMetricsRequest;
import org.onosproject.cpman.ControlPlaneMonitorService;
+import org.onosproject.cpman.ControlResource;
+import org.onosproject.cpman.ControlResourceRequest;
import org.onosproject.cpman.MetricsDatabase;
import org.onosproject.net.DeviceId;
import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
@@ -86,6 +88,9 @@
private static final MessageSubject CONTROL_STATS =
new MessageSubject("control-plane-stats");
+ private static final MessageSubject CONTROL_RESOURCE =
+ new MessageSubject("control-plane-resources");
+
private Map<ControlMetricType, Double> cpuBuf;
private Map<ControlMetricType, Double> memoryBuf;
private Map<String, Map<ControlMetricType, Double>> diskBuf;
@@ -96,14 +101,17 @@
private Set<DeviceId> availableDeviceIdSet;
private static final String METRIC_TYPE_NULL = "Control metric type cannot be null";
+ private static final String RESOURCE_TYPE_NULL = "Control resource type cannot be null";
private static final Serializer SERIALIZER = Serializer
.using(new KryoNamespace.Builder()
.register(KryoNamespaces.API)
.register(KryoNamespaces.BASIC)
.register(ControlMetricsRequest.class)
+ .register(ControlResourceRequest.class)
.register(ControlLoadSnapshot.class)
.register(ControlMetricType.class)
+ .register(ControlResource.Type.class)
.register(TimeUnit.class)
.nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build());
@@ -125,7 +133,10 @@
availableDeviceIdSet = Sets.newConcurrentHashSet();
communicationService.<ControlMetricsRequest, ControlLoadSnapshot>addSubscriber(CONTROL_STATS,
- SERIALIZER::decode, this::handleRequest, SERIALIZER::encode);
+ SERIALIZER::decode, this::handleMetricsRequest, SERIALIZER::encode);
+
+ communicationService.<ControlResourceRequest, Set<String>>addSubscriber(CONTROL_RESOURCE,
+ SERIALIZER::decode, this::handleResourceRequest, SERIALIZER::encode);
log.info("Started");
}
@@ -141,6 +152,7 @@
ctrlMsgBuf.clear();
communicationService.removeSubscriber(CONTROL_STATS);
+ communicationService.removeSubscriber(CONTROL_RESOURCE);
log.info("Stopped");
}
@@ -243,7 +255,7 @@
if (clusterService.getLocalNode().id().equals(nodeId)) {
return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, deviceId)));
} else {
- return communicationService.sendAndReceive(createRequest(type, deviceId),
+ return communicationService.sendAndReceive(createMetricsRequest(type, deviceId),
CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
}
}
@@ -255,7 +267,7 @@
if (clusterService.getLocalNode().id().equals(nodeId)) {
return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, resourceName)));
} else {
- return communicationService.sendAndReceive(createRequest(type, resourceName),
+ return communicationService.sendAndReceive(createMetricsRequest(type, resourceName),
CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
}
}
@@ -268,7 +280,7 @@
if (clusterService.getLocalNode().id().equals(nodeId)) {
return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, deviceId), duration, unit));
} else {
- return communicationService.sendAndReceive(createRequest(type, duration, unit, deviceId),
+ return communicationService.sendAndReceive(createMetricsRequest(type, duration, unit, deviceId),
CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
}
}
@@ -281,23 +293,21 @@
if (clusterService.getLocalNode().id().equals(nodeId)) {
return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, resourceName), duration, unit));
} else {
- return communicationService.sendAndReceive(createRequest(type, duration, unit, resourceName),
+ return communicationService.sendAndReceive(createMetricsRequest(type, duration, unit, resourceName),
CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId);
}
}
@Override
- public Set<String> availableResources(Type resourceType) {
- if (RESOURCE_TYPE_SET.contains(resourceType)) {
- if (Type.CONTROL_MESSAGE.equals(resourceType)) {
- return availableDeviceIdSet.stream().map(id ->
- id.toString()).collect(Collectors.toSet());
- } else {
- Set<String> res = availableResourceMap.get(resourceType);
- return res == null ? ImmutableSet.of() : res;
- }
+ public CompletableFuture<Set<String>> availableResources(NodeId nodeId,
+ Type resourceType) {
+ if (clusterService.getLocalNode().id().equals(nodeId)) {
+ Set<String> resources = getLocalAvailableResources(resourceType);
+ return CompletableFuture.completedFuture(resources);
+ } else {
+ return communicationService.sendAndReceive(createResourceRequest(resourceType),
+ CONTROL_RESOURCE, SERIALIZER::encode, SERIALIZER::decode, nodeId);
}
- return ImmutableSet.of();
}
/**
@@ -385,7 +395,8 @@
* @param request control metric request
* @return completable future object of control load snapshot
*/
- private CompletableFuture<ControlLoadSnapshot> handleRequest(ControlMetricsRequest request) {
+ private CompletableFuture<ControlLoadSnapshot>
+ handleMetricsRequest(ControlMetricsRequest request) {
checkArgument(request.getType() != null, METRIC_TYPE_NULL);
@@ -408,14 +419,29 @@
}
/**
+ * Handles control resource request from remote node.
+ *
+ * @param request control resource type
+ * @return completable future object of control resource set
+ */
+ private CompletableFuture<Set<String>>
+ handleResourceRequest(ControlResourceRequest request) {
+
+ checkArgument(request.getType() != null, RESOURCE_TYPE_NULL);
+
+ Set<String> resources = getLocalAvailableResources(request.getType());
+ return CompletableFuture.completedFuture(resources);
+ }
+
+ /**
* Generates a control metric request.
*
* @param type control metric type
* @param deviceId device identifier
* @return control metric request instance
*/
- private ControlMetricsRequest createRequest(ControlMetricType type,
- Optional<DeviceId> deviceId) {
+ private ControlMetricsRequest createMetricsRequest(ControlMetricType type,
+ Optional<DeviceId> deviceId) {
return new ControlMetricsRequest(type, deviceId);
}
@@ -428,9 +454,9 @@
* @param deviceId device identifier
* @return control metric request instance
*/
- private ControlMetricsRequest createRequest(ControlMetricType type,
- int duration, TimeUnit unit,
- Optional<DeviceId> deviceId) {
+ private ControlMetricsRequest createMetricsRequest(ControlMetricType type,
+ int duration, TimeUnit unit,
+ Optional<DeviceId> deviceId) {
return new ControlMetricsRequest(type, duration, unit, deviceId);
}
@@ -441,8 +467,8 @@
* @param resourceName resource name
* @return control metric request instance
*/
- private ControlMetricsRequest createRequest(ControlMetricType type,
- String resourceName) {
+ private ControlMetricsRequest createMetricsRequest(ControlMetricType type,
+ String resourceName) {
return new ControlMetricsRequest(type, resourceName);
}
@@ -455,13 +481,23 @@
* @param resourceName resource name
* @return control metric request instance
*/
- private ControlMetricsRequest createRequest(ControlMetricType type,
- int duration, TimeUnit unit,
- String resourceName) {
+ private ControlMetricsRequest createMetricsRequest(ControlMetricType type,
+ int duration, TimeUnit unit,
+ String resourceName) {
return new ControlMetricsRequest(type, duration, unit, resourceName);
}
/**
+ * Generates a control resource request with given resource type.
+ *
+ * @param type control resource type
+ * @return control resource request instance
+ */
+ private ControlResourceRequest createResourceRequest(ControlResource.Type type) {
+ return new ControlResourceRequest(type);
+ }
+
+ /**
* Returns a snapshot of control load.
*
* @param cl control load
@@ -528,17 +564,39 @@
* @return control load
*/
private ControlLoad getLocalLoad(ControlMetricType type, String resourceName) {
+ NodeId localNodeId = clusterService.getLocalNode().id();
+
// returns disk I/O stats
if (DISK_METRICS.contains(type) &&
- availableResources(Type.DISK).contains(resourceName)) {
+ availableResourcesSync(localNodeId, Type.DISK).contains(resourceName)) {
return new DefaultControlLoad(diskMetricsMap.get(resourceName), type);
}
// returns network I/O stats
if (NETWORK_METRICS.contains(type) &&
- availableResources(Type.NETWORK).contains(resourceName)) {
+ availableResourcesSync(localNodeId, Type.NETWORK).contains(resourceName)) {
return new DefaultControlLoad(networkMetricsMap.get(resourceName), type);
}
return null;
}
+
+ /**
+ * Obtains the available resource list from local node.
+ *
+ * @param resourceType control resource type
+ * @return a set of available control resource
+ */
+ private Set<String> getLocalAvailableResources(Type resourceType) {
+ Set<String> resources = ImmutableSet.of();
+ if (RESOURCE_TYPE_SET.contains(resourceType)) {
+ if (Type.CONTROL_MESSAGE.equals(resourceType)) {
+ resources = ImmutableSet.copyOf(availableDeviceIdSet.stream()
+ .map(DeviceId::toString).collect(Collectors.toSet()));
+ } else {
+ Set<String> res = availableResourceMap.get(resourceType);
+ resources = res == null ? ImmutableSet.of() : res;
+ }
+ }
+ return resources;
+ }
}
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/ControlMetricsWebResource.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/ControlMetricsWebResource.java
index 5e985b0..b18a6b5 100644
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/ControlMetricsWebResource.java
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/ControlMetricsWebResource.java
@@ -54,7 +54,7 @@
public class ControlMetricsWebResource extends AbstractWebResource {
private final ControlPlaneMonitorService monitorService =
- get(ControlPlaneMonitorService.class);
+ get(ControlPlaneMonitorService.class);
private final ClusterService clusterService = get(ClusterService.class);
private final NodeId localNodeId = clusterService.getLocalNode().id();
private final ObjectNode root = mapper().createObjectNode();
@@ -72,7 +72,7 @@
public Response controlMessageMetrics() {
ArrayNode deviceNodes = root.putArray("devices");
- monitorService.availableResources(CONTROL_MESSAGE).forEach(name -> {
+ monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> {
ObjectNode deviceNode = mapper().createObjectNode();
ObjectNode valueNode = mapper().createObjectNode();
@@ -147,7 +147,7 @@
public Response diskMetrics() {
ArrayNode diskNodes = root.putArray("disks");
- monitorService.availableResources(DISK).forEach(name -> {
+ monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> {
ObjectNode diskNode = mapper().createObjectNode();
ObjectNode valueNode = mapper().createObjectNode();
@@ -173,7 +173,7 @@
public Response networkMetrics() {
ArrayNode networkNodes = root.putArray("networks");
- monitorService.availableResources(NETWORK).forEach(name -> {
+ monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> {
ObjectNode networkNode = mapper().createObjectNode();
ObjectNode valueNode = mapper().createObjectNode();