HostTracker REST APIs compliance of new guidelines
[controller.git] / opendaylight / northbound / hosttracker / src / main / java / org / opendaylight / controller / hosttracker / northbound / HostTrackerNorthbound.java
index 285967522caa4d99c85515f4a491eafec66f23b8..2530d78416bf691dc2ea34bded2c8014fc574905 100644 (file)
@@ -10,21 +10,22 @@ package org.opendaylight.controller.hosttracker.northbound;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 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;
 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.xml.bind.JAXBElement;
 
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
@@ -116,6 +117,17 @@ public class HostTrackerNorthbound {
         return hostTracker;
     }
 
+    private Hosts convertHosts(Set<HostNodeConnector> hostNodeConnectors) {
+        if(hostNodeConnectors == null) {
+            return null;
+        }
+        Set<HostConfig> hosts = new HashSet<HostConfig>();
+        for(HostNodeConnector hnc : hostNodeConnectors) {
+            hosts.add(HostConfig.convert(hnc));
+        }
+        return new Hosts(hosts);
+    }
+
     /**
      * Returns a list of all Hosts : both configured via PUT API and dynamically
      * learnt on the network.
@@ -124,6 +136,66 @@ public class HostTrackerNorthbound {
      *            Name of the Container. The Container name for the base
      *            controller is "default".
      * @return List of Active Hosts.
+     * <pre>
+     *
+     * Example:
+     *
+     * RequestURL:
+     *
+     * http://localhost:8080/controller/nb/v2/host/default
+     *
+     * Response in XML
+     *
+     * &lt;list&gt;
+     * &#x20;&lt;hostConfig&gt;
+     * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
+     * &#x20;&#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
+     * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
+     * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
+     * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
+     * &#x20;&#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
+     * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
+     * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
+     * &#x20;&lt;/hostConfig&gt;
+     * &#x20;&lt;hostConfig&gt;
+     * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:02:02&lt;/dataLayerAddress&gt;
+     * &#x20;&#x20;&lt;networkAddress&gt;2.2.2.2&lt;/networkAddress&gt;
+     * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
+     * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:02&lt;/nodeId&gt;
+     * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
+     * &#x20;&#x20;&lt;nodeConnectorId&gt;5&lt;/nodeConnectorId&gt;
+     * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
+     * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
+     * &#x20;&lt;/hostConfig&gt;
+     * &lt;/list&gt;
+     *
+     * Response in JSON:
+     *
+     * {
+     * &#x20;"hostConfig":[
+     * &#x20;&#x20;{
+     * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:01:01",
+     * &#x20;&#x20;&#x20;"nodeType":"OF",
+     * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:01",
+     * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
+     * &#x20;&#x20;&#x20;"nodeConnectorId":"9",
+     * &#x20;&#x20;&#x20;"vlan":"0",
+     * &#x20;&#x20;&#x20;"staticHost":"false",
+     * &#x20;&#x20;&#x20;"networkAddress":"1.1.1.1"
+     * &#x20;&#x20;},
+     * &#x20;&#x20;{
+     * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:02:02",
+     * &#x20;&#x20;&#x20;"nodeType":"OF",
+     * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:02",
+     * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
+     * &#x20;&#x20;&#x20;"nodeConnectorId":"5",
+     * &#x20;&#x20;&#x20;"vlan":"0",
+     * &#x20;&#x20;&#x20;"staticHost":"false",
+     * &#x20;&#x20;&#x20;"networkAddress":"2.2.2.2"
+     * &#x20;&#x20;}
+     * &#x20;]
+     * }
+     * </pre>
      */
     @Path("/{containerName}")
     @GET
@@ -146,8 +218,7 @@ public class HostTrackerNorthbound {
             throw new ServiceUnavailableException("Host Tracker "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
         }
-
-        return new Hosts(hostTracker.getAllHosts());
+        return convertHosts(hostTracker.getAllHosts());
     }
 
     /**
@@ -158,6 +229,66 @@ public class HostTrackerNorthbound {
      *            Name of the Container. The Container name for the base
      *            controller is "default".
      * @return List of inactive Hosts.
+     * <pre>
+     *
+     * Example:
+     *
+     * RequestURL:
+     *
+     * http://localhost:8080/controller/nb/v2/host/default/inactive
+     *
+     * Response in XML
+     *
+     * &lt;list&gt;
+     * &#x20;&lt;hostConfig&gt;
+     * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
+     * &#x20;&#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
+     * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
+     * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
+     * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
+     * &#x20;&#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
+     * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
+     * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
+     * &#x20;&lt;/hostConfig&gt;
+     * &#x20;&lt;hostConfig&gt;
+     * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:02:02&lt;/dataLayerAddress&gt;
+     * &#x20;&#x20;&lt;networkAddress&gt;2.2.2.2&lt;/networkAddress&gt;
+     * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
+     * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:02&lt;/nodeId&gt;
+     * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
+     * &#x20;&#x20;&lt;nodeConnectorId&gt;5&lt;/nodeConnectorId&gt;
+     * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
+     * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
+     * &#x20;&lt;/hostConfig&gt;
+     * &lt;/list&gt;
+     *
+     * Response in JSON:
+     *
+     * {
+     * &#x20;"hostConfig":[
+     * &#x20;&#x20;{
+     * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:01:01",
+     * &#x20;&#x20;&#x20;"nodeType":"OF",
+     * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:01",
+     * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
+     * &#x20;&#x20;&#x20;"nodeConnectorId":"9",
+     * &#x20;&#x20;&#x20;"vlan":"0",
+     * &#x20;&#x20;&#x20;"staticHost":"false",
+     * &#x20;&#x20;&#x20;"networkAddress":"1.1.1.1"
+     * &#x20;&#x20;},
+     * &#x20;&#x20;{
+     * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:02:02",
+     * &#x20;&#x20;&#x20;"nodeType":"OF",
+     * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:02",
+     * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
+     * &#x20;&#x20;&#x20;"nodeConnectorId":"5",
+     * &#x20;&#x20;&#x20;"vlan":"0",
+     * &#x20;&#x20;&#x20;"staticHost":"false",
+     * &#x20;&#x20;&#x20;"networkAddress":"2.2.2.2"
+     * &#x20;&#x20;}
+     * &#x20;]
+     * }
+     * </pre>
      */
     @Path("/{containerName}/inactive")
     @GET
@@ -180,8 +311,7 @@ public class HostTrackerNorthbound {
             throw new ServiceUnavailableException("Host Tracker "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
         }
-
-        return new Hosts(hostTracker.getInactiveStaticHosts());
+        return convertHosts(hostTracker.getInactiveStaticHosts());
     }
 
     /**
@@ -193,17 +323,51 @@ public class HostTrackerNorthbound {
      * @param networkAddress
      *            IP Address being looked up
      * @return host that matches the IP Address
+     * <pre>
+     *
+     * Example:
+     *
+     * RequestURL:
+     *
+     * http://localhost:8080/controller/nb/v2/host/default/1.1.1.1
+     *
+     * Response in XML
+     *
+     * &lt;hostConfig&gt;
+     * &#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
+     * &#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
+     * &#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
+     * &#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;
+     * &lt;/hostConfig&gt;
+     *
+     * Response in JSON:
+     *
+     * {
+     * &#x20;"dataLayerAddress":"00:00:00:00:01:01",
+     * &#x20;"nodeType":"OF",
+     * &#x20;"nodeId":"00:00:00:00:00:00:00:01",
+     * &#x20;"nodeConnectorType":"OF",
+     * &#x20;"nodeConnectorId":"9",
+     * &#x20;"vlan":"0",
+     * &#x20;"staticHost":"false",
+     * &#x20;"networkAddress":"1.1.1.1"
+     * }
+     * </pre>
      */
     @Path("/{containerName}/{networkAddress}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @TypeHint(HostNodeConnector.class)
+    @TypeHint(HostConfig.class)
     @StatusCodes({
             @ResponseCode(code = 200, condition = "Operation successful"),
             @ResponseCode(code = 404, condition = "The containerName is not found"),
             @ResponseCode(code = 415, condition = "Invalid IP Address passed in networkAddress parameter"),
             @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public HostNodeConnector getHostDetails(
+    public HostConfig getHostDetails(
             @PathParam("containerName") String containerName,
             @PathParam("networkAddress") String networkAddress) {
         if (!NorthboundUtils.isAuthorized(
@@ -227,7 +391,7 @@ public class HostTrackerNorthbound {
         }
         for (HostNodeConnector host : hostTracker.getAllHosts()) {
             if (host.getNetworkAddress().equals(ip)) {
-                return host;
+                return HostConfig.convert(host);
             }
         }
         throw new ResourceNotFoundException(RestMessages.NOHOST.toString());
@@ -241,23 +405,48 @@ public class HostTrackerNorthbound {
      *            controller is "default".
      * @param networkAddress
      *            Host IP Address
-     * @param dataLayerAddress
-     *            Host L2 data-layer address.
-     * @param nodeType
-     *            Node Type as specifid by Node class
-     * @param nodeId
-     *            Node Identifier as specifid by Node class
-     * @param nodeConnectorType
-     *            Port Type as specified by NodeConnector class
-     * @param nodeConnectorId
-     *            Port Identifier as specified by NodeConnector class
-     * @param vlan
-     *            Vlan number
+     * @param hostConfig
+     *            Host Config Details
      * @return Response as dictated by the HTTP Response Status code
+     *
+     * <pre>
+     *
+     * Example:
+     *
+     * RequestURL:
+     *
+     * http://localhost:8080/controller/nb/v2/host/default/1.1.1.1
+     *
+     * Request in XML
+     *
+     * &lt;hostConfig&gt;
+     * &#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
+     * &#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
+     * &#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
+     * &#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;
+     * &lt;/hostConfig&gt;
+     *
+     * Request in JSON:
+     *
+     * {
+     * &#x20;"dataLayerAddress":"00:00:00:00:01:01",
+     * &#x20;"nodeType":"OF",
+     * &#x20;"nodeId":"00:00:00:00:00:00:00:01",
+     * &#x20;"nodeConnectorType":"OF",
+     * &#x20;"nodeConnectorId":"9",
+     * &#x20;"vlan":"0",
+     * &#x20;"staticHost":"false",
+     * &#x20;"networkAddress":"1.1.1.1"
+     * }
+     * </pre>
      */
 
     @Path("/{containerName}/{networkAddress}")
-    @POST
+    @PUT
     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @StatusCodes({
             @ResponseCode(code = 201, condition = "Static host created successfully"),
@@ -268,12 +457,7 @@ public class HostTrackerNorthbound {
             @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
     public Response addHost(@PathParam("containerName") String containerName,
             @PathParam("networkAddress") String networkAddress,
-            @QueryParam("dataLayerAddress") String dataLayerAddress,
-            @QueryParam("nodeType") String nodeType,
-            @QueryParam("nodeId") String nodeId,
-            @QueryParam("nodeConnectorType") String nodeConnectorType,
-            @QueryParam("nodeConnectorId") String nodeConnectorId,
-            @DefaultValue("0") @QueryParam("vlan") String vlan) {
+            @TypeHint(HostConfig.class) JAXBElement<HostConfig> hostConfig) {
 
         if (!NorthboundUtils.isAuthorized(
                 getUserName(), containerName, Privilege.WRITE, this)) {
@@ -289,7 +473,8 @@ public class HostTrackerNorthbound {
                     + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = handleNodeAvailability(containerName, nodeType, nodeId);
+        HostConfig hc = hostConfig.getValue();
+        Node node = handleNodeAvailability(containerName, hc.getNodeType(), hc.getNodeId());
         if (node == null) {
             throw new InternalServerErrorException(
                     RestMessages.NONODE.toString());
@@ -301,14 +486,20 @@ public class HostTrackerNorthbound {
             throw new UnsupportedMediaTypeException(networkAddress + " "
                     + RestMessages.INVALIDADDRESS.toString());
         }
-        NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType,
-                nodeConnectorId, node);
+        if(!networkAddress.equals(hc.getNetworkAddress())) {
+            throw new UnsupportedMediaTypeException(networkAddress + " is not the same as "
+                    + hc.getNetworkAddress());
+        }
+        if(!hc.isStaticHost()) {
+            throw new UnsupportedMediaTypeException("StaticHost flag must be true");
+        }
+        NodeConnector nc = NodeConnector.fromStringNoNode(hc.getNodeConnectorType(), hc.getNodeConnectorId(), node);
         if (nc == null) {
-            throw new ResourceNotFoundException(nodeConnectorType + "|"
-                    + nodeConnectorId + " : " + RestMessages.NONODE.toString());
+            throw new ResourceNotFoundException(hc.getNodeConnectorType() + "|"
+                    + hc.getNodeConnectorId() + " : " + RestMessages.NONODE.toString());
         }
         Status status = hostTracker.addStaticHost(networkAddress,
-                dataLayerAddress, nc, vlan);
+                hc.getDataLayerAddress(), nc, hc.getVlan());
         if (status.isSuccess()) {
             NorthboundUtils.auditlog("Static Host", username, "added", networkAddress, containerName);
             return Response.status(Response.Status.CREATED).build();