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 8462ef804ad0cce4c81819bd6b0369a675673566..20f6cb40a56cb812fa5370a5dd01cc6bf2ef85e8 100644 (file)
@@ -15,16 +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;
@@ -38,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;
@@ -56,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>
  *
@@ -70,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) {
@@ -113,9 +128,9 @@ public class StaticRoutingNorthbound {
      * Example:
      *
      * Request URL:
-     * GET http://localhost:8080/controller/nb/v2/staticroute/default
+     * 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;
@@ -124,12 +139,19 @@ 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}")
+    @Path("/{containerName}/routes")
     @GET
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(StaticRoutes.class)
@@ -137,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)){
@@ -145,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;
     }
 
     /**
@@ -159,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;
@@ -169,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>
      */
@@ -204,7 +236,8 @@ public class StaticRoutingNorthbound {
 
     /**
      *
-     * Add a new Static Route
+     * Add a new Static Route. If a route by the given name already exists, this
+     * method will return a non-successful status response.
      *
      * @param containerName Name of the Container. The Container name for the base controller is "default".
      * @param route Name of the Static Route configuration
@@ -214,15 +247,24 @@ public class StaticRoutingNorthbound {
      * Example:
      *
      * Request URL:
-     * POST 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}")
-    @POST
+    @PUT
     @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @StatusCodes( {
             @ResponseCode(code = 201, condition = "Created Static Route successfully"),
@@ -233,7 +275,7 @@ public class StaticRoutingNorthbound {
             @Context UriInfo uriInfo,
             @PathParam(value = "containerName") String containerName,
             @PathParam(value = "route") String route,
-            @TypeHint(StaticRoute.class) JAXBElement<StaticRoute> staticRouteData) {
+            @TypeHint(StaticRoute.class) StaticRoute staticRouteData) {
 
 
         if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
@@ -253,7 +295,7 @@ public class StaticRoutingNorthbound {
                     .toString());
         }
 
-        StaticRoute sRoute = staticRouteData.getValue();
+        StaticRoute sRoute = staticRouteData;
         StaticRouteConfig cfgObject = new StaticRouteConfig(sRoute.getName(),
                 sRoute.getPrefix(), sRoute.getNextHop());
         Status response = staticRouting.addStaticRoute(cfgObject);
@@ -284,7 +326,7 @@ public class StaticRoutingNorthbound {
     @Path("/{containerName}/route/{route}")
     @DELETE
     @StatusCodes( {
-            @ResponseCode(code = 200, condition = "Operation successful"),
+            @ResponseCode(code = 204, condition = "Static route removed successfully"),
             @ResponseCode(code = 404, condition = "Container Name or Configuration Name not found"),
             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active") })
     public Response removeStaticRoute(
@@ -311,9 +353,9 @@ public class StaticRoutingNorthbound {
         Status status = staticRouting.removeStaticRoute(route);
         if (status.isSuccess()) {
             NorthboundUtils.auditlog("Static Route", username, "removed", route, containerName);
-            return Response.ok().build();
+            return Response.noContent().build();
         }
-        throw new ResourceNotFoundException(status.getDescription());
+        return NorthboundUtils.getResponse(status);
     }
 
     private void handleDefaultDisabled(String containerName) {