Refactor Neutron Pagination Code to support Network, Port and Subnet
[controller.git] / opendaylight / northbound / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / northbound / NeutronSubnetsNorthbound.java
index dffac55c5030ac96da1b52bde366f009cbb4a63d..8f20269603b1ed1802fc04812974125016c1811b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright IBM Corporation, 2013.  All rights reserved.
+ * Copyright IBM Corporation and others, 2013.  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,
@@ -13,8 +13,10 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
@@ -22,8 +24,10 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+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;
@@ -33,13 +37,16 @@ import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
 import org.opendaylight.controller.northbound.commons.RestMessages;
+import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
+import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
+import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
 
 /**
- * Open DOVE Northbound REST APIs.<br>
- * This class provides REST APIs for managing open DOVE internals related to Subnets
+ * Neutron Northbound REST APIs for Subnets.<br>
+ * This class provides REST APIs for managing neutron Subnets
  *
  * <br>
  * <br>
@@ -62,6 +69,8 @@ public class NeutronSubnetsNorthbound {
         return o.extractFields(fields);
     }
 
+    @Context
+    UriInfo uriInfo;
 
     /**
      * Returns a list of all Subnets */
@@ -84,13 +93,15 @@ public class NeutronSubnetsNorthbound {
             @QueryParam("gateway_ip") String queryGatewayIP,
             @QueryParam("enable_dhcp") String queryEnableDHCP,
             @QueryParam("tenant_id") String queryTenantID,
-            // pagination
-            @QueryParam("limit") String limit,
+            @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
+            @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
+            // linkTitle
+            @QueryParam("limit") Integer limit,
             @QueryParam("marker") String marker,
-            @QueryParam("page_reverse") String pageReverse
+            @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
             // sorting not supported
             ) {
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
+        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
         if (subnetInterface == null) {
             throw new ServiceUnavailableException("Subnet CRUD Interface "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -107,7 +118,9 @@ public class NeutronSubnetsNorthbound {
                     (queryCIDR == null || queryCIDR.equals(oSS.getCidr())) &&
                     (queryGatewayIP == null || queryGatewayIP.equals(oSS.getGatewayIP())) &&
                     (queryEnableDHCP == null || queryEnableDHCP.equals(oSS.getEnableDHCP())) &&
-                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
+                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID())) &&
+                    (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(oSS.getIpV6AddressMode())) &&
+                    (queryIpV6RaMode == null || queryIpV6RaMode.equals(oSS.getIpV6RaMode()))){
                 if (fields.size() > 0) {
                     ans.add(extractFields(oSS,fields));
                 } else {
@@ -115,7 +128,14 @@ public class NeutronSubnetsNorthbound {
                 }
             }
         }
-        //TODO: apply pagination to results
+
+        if (limit != null && ans.size() > 1) {
+            // Return a paginated request
+            NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit,
+                    marker, pageReverse, uriInfo, ans, NeutronSubnet.class);
+            return Response.status(200).entity(request).build();
+        }
+
         return Response.status(200).entity(
                 new NeutronSubnetRequest(ans)).build();
     }
@@ -142,7 +162,7 @@ public class NeutronSubnetsNorthbound {
                     + RestMessages.SERVICEUNAVAILABLE.toString());
         }
         if (!subnetInterface.subnetExists(subnetUUID)) {
-            return Response.status(404).build();
+            throw new ResourceNotFoundException("subnet UUID does not exist.");
         }
         if (fields.size() > 0) {
             NeutronSubnet ans = subnetInterface.getSubnet(subnetUUID);
@@ -190,19 +210,19 @@ public class NeutronSubnetsNorthbound {
              *  *then* add the subnet to the cache
              */
             if (subnetInterface.subnetExists(singleton.getID())) {
-                return Response.status(400).build();
+                throw new BadRequestException("subnet UUID already exists");
             }
             if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
-                return Response.status(404).build();
+                throw new ResourceNotFoundException("network UUID does not exist.");
             }
             if (!singleton.isValidCIDR()) {
-                return Response.status(400).build();
+                throw new BadRequestException("invaild CIDR");
             }
             if (!singleton.initDefaults()) {
                 throw new InternalServerErrorException("subnet object could not be initialized properly");
             }
             if (singleton.gatewayIP_Pool_overlap()) {
-                return Response.status(409).build();
+                throw new ResourceConflictException("IP pool overlaps with gateway");
             }
             Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);
             if (instances != null) {
@@ -240,20 +260,20 @@ public class NeutronSubnetsNorthbound {
                     throw new InternalServerErrorException("subnet object could not be initialized properly");
                 }
                 if (subnetInterface.subnetExists(test.getID())) {
-                    return Response.status(400).build();
+                    throw new BadRequestException("subnet UUID already exists");
                 }
                 if (testMap.containsKey(test.getID())) {
-                    return Response.status(400).build();
+                    throw new BadRequestException("subnet UUID already exists");
                 }
                 testMap.put(test.getID(), test);
                 if (!networkInterface.networkExists(test.getNetworkUUID())) {
-                    return Response.status(404).build();
+                    throw new ResourceNotFoundException("network UUID does not exist.");
                 }
                 if (!test.isValidCIDR()) {
-                    return Response.status(400).build();
+                    throw new BadRequestException("Invalid CIDR");
                 }
                 if (test.gatewayIP_Pool_overlap()) {
-                    return Response.status(409).build();
+                    throw new ResourceConflictException("IP pool overlaps with gateway");
                 }
                 if (instances != null) {
                     for (Object instance : instances) {
@@ -312,10 +332,10 @@ public class NeutronSubnetsNorthbound {
          * verify the subnet exists and there is only one delta provided
          */
         if (!subnetInterface.subnetExists(subnetUUID)) {
-            return Response.status(404).build();
+            throw new ResourceNotFoundException("subnet UUID does not exist.");
         }
         if (!input.isSingleton()) {
-            return Response.status(400).build();
+            throw new BadRequestException("Only singleton edit supported");
         }
         NeutronSubnet delta = input.getSingleton();
         NeutronSubnet original = subnetInterface.getSubnet(subnetUUID);
@@ -326,7 +346,7 @@ public class NeutronSubnetsNorthbound {
         if (delta.getID() != null || delta.getTenantID() != null ||
                 delta.getIpVersion() != null || delta.getCidr() != null ||
                 delta.getAllocationPools() != null) {
-            return Response.status(400).build();
+            throw new BadRequestException("Attribute edit blocked by Neutron");
         }
 
         Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);
@@ -378,7 +398,7 @@ public class NeutronSubnetsNorthbound {
          * verify the subnet exists and it isn't currently in use
          */
         if (!subnetInterface.subnetExists(subnetUUID)) {
-            return Response.status(404).build();
+            throw new ResourceNotFoundException("subnet UUID does not exist.");
         }
         if (subnetInterface.subnetInUse(subnetUUID)) {
             return Response.status(409).build();