- Fix NumberFormatException when adding static host
through REST API with no vlan configured.
- Add IT to verify this
- Clean up Host Tracker REST API
Change-Id: Iefc672a0a978640ca47903f20962f150298ba11c
Signed-off-by: Yevgeny Khodorkovsky <ykhodork@cisco.com>
* @param nc
* NodeConnector to which the host is attached
* @param vlan
* @param nc
* NodeConnector to which the host is attached
* @param vlan
- * VLAN the host belongs to
+ * VLAN the host belongs to (null or empty for no vlan)
* @return The status object as described in {@code Status} indicating the
* result of this action.
*/
* @return The status object as described in {@code Status} indicating the
* result of this action.
*/
@Override
public Future<HostNodeConnector> discoverHost(InetAddress networkAddress) {
if (executor == null) {
@Override
public Future<HostNodeConnector> discoverHost(InetAddress networkAddress) {
if (executor == null) {
- logger.error("discoverHost: Null executor");
+ logger.debug("discoverHost: Null executor");
return null;
}
Callable<HostNodeConnector> worker = new HostTrackerCallable(this, networkAddress);
return null;
}
Callable<HostNodeConnector> worker = new HostTrackerCallable(this, networkAddress);
@Override
public Set<HostNodeConnector> getAllHosts() {
@Override
public Set<HostNodeConnector> getAllHosts() {
- Set<HostNodeConnector> allHosts = new HashSet<HostNodeConnector>();
- for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
- HostNodeConnector host = entry.getValue();
- allHosts.add(host);
- }
- logger.debug("Exiting getAllHosts, Found {} Hosts", allHosts.size());
+ Set<HostNodeConnector> allHosts = new HashSet<HostNodeConnector>(hostsDB.values());
- logger.debug("getActiveStaticHosts(): Found {} Hosts", list.size());
return list;
}
@Override
public Set<HostNodeConnector> getInactiveStaticHosts() {
return list;
}
@Override
public Set<HostNodeConnector> getInactiveStaticHosts() {
- Set<HostNodeConnector> list = new HashSet<HostNodeConnector>();
- for (Entry<NodeConnector, HostNodeConnector> entry : inactiveStaticHosts.entrySet()) {
- list.add(entry.getValue());
- }
- logger.debug("getInactiveStaticHosts(): Found {} Hosts", list.size());
+ Set<HostNodeConnector> list = new HashSet<HostNodeConnector>(inactiveStaticHosts.values());
ta.notifyHTClientHostRemoved(host);
}
} catch (Exception e) {
ta.notifyHTClientHostRemoved(host);
}
} catch (Exception e) {
- logger.error("Exception on callback", e);
+ logger.error("Exception on new host notification", e);
* probe. However, continue the age out the hosts since
* we don't know if the host is indeed out there or not.
*/
* probe. However, continue the age out the hosts since
* we don't know if the host is indeed out there or not.
*/
- logger.warn("ARPHandler is not avaialable, can't send the probe");
+ logger.trace("ARPHandler is not avaialable, can't send the probe");
continue;
}
hostFinder.probe(host);
continue;
}
hostFinder.probe(host);
* indicating the result of this action.
*/
* indicating the result of this action.
*/
- public Status addStaticHostReq(InetAddress networkAddr, byte[] dataLayerAddress, NodeConnector nc, short vlan) {
+ protected Status addStaticHostReq(InetAddress networkAddr, byte[] dataLayerAddress, NodeConnector nc, short vlan) {
if (dataLayerAddress.length != NetUtils.MACAddrLengthInBytes) {
return new Status(StatusCode.BADREQUEST, "Invalid MAC address");
}
if (dataLayerAddress.length != NetUtils.MACAddrLengthInBytes) {
return new Status(StatusCode.BADREQUEST, "Invalid MAC address");
}
// northbound request
HostNodeConnector transHost = hostsDB.get(networkAddr);
transHost.setStaticHost(true);
// northbound request
HostNodeConnector transHost = hostsDB.get(networkAddr);
transHost.setStaticHost(true);
- return new Status(StatusCode.SUCCESS, null);
+ return new Status(StatusCode.SUCCESS);
}
if (hostsDB.get(networkAddr) != null) {
// There is already a host with this IP address (but behind
// a different (switch, port, vlan) tuple. Return an error
}
if (hostsDB.get(networkAddr) != null) {
// There is already a host with this IP address (but behind
// a different (switch, port, vlan) tuple. Return an error
- return new Status(StatusCode.CONFLICT, "Existing IP, Use PUT to update");
+ return new Status(StatusCode.CONFLICT, "Host with this IP already exists.");
}
host.setStaticHost(true);
/*
}
host.setStaticHost(true);
/*
logger.debug("Switch or switchport is not up, adding host {} to inactive list",
networkAddr.getHostName());
}
logger.debug("Switch or switchport is not up, adding host {} to inactive list",
networkAddr.getHostName());
}
- return new Status(StatusCode.SUCCESS, null);
+ return new Status(StatusCode.SUCCESS);
} catch (ConstructionException e) {
logger.error("", e);
return new Status(StatusCode.INTERNALERROR, "Host could not be created");
} catch (ConstructionException e) {
logger.error("", e);
return new Status(StatusCode.INTERNALERROR, "Host could not be created");
public Status addStaticHost(String networkAddress, String dataLayerAddress, NodeConnector nc, String vlan) {
try {
InetAddress ip = InetAddress.getByName(networkAddress);
public Status addStaticHost(String networkAddress, String dataLayerAddress, NodeConnector nc, String vlan) {
try {
InetAddress ip = InetAddress.getByName(networkAddress);
- if (nc == null) {
- return new Status(StatusCode.BADREQUEST, "Invalid NodeId");
+ short vl = 0;
+ if (vlan != null && !vlan.isEmpty()) {
+ vl = Short.decode(vlan);
+ if (vl < 1 || vl > 4095) {
+ return new Status(StatusCode.BADREQUEST, "Host vlan out of range [1 - 4095]");
+ }
- return addStaticHostReq(ip, HexEncode.bytesFromHexString(dataLayerAddress), nc, Short.valueOf(vlan));
+
+ return addStaticHostReq(ip, HexEncode.bytesFromHexString(dataLayerAddress), nc, vl);
+
} catch (UnknownHostException e) {
} catch (UnknownHostException e) {
- logger.error("", e);
- return new Status(StatusCode.BADREQUEST, "Invalid Address");
+ logger.debug("Invalid host IP specified when adding static host", e);
+ return new Status(StatusCode.BADREQUEST, "Invalid Host IP Address");
+ } catch (NumberFormatException nfe) {
+ logger.debug("Invalid host vlan or MAC specified when adding static host", nfe);
+ return new Status(StatusCode.BADREQUEST, "Invalid Host vLan/MAC");
}
}
@Override
public Status removeStaticHost(String networkAddress) {
}
}
@Override
public Status removeStaticHost(String networkAddress) {
- address = InetAddress.getByName(networkAddress);
+ InetAddress address = InetAddress.getByName(networkAddress);
return removeStaticHostReq(address);
} catch (UnknownHostException e) {
return removeStaticHostReq(address);
} catch (UnknownHostException e) {
- logger.error("", e);
- return new Status(StatusCode.BADREQUEST, "Invalid Address");
+ logger.debug("Invalid IP Address when trying to remove host", e);
+ return new Status(StatusCode.BADREQUEST, "Invalid IP Address when trying to remove host");
ARPPending arphost;
HostNodeConnector host = null;
ARPPending arphost;
HostNodeConnector host = null;
- logger.debug("handleNodeConnectorStatusUp {}", nodeConnector);
+ logger.trace("handleNodeConnectorStatusUp {}", nodeConnector);
for (Entry<InetAddress, ARPPending> entry : failedARPReqList.entrySet()) {
arphost = entry.getValue();
for (Entry<InetAddress, ARPPending> entry : failedARPReqList.entrySet()) {
arphost = entry.getValue();
- logger.debug("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress());
+ logger.trace("Sending the ARP from FailedARPReqList fors IP: {}", arphost.getHostIP().getHostAddress());
if (hostFinder == null) {
logger.warn("ARPHandler is not available at interface up");
logger.warn("Since this event is missed, host(s) connected to interface {} may not be discovered",
if (hostFinder == null) {
logger.warn("ARPHandler is not available at interface up");
logger.warn("Since this event is missed, host(s) connected to interface {} may not be discovered",
arphost.getHostIP(), nodeConnector);
logger.error("", e);
}
arphost.getHostIP(), nodeConnector);
logger.error("", e);
}
- logger.debug("Done. handleNodeConnectorStatusUp {}", nodeConnector);
}
host = inactiveStaticHosts.get(nodeConnector);
}
host = inactiveStaticHosts.get(nodeConnector);
}
private void handleNodeConnectorStatusDown(NodeConnector nodeConnector) {
}
private void handleNodeConnectorStatusDown(NodeConnector nodeConnector) {
- logger.debug("handleNodeConnectorStatusDown {}", nodeConnector);
+ logger.trace("handleNodeConnectorStatusDown {}", nodeConnector);
for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
HostNodeConnector host = entry.getValue();
for (Entry<InetAddress, HostNodeConnector> entry : hostsDB.entrySet()) {
HostNodeConnector host = entry.getValue();
NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
// test addStaticHost(), store into inactive host DB
NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
// test addStaticHost(), store into inactive host DB
- Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, "0");
+ Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, null);
Assert.assertTrue(st.isSuccess());
Assert.assertTrue(st.isSuccess());
- st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "0");
+ st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "");
Assert.assertTrue(st.isSuccess());
// check inactive DB
Assert.assertTrue(st.isSuccess());
// check inactive DB
NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
// test addStaticHost(), put into inactive host DB if not verifiable
NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
// test addStaticHost(), put into inactive host DB if not verifiable
- Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, "0");
+ Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, null);
+ Assert.assertTrue(st.isSuccess());
st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "0");
st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "0");
+ Assert.assertFalse(st.isSuccess());
+
this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
// test addStaticHost(), put into inactive host DB if not verifiable
NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
// test addStaticHost(), put into inactive host DB if not verifiable
- Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, "0");
- st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "0");
+ Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, null);
+ st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "");
HostNodeConnector hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));
assertNull(hnc_1);
HostNodeConnector hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));
assertNull(hnc_1);
"Operation failed due to Resource Conflict"), NODEFAULT("Container default is not a custom container"), DEFAULTDISABLED(
"Container(s) are configured. Container default is not operational"), NOTALLOWEDONDEFAULT(
"Container default is a static resource, no modification allowed on it"), UNKNOWNACTION("Unknown action"), INVALIDJSON(
"Operation failed due to Resource Conflict"), NODEFAULT("Container default is not a custom container"), DEFAULTDISABLED(
"Container(s) are configured. Container default is not operational"), NOTALLOWEDONDEFAULT(
"Container default is a static resource, no modification allowed on it"), UNKNOWNACTION("Unknown action"), INVALIDJSON(
- "JSON message is invalid"), INVALIDADDRESS("invalid InetAddress"), AVAILABLESOON(
+ "JSON message is invalid"), INVALIDADDRESS("Invalid InetAddress"), AVAILABLESOON(
"Resource is not implemented yet"), INTERNALERROR("Internal Error"), SERVICEUNAVAILABLE(
"Service is not available. Could be down for maintanence"), INVALIDDATA(
"Data is invalid or conflicts with URI");
"Resource is not implemented yet"), INTERNALERROR("Internal Error"), SERVICEUNAVAILABLE(
"Service is not available. Could be down for maintanence"), INVALIDDATA(
"Data is invalid or conflicts with URI");
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
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 org.codehaus.enunciate.jaxrs.ResponseCode;
import javax.xml.bind.JAXBElement;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.opendaylight.controller.hosttracker.IfIptoHost;
import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
import org.opendaylight.controller.northbound.commons.RestMessages;
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.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;
import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
import org.opendaylight.controller.sal.authorization.Privilege;
import org.opendaylight.controller.sal.core.Node;
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.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;
/**
import org.opendaylight.controller.switchmanager.ISwitchManager;
/**
}
private IfIptoHost getIfIpToHostService(String containerName) {
}
private IfIptoHost getIfIpToHostService(String containerName) {
- IContainerManager containerManager = (IContainerManager) ServiceHelper
- .getGlobalInstance(IContainerManager.class, this);
+ IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
+ IContainerManager.class, this);
if (containerManager == null) {
if (containerManager == null) {
- throw new ServiceUnavailableException("Container "
- + RestMessages.SERVICEUNAVAILABLE.toString());
+ throw new ServiceUnavailableException("Container " + RestMessages.SERVICEUNAVAILABLE.toString());
for (String cName : containerNames) {
if (cName.trim().equalsIgnoreCase(containerName.trim())) {
found = true;
for (String cName : containerNames) {
if (cName.trim().equalsIgnoreCase(containerName.trim())) {
found = true;
- 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) {
if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
+ throw new ServiceUnavailableException("Host Tracker " + RestMessages.SERVICEUNAVAILABLE.toString());
@ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
public Hosts getActiveHosts(@PathParam("containerName") String containerName) {
@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);
}
IfIptoHost hostTracker = getIfIpToHostService(containerName);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
return convertHosts(hostTracker.getAllHosts());
}
return convertHosts(hostTracker.getAllHosts());
}
@ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
public Hosts getInactiveHosts(
@PathParam("containerName") String containerName) {
@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);
}
IfIptoHost hostTracker = getIfIpToHostService(containerName);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
return convertHosts(hostTracker.getInactiveStaticHosts());
}
return convertHosts(hostTracker.getInactiveStaticHosts());
}
@TypeHint(HostConfig.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "Operation successful"),
@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 = 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) {
@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);
}
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) {
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)) {
}
for (HostNodeConnector host : hostTracker.getAllHosts()) {
if (host.getNetworkAddress().equals(ip)) {
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@StatusCodes({
@ResponseCode(code = 201, condition = "Static host created successfully"),
@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") })
@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> hostConfig) {
@PathParam("networkAddress") String networkAddress,
@TypeHint(HostConfig.class) JAXBElement<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);
}
handleDefaultDisabled(containerName);
IfIptoHost hostTracker = getIfIpToHostService(containerName);
- if (hostTracker == null) {
- throw new ServiceUnavailableException("Host Tracker "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
HostConfig hc = hostConfig.getValue();
HostConfig hc = hostConfig.getValue();
- Node node = handleNodeAvailability(containerName, hc.getNodeType(), hc.getNodeId());
- if (node == null) {
- throw new InternalServerErrorException(
- RestMessages.NONODE.toString());
+ if (!networkAddress.equals(hc.getNetworkAddress())) {
+ return Response.status(Response.Status.CONFLICT)
+ .entity("Resource name in config object doesn't match URI")
+ .build();
-
- 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());
- }
- 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);
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);
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);
@DELETE
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@StatusCodes({
@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 = 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") })
@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) {
@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);
}
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);
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);
IContainerManager containerManager = (IContainerManager) ServiceHelper
.getGlobalInstance(IContainerManager.class, this);
if (containerManager == null) {
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()) {
}
if (containerName.equals(GlobalConstants.DEFAULT.toString())
&& containerManager.hasNonDefaultContainer()) {
- 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) {
Node node = Node.fromString(nodeType, nodeId);
if (node == null) {
Integer nodeId_1 = 3366;
String nodeConnectorType_1 = "STUB";
Integer nodeConnectorId_1 = 12;
Integer nodeId_1 = 3366;
String nodeConnectorType_1 = "STUB";
Integer nodeConnectorId_1 = 12;
// 2nd host
String networkAddress_2 = "10.1.1.1";
// 2nd host
String networkAddress_2 = "10.1.1.1";
Integer nodeId_2 = 4477;
String nodeConnectorType_2 = "STUB";
Integer nodeConnectorId_2 = 34;
Integer nodeId_2 = 4477;
String nodeConnectorType_2 = "STUB";
Integer nodeConnectorId_2 = 34;
String baseURL = "http://127.0.0.1:8080/controller/nb/v2/host/default";
String baseURL = "http://127.0.0.1:8080/controller/nb/v2/host/default";
Assert.assertTrue(host_jo.getInt("nodeConnectorId") == nodeConnectorId_1);
Assert.assertTrue(host_jo.getString("nodeType").equalsIgnoreCase(nodeType_1));
Assert.assertTrue(host_jo.getInt("nodeId") == nodeId_1);
Assert.assertTrue(host_jo.getInt("nodeConnectorId") == nodeConnectorId_1);
Assert.assertTrue(host_jo.getString("nodeType").equalsIgnoreCase(nodeType_1));
Assert.assertTrue(host_jo.getInt("nodeId") == nodeId_1);
- Assert.assertTrue(host_jo.getString("vlan").equalsIgnoreCase(vlan_1));
+ Assert.assertTrue(host_jo.getString("vlan").equals("0"));
Assert.assertTrue(host_jo.getBoolean("staticHost"));
} else if (networkAddress.equalsIgnoreCase(networkAddress_2)) {
Assert.assertTrue(host_jo.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_2));
Assert.assertTrue(host_jo.getBoolean("staticHost"));
} else if (networkAddress.equalsIgnoreCase(networkAddress_2)) {
Assert.assertTrue(host_jo.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_2));
Assert.assertTrue(json.getInt("nodeConnectorId") == nodeConnectorId_1);
Assert.assertTrue(json.getString("nodeType").equalsIgnoreCase(nodeType_1));
Assert.assertTrue(json.getInt("nodeId") == nodeId_1);
Assert.assertTrue(json.getInt("nodeConnectorId") == nodeConnectorId_1);
Assert.assertTrue(json.getString("nodeType").equalsIgnoreCase(nodeType_1));
Assert.assertTrue(json.getInt("nodeId") == nodeId_1);
- Assert.assertTrue(json.getString("vlan").equalsIgnoreCase(vlan_1));
+ Assert.assertTrue(json.getString("vlan").equals("0"));
Assert.assertTrue(json.getBoolean("staticHost"));
// test DELETE method for deleteFlow()
result = getJsonResult(baseURL + "/" + networkAddress_1, "DELETE");
Assert.assertTrue(json.getBoolean("staticHost"));
// test DELETE method for deleteFlow()
result = getJsonResult(baseURL + "/" + networkAddress_1, "DELETE");
- Assert.assertTrue(httpResponseCode == 200);
+ Assert.assertTrue(httpResponseCode == 204);
// verify host_1 removed from active host DB
// test GET method: getActiveHosts() - no host expected
// verify host_1 removed from active host DB
// test GET method: getActiveHosts() - no host expected