Checkstyle formatting issues fix (Northbound API)
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronPortsNorthbound.java
index 1a88dd059559a3f49d36aa420ef119adb3e93348..d7f5dbb7aa32f11a4c43313dd20f978d7ddcfc54 100644 (file)
@@ -9,11 +9,9 @@
 package org.opendaylight.neutron.northbound.api;
 
 import java.net.HttpURLConnection;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.DefaultValue;
@@ -28,13 +26,9 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 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,36 +51,53 @@ 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);
+            answer = answer.fetchINeutronNetworkCRUD(this);
             if (answer.getNetworkInterface() == null) {
-                throw new ServiceUnavailableException("Network CRUD Interface "
-                        + RestMessages.SERVICEUNAVAILABLE.toString());
+                throw new ServiceUnavailableException(
+                        "Network CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
             }
         }
         if (needSubnets) {
-            answer = answer.fetchINeutronSubnetCRUD( this);
+            answer = answer.fetchINeutronSubnetCRUD(this);
             if (answer.getSubnetInterface() == null) {
-                throw new ServiceUnavailableException("Subnet CRUD Interface "
-                        + RestMessages.SERVICEUNAVAILABLE.toString());
+                throw new ServiceUnavailableException(
+                        "Subnet CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
             }
         }
         return answer;
     }
 
+    @Override
+    protected INeutronPortCRUD getNeutronCRUD() {
+        return getNeutronInterfaces(false, false).getPortInterface();
+    }
+
+    @Override
+    protected NeutronPortRequest newNeutronRequest(NeutronPort o) {
+        return new NeutronPortRequest(o);
+    }
+
     @Context
     UriInfo uriInfo;
 
@@ -96,11 +107,10 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.class)
-    @StatusCodes({
-        @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") })
+    @StatusCodes({ @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,
@@ -108,35 +118,40 @@ 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,
+            @QueryParam("qos_policy_id") String queryQosPolicyId,
             // linkTitle
             @QueryParam("limit") Integer limit,
             @QueryParam("marker") String marker,
             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
-            // sorting not supported
-            ) {
+    // sorting not supported
+    ) {
         INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
-        List<NeutronPort> allPorts = portInterface.getAllPorts();
-        List<NeutronPort> ans = new ArrayList<NeutronPort>();
+        List<NeutronPort> allPorts = portInterface.getAll();
+        List<NeutronPort> ans = new ArrayList<>();
         Iterator<NeutronPort> i = allPorts.iterator();
         while (i.hasNext()) {
             NeutronPort oSS = i.next();
-            if ((queryID == null || queryID.equals(oSS.getID())) &&
-                    (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
-                    (queryName == null || queryName.equals(oSS.getName())) &&
-                    (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
-                    (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
-                    (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
-                    (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
-                    (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
-                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
+            if ((queryID == null || queryID.equals(oSS.getID()))
+                    && (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID()))
+                    && (queryName == null || queryName.equals(oSS.getName()))
+                    && (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp()))
+                    && (queryStatus == null || queryStatus.equals(oSS.getStatus()))
+                    && (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress()))
+                    && (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID()))
+                    && (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner()))
+                    && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))
+                    && (queryPortSecurityEnabled == null
+                            || queryPortSecurityEnabled.equals(oSS.getPortSecurityEnabled()))
+                    && (queryQosPolicyId == null || queryQosPolicyId.equals(oSS.getQosPolicyId()))) {
                 if (fields.size() > 0) {
-                    ans.add(extractFields(oSS,fields));
+                    ans.add(extractFields(oSS, fields));
                 } else {
                     ans.add(oSS);
                 }
@@ -145,13 +160,12 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
 
         if (limit != null && ans.size() > 1) {
             // Return a paginated request
-            NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit,
-                    marker, pageReverse, uriInfo, ans, NeutronPort.class);
+            NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit, marker,
+                    pageReverse, uriInfo, ans, NeutronPort.class);
             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
         }
 
-        return Response.status(HttpURLConnection.HTTP_OK).entity(
-                new NeutronPortRequest(ans)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronPortRequest(ans)).build();
     }
 
     /**
@@ -161,28 +175,15 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.class)
-    @StatusCodes({
-        @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,
+    @StatusCodes({ @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 = 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();
-        }
+            @QueryParam("fields") List<String> fields) {
+        return show(portUUID, fields);
     }
 
     /**
@@ -192,146 +193,52 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackPorts.class)
-    @StatusCodes({
-        @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    @StatusCodes({ @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);
-                }
-            }
-
-            //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);
-                    }
-                }
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
+        getNeutronInterfaces(true, true); // Ensure that services for networks and subnets are loaded
+        return create(input);
     }
 
-    /**
-     * Updates a Port */
-
-    @Path("{portUUID}")
-    @PUT
-    @Produces({ MediaType.APPLICATION_JSON })
-    @Consumes({ MediaType.APPLICATION_JSON })
-    //@TypeHint(OpenStackPorts.class)
-    @StatusCodes({
-        @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
-            ) {
-        NeutronCRUDInterfaces interfaces = getNeutronInterfaces(false, true);
-        INeutronPortCRUD portInterface = interfaces.getPortInterface();
-        NeutronPort original = portInterface.getPort(portUUID);
-
+    @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
          */
-
-        NeutronPort updatedObject = input.getSingleton();
-        if (updatedObject.getID() == null) {
-            updatedObject.setID(portUUID);
+        if (delta.getID() == null) {
+            delta.setID(uuid);
         }
-        if (updatedObject.getTenantID() == null) {
-            updatedObject.setTenantID(original.getTenantID());
+        if (delta.getTenantID() == null) {
+            delta.setTenantID(original.getTenantID());
         }
-        if (updatedObject.getNetworkUUID() == null) {
-            updatedObject.setNetworkUUID(original.getNetworkUUID());
+        if (delta.getNetworkUUID() == null) {
+            delta.setNetworkUUID(original.getNetworkUUID());
         }
-        if (updatedObject.getMacAddress() == null) {
-            updatedObject.setMacAddress(original.getMacAddress());
+        if (delta.getMacAddress() == null) {
+            delta.setMacAddress(original.getMacAddress());
         }
-        if (updatedObject.getFixedIPs() == null) {
-            updatedObject.setFixedIPs(original.getFixedIPs());
+        if (delta.getFixedIPs() == null) {
+            delta.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);
-        }
+    /**
+     * Updates a Port */
 
+    @Path("{portUUID}")
+    @PUT
+    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_JSON })
+    //@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) {
         //        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);
     }
 
     /**
@@ -339,42 +246,10 @@ public class NeutronPortsNorthbound extends AbstractNeutronNorthbound {
 
     @Path("{portUUID}")
     @DELETE
-    @StatusCodes({
-        @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) {
-        final 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);
-        }
-        deleteUuid(RESOURCE_NAME, portUUID,
-                   new Remover() {
-                       public boolean remove(String uuid) {
-                           return portInterface.removePort(uuid);
-                       }
-                   });
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronPortAware service = (INeutronPortAware) instance;
-                service.neutronPortDeleted(singleton);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+    @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) {
+        return delete(portUUID);
     }
 }