northbound: introduce a base class for nortubhound classes
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronPortsNorthbound.java
index db87a741b24cdc51bd05217c7ddeeda359f53270..63d5aaf1e585e096045ebe9e0004ad3a598a0c70 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright IBM Corporation, 2013.  All rights reserved.
+ * Copyright (c) 2013, 2015 IBM Corporation and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,8 +8,9 @@
 
 package org.opendaylight.neutron.northbound.api;
 
+import java.net.HttpURLConnection;
+
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 
@@ -36,8 +37,6 @@ import org.opendaylight.neutron.spi.INeutronPortCRUD;
 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronPort;
-import org.opendaylight.neutron.spi.NeutronSubnet;
-import org.opendaylight.neutron.spi.Neutron_IPs;
 
 /**
  * Neutron Northbound REST APIs.<br>
@@ -58,14 +57,36 @@ import org.opendaylight.neutron.spi.Neutron_IPs;
  */
 
 @Path("/ports")
-public class NeutronPortsNorthbound {
+public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
 
-    final String mac_regex="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
+    private static final String RESOURCE_NAME = "Port";
 
     private NeutronPort extractFields(NeutronPort o, List<String> fields) {
         return o.extractFields(fields);
     }
 
+    private NeutronCRUDInterfaces getNeutronInterfaces(boolean needNetworks, boolean needSubnets) {
+        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronPortCRUD(this);
+        if (answer.getPortInterface() == null) {
+            throw new ServiceUnavailableException(serviceUnavailable(RESOURCE_NAME));
+        }
+        if (needNetworks) {
+            answer = answer.fetchINeutronNetworkCRUD( this);
+            if (answer.getNetworkInterface() == null) {
+                throw new ServiceUnavailableException("Network CRUD Interface "
+                        + RestMessages.SERVICEUNAVAILABLE.toString());
+            }
+        }
+        if (needSubnets) {
+            answer = answer.fetchINeutronSubnetCRUD( this);
+            if (answer.getSubnetInterface() == null) {
+                throw new ServiceUnavailableException("Subnet CRUD Interface "
+                        + RestMessages.SERVICEUNAVAILABLE.toString());
+            }
+        }
+        return answer;
+    }
+
     @Context
     UriInfo uriInfo;
 
@@ -76,10 +97,10 @@ public class NeutronPortsNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.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 listPorts(
             // return fields
             @QueryParam("fields") List<String> fields,
@@ -99,11 +120,7 @@ public class NeutronPortsNorthbound {
             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
             // sorting not supported
             ) {
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
         List<NeutronPort> allPorts = portInterface.getAllPorts();
         List<NeutronPort> ans = new ArrayList<NeutronPort>();
         Iterator<NeutronPort> i = allPorts.iterator();
@@ -130,10 +147,10 @@ public class NeutronPortsNorthbound {
             // Return a paginated request
             NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit,
                     marker, pageReverse, uriInfo, ans, NeutronPort.class);
-            return Response.status(200).entity(request).build();
+            return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
         }
 
-        return Response.status(200).entity(
+        return Response.status(HttpURLConnection.HTTP_OK).entity(
                 new NeutronPortRequest(ans)).build();
     }
 
@@ -145,29 +162,25 @@ public class NeutronPortsNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.class)
     @StatusCodes({
-        @ResponseCode(code = 200, condition = "Operation successful"),
-        @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_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 showPort(
             @PathParam("portUUID") String portUUID,
             // return fields
             @QueryParam("fields") List<String> fields ) {
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
         if (!portInterface.portExists(portUUID)) {
-            throw new ResourceNotFoundException("port UUID does not exist.");
+            throw new ResourceNotFoundException(uuidNoExist(RESOURCE_NAME));
         }
         if (fields.size() > 0) {
             NeutronPort ans = portInterface.getPort(portUUID);
-            return Response.status(200).entity(
+            return Response.status(HttpURLConnection.HTTP_OK).entity(
                     new NeutronPortRequest(extractFields(ans, fields))).build();
         } else {
-            return Response.status(200).entity(
+            return Response.status(HttpURLConnection.HTTP_OK).entity(
                     new NeutronPortRequest(portInterface.getPort(portUUID))).build();
         }
     }
@@ -180,101 +193,29 @@ public class NeutronPortsNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.class)
     @StatusCodes({
-        @ResponseCode(code = 201, condition = "Created"),
-        @ResponseCode(code = 400, condition = "Bad Request"),
-        @ResponseCode(code = 401, condition = "Unauthorized"),
-        @ResponseCode(code = 403, condition = "Forbidden"),
-        @ResponseCode(code = 404, condition = "Not Found"),
-        @ResponseCode(code = 409, condition = "Conflict"),
-        @ResponseCode(code = 501, condition = "Not Implemented"),
-        @ResponseCode(code = 503, condition = "MAC generation failure"),
-        @ResponseCode(code = 503, condition = "No providers available") })
+        @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createPorts(final NeutronPortRequest input) {
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
-        if (networkInterface == null) {
-            throw new ServiceUnavailableException("Network CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
-        if (subnetInterface == null) {
-            throw new ServiceUnavailableException("Subnet CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        NeutronCRUDInterfaces interfaces = getNeutronInterfaces(true, true);
+        INeutronPortCRUD portInterface = interfaces.getPortInterface();
         if (input.isSingleton()) {
             NeutronPort singleton = input.getSingleton();
 
-            /*
-             * the port must be part of an existing network, must not already exist,
-             * have a valid MAC and the MAC not be in use
-             */
-            if (singleton.getNetworkUUID() == null) {
-                throw new BadRequestException("network UUID musy be specified");
-            }
-            if (portInterface.portExists(singleton.getID())) {
-                throw new BadRequestException("port UUID already exists");
-            }
-            if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
-                throw new ResourceNotFoundException("network UUID does not exist.");
-            }
-            if (singleton.getMacAddress() == null ||
-                    !singleton.getMacAddress().matches(mac_regex)) {
-                throw new BadRequestException("MAC address not properly formatted");
-            }
-            if (portInterface.macInUse(singleton.getMacAddress())) {
-                throw new ResourceConflictException("MAC Address is in use.");
-            }
-            /*
-             * if fixed IPs are specified, each one has to have an existing subnet ID
-             * that is in the same scoping network as the port.  In addition, if an IP
-             * address is specified it has to be a valid address for the subnet and not
-             * already in use
-             */
-            List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
-            if (fixedIPs != null && fixedIPs.size() > 0) {
-                Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
-                while (fixedIPIterator.hasNext()) {
-                    Neutron_IPs ip = fixedIPIterator.next();
-                    if (ip.getSubnetUUID() == null) {
-                        throw new BadRequestException("subnet UUID not specified");
-                    }
-                    NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
-                    if (subnet == null) {
-                        throw new BadRequestException("subnet UUID must exist");
-                    }
-                    if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
-                        throw new BadRequestException("network UUID must match that of subnet");
-                    }
-                    if (ip.getIpAddress() != null) {
-                        if (!subnet.isValidIP(ip.getIpAddress())) {
-                            throw new BadRequestException("IP address is not valid");
-                        }
-                        if (subnet.isIPInUse(ip.getIpAddress())) {
-                            throw new ResourceConflictException("IP address is in use.");
-                        }
-                    }
-                }
-            }
-
             Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
             if (instances != null) {
                 if (instances.length > 0) {
                     for (Object instance : instances) {
                         INeutronPortAware service = (INeutronPortAware) instance;
                         int status = service.canCreatePort(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);
             }
 
             // add the port to the cache
@@ -286,103 +227,28 @@ public class NeutronPortsNorthbound {
                 }
             }
         } else {
-            List<NeutronPort> bulk = input.getBulk();
-            Iterator<NeutronPort> i = bulk.iterator();
-            HashMap<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
             Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
-            while (i.hasNext()) {
-                NeutronPort test = i.next();
-
-                /*
-                 * the port must be part of an existing network, must not already exist,
-                 * have a valid MAC and the MAC not be in use.  Further the bulk request
-                 * can't already contain a new port with the same UUID
-                 */
-                if (portInterface.portExists(test.getID())) {
-                    throw new BadRequestException("port UUID already exists");
-                }
-                if (testMap.containsKey(test.getID())) {
-                    throw new BadRequestException("port UUID already exists");
-                }
-                for (NeutronPort check : testMap.values()) {
-                    if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
-                        throw new ResourceConflictException("MAC address already allocated");
-                    }
-                    for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
-                        for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
-                            if (test_fixedIP.getSubnetUUID().equals(check_fixedIP.getSubnetUUID())) {
-                                if (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress())) {
-                                    throw new ResourceConflictException("IP address already allocated");
-                                }
-                            }
-                        }
-                    }
-                }
-                testMap.put(test.getID(), test);
-                if (!networkInterface.networkExists(test.getNetworkUUID())) {
-                    throw new ResourceNotFoundException("network UUID does not exist.");
-                }
-                if (!test.getMacAddress().matches(mac_regex)) {
-                    throw new BadRequestException("MAC address not properly formatted");
-                }
-                if (portInterface.macInUse(test.getMacAddress())) {
-                    throw new ResourceConflictException("MAC address in use");
-                }
+            for (NeutronPort test : input.getBulk()) {
 
-                /*
-                 * if fixed IPs are specified, each one has to have an existing subnet ID
-                 * that is in the same scoping network as the port.  In addition, if an IP
-                 * address is specified it has to be a valid address for the subnet and not
-                 * already in use (or be the gateway IP address of the subnet)
-                 */
-                List<Neutron_IPs> fixedIPs = test.getFixedIPs();
-                if (fixedIPs != null && fixedIPs.size() > 0) {
-                    Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
-                    while (fixedIPIterator.hasNext()) {
-                        Neutron_IPs ip = fixedIPIterator.next();
-                        if (ip.getSubnetUUID() == null) {
-                            throw new BadRequestException("subnet UUID must be specified");
-                        }
-                        if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
-                            throw new BadRequestException("subnet UUID doesn't exists");
-                        }
-                        NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
-                        if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
-                            throw new BadRequestException("network UUID must match that of subnet");
-                        }
-                        if (ip.getIpAddress() != null) {
-                            if (!subnet.isValidIP(ip.getIpAddress())) {
-                                throw new BadRequestException("ip address not valid");
-                            }
-                            //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
-                            //same bulk create
-                            if (subnet.isIPInUse(ip.getIpAddress())) {
-                                throw new ResourceConflictException("IP address in use");
-                            }
-                        }
-                    }
-                }
                 if (instances != null) {
                     if (instances.length > 0) {
                         for (Object instance : instances) {
                             INeutronPortAware service = (INeutronPortAware) instance;
                             int status = service.canCreatePort(test);
-                            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);
                 }
             }
 
             //once everything has passed, then we can add to the cache
-            i = bulk.iterator();
-            while (i.hasNext()) {
-                NeutronPort test = i.next();
+            for (NeutronPort test : input.getBulk()) {
                 portInterface.addPort(test);
                 if (instances != null) {
                     for (Object instance : instances) {
@@ -392,7 +258,7 @@ public class NeutronPortsNorthbound {
                 }
             }
         }
-        return Response.status(201).entity(input).build();
+        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
     }
 
     /**
@@ -404,44 +270,37 @@ public class NeutronPortsNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.class)
     @StatusCodes({
-        @ResponseCode(code = 200, condition = "Operation successful"),
-        @ResponseCode(code = 400, condition = "Bad Request"),
-        @ResponseCode(code = 401, condition = "Unauthorized"),
-        @ResponseCode(code = 403, condition = "Forbidden"),
-        @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_UNAVAILABLE, condition = "No providers available") })
     public Response updatePort(
             @PathParam("portUUID") String portUUID,
             NeutronPortRequest input
             ) {
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        NeutronCRUDInterfaces interfaces = getNeutronInterfaces(false, true);
+        INeutronPortCRUD portInterface = interfaces.getPortInterface();
+        NeutronPort original = portInterface.getPort(portUUID);
+
+        /*
+         * note: what we would like to get is the complete object as it
+         * is known by neutron.  Until then, patch what we *do* get
+         * so that we don't lose already known information
+         */
+
+        NeutronPort updatedObject = input.getSingleton();
+        if (updatedObject.getID() == null) {
+            updatedObject.setID(portUUID);
         }
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
-        if (subnetInterface == null) {
-            throw new ServiceUnavailableException("Subnet CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        if (updatedObject.getTenantID() == null) {
+            updatedObject.setTenantID(original.getTenantID());
         }
-
-        // port has to exist and only a single delta is supported
-        if (!portInterface.portExists(portUUID)) {
-            throw new ResourceNotFoundException("port UUID does not exist.");
+        if (updatedObject.getNetworkUUID() == null) {
+            updatedObject.setNetworkUUID(original.getNetworkUUID());
         }
-        NeutronPort target = portInterface.getPort(portUUID);
-        if (!input.isSingleton()) {
-            throw new BadRequestException("only singleton edit suported");
+        if (updatedObject.getMacAddress() == null) {
+            updatedObject.setMacAddress(original.getMacAddress());
         }
-        NeutronPort singleton = input.getSingleton();
-        NeutronPort original = portInterface.getPort(portUUID);
-
-        // deltas restricted by Neutron
-        if (singleton.getID() != null || singleton.getTenantID() != null ||
-                singleton.getStatus() != null) {
-            throw new BadRequestException("attribute change blocked by Neutron");
+        if (updatedObject.getFixedIPs() == null) {
+            updatedObject.setFixedIPs(original.getFixedIPs());
         }
 
         Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
@@ -449,57 +308,29 @@ public class NeutronPortsNorthbound {
             if (instances.length > 0) {
                 for (Object instance : instances) {
                     INeutronPortAware service = (INeutronPortAware) instance;
-                    int status = service.canUpdatePort(singleton, original);
-                    if (status < 200 || status > 299) {
+                    int status = service.canUpdatePort(updatedObject, 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");
-        }
-
-        // Verify the new fixed ips are valid
-        List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
-        if (fixedIPs != null && fixedIPs.size() > 0) {
-            Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
-            while (fixedIPIterator.hasNext()) {
-                Neutron_IPs ip = fixedIPIterator.next();
-                if (ip.getSubnetUUID() == null) {
-                    throw new BadRequestException("subnet UUID must be specified");
-                }
-                if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
-                    throw new BadRequestException("subnet UUID doesn't exist.");
-                }
-                NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
-                if (!target.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
-                    throw new BadRequestException("network UUID must match that of subnet");
-                }
-                if (ip.getIpAddress() != null) {
-                    if (!subnet.isValidIP(ip.getIpAddress())) {
-                        throw new BadRequestException("invalid IP address");
-                    }
-                    if (subnet.isIPInUse(ip.getIpAddress())) {
-                        throw new ResourceConflictException("IP address in use");
-                    }
-                }
-            }
+            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
         }
 
         //        TODO: Support change of security groups
         // update the port and return the modified object
-        portInterface.updatePort(portUUID, singleton);
-        NeutronPort updatedPort = portInterface.getPort(portUUID);
+        portInterface.updatePort(portUUID, updatedObject);
         if (instances != null) {
             for (Object instance : instances) {
                 INeutronPortAware service = (INeutronPortAware) instance;
-                service.neutronPortUpdated(updatedPort);
+                service.neutronPortUpdated(updatedObject);
             }
         }
-        return Response.status(200).entity(
-                new NeutronPortRequest(updatedPort)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(
+                new NeutronPortRequest(updatedObject)).build();
 
     }
 
@@ -509,29 +340,12 @@ public class NeutronPortsNorthbound {
     @Path("{portUUID}")
     @DELETE
     @StatusCodes({
-        @ResponseCode(code = 204, condition = "No Content"),
-        @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_NO_CONTENT, condition = "No Content"),
+        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response deletePort(
             @PathParam("portUUID") String portUUID) {
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
-        if (portInterface == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
 
-        // port has to exist and not be owned by anyone.  then it can be removed from the cache
-        if (!portInterface.portExists(portUUID)) {
-            throw new ResourceNotFoundException("port UUID does not exist.");
-        }
-        NeutronPort port = portInterface.getPort(portUUID);
-        if (port.getDeviceID() != null ||
-                port.getDeviceOwner() != null) {
-            Response.status(403).build();
-        }
         NeutronPort singleton = portInterface.getPort(portUUID);
         Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
         if (instances != null) {
@@ -539,15 +353,15 @@ public class NeutronPortsNorthbound {
                 for (Object instance : instances) {
                     INeutronPortAware service = (INeutronPortAware) instance;
                     int status = service.canDeletePort(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);
         }
         portInterface.removePort(portUUID);
         if (instances != null) {
@@ -556,6 +370,6 @@ public class NeutronPortsNorthbound {
                 service.neutronPortDeleted(singleton);
             }
         }
-        return Response.status(204).build();
+        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
     }
 }