X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=opendaylight%2Fnorthbound%2Fhosttracker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fhosttracker%2Fnorthbound%2FHostTrackerNorthbound.java;h=74c13d11f129943ba45e449411adfbc9f09d41e3;hb=caee336f062eba4909ba53cbaccdde0714236134;hp=769461167c1036812ea33f17d0ffcf8f2b21a6c5;hpb=505e623d6a699dd292fdee5e7c100d528be3ae99;p=controller.git
diff --git a/opendaylight/northbound/hosttracker/src/main/java/org/opendaylight/controller/hosttracker/northbound/HostTrackerNorthbound.java b/opendaylight/northbound/hosttracker/src/main/java/org/opendaylight/controller/hosttracker/northbound/HostTrackerNorthbound.java
index 769461167c..74c13d11f1 100644
--- a/opendaylight/northbound/hosttracker/src/main/java/org/opendaylight/controller/hosttracker/northbound/HostTrackerNorthbound.java
+++ b/opendaylight/northbound/hosttracker/src/main/java/org/opendaylight/controller/hosttracker/northbound/HostTrackerNorthbound.java
@@ -25,7 +25,7 @@ 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 javax.ws.rs.core.UriInfo;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
@@ -34,12 +34,11 @@ import org.opendaylight.controller.containermanager.IContainerManager;
import org.opendaylight.controller.hosttracker.IfIptoHost;
import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
import org.opendaylight.controller.northbound.commons.RestMessages;
-import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
+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.ServiceUnavailableException;
import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
-import org.opendaylight.controller.northbound.commons.exception.UnsupportedMediaTypeException;
import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
import org.opendaylight.controller.sal.authorization.Privilege;
import org.opendaylight.controller.sal.core.Node;
@@ -47,7 +46,6 @@ import org.opendaylight.controller.sal.core.NodeConnector;
import org.opendaylight.controller.sal.utils.GlobalConstants;
import org.opendaylight.controller.sal.utils.ServiceHelper;
import org.opendaylight.controller.sal.utils.Status;
-import org.opendaylight.controller.sal.utils.StatusCode;
import org.opendaylight.controller.switchmanager.ISwitchManager;
/**
@@ -63,11 +61,7 @@ import org.opendaylight.controller.switchmanager.ISwitchManager;
* Authentication realm : opendaylight
* Transport : HTTP and HTTPS
*
- * 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.
- * More info :
- * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
+ * HTTPS Authentication is disabled by default.
*
*/
@@ -86,11 +80,10 @@ public class HostTrackerNorthbound {
}
private IfIptoHost getIfIpToHostService(String containerName) {
- IContainerManager containerManager = (IContainerManager) ServiceHelper
- .getGlobalInstance(IContainerManager.class, this);
+ IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
+ IContainerManager.class, this);
if (containerManager == null) {
- throw new ServiceUnavailableException("Container "
- + RestMessages.SERVICEUNAVAILABLE.toString());
+ throw new ServiceUnavailableException("Container " + RestMessages.SERVICEUNAVAILABLE.toString());
}
boolean found = false;
@@ -98,20 +91,17 @@ public class HostTrackerNorthbound {
for (String cName : containerNames) {
if (cName.trim().equalsIgnoreCase(containerName.trim())) {
found = true;
+ break;
}
}
- if (found == false) {
- throw new ResourceNotFoundException(containerName + " "
- + RestMessages.NOCONTAINER.toString());
+ if (!found) {
+ throw new ResourceNotFoundException(containerName + " " + RestMessages.NOCONTAINER.toString());
}
- IfIptoHost hostTracker = (IfIptoHost) ServiceHelper.getInstance(
- IfIptoHost.class, containerName, this);
-
+ IfIptoHost hostTracker = (IfIptoHost) ServiceHelper.getInstance(IfIptoHost.class, containerName, this);
if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
+ throw new ServiceUnavailableException("Host Tracker " + RestMessages.SERVICEUNAVAILABLE.toString());
}
return hostTracker;
@@ -140,11 +130,10 @@ public class HostTrackerNorthbound {
*
* Example:
*
- * RequestURL:
- *
- * http://localhost:8080/controller/nb/v2/host/default
+ * Request URL:
+ * http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/active
*
- * Response in XML
+ * Response body in XML
*
* <list>
* <hostConfig>
@@ -169,7 +158,7 @@ public class HostTrackerNorthbound {
* </hostConfig>
* </list>
*
- * Response in JSON:
+ * Response body in JSON:
*
* {
* "hostConfig":[
@@ -197,7 +186,7 @@ public class HostTrackerNorthbound {
* }
*
*/
- @Path("/{containerName}")
+ @Path("/{containerName}/hosts/active")
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@TypeHint(Hosts.class)
@@ -207,17 +196,11 @@ public class HostTrackerNorthbound {
@ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
public Hosts getActiveHosts(@PathParam("containerName") String containerName) {
- if (!NorthboundUtils.isAuthorized(
- getUserName(), containerName, Privilege.READ, this)) {
- throw new UnauthorizedException(
- "User is not authorized to perform this operation on container "
- + containerName);
+ 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);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
return convertHosts(hostTracker.getAllHosts());
}
@@ -233,11 +216,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
*
* <list>
* <hostConfig>
@@ -262,7 +244,7 @@ public class HostTrackerNorthbound {
* </hostConfig>
* </list>
*
- * Response in JSON:
+ * Response body in JSON:
*
* {
* "hostConfig":[
@@ -290,7 +272,7 @@ public class HostTrackerNorthbound {
* }
*
*/
- @Path("/{containerName}/inactive")
+ @Path("/{containerName}/hosts/inactive")
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@TypeHint(Hosts.class)
@@ -300,17 +282,11 @@ public class HostTrackerNorthbound {
@ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
public Hosts getInactiveHosts(
@PathParam("containerName") String containerName) {
- if (!NorthboundUtils.isAuthorized(
- getUserName(), containerName, Privilege.READ, this)) {
- throw new UnauthorizedException(
- "User is not authorized to perform this operation on container "
- + containerName);
+ 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);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
return convertHosts(hostTracker.getInactiveStaticHosts());
}
@@ -327,11 +303,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
*
* <hostConfig>
* <dataLayerAddress>00:00:00:00:01:01</dataLayerAddress>
@@ -344,7 +319,7 @@ public class HostTrackerNorthbound {
* <staticHost>false</staticHost>
* </hostConfig>
*
- * Response in JSON:
+ * Response body in JSON:
*
* {
* "dataLayerAddress":"00:00:00:00:01:01",
@@ -358,36 +333,29 @@ public class HostTrackerNorthbound {
* }
*
*/
- @Path("/{containerName}/{networkAddress}")
+ @Path("/{containerName}/address/{networkAddress}")
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@TypeHint(HostConfig.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 400, condition = "Invalid IP specified in networkAddress parameter"),
@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 HostConfig getHostDetails(
@PathParam("containerName") String containerName,
@PathParam("networkAddress") String networkAddress) {
- if (!NorthboundUtils.isAuthorized(
- getUserName(), containerName, Privilege.READ, this)) {
- throw new UnauthorizedException(
- "User is not authorized to perform this operation on container "
- + containerName);
+ 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);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
InetAddress ip;
try {
ip = InetAddress.getByName(networkAddress);
} catch (UnknownHostException e) {
- throw new UnsupportedMediaTypeException(networkAddress + " "
- + RestMessages.INVALIDADDRESS.toString());
+ throw new BadRequestException(RestMessages.INVALIDADDRESS.toString() + " " + networkAddress);
}
for (HostNodeConnector host : hostTracker.getAllHosts()) {
if (host.getNetworkAddress().equals(ip)) {
@@ -398,7 +366,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
@@ -413,11 +382,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
*
- * Request in XML
+ * Request body in XML
*
* <hostConfig>
* <dataLayerAddress>00:00:00:00:01:01</dataLayerAddress>
@@ -426,11 +394,11 @@ public class HostTrackerNorthbound {
* <nodeId>00:00:00:00:00:00:00:01</nodeId>
* <nodeConnectorType>OF</nodeConnectorType>
* <nodeConnectorId>9</nodeConnectorId>
- * <vlan>0</vlan>
- * <staticHost>false</staticHost>
+ * <vlan>1</vlan>
+ * <staticHost>true</staticHost>
* </hostConfig>
*
- * Request in JSON:
+ * Request body in JSON:
*
* {
* "dataLayerAddress":"00:00:00:00:01:01",
@@ -438,75 +406,56 @@ public class HostTrackerNorthbound {
* "nodeId":"00:00:00:00:00:00:00:01",
* "nodeConnectorType":"OF",
* "nodeConnectorId":"9",
- * "vlan":"0",
- * "staticHost":"false",
+ * "vlan":"1",
+ * "staticHost":"true",
* "networkAddress":"1.1.1.1"
* }
*
*/
- @Path("/{containerName}/{networkAddress}")
+ @Path("/{containerName}/address/{networkAddress}")
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@StatusCodes({
@ResponseCode(code = 201, condition = "Static host created successfully"),
- @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
- @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
- @ResponseCode(code = 415, condition = "Invalid IP Address passed in networkAddress parameter"),
- @ResponseCode(code = 500, condition = "Failed to create Static Host entry. Failure Reason included in HTTP Error response"),
+ @ResponseCode(code = 400, condition = "Invalid parameters specified, see response body for details"),
+ @ResponseCode(code = 404, condition = "The container or resource is not found"),
+ @ResponseCode(code = 409, condition = "Resource conflict, see response body for details"),
@ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
- public Response addHost(@PathParam("containerName") String containerName,
+ public Response addHost(@Context UriInfo uriInfo, @PathParam("containerName") String containerName,
@PathParam("networkAddress") String networkAddress,
- @TypeHint(HostConfig.class) JAXBElement hostConfig) {
+ @TypeHint(HostConfig.class) HostConfig hostConfig) {
- if (!NorthboundUtils.isAuthorized(
- getUserName(), containerName, Privilege.WRITE, this)) {
- throw new UnauthorizedException(
- "User is not authorized to perform this operation on container "
- + containerName);
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ return Response.status(Response.Status.UNAUTHORIZED)
+ .entity("User is not authorized to perform this operation on container " + containerName)
+ .build();
}
handleDefaultDisabled(containerName);
IfIptoHost hostTracker = getIfIpToHostService(containerName);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- HostConfig hc = hostConfig.getValue();
- Node node = handleNodeAvailability(containerName, hc.getNodeType(), hc.getNodeId());
- if (node == null) {
- throw new InternalServerErrorException(
- RestMessages.NONODE.toString());
- }
- try {
- InetAddress.getByName(networkAddress);
- } catch (UnknownHostException e) {
- throw new UnsupportedMediaTypeException(networkAddress + " "
- + RestMessages.INVALIDADDRESS.toString());
- }
- if(!networkAddress.equals(hc.getNetworkAddress())) {
- throw new UnsupportedMediaTypeException(networkAddress + " is not the same as "
- + hc.getNetworkAddress());
+ HostConfig hc = hostConfig;
+ if (!networkAddress.equals(hc.getNetworkAddress())) {
+ return Response.status(Response.Status.CONFLICT)
+ .entity("Resource name in config object doesn't match URI")
+ .build();
}
- if(!hc.isStaticHost()) {
- throw new UnsupportedMediaTypeException("StaticHost flag must be true");
+ if (!hc.isStaticHost()) {
+ return Response.status(Response.Status.BAD_REQUEST)
+ .entity("Can only add static host.")
+ .build();
}
+ Node node = handleNodeAvailability(containerName, hc.getNodeType(), hc.getNodeId());
NodeConnector nc = NodeConnector.fromStringNoNode(hc.getNodeConnectorType(), hc.getNodeConnectorId(), node);
- if (nc == null) {
- throw new ResourceNotFoundException(hc.getNodeConnectorType() + "|"
- + hc.getNodeConnectorId() + " : " + RestMessages.NONODE.toString());
- }
- Status status = hostTracker.addStaticHost(networkAddress,
- hc.getDataLayerAddress(), nc, hc.getVlan());
+
+ Status status = hostTracker.addStaticHost(networkAddress, hc.getDataLayerAddress(), nc, hc.getVlan());
if (status.isSuccess()) {
NorthboundUtils.auditlog("Static Host", username, "added", networkAddress, containerName);
- return Response.status(Response.Status.CREATED).build();
- } else if (status.getCode().equals(StatusCode.BADREQUEST)) {
- throw new UnsupportedMediaTypeException(status.getDescription());
+ return Response.created(uriInfo.getRequestUri()).build();
}
- throw new InternalServerErrorException(status.getDescription());
+
+ return NorthboundUtils.getResponse(status);
}
/**
@@ -518,48 +467,40 @@ 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({
- @ResponseCode(code = 200, condition = "Flow Config deleted successfully"),
- @ResponseCode(code = 404, condition = "The Container Name or Node-id or Flow Name passed is not found"),
+ @ResponseCode(code = 204, condition = "Static host deleted successfully"),
+ @ResponseCode(code = 404, condition = "The container or a specified resource was not found"),
@ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
- @ResponseCode(code = 415, condition = "Invalid IP Address passed in networkAddress parameter"),
- @ResponseCode(code = 500, condition = "Failed to delete Flow config. Failure Reason included in HTTP Error response"),
@ResponseCode(code = 503, condition = "One or more of Controller service is unavailable") })
- public Response deleteFlow(
+ public Response deleteHost(
@PathParam(value = "containerName") String containerName,
@PathParam(value = "networkAddress") String networkAddress) {
- if (!NorthboundUtils.isAuthorized(
- getUserName(), containerName, Privilege.WRITE, this)) {
- throw new UnauthorizedException(
- "User is not authorized to perform this operation on container "
- + containerName);
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ return Response.status(Response.Status.UNAUTHORIZED)
+ .entity("User is not authorized to perform this operation on container " + containerName)
+ .build();
}
handleDefaultDisabled(containerName);
IfIptoHost hostTracker = getIfIpToHostService(containerName);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- try {
- InetAddress.getByName(networkAddress);
- } catch (UnknownHostException e) {
- throw new UnsupportedMediaTypeException(networkAddress + " "
- + RestMessages.INVALIDADDRESS.toString());
- }
Status status = hostTracker.removeStaticHost(networkAddress);
if (status.isSuccess()) {
NorthboundUtils.auditlog("Static Host", username, "removed", networkAddress, containerName);
- return Response.ok().build();
+ return Response.noContent().build();
}
- throw new InternalServerErrorException(status.getDescription());
+ return NorthboundUtils.getResponse(status);
}
@@ -567,8 +508,8 @@ public class HostTrackerNorthbound {
IContainerManager containerManager = (IContainerManager) ServiceHelper
.getGlobalInstance(IContainerManager.class, this);
if (containerManager == null) {
- throw new InternalServerErrorException(
- RestMessages.INTERNALERROR.toString());
+ throw new ServiceUnavailableException(
+ RestMessages.SERVICEUNAVAILABLE.toString());
}
if (containerName.equals(GlobalConstants.DEFAULT.toString())
&& containerManager.hasNonDefaultContainer()) {
@@ -577,8 +518,7 @@ public class HostTrackerNorthbound {
}
}
- private Node handleNodeAvailability(String containerName, String nodeType,
- String nodeId) {
+ private Node handleNodeAvailability(String containerName, String nodeType, String nodeId) {
Node node = Node.fromString(nodeType, nodeId);
if (node == null) {