public FilterGroup toFilterGroup() { return new FilterGroup(ImmutableList.copyOf(firstNonNull(filters, ImmutableSet.of())), name); } }
public FilterGroupBean(FilterGroup filterGroup) { name = filterGroup.getName(); filters = ImmutableList.copyOf(filterGroup.getFilters()); }
/** Return the {@link FilterGroup} with name {@code groupName} */ public Optional<FilterGroup> getFilterGroup(String groupName) { return _filterGroups.stream().filter(group -> group.getName().equals(groupName)).findAny(); }
@Override public Set<IpAccessList> resolve(String node, SpecifierContext ctxt) { checkArgument( ctxt.getConfigs().containsKey(node), "SpecifierContext does not have configs for node " + node); Configuration config = ctxt.getConfigs().get(node); FilterGroup filterGroup = ctxt.getReferenceBook(_bookName) .orElseThrow( () -> new NoSuchElementException("ReferenceBook '" + _bookName + "' not found")) .getFilterGroup(_filterGroupName) .orElseThrow( () -> new NoSuchElementException( "FilterGroup '" + _filterGroupName + "' not found in ReferenceBook '" + _bookName + "'")); return config.getIpAccessLists().values().stream() .filter( filter -> filterGroup.getFilters().stream() .anyMatch(specifier -> specifier.matches(filter, config))) .collect(Collectors.toSet()); } }
@Test public void conversionToAndFrom() { FilterGroup group = new FilterGroup( ImmutableList.of(new FiltersSpecifier("abc"), new FiltersSpecifier("ipv4:def")), "fg1"); FilterGroupBean bean = new FilterGroupBean(group); assertThat(new FilterGroupBean(bean.toFilterGroup()), equalTo(bean)); } }
@Test public void constructorNonEmptyBook() { ReferenceBook book2 = ReferenceBook.builder("book2") .setAddressGroups(ImmutableList.of(new AddressGroup(ImmutableSortedSet.of(), "ag1"))) .setFilterGroups(ImmutableList.of(new FilterGroup(ImmutableList.of(), "fg1"))) .setInterfaceGroups( ImmutableList.of(new InterfaceGroup(ImmutableSortedSet.of(), "ig1"))) .setServiceEndpoints(ImmutableList.of(new ServiceEndpoint("ag1", "se1", "so1"))) .setServiceObjectGroups( ImmutableList.of(new ServiceObjectGroup("sog1", ImmutableSortedSet.of()))) .setServiceObjects( ImmutableList.of(new ServiceObject(IpProtocol.TCP, "so1", new SubRange(2, 3)))) .build(); beanBookMatch(new ReferenceBookBean(book2), book2); }
@Test public void toAddressBook() { ReferenceBookBean bean = new ReferenceBookBean( ReferenceBook.builder("book2") .setAddressGroups( ImmutableList.of(new AddressGroup(ImmutableSortedSet.of(), "ag1"))) .setFilterGroups(ImmutableList.of(new FilterGroup(ImmutableList.of(), "fg1"))) .setInterfaceGroups( ImmutableList.of(new InterfaceGroup(ImmutableSortedSet.of(), "ig1"))) .setServiceEndpoints(ImmutableList.of(new ServiceEndpoint("ag1", "se1", "so1"))) .setServiceObjectGroups( ImmutableList.of(new ServiceObjectGroup("sog1", ImmutableSortedSet.of()))) .setServiceObjects( ImmutableList.of(new ServiceObject(IpProtocol.TCP, "so1", new SubRange(2, 3)))) .build()); beanBookMatch(bean, bean.toAddressBook()); } }