diff options
Diffstat (limited to 'framework/src/onos/apps/vtn/vtnweb')
3 files changed, 110 insertions, 132 deletions
diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java index 7a57c0ab..b0e2f38d 100644 --- a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java @@ -15,15 +15,12 @@ */ package org.onosproject.vtnweb.resources; -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; -import static org.onlab.util.Tools.nullIsNotFound; import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.OK; +import static org.onlab.util.Tools.nullIsNotFound; import java.io.IOException; import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.UUID; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -36,13 +33,17 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.onosproject.rest.AbstractWebResource; import org.onosproject.vtnrsc.FlowClassifier; import org.onosproject.vtnrsc.FlowClassifierId; -import org.onosproject.rest.AbstractWebResource; import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; import org.onosproject.vtnweb.web.FlowClassifierCodec; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; /** @@ -51,73 +52,51 @@ import com.fasterxml.jackson.databind.node.ObjectNode; @Path("flow_classifiers") public class FlowClassifierWebResource extends AbstractWebResource { + private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); + final FlowClassifierService service = get(FlowClassifierService.class); - final ObjectNode root = mapper().createObjectNode(); public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; /** - * Get all flow classifiers created. Returns list of all flow classifiers - * created. + * Get all flow classifiers created. * * @return 200 OK */ @GET @Produces(MediaType.APPLICATION_JSON) public Response getFlowClassifiers() { - Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); + final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); ObjectNode result = new ObjectMapper().createObjectNode(); - result.set("flow_classifiers", new FlowClassifierCodec().encode(flowClassifiers, this)); + ArrayNode flowClassifierEntry = result.putArray("flow_classifiers"); + if (flowClassifiers != null) { + for (final FlowClassifier flowClassifier : flowClassifiers) { + flowClassifierEntry.add(new FlowClassifierCodec().encode(flowClassifier, this)); + } + } return ok(result.toString()).build(); } /** - * Get details of a flow classifier. Returns details of a specified flow - * classifier id. + * Get details of a flow classifier. * * @param id flow classifier id - * @return 200 OK + * @return 200 OK , 404 if given identifier does not exist */ @GET @Path("{flow_id}") @Produces(MediaType.APPLICATION_JSON) public Response getFlowClassifier(@PathParam("flow_id") String id) { - if (!service.hasFlowClassifier(FlowClassifierId.of(UUID.fromString(id)))) { + if (!service.hasFlowClassifier(FlowClassifierId.of(id))) { return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); } - FlowClassifier flowClassifier = nullIsNotFound( - service.getFlowClassifier(FlowClassifierId.of(UUID.fromString(id))), + FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND); ObjectNode result = new ObjectMapper().createObjectNode(); result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); - return ok(result.toString()).build(); - } - /** - * Creates and stores a new flow classifier. - * - * @param flowClassifierId flow classifier identifier - * @param stream flow classifier from JSON - * @return status of the request - CREATED if the JSON is correct, - * BAD_REQUEST if the JSON is invalid - */ - @POST - @Path("{flow_id}") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response createFlowClassifier(@PathParam("flow_id") String flowClassifierId, InputStream stream) { - URI location; - try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); - - FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); - service.createFlowClassifier(flowClassifier); - location = new URI(flowClassifierId); - } catch (IOException | URISyntaxException ex) { - throw new IllegalArgumentException(ex); - } - return Response.created(location).build(); + return ok(result.toString()).build(); } /** @@ -125,32 +104,32 @@ public class FlowClassifierWebResource extends AbstractWebResource { * * @param stream flow classifier from JSON * @return status of the request - CREATED if the JSON is correct, - * BAD_REQUEST if the JSON is invalid + * BAD_REQUEST if the JSON is invalid */ @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response createFlowClassifier(InputStream stream) { - URI location; try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); - - FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); - service.createFlowClassifier(flowClassifier); - location = new URI(flowClassifier.flowClassifierId().toString()); - } catch (IOException | URISyntaxException ex) { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); + JsonNode flow = jsonTree.get("flow_classifier"); + + FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); + Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); + return Response.status(OK).entity(issuccess.toString()).build(); + } catch (IOException ex) { + log.error("Exception while creating flow classifier {}.", ex.toString()); throw new IllegalArgumentException(ex); } - return Response.created(location).build(); } /** - * Update details of a flow classifier. Update details of a specified flow - * classifier id. + * Update details of a flow classifier. * * @param id flow classifier id * @param stream InputStream - * @return 200 OK + * @return 200 OK, 404 if given identifier does not exist */ @PUT @Path("{flow_id}") @@ -158,35 +137,29 @@ public class FlowClassifierWebResource extends AbstractWebResource { @Consumes(MediaType.APPLICATION_JSON) public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) { try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); - FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); + + JsonNode jsonTree = mapper().readTree(stream); + JsonNode flow = jsonTree.get("flow_classifier"); + FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); - if (!result) { - return Response.status(204).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); - } - return Response.status(203).entity(result.toString()).build(); - } catch (Exception e) { - return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); + return Response.status(OK).entity(result.toString()).build(); + } catch (IOException e) { + log.error("Update flow classifier failed because of exception {}.", e.toString()); + throw new IllegalArgumentException(e); } } /** - * Delete details of a flow classifier. Delete details of a specified flow - * classifier id. + * Delete details of a flow classifier. * * @param id flow classifier id - * @return 200 OK - * @throws IOException when input doesn't match. */ @Path("{flow_id}") @DELETE - public Response deleteFlowClassifier(@PathParam("flow_id") String id) throws IOException { - try { - FlowClassifierId flowClassifierId = FlowClassifierId.of(UUID.fromString(id)); - service.removeFlowClassifier(flowClassifierId); - return Response.status(201).entity("SUCCESS").build(); - } catch (Exception e) { - return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); - } + public void deleteFlowClassifier(@PathParam("flow_id") String id) { + log.debug("Deletes flow classifier by identifier {}.", id); + FlowClassifierId flowClassifierId = FlowClassifierId.of(id); + Boolean issuccess = nullIsNotFound(service.removeFlowClassifier(flowClassifierId), FLOW_CLASSIFIER_NOT_FOUND); + } } diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairWebResource.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairWebResource.java index 8bf459c2..b9012898 100644 --- a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairWebResource.java +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairWebResource.java @@ -42,7 +42,9 @@ import org.onosproject.vtnweb.web.PortPairCodec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; /** @@ -67,8 +69,13 @@ public class PortPairWebResource extends AbstractWebResource { public Response getPortPairs() { Iterable<PortPair> portPairs = service.getPortPairs(); ObjectNode result = new ObjectMapper().createObjectNode(); - result.set("port_pairs", new PortPairCodec().encode(portPairs, this)); - return ok(result).build(); + ArrayNode portPairEntry = result.putArray("port_pairs"); + if (portPairs != null) { + for (final PortPair portPair : portPairs) { + portPairEntry.add(new PortPairCodec().encode(portPair, this)); + } + } + return ok(result.toString()).build(); } /** @@ -80,18 +87,15 @@ public class PortPairWebResource extends AbstractWebResource { @GET @Path("{pair_id}") @Produces(MediaType.APPLICATION_JSON) - public Response getPortPair(@PathParam("portPairId") String id) { + public Response getPortPair(@PathParam("pair_id") String id) { if (!service.exists(PortPairId.of(id))) { - return Response.status(NOT_FOUND) - .entity(PORT_PAIR_NOT_FOUND).build(); + return Response.status(NOT_FOUND).entity(PORT_PAIR_NOT_FOUND).build(); } - PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), - PORT_PAIR_NOT_FOUND); - + PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), PORT_PAIR_NOT_FOUND); ObjectNode result = new ObjectMapper().createObjectNode(); result.set("port_pair", new PortPairCodec().encode(portPair, this)); - return ok(result).build(); + return ok(result.toString()).build(); } /** @@ -106,11 +110,11 @@ public class PortPairWebResource extends AbstractWebResource { @Produces(MediaType.APPLICATION_JSON) public Response createPortPair(InputStream stream) { try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); - - PortPair portPair = codec(PortPair.class).decode(jsonTree, this); - Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), - PORT_PAIR_NOT_FOUND); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); + JsonNode port = jsonTree.get("port_pair"); + PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this); + Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), PORT_PAIR_NOT_FOUND); return Response.status(OK).entity(isSuccess.toString()).build(); } catch (IOException e) { log.error("Exception while creating port pair {}.", e.toString()); @@ -132,8 +136,10 @@ public class PortPairWebResource extends AbstractWebResource { public Response updatePortPair(@PathParam("pair_id") String id, final InputStream stream) { try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); - PortPair portPair = codec(PortPair.class).decode(jsonTree, this); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); + JsonNode port = jsonTree.get("port_pair"); + PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this); Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND); return Response.status(OK).entity(isSuccess.toString()).build(); } catch (IOException e) { @@ -152,8 +158,7 @@ public class PortPairWebResource extends AbstractWebResource { public void deletePortPair(@PathParam("pair_id") String id) { PortPairId portPairId = PortPairId.of(id); - Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), - PORT_PAIR_NOT_FOUND); + Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), PORT_PAIR_NOT_FOUND); if (!isSuccess) { log.debug("Port pair identifier {} does not exist", id); } diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java index 4c17633c..a18ca362 100644 --- a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FlowClassifierCodec.java @@ -18,16 +18,14 @@ package org.onosproject.vtnweb.web; import static com.google.common.base.Preconditions.checkNotNull; import static org.onlab.util.Tools.nullIsIllegal; -import java.util.UUID; - import org.onlab.packet.IpPrefix; import org.onosproject.codec.CodecContext; import org.onosproject.codec.JsonCodec; import org.onosproject.vtnrsc.DefaultFlowClassifier; import org.onosproject.vtnrsc.FlowClassifier; import org.onosproject.vtnrsc.FlowClassifierId; -import org.onosproject.vtnrsc.VirtualPortId; import org.onosproject.vtnrsc.TenantId; +import org.onosproject.vtnrsc.VirtualPortId; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -40,7 +38,7 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { private static final String TENANT_ID = "tenant_id"; private static final String NAME = "name"; private static final String DESCRIPTION = "description"; - private static final String ETHER_TYPE = "etherType"; + private static final String ETHER_TYPE = "ethertype"; private static final String PROTOCOL = "protocol"; private static final String MIN_SRC_PORT_RANGE = "source_port_range_min"; private static final String MAX_SRC_PORT_RANGE = "source_port_range_max"; @@ -62,7 +60,7 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID), FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText(); - resultBuilder.setFlowClassifierId(FlowClassifierId.of(UUID.fromString(flowClassifierId))); + resultBuilder.setFlowClassifierId(FlowClassifierId.of(flowClassifierId)); String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); resultBuilder.setTenantId(TenantId.tenantId(tenantId)); @@ -70,44 +68,46 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText(); resultBuilder.setName(flowClassiferName); - String flowClassiferDescription = nullIsIllegal(json.get(DESCRIPTION), DESCRIPTION + MISSING_MEMBER_MESSAGE) - .asText(); + String flowClassiferDescription = (json.get(DESCRIPTION)).asText(); resultBuilder.setDescription(flowClassiferDescription); String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText(); resultBuilder.setEtherType(etherType); - String protocol = nullIsIllegal(json.get(PROTOCOL), PROTOCOL + MISSING_MEMBER_MESSAGE).asText(); + String protocol = (json.get(PROTOCOL)).asText(); resultBuilder.setProtocol(protocol); - int minSrcPortRange = nullIsIllegal(json.get(MIN_SRC_PORT_RANGE), MIN_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE) - .asInt(); + int minSrcPortRange = (json.get(MIN_SRC_PORT_RANGE)).asInt(); resultBuilder.setMinSrcPortRange(minSrcPortRange); - int maxSrcPortRange = nullIsIllegal(json.get(MAX_SRC_PORT_RANGE), MAX_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE) - .asInt(); + int maxSrcPortRange = (json.get(MAX_SRC_PORT_RANGE)).asInt(); resultBuilder.setMaxSrcPortRange(maxSrcPortRange); - int minDstPortRange = nullIsIllegal(json.get(MIN_DST_PORT_RANGE), MIN_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE) - .asInt(); + int minDstPortRange = (json.get(MIN_DST_PORT_RANGE)).asInt(); resultBuilder.setMinDstPortRange(minDstPortRange); - int maxDstPortRange = nullIsIllegal(json.get(MAX_DST_PORT_RANGE), MAX_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE) - .asInt(); + int maxDstPortRange = (json.get(MAX_DST_PORT_RANGE)).asInt(); resultBuilder.setMaxDstPortRange(maxDstPortRange); - String srcIpPrefix = nullIsIllegal(json.get(SRC_IP_PREFIX), SRC_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText(); - resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix)); - - String dstIpPrefix = nullIsIllegal(json.get(DST_IP_PREFIX), DST_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText(); - resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix)); + String srcIpPrefix = (json.get(SRC_IP_PREFIX)).asText(); + if (!srcIpPrefix.isEmpty()) { + resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix)); + } - String srcPort = nullIsIllegal(json.get(SRC_PORT), SRC_PORT + MISSING_MEMBER_MESSAGE).asText(); - resultBuilder.setSrcPort(VirtualPortId.portId(srcPort)); + String dstIpPrefix = (json.get(DST_IP_PREFIX)).asText(); + if (!dstIpPrefix.isEmpty()) { + resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix)); + } - String dstPort = nullIsIllegal(json.get(DST_PORT), DST_PORT + MISSING_MEMBER_MESSAGE).asText(); - resultBuilder.setDstPort(VirtualPortId.portId(dstPort)); + String srcPort = json.get(SRC_PORT) != null ? (json.get(SRC_PORT)).asText() : ""; + if (!srcPort.isEmpty()) { + resultBuilder.setSrcPort(VirtualPortId.portId(srcPort)); + } + String dstPort = json.get(DST_PORT) != null ? (json.get(DST_PORT)).asText() : ""; + if (!dstPort.isEmpty()) { + resultBuilder.setDstPort(VirtualPortId.portId(dstPort)); + } return resultBuilder.build(); } @@ -115,20 +115,20 @@ public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> { public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) { checkNotNull(flowClassifier, "flowClassifier cannot be null"); ObjectNode result = context.mapper().createObjectNode() - .put("FLOW_CLASSIFIER_ID", flowClassifier.flowClassifierId().toString()) - .put("TENANT_ID", flowClassifier.tenantId().toString()) - .put("NAME", flowClassifier.name()) - .put("DESCRIPTION", flowClassifier.description()) - .put("ETHER_TYPE", flowClassifier.etherType()) - .put("PROTOCOL", flowClassifier.protocol()) - .put("MIN_SRC_PORT_RANGE", flowClassifier.minSrcPortRange()) - .put("MAX_SRC_PORT_RANGE", flowClassifier.maxSrcPortRange()) - .put("MIN_DST_PORT_RANGE", flowClassifier.minDstPortRange()) - .put("MAX_DST_PORT_RANGE", flowClassifier.maxDstPortRange()) - .put("SRC_IP_PREFIX", flowClassifier.srcIpPrefix().toString()) - .put("DST_IP_PREFIX", flowClassifier.dstIpPrefix().toString()) - .put("SRC_PORT", flowClassifier.srcPort().toString()) - .put("DST_PORT", flowClassifier.dstPort().toString()); + .put(FLOW_CLASSIFIER_ID, flowClassifier.flowClassifierId().toString()) + .put(TENANT_ID, flowClassifier.tenantId().toString()) + .put(NAME, flowClassifier.name()) + .put(DESCRIPTION, flowClassifier.description()) + .put(ETHER_TYPE, flowClassifier.etherType()) + .put(PROTOCOL, flowClassifier.protocol()) + .put(MIN_SRC_PORT_RANGE, flowClassifier.minSrcPortRange()) + .put(MAX_SRC_PORT_RANGE, flowClassifier.maxSrcPortRange()) + .put(MIN_DST_PORT_RANGE, flowClassifier.minDstPortRange()) + .put(MAX_DST_PORT_RANGE, flowClassifier.maxDstPortRange()) + .put(SRC_IP_PREFIX, flowClassifier.srcIpPrefix().toString()) + .put(DST_IP_PREFIX, flowClassifier.dstIpPrefix().toString()) + .put(SRC_PORT, flowClassifier.srcPort().toString()) + .put(DST_PORT, flowClassifier.dstPort().toString()); return result; } } |