@Test public void testCreatePublicIpsRefBook() { NetworkInterface networkInterface = new NetworkInterface( "interface", "subnet", "vpc", ImmutableList.of(), ImmutableList.of( new PrivateIpAddress(true, Ip.parse("10.10.10.10"), Ip.parse("5.5.5.5"))), "desc2", null, ImmutableMap.of()); NetworkInterface networkInterface2 = new NetworkInterface( "interface2", "subnet", "vpc", ImmutableList.of(), ImmutableList.of( new PrivateIpAddress(true, Ip.parse("10.10.10.10"), Ip.parse("3.3.3.3")), new PrivateIpAddress(true, Ip.parse("10.10.10.10"), Ip.parse("4.4.4.4"))), "desc", null, ImmutableMap.of()); NetworkInterface networkInterface0 = new NetworkInterface( "interface0", "subnet", "vpc", ImmutableList.of(), ImmutableList.of(new PrivateIpAddress(true, Ip.parse("10.10.10.10"), null)), "desc", null, ImmutableMap.of()); Configuration cfgNode = new Configuration("cfg", ConfigurationFormat.AWS); String bookName = GeneratedRefBookUtils.getName(cfgNode.getHostname(), BookType.PublicIps); createPublicIpsRefBook(Collections.singleton(networkInterface0), cfgNode); assertTrue(cfgNode.getGeneratedReferenceBooks().isEmpty()); createPublicIpsRefBook( ImmutableList.of(networkInterface, networkInterface0, networkInterface2), cfgNode); assertThat( cfgNode.getGeneratedReferenceBooks().get(bookName), equalTo( ReferenceBook.builder(bookName) .setAddressGroups( ImmutableList.of( new AddressGroup( ImmutableSortedSet.of("5.5.5.5"), publicIpAddressGroupName(networkInterface)), new AddressGroup( ImmutableSortedSet.of("3.3.3.3", "4.4.4.4"), publicIpAddressGroupName(networkInterface2)))) .build())); _thrown.expect(IllegalArgumentException.class); createPublicIpsRefBook(ImmutableList.of(networkInterface), cfgNode); }
static void createPublicIpsRefBook( Collection<NetworkInterface> networkInterfaces, Configuration cfgNode) { String publicIpBookName = GeneratedRefBookUtils.getName(cfgNode.getHostname(), BookType.PublicIps); checkArgument( !cfgNode.getGeneratedReferenceBooks().containsKey(publicIpBookName), "Generated reference book for public IPs already exists for node %s", cfgNode.getHostname()); List<AddressGroup> publicIpAddressGroups = networkInterfaces.stream() .map( iface -> new AddressGroup( iface.getPrivateIpAddresses().stream() .filter(privIp -> privIp.getPublicIp() != null) .map(privIp -> privIp.getPublicIp().toString()) .collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder())), publicIpAddressGroupName(iface))) .filter(ag -> !ag.getAddresses().isEmpty()) .collect(ImmutableList.toImmutableList()); if (!publicIpAddressGroups.isEmpty()) { cfgNode .getGeneratedReferenceBooks() .put( publicIpBookName, ReferenceBook.builder(publicIpBookName) .setAddressGroups(publicIpAddressGroups) .build()); } }
Utils { static void createPublicIpsRefBook( Collection<NetworkInterface> networkInterfaces, Configuration cfgNode) { String publicIpBookName = GeneratedRefBookUtils.getName(cfgNode.getHostname(), BookType.PublicIps); checkArgument( !cfgNode.getGeneratedReferenceBooks().containsKey(publicIpBookName), "Generated reference book for public IPs already exists for node %s", cfgNode.getHostname()); List<AddressGroup> publicIpAddressGroups = networkInterfaces.stream() .map( iface -> new AddressGroup( iface.getPrivateIpAddresses().stream() .filter(privIp -> privIp.getPublicIp() != null) .map(privIp -> privIp.getPublicIp().toString()) .collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder())), publicIpAddressGroupName(iface))) .filter(ag -> !ag.getAddresses().isEmpty()) .collect(ImmutableList.toImmutableList()); if (!publicIpAddressGroups.isEmpty()) { cfgNode .getGeneratedReferenceBooks() .put( publicIpBookName, ReferenceBook.builder(publicIpBookName) .setAddressGroups(publicIpAddressGroups) .build()); } } }
Utils { static void createPublicIpsRefBook( Collection<NetworkInterface> networkInterfaces, Configuration cfgNode) { String publicIpBookName = GeneratedRefBookUtils.getName(cfgNode.getHostname(), BookType.PublicIps); checkArgument( !cfgNode.getGeneratedReferenceBooks().containsKey(publicIpBookName), "Generated reference book for public IPs already exists for node %s", cfgNode.getHostname()); List<AddressGroup> publicIpAddressGroups = networkInterfaces.stream() .map( iface -> new AddressGroup( iface.getPrivateIpAddresses().stream() .filter(privIp -> privIp.getPublicIp() != null) .map(privIp -> privIp.getPublicIp().toString()) .collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder())), publicIpAddressGroupName(iface))) .filter(ag -> !ag.getAddresses().isEmpty()) .collect(ImmutableList.toImmutableList()); if (!publicIpAddressGroups.isEmpty()) { cfgNode .getGeneratedReferenceBooks() .put( publicIpBookName, ReferenceBook.builder(publicIpBookName) .setAddressGroups(publicIpAddressGroups) .build()); } } private  Utils(); }
Utils { static void createPublicIpsRefBook( Collection<NetworkInterface> networkInterfaces, Configuration cfgNode) { String publicIpBookName = GeneratedRefBookUtils.getName(cfgNode.getHostname(), BookType.PublicIps); checkArgument( !cfgNode.getGeneratedReferenceBooks().containsKey(publicIpBookName), "Generated reference book for public IPs already exists for node %s", cfgNode.getHostname()); List<AddressGroup> publicIpAddressGroups = networkInterfaces.stream() .map( iface -> new AddressGroup( iface.getPrivateIpAddresses().stream() .filter(privIp -> privIp.getPublicIp() != null) .map(privIp -> privIp.getPublicIp().toString()) .collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder())), publicIpAddressGroupName(iface))) .filter(ag -> !ag.getAddresses().isEmpty()) .collect(ImmutableList.toImmutableList()); if (!publicIpAddressGroups.isEmpty()) { cfgNode .getGeneratedReferenceBooks() .put( publicIpBookName, ReferenceBook.builder(publicIpBookName) .setAddressGroups(publicIpAddressGroups) .build()); } } private  Utils(); @Nullable static IpProtocol toIpProtocol(String ipProtocolAsString); static TraceElement getTraceElementForRule(@Nullable String ruleDescription); static TraceElement getTraceElementForSecurityGroup(String securityGroupName); }
Utils { static void createPublicIpsRefBook( Collection<NetworkInterface> networkInterfaces, Configuration cfgNode) { String publicIpBookName = GeneratedRefBookUtils.getName(cfgNode.getHostname(), BookType.PublicIps); checkArgument( !cfgNode.getGeneratedReferenceBooks().containsKey(publicIpBookName), "Generated reference book for public IPs already exists for node %s", cfgNode.getHostname()); List<AddressGroup> publicIpAddressGroups = networkInterfaces.stream() .map( iface -> new AddressGroup( iface.getPrivateIpAddresses().stream() .filter(privIp -> privIp.getPublicIp() != null) .map(privIp -> privIp.getPublicIp().toString()) .collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder())), publicIpAddressGroupName(iface))) .filter(ag -> !ag.getAddresses().isEmpty()) .collect(ImmutableList.toImmutableList()); if (!publicIpAddressGroups.isEmpty()) { cfgNode .getGeneratedReferenceBooks() .put( publicIpBookName, ReferenceBook.builder(publicIpBookName) .setAddressGroups(publicIpAddressGroups) .build()); } } private  Utils(); @Nullable static IpProtocol toIpProtocol(String ipProtocolAsString); static TraceElement getTraceElementForRule(@Nullable String ruleDescription); static TraceElement getTraceElementForSecurityGroup(String securityGroupName);  }