Support for FIQL queries on REST retreive operations
[controller.git] / opendaylight / northbound / staticrouting / src / main / java / org / opendaylight / controller / forwarding / staticrouting / northbound / StaticRoutingNorthbound.java
index 07f125dae3f79a8428d7609a18c1f4194b2553a1..20f6cb40a56cb812fa5370a5dd01cc6bf2ef85e8 100644 (file)
@@ -15,17 +15,17 @@ import java.util.List;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
-import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
 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.SecurityContext;
 import javax.ws.rs.core.UriInfo;
-import javax.xml.bind.JAXBElement;
+import javax.ws.rs.ext.ContextResolver;
 
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
@@ -39,6 +39,7 @@ import org.opendaylight.controller.northbound.commons.exception.NotAcceptableExc
 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
+import org.opendaylight.controller.northbound.commons.query.QueryContext;
 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
 import org.opendaylight.controller.sal.authorization.Privilege;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
@@ -57,7 +58,12 @@ import org.opendaylight.controller.sal.utils.Status;
  * HTTP/1.1 200 OK
  * Content-Type: application/json
  *
- * {"staticRoute":{"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}}
+ * { "staticRoute":[
+ *       "name":"route-1",
+ *       "prefix":"10.10.1.0/24",
+ *       "nextHop":"1.1.1.1"
+ *   ]
+ * }
  *
  * </pre>
  *
@@ -71,6 +77,14 @@ import org.opendaylight.controller.sal.utils.Status;
 public class StaticRoutingNorthbound {
 
     private String username;
+    private QueryContext queryContext;
+
+    @Context
+    public void setQueryContext(ContextResolver<QueryContext> queryCtxResolver) {
+      if (queryCtxResolver != null) {
+        queryContext = queryCtxResolver.getContext(QueryContext.class);
+      }
+    }
 
     @Context
     public void setSecurityContext(SecurityContext context) {
@@ -114,9 +128,9 @@ public class StaticRoutingNorthbound {
      * Example:
      *
      * Request URL:
-     * GET http://localhost:8080/controller/nb/v2/staticroute/default/routes
+     * http://localhost:8080/controller/nb/v2/staticroute/default/routes
      *
-     * Response in XML:
+     * Response body in XML:
      *  &lt;list&gt;
      *   &lt;staticRoute&gt;
      *     &lt;name&gt;route-1&lt;/name&gt;
@@ -125,9 +139,16 @@ public class StaticRoutingNorthbound {
      *   &lt;/staticRoute&gt;
      *  &lt;/list&gt;
      *
-     * Response in JSON:
-     * {"staticRoute":{"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}}
-     *
+     * Response body in JSON:
+     * {
+     *    "staticRoute": [
+     *      {
+     *       "name": "route-1",
+     *       "prefix": "10.10.1.0/24",
+     *       "nextHop": "1.1.1.1"
+     *      }
+     *    ]
+     * }
      * </pre>
      */
     @Path("/{containerName}/routes")
@@ -138,7 +159,8 @@ public class StaticRoutingNorthbound {
             @ResponseCode(code = 200, condition = "Operation successful"),
             @ResponseCode(code = 404, condition = "The containerName passed was not found") })
     public StaticRoutes getStaticRoutes(
-            @PathParam("containerName") String containerName) {
+            @PathParam("containerName") String containerName,
+            @QueryParam("_q") String queryString) {
 
         if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
                 Privilege.WRITE, this)){
@@ -146,7 +168,12 @@ public class StaticRoutingNorthbound {
                 UnauthorizedException("User is not authorized to perform this operation on container "
                             + containerName);
         }
-        return new StaticRoutes(getStaticRoutesInternal(containerName));
+        StaticRoutes result = new StaticRoutes(getStaticRoutesInternal(containerName));
+        if (queryString != null) {
+            queryContext.createQuery(queryString, StaticRoutes.class)
+                .filter(result, StaticRoute.class);
+        }
+        return result;
     }
 
     /**
@@ -160,9 +187,9 @@ public class StaticRoutingNorthbound {
      * Example:
      *
      * Request URL:
-     * GET http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
+     * http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
      *
-     * Response in XML:
+     * Response body in XML:
      *
      *   &lt;staticRoute&gt;
      *     &lt;name&gt;route-1&lt;/name&gt;
@@ -170,8 +197,12 @@ public class StaticRoutingNorthbound {
      *     &lt;nextHop&gt;1.1.1.1&lt;/nextHop&gt;
      *   &lt;/staticRoute&gt;
      *
-     * Response in JSON:
-     * {"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}
+     * Response body in JSON:
+     * {
+     *    "name":"route-1",
+     *    "prefix":"10.10.1.0/24",
+     *    "nextHop":"1.1.1.1"
+     * }
      *
      * </pre>
      */
@@ -216,11 +247,20 @@ public class StaticRoutingNorthbound {
      * Example:
      *
      * Request URL:
-     * PUT http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
-     *
-     * Request payload in JSON:
-     * {"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}
+     * http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
      *
+     * Request body in XML:
+     * &lt;staticRoute&gt;
+     *   &lt;name&gt;route-1&lt;/name&gt;
+     *   &lt;prefix>10.10.1.0/24&lt;/prefix&gt;
+     *   &lt;nextHop&gt;1.1.1.1&lt;/nextHop&gt;
+     *   &lt;/staticRoute&gt;
+     * Request body in JSON:
+     * {
+     *    "name":"route-1",
+     *    "prefix":"10.10.1.0/24",
+     *    "nextHop":"1.1.1.1"
+     * }
      * </pre>
      */
     @Path("/{containerName}/route/{route}")