Committed by
Brian O'Connor
[ONOS-3402] parseHost method adds all Json fields as annotations
Change-Id: Ic23a7509bbdc8ffb3fdd77299504ef03598145a0
Showing
1 changed file
with
22 additions
and
2 deletions
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | package org.onosproject.rest.resources; | 16 | package org.onosproject.rest.resources; |
17 | 17 | ||
18 | import com.fasterxml.jackson.databind.JsonNode; | 18 | import com.fasterxml.jackson.databind.JsonNode; |
19 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
19 | import com.fasterxml.jackson.databind.node.ObjectNode; | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
20 | import org.onlab.packet.IpAddress; | 21 | import org.onlab.packet.IpAddress; |
21 | import org.onlab.packet.MacAddress; | 22 | import org.onlab.packet.MacAddress; |
... | @@ -50,6 +51,7 @@ import java.io.InputStream; | ... | @@ -50,6 +51,7 @@ import java.io.InputStream; |
50 | import java.net.URI; | 51 | import java.net.URI; |
51 | import java.util.HashSet; | 52 | import java.util.HashSet; |
52 | import java.util.Iterator; | 53 | import java.util.Iterator; |
54 | +import java.util.Map; | ||
53 | import java.util.Set; | 55 | import java.util.Set; |
54 | 56 | ||
55 | import static org.onlab.util.Tools.nullIsNotFound; | 57 | import static org.onlab.util.Tools.nullIsNotFound; |
... | @@ -64,6 +66,7 @@ public class HostsWebResource extends AbstractWebResource { | ... | @@ -64,6 +66,7 @@ public class HostsWebResource extends AbstractWebResource { |
64 | @Context | 66 | @Context |
65 | UriInfo uriInfo; | 67 | UriInfo uriInfo; |
66 | public static final String HOST_NOT_FOUND = "Host is not found"; | 68 | public static final String HOST_NOT_FOUND = "Host is not found"; |
69 | + private static final String[] REMOVAL_KEYS = {"mac", "vlan", "location", "ipAddresses"}; | ||
67 | 70 | ||
68 | /** | 71 | /** |
69 | * Get all end-station hosts. | 72 | * Get all end-station hosts. |
... | @@ -199,8 +202,9 @@ public class HostsWebResource extends AbstractWebResource { | ... | @@ -199,8 +202,9 @@ public class HostsWebResource extends AbstractWebResource { |
199 | while (ipStrings.hasNext()) { | 202 | while (ipStrings.hasNext()) { |
200 | ips.add(IpAddress.valueOf(ipStrings.next().asText())); | 203 | ips.add(IpAddress.valueOf(ipStrings.next().asText())); |
201 | } | 204 | } |
202 | - //TODO remove elements from json node after reading them | 205 | + |
203 | - SparseAnnotations annotations = annotations(node); | 206 | + // try to remove elements from json node after reading them |
207 | + SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS)); | ||
204 | // Update host inventory | 208 | // Update host inventory |
205 | 209 | ||
206 | HostId hostId = HostId.hostId(mac, vlanId); | 210 | HostId hostId = HostId.hostId(mac, vlanId); |
... | @@ -210,6 +214,22 @@ public class HostsWebResource extends AbstractWebResource { | ... | @@ -210,6 +214,22 @@ public class HostsWebResource extends AbstractWebResource { |
210 | } | 214 | } |
211 | 215 | ||
212 | /** | 216 | /** |
217 | + * Remove a set of elements from JsonNode by specifying keys. | ||
218 | + * | ||
219 | + * @param node JsonNode containing host information | ||
220 | + * @param removalKeys key of elements that need to be removed | ||
221 | + * @return removal keys | ||
222 | + */ | ||
223 | + private JsonNode removeElements(JsonNode node, String[] removalKeys) { | ||
224 | + ObjectMapper mapper = new ObjectMapper(); | ||
225 | + Map<String, Object> map = mapper.convertValue(node, Map.class); | ||
226 | + for (String key : removalKeys) { | ||
227 | + map.remove(key); | ||
228 | + } | ||
229 | + return mapper.convertValue(map, JsonNode.class); | ||
230 | + } | ||
231 | + | ||
232 | + /** | ||
213 | * Produces annotations from specified JsonNode. Copied from the ConfigProvider | 233 | * Produces annotations from specified JsonNode. Copied from the ConfigProvider |
214 | * class for use in the POST method. | 234 | * class for use in the POST method. |
215 | * | 235 | * | ... | ... |
-
Please register or login to post a comment