Merge "Bug-835:Reserve ports should be logical ports"
[controller.git] / opendaylight / northbound / hosttracker / src / main / java / org / opendaylight / controller / hosttracker / northbound / HostTrackerNorthbound.java
index 91185bb40573eea09b8e26c5aeb596e32eb23ab4..d7579c82e10bdd378dd603bfee9c4fde1d0ad414 100644 (file)
@@ -21,12 +21,13 @@ 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;
@@ -40,6 +41,7 @@ import org.opendaylight.controller.northbound.commons.exception.ResourceConflict
 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
 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.core.Node;
@@ -62,11 +64,7 @@ import org.opendaylight.controller.switchmanager.ISwitchManager;
  * Authentication realm : <b>opendaylight</b><br>
  * Transport : <b>HTTP and HTTPS</b><br>
  * <br>
- * HTTPS Authentication is disabled by default. Administrator can enable it in
- * tomcat-server.xml after adding a proper keystore / SSL certificate from a
- * trusted authority.<br>
- * More info :
- * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
+ * HTTPS Authentication is disabled by default.
  *
  */
 
@@ -74,6 +72,14 @@ import org.opendaylight.controller.switchmanager.ISwitchManager;
 public class HostTrackerNorthbound {
 
     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) {
@@ -112,7 +118,7 @@ public class HostTrackerNorthbound {
         return hostTracker;
     }
 
-    private Hosts convertHosts(Set<HostNodeConnector> hostNodeConnectors) {
+    private Set<HostConfig> convertHosts(Set<HostNodeConnector> hostNodeConnectors) {
         if(hostNodeConnectors == null) {
             return null;
         }
@@ -120,7 +126,7 @@ public class HostTrackerNorthbound {
         for(HostNodeConnector hnc : hostNodeConnectors) {
             hosts.add(HostConfig.convert(hnc));
         }
-        return new Hosts(hosts);
+        return hosts;
     }
 
     /**
@@ -135,11 +141,10 @@ public class HostTrackerNorthbound {
      *
      * Example:
      *
-     * RequestURL:
+     * Request URL:
+     * http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/active
      *
-     * http://localhost:8080/controller/nb/v2/host/default
-     *
-     * Response in XML
+     * Response body in XML
      *
      * &lt;list&gt;
      * &#x20;&lt;hostConfig&gt;
@@ -164,7 +169,7 @@ public class HostTrackerNorthbound {
      * &#x20;&lt;/hostConfig&gt;
      * &lt;/list&gt;
      *
-     * Response in JSON:
+     * Response body in JSON:
      *
      * {
      * &#x20;"hostConfig":[
@@ -192,7 +197,7 @@ public class HostTrackerNorthbound {
      * }
      * </pre>
      */
-    @Path("/{containerName}")
+    @Path("/{containerName}/hosts/active")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(Hosts.class)
@@ -200,14 +205,20 @@ public class HostTrackerNorthbound {
             @ResponseCode(code = 200, condition = "Operation successful"),
             @ResponseCode(code = 404, condition = "The containerName is not found"),
             @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public Hosts getActiveHosts(@PathParam("containerName") String containerName) {
+    public Hosts getActiveHosts(@PathParam("containerName") String containerName,
+        @QueryParam("_q") String queryString) {
 
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
             throw new UnauthorizedException("User is not authorized to perform this operation on container "
                     + containerName);
         }
         IfIptoHost hostTracker = getIfIpToHostService(containerName);
-        return convertHosts(hostTracker.getAllHosts());
+        Hosts hosts = new Hosts(convertHosts(hostTracker.getAllHosts()));
+        if (queryString != null) {
+            queryContext.createQuery(queryString, Hosts.class)
+                .filter(hosts, HostConfig.class);
+        }
+        return hosts;
     }
 
     /**
@@ -222,11 +233,10 @@ public class HostTrackerNorthbound {
      *
      * Example:
      *
-     * RequestURL:
+     * Request URL:
+     * http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/inactive
      *
-     * http://localhost:8080/controller/nb/v2/host/default/inactive
-     *
-     * Response in XML
+     * Response body in XML
      *
      * &lt;list&gt;
      * &#x20;&lt;hostConfig&gt;
@@ -251,7 +261,7 @@ public class HostTrackerNorthbound {
      * &#x20;&lt;/hostConfig&gt;
      * &lt;/list&gt;
      *
-     * Response in JSON:
+     * Response body in JSON:
      *
      * {
      * &#x20;"hostConfig":[
@@ -279,7 +289,7 @@ public class HostTrackerNorthbound {
      * }
      * </pre>
      */
-    @Path("/{containerName}/inactive")
+    @Path("/{containerName}/hosts/inactive")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(Hosts.class)
@@ -288,13 +298,19 @@ public class HostTrackerNorthbound {
             @ResponseCode(code = 404, condition = "The containerName is not found"),
             @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
     public Hosts getInactiveHosts(
-            @PathParam("containerName") String containerName) {
+            @PathParam("containerName") String containerName,
+            @QueryParam("_q") String queryString) {
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
             throw new UnauthorizedException("User is not authorized to perform this operation on container "
                     + containerName);
         }
         IfIptoHost hostTracker = getIfIpToHostService(containerName);
-        return convertHosts(hostTracker.getInactiveStaticHosts());
+        Hosts hosts = new Hosts(convertHosts(hostTracker.getInactiveStaticHosts()));
+        if (queryString != null) {
+            queryContext.createQuery(queryString, Hosts.class)
+                .filter(hosts, HostConfig.class);
+        }
+        return hosts;
     }
 
     /**
@@ -310,11 +326,10 @@ public class HostTrackerNorthbound {
      *
      * Example:
      *
-     * RequestURL:
-     *
-     * http://localhost:8080/controller/nb/v2/host/default/1.1.1.1
+     * Request URL:
+     * http://localhost:8080/controller/nb/v2/hosttracker/default/address/1.1.1.1
      *
-     * Response in XML
+     * Response body in XML
      *
      * &lt;hostConfig&gt;
      * &#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
@@ -327,7 +342,7 @@ public class HostTrackerNorthbound {
      * &#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
      * &lt;/hostConfig&gt;
      *
-     * Response in JSON:
+     * Response body in JSON:
      *
      * {
      * &#x20;"dataLayerAddress":"00:00:00:00:01:01",
@@ -341,7 +356,7 @@ public class HostTrackerNorthbound {
      * }
      * </pre>
      */
-    @Path("/{containerName}/{networkAddress}")
+    @Path("/{containerName}/address/{networkAddress}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(HostConfig.class)
@@ -374,7 +389,8 @@ public class HostTrackerNorthbound {
     }
 
     /**
-     * Add a Static Host configuration
+     * Add a Static Host configuration. If a host by the given address already
+     * exists, this method will respond with a non-successful status response.
      *
      * @param containerName
      *            Name of the Container. The Container name for the base
@@ -389,11 +405,10 @@ public class HostTrackerNorthbound {
      *
      * Example:
      *
-     * RequestURL:
+     * Request URL:
+     * http://localhost:8080/controller/nb/v2/hosttracker/default/address/1.1.1.1
      *
-     * http://localhost:8080/controller/nb/v2/host/default/1.1.1.1
-     *
-     * Request in XML
+     * Request body in XML
      *
      * &lt;hostConfig&gt;
      * &#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
@@ -402,11 +417,11 @@ public class HostTrackerNorthbound {
      * &#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
      * &#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
      * &#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
-     * &#x20;&lt;vlan&gt;0&lt;/vlan&gt;
-     * &#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
+     * &#x20;&lt;vlan&gt;1&lt;/vlan&gt;
+     * &#x20;&lt;staticHost&gt;true&lt;/staticHost&gt;
      * &lt;/hostConfig&gt;
      *
-     * Request in JSON:
+     * Request body in JSON:
      *
      * {
      * &#x20;"dataLayerAddress":"00:00:00:00:01:01",
@@ -414,14 +429,14 @@ public class HostTrackerNorthbound {
      * &#x20;"nodeId":"00:00:00:00:00:00:00:01",
      * &#x20;"nodeConnectorType":"OF",
      * &#x20;"nodeConnectorId":"9",
-     * &#x20;"vlan":"0",
-     * &#x20;"staticHost":"false",
+     * &#x20;"vlan":"1",
+     * &#x20;"staticHost":"true",
      * &#x20;"networkAddress":"1.1.1.1"
      * }
      * </pre>
      */
 
-    @Path("/{containerName}/{networkAddress}")
+    @Path("/{containerName}/address/{networkAddress}")
     @PUT
     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @StatusCodes({
@@ -432,7 +447,7 @@ public class HostTrackerNorthbound {
             @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
     public Response addHost(@Context UriInfo uriInfo, @PathParam("containerName") String containerName,
             @PathParam("networkAddress") String networkAddress,
-            @TypeHint(HostConfig.class) JAXBElement<HostConfig> hostConfig) {
+            @TypeHint(HostConfig.class) HostConfig hostConfig) {
 
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
             return Response.status(Response.Status.UNAUTHORIZED)
@@ -443,7 +458,7 @@ public class HostTrackerNorthbound {
 
         IfIptoHost hostTracker = getIfIpToHostService(containerName);
 
-        HostConfig hc = hostConfig.getValue();
+        HostConfig hc = hostConfig;
         if (!networkAddress.equals(hc.getNetworkAddress())) {
             return Response.status(Response.Status.CONFLICT)
                     .entity("Resource name in config object doesn't match URI")
@@ -475,9 +490,15 @@ public class HostTrackerNorthbound {
      * @param networkAddress
      *            IP Address
      * @return Response as dictated by the HTTP Response code.
+     *
+     * Example:
+     *
+     * Request URL:
+     * http://localhost:8080/controller/nb/v2/hosttracker/default/address/1.1.1.1
+     *
      */
 
-    @Path("/{containerName}/{networkAddress}")
+    @Path("/{containerName}/address/{networkAddress}")
     @DELETE
     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @StatusCodes({