This api will register hostPorvider, if something wrong like no ipAddress or location specified, it will cause HostProviderRegistry say hostprovider already registered
Change-Id: I43c0551e140ebc4de45759b28e42de7eb71824c6
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java
index 3e5dc21..04886e4 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java
@@ -138,12 +138,12 @@
@Produces(MediaType.APPLICATION_JSON)
public Response createAndAddHost(InputStream stream) {
URI location;
+ HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
+ InternalHostProvider hostProvider = new InternalHostProvider();
try {
// Parse the input stream
ObjectNode root = readTreeFromStream(mapper(), stream);
- HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
- InternalHostProvider hostProvider = new InternalHostProvider();
HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
hostProvider.setHostProviderService(hostProviderService);
HostId hostId = hostProvider.parseHost(root);
@@ -153,11 +153,12 @@
.path(hostId.mac().toString())
.path(hostId.vlanId().toString());
location = locationBuilder.build();
- hostProviderRegistry.unregister(hostProvider);
-
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
+ } finally {
+ hostProviderRegistry.unregister(hostProvider);
}
+
return Response
.created(location)
.build();
@@ -221,6 +222,10 @@
MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
+ if (null == node.get("locations")) {
+ throw new IllegalArgumentException("location isn't specified");
+ }
+
Iterator<JsonNode> locationNodes = node.get("locations").elements();
Set<HostLocation> locations = new HashSet<>();
while (locationNodes.hasNext()) {
@@ -231,6 +236,10 @@
locations.add(hostLocation);
}
+ if (null == node.get("ipAddresses")) {
+ throw new IllegalArgumentException("ipAddress isn't specified");
+ }
+
Iterator<JsonNode> ipNodes = node.get("ipAddresses").elements();
Set<IpAddress> ips = new HashSet<>();
while (ipNodes.hasNext()) {