Jian Li
Committed by Gerrit Code Review

Support to query available resources from remote node

Change-Id: I465327143b5959b9e18daac9481ffea332f889c8
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onosproject.cpman; 16 package org.onosproject.cpman;
17 17
18 +import com.google.common.collect.ImmutableSet;
19 +import org.onlab.util.Tools;
18 import org.onosproject.cluster.NodeId; 20 import org.onosproject.cluster.NodeId;
19 import org.onosproject.net.DeviceId; 21 import org.onosproject.net.DeviceId;
20 22
...@@ -30,6 +32,8 @@ import static org.onosproject.cpman.ControlResource.Type; ...@@ -30,6 +32,8 @@ import static org.onosproject.cpman.ControlResource.Type;
30 */ 32 */
31 public interface ControlPlaneMonitorService { 33 public interface ControlPlaneMonitorService {
32 34
35 + long TIMEOUT_MILLIS = 2000;
36 +
33 /** 37 /**
34 * Adds a new control metric value with a certain update interval. 38 * Adds a new control metric value with a certain update interval.
35 * 39 *
...@@ -116,8 +120,22 @@ public interface ControlPlaneMonitorService { ...@@ -116,8 +120,22 @@ public interface ControlPlaneMonitorService {
116 /** 120 /**
117 * Obtains a list of names of available resources. 121 * Obtains a list of names of available resources.
118 * 122 *
123 + * @param nodeId node identifier
124 + * @param resourceType resource type
125 + * @return completable future object of a collection of available resource names
126 + */
127 + CompletableFuture<Set<String>> availableResources(NodeId nodeId, Type resourceType);
128 +
129 + /**
130 + * Synchronous version of availableResource.
131 + * Obtains a list of names of available resources.
132 + *
133 + * @param nodeId node identifier
119 * @param resourceType resource type 134 * @param resourceType resource type
120 - * @return a collection of names of available resources 135 + * @return a collection of available resource names
121 */ 136 */
122 - Set<String> availableResources(Type resourceType); 137 + default Set<String> availableResourcesSync(NodeId nodeId, Type resourceType) {
138 + return Tools.futureGetOrElse(availableResources(nodeId, resourceType),
139 + TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, ImmutableSet.of());
140 + }
123 } 141 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +/*
2 + * Copyright 2016-present 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;
17 +
18 +import com.google.common.base.MoreObjects;
19 +
20 +import java.util.Objects;
21 +
22 +import static com.google.common.base.MoreObjects.toStringHelper;
23 +
24 +/**
25 + * A container class that is used to request available resource of remote node.
26 + */
27 +public class ControlResourceRequest {
28 + private final ControlResource.Type type;
29 +
30 + /**
31 + * Instantiates a new control resource request of the control resource type.
32 + *
33 + * @param type control resource type
34 + */
35 + public ControlResourceRequest(ControlResource.Type type) {
36 + this.type = type;
37 + }
38 +
39 + /**
40 + * Obtains control resource type.
41 + *
42 + * @return control resource type
43 + */
44 + public ControlResource.Type getType() {
45 + return type;
46 + }
47 +
48 + @Override
49 + public int hashCode() {
50 + return Objects.hash(type);
51 + }
52 +
53 + @Override
54 + public boolean equals(Object obj) {
55 + if (this == obj) {
56 + return true;
57 + }
58 + if (obj instanceof ControlResourceRequest) {
59 + final ControlResourceRequest other = (ControlResourceRequest) obj;
60 + return Objects.equals(this.type, other.type);
61 + }
62 + return false;
63 + }
64 +
65 + @Override
66 + public String toString() {
67 + MoreObjects.ToStringHelper helper;
68 + helper = toStringHelper(this)
69 + .add("type", type);
70 + return helper.toString();
71 + }
72 +}
...@@ -21,6 +21,7 @@ import org.apache.karaf.shell.console.completer.ArgumentCompleter; ...@@ -21,6 +21,7 @@ import org.apache.karaf.shell.console.completer.ArgumentCompleter;
21 import org.apache.karaf.shell.console.completer.StringsCompleter; 21 import org.apache.karaf.shell.console.completer.StringsCompleter;
22 import org.onosproject.cli.AbstractCompleter; 22 import org.onosproject.cli.AbstractCompleter;
23 import org.onosproject.cli.AbstractShellCommand; 23 import org.onosproject.cli.AbstractShellCommand;
24 +import org.onosproject.cluster.NodeId;
24 import org.onosproject.cpman.ControlPlaneMonitorService; 25 import org.onosproject.cpman.ControlPlaneMonitorService;
25 import org.onosproject.cpman.ControlResource; 26 import org.onosproject.cpman.ControlResource;
26 import org.slf4j.Logger; 27 import org.slf4j.Logger;
...@@ -40,7 +41,7 @@ public class ResourceNameCompleter extends AbstractCompleter { ...@@ -40,7 +41,7 @@ public class ResourceNameCompleter extends AbstractCompleter {
40 private static final String NETWORK = "network"; 41 private static final String NETWORK = "network";
41 private static final String DISK = "disk"; 42 private static final String DISK = "disk";
42 private static final String CONTROL_MESSAGE = "control_message"; 43 private static final String CONTROL_MESSAGE = "control_message";
43 - Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE); 44 + private final Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE);
44 private static final String INVALID_MSG = "Invalid type name"; 45 private static final String INVALID_MSG = "Invalid type name";
45 46
46 47
...@@ -51,7 +52,8 @@ public class ResourceNameCompleter extends AbstractCompleter { ...@@ -51,7 +52,8 @@ public class ResourceNameCompleter extends AbstractCompleter {
51 52
52 // Resource type is the second argument. 53 // Resource type is the second argument.
53 ArgumentCompleter.ArgumentList list = getArgumentList(); 54 ArgumentCompleter.ArgumentList list = getArgumentList();
54 - String type = list.getArguments()[1]; 55 + String nodeId = list.getArguments()[1];
56 + String type = list.getArguments()[2];
55 57
56 if (resourceTypes.contains(type)) { 58 if (resourceTypes.contains(type)) {
57 ControlPlaneMonitorService monitorService = 59 ControlPlaneMonitorService monitorService =
...@@ -60,13 +62,16 @@ public class ResourceNameCompleter extends AbstractCompleter { ...@@ -60,13 +62,16 @@ public class ResourceNameCompleter extends AbstractCompleter {
60 Set<String> set = Sets.newHashSet(); 62 Set<String> set = Sets.newHashSet();
61 switch (type) { 63 switch (type) {
62 case NETWORK: 64 case NETWORK:
63 - set = monitorService.availableResources(ControlResource.Type.NETWORK); 65 + set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId),
66 + ControlResource.Type.NETWORK);
64 break; 67 break;
65 case DISK: 68 case DISK:
66 - set = monitorService.availableResources(ControlResource.Type.DISK); 69 + set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId),
70 + ControlResource.Type.DISK);
67 break; 71 break;
68 case CONTROL_MESSAGE: 72 case CONTROL_MESSAGE:
69 - set = monitorService.availableResources(ControlResource.Type.CONTROL_MESSAGE); 73 + set = monitorService.availableResourcesSync(NodeId.nodeId(nodeId),
74 + ControlResource.Type.CONTROL_MESSAGE);
70 break; 75 break;
71 default: 76 default:
72 log.warn(INVALID_MSG); 77 log.warn(INVALID_MSG);
...@@ -76,7 +81,7 @@ public class ResourceNameCompleter extends AbstractCompleter { ...@@ -76,7 +81,7 @@ public class ResourceNameCompleter extends AbstractCompleter {
76 SortedSet<String> strings = delegate.getStrings(); 81 SortedSet<String> strings = delegate.getStrings();
77 82
78 if (set.size() != 0) { 83 if (set.size() != 0) {
79 - set.forEach(s -> strings.add(s)); 84 + set.forEach(strings::add);
80 } 85 }
81 } 86 }
82 87
......
...@@ -25,6 +25,7 @@ import org.apache.commons.lang.ArrayUtils; ...@@ -25,6 +25,7 @@ import org.apache.commons.lang.ArrayUtils;
25 import org.apache.commons.lang3.StringUtils; 25 import org.apache.commons.lang3.StringUtils;
26 import org.joda.time.LocalDateTime; 26 import org.joda.time.LocalDateTime;
27 import org.onosproject.cluster.ClusterService; 27 import org.onosproject.cluster.ClusterService;
28 +import org.onosproject.cluster.NodeId;
28 import org.onosproject.cpman.ControlLoadSnapshot; 29 import org.onosproject.cpman.ControlLoadSnapshot;
29 import org.onosproject.cpman.ControlMetricType; 30 import org.onosproject.cpman.ControlMetricType;
30 import org.onosproject.cpman.ControlPlaneMonitorService; 31 import org.onosproject.cpman.ControlPlaneMonitorService;
...@@ -96,10 +97,11 @@ public class CpmanViewMessageHandler extends UiMessageHandler { ...@@ -96,10 +97,11 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
96 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class); 97 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
97 ClusterService cs = get(ClusterService.class); 98 ClusterService cs = get(ClusterService.class);
98 DeviceService ds = get(DeviceService.class); 99 DeviceService ds = get(DeviceService.class);
100 + NodeId localNodeId = cs.getLocalNode().id();
99 101
100 if (!Strings.isNullOrEmpty(uri)) { 102 if (!Strings.isNullOrEmpty(uri)) {
101 DeviceId deviceId = DeviceId.deviceId(uri); 103 DeviceId deviceId = DeviceId.deviceId(uri);
102 - if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) { 104 + if (cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE).contains(deviceId.toString())) {
103 Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId); 105 Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId);
104 LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT); 106 LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT);
105 107
...@@ -110,7 +112,7 @@ public class CpmanViewMessageHandler extends UiMessageHandler { ...@@ -110,7 +112,7 @@ public class CpmanViewMessageHandler extends UiMessageHandler {
110 attachDeviceList(cm, deviceIds); 112 attachDeviceList(cm, deviceIds);
111 } 113 }
112 } else { 114 } else {
113 - Set<String> deviceIds = cpms.availableResources(CONTROL_MESSAGE); 115 + Set<String> deviceIds = cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE);
114 for (String deviceId : deviceIds) { 116 for (String deviceId : deviceIds) {
115 Map<ControlMetricType, Long> data = 117 Map<ControlMetricType, Long> data =
116 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId)); 118 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
......
...@@ -54,7 +54,7 @@ import static org.onosproject.cpman.ControlResource.Type.NETWORK; ...@@ -54,7 +54,7 @@ import static org.onosproject.cpman.ControlResource.Type.NETWORK;
54 public class ControlMetricsWebResource extends AbstractWebResource { 54 public class ControlMetricsWebResource extends AbstractWebResource {
55 55
56 private final ControlPlaneMonitorService monitorService = 56 private final ControlPlaneMonitorService monitorService =
57 - get(ControlPlaneMonitorService.class); 57 + get(ControlPlaneMonitorService.class);
58 private final ClusterService clusterService = get(ClusterService.class); 58 private final ClusterService clusterService = get(ClusterService.class);
59 private final NodeId localNodeId = clusterService.getLocalNode().id(); 59 private final NodeId localNodeId = clusterService.getLocalNode().id();
60 private final ObjectNode root = mapper().createObjectNode(); 60 private final ObjectNode root = mapper().createObjectNode();
...@@ -72,7 +72,7 @@ public class ControlMetricsWebResource extends AbstractWebResource { ...@@ -72,7 +72,7 @@ public class ControlMetricsWebResource extends AbstractWebResource {
72 public Response controlMessageMetrics() { 72 public Response controlMessageMetrics() {
73 73
74 ArrayNode deviceNodes = root.putArray("devices"); 74 ArrayNode deviceNodes = root.putArray("devices");
75 - monitorService.availableResources(CONTROL_MESSAGE).forEach(name -> { 75 + monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> {
76 ObjectNode deviceNode = mapper().createObjectNode(); 76 ObjectNode deviceNode = mapper().createObjectNode();
77 ObjectNode valueNode = mapper().createObjectNode(); 77 ObjectNode valueNode = mapper().createObjectNode();
78 78
...@@ -147,7 +147,7 @@ public class ControlMetricsWebResource extends AbstractWebResource { ...@@ -147,7 +147,7 @@ public class ControlMetricsWebResource extends AbstractWebResource {
147 public Response diskMetrics() { 147 public Response diskMetrics() {
148 148
149 ArrayNode diskNodes = root.putArray("disks"); 149 ArrayNode diskNodes = root.putArray("disks");
150 - monitorService.availableResources(DISK).forEach(name -> { 150 + monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> {
151 ObjectNode diskNode = mapper().createObjectNode(); 151 ObjectNode diskNode = mapper().createObjectNode();
152 ObjectNode valueNode = mapper().createObjectNode(); 152 ObjectNode valueNode = mapper().createObjectNode();
153 153
...@@ -173,7 +173,7 @@ public class ControlMetricsWebResource extends AbstractWebResource { ...@@ -173,7 +173,7 @@ public class ControlMetricsWebResource extends AbstractWebResource {
173 public Response networkMetrics() { 173 public Response networkMetrics() {
174 174
175 ArrayNode networkNodes = root.putArray("networks"); 175 ArrayNode networkNodes = root.putArray("networks");
176 - monitorService.availableResources(NETWORK).forEach(name -> { 176 + monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> {
177 ObjectNode networkNode = mapper().createObjectNode(); 177 ObjectNode networkNode = mapper().createObjectNode();
178 ObjectNode valueNode = mapper().createObjectNode(); 178 ObjectNode valueNode = mapper().createObjectNode();
179 179
......
...@@ -240,7 +240,7 @@ public class ControlPlaneMonitorTest { ...@@ -240,7 +240,7 @@ public class ControlPlaneMonitorTest {
240 networkSet.forEach(network -> NETWORK_METRICS.forEach(cmt -> 240 networkSet.forEach(network -> NETWORK_METRICS.forEach(cmt ->
241 testUpdateMetricWithResource(cmt, mv, network))); 241 testUpdateMetricWithResource(cmt, mv, network)));
242 242
243 - assertThat(monitor.availableResources(Type.DISK), is(diskSet)); 243 + assertThat(monitor.availableResourcesSync(nodeId, Type.DISK), is(diskSet));
244 - assertThat(monitor.availableResources(Type.NETWORK), is(networkSet)); 244 + assertThat(monitor.availableResourcesSync(nodeId, Type.NETWORK), is(networkSet));
245 } 245 }
246 } 246 }
......
...@@ -75,7 +75,8 @@ public class ControlPlaneMonitorServiceAdaptor implements ControlPlaneMonitorSer ...@@ -75,7 +75,8 @@ public class ControlPlaneMonitorServiceAdaptor implements ControlPlaneMonitorSer
75 } 75 }
76 76
77 @Override 77 @Override
78 - public Set<String> availableResources(ControlResource.Type resourceType) { 78 + public CompletableFuture<Set<String>> availableResources(NodeId nodeId,
79 + ControlResource.Type resourceType) {
79 return null; 80 return null;
80 } 81 }
81 } 82 }
......
...@@ -166,7 +166,7 @@ public class ControlMetricsResourceTest extends ResourceTest { ...@@ -166,7 +166,7 @@ public class ControlMetricsResourceTest extends ResourceTest {
166 */ 166 */
167 @Test 167 @Test
168 public void testResourceEmptyArray() { 168 public void testResourceEmptyArray() {
169 - expect(mockControlPlaneMonitorService.availableResources(anyObject())) 169 + expect(mockControlPlaneMonitorService.availableResourcesSync(anyObject(), anyObject()))
170 .andReturn(ImmutableSet.of()).once(); 170 .andReturn(ImmutableSet.of()).once();
171 replay(mockControlPlaneMonitorService); 171 replay(mockControlPlaneMonitorService);
172 final WebTarget wt = target(); 172 final WebTarget wt = target();
...@@ -181,7 +181,7 @@ public class ControlMetricsResourceTest extends ResourceTest { ...@@ -181,7 +181,7 @@ public class ControlMetricsResourceTest extends ResourceTest {
181 */ 181 */
182 @Test 182 @Test
183 public void testResourcePopulatedArray() { 183 public void testResourcePopulatedArray() {
184 - expect(mockControlPlaneMonitorService.availableResources(anyObject())) 184 + expect(mockControlPlaneMonitorService.availableResourcesSync(anyObject(), anyObject()))
185 .andReturn(resourceSet).once(); 185 .andReturn(resourceSet).once();
186 expect(mockControlPlaneMonitorService.getLoad(anyObject(), anyObject(), 186 expect(mockControlPlaneMonitorService.getLoad(anyObject(), anyObject(),
187 anyString())).andReturn(null).times(4); 187 anyString())).andReturn(null).times(4);
......