[ONOS-3402] parseHost method adds all Json fields as annotations
Change-Id: Ic23a7509bbdc8ffb3fdd77299504ef03598145a0
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 455e492..00cbdf2 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
@@ -16,6 +16,7 @@
package org.onosproject.rest.resources;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
@@ -50,6 +51,7 @@
import java.net.URI;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Map;
import java.util.Set;
import static org.onlab.util.Tools.nullIsNotFound;
@@ -64,6 +66,7 @@
@Context
UriInfo uriInfo;
public static final String HOST_NOT_FOUND = "Host is not found";
+ private static final String[] REMOVAL_KEYS = {"mac", "vlan", "location", "ipAddresses"};
/**
* Get all end-station hosts.
@@ -199,8 +202,9 @@
while (ipStrings.hasNext()) {
ips.add(IpAddress.valueOf(ipStrings.next().asText()));
}
- //TODO remove elements from json node after reading them
- SparseAnnotations annotations = annotations(node);
+
+ // try to remove elements from json node after reading them
+ SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS));
// Update host inventory
HostId hostId = HostId.hostId(mac, vlanId);
@@ -210,6 +214,22 @@
}
/**
+ * Remove a set of elements from JsonNode by specifying keys.
+ *
+ * @param node JsonNode containing host information
+ * @param removalKeys key of elements that need to be removed
+ * @return removal keys
+ */
+ private JsonNode removeElements(JsonNode node, String[] removalKeys) {
+ ObjectMapper mapper = new ObjectMapper();
+ Map<String, Object> map = mapper.convertValue(node, Map.class);
+ for (String key : removalKeys) {
+ map.remove(key);
+ }
+ return mapper.convertValue(map, JsonNode.class);
+ }
+
+ /**
* Produces annotations from specified JsonNode. Copied from the ConfigProvider
* class for use in the POST method.
*