public synchronized Pair<InterfaceAddress, InterfaceAddress> getNextGeneratedLinkSubnet() { assert _currentGeneratedIpAsLong % 2 == 0; InterfaceAddress val = new InterfaceAddress(Ip.create(_currentGeneratedIpAsLong), Prefix.MAX_PREFIX_LENGTH - 1); InterfaceAddress val2 = new InterfaceAddress( Ip.create(_currentGeneratedIpAsLong + 1), Prefix.MAX_PREFIX_LENGTH - 1); _currentGeneratedIpAsLong += 2L; return new Pair<>(val, val2); }
@Override public void exitSniel3_ip(Sniel3_ipContext ctx) { InterfaceAddress address = new InterfaceAddress(ctx.address.getText()); _currentInterface.setAddress(address); _currentInterface.getAllAddresses().add(address); }
@Override public void exitIf_ip_address_secondary(If_ip_address_secondaryContext ctx) { Ip ip; Ip mask; InterfaceAddress address; if (ctx.prefix != null) { address = new InterfaceAddress(ctx.prefix.getText()); } else { ip = toIp(ctx.ip); mask = toIp(ctx.subnet); address = new InterfaceAddress(ip, mask.numSubnetBits()); } for (Interface currentInterface : _currentInterfaces) { currentInterface.getSecondaryAddresses().add(address); } }
@Override public void exitFailover_interface(Failover_interfaceContext ctx) { String name = ctx.name.getText(); Ip primaryIp = toIp(ctx.pip); Ip primaryMask = toIp(ctx.pmask); Ip standbyIp = toIp(ctx.sip); InterfaceAddress primaryAddress = new InterfaceAddress(primaryIp, primaryMask); InterfaceAddress standbyAddress = new InterfaceAddress(standbyIp, primaryMask); _configuration.getFailoverPrimaryAddresses().put(name, primaryAddress); _configuration.getFailoverStandbyAddresses().put(name, standbyAddress); }
@Override public void exitIf_ip_address(If_ip_addressContext ctx) { InterfaceAddress address; if (ctx.prefix != null) { address = new InterfaceAddress(ctx.prefix.getText()); } else { Ip ip = toIp(ctx.ip); Ip mask = toIp(ctx.subnet); address = new InterfaceAddress(ip, mask); } for (Interface currentInterface : _currentInterfaces) { currentInterface.setAddress(address); } if (ctx.STANDBY() != null) { Ip standbyIp = toIp(ctx.standby_address); InterfaceAddress standbyAddress = new InterfaceAddress(standbyIp, address.getNetworkBits()); for (Interface currentInterface : _currentInterfaces) { currentInterface.setStandbyAddress(standbyAddress); } } }
@Test public void testJsonSerialization() throws IOException { LocalRoute lr = new LocalRoute(new InterfaceAddress("1.1.1.1/24"), "Ethernet0"); assertThat(BatfishObjectMapper.clone(lr, LocalRoute.class), equalTo(lr)); }
@Test public void testJavaSerialization() { LocalRoute lr = new LocalRoute(new InterfaceAddress("1.1.1.1/24"), "Ethernet0"); assertThat(SerializationUtils.clone(lr), equalTo(lr)); }
@Test public void testToBuilder() { LocalRoute lr = new LocalRoute(new InterfaceAddress("1.1.1.1/24"), "Ethernet0"); assertThat(lr, equalTo(lr.toBuilder().build())); }
@Override public void exitNatp_address(Natp_addressContext ctx) { if (ctx.TO() != null) { if (ctx.IP_ADDRESS().isEmpty()) { // from IP_PREFIX to IP_PREFIX // Juniper will treat IP_PREFIX as IP ADDRESS _currentNatPool.setFromAddress(new InterfaceAddress(ctx.from.getText()).getIp()); _currentNatPool.setToAddress(new InterfaceAddress(ctx.to.getText()).getIp()); } else { // from IP_ADDRESS to IP_ADDRESS _currentNatPool.setFromAddress(Ip.parse(ctx.from.getText())); _currentNatPool.setToAddress(Ip.parse(ctx.to.getText())); } } else { Prefix prefix = Prefix.parse(ctx.prefix.getText()); _currentNatPool.setFromAddress(prefix.getFirstHostIp()); _currentNatPool.setToAddress(prefix.getLastHostIp()); } }
@Override public void exitIfiav_virtual_address(Ifiav_virtual_addressContext ctx) { Ip virtualAddress = Ip.parse(ctx.IP_ADDRESS().getText()); int prefixLength = _currentInterfaceAddress.getNetworkBits(); _currentVrrpGroup.setVirtualAddress(new InterfaceAddress(virtualAddress, prefixLength)); }
private Interface createIbgpInterface(BgpActivePeerConfig n, String peer) { Interface iface = new Interface("iBGP-" + peer); iface.setActive(true); // TODO is this valid. iface.setAddress(new InterfaceAddress(n.getPeerAddress(), Prefix.MAX_PREFIX_LENGTH)); iface.setBandwidth(0.); return iface; }
@Override public void exitIt_address(It_addressContext ctx) { if (ctx.DHCP() != null) { todo(ctx); } else if (ctx.IP_PREFIX() != null) { InterfaceAddress address = new InterfaceAddress(ctx.IP_PREFIX().getText()); if (_currentInterface.getAddress() == null) { _currentInterface.setAddress(address); } _currentInterface.getAllAddresses().add(address); } }
@Test public void testComputeIpsAssignedToThisInterface() { Configuration config = _cb.build(); Map<String, Configuration> configs = ImmutableMap.of(config.getHostname(), config); _ib.setOwner(config); InterfaceAddress primary = new InterfaceAddress(P1.getStartIp(), P1.getPrefixLength()); InterfaceAddress secondary = new InterfaceAddress(P2.getStartIp(), P2.getPrefixLength()); Interface i = _ib.setAddresses(primary, secondary).build(); _interfaceOwnedIps = TopologyUtil.computeInterfaceOwnedIps(configs, false); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); IpSpace result = forwardingAnalysisImpl.computeIpsAssignedToThisInterface(i); assertThat(result, containsIp(P1.getStartIp())); assertThat(result, containsIp(P2.getStartIp())); assertThat(result, not(containsIp(P2.getEndIp()))); }
/** Make an interface with the specified parameters */ private Interface iface(String interfaceName, String ip, boolean active, boolean blacklisted) { return _nf.interfaceBuilder() .setName(interfaceName) .setActive(active) .setAddress(new InterfaceAddress(ip)) .setBlacklisted(blacklisted) .build(); }
@Test public void testSynthesizeTopology_asymmetricPartialOverlap() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); Configuration c1 = cb.build(); Configuration c2 = cb.build(); nf.interfaceBuilder().setOwner(c1).setAddresses(new InterfaceAddress("1.2.3.4/24")).build(); nf.interfaceBuilder().setOwner(c2).setAddresses(new InterfaceAddress("1.2.3.17/28")).build(); Topology t = TopologyUtil.synthesizeL3Topology( ImmutableMap.of(c1.getHostname(), c1, c2.getHostname(), c2)); assertThat(t.getEdges(), empty()); }
@Test public void testSynthesizeTopology_asymmetricSharedIp() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); Configuration c1 = cb.build(); Configuration c2 = cb.build(); nf.interfaceBuilder().setOwner(c1).setAddresses(new InterfaceAddress("1.2.3.4/24")).build(); nf.interfaceBuilder().setOwner(c2).setAddresses(new InterfaceAddress("1.2.3.4/28")).build(); Topology t = TopologyUtil.synthesizeL3Topology( ImmutableMap.of(c1.getHostname(), c1, c2.getHostname(), c2)); assertThat(t.getEdges(), empty()); } }
@Test public void testSynthesizeTopology_asymmetric() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); Configuration c1 = cb.build(); Configuration c2 = cb.build(); Interface i1 = nf.interfaceBuilder().setOwner(c1).setAddresses(new InterfaceAddress("1.2.3.4/24")).build(); Interface i2 = nf.interfaceBuilder().setOwner(c2).setAddresses(new InterfaceAddress("1.2.3.5/28")).build(); Topology t = TopologyUtil.synthesizeL3Topology( ImmutableMap.of(c1.getHostname(), c1, c2.getHostname(), c2)); assertThat(t.getEdges(), equalTo(ImmutableSet.of(new Edge(i1, i2), new Edge(i2, i1)))); }
@Test public void testSynthesizeTopology_selfEdges() { NetworkFactory nf = new NetworkFactory(); Configuration c = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS).build(); Vrf v1 = nf.vrfBuilder().setOwner(c).setName("v1").build(); Vrf v2 = nf.vrfBuilder().setOwner(c).setName("v2").build(); Interface.Builder builder = nf.interfaceBuilder().setOwner(c); Interface i1 = builder.setAddresses(new InterfaceAddress("1.2.3.4/24")).setVrf(v1).build(); Interface i2 = builder.setAddresses(new InterfaceAddress("1.2.3.5/24")).setVrf(v1).build(); Interface i3 = builder.setAddresses(new InterfaceAddress("1.2.3.6/24")).setVrf(v2).build(); Topology t = TopologyUtil.synthesizeL3Topology(ImmutableMap.of(c.getHostname(), c)); assertThat( t.getEdges(), equalTo( ImmutableSet.of( new Edge(i1, i3), new Edge(i3, i1), new Edge(i2, i3), new Edge(i3, i2)))); }
private static Configuration createConfigurationWithDynamicSession( Configuration.Builder cb, Ip localIp, Prefix remotePrefix, List<Long> remoteAsList) { Configuration node = cb.setHostname("node4").build(); Interface iface = new Interface("iface", node, InterfaceType.PHYSICAL); iface.setAllAddresses( ImmutableList.of(new InterfaceAddress(localIp, Ip.numSubnetBitsToSubnetMask(32)))); BgpPassivePeerConfig peerConfig = BgpPassivePeerConfig.builder() .setLocalAs(3L) .setRemoteAs(remoteAsList) .setLocalIp(localIp) .setPeerPrefix(remotePrefix) .build(); BgpProcess bgpProcess = new BgpProcess(); bgpProcess.setPassiveNeighbors(ImmutableSortedMap.of(remotePrefix, peerConfig)); Vrf vrf1 = new Vrf("vrf"); vrf1.setBgpProcess(bgpProcess); node.setVrfs(ImmutableMap.of("vrf", vrf1)); node.setInterfaces(ImmutableSortedMap.of("iface", iface)); return node; }
private Batfish maxTracesBatfish() throws IOException { // create a simple network with two paths for a given destination Prefix dst = Prefix.parse("1.1.1.1/32"); NetworkFactory nf = new NetworkFactory(); Configuration config = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS).build(); Vrf vrf = nf.vrfBuilder().setOwner(config).build(); Interface.Builder ifaceBuilder = nf.interfaceBuilder().setActive(true).setOwner(config).setVrf(vrf); Interface lo0 = ifaceBuilder.setAddress(new InterfaceAddress("5.5.5.5/32")).build(); Interface lo1 = ifaceBuilder.setAddress(new InterfaceAddress("6.6.6.6/32")).build(); Builder routeBuilder = StaticRoute.builder().setNetwork(dst).setAdministrativeCost(1); vrf.setStaticRoutes( ImmutableSortedSet.of( routeBuilder.setNextHopInterface(lo0.getName()).build(), routeBuilder.setNextHopInterface(lo1.getName()).build())); Batfish batfish = BatfishTestUtils.getBatfish(ImmutableSortedMap.of(config.getHostname(), config), _folder); batfish.computeDataPlane(); return batfish; }