@Test public void testToHostInterfacesDeserializationFromArrayDuplicate() throws IOException { String jsonText = "[{ \"name\": \"eth0\", \"gateway\": \"10.0.0.1\" },{ \"name\": \"eth0\", \"gateway\": \"10.0.0.2\" }]"; Map<String, HostInterface> hostInterfaces = toHostInterfaces(BatfishObjectMapper.mapper().readTree(jsonText)); assertThat(hostInterfaces, hasKeys("eth0")); assertThat(hostInterfaces.get("eth0").getGateway(), equalTo(Ip.parse("10.0.0.2"))); }
@VisibleForTesting static @Nonnull Map<String, HostInterface> toHostInterfaces(JsonNode hostInterfacesNode) { Map<String, HostInterface> hostInterfaces; if (hostInterfacesNode == null) { hostInterfaces = ImmutableMap.of(); } else if (hostInterfacesNode instanceof ObjectNode) { hostInterfaces = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<Map<String, HostInterface>>() {}); hostInterfaces.forEach( (name, hostInterface) -> checkArgument( name.equals(hostInterface.getName()), "Mismatch between hostInterface key '%s' and name '%s'", name, hostInterface.getName())); } else { assert hostInterfacesNode instanceof ArrayNode; List<HostInterface> hostInterfacesList = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<List<HostInterface>>() {}); hostInterfaces = hostInterfacesList.stream() .collect( ImmutableMap.toImmutableMap( HostInterface::getName, Function.identity(), (hi1, hi2) -> hi2)); } return hostInterfaces; }
HostConfiguration extends VendorConfiguration { @VisibleForTesting static @Nonnull Map<String, HostInterface> toHostInterfaces(JsonNode hostInterfacesNode) { Map<String, HostInterface> hostInterfaces; if (hostInterfacesNode == null) { hostInterfaces = ImmutableMap.of(); } else if (hostInterfacesNode instanceof ObjectNode) { hostInterfaces = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<Map<String, HostInterface>>() {}); hostInterfaces.forEach( (name, hostInterface) -> checkArgument( name.equals(hostInterface.getName()), "Mismatch between hostInterface key '%s' and name '%s'", name, hostInterface.getName())); } else { assert hostInterfacesNode instanceof ArrayNode; List<HostInterface> hostInterfacesList = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<List<HostInterface>>() {}); hostInterfaces = hostInterfacesList.stream() .collect( ImmutableMap.toImmutableMap( HostInterface::getName, Function.identity(), (hi1, hi2) -> hi2)); } return hostInterfaces; } }
HostConfiguration extends VendorConfiguration { @VisibleForTesting static @Nonnull Map<String, HostInterface> toHostInterfaces(JsonNode hostInterfacesNode) { Map<String, HostInterface> hostInterfaces; if (hostInterfacesNode == null) { hostInterfaces = ImmutableMap.of(); } else if (hostInterfacesNode instanceof ObjectNode) { hostInterfaces = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<Map<String, HostInterface>>() {}); hostInterfaces.forEach( (name, hostInterface) -> checkArgument( name.equals(hostInterface.getName()), "Mismatch between hostInterface key '%s' and name '%s'", name, hostInterface.getName())); } else { assert hostInterfacesNode instanceof ArrayNode; List<HostInterface> hostInterfacesList = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<List<HostInterface>>() {}); hostInterfaces = hostInterfacesList.stream() .collect( ImmutableMap.toImmutableMap( HostInterface::getName, Function.identity(), (hi1, hi2) -> hi2)); } return hostInterfaces; } HostConfiguration(); }
HostConfiguration extends VendorConfiguration { @VisibleForTesting static @Nonnull Map<String, HostInterface> toHostInterfaces(JsonNode hostInterfacesNode) { Map<String, HostInterface> hostInterfaces; if (hostInterfacesNode == null) { hostInterfaces = ImmutableMap.of(); } else if (hostInterfacesNode instanceof ObjectNode) { hostInterfaces = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<Map<String, HostInterface>>() {}); hostInterfaces.forEach( (name, hostInterface) -> checkArgument( name.equals(hostInterface.getName()), "Mismatch between hostInterface key '%s' and name '%s'", name, hostInterface.getName())); } else { assert hostInterfacesNode instanceof ArrayNode; List<HostInterface> hostInterfacesList = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<List<HostInterface>>() {}); hostInterfaces = hostInterfacesList.stream() .collect( ImmutableMap.toImmutableMap( HostInterface::getName, Function.identity(), (hi1, hi2) -> hi2)); } return hostInterfaces; } HostConfiguration(); static HostConfiguration fromJson(String filename, String text, Warnings warnings); @JsonProperty(PROP_HOST_INTERFACES) Map<String, HostInterface> getHostInterfaces(); @JsonProperty(PROP_HOSTNAME) @Override String getHostname(); @JsonProperty(PROP_IPTABLES_FILE) String getIptablesFile(); IptablesVendorConfiguration getIptablesVendorConfig(); @JsonProperty(PROP_OVERLAY) boolean getOverlay(); @Override void setHostname(String hostname); void setIptablesFile(String file); void setIptablesVendorConfig(IptablesVendorConfiguration config); @JsonProperty(PROP_OVERLAY) void setOverlay(boolean overlay); @JsonIgnore @Override void setVendor(ConfigurationFormat format); @Override List<Configuration> toVendorIndependentConfigurations(); List<Configuration> toVendorIndependentConfigurations(
      VendorConfiguration underlayConfiguration); }
HostConfiguration extends VendorConfiguration { @VisibleForTesting static @Nonnull Map<String, HostInterface> toHostInterfaces(JsonNode hostInterfacesNode) { Map<String, HostInterface> hostInterfaces; if (hostInterfacesNode == null) { hostInterfaces = ImmutableMap.of(); } else if (hostInterfacesNode instanceof ObjectNode) { hostInterfaces = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<Map<String, HostInterface>>() {}); hostInterfaces.forEach( (name, hostInterface) -> checkArgument( name.equals(hostInterface.getName()), "Mismatch between hostInterface key '%s' and name '%s'", name, hostInterface.getName())); } else { assert hostInterfacesNode instanceof ArrayNode; List<HostInterface> hostInterfacesList = BatfishObjectMapper.mapper() .convertValue(hostInterfacesNode, new TypeReference<List<HostInterface>>() {}); hostInterfaces = hostInterfacesList.stream() .collect( ImmutableMap.toImmutableMap( HostInterface::getName, Function.identity(), (hi1, hi2) -> hi2)); } return hostInterfaces; } HostConfiguration(); static HostConfiguration fromJson(String filename, String text, Warnings warnings); @JsonProperty(PROP_HOST_INTERFACES) Map<String, HostInterface> getHostInterfaces(); @JsonProperty(PROP_HOSTNAME) @Override String getHostname(); @JsonProperty(PROP_IPTABLES_FILE) String getIptablesFile(); IptablesVendorConfiguration getIptablesVendorConfig(); @JsonProperty(PROP_OVERLAY) boolean getOverlay(); @Override void setHostname(String hostname); void setIptablesFile(String file); void setIptablesVendorConfig(IptablesVendorConfiguration config); @JsonProperty(PROP_OVERLAY) void setOverlay(boolean overlay); @JsonIgnore @Override void setVendor(ConfigurationFormat format); @Override List<Configuration> toVendorIndependentConfigurations(); List<Configuration> toVendorIndependentConfigurations(
      VendorConfiguration underlayConfiguration);  }