checkstyle: enable SummaryJavadoc
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronLoadBalancerNorthbound.java
index 1a17bf21c3f2039ef3a71cc2e11143f45c3affb7..6b35ed82eb481c43040d8bbffc459835f1f46fbc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Red Hat, Inc.
+ * Copyright (c) 2014, 2015 Red Hat, Inc. 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,
@@ -9,13 +9,8 @@
 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;
-import java.util.Map;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
@@ -27,12 +22,9 @@ 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.INeutronLoadBalancerAware;
 import org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
-import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronLoadBalancer;
 
 /**
@@ -53,34 +45,22 @@ import org.opendaylight.neutron.spi.NeutronLoadBalancer;
  *
  */
 @Path("/lbaas/loadbalancers")
-public class NeutronLoadBalancerNorthbound {
-
-    private static final int HTTP_OK_BOTTOM = 200;
-    private static final int HTTP_OK_TOP = 299;
-    private static final String INTERFACE_NAME = "LoadBalancer CRUD Interface";
-    private static final String UUID_NO_EXIST = "LoadBalancer 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";
+public final class NeutronLoadBalancerNorthbound
+        extends AbstractNeutronNorthbound<NeutronLoadBalancer, NeutronLoadBalancerRequest, INeutronLoadBalancerCRUD> {
 
-    private NeutronLoadBalancer extractFields(NeutronLoadBalancer o, List<String> fields) {
-        return o.extractFields(fields);
-    }
+    private static final String RESOURCE_NAME = "LoadBalancer";
 
-    private NeutronCRUDInterfaces getNeutronInterfaces() {
-        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronLoadBalancerCRUD(this);
-        if (answer.getLoadBalancerInterface() == null) {
-            throw new ServiceUnavailableException(INTERFACE_NAME
-                + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        return answer;
+    @Override
+    protected String getResourceName() {
+        return RESOURCE_NAME;
     }
 
     /**
-     * Returns a list of all LoadBalancer */
+     * Returns a list of all LoadBalancer.
+     */
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+    @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") })
@@ -92,7 +72,6 @@ public class NeutronLoadBalancerNorthbound {
             @QueryParam("id") String queryLoadBalancerID,
             @QueryParam("tenant_id") String queryLoadBalancerTenantID,
             @QueryParam("name") String queryLoadBalancerName,
-            @QueryParam("description") String queryLoadBalancerDescription,
             @QueryParam("status") String queryLoadBalancerStatus,
             @QueryParam("vip_address") String queryLoadBalancerVipAddress,
             @QueryParam("vip_subnet") String queryLoadBalancerVipSubnet,
@@ -100,46 +79,38 @@ public class NeutronLoadBalancerNorthbound {
             @QueryParam("limit") String limit,
             @QueryParam("marker") String marker,
             @QueryParam("page_reverse") String pageReverse
-            // sorting not supported
+    // sorting not supported
     ) {
-        INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
-        List<NeutronLoadBalancer> allLoadBalancers = loadBalancerInterface.getAllNeutronLoadBalancers();
-        List<NeutronLoadBalancer> ans = new ArrayList<NeutronLoadBalancer>();
-        Iterator<NeutronLoadBalancer> i = allLoadBalancers.iterator();
-        while (i.hasNext()) {
-            NeutronLoadBalancer nsg = i.next();
-            if ((queryLoadBalancerID == null ||
-                    queryLoadBalancerID.equals(nsg.getLoadBalancerID())) &&
-                    (queryLoadBalancerTenantID == null ||
-                            queryLoadBalancerTenantID.equals(nsg.getLoadBalancerTenantID())) &&
-                    (queryLoadBalancerName == null ||
-                            queryLoadBalancerName.equals(nsg.getLoadBalancerName())) &&
-                    (queryLoadBalancerDescription == null ||
-                            queryLoadBalancerDescription.equals(nsg.getLoadBalancerDescription())) &&
-                    (queryLoadBalancerVipAddress == null ||
-                            queryLoadBalancerVipAddress.equals(nsg.getLoadBalancerVipAddress())) &&
-                    (queryLoadBalancerVipSubnet == null ||
-                            queryLoadBalancerVipSubnet.equals(nsg.getLoadBalancerVipSubnetID()))) {
+        INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronCRUD();
+        List<NeutronLoadBalancer> allLoadBalancers = loadBalancerInterface.getAll();
+        List<NeutronLoadBalancer> ans = new ArrayList<>();
+        for (NeutronLoadBalancer nsg : allLoadBalancers) {
+            if ((queryLoadBalancerID == null || queryLoadBalancerID.equals(nsg.getID()))
+                    && (queryLoadBalancerTenantID == null || queryLoadBalancerTenantID.equals(nsg.getTenantID()))
+                    && (queryLoadBalancerName == null || queryLoadBalancerName.equals(nsg.getName()))
+                    && (queryLoadBalancerVipAddress == null
+                            || queryLoadBalancerVipAddress.equals(nsg.getLoadBalancerVipAddress()))
+                    && (queryLoadBalancerVipSubnet == null
+                            || queryLoadBalancerVipSubnet.equals(nsg.getLoadBalancerVipSubnetID()))) {
                 if (fields.size() > 0) {
-                    ans.add(extractFields(nsg,fields));
+                    ans.add(nsg.extractFields(fields));
                 } else {
                     ans.add(nsg);
                 }
             }
         }
-        return Response.status(HttpURLConnection.HTTP_OK).entity(
-                new NeutronLoadBalancerRequest(ans)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronLoadBalancerRequest(ans)).build();
     }
 
     /**
-     * Returns a specific LoadBalancer */
+     * Returns a specific LoadBalancer.
+     */
 
     @Path("{loadBalancerID}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
 
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+    @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"),
@@ -147,252 +118,49 @@ public class NeutronLoadBalancerNorthbound {
     public Response showLoadBalancer(@PathParam("loadBalancerID") String loadBalancerID,
             // return fields
             @QueryParam("fields") List<String> fields) {
-        INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
-        if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
-            throw new ResourceNotFoundException(UUID_NO_EXIST);
-        }
-        if (fields.size() > 0) {
-            NeutronLoadBalancer ans = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
-            return Response.status(HttpURLConnection.HTTP_OK).entity(
-                    new NeutronLoadBalancerRequest(extractFields(ans, fields))).build();
-        } else {
-            return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
-                    loadBalancerID))).build();
-        }
+        return show(loadBalancerID, fields);
     }
 
     /**
-     * Creates new LoadBalancer */
+     * Creates new LoadBalancer.
+     */
 
     @POST
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
 
-    @StatusCodes({
-            @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_FORBIDDEN, condition = "Forbidden"),
-            @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"),
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createLoadBalancers(final NeutronLoadBalancerRequest input) {
-        INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
-        if (input.isSingleton()) {
-            NeutronLoadBalancer singleton = input.getSingleton();
-
-            /*
-             *  Verify that the LoadBalancer doesn't already exist.
-             */
-            if (loadBalancerInterface.neutronLoadBalancerExists(singleton.getLoadBalancerID())) {
-                throw new BadRequestException("LoadBalancer UUID already exists");
-            }
-            Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
-            if (instances != null) {
-                if (instances.length > 0) {
-                    for (Object instance : instances) {
-                        INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                        int status = service.canCreateNeutronLoadBalancer(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);
-            }
-
-            loadBalancerInterface.addNeutronLoadBalancer(singleton);
-            if (instances != null) {
-                for (Object instance : instances) {
-                    INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                    service.neutronLoadBalancerCreated(singleton);
-                }
-            }
-        } else {
-            List<NeutronLoadBalancer> bulk = input.getBulk();
-            Iterator<NeutronLoadBalancer> i = bulk.iterator();
-            Map<String, NeutronLoadBalancer> testMap = new HashMap<String, NeutronLoadBalancer>();
-            Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
-            while (i.hasNext()) {
-                NeutronLoadBalancer test = i.next();
-
-                /*
-                 *  Verify that the loadbalancer doesn't already exist
-                 */
-
-                if (loadBalancerInterface.neutronLoadBalancerExists(test.getLoadBalancerID())) {
-                    throw new BadRequestException("Load Balancer Pool UUID already is already created");
-                }
-                if (testMap.containsKey(test.getLoadBalancerID())) {
-                    throw new BadRequestException("Load Balancer Pool UUID already exists");
-                }
-                if (instances != null) {
-                    if (instances.length > 0) {
-                        for (Object instance : instances) {
-                            INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                            int status = service.canCreateNeutronLoadBalancer(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
-             */
-            i = bulk.iterator();
-            while (i.hasNext()) {
-                NeutronLoadBalancer test = i.next();
-                loadBalancerInterface.addNeutronLoadBalancer(test);
-                if (instances != null) {
-                    for (Object instance : instances) {
-                        INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                        service.neutronLoadBalancerCreated(test);
-                    }
-                }
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
+        return create(input);
     }
 
     /**
-     * Updates a LoadBalancer Policy
+     * Updates a LoadBalancer Policy.
      */
     @Path("{loadBalancerID}")
     @PUT
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
 
-    @StatusCodes({
-            @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_FORBIDDEN, condition = "Forbidden"),
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
             @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 updateLoadBalancer(
-            @PathParam("loadBalancerID") String loadBalancerID, final NeutronLoadBalancerRequest input) {
-        INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
-
-        /*
-         * verify the LoadBalancer exists and there is only one delta provided
-         */
-        if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
-            throw new ResourceNotFoundException(UUID_NO_EXIST);
-        }
-        if (!input.isSingleton()) {
-            throw new BadRequestException("Only singleton edit supported");
-        }
-        NeutronLoadBalancer delta = input.getSingleton();
-        NeutronLoadBalancer original = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
-
-        /*
-         * updates restricted by Neutron
-         */
-        if (delta.getLoadBalancerID() != null ||
-                delta.getLoadBalancerTenantID() != null ||
-                delta.getLoadBalancerName() != null ||
-                delta.getLoadBalancerDescription() != null ||
-                delta.getLoadBalancerStatus() != null ||
-                delta.getLoadBalancerVipAddress() != null ||
-                delta.getLoadBalancerVipSubnetID() != null) {
-            throw new BadRequestException("Attribute edit blocked by Neutron");
-        }
-
-        Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
-        if (instances != null) {
-            if (instances.length > 0) {
-                for (Object instance : instances) {
-                    INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                    int status = service.canUpdateNeutronLoadBalancer(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
-         */
-        loadBalancerInterface.updateNeutronLoadBalancer(loadBalancerID, delta);
-        NeutronLoadBalancer updatedLoadBalancer = loadBalancerInterface.getNeutronLoadBalancer(
-                loadBalancerID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                service.neutronLoadBalancerUpdated(updatedLoadBalancer);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronLoadBalancerRequest(loadBalancerInterface.getNeutronLoadBalancer(
-                loadBalancerID))).build();
+    public Response updateLoadBalancer(@PathParam("loadBalancerID") String loadBalancerID,
+            final NeutronLoadBalancerRequest input) {
+        return update(loadBalancerID, input);
     }
 
     /**
-     * Deletes a LoadBalancer */
+     * Deletes a LoadBalancer.
+     */
 
     @Path("{loadBalancerID}")
     @DELETE
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
-            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
             @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 deleteLoadBalancer(
-            @PathParam("loadBalancerID") String loadBalancerID) {
-        INeutronLoadBalancerCRUD loadBalancerInterface = getNeutronInterfaces().getLoadBalancerInterface();
-
-        /*
-         * verify the LoadBalancer exists and it isn't currently in use
-         */
-        if (!loadBalancerInterface.neutronLoadBalancerExists(loadBalancerID)) {
-            throw new ResourceNotFoundException(UUID_NO_EXIST);
-        }
-        if (loadBalancerInterface.neutronLoadBalancerInUse(loadBalancerID)) {
-            return Response.status(HttpURLConnection.HTTP_CONFLICT).build();
-        }
-        NeutronLoadBalancer singleton = loadBalancerInterface.getNeutronLoadBalancer(loadBalancerID);
-        Object[] instances = NeutronUtil.getInstances(INeutronLoadBalancerAware.class, this);
-        if (instances != null) {
-            if (instances.length > 0) {
-                for (Object instance : instances) {
-                    INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                    int status = service.canDeleteNeutronLoadBalancer(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);
-        }
-
-
-        loadBalancerInterface.removeNeutronLoadBalancer(loadBalancerID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronLoadBalancerAware service = (INeutronLoadBalancerAware) instance;
-                service.neutronLoadBalancerDeleted(singleton);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+    public Response deleteLoadBalancer(@PathParam("loadBalancerID") String loadBalancerID) {
+        return delete(loadBalancerID);
     }
 }