diff options
author | Ashlee Young <ashlee@wildernessvoice.com> | 2015-12-01 05:49:27 -0800 |
---|---|---|
committer | Ashlee Young <ashlee@wildernessvoice.com> | 2015-12-01 05:49:27 -0800 |
commit | e63291850fd0795c5700e25e67e5dee89ba54c5f (patch) | |
tree | 9707289536ad95bb739c9856761ad43275e07d8c /framework/src/onos/apps/vtn/vtnweb | |
parent | 671823e12bc13be9a8b87a5d7de33da1bb7a44e8 (diff) |
onos commit hash c2999f30c69e50df905a9d175ef80b3f23a98514
Change-Id: I2bb8562c4942b6d6a6d60b663db2e17540477b81
Signed-off-by: Ashlee Young <ashlee@wildernessvoice.com>
Diffstat (limited to 'framework/src/onos/apps/vtn/vtnweb')
15 files changed, 1761 insertions, 92 deletions
diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FloatingIpWebResource.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FloatingIpWebResource.java new file mode 100644 index 00000000..f7e97d5d --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FloatingIpWebResource.java @@ -0,0 +1,285 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.resources; + +import static com.google.common.base.Preconditions.checkNotNull; +import static javax.ws.rs.core.Response.Status.BAD_REQUEST; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.CREATED; +import static javax.ws.rs.core.Response.Status.CONFLICT; +import static javax.ws.rs.core.Response.Status.NO_CONTENT; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.onlab.packet.IpAddress; +import org.onlab.util.ItemNotFoundException; +import org.onosproject.rest.AbstractWebResource; +import org.onosproject.vtnrsc.DefaultFloatingIp; +import org.onosproject.vtnrsc.FloatingIp; +import org.onosproject.vtnrsc.FloatingIpId; +import org.onosproject.vtnrsc.TenantId; +import org.onosproject.vtnrsc.TenantNetworkId; +import org.onosproject.vtnrsc.VirtualPortId; +import org.onosproject.vtnrsc.RouterId; +import org.onosproject.vtnrsc.FloatingIp.Status; +import org.onosproject.vtnrsc.floatingip.FloatingIpService; +import org.onosproject.vtnweb.web.FloatingIpCodec; +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.ObjectNode; +import com.google.common.collect.Sets; + +@Path("floatingips") +public class FloatingIpWebResource extends AbstractWebResource { + private final Logger log = LoggerFactory + .getLogger(FloatingIpWebResource.class); + public static final String CREATE_FAIL = "Floating IP is failed to create!"; + public static final String UPDATE_FAIL = "Floating IP is failed to update!"; + public static final String GET_FAIL = "Floating IP is failed to get!"; + public static final String NOT_EXIST = "Floating IP does not exist!"; + public static final String DELETE_SUCCESS = "Floating IP delete success!"; + public static final String JSON_NOT_NULL = "JsonNode can not be null"; + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response listFloatingIps() { + Collection<FloatingIp> floatingIps = get(FloatingIpService.class) + .getFloatingIps(); + ObjectNode result = new ObjectMapper().createObjectNode(); + result.set("floatingips", + new FloatingIpCodec().encode(floatingIps, this)); + return ok(result.toString()).build(); + } + + @GET + @Path("{floatingIpUUID}") + @Produces(MediaType.APPLICATION_JSON) + public Response getFloatingIp(@PathParam("floatingIpUUID") String id, + @QueryParam("fields") List<String> fields) { + + if (!get(FloatingIpService.class).exists(FloatingIpId.of(id))) { + return Response.status(NOT_FOUND).entity(NOT_EXIST).build(); + } + FloatingIp sub = nullIsNotFound(get(FloatingIpService.class) + .getFloatingIp(FloatingIpId.of(id)), GET_FAIL); + + ObjectNode result = new ObjectMapper().createObjectNode(); + if (fields.size() > 0) { + result.set("floatingip", + new FloatingIpCodec().extracFields(sub, this, fields)); + } else { + result.set("floatingip", new FloatingIpCodec().encode(sub, this)); + } + return ok(result.toString()).build(); + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response createFloatingIp(final InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode subnode = mapper.readTree(input); + Collection<FloatingIp> floatingIps = createOrUpdateByInputStream(subnode); + Boolean result = nullIsNotFound((get(FloatingIpService.class) + .createFloatingIps(floatingIps)), + CREATE_FAIL); + if (!result) { + return Response.status(CONFLICT).entity(CREATE_FAIL).build(); + } + return Response.status(CREATED).entity(result.toString()).build(); + + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @PUT + @Path("{floatingIpUUID}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response updateFloatingIp(@PathParam("floatingIpUUID") String id, + final InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode subnode = mapper.readTree(input); + Collection<FloatingIp> floatingIps = createOrUpdateByInputStream(subnode); + Boolean result = nullIsNotFound(get(FloatingIpService.class) + .updateFloatingIps(floatingIps), UPDATE_FAIL); + if (!result) { + return Response.status(CONFLICT).entity(UPDATE_FAIL).build(); + } + return ok(result.toString()).build(); + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @Path("{floatingIpUUID}") + @DELETE + public Response deleteSingleFloatingIp(@PathParam("floatingIpUUID") String id) + throws IOException { + try { + FloatingIpId floatingIpId = FloatingIpId.of(id); + Set<FloatingIpId> floatingIpIds = Sets.newHashSet(floatingIpId); + get(FloatingIpService.class).removeFloatingIps(floatingIpIds); + return Response.status(NO_CONTENT).entity(DELETE_SUCCESS).build(); + } catch (Exception e) { + return Response.status(NOT_FOUND).entity(e.getMessage()).build(); + } + } + + private Collection<FloatingIp> createOrUpdateByInputStream(JsonNode subnode) + throws Exception { + checkNotNull(subnode, JSON_NOT_NULL); + Collection<FloatingIp> floatingIps = null; + JsonNode floatingIpNodes = subnode.get("floatingips"); + if (floatingIpNodes == null) { + floatingIpNodes = subnode.get("floatingip"); + } + log.debug("floatingNodes is {}", floatingIpNodes.toString()); + + if (floatingIpNodes.isArray()) { + throw new IllegalArgumentException("only singleton requests allowed"); + } else { + floatingIps = changeJsonToSub(floatingIpNodes); + } + return floatingIps; + } + + /** + * Returns a collection of floatingIps from floatingIpNodes. + * + * @param floatingIpNodes the floatingIp json node + * @return floatingIps a collection of floatingIp + * @throws Exception when any argument is illegal + */ + public Collection<FloatingIp> changeJsonToSub(JsonNode floatingIpNodes) + throws Exception { + checkNotNull(floatingIpNodes, JSON_NOT_NULL); + Map<FloatingIpId, FloatingIp> subMap = new HashMap<FloatingIpId, FloatingIp>(); + if (!floatingIpNodes.hasNonNull("id")) { + throw new IllegalArgumentException("id should not be null"); + } else if (floatingIpNodes.get("id").asText().isEmpty()) { + throw new IllegalArgumentException("id should not be empty"); + } + FloatingIpId id = FloatingIpId.of(floatingIpNodes.get("id") + .asText()); + + if (!floatingIpNodes.hasNonNull("tenant_id")) { + throw new IllegalArgumentException("tenant_id should not be null"); + } else if (floatingIpNodes.get("tenant_id").asText().isEmpty()) { + throw new IllegalArgumentException("tenant_id should not be empty"); + } + TenantId tenantId = TenantId.tenantId(floatingIpNodes.get("tenant_id") + .asText()); + + if (!floatingIpNodes.hasNonNull("floating_network_id")) { + throw new IllegalArgumentException( + "floating_network_id should not be null"); + } else if (floatingIpNodes.get("floating_network_id").asText() + .isEmpty()) { + throw new IllegalArgumentException( + "floating_network_id should not be empty"); + } + TenantNetworkId networkId = TenantNetworkId.networkId(floatingIpNodes + .get("floating_network_id").asText()); + + VirtualPortId portId = null; + if (floatingIpNodes.hasNonNull("port_id")) { + portId = VirtualPortId.portId(floatingIpNodes.get("port_id") + .asText()); + } + + RouterId routerId = null; + if (floatingIpNodes.hasNonNull("router_id")) { + routerId = RouterId.valueOf(floatingIpNodes.get("router_id") + .asText()); + } + + IpAddress fixedIp = null; + if (floatingIpNodes.hasNonNull("fixed_ip_address")) { + fixedIp = IpAddress.valueOf(floatingIpNodes.get("fixed_ip_address") + .asText()); + } + + if (!floatingIpNodes.hasNonNull("floating_ip_address")) { + throw new IllegalArgumentException( + "floating_ip_address should not be null"); + } else if (floatingIpNodes.get("floating_ip_address").asText() + .isEmpty()) { + throw new IllegalArgumentException( + "floating_ip_address should not be empty"); + } + IpAddress floatingIp = IpAddress.valueOf(floatingIpNodes + .get("floating_ip_address").asText()); + + if (!floatingIpNodes.hasNonNull("status")) { + throw new IllegalArgumentException("status should not be null"); + } else if (floatingIpNodes.get("status").asText().isEmpty()) { + throw new IllegalArgumentException("status should not be empty"); + } + Status status = Status.valueOf(floatingIpNodes.get("status").asText()); + + DefaultFloatingIp floatingIpObj = new DefaultFloatingIp(id, tenantId, + networkId, + portId, + routerId, + floatingIp, + fixedIp, status); + subMap.put(id, floatingIpObj); + return Collections.unmodifiableCollection(subMap.values()); + } + + /** + * Returns the specified item if that items is null; otherwise throws not + * found exception. + * + * @param item item to check + * @param <T> item type + * @param message not found message + * @return item if not null + * @throws org.onlab.util.ItemNotFoundException if item is null + */ + protected <T> T nullIsNotFound(T item, String message) { + if (item == null) { + throw new ItemNotFoundException(message); + } + return item; + } +} 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 b0e2f38d..08e37f96 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,7 +15,6 @@ */ package org.onosproject.vtnweb.resources; -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; @@ -54,7 +53,6 @@ public class FlowClassifierWebResource extends AbstractWebResource { private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); - final FlowClassifierService service = get(FlowClassifierService.class); public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; /** @@ -65,7 +63,7 @@ public class FlowClassifierWebResource extends AbstractWebResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getFlowClassifiers() { - final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); + Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers(); ObjectNode result = new ObjectMapper().createObjectNode(); ArrayNode flowClassifierEntry = result.putArray("flow_classifiers"); if (flowClassifiers != null) { @@ -79,19 +77,16 @@ public class FlowClassifierWebResource extends AbstractWebResource { /** * Get details of a flow classifier. * - * @param id flow classifier id + * @param id + * flow classifier id * @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(id))) { - return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); - } - FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)), - FLOW_CLASSIFIER_NOT_FOUND); + FlowClassifier flowClassifier = nullIsNotFound( + get(FlowClassifierService.class).getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND); ObjectNode result = new ObjectMapper().createObjectNode(); result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); @@ -102,9 +97,10 @@ public class FlowClassifierWebResource extends AbstractWebResource { /** * Creates and stores a new flow classifier. * - * @param stream flow classifier from JSON + * @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) @@ -116,7 +112,8 @@ public class FlowClassifierWebResource extends AbstractWebResource { JsonNode flow = jsonTree.get("flow_classifier"); FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); - Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); + Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).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()); @@ -127,8 +124,10 @@ public class FlowClassifierWebResource extends AbstractWebResource { /** * Update details of a flow classifier. * - * @param id flow classifier id - * @param stream InputStream + * @param id + * flow classifier id + * @param stream + * InputStream * @return 200 OK, 404 if given identifier does not exist */ @PUT @@ -141,7 +140,8 @@ public class FlowClassifierWebResource extends AbstractWebResource { 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); + Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier), + FLOW_CLASSIFIER_NOT_FOUND); return Response.status(OK).entity(result.toString()).build(); } catch (IOException e) { log.error("Update flow classifier failed because of exception {}.", e.toString()); @@ -152,14 +152,16 @@ public class FlowClassifierWebResource extends AbstractWebResource { /** * Delete details of a flow classifier. * - * @param id flow classifier id + * @param id + * flow classifier id */ @Path("{flow_id}") @DELETE 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); + Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId), + FLOW_CLASSIFIER_NOT_FOUND); } } diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java new file mode 100644 index 00000000..6f80dd15 --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/RouterWebResource.java @@ -0,0 +1,447 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.resources; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static javax.ws.rs.core.Response.Status.BAD_REQUEST; +import static javax.ws.rs.core.Response.Status.CONFLICT; +import static javax.ws.rs.core.Response.Status.CREATED; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.NO_CONTENT; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.onlab.packet.IpAddress; +import org.onlab.util.ItemNotFoundException; +import org.onosproject.rest.AbstractWebResource; +import org.onosproject.vtnrsc.DefaultRouter; +import org.onosproject.vtnrsc.FixedIp; +import org.onosproject.vtnrsc.Router; +import org.onosproject.vtnrsc.Router.Status; +import org.onosproject.vtnrsc.RouterGateway; +import org.onosproject.vtnrsc.RouterId; +import org.onosproject.vtnrsc.RouterInterface; +import org.onosproject.vtnrsc.SubnetId; +import org.onosproject.vtnrsc.TenantId; +import org.onosproject.vtnrsc.TenantNetworkId; +import org.onosproject.vtnrsc.VirtualPortId; +import org.onosproject.vtnrsc.router.RouterService; +import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService; +import org.onosproject.vtnweb.web.RouterCodec; +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.ObjectNode; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +@Path("routers") +public class RouterWebResource extends AbstractWebResource { + private final Logger log = LoggerFactory.getLogger(RouterWebResource.class); + public static final String CREATE_FAIL = "Router is failed to create!"; + public static final String UPDATE_FAIL = "Router is failed to update!"; + public static final String GET_FAIL = "Router is failed to get!"; + public static final String NOT_EXIST = "Router does not exist!"; + public static final String DELETE_SUCCESS = "Router delete success!"; + public static final String JSON_NOT_NULL = "JsonNode can not be null"; + public static final String INTFACR_ADD_SUCCESS = "Interface add success"; + public static final String INTFACR_DEL_SUCCESS = "Interface delete success"; + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response listRouters() { + Collection<Router> routers = get(RouterService.class).getRouters(); + ObjectNode result = new ObjectMapper().createObjectNode(); + result.set("routers", new RouterCodec().encode(routers, this)); + return ok(result.toString()).build(); + } + + @GET + @Path("{routerUUID}") + @Produces(MediaType.APPLICATION_JSON) + public Response getRouter(@PathParam("routerUUID") String id, + @QueryParam("fields") List<String> fields) { + + if (!get(RouterService.class).exists(RouterId.valueOf(id))) { + return Response.status(NOT_FOUND) + .entity("The Router does not exists").build(); + } + Router sub = nullIsNotFound(get(RouterService.class) + .getRouter(RouterId.valueOf(id)), + NOT_EXIST); + + ObjectNode result = new ObjectMapper().createObjectNode(); + if (fields.size() > 0) { + result.set("router", + new RouterCodec().extracFields(sub, this, fields)); + } else { + result.set("router", new RouterCodec().encode(sub, this)); + } + return ok(result.toString()).build(); + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response createRouter(final InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode subnode = mapper.readTree(input); + Collection<Router> routers = createOrUpdateByInputStream(subnode); + + Boolean result = nullIsNotFound((get(RouterService.class) + .createRouters(routers)), + CREATE_FAIL); + if (!result) { + return Response.status(CONFLICT).entity(CREATE_FAIL).build(); + } + return Response.status(CREATED).entity(result.toString()).build(); + + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @PUT + @Path("{routerUUID}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response updateRouter(@PathParam("routerUUID") String id, + final InputStream input) { + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode subnode = mapper.readTree(input); + Collection<Router> routers = createOrUpdateByInputStream(subnode); + Boolean result = nullIsNotFound(get(RouterService.class) + .updateRouters(routers), UPDATE_FAIL); + if (!result) { + return Response.status(CONFLICT).entity(UPDATE_FAIL).build(); + } + return ok(result.toString()).build(); + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @Path("{routerUUID}") + @DELETE + public Response deleteSingleRouter(@PathParam("routerUUID") String id) + throws IOException { + try { + RouterId routerId = RouterId.valueOf(id); + Set<RouterId> routerIds = Sets.newHashSet(routerId); + get(RouterService.class).removeRouters(routerIds); + return Response.status(NO_CONTENT).entity(DELETE_SUCCESS).build(); + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @PUT + @Path("{routerUUID}/add_router_interface") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response addRouterInterface(@PathParam("routerUUID") String id, + final InputStream input) { + if (!get(RouterService.class).exists(RouterId.valueOf(id))) { + return Response.status(NOT_FOUND).entity(NOT_EXIST).build(); + } + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode subnode = mapper.readTree(input); + if (!subnode.hasNonNull("id")) { + throw new IllegalArgumentException("id should not be null"); + } else if (subnode.get("id").asText().isEmpty()) { + throw new IllegalArgumentException("id should not be empty"); + } + RouterId routerId = RouterId.valueOf(id); + if (!subnode.hasNonNull("subnet_id")) { + throw new IllegalArgumentException("subnet_id should not be null"); + } else if (subnode.get("subnet_id").asText().isEmpty()) { + throw new IllegalArgumentException("subnet_id should not be empty"); + } + SubnetId subnetId = SubnetId.subnetId(subnode.get("subnet_id") + .asText()); + if (!subnode.hasNonNull("tenant_id")) { + throw new IllegalArgumentException("tenant_id should not be null"); + } else if (subnode.get("tenant_id").asText().isEmpty()) { + throw new IllegalArgumentException("tenant_id should not be empty"); + } + TenantId tenentId = TenantId.tenantId(subnode.get("tenant_id") + .asText()); + if (!subnode.hasNonNull("port_id")) { + throw new IllegalArgumentException("port_id should not be null"); + } else if (subnode.get("port_id").asText().isEmpty()) { + throw new IllegalArgumentException("port_id should not be empty"); + } + VirtualPortId portId = VirtualPortId.portId(subnode.get("port_id") + .asText()); + RouterInterface routerInterface = RouterInterface + .routerInterface(subnetId, portId, routerId, tenentId); + get(RouterInterfaceService.class) + .addRouterInterface(routerInterface); + return ok(INTFACR_ADD_SUCCESS).build(); + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @PUT + @Path("{routerUUID}/remove_router_interface") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response removeRouterInterface(@PathParam("routerUUID") String id, + final InputStream input) { + if (!get(RouterService.class).exists(RouterId.valueOf(id))) { + return Response.status(NOT_FOUND).entity(NOT_EXIST).build(); + } + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode subnode = mapper.readTree(input); + if (!subnode.hasNonNull("id")) { + throw new IllegalArgumentException("id should not be null"); + } else if (subnode.get("id").asText().isEmpty()) { + throw new IllegalArgumentException("id should not be empty"); + } + RouterId routerId = RouterId.valueOf(id); + if (!subnode.hasNonNull("subnet_id")) { + throw new IllegalArgumentException("subnet_id should not be null"); + } else if (subnode.get("subnet_id").asText().isEmpty()) { + throw new IllegalArgumentException("subnet_id should not be empty"); + } + SubnetId subnetId = SubnetId.subnetId(subnode.get("subnet_id") + .asText()); + if (!subnode.hasNonNull("port_id")) { + throw new IllegalArgumentException("port_id should not be null"); + } else if (subnode.get("port_id").asText().isEmpty()) { + throw new IllegalArgumentException("port_id should not be empty"); + } + VirtualPortId portId = VirtualPortId.portId(subnode.get("port_id") + .asText()); + if (!subnode.hasNonNull("tenant_id")) { + throw new IllegalArgumentException("tenant_id should not be null"); + } else if (subnode.get("tenant_id").asText().isEmpty()) { + throw new IllegalArgumentException("tenant_id should not be empty"); + } + TenantId tenentId = TenantId.tenantId(subnode.get("tenant_id") + .asText()); + RouterInterface routerInterface = RouterInterface + .routerInterface(subnetId, portId, routerId, tenentId); + get(RouterInterfaceService.class) + .removeRouterInterface(routerInterface); + return ok(INTFACR_DEL_SUCCESS).build(); + } catch (Exception e) { + return Response.status(BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + private Collection<Router> createOrUpdateByInputStream(JsonNode subnode) + throws Exception { + checkNotNull(subnode, JSON_NOT_NULL); + JsonNode routerNode = subnode.get("routers"); + if (routerNode == null) { + routerNode = subnode.get("router"); + } + log.debug("routerNode is {}", routerNode.toString()); + + if (routerNode.isArray()) { + throw new Exception("only singleton requests allowed"); + } else { + return changeJsonToSub(routerNode); + } + } + + /** + * Returns a collection of floatingIps from floatingIpNodes. + * + * @param routerNode the router json node + * @return routers a collection of router + * @throws Exception when any argument is illegal + */ + public Collection<Router> changeJsonToSub(JsonNode routerNode) + throws Exception { + checkNotNull(routerNode, JSON_NOT_NULL); + Map<RouterId, Router> subMap = new HashMap<RouterId, Router>(); + if (!routerNode.hasNonNull("id")) { + new IllegalArgumentException("id should not be null"); + } else if (routerNode.get("id").asText().isEmpty()) { + throw new IllegalArgumentException("id should not be empty"); + } + RouterId id = RouterId.valueOf(routerNode.get("id").asText()); + + if (!routerNode.hasNonNull("tenant_id")) { + throw new IllegalArgumentException("tenant_id should not be null"); + } else if (routerNode.get("tenant_id").asText().isEmpty()) { + throw new IllegalArgumentException("tenant_id should not be empty"); + } + TenantId tenantId = TenantId.tenantId(routerNode.get("tenant_id") + .asText()); + + VirtualPortId gwPortId = null; + if (routerNode.hasNonNull("gw_port_id")) { + gwPortId = VirtualPortId.portId(routerNode.get("gw_port_id") + .asText()); + } + + if (!routerNode.hasNonNull("status")) { + throw new IllegalArgumentException("status should not be null"); + } else if (routerNode.get("status").asText().isEmpty()) { + throw new IllegalArgumentException("status should not be empty"); + } + Status status = Status.valueOf(routerNode.get("status").asText()); + + String routerName = null; + if (routerNode.hasNonNull("name")) { + routerName = routerNode.get("name").asText(); + } + + boolean adminStateUp = true; + checkArgument(routerNode.get("admin_state_up").isBoolean(), + "admin_state_up should be boolean"); + if (routerNode.hasNonNull("admin_state_up")) { + adminStateUp = routerNode.get("admin_state_up").asBoolean(); + } + boolean distributed = false; + if (routerNode.hasNonNull("distributed")) { + distributed = routerNode.get("distributed").asBoolean(); + } + RouterGateway gateway = null; + if (routerNode.hasNonNull("external_gateway_info")) { + gateway = jsonNodeToGateway(routerNode.get("external_gateway_info")); + } + List<String> routes = new ArrayList<String>(); + DefaultRouter routerObj = new DefaultRouter(id, routerName, + adminStateUp, status, + distributed, gateway, + gwPortId, tenantId, routes); + subMap.put(id, routerObj); + return Collections.unmodifiableCollection(subMap.values()); + } + + /** + * Changes JsonNode Gateway to the Gateway. + * + * @param gateway the gateway JsonNode + * @return gateway + */ + private RouterGateway jsonNodeToGateway(JsonNode gateway) { + checkNotNull(gateway, JSON_NOT_NULL); + if (!gateway.hasNonNull("network_id")) { + throw new IllegalArgumentException("network_id should not be null"); + } else if (gateway.get("network_id").asText().isEmpty()) { + throw new IllegalArgumentException("network_id should not be empty"); + } + TenantNetworkId networkId = TenantNetworkId.networkId(gateway + .get("network_id").asText()); + + if (!gateway.hasNonNull("enable_snat")) { + throw new IllegalArgumentException("enable_snat should not be null"); + } else if (gateway.get("enable_snat").asText().isEmpty()) { + throw new IllegalArgumentException("enable_snat should not be empty"); + } + checkArgument(gateway.get("enable_snat").isBoolean(), + "enable_snat should be boolean"); + boolean enableSnat = gateway.get("enable_snat").asBoolean(); + + if (!gateway.hasNonNull("external_fixed_ips")) { + throw new IllegalArgumentException( + "external_fixed_ips should not be null"); + } else if (gateway.get("external_fixed_ips").isNull()) { + throw new IllegalArgumentException( + "external_fixed_ips should not be empty"); + } + Collection<FixedIp> fixedIpList = jsonNodeToFixedIp(gateway + .get("external_fixed_ips")); + RouterGateway gatewayObj = RouterGateway.routerGateway(networkId, + enableSnat, + fixedIpList); + return gatewayObj; + } + + /** + * Changes JsonNode fixedIp to a collection of the fixedIp. + * + * @param fixedIp the allocationPools JsonNode + * @return a collection of fixedIp + */ + private Collection<FixedIp> jsonNodeToFixedIp(JsonNode fixedIp) { + checkNotNull(fixedIp, JSON_NOT_NULL); + ConcurrentMap<Integer, FixedIp> fixedIpMaps = Maps.newConcurrentMap(); + Integer i = 0; + for (JsonNode node : fixedIp) { + if (!node.hasNonNull("subnet_id")) { + throw new IllegalArgumentException("subnet_id should not be null"); + } else if (node.get("subnet_id").asText().isEmpty()) { + throw new IllegalArgumentException("subnet_id should not be empty"); + } + SubnetId subnetId = SubnetId.subnetId(node.get("subnet_id") + .asText()); + if (!node.hasNonNull("ip_address")) { + throw new IllegalArgumentException("ip_address should not be null"); + } else if (node.get("ip_address").asText().isEmpty()) { + throw new IllegalArgumentException("ip_address should not be empty"); + } + IpAddress ipAddress = IpAddress.valueOf(node.get("ip_address") + .asText()); + FixedIp fixedIpObj = FixedIp.fixedIp(subnetId, ipAddress); + + fixedIpMaps.putIfAbsent(i, fixedIpObj); + i++; + } + return Collections.unmodifiableCollection(fixedIpMaps.values()); + } + + /** + * Returns the specified item if that items is null; otherwise throws not + * found exception. + * + * @param item item to check + * @param <T> item type + * @param message not found message + * @return item if not null + * @throws org.onlab.util.ItemNotFoundException if item is null + */ + protected <T> T nullIsNotFound(T item, String message) { + if (item == null) { + throw new ItemNotFoundException(message); + } + return item; + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/TenantNetworkWebResource.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/TenantNetworkWebResource.java index 2dd931ea..fd2c4790 100644 --- a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/TenantNetworkWebResource.java +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/TenantNetworkWebResource.java @@ -98,20 +98,20 @@ public class TenantNetworkWebResource extends AbstractWebResource { if ((queryId == null || queryId.equals(network.id().toString())) && (queryName == null || queryName.equals(network.name())) && (queryadminStateUp == null || queryadminStateUp - .equals(network.adminStateUp())) + .equals(Boolean.toString(network.adminStateUp()))) && (querystate == null || querystate.equals(network.state() .toString())) - && (queryshared == null || queryshared.equals(network - .shared())) + && (queryshared == null || queryshared.equals(Boolean.toString(network + .shared()))) && (querytenantId == null || querytenantId.equals(network .tenantId().toString())) - && (routerExternal == null || routerExternal.equals(network - .routerExternal())) - && (type == null || type.equals(network.type())) + && (routerExternal == null || routerExternal.equals(Boolean.toString(network + .routerExternal()))) + && (type == null || type.equals(network.type().toString())) && (physicalNetwork == null || physicalNetwork - .equals(network.physicalNetwork())) + .equals(network.physicalNetwork().toString())) && (segmentationId == null || segmentationId.equals(network - .segmentationId()))) { + .segmentationId().toString()))) { networksMap.putIfAbsent(network.id(), network); } } @@ -269,42 +269,39 @@ public class TenantNetworkWebResource extends AbstractWebResource { TenantNetwork network = null; ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps .newConcurrentMap(); - if (node != null) { - checkArgument(node.get("admin_state_up").isBoolean(), "admin_state_up should be boolean"); - checkArgument(node.get("shared").isBoolean(), "shared should be boolean"); - checkArgument(node.get("router:external").isBoolean(), "router:external should be boolean"); - String name = node.get("name").asText(); - boolean adminStateUp = node.get("admin_state_up").asBoolean(); - String state = node.get("status").asText(); - boolean shared = node.get("shared").asBoolean(); - String tenantId = node.get("tenant_id").asText(); - boolean routerExternal = node.get("router:external").asBoolean(); - String type = node.get("provider:network_type").asText(); - String physicalNetwork = node.get("provider:physical_network") - .asText(); - String segmentationId = node.get("provider:segmentation_id") - .asText(); - TenantNetworkId id = null; - if (flag == CREATE_NETWORK) { - id = TenantNetworkId.networkId(node.get("id").asText()); - } else if (flag == UPDATE_NETWORK) { - id = networkId; - } - network = new DefaultTenantNetwork( - id, - name, - adminStateUp, - isState(state), - shared, - TenantId.tenantId(tenantId), - routerExternal, - isType(type), - PhysicalNetwork - .physicalNetwork(physicalNetwork), - SegmentationId - .segmentationId(segmentationId)); - networksMap.putIfAbsent(id, network); + checkArgument(node.get("admin_state_up").isBoolean(), "admin_state_up should be boolean"); + checkArgument(node.get("shared").isBoolean(), "shared should be boolean"); + checkArgument(node.get("router:external").isBoolean(), "router:external should be boolean"); + String name = node.get("name").asText(); + boolean adminStateUp = node.get("admin_state_up").asBoolean(); + String state = node.get("status").asText(); + boolean shared = node.get("shared").asBoolean(); + String tenantId = node.get("tenant_id").asText(); + boolean routerExternal = node.get("router:external").asBoolean(); + String type = node.get("provider:network_type").asText(); + String physicalNetwork = node.get("provider:physical_network").asText(); + String segmentationId = node.get("provider:segmentation_id").asText(); + TenantNetworkId id = null; + if (flag.equals(CREATE_NETWORK)) { + id = TenantNetworkId.networkId(node.get("id").asText()); + } else if (flag.equals(UPDATE_NETWORK)) { + id = networkId; } + network = new DefaultTenantNetwork( + id, + name, + adminStateUp, + isState(state), + shared, + TenantId.tenantId(tenantId), + routerExternal, + isType(type), + PhysicalNetwork + .physicalNetwork(physicalNetwork), + SegmentationId + .segmentationId(segmentationId)); + networksMap.putIfAbsent(id, network); + return Collections.unmodifiableCollection(networksMap.values()); } @@ -319,38 +316,32 @@ public class TenantNetworkWebResource extends AbstractWebResource { TenantNetwork network = null; ConcurrentMap<TenantNetworkId, TenantNetwork> networksMap = Maps .newConcurrentMap(); - if (nodes != null) { - for (JsonNode node : nodes) { - String id = node.get("id").asText(); - String name = node.get("name").asText(); - boolean adminStateUp = node.get("admin_state_up").asBoolean(); - String state = node.get("status").asText(); - boolean shared = node.get("shared").asBoolean(); - String tenantId = node.get("tenant_id").asText(); - boolean routerExternal = node.get("router:external") - .asBoolean(); - String type = node.get("provider:network_type").asText(); - String physicalNetwork = node.get("provider:physical_network") - .asText(); - String segmentationId = node.get("provider:segmentation_id") - .asText(); - network = new DefaultTenantNetwork( - TenantNetworkId - .networkId(id), - name, - adminStateUp, - isState(state), - shared, - TenantId.tenantId(tenantId), - routerExternal, - isType(type), - PhysicalNetwork - .physicalNetwork(physicalNetwork), - SegmentationId - .segmentationId(segmentationId)); - networksMap.putIfAbsent(TenantNetworkId.networkId(id), network); - } + for (JsonNode node : nodes) { + String id = node.get("id").asText(); + String name = node.get("name").asText(); + boolean adminStateUp = node.get("admin_state_up").asBoolean(); + String state = node.get("status").asText(); + boolean shared = node.get("shared").asBoolean(); + String tenantId = node.get("tenant_id").asText(); + boolean routerExternal = node.get("router:external") + .asBoolean(); + String type = node.get("provider:network_type").asText(); + String physicalNetwork = node.get("provider:physical_network").asText(); + String segmentationId = node.get("provider:segmentation_id").asText(); + network = new DefaultTenantNetwork( + TenantNetworkId.networkId(id), + name, + adminStateUp, + isState(state), + shared, + TenantId.tenantId(tenantId), + routerExternal, + isType(type), + PhysicalNetwork.physicalNetwork(physicalNetwork), + SegmentationId.segmentationId(segmentationId)); + networksMap.putIfAbsent(TenantNetworkId.networkId(id), network); } + return Collections.unmodifiableCollection(networksMap.values()); } diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FloatingIpCodec.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FloatingIpCodec.java new file mode 100644 index 00000000..ff5aebb4 --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/FloatingIpCodec.java @@ -0,0 +1,98 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.web; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Iterator; +import java.util.List; + +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; +import org.onosproject.vtnrsc.FloatingIp; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * FloatingIp JSON codec. + */ +public final class FloatingIpCodec extends JsonCodec<FloatingIp> { + @Override + public ObjectNode encode(FloatingIp floatingIp, CodecContext context) { + checkNotNull(floatingIp, "floatingIp cannot be null"); + ObjectNode result = context + .mapper() + .createObjectNode() + .put("id", floatingIp.id().floatingIpId().toString()) + .put("floating_network_id", floatingIp.networkId().toString()) + .put("router_id", + floatingIp.routerId() == null ? null : floatingIp + .routerId().routerId()) + .put("tenant_id", floatingIp.tenantId().toString()) + .put("port_id", + floatingIp.portId() == null ? null : floatingIp.portId() + .toString()) + .put("fixed_ip_address", + floatingIp.fixedIp() == null ? null : floatingIp.fixedIp() + .toString()) + .put("floating_ip_address", floatingIp.floatingIp().toString()) + .put("status", floatingIp.status().toString()); + return result; + } + + public ObjectNode extracFields(FloatingIp floatingIp, CodecContext context, + List<String> fields) { + checkNotNull(floatingIp, "floatingIp cannot be null"); + ObjectNode result = context.mapper().createObjectNode(); + Iterator<String> i = fields.iterator(); + while (i.hasNext()) { + String s = i.next(); + if (s.equals("floating_network_id")) { + result.put("floating_network_id", floatingIp.networkId() + .toString()); + } + if (s.equals("router_id")) { + result.put("router_id", + floatingIp.routerId() == null ? null : floatingIp + .routerId().routerId()); + } + if (s.equals("tenant_id")) { + result.put("tenant_id", floatingIp.tenantId().toString()); + } + if (s.equals("port_id")) { + result.put("port_id", + floatingIp.portId() == null ? null : floatingIp + .portId().toString()); + } + if (s.equals("id")) { + result.put("id", floatingIp.id().floatingIpId().toString()); + } + if (s.equals("fixed_ip_address")) { + result.put("fixed_ip_address", + floatingIp.fixedIp() == null ? null : floatingIp + .fixedIp().toString()); + } + if (s.equals("floating_ip_address")) { + result.put("floating_ip_address", floatingIp.floatingIp() + .toString()); + } + if (s.equals("status")) { + result.put("status", floatingIp.status().toString()); + } + } + return result; + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/RouterCodec.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/RouterCodec.java new file mode 100644 index 00000000..61f7e955 --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/RouterCodec.java @@ -0,0 +1,91 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.web; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Iterator; +import java.util.List; + +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; +import org.onosproject.vtnrsc.Router; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * Router JSON codec. + */ +public class RouterCodec extends JsonCodec<Router> { + @Override + public ObjectNode encode(Router router, CodecContext context) { + checkNotNull(router, "router cannot be null"); + ObjectNode result = context + .mapper() + .createObjectNode() + .put("id", router.id().routerId()) + .put("status", router.status().toString()) + .put("name", router.name().toString()) + .put("admin_state_up", router.adminStateUp()) + .put("tenant_id", router.tenantId().toString()) + .put("routes", + router.routes() == null ? null : router.routes() + .toString()); + result.set("external_gateway_info", + router.externalGatewayInfo() == null ? null + : new RouterGatewayInfoCodec() + .encode(router.externalGatewayInfo(), context)); + + return result; + } + + public ObjectNode extracFields(Router router, CodecContext context, + List<String> fields) { + checkNotNull(router, "router cannot be null"); + ObjectNode result = context.mapper().createObjectNode(); + Iterator<String> i = fields.iterator(); + while (i.hasNext()) { + String s = i.next(); + if (s.equals("id")) { + result.put("id", router.id().routerId()); + } + if (s.equals("status")) { + result.put("status", router.status().toString()); + } + if (s.equals("name")) { + result.put("name", router.name().toString()); + } + if (s.equals("admin_state_up")) { + result.put("admin_state_up", router.adminStateUp()); + } + if (s.equals("tenant_id")) { + result.put("tenant_id", router.tenantId().toString()); + } + if (s.equals("routes")) { + result.put("routes", router.routes() == null ? null : router + .routes().toString()); + } + if (s.equals("external_gateway_info")) { + result.set("external_gateway_info", + router.externalGatewayInfo() == null ? null + : new RouterGatewayInfoCodec() + .encode(router.externalGatewayInfo(), + context)); + } + } + return result; + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/RouterGatewayInfoCodec.java b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/RouterGatewayInfoCodec.java new file mode 100644 index 00000000..cb9fb67d --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/RouterGatewayInfoCodec.java @@ -0,0 +1,39 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.web; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; +import org.onosproject.vtnrsc.RouterGateway; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * Subnet Router Gateway Info codec. + */ +public class RouterGatewayInfoCodec extends JsonCodec<RouterGateway> { + @Override + public ObjectNode encode(RouterGateway routerGateway, CodecContext context) { + checkNotNull(routerGateway, "routerGateway cannot be null"); + ObjectNode result = context.mapper().createObjectNode() + .put("network_id", routerGateway.networkId().toString()); + result.set("external_fixed_ips", new FixedIpCodec() + .encode(routerGateway.externalFixedIps(), context)); + return result; + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/main/webapp/WEB-INF/web.xml b/framework/src/onos/apps/vtn/vtnweb/src/main/webapp/WEB-INF/web.xml index 97337960..13b377bf 100644 --- a/framework/src/onos/apps/vtn/vtnweb/src/main/webapp/WEB-INF/web.xml +++ b/framework/src/onos/apps/vtn/vtnweb/src/main/webapp/WEB-INF/web.xml @@ -37,6 +37,8 @@ org.onosproject.vtnweb.resources.PortChainWebResource org.onosproject.vtnweb.resources.PortPairGroupWebResource org.onosproject.vtnweb.resources.PortPairWebResource + org.onosproject.vtnweb.resources.FloatingIpWebResource + org.onosproject.vtnweb.resources.RouterWebResource </param-value> </init-param> <load-on-startup>1</load-on-startup> diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java new file mode 100644 index 00000000..be645be0 --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.java @@ -0,0 +1,296 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.resources; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import javax.ws.rs.core.MediaType; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onlab.osgi.ServiceDirectory; +import org.onlab.osgi.TestServiceDirectory; +import org.onlab.packet.IpPrefix; +import org.onlab.rest.BaseResource; +import org.onosproject.vtnrsc.FlowClassifier; +import org.onosproject.vtnrsc.FlowClassifierId; +import org.onosproject.vtnrsc.TenantId; +import org.onosproject.vtnrsc.VirtualPortId; +import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; + +import com.eclipsesource.json.JsonObject; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.api.client.WebResource; +/** + * Unit tests for flow classifier REST APIs. + */ +public class FlowClassifierResourceTest extends VtnResourceTest { + + final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class); + + FlowClassifierId flowClassifierId1 = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051"); + TenantId tenantId1 = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1"); + VirtualPortId srcPortId1 = VirtualPortId.portId("dace4513-24fc-4fae-af4b-321c5e2eb3d1"); + VirtualPortId dstPortId1 = VirtualPortId.portId("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"); + + final MockFlowClassifier flowClassifier1 = new MockFlowClassifier(flowClassifierId1, tenantId1, "flowClassifier1", + "Mock flow classifier", "IPv4", "IP", 1001, 1500, + 5001, 6000, IpPrefix.valueOf("1.1.1.1/16"), + IpPrefix.valueOf("22.12.34.45/16"), + srcPortId1, dstPortId1); + + /** + * Mock class for a flow classifier. + */ + private static class MockFlowClassifier implements FlowClassifier { + + private final FlowClassifierId flowClassifierId; + private final TenantId tenantId; + private final String name; + private final String description; + private final String etherType; + private final String protocol; + private final int minSrcPortRange; + private final int maxSrcPortRange; + private final int minDstPortRange; + private final int maxDstPortRange; + private final IpPrefix srcIpPrefix; + private final IpPrefix dstIpPrefix; + private final VirtualPortId srcPort; + private final VirtualPortId dstPort; + + public MockFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name, + String description, String etherType, String protocol, int minSrcPortRange, + int maxSrcPortRange, int minDstPortRange, int maxDstPortRange, IpPrefix srcIpPrefix, + IpPrefix dstIpPrefix, VirtualPortId srcPort, VirtualPortId dstPort) { + this.flowClassifierId = flowClassifierId; + this.tenantId = tenantId; + this.name = name; + this.description = description; + this.etherType = etherType; + this.protocol = protocol; + this.minSrcPortRange = minSrcPortRange; + this.maxSrcPortRange = maxSrcPortRange; + this.minDstPortRange = minDstPortRange; + this.maxDstPortRange = maxDstPortRange; + this.srcIpPrefix = srcIpPrefix; + this.dstIpPrefix = dstIpPrefix; + this.srcPort = srcPort; + this.dstPort = dstPort; + } + + + @Override + public FlowClassifierId flowClassifierId() { + return flowClassifierId; + } + + @Override + public TenantId tenantId() { + return tenantId; + } + + @Override + public String name() { + return name; + } + + @Override + public String description() { + return description; + } + + @Override + public String etherType() { + return etherType; + } + + @Override + public String protocol() { + return protocol; + } + + @Override + public int minSrcPortRange() { + return minSrcPortRange; + } + + @Override + public int maxSrcPortRange() { + return maxSrcPortRange; + } + + @Override + public int minDstPortRange() { + return minDstPortRange; + } + + @Override + public int maxDstPortRange() { + return maxDstPortRange; + } + + @Override + public IpPrefix srcIpPrefix() { + return srcIpPrefix; + } + + @Override + public IpPrefix dstIpPrefix() { + return dstIpPrefix; + } + + @Override + public VirtualPortId srcPort() { + return srcPort; + } + + @Override + public VirtualPortId dstPort() { + return dstPort; + } + + @Override + public boolean exactMatch(FlowClassifier flowClassifier) { + return this.equals(flowClassifier) && + Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) && + Objects.equals(this.tenantId, flowClassifier.tenantId()); + } + } + + /** + * Sets up the global values for all the tests. + */ + @Before + public void setUpTest() { + ServiceDirectory testDirectory = new TestServiceDirectory().add(FlowClassifierService.class, + flowClassifierService); + BaseResource.setServiceDirectory(testDirectory); + + } + + /** + * Cleans up. + */ + @After + public void tearDownTest() { + } + + /** + * Tests the result of the rest api GET when there are no flow classifiers. + */ + @Test + public void testFlowClassifiersEmpty() { + + expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes(); + replay(flowClassifierService); + final WebResource rs = resource(); + final String response = rs.path("flow_classifiers").get(String.class); + assertThat(response, is("{\"flow_classifiers\":[]}")); + } + + /** + * Tests the result of a rest api GET for flow classifier id. + */ + @Test + public void testGetFlowClassifierId() { + + final Set<FlowClassifier> flowClassifiers = new HashSet<>(); + flowClassifiers.add(flowClassifier1); + + expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes(); + expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes(); + replay(flowClassifierService); + + final WebResource rs = resource(); + final String response = rs.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051").get(String.class); + final JsonObject result = JsonObject.readFrom(response); + assertThat(result, notNullValue()); + } + + /** + * Tests that a fetch of a non-existent flow classifier object throws an exception. + */ + @Test + public void testBadGet() { + expect(flowClassifierService.getFlowClassifier(anyObject())) + .andReturn(null).anyTimes(); + replay(flowClassifierService); + WebResource rs = resource(); + try { + rs.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); + fail("Fetch of non-existent flow classifier did not throw an exception"); + } catch (UniformInterfaceException ex) { + assertThat(ex.getMessage(), + containsString("returned a response status of")); + } + } + + /** + * Tests creating a flow classifier with POST. + */ + @Test + public void testPost() { + + expect(flowClassifierService.createFlowClassifier(anyObject())) + .andReturn(true).anyTimes(); + replay(flowClassifierService); + + WebResource rs = resource(); + InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json"); + + ClientResponse response = rs.path("flow_classifiers") + .type(MediaType.APPLICATION_JSON_TYPE) + .post(ClientResponse.class, jsonStream); + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); + } + + /** + * Tests deleting a flow classifier. + */ + @Test + public void testDelete() { + expect(flowClassifierService.removeFlowClassifier(anyObject())) + .andReturn(true).anyTimes(); + replay(flowClassifierService); + + WebResource rs = resource(); + + String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051"; + + ClientResponse deleteResponse = rs.path(location) + .type(MediaType.APPLICATION_JSON_TYPE) + .delete(ClientResponse.class); + assertThat(deleteResponse.getStatus(), + is(HttpURLConnection.HTTP_NO_CONTENT)); + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairResourceTest.java b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairResourceTest.java new file mode 100644 index 00000000..271904cc --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairResourceTest.java @@ -0,0 +1,232 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.resources; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import javax.ws.rs.core.MediaType; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onlab.osgi.ServiceDirectory; +import org.onlab.osgi.TestServiceDirectory; +import org.onlab.rest.BaseResource; +import org.onosproject.vtnrsc.PortPair; +import org.onosproject.vtnrsc.PortPairId; +import org.onosproject.vtnrsc.TenantId; +import org.onosproject.vtnrsc.portpair.PortPairService; + +import com.eclipsesource.json.JsonObject; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.api.client.WebResource; +/** + * Unit tests for port pair REST APIs. + */ +public class PortPairResourceTest extends VtnResourceTest { + + final PortPairService portPairService = createMock(PortPairService.class); + + PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); + TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); + + final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1", + "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1", + "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"); + + /** + * Mock class for a port pair. + */ + private static class MockPortPair implements PortPair { + + private final PortPairId portPairId; + private final TenantId tenantId; + private final String name; + private final String description; + private final String ingress; + private final String egress; + + public MockPortPair(PortPairId portPairId, TenantId tenantId, + String name, String description, + String ingress, String egress) { + + this.portPairId = portPairId; + this.tenantId = tenantId; + this.name = name; + this.description = description; + this.ingress = ingress; + this.egress = egress; + } + + @Override + public PortPairId portPairId() { + return portPairId; + } + + @Override + public TenantId tenantId() { + return tenantId; + } + + @Override + public String name() { + return name; + } + + @Override + public String description() { + return description; + } + + @Override + public String ingress() { + return ingress; + } + + @Override + public String egress() { + return egress; + } + + @Override + public boolean exactMatch(PortPair portPair) { + return this.equals(portPair) && + Objects.equals(this.portPairId, portPair.portPairId()) && + Objects.equals(this.tenantId, portPair.tenantId()); + } + } + + /** + * Sets up the global values for all the tests. + */ + @Before + public void setUpTest() { + ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService); + BaseResource.setServiceDirectory(testDirectory); + + } + + /** + * Cleans up. + */ + @After + public void tearDownTest() { + } + + /** + * Tests the result of the rest api GET when there are no port pairs. + */ + @Test + public void testPortPairsEmpty() { + + expect(portPairService.getPortPairs()).andReturn(null).anyTimes(); + replay(portPairService); + final WebResource rs = resource(); + final String response = rs.path("port_pairs").get(String.class); + assertThat(response, is("{\"port_pairs\":[]}")); + } + + /** + * Tests the result of a rest api GET for port pair id. + */ + @Test + public void testGetPortPairId() { + + final Set<PortPair> portPairs = new HashSet<>(); + portPairs.add(portPair1); + + expect(portPairService.exists(anyObject())).andReturn(true).anyTimes(); + expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes(); + replay(portPairService); + + final WebResource rs = resource(); + final String response = rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae").get(String.class); + final JsonObject result = JsonObject.readFrom(response); + assertThat(result, notNullValue()); + } + + /** + * Tests that a fetch of a non-existent port pair object throws an exception. + */ + @Test + public void testBadGet() { + expect(portPairService.getPortPair(anyObject())) + .andReturn(null).anyTimes(); + replay(portPairService); + WebResource rs = resource(); + try { + rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); + fail("Fetch of non-existent port pair did not throw an exception"); + } catch (UniformInterfaceException ex) { + assertThat(ex.getMessage(), + containsString("returned a response status of")); + } + } + + /** + * Tests creating a port pair with POST. + */ + @Test + public void testPost() { + + expect(portPairService.createPortPair(anyObject())) + .andReturn(true).anyTimes(); + replay(portPairService); + + WebResource rs = resource(); + InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json"); + + ClientResponse response = rs.path("port_pairs") + .type(MediaType.APPLICATION_JSON_TYPE) + .post(ClientResponse.class, jsonStream); + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); + } + + /** + * Tests deleting a port pair. + */ + @Test + public void testDelete() { + expect(portPairService.removePortPair(anyObject())) + .andReturn(true).anyTimes(); + replay(portPairService); + + WebResource rs = resource(); + + String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"; + + ClientResponse deleteResponse = rs.path(location) + .type(MediaType.APPLICATION_JSON_TYPE) + .delete(ClientResponse.class); + assertThat(deleteResponse.getStatus(), + is(HttpURLConnection.HTTP_NO_CONTENT)); + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/VtnResourceTest.java b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/VtnResourceTest.java new file mode 100644 index 00000000..4b95844d --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/VtnResourceTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.resources; + +import java.io.IOException; +import java.net.ServerSocket; + +import com.sun.jersey.test.framework.AppDescriptor; +import com.sun.jersey.test.framework.JerseyTest; +import com.sun.jersey.test.framework.WebAppDescriptor; + +/** + * Base class for VTN REST API tests. Performs common configuration operations. + */ +public class VtnResourceTest extends JerseyTest { + + /** + * Assigns an available port for the test. + * + * @param defaultPort If a port cannot be determined, this one is used. + * @return free port + */ + @Override + public int getPort(int defaultPort) { + try { + ServerSocket socket = new ServerSocket(0); + socket.setReuseAddress(true); + int port = socket.getLocalPort(); + socket.close(); + return port; + } catch (IOException ioe) { + return defaultPort; + } + } + + @Override + public AppDescriptor configure() { + return new WebAppDescriptor.Builder("org.onosproject.vtnweb.resources").build(); + } + +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/web/FlowClassifierCodecTest.java b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/web/FlowClassifierCodecTest.java new file mode 100644 index 00000000..be36aa83 --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/web/FlowClassifierCodecTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.web; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Before; +import org.junit.Test; +import org.onosproject.codec.JsonCodec; +import org.onosproject.vtnrsc.FlowClassifier; +import org.onosproject.vtnrsc.FlowClassifierId; +import org.onosproject.vtnrsc.TenantId; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * Flow classifier codec unit tests. + */ +public class FlowClassifierCodecTest { + + SfcCodecContext context; + JsonCodec<FlowClassifier> flowClassifierCodec; + /** + * Sets up for each test. Creates a context and fetches the flow classifier + * codec. + */ + @Before + public void setUp() { + context = new SfcCodecContext(); + flowClassifierCodec = context.codec(FlowClassifier.class); + assertThat(flowClassifierCodec, notNullValue()); + } + + /** + * Reads in a flow classifier from the given resource and decodes it. + * + * @param resourceName resource to use to read the JSON for the flow classifier + * @return decoded flow classifier + * @throws IOException if processing the resource fails + */ + private FlowClassifier getFlowClassifier(String resourceName) throws IOException { + InputStream jsonStream = FlowClassifierCodecTest.class + .getResourceAsStream(resourceName); + ObjectMapper mapper = new ObjectMapper(); + JsonNode json = mapper.readTree(jsonStream); + assertThat(json, notNullValue()); + FlowClassifier flowClassifier = flowClassifierCodec.decode((ObjectNode) json, context); + assertThat(flowClassifier, notNullValue()); + return flowClassifier; + } + + /** + * Checks that a simple flow classifier decodes properly. + * + * @throws IOException if the resource cannot be processed + */ + @Test + public void codecFlowClassifierTest() throws IOException { + + FlowClassifier flowClassifier = getFlowClassifier("flowClassifier.json"); + + assertThat(flowClassifier, notNullValue()); + + FlowClassifierId flowClassifierId = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051"); + TenantId tenantId = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1"); + + assertThat(flowClassifier.flowClassifierId().toString(), is(flowClassifierId.toString())); + assertThat(flowClassifier.name(), is("flow1")); + assertThat(flowClassifier.tenantId().toString(), is(tenantId.toString())); + assertThat(flowClassifier.description(), is("flow classifier")); + assertThat(flowClassifier.protocol(), is("tcp")); + assertThat(flowClassifier.minSrcPortRange(), is(22)); + assertThat(flowClassifier.maxSrcPortRange(), is(4000)); + assertThat(flowClassifier.minDstPortRange(), is(80)); + assertThat(flowClassifier.maxDstPortRange(), is(80)); + + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-FlowClassifier.json b/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-FlowClassifier.json new file mode 100644 index 00000000..6e72e8fd --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-FlowClassifier.json @@ -0,0 +1,14 @@ +{"flow_classifier": { + "id": "4a334cd4-fe9c-4fae-af4b-321c5e2eb051", + "name": "flow1", + "tenant_id": "1814726e2d22407b8ca76db5e567dcf1", + "description": "flow classifier", + "ethertype": "IPv4", + "protocol": "tcp", + "source_port_range_min": 22, "source_port_range_max": 4000, + "destination_port_range_min": 80, "destination_port_range_max": 80, + "source_ip_prefix": "1.1.1.1/16" , "destination_ip_prefix": "22.12.34.45/16", + "logical_destination_port": "dace4513-24fc-4fae-af4b-321c5e2eb3d1", + "logical_source_port": "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345" + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPair.json b/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPair.json new file mode 100644 index 00000000..2a774e31 --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPair.json @@ -0,0 +1,9 @@ +{"port_pair": { + "id": "78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae", + "name": "PP1", + "tenant_id": "d382007aa9904763a801f68ecf065cf5", + "description": "SF-A", + "ingress": "dace4513-24fc-4fae-af4b-321c5e2eb3d1", + "egress": "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345" + } +} diff --git a/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/web/flowClassifier.json b/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/web/flowClassifier.json new file mode 100644 index 00000000..0fc0b74e --- /dev/null +++ b/framework/src/onos/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/web/flowClassifier.json @@ -0,0 +1,11 @@ +{ + "id": "4a334cd4-fe9c-4fae-af4b-321c5e2eb051", + "name": "flow1", + "tenant_id": "1814726e2d22407b8ca76db5e567dcf1", + "description": "flow classifier", + "ethertype": "IPv4", + "protocol": "tcp", + "source_port_range_min": 22, "source_port_range_max": 4000, + "destination_port_range_min": 80, "destination_port_range_max": 80, + "source_ip_prefix": "1.1.1.1/16" , "destination_ip_prefix": "22.12.34.45/16" + } |