diff options
Diffstat (limited to 'framework/src/onos/web/api/src/main/java/org')
3 files changed, 84 insertions, 5 deletions
diff --git a/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java b/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java index 455e4929..00cbdf2f 100644 --- a/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java +++ b/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java @@ -16,6 +16,7 @@ package org.onosproject.rest.resources; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import org.onlab.packet.IpAddress; import org.onlab.packet.MacAddress; @@ -50,6 +51,7 @@ import java.io.InputStream; import java.net.URI; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; import java.util.Set; import static org.onlab.util.Tools.nullIsNotFound; @@ -64,6 +66,7 @@ public class HostsWebResource extends AbstractWebResource { @Context UriInfo uriInfo; public static final String HOST_NOT_FOUND = "Host is not found"; + private static final String[] REMOVAL_KEYS = {"mac", "vlan", "location", "ipAddresses"}; /** * Get all end-station hosts. @@ -199,8 +202,9 @@ public class HostsWebResource extends AbstractWebResource { while (ipStrings.hasNext()) { ips.add(IpAddress.valueOf(ipStrings.next().asText())); } - //TODO remove elements from json node after reading them - SparseAnnotations annotations = annotations(node); + + // try to remove elements from json node after reading them + SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS)); // Update host inventory HostId hostId = HostId.hostId(mac, vlanId); @@ -210,6 +214,22 @@ public class HostsWebResource extends AbstractWebResource { } /** + * Remove a set of elements from JsonNode by specifying keys. + * + * @param node JsonNode containing host information + * @param removalKeys key of elements that need to be removed + * @return removal keys + */ + private JsonNode removeElements(JsonNode node, String[] removalKeys) { + ObjectMapper mapper = new ObjectMapper(); + Map<String, Object> map = mapper.convertValue(node, Map.class); + for (String key : removalKeys) { + map.remove(key); + } + return mapper.convertValue(map, JsonNode.class); + } + + /** * Produces annotations from specified JsonNode. Copied from the ConfigProvider * class for use in the POST method. * diff --git a/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java b/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java index a4dd9380..2c63db9c 100644 --- a/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java +++ b/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/IntentsWebResource.java @@ -70,7 +70,7 @@ public class IntentsWebResource extends AbstractWebResource { /** * Get all intents. * Returns array containing all the intents in the system. - * + * @rsModel Intents * @return array of all the intents in the system */ @GET @@ -84,7 +84,7 @@ public class IntentsWebResource extends AbstractWebResource { /** * Get intent by application and key. * Returns details of the specified intent. - * + * @rsModel Intents * @param appId application identifier * @param key intent key * @return intent data @@ -136,7 +136,7 @@ public class IntentsWebResource extends AbstractWebResource { /** * Submit a new intent. * Creates and submits intent from the JSON request. - * + * @rsModel IntentHost * @param stream input JSON * @return status of the request - CREATED if the JSON is correct, * BAD_REQUEST if the JSON is invalid diff --git a/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/StatisticsWebResource.java b/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/StatisticsWebResource.java index 7a452044..284d3775 100644 --- a/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/StatisticsWebResource.java +++ b/framework/src/onos/web/api/src/main/java/org/onosproject/rest/resources/StatisticsWebResource.java @@ -36,6 +36,7 @@ import org.onosproject.net.Device; import org.onosproject.net.DeviceId; import org.onosproject.net.Link; import org.onosproject.net.device.DeviceService; +import org.onosproject.net.device.PortStatistics; import org.onosproject.net.flow.FlowRuleService; import org.onosproject.net.flow.TableStatisticsEntry; import org.onosproject.net.link.LinkService; @@ -153,4 +154,62 @@ public class StatisticsWebResource extends AbstractWebResource { rootArrayNode.add(deviceStatsNode); return ok(root).build(); } + + /** + * Get port statistics of all devices. + * @rsModel StatisticsPorts + * @return JSON encoded array of port statistics + */ + @GET + @Path("ports") + @Produces(MediaType.APPLICATION_JSON) + public Response getPortStatistics() { + final DeviceService service = get(DeviceService.class); + final Iterable<Device> devices = service.getDevices(); + final ObjectNode root = mapper().createObjectNode(); + final ArrayNode rootArrayNode = root.putArray("statistics"); + for (final Device device : devices) { + final ObjectNode deviceStatsNode = mapper().createObjectNode(); + deviceStatsNode.put("device", device.id().toString()); + final ArrayNode statisticsNode = deviceStatsNode.putArray("ports"); + final Iterable<PortStatistics> portStatsEntries = service.getPortStatistics(device.id()); + if (portStatsEntries != null) { + for (final PortStatistics entry : portStatsEntries) { + statisticsNode.add(codec(PortStatistics.class).encode(entry, this)); + } + } + rootArrayNode.add(deviceStatsNode); + } + + return ok(root).build(); + } + + /** + * Get port statistics of a specified devices. + * @rsModel StatisticsPorts + * @param deviceId device ID + * @return JSON encoded array of port statistics + */ + @GET + @Path("ports/{deviceId}") + @Produces(MediaType.APPLICATION_JSON) + public Response getPortStatisticsByDeviceId(@PathParam("deviceId") String deviceId) { + final DeviceService service = get(DeviceService.class); + final Iterable<PortStatistics> portStatsEntries = + service.getPortStatistics(DeviceId.deviceId(deviceId)); + final ObjectNode root = mapper().createObjectNode(); + final ArrayNode rootArrayNode = root.putArray("statistics"); + final ObjectNode deviceStatsNode = mapper().createObjectNode(); + deviceStatsNode.put("device", deviceId); + final ArrayNode statisticsNode = deviceStatsNode.putArray("ports"); + if (portStatsEntries != null) { + for (final PortStatistics entry : portStatsEntries) { + statisticsNode.add(codec(PortStatistics.class).encode(entry, this)); + } + } + rootArrayNode.add(deviceStatsNode); + + return ok(root).build(); + } + } |