From dffa9fb108536dc31b02e5acca756beff0b2e22d Mon Sep 17 00:00:00 2001 From: Sapan Shah Date: Tue, 29 Oct 2013 17:22:59 -0700 Subject: [PATCH] Edit Static Flow from NB APIs Change-Id: I837a288755b7800abd8ccd76a35895eed82a4643 Signed-off-by: Sapan Shah --- .../northbound/FlowProgrammerNorthbound.java | 31 +++++++++++-------- .../integrationtest/NorthboundIT.java | 5 +-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/opendaylight/northbound/flowprogrammer/src/main/java/org/opendaylight/controller/flowprogrammer/northbound/FlowProgrammerNorthbound.java b/opendaylight/northbound/flowprogrammer/src/main/java/org/opendaylight/controller/flowprogrammer/northbound/FlowProgrammerNorthbound.java index c53e2d0a5e..973c63b790 100644 --- a/opendaylight/northbound/flowprogrammer/src/main/java/org/opendaylight/controller/flowprogrammer/northbound/FlowProgrammerNorthbound.java +++ b/opendaylight/northbound/flowprogrammer/src/main/java/org/opendaylight/controller/flowprogrammer/northbound/FlowProgrammerNorthbound.java @@ -34,7 +34,6 @@ import org.opendaylight.controller.northbound.commons.RestMessages; import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException; import org.opendaylight.controller.northbound.commons.exception.MethodNotAllowedException; import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException; -import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException; import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException; import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException; import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException; @@ -373,8 +372,7 @@ public class FlowProgrammerNorthbound { } /** - * Add a flow configuration. If a flow by the given name already exists, - * this method will respond with a non-successful status response. + * Add or Modify a flow configuration. If the flow exists already, it will replace the current flow. * * @param containerName * Name of the Container (Eg. 'default') @@ -433,6 +431,7 @@ public class FlowProgrammerNorthbound { @PUT @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @StatusCodes({ + @ResponseCode(code = 200, condition = "Static Flow modified successfully"), @ResponseCode(code = 201, condition = "Flow Config processed successfully"), @ResponseCode(code = 400, condition = "Failed to create Static Flow entry due to invalid flow configuration"), @ResponseCode(code = 401, condition = "User not authorized to perform this operation"), @@ -441,7 +440,7 @@ public class FlowProgrammerNorthbound { @ResponseCode(code = 409, condition = "Failed to create Static Flow entry due to Conflicting Name or configuration"), @ResponseCode(code = 500, condition = "Failed to create Static Flow entry. Failure Reason included in HTTP Error response"), @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") }) - public Response addFlow(@PathParam(value = "containerName") String containerName, + public Response addOrModifyFlow(@PathParam(value = "containerName") String containerName, @PathParam(value = "name") String name, @PathParam("nodeType") String nodeType, @PathParam(value = "nodeId") String nodeId, @TypeHint(FlowConfig.class) FlowConfig flowConfig) { @@ -465,18 +464,24 @@ public class FlowProgrammerNorthbound { } Node node = handleNodeAvailability(containerName, nodeType, nodeId); + Status status; FlowConfig staticFlow = frm.getStaticFlow(name, node); - if (staticFlow != null) { - throw new ResourceConflictException(name + " already exists." + RestMessages.RESOURCECONFLICT.toString()); - } - - Status status = frm.addStaticFlow(flowConfig); - if (status.isSuccess()) { - NorthboundUtils.auditlog("Flow Entry", username, "added", - name + " on Node " + NorthboundUtils.getNodeDesc(node, containerName, this), containerName); - return Response.status(Response.Status.CREATED).entity("Success").build(); + if (staticFlow == null) { + status = frm.addStaticFlow(flowConfig); + if(status.isSuccess()){ + NorthboundUtils.auditlog("Flow Entry", username, "added", + name + " on Node " + NorthboundUtils.getNodeDesc(node, containerName, this), containerName); + return Response.status(Response.Status.CREATED).entity("Success").build(); + } + } else { + status = frm.modifyStaticFlow(flowConfig); + if(status.isSuccess()){ + NorthboundUtils.auditlog("Flow Entry", username, "updated", + name + " on Node " + NorthboundUtils.getNodeDesc(node, containerName, this), containerName); + return NorthboundUtils.getResponse(status); + } } return NorthboundUtils.getResponse(status); } diff --git a/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIT.java b/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIT.java index 6829fadff5..7bec2722bf 100644 --- a/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIT.java +++ b/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIT.java @@ -859,10 +859,11 @@ public class NorthboundIT { JSONObject node = json.getJSONObject("node"); Assert.assertEquals(node.getString("type"), "STUB"); Assert.assertEquals(node.getString("id"), "51966"); - // test adding same flow again fails due to repeat name..return 409 + // test adding same flow again succeeds with a change in any field ..return Success // code + fc = "{\"name\":\"test1\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"LOOPBACK\"]}"; result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test1", "PUT", fc); - Assert.assertTrue(result.equals("409")); + Assert.assertTrue(result.equals("Success")); fc = "{\"name\":\"test2\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}"; result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test2", "PUT", fc); -- 2.36.6