@Override public Set<String> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().keySet(); } }
/** * Get the {@link IpSpace} owned by the input interface. * * @param hostname The node the interface belongs to. * @param iface The name of the interface. * @return The {@link IpSpace} owned by the interface. */ default IpSpace getInterfaceOwnedIps(String hostname, String iface) { return getInterfaceOwnedIps() .getOrDefault(hostname, ImmutableMap.of()) .getOrDefault(iface, EmptyIpSpace.INSTANCE); }
@JsonIgnore /** Return the set of nodes that match this specifier */ public Set<String> getMatchingNodes(SpecifierContext ctxt) { switch (_type) { case NAME: return getMatchingNodesByName(ctxt.getConfigs().keySet()); case ROLE: Optional<NodeRoleDimension> roleDimension = ctxt.getNodeRoleDimension(_roleDimension); if (roleDimension.isPresent()) { return getMatchingNodesByRole(roleDimension.get(), ctxt.getConfigs().keySet()); } else { return Collections.emptySet(); } default: throw new BatfishException("Unhandled NodesSpecifier type: " + _type); } }
@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()); } }
@Override public IpSpace visitInterfaceLinkLocation(InterfaceLinkLocation interfaceLinkLocation) { String node = interfaceLinkLocation.getNodeName(); String iface = interfaceLinkLocation.getInterfaceName(); @Nullable IpSpace linkIpSpace = AclIpSpace.union( interfaceAddresses(node, iface).stream() /* * Only include addresses on networks that might have hosts. */ .filter(address -> address.getNetworkBits() <= HOST_SUBNET_MAX_PREFIX_LENGTH) .map(address -> address.getPrefix().toHostIpSpace()) .collect(Collectors.toList())); return linkIpSpace == null ? EmptyIpSpace.INSTANCE : AclIpSpace.difference(linkIpSpace, _specifierContext.getSnapshotDeviceOwnedIps()); }
@Override public IpSpaceAssignment resolve(Set<Location> locations, SpecifierContext ctxt) { AddressGroup addressGroup = ctxt.getReferenceBook(_bookName) .orElseThrow( () -> new NoSuchElementException("ReferenceBook '" + _bookName + "' not found")) .getAddressGroup(_addressGroupName) .orElseThrow( () -> new NoSuchElementException( "AddressGroup '" + _addressGroupName + "' not found in ReferenceBook '" + _bookName + "'")); return IpSpaceAssignment.builder() .assign( locations, firstNonNull( AclIpSpace.union( addressGroup.getAddresses().stream() .map(add -> new IpWildcard(add).toIpSpace()) .collect(Collectors.toList())), EmptyIpSpace.INSTANCE)) .build(); } }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { InterfaceGroup interfaceGroup = ctxt.getReferenceBook(_bookName) .orElseThrow( () -> new NoSuchElementException("ReferenceBook '" + _bookName + "' not found")) .getInterfaceGroup(_interfaceGroupName) .orElseThrow( () -> new NoSuchElementException( "InterfaceGroup '" + _interfaceGroupName + "' not found in ReferenceBook '" + _bookName + "'")); return nodes.stream() .map(n -> ctxt.getConfigs().get(n).getAllInterfaces().values()) .flatMap(Collection::stream) // we have a stream of Interfaces now .filter( v -> interfaceGroup .getInterfaces() .contains(new NodeInterfacePair(v.getOwner().getHostname(), v.getName()))) .collect(Collectors.toSet()); } }
@Override public Set<Location> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().entrySet().stream() .filter(entry -> _pattern.matcher(entry.getKey()).matches()) .map(Entry::getValue) .flatMap(this::getNodeLocations) .collect(ImmutableSet.toImmutableSet()); } }
@Override public Set<String> resolve(SpecifierContext ctxt) { Optional<NodeRoleDimension> dimension = ctxt.getNodeRoleDimension(_roleDimension); Set<NodeRole> roles = dimension.isPresent() ? dimension.get().getRoles().stream() .filter(role -> _rolePattern.matcher(role.getName()).matches()) .collect(ImmutableSet.toImmutableSet()) : ImmutableSet.of(); return ctxt.getConfigs().keySet().stream() .filter(node -> roles.stream().anyMatch(role -> role.matches(node))) .collect(ImmutableSet.toImmutableSet()); } }
@Override public IpSpace visitInterfaceLocation(InterfaceLocation interfaceLocation) { return _specifierContext.getInterfaceOwnedIps( interfaceLocation.getNodeName(), interfaceLocation.getInterfaceName()); } }
@Override public Set<String> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().keySet().stream() .filter(n -> _namePattern.matcher(n).matches()) .collect(ImmutableSet.toImmutableSet()); } }
@Override public Set<Location> resolve(SpecifierContext ctxt) { Optional<NodeRoleDimension> dimension = ctxt.getNodeRoleDimension(_roleDimension); Set<NodeRole> matchingRoles = dimension.isPresent() ? dimension.get().getRoles().stream() .filter(role -> _rolePattern.matcher(role.getName()).matches()) .collect(ImmutableSet.toImmutableSet()) : ImmutableSet.of(); return ctxt.getConfigs().values().stream() .filter(node -> matchingRoles.stream().anyMatch(role -> role.matches(node.getHostname()))) .flatMap(this::getNodeLocations) .collect(ImmutableSet.toImmutableSet()); } }
@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); return config.getIpAccessLists().values().stream() .filter(filter -> _shorthandSpecifier.matches(filter, config)) .collect(Collectors.toSet()); } }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .filter(c -> nodes.contains(c.getHostname())) .map(c -> c.getAllInterfaces().values()) .flatMap(Collection::stream) .filter(_shorthandSpecifier::matches) .collect(Collectors.toSet()); } }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { return nodes.stream() .map(n -> ctxt.getConfigs().get(n).getVrfs().values()) .flatMap(Collection::stream) // we have a stream of VRFs now .filter(v -> _pattern.matcher(v.getName()).matches()) .map(v -> v.getInterfaces().values()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .filter(c -> nodes.contains(c.getHostname())) .flatMap(c -> c.getAllInterfaces().values().stream().filter(Interface::getActive)) .filter(i -> i.getAllAddresses().stream().anyMatch(this::interfaceAddressMatchesIpSpace)) .collect(Collectors.toSet()); }
@Override public Set<Location> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .flatMap(node -> node.getVrfs().values().stream()) .filter(vrf -> _pattern.matcher(vrf.getName()).matches()) .flatMap(this::getVrfLocations) .collect(ImmutableSet.toImmutableSet()); } }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { Map<String, Configuration> configs = ctxt.getConfigs(); return nodes.stream() .map(configs::get) .flatMap(config -> config.getAllInterfaces().values().stream()) .filter(iface -> _interfaceTypes.contains(iface.getInterfaceType())) .collect(ImmutableSet.toImmutableSet()); }
@Override public Set<Location> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .flatMap(config -> config.getAllInterfaces().values().stream()) .filter(iface -> _pattern.matcher(iface.getDescription()).matches()) .map(this::getLocation) .collect(ImmutableSet.toImmutableSet()); } }
@Override public Set<Location> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .flatMap(node -> node.getAllInterfaces().values().stream()) .filter(iface -> _pattern.matcher(iface.getName()).matches()) .map(this::getLocation) .collect(ImmutableSet.toImmutableSet()); } }