@Override protected Stream<Location> getNodeLocations(Configuration node) { return node.getAllInterfaces().values().stream() .map(iface -> new InterfaceLocation(node.getHostname(), iface.getName())); } }
@Override public Location visitInterfaceLocation(InterfaceLocation interfaceLocation) { return new InterfaceLinkLocation( interfaceLocation.getNodeName(), interfaceLocation.getInterfaceName()); } });
@Test public void resolveLocationTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.LOCATION); question.setLocationSpecifierInput(_c1.getHostname()); // only c1:iface1 should be present assertThat( resolveLocation(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_LOCATION, new InterfaceLocation(_c1.getHostname(), _iface1.getName()).toString())))); }
@Override public Boolean visitInterfaceLocation(InterfaceLocation interfaceLocation) { return interfaceLocation.getNodeName().equals(node); } };
@Override public IpSpace visitInterfaceLocation(InterfaceLocation interfaceLocation) { return _specifierContext.getInterfaceOwnedIps( interfaceLocation.getNodeName(), interfaceLocation.getInterfaceName()); } }
@Override Stream<Location> getNodeLocations(Configuration node) { return node.getAllInterfaces().values().stream() .map(iface -> new InterfaceLocation(node.getHostname(), iface.getName())); } }
@Override public Void visitInterfaceLocation(@Nonnull InterfaceLocation interfaceLocation) { flowBuilder .setIngressInterface(null) .setIngressNode(interfaceLocation.getNodeName()) .setIngressVrf( interfaceVrf( interfaceLocation.getNodeName(), interfaceLocation.getInterfaceName())); return null; } });
@Override public Set<Location> resolve(SpecifierContext ctxt) { return _nodeSpecifier.resolve(ctxt).stream() .map(n -> ctxt.getConfigs().get(n).getAllInterfaces().values()) .flatMap(Collection::stream) .map(iface -> new InterfaceLocation(iface.getOwner().getHostname(), iface.getName())) .collect(Collectors.toSet()); } }
@Override public Optional<StateExpr> visitInterfaceLocation( @Nonnull InterfaceLocation interfaceLocation) { Configuration config = _configs.get(interfaceLocation.getNodeName()); if (config == null) { return Optional.empty(); } Interface iface = config.getAllInterfaces().get(interfaceLocation.getInterfaceName()); if (iface == null) { return Optional.empty(); } String vrf = iface.getVrfName(); return Optional.of(new OriginateVrf(interfaceLocation.getNodeName(), vrf)); } };
@Override public Set<Location> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .flatMap(node -> node.getAllInterfaces().values().stream()) .map(iface -> new InterfaceLocation(iface.getOwner().getHostname(), iface.getName())) .collect(ImmutableSet.toImmutableSet()); } }
@Override public IngressLocation visitInterfaceLocation(InterfaceLocation interfaceLocation) { String node = interfaceLocation.getNodeName(); String iface = interfaceLocation.getInterfaceName(); return IngressLocation.vrf(node, _configs.get(node).getAllInterfaces().get(iface).getVrfName()); } }
@Override public Set<Location> resolve(SpecifierContext ctxt) { return _interfaceSpecifier.resolve(ctxt.getConfigs().keySet(), ctxt).stream() .map(iface -> new InterfaceLocation(iface.getOwner().getHostname(), iface.getName())) .collect(Collectors.toSet()); } }
@Override public Void visitInterfaceLocation(@Nonnull InterfaceLocation interfaceLocation) { flowBuilder .setIngressInterface(null) .setIngressNode(interfaceLocation.getNodeName()) .setIngressVrf( c.getAllInterfaces() .get(interfaceLocation.getInterfaceName()) .getVrf() .getName()); return null; } });
@Override protected Stream<Location> getVrfLocations(Vrf vrf) { return vrf.getInterfaces().values().stream() .map(iface -> new InterfaceLocation(iface.getOwner().getHostname(), iface.getName())); } }
/** Returns {@code true} iff the given {@link Location} is active (aka, interface is up). */ @VisibleForTesting static boolean isActive(Location l, Map<String, Configuration> configs) { NodeInterfacePair iface; if (l instanceof InterfaceLocation) { iface = new NodeInterfacePair( ((InterfaceLocation) l).getNodeName(), ((InterfaceLocation) l).getInterfaceName()); } else { assert l instanceof InterfaceLinkLocation; iface = new NodeInterfacePair( ((InterfaceLinkLocation) l).getNodeName(), ((InterfaceLinkLocation) l).getInterfaceName()); } return configs .get(iface.getHostname()) .getAllInterfaces() .get(iface.getInterface()) .getActive(); }
@Override protected Location getLocation(Interface iface) { return new InterfaceLocation(iface.getOwner().getHostname(), iface.getName()); } }
@Override protected Location getLocation(Interface iface) { return new InterfaceLocation(iface.getOwner().getHostname(), iface.getName()); } }
/** Do we convert locations properly? */ @Test public void toInterfaceLinkLocation() { InterfaceLocation ifaceLocation = new InterfaceLocation("foo", "bar"); InterfaceLinkLocation ifaceLinkLocation = new InterfaceLinkLocation("foo", "bar"); assertThat( ToInterfaceLinkLocationSpecifier.toInterfaceLinkLocation(ifaceLocation), equalTo(ifaceLinkLocation)); assertThat( ToInterfaceLinkLocationSpecifier.toInterfaceLinkLocation(ifaceLinkLocation), equalTo(ifaceLinkLocation)); } }
@Test public void testClone() throws IOException { InterfaceLocation orig = new InterfaceLocation("foo", "bar"); assertThat(orig, equalTo(BatfishObjectMapper.clone(orig, new TypeReference<Location>() {}))); } }
/** Do we resolve non empty sets properly? */ @Test public void resolveNonEmptySet() { LocationSpecifier inner = new MockLocationSpecifier( ImmutableSet.of( new InterfaceLocation("foo1", "bar1"), new InterfaceLinkLocation("foo2", "bar2"))); assertThat( new ToInterfaceLinkLocationSpecifier(inner).resolve(_mockContext), equalTo( ImmutableSet.of( new InterfaceLinkLocation("foo1", "bar1"), new InterfaceLinkLocation("foo2", "bar2")))); }