checkstyle: enable SummaryJavadoc
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronSecurityRulesNorthbound.java
index 933f52881ba21142541a57ccb48e086a0c711b0f..717a70fa1b16ec915dfeda92514387d9973f12cb 100644 (file)
@@ -9,11 +9,8 @@
 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.GET;
@@ -25,17 +22,15 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
-
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
-import org.opendaylight.neutron.spi.INeutronSecurityRuleAware;
 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
-import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronSecurityRule;
 
 /**
  * Neutron Northbound REST APIs for Security Rule.<br>
  * This class provides REST APIs for managing neutron Security Rule
+ *
  * <p>
  * <br>
  * <br>
@@ -50,289 +45,131 @@ import org.opendaylight.neutron.spi.NeutronSecurityRule;
  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
  */
 
-@Path ("/security-group-rules")
-public class NeutronSecurityRulesNorthbound extends AbstractNeutronNorthbound {
+@Path("/security-group-rules")
+public final class NeutronSecurityRulesNorthbound
+        extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
     private static final String RESOURCE_NAME = "Security Rule";
 
-    private NeutronSecurityRule extractFields(NeutronSecurityRule o, List<String> fields) {
-        return o.extractFields(fields);
-    }
-
-    private NeutronCRUDInterfaces getNeutronInterfaces() {
-        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSecurityRuleCRUD(this);
-        if (answer.getSecurityRuleInterface() == null) {
-            throw new ServiceUnavailableException(serviceUnavailable(RESOURCE_NAME));
-        }
-        return answer;
+    @Override
+    protected String getResourceName() {
+        return RESOURCE_NAME;
     }
 
     /**
-     * Returns a list of all Security Rules
+     * Returns a list of all Security Rules.
      */
     @GET
-    @Produces ({MediaType.APPLICATION_JSON})
-    @StatusCodes ({
-            @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-            @ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+    @Produces({ MediaType.APPLICATION_JSON })
+    @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 listRules(
             // return fields
-            @QueryParam ("fields") List<String> fields,
+            @QueryParam("fields") List<String> fields,
             // OpenStack security rule attributes
-            @QueryParam ("id") String querySecurityRuleUUID,
-            @QueryParam ("direction") String querySecurityRuleDirection,
-            @QueryParam ("protocol") String querySecurityRuleProtocol,
-            @QueryParam ("port_range_min") Integer querySecurityRulePortMin,
-            @QueryParam ("port_range_max") Integer querySecurityRulePortMax,
-            @QueryParam ("ethertype") String querySecurityRuleEthertype,
-            @QueryParam ("remote_ip_prefix") String querySecurityRuleIpPrefix,
-            @QueryParam ("remote_group_id") String querySecurityRemoteGroupID,
-            @QueryParam ("security_group_id") String querySecurityRuleGroupID,
-            @QueryParam ("tenant_id") String querySecurityRuleTenantID,
-            @QueryParam ("limit") String limit,
-            @QueryParam ("marker") String marker,
-            @QueryParam ("page_reverse") String pageReverse
-    ) {
-        INeutronSecurityRuleCRUD securityRuleInterface = getNeutronInterfaces().getSecurityRuleInterface();
-        List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAllNeutronSecurityRules();
-        List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
-        Iterator<NeutronSecurityRule> i = allSecurityRules.iterator();
-        while (i.hasNext()) {
-            NeutronSecurityRule nsr = i.next();
-            if ((querySecurityRuleUUID == null ||
-                    querySecurityRuleUUID.equals(nsr.getID())) &&
-                    (querySecurityRuleDirection == null ||
-                            querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection())) &&
-                    (querySecurityRuleProtocol == null ||
-                            querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol())) &&
-                    (querySecurityRulePortMin == null ||
-                            querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin())) &&
-                    (querySecurityRulePortMax == null ||
-                            querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax())) &&
-                    (querySecurityRuleEthertype == null ||
-                            querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype())) &&
-                    (querySecurityRuleIpPrefix == null ||
-                            querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix())) &&
-                    (querySecurityRuleGroupID == null ||
-                            querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID())) &&
-                    (querySecurityRemoteGroupID == null ||
-                            querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID())) &&
-                    (querySecurityRuleTenantID == null ||
-                            querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
+            @QueryParam("id") String querySecurityRuleUUID,
+            @QueryParam("direction") String querySecurityRuleDirection,
+            @QueryParam("protocol") String querySecurityRuleProtocol,
+            @QueryParam("port_range_min") Integer querySecurityRulePortMin,
+            @QueryParam("port_range_max") Integer querySecurityRulePortMax,
+            @QueryParam("ethertype") String querySecurityRuleEthertype,
+            @QueryParam("remote_ip_prefix") String querySecurityRuleIpPrefix,
+            @QueryParam("remote_group_id") String querySecurityRemoteGroupID,
+            @QueryParam("security_group_id") String querySecurityRuleGroupID,
+            @QueryParam("tenant_id") String querySecurityRuleTenantID,
+            @QueryParam("limit") String limit,
+            @QueryParam("marker") String marker,
+            @QueryParam("page_reverse") String pageReverse) {
+        INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
+        List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
+        List<NeutronSecurityRule> ans = new ArrayList<>();
+        for (NeutronSecurityRule nsr : allSecurityRules) {
+            if ((querySecurityRuleUUID == null || querySecurityRuleUUID.equals(nsr.getID()))
+                    && (querySecurityRuleDirection == null
+                            || querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection()))
+                    && (querySecurityRuleProtocol == null
+                            || querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol()))
+                    && (querySecurityRulePortMin == null
+                            || querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin()))
+                    && (querySecurityRulePortMax == null
+                            || querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax()))
+                    && (querySecurityRuleEthertype == null
+                            || querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype()))
+                    && (querySecurityRuleIpPrefix == null
+                            || querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix()))
+                    && (querySecurityRuleGroupID == null
+                            || querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID()))
+                    && (querySecurityRemoteGroupID == null
+                            || querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID()))
+                    && (querySecurityRuleTenantID == null || querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
                 if (fields.size() > 0) {
-                    ans.add(extractFields(nsr, fields));
+                    ans.add(nsr.extractFields(fields));
                 } else {
                     ans.add(nsr);
                 }
             }
         }
-        return Response.status(HttpURLConnection.HTTP_OK).entity(
-                new NeutronSecurityRuleRequest(ans)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
     }
 
     /**
-     * Returns a specific Security Rule
+     * Returns a specific Security Rule.
      */
 
-    @Path ("{securityRuleUUID}")
+    @Path("{securityRuleUUID}")
     @GET
-    @Produces ({MediaType.APPLICATION_JSON})
-    @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"),
+    @Produces({ MediaType.APPLICATION_JSON })
+    @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 showSecurityRule(@PathParam ("securityRuleUUID") String securityRuleUUID,
-                                     // return fields
-                                     @QueryParam ("fields") List<String> fields) {
-        INeutronSecurityRuleCRUD securityRuleInterface = getNeutronInterfaces().getSecurityRuleInterface();
-        if (!securityRuleInterface.neutronSecurityRuleExists(securityRuleUUID)) {
-            throw new ResourceNotFoundException(uuidNoExist(RESOURCE_NAME));
-        }
-        if (!fields.isEmpty()) {
-            NeutronSecurityRule ans = securityRuleInterface.getNeutronSecurityRule(securityRuleUUID);
-            return Response.status(HttpURLConnection.HTTP_OK).entity(
-                    new NeutronSecurityRuleRequest(extractFields(ans, fields))).build();
-        } else {
-            return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(securityRuleInterface.getNeutronSecurityRule(securityRuleUUID))).build();
-        }
+    public Response showSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
+            // return fields
+            @QueryParam("fields") List<String> fields) {
+        return show(securityRuleUUID, fields);
     }
 
     /**
-     * Creates new Security Rule
+     * Creates new Security Rule.
      */
 
     @POST
-    @Produces ({MediaType.APPLICATION_JSON})
-    @Consumes ({MediaType.APPLICATION_JSON})
-    @StatusCodes ({
-            @ResponseCode (code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_JSON })
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
-        INeutronSecurityRuleCRUD securityRuleInterface = getNeutronInterfaces().getSecurityRuleInterface();
-
-        if (input.isSingleton()) {
-            NeutronSecurityRule singleton = input.getSingleton();
-            Object[] instances = NeutronUtil.getInstances(INeutronSecurityRuleAware.class, this);
-            if (instances != null) {
-                if (instances.length > 0) {
-                    for (Object instance : instances) {
-                        INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                        int status = service.canCreateNeutronSecurityRule(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 rule to cache
-            singleton.initDefaults();
-            securityRuleInterface.addNeutronSecurityRule(singleton);
-            if (instances != null) {
-                for (Object instance : instances) {
-                    INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                    service.neutronSecurityRuleCreated(singleton);
-                }
-            }
-        } else {
-            Object[] instances = NeutronUtil.getInstances(INeutronSecurityRuleAware.class, this);
-            for (NeutronSecurityRule test : input.getBulk()) {
-                if (instances != null) {
-                    if (instances.length > 0) {
-                        for (Object instance : instances) {
-                            INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                            int status = service.canCreateNeutronSecurityRule(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);
-                }
-            }
-
-            /*
-             * now, each element of the bulk request can be added to the cache
-             */
-            for (NeutronSecurityRule test : input.getBulk()) {
-                securityRuleInterface.addNeutronSecurityRule(test);
-                if (instances != null) {
-                    for (Object instance : instances) {
-                        INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                        service.neutronSecurityRuleCreated(test);
-                    }
-                }
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
+        return create(input);
     }
 
     /**
-     * Updates a Security Rule
+     * Updates a Security Rule.
      */
 
-    @Path ("{securityRuleUUID}")
+    @Path("{securityRuleUUID}")
     @PUT
-    @Produces ({MediaType.APPLICATION_JSON})
-    @Consumes ({MediaType.APPLICATION_JSON})
-    @StatusCodes ({
-            @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_JSON })
+    @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 updateSecurityRule(
-            @PathParam ("securityRuleUUID") String securityRuleUUID, final NeutronSecurityRuleRequest input) {
-        INeutronSecurityRuleCRUD securityRuleInterface = getNeutronInterfaces().getSecurityRuleInterface();
-
-        NeutronSecurityRule delta = input.getSingleton();
-        NeutronSecurityRule original = securityRuleInterface.getNeutronSecurityRule(securityRuleUUID);
-
-        Object[] instances = NeutronUtil.getInstances(INeutronSecurityRuleAware.class, this);
-        if (instances != null) {
-            if (instances.length > 0) {
-                for (Object instance : instances) {
-                    INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                    int status = service.canUpdateNeutronSecurityRule(delta, 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);
-        }
-
-        /*
-         * update the object and return it
-         */
-        securityRuleInterface.updateNeutronSecurityRule(securityRuleUUID, delta);
-        NeutronSecurityRule updatedSecurityRule = securityRuleInterface.getNeutronSecurityRule(securityRuleUUID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                service.neutronSecurityRuleUpdated(updatedSecurityRule);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(securityRuleInterface.getNeutronSecurityRule(securityRuleUUID))).build();
+    public Response updateSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
+            final NeutronSecurityRuleRequest input) {
+        return update(securityRuleUUID, input);
     }
 
     /**
-     * Deletes a Security Rule
+     * Deletes a Security Rule.
      */
 
-    @Path ("{securityRuleUUID}")
+    @Path("{securityRuleUUID}")
     @DELETE
-    @StatusCodes ({
-            @ResponseCode (code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+    @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 deleteSecurityRule(
-            @PathParam ("securityRuleUUID") String securityRuleUUID) {
-        final INeutronSecurityRuleCRUD securityRuleInterface = getNeutronInterfaces().getSecurityRuleInterface();
-
-        NeutronSecurityRule singleton = securityRuleInterface.getNeutronSecurityRule(securityRuleUUID);
-        Object[] instances = NeutronUtil.getInstances(INeutronSecurityRuleAware.class, this);
-        if (instances != null) {
-            if (instances.length > 0) {
-                for (Object instance : instances) {
-                    INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                    int status = service.canDeleteNeutronSecurityRule(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);
-        }
-
-
-        /*
-         * remove it and return 204 status
-         */
-        deleteUuid(RESOURCE_NAME, securityRuleUUID,
-                   new Remover() {
-                       public boolean remove(String uuid) {
-                           return securityRuleInterface.removeNeutronSecurityRule(uuid);
-                       }
-                   });
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
-                service.neutronSecurityRuleDeleted(singleton);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+    public Response deleteSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID) {
+        return delete(securityRuleUUID);
     }
 }