Make NN more transparent, part I
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronRoutersNorthbound.java
index fec761ff44a7088af785f8cacd3fe4873acbdf46..5fe6e6a7251328053c9f91a213c11035bea4f028 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.neutron.northbound.api;
 
+import java.net.HttpURLConnection;
+
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -59,11 +61,53 @@ import org.opendaylight.neutron.spi.NeutronSubnet;
 
 @Path("/routers")
 public class NeutronRoutersNorthbound {
+    static final String ROUTER_INTERFACE_STR = "network:router_interface";
+    static final String ROUTER_GATEWAY_STR = "network:router_gateway";
+    private static final int HTTP_OK_BOTTOM = 200;
+    private static final int HTTP_OK_TOP = 299;
+    private static final String INTERFACE_NAME = "Router CRUD Interface";
+    private static final String UUID_NO_EXIST = "Router UUID does not exist.";
+    private static final String NO_PROVIDERS = "No providers registered.  Please try again later";
+    private static final String NO_PROVIDER_LIST = "Couldn't get providers list.  Please try again later";
 
     private NeutronRouter extractFields(NeutronRouter o, List<String> fields) {
         return o.extractFields(fields);
     }
 
+    private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
+        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
+        if (answer.getRouterInterface() == null) {
+            throw new ServiceUnavailableException(INTERFACE_NAME
+                + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (flag) {
+            answer = answer.fetchINeutronNetworkCRUD(this);
+            if (answer.getNetworkInterface() == null) {
+                throw new ServiceUnavailableException("Network CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+            }
+        }
+        return answer;
+    }
+
+    private NeutronCRUDInterfaces getAttachInterfaces() {
+        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
+        if (answer.getRouterInterface() == null) {
+            throw new ServiceUnavailableException(INTERFACE_NAME
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        answer = answer.fetchINeutronPortCRUD(this).fetchINeutronSubnetCRUD(this);
+        if (answer.getPortInterface() == null) {
+            throw new ServiceUnavailableException("Port CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (answer.getSubnetInterface() == null) {
+            throw new ServiceUnavailableException("Subnet CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        return answer;
+    }
+
     /**
      * Returns a list of all Routers */
 
@@ -71,10 +115,10 @@ public class NeutronRoutersNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
     @StatusCodes({
-            @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response listRouters(
             // return fields
             @QueryParam("fields") List<String> fields,
@@ -91,9 +135,9 @@ public class NeutronRoutersNorthbound {
             @QueryParam("page_reverse") String pageReverse
             // sorting not supported
             ) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
+        INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
         if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
+            throw new ServiceUnavailableException(INTERFACE_NAME
                     + RestMessages.SERVICEUNAVAILABLE.toString());
         }
         List<NeutronRouter> allRouters = routerInterface.getAllRouters();
@@ -107,14 +151,15 @@ public class NeutronRoutersNorthbound {
                     (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
                     (queryExternalGatewayInfo == null || queryExternalGatewayInfo.equals(oSS.getExternalGatewayInfo())) &&
                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
-                if (fields.size() > 0)
+                if (fields.size() > 0) {
                     ans.add(extractFields(oSS,fields));
-                else
+                } else {
                     ans.add(oSS);
+                }
             }
         }
         //TODO: apply pagination to results
-        return Response.status(200).entity(
+        return Response.status(HttpURLConnection.HTTP_OK).entity(
                 new NeutronRouterRequest(ans)).build();
     }
 
@@ -126,31 +171,32 @@ public class NeutronRoutersNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
     @StatusCodes({
-            @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 403, condition = "Forbidden"),
-            @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response showRouter(
             @PathParam("routerUUID") String routerUUID,
             // return fields
             @QueryParam("fields") List<String> fields) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
+        INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
         if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
+            throw new ServiceUnavailableException(INTERFACE_NAME
                     + RestMessages.SERVICEUNAVAILABLE.toString());
         }
         if (!routerInterface.routerExists(routerUUID)) {
-            throw new ResourceNotFoundException("Router UUID not found");
+            throw new ResourceNotFoundException(UUID_NO_EXIST);
         }
         if (fields.size() > 0) {
             NeutronRouter ans = routerInterface.getRouter(routerUUID);
-            return Response.status(200).entity(
+            return Response.status(HttpURLConnection.HTTP_OK).entity(
                     new NeutronRouterRequest(extractFields(ans, fields))).build();
-        } else
-            return Response.status(200).entity(
+        } else {
+            return Response.status(HttpURLConnection.HTTP_OK).entity(
                     new NeutronRouterRequest(routerInterface.getRouter(routerUUID))).build();
+        }
     }
 
     /**
@@ -161,54 +207,33 @@ public class NeutronRoutersNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
     @StatusCodes({
-            @ResponseCode(code = 201, condition = "Created"),
-            @ResponseCode(code = 400, condition = "Bad Request"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+            @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createRouters(final NeutronRouterRequest input) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
-        if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
-        if (networkInterface == null) {
-            throw new ServiceUnavailableException("Network CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        NeutronCRUDInterfaces interfaces = getNeutronInterfaces(true);
+        INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
+        INeutronNetworkCRUD networkInterface = interfaces.getNetworkInterface();
         if (input.isSingleton()) {
             NeutronRouter singleton = input.getSingleton();
 
-            /*
-             * verify that the router doesn't already exist (issue: is deeper inspection necessary?)
-             * if there is external gateway information provided, verify that the specified network
-             * exists and has been designated as "router:external"
-             */
-            if (routerInterface.routerExists(singleton.getID()))
-                throw new BadRequestException("router UUID already exists");
-            if (singleton.getExternalGatewayInfo() != null) {
-                String externNetworkPtr = singleton.getExternalGatewayInfo().getNetworkID();
-                if (!networkInterface.networkExists(externNetworkPtr))
-                    throw new BadRequestException("External Network Pointer doesn't exist");
-                NeutronNetwork externNetwork = networkInterface.getNetwork(externNetworkPtr);
-                if (!externNetwork.isRouterExternal())
-                    throw new BadRequestException("External Network Pointer isn't marked as router:external");
-            }
             Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
             if (instances != null) {
                 if (instances.length > 0) {
                     for (Object instance : instances) {
                         INeutronRouterAware service = (INeutronRouterAware) instance;
                         int status = service.canCreateRouter(singleton);
-                        if (status < 200 || status > 299)
+                        if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
                             return Response.status(status).build();
+                        }
                     }
                 } else {
-                    throw new ServiceUnavailableException("No providers registered.  Please try again later");
+                    throw new ServiceUnavailableException(NO_PROVIDERS);
                 }
             } else {
-                throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
+                throw new ServiceUnavailableException(NO_PROVIDER_LIST);
             }
 
             /*
@@ -228,7 +253,7 @@ public class NeutronRoutersNorthbound {
              */
             throw new BadRequestException("Only singleton router creates supported");
         }
-        return Response.status(201).entity(input).build();
+        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
     }
 
     /**
@@ -240,84 +265,51 @@ public class NeutronRoutersNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
     @StatusCodes({
-            @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 400, condition = "Bad Request"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response updateRouter(
             @PathParam("routerUUID") String routerUUID,
             NeutronRouterRequest input
             ) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
-        if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
-        if (networkInterface == null) {
-            throw new ServiceUnavailableException("Network CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        NeutronCRUDInterfaces interfaces = getNeutronInterfaces(true);
+        INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
+        INeutronNetworkCRUD networkInterface = interfaces.getNetworkInterface();
 
-        /*
-         * router has to exist and only a single delta can be supplied
-         */
-        if (!routerInterface.routerExists(routerUUID))
-            throw new ResourceNotFoundException("Router UUID not found");
-        if (!input.isSingleton())
-            throw new BadRequestException("Only single router deltas supported");
-        NeutronRouter singleton = input.getSingleton();
+        NeutronRouter updatedRouter = input.getSingleton();
         NeutronRouter original = routerInterface.getRouter(routerUUID);
 
-        /*
-         * attribute changes blocked by Neutron
-         */
-        if (singleton.getID() != null || singleton.getTenantID() != null ||
-                singleton.getStatus() != null)
-            throw new BadRequestException("Request attribute change not allowed");
-
         Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
         if (instances != null) {
             if (instances.length > 0) {
                 for (Object instance : instances) {
                     INeutronRouterAware service = (INeutronRouterAware) instance;
-                    int status = service.canUpdateRouter(singleton, original);
-                    if (status < 200 || status > 299)
+                    int status = service.canUpdateRouter(updatedRouter, original);
+                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
                         return Response.status(status).build();
+                    }
                 }
             } else {
-                throw new ServiceUnavailableException("No providers registered.  Please try again later");
+                throw new ServiceUnavailableException(NO_PROVIDERS);
             }
         } else {
-            throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-        }
-        /*
-         * if the external gateway info is being changed, verify that the new network
-         * exists and has been designated as an external network
-         */
-        if (singleton.getExternalGatewayInfo() != null) {
-            String externNetworkPtr = singleton.getExternalGatewayInfo().getNetworkID();
-            if (!networkInterface.networkExists(externNetworkPtr))
-                throw new BadRequestException("External Network Pointer does not exist");
-            NeutronNetwork externNetwork = networkInterface.getNetwork(externNetworkPtr);
-            if (!externNetwork.isRouterExternal())
-                throw new BadRequestException("External Network Pointer isn't marked as router:external");
+            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
         }
 
         /*
          * update the router entry and return the modified object
          */
-        routerInterface.updateRouter(routerUUID, singleton);
-        NeutronRouter updatedRouter = routerInterface.getRouter(routerUUID);
+        routerInterface.updateRouter(routerUUID, updatedRouter);
         if (instances != null) {
             for (Object instance : instances) {
                 INeutronRouterAware service = (INeutronRouterAware) instance;
                 service.neutronRouterUpdated(updatedRouter);
             }
         }
-        return Response.status(200).entity(
+        return Response.status(HttpURLConnection.HTTP_OK).entity(
                 new NeutronRouterRequest(routerInterface.getRouter(routerUUID))).build();
 
     }
@@ -328,27 +320,16 @@ public class NeutronRoutersNorthbound {
     @Path("{routerUUID}")
     @DELETE
     @StatusCodes({
-            @ResponseCode(code = 204, condition = "No Content"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 409, condition = "Conflict"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response deleteRouter(
             @PathParam("routerUUID") String routerUUID) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
-        if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
 
-        /*
-         * verify that the router exists and is not in use before removing it
-         */
-        if (!routerInterface.routerExists(routerUUID))
-            throw new ResourceNotFoundException("Router UUID not found");
-        if (routerInterface.routerInUse(routerUUID))
-            throw new ResourceConflictException("Router UUID in Use");
         NeutronRouter singleton = routerInterface.getRouter(routerUUID);
         Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
         if (instances != null) {
@@ -356,14 +337,15 @@ public class NeutronRoutersNorthbound {
                 for (Object instance : instances) {
                     INeutronRouterAware service = (INeutronRouterAware) instance;
                     int status = service.canDeleteRouter(singleton);
-                    if (status < 200 || status > 299)
+                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
                         return Response.status(status).build();
+                    }
                 }
             } else {
-                throw new ServiceUnavailableException("No providers registered.  Please try again later");
+                throw new ServiceUnavailableException(NO_PROVIDERS);
             }
         } else {
-            throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
+            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
         }
         routerInterface.removeRouter(routerUUID);
         if (instances != null) {
@@ -372,7 +354,7 @@ public class NeutronRoutersNorthbound {
                 service.neutronRouterDeleted(singleton);
             }
         }
-        return Response.status(204).build();
+        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
     }
 
     /**
@@ -384,79 +366,40 @@ public class NeutronRoutersNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouterInterfaces.class)
     @StatusCodes({
-            @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 400, condition = "Bad Request"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 409, condition = "Conflict"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response addRouterInterface(
             @PathParam("routerUUID") String routerUUID,
             NeutronRouter_Interface input
             ) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
-        if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
-        if (subnetInterface == null) {
-            throw new ServiceUnavailableException("Subnet CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        NeutronCRUDInterfaces interfaces = getAttachInterfaces();
+        INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
+        INeutronPortCRUD portInterface = interfaces.getPortInterface();
+        INeutronSubnetCRUD subnetInterface = interfaces.getSubnetInterface();
 
-        /*
-         *  While the Neutron specification says that the router has to exist and the input can only specify either a subnet id
-         *  or a port id, but not both, this code assumes that the plugin has filled everything in for us and so both must be present
-         */
-        if (!routerInterface.routerExists(routerUUID))
-            throw new BadRequestException("Router UUID doesn't exist");
         NeutronRouter target = routerInterface.getRouter(routerUUID);
-        if (input.getSubnetUUID() == null ||
-                    input.getPortUUID() == null)
-            throw new BadRequestException("Must specify at subnet id, port id or both");
-
-        // check that the port is part of the subnet
-        NeutronSubnet targetSubnet = subnetInterface.getSubnet(input.getSubnetUUID());
-        if (targetSubnet == null)
-            throw new BadRequestException("Subnet id doesn't exist");
-        NeutronPort targetPort = portInterface.getPort(input.getPortUUID());
-        if (targetPort == null)
-            throw new BadRequestException("Port id doesn't exist");
-        if (!targetSubnet.getPortsInSubnet().contains(targetPort))
-            throw new BadRequestException("Port id not part of subnet id");
-
-        if (targetPort.getFixedIPs().size() != 1)
-            throw new BadRequestException("Port id must have a single fixedIP address");
-        if (targetPort.getDeviceID() != null ||
-                targetPort.getDeviceOwner() != null)
-            throw new ResourceConflictException("Target Port already allocated");
         Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
         if (instances != null) {
             if (instances.length > 0) {
                 for (Object instance : instances) {
                     INeutronRouterAware service = (INeutronRouterAware) instance;
                     int status = service.canAttachInterface(target, input);
-                    if (status < 200 || status > 299)
+                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
                         return Response.status(status).build();
+                    }
                 }
             } else {
-                throw new ServiceUnavailableException("No providers registered.  Please try again later");
+                throw new ServiceUnavailableException(NO_PROVIDERS);
             }
         } else {
-            throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
+            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
         }
 
-        //mark the port device id and device owner fields
-        targetPort.setDeviceOwner("network:router_interface");
-        targetPort.setDeviceID(routerUUID);
-
         target.addInterface(input.getPortUUID(), input);
         if (instances != null) {
             for (Object instance : instances) {
@@ -465,7 +408,28 @@ public class NeutronRoutersNorthbound {
             }
         }
 
-        return Response.status(200).entity(input).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
+    }
+
+
+    private int checkDownstreamDetach(NeutronRouter target, NeutronRouter_Interface input) {
+        Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
+        if (instances != null) {
+            if (instances.length > 0) {
+                for (Object instance : instances) {
+                    INeutronRouterAware service = (INeutronRouterAware) instance;
+                    int status = service.canDetachInterface(target, input);
+                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
+                        return status;
+                    }
+                }
+            } else {
+                throw new ServiceUnavailableException(NO_PROVIDERS);
+            }
+        } else {
+            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
+        }
+        return HTTP_OK_BOTTOM;
     }
 
     /**
@@ -477,173 +441,35 @@ public class NeutronRoutersNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouterInterfaces.class)
     @StatusCodes({
-            @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 400, condition = "Bad Request"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 409, condition = "Conflict"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response removeRouterInterface(
             @PathParam("routerUUID") String routerUUID,
             NeutronRouter_Interface input
             ) {
-        INeutronRouterCRUD routerInterface = NeutronCRUDInterfaces.getINeutronRouterCRUD(this);
-        if (routerInterface == null) {
-            throw new ServiceUnavailableException("Router CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
-        if (subnetInterface == null) {
-            throw new ServiceUnavailableException("Subnet CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        NeutronCRUDInterfaces interfaces = getAttachInterfaces();
+        INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
+        INeutronPortCRUD portInterface = interfaces.getPortInterface();
+        INeutronSubnetCRUD subnetInterface = interfaces.getSubnetInterface();
+        Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
 
-        // verify the router exists
-        if (!routerInterface.routerExists(routerUUID))
-            throw new BadRequestException("Router does not exist");
         NeutronRouter target = routerInterface.getRouter(routerUUID);
-
-        /*
-         * remove by subnet id.  Collect information about the impacted router for the response and
-         * remove the port corresponding to the gateway IP address of the subnet
-         */
-        if (input.getPortUUID() == null &&
-                input.getSubnetUUID() != null) {
-            NeutronPort port = portInterface.getGatewayPort(input.getSubnetUUID());
-            if (port == null)
-                throw new ResourceNotFoundException("Port UUID not found");
-            input.setPortUUID(port.getID());
-            input.setID(target.getID());
-            input.setTenantID(target.getTenantID());
-
-            Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
-            if (instances != null) {
-                if (instances.length > 0) {
-                    for (Object instance : instances) {
-                        INeutronRouterAware service = (INeutronRouterAware) instance;
-                        int status = service.canDetachInterface(target, input);
-                        if (status < 200 || status > 299)
-                            return Response.status(status).build();
-                    }
-                } else {
-                    throw new ServiceUnavailableException("No providers registered.  Please try again later");
-                }
-            } else {
-                throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-            }
-
-            // reset the port ownership
-            port.setDeviceID(null);
-            port.setDeviceOwner(null);
-
-            target.removeInterface(input.getPortUUID());
-            if (instances != null) {
-                for (Object instance : instances) {
-                    INeutronRouterAware service = (INeutronRouterAware) instance;
-                    service.neutronRouterInterfaceDetached(target, input);
-                }
-            }
-            return Response.status(200).entity(input).build();
+        input.setID(target.getID());
+        input.setTenantID(target.getTenantID());
+        int status = checkDownstreamDetach(target, input);
+        if (status != HTTP_OK_BOTTOM) {
+            return Response.status(status).build();
         }
-
-        /*
-         * remove by port id. collect information about the impacted router for the response
-         * remove the interface and reset the port ownership
-         */
-        if (input.getPortUUID() != null &&
-                input.getSubnetUUID() == null) {
-            NeutronRouter_Interface targetInterface = target.getInterfaces().get(input.getPortUUID());
-            if (targetInterface == null) {
-                throw new ResourceNotFoundException("Router interface not found for given Port UUID");
-            }
-            input.setSubnetUUID(targetInterface.getSubnetUUID());
-            input.setID(target.getID());
-            input.setTenantID(target.getTenantID());
-            Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
-            if (instances != null) {
-                if (instances.length > 0) {
-                    for (Object instance : instances) {
-                        INeutronRouterAware service = (INeutronRouterAware) instance;
-                        int status = service.canDetachInterface(target, input);
-                        if (status < 200 || status > 299)
-                            return Response.status(status).build();
-                    }
-                } else {
-                    throw new ServiceUnavailableException("No providers registered.  Please try again later");
-                }
-            } else {
-                throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-            }
-            NeutronPort port = portInterface.getPort(input.getPortUUID());
-            port.setDeviceID(null);
-            port.setDeviceOwner(null);
-            target.removeInterface(input.getPortUUID());
-            for (Object instance : instances) {
-                INeutronRouterAware service = (INeutronRouterAware) instance;
-                service.neutronRouterInterfaceDetached(target, input);
-            }
-            return Response.status(200).entity(input).build();
+        target.removeInterface(input.getPortUUID());
+        for (Object instance : instances) {
+            INeutronRouterAware service = (INeutronRouterAware) instance;
+            service.neutronRouterInterfaceDetached(target, input);
         }
-
-        /*
-         * remove by both port and subnet ID.  Verify that the first fixed IP of the port is a valid
-         * IP address for the subnet, and then remove the interface, collecting information about the
-         * impacted router for the response and reset port ownership
-         */
-        if (input.getPortUUID() != null &&
-                input.getSubnetUUID() != null) {
-            NeutronPort port = portInterface.getPort(input.getPortUUID());
-            if (port == null) {
-                throw new ResourceNotFoundException("Port UUID not found");
-            }
-            if (port.getFixedIPs() == null) {
-                throw new ResourceNotFoundException("Port UUID has no fixed IPs");
-            }
-            NeutronSubnet subnet = subnetInterface.getSubnet(input.getSubnetUUID());
-            if (subnet == null) {
-                throw new ResourceNotFoundException("Subnet UUID not found");
-            }
-            if (!subnet.isValidIP(port.getFixedIPs().get(0).getIpAddress()))
-                throw new ResourceConflictException("Target Port IP not in Target Subnet");
-            Object[] instances = NeutronUtil.getInstances(INeutronRouterAware.class, this);
-            if (instances != null) {
-                if (instances.length > 0) {
-                    for (Object instance : instances) {
-                        INeutronRouterAware service = (INeutronRouterAware) instance;
-                        int status = service.canDetachInterface(target, input);
-                        if (status < 200 || status > 299)
-                            return Response.status(status).build();
-                    }
-                } else {
-                    throw new ServiceUnavailableException("No providers registered.  Please try again later");
-                }
-            } else {
-                throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-            }
-            input.setID(target.getID());
-            input.setTenantID(target.getTenantID());
-            port.setDeviceID(null);
-            port.setDeviceOwner(null);
-            target.removeInterface(input.getPortUUID());
-            if (instances != null) {
-                for (Object instance : instances) {
-                    INeutronRouterAware service = (INeutronRouterAware) instance;
-                    service.canDetachInterface(target, input);
-                }
-            }            for (Object instance : instances) {
-                INeutronRouterAware service = (INeutronRouterAware) instance;
-                service.neutronRouterInterfaceDetached(target, input);
-            }
-            return Response.status(200).entity(input).build();
-        }
-
-        // have to specify either a port ID or a subnet ID
-        throw new BadRequestException("Must specify port id or subnet id or both");
+        return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
     }
 }