Merge "remove redundant methods for compatibility"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronPortsNorthbound.java
index 63d5aaf1e585e096045ebe9e0004ad3a598a0c70..7168298a18101922ed62114d316c52c38c65721c 100644 (file)
@@ -31,10 +31,7 @@ import javax.ws.rs.core.UriInfo;
 
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
-import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
-import org.opendaylight.neutron.spi.INeutronPortAware;
 import org.opendaylight.neutron.spi.INeutronPortCRUD;
-import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronPort;
 
@@ -57,18 +54,25 @@ import org.opendaylight.neutron.spi.NeutronPort;
  */
 
 @Path("/ports")
-public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
+public class NeutronPortsNorthbound
+    extends AbstractNeutronNorthbound<NeutronPort, NeutronPortRequest, INeutronPortCRUD> {
 
     private static final String RESOURCE_NAME = "Port";
 
-    private NeutronPort extractFields(NeutronPort o, List<String> fields) {
+    @Override
+    protected String getResourceName() {
+        return RESOURCE_NAME;
+    }
+
+    @Override
+    protected 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));
+            throw new ServiceUnavailableException(serviceUnavailable());
         }
         if (needNetworks) {
             answer = answer.fetchINeutronNetworkCRUD( this);
@@ -87,6 +91,16 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
         return answer;
     }
 
+    @Override
+    protected INeutronPortCRUD getNeutronCRUD() {
+        return getNeutronInterfaces(false, false).getPortInterface();
+    }
+
+    @Override
+    protected NeutronPortRequest newNeutronRequest(NeutronPort o) {
+        return new NeutronPortRequest(o);
+    }
+
     @Context
     UriInfo uriInfo;
 
@@ -108,12 +122,13 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
             @QueryParam("id") String queryID,
             @QueryParam("network_id") String queryNetworkID,
             @QueryParam("name") String queryName,
-            @QueryParam("admin_state_up") String queryAdminStateUp,
+            @QueryParam("admin_state_up") Boolean queryAdminStateUp,
             @QueryParam("status") String queryStatus,
             @QueryParam("mac_address") String queryMACAddress,
             @QueryParam("device_id") String queryDeviceID,
             @QueryParam("device_owner") String queryDeviceOwner,
             @QueryParam("tenant_id") String queryTenantID,
+            @QueryParam("port_security_enabled") Boolean queryPortSecurityEnabled,
             // linkTitle
             @QueryParam("limit") Integer limit,
             @QueryParam("marker") String marker,
@@ -121,7 +136,7 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
             // sorting not supported
             ) {
         INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
-        List<NeutronPort> allPorts = portInterface.getAllPorts();
+        List<NeutronPort> allPorts = portInterface.getAll();
         List<NeutronPort> ans = new ArrayList<NeutronPort>();
         Iterator<NeutronPort> i = allPorts.iterator();
         while (i.hasNext()) {
@@ -134,7 +149,8 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
                     (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
                     (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
                     (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
-                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
+                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID())) &&
+                    (queryPortSecurityEnabled == null || queryPortSecurityEnabled.equals(oSS.getPortSecurityEnabled()))) {
                 if (fields.size() > 0) {
                     ans.add(extractFields(oSS,fields));
                 } else {
@@ -171,18 +187,7 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
             @PathParam("portUUID") String portUUID,
             // return fields
             @QueryParam("fields") List<String> fields ) {
-        INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
-        if (!portInterface.portExists(portUUID)) {
-            throw new ResourceNotFoundException(uuidNoExist(RESOURCE_NAME));
-        }
-        if (fields.size() > 0) {
-            NeutronPort ans = portInterface.getPort(portUUID);
-            return Response.status(HttpURLConnection.HTTP_OK).entity(
-                    new NeutronPortRequest(extractFields(ans, fields))).build();
-        } else {
-            return Response.status(HttpURLConnection.HTTP_OK).entity(
-                    new NeutronPortRequest(portInterface.getPort(portUUID))).build();
-        }
+        return show(portUUID, fields);
     }
 
     /**
@@ -196,69 +201,32 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
         @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createPorts(final NeutronPortRequest input) {
-        NeutronCRUDInterfaces interfaces = getNeutronInterfaces(true, true);
-        INeutronPortCRUD portInterface = interfaces.getPortInterface();
-        if (input.isSingleton()) {
-            NeutronPort singleton = input.getSingleton();
-
-            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 < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                            return Response.status(status).build();
-                        }
-                    }
-                } else {
-                    throw new ServiceUnavailableException(NO_PROVIDERS);
-                }
-            } else {
-                throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-            }
-
-            // add the port to the cache
-            portInterface.addPort(singleton);
-            if (instances != null) {
-                for (Object instance : instances) {
-                    INeutronPortAware service = (INeutronPortAware) instance;
-                    service.neutronPortCreated(singleton);
-                }
-            }
-        } else {
-            Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
-            for (NeutronPort test : input.getBulk()) {
-
-                if (instances != null) {
-                    if (instances.length > 0) {
-                        for (Object instance : instances) {
-                            INeutronPortAware service = (INeutronPortAware) instance;
-                            int status = service.canCreatePort(test);
-                            if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                                return Response.status(status).build();
-                            }
-                        }
-                    } else {
-                        throw new ServiceUnavailableException(NO_PROVIDERS);
-                    }
-                } else {
-                    throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-                }
-            }
+        getNeutronInterfaces(true, true); // Ensure that services for networks and subnets are loaded
+        return create(input);
+    }
 
-            //once everything has passed, then we can add to the cache
-            for (NeutronPort test : input.getBulk()) {
-                portInterface.addPort(test);
-                if (instances != null) {
-                    for (Object instance : instances) {
-                        INeutronPortAware service = (INeutronPortAware) instance;
-                        service.neutronPortCreated(test);
-                    }
-                }
-            }
+    @Override
+    protected void updateDelta(String uuid, NeutronPort delta, NeutronPort original) {
+        /*
+         * 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
+         */
+        if (delta.getID() == null) {
+            delta.setID(uuid);
+        }
+        if (delta.getTenantID() == null) {
+            delta.setTenantID(original.getTenantID());
+        }
+        if (delta.getNetworkUUID() == null) {
+            delta.setNetworkUUID(original.getNetworkUUID());
+        }
+        if (delta.getMacAddress() == null) {
+            delta.setMacAddress(original.getMacAddress());
+        }
+        if (delta.getFixedIPs() == null) {
+            delta.setFixedIPs(original.getFixedIPs());
         }
-        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
     }
 
     /**
@@ -271,67 +239,15 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
     //@TypeHint(OpenStackPorts.class)
     @StatusCodes({
         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+        @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response updatePort(
             @PathParam("portUUID") String portUUID,
             NeutronPortRequest input
             ) {
-        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);
-        }
-        if (updatedObject.getTenantID() == null) {
-            updatedObject.setTenantID(original.getTenantID());
-        }
-        if (updatedObject.getNetworkUUID() == null) {
-            updatedObject.setNetworkUUID(original.getNetworkUUID());
-        }
-        if (updatedObject.getMacAddress() == null) {
-            updatedObject.setMacAddress(original.getMacAddress());
-        }
-        if (updatedObject.getFixedIPs() == null) {
-            updatedObject.setFixedIPs(original.getFixedIPs());
-        }
-
-        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.canUpdatePort(updatedObject, original);
-                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                        return Response.status(status).build();
-                    }
-                }
-            } else {
-                throw new ServiceUnavailableException(NO_PROVIDERS);
-            }
-        } else {
-            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-        }
-
         //        TODO: Support change of security groups
         // update the port and return the modified object
-        portInterface.updatePort(portUUID, updatedObject);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronPortAware service = (INeutronPortAware) instance;
-                service.neutronPortUpdated(updatedObject);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_OK).entity(
-                new NeutronPortRequest(updatedObject)).build();
-
+        return update(portUUID, input);
     }
 
     /**
@@ -341,35 +257,10 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
     @DELETE
     @StatusCodes({
         @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+        @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response deletePort(
             @PathParam("portUUID") String portUUID) {
-        INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
-
-        NeutronPort singleton = portInterface.getPort(portUUID);
-        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.canDeletePort(singleton);
-                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                        return Response.status(status).build();
-                    }
-                }
-            } else {
-                throw new ServiceUnavailableException(NO_PROVIDERS);
-            }
-        } else {
-            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-        }
-        portInterface.removePort(portUUID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronPortAware service = (INeutronPortAware) instance;
-                service.neutronPortDeleted(singleton);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+        return delete(portUUID);
     }
 }