@Test public void testComputeInterfaceCost() { Interface.Builder ib = Interface.builder() .setName("eth0") .setBandwidth(1e3) .setOwner(new Configuration("r1", ConfigurationFormat.CISCO_IOS)); Interface i = ib.build(); // Round up to 1 int cost = OspfProcess.computeInterfaceCost(1.0, i); assertThat(cost, equalTo(1)); cost = OspfProcess.computeInterfaceCost(1e6, i); assertThat(cost, equalTo(1000)); _thrown.expectMessage("Interface eth0 on r1 is missing bandwidth"); _thrown.expect(IllegalStateException.class); OspfProcess.computeInterfaceCost(1e6, ib.setBandwidth(null).build()); }
@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()))); }
@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)))); }
private static Configuration configWithOneAcl(NetworkFactory nf) { Configuration config = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS).build(); Interface.Builder ib = nf.interfaceBuilder().setOwner(config); ib.setName(IFACE1).build(); ib.setName(IFACE2).build(); ib.setName(IFACE3).build(); ib.setName(IFACE4).setActive(false).build(); // an ACL that can only match with an IFACE2 or iface3 IpAccessList.builder() .setName("acl") .setOwner(config) .setLines( ImmutableList.of( accepting().setMatchCondition(matchSrcInterface(IFACE1)).build(), rejecting().setMatchCondition(matchSrcInterface(IFACE4)).build(), ACCEPT_ALL)) .build(); return config; }
@Test public void testToLogicalEdgeNode1Absent() { String c1Name = "c1"; String c2Name = "c2"; String iName = "i1"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1); _ib.setName(iName).build().setChannelGroup("missing"); Configuration c2 = _cb.setHostname(c2Name).build(); Vrf v2 = _vb.setOwner(c2).build(); _ib.setOwner(c2).setVrf(v2); _ib.setName(iName).build(); NetworkConfigurations networkConfigurations = NetworkConfigurations.of(ImmutableSortedMap.of(c1Name, c1, c2Name, c2)); Layer1Edge edge = new Layer1Edge(new Layer1Node(c1Name, iName), new Layer1Node(c2Name, iName)); // If node1 of a layer-1 physical edge cannot be mapped to a layer-1 logical node, a null edge // should be returned. assertThat(edge.toLogicalEdge(networkConfigurations), nullValue()); }
@Test public void testToLogicalEdgeNode2Absent() { String c1Name = "c1"; String c2Name = "c2"; String iName = "i1"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1); _ib.setName(iName).build(); Configuration c2 = _cb.setHostname(c2Name).build(); Vrf v2 = _vb.setOwner(c2).build(); _ib.setOwner(c2).setVrf(v2); // force node2 to map to null _ib.setName(iName).build().setChannelGroup("missing"); NetworkConfigurations networkConfigurations = NetworkConfigurations.of(ImmutableSortedMap.of(c1Name, c1, c2Name, c2)); Layer1Edge edge = new Layer1Edge(new Layer1Node(c1Name, iName), new Layer1Node(c2Name, iName)); // If node2 of a layer-1 physical edge cannot be mapped to a layer-1 logical node, a null edge // should be returned. assertThat(edge.toLogicalEdge(networkConfigurations), nullValue()); }
@Test public void resolve() { Configuration node1 = new Configuration("node1", ConfigurationFormat.CISCO_IOS); Configuration node2 = new Configuration("node2", ConfigurationFormat.CISCO_IOS); Interface iface11 = Interface.builder().setName("iface11").setOwner(node1).build(); Interface iface12 = Interface.builder().setName("iface12").setOwner(node1).build(); Interface iface2 = Interface.builder().setName("iface2").setOwner(node2).build(); node1.setInterfaces(ImmutableSortedMap.of("iface11", iface11, "iface12", iface12)); node2.setInterfaces(ImmutableSortedMap.of("iface2", iface2)); MockSpecifierContext ctxt = MockSpecifierContext.builder() .setConfigs(ImmutableMap.of("node1", node1, "node2", node2)) .build(); assertThat( new ShorthandInterfaceSpecifier(new InterfacesSpecifier("iface11")) .resolve(ImmutableSet.of("node1"), ctxt), equalTo(ImmutableSet.of(iface11))); } }
@Test public void testOspfAreaBuilder() { NetworkFactory nf = new NetworkFactory(); Configuration c = nf.configurationBuilder().setConfigurationFormat(CONFIG_FORMAT).build(); Vrf vrf = nf.vrfBuilder().setOwner(c).build(); OspfProcess.Builder ob = nf.ospfProcessBuilder(); OspfProcess ospfProcess = ob.setVrf(vrf).build(); OspfArea.Builder oab = nf.ospfAreaBuilder(); OspfArea oa1 = oab.build(); OspfArea oa2 = oab.setOspfProcess(ospfProcess).build(); Interface iface = nf.interfaceBuilder().setOwner(c).setActive(false).setVrf(vrf).setOspfArea(oa2).build(); assertThat(oa1.getAreaNumber(), not(equalTo(oa2.getAreaNumber()))); assertThat(oa1, not(sameInstance(oa2))); assertThat(ospfProcess.getAreas().get(oa2.getAreaNumber()), sameInstance(oa2)); assertThat(oa2, OspfAreaMatchers.hasInterfaces(hasItem(iface.getName()))); assertThat(iface.getOspfAreaName(), equalTo(oa2.getAreaNumber())); } }
public Interface toInterface(Configuration configuration, Warnings warnings) { String name = _canonicalName != null ? _canonicalName : _name; Interface.Builder iface = Interface.builder() .setName(name) .setOwner(configuration) .setAddresses(_address, _otherAddresses) .setBandwidth(_bandwidth) .setDeclaredNames(ImmutableSortedSet.of(_name)) .setProxyArp(false) .setVrf(configuration.getDefaultVrf()); if (_shared) { Ip publicIp = _address.getIp(); iface.setOutgoingTransformation(always().apply(assignSourceIp(publicIp, publicIp)).build()); } return iface.build(); } }
@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)))); }
@Test public void testToLogicalNodeAggregate() { String c1Name = "c1"; String iName = "i1"; String aName = "a1"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1); _ib.setName(iName).build().setChannelGroup(aName); _ib.setName(aName).build(); NetworkConfigurations networkConfigurations = NetworkConfigurations.of(ImmutableSortedMap.of(c1Name, c1)); Layer1Node node = new Layer1Node(c1Name, iName); // If node is member of an aggregate, the resulting logical node should be for the aggregate // interface. assertThat(node.toLogicalNode(networkConfigurations), equalTo(new Layer1Node(c1Name, aName))); }
@Test public void testToLogicalNodeNonAggregate() { String c1Name = "c1"; String iName = "i1"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1); _ib.setName(iName).build(); NetworkConfigurations networkConfigurations = NetworkConfigurations.of(ImmutableSortedMap.of(c1Name, c1)); Layer1Node node = new Layer1Node(c1Name, iName); // If node is not member of an aggregate, the resulting logical node should reference the // physical interface name. assertThat(node.toLogicalNode(networkConfigurations), equalTo(node)); } }
@Test public void testToLogicalNodeInactiveAggregate() { String c1Name = "c1"; String iName = "i1"; String aName = "a1"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1); _ib.setName(iName).build().setChannelGroup(aName); _ib.setName(aName).setActive(false).build(); NetworkConfigurations networkConfigurations = NetworkConfigurations.of(ImmutableSortedMap.of(c1Name, c1)); Layer1Node node = new Layer1Node(c1Name, iName); // If node is member of a disabled aggregate, the result should be null assertThat(node.toLogicalNode(networkConfigurations), nullValue()); }
@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_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 matchesCaseInsensitive() { Interface iface = Interface.builder() .setName("Loopback0") .setOwner(new Configuration("c", ConfigurationFormat.CISCO_IOS)) .setVrf(new Vrf("vrfA")) .build(); iface.setDescription("secrets are never secrets for long"); assertTrue(new InterfacesSpecifier("name:LoopBAck.*").matches(iface)); assertTrue(new InterfacesSpecifier("desc:SECrets.*").matches(iface)); assertTrue(new InterfacesSpecifier("type:Loopback").matches(iface)); assertTrue(new InterfacesSpecifier("vrf:vrfa").matches(iface)); }
@Test public void testToLogicalNodeMissingAggregate() { String c1Name = "c1"; String iName = "i1"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1); _ib.setName(iName).build().setChannelGroup("missing"); NetworkConfigurations networkConfigurations = NetworkConfigurations.of(ImmutableSortedMap.of(c1Name, c1)); Layer1Node node = new Layer1Node(c1Name, iName); // If node is member of a missing aggregate, the result should be null assertThat(node.toLogicalNode(networkConfigurations), nullValue()); }
public static Interface newInterface( String name, Configuration c, InterfaceAddress primaryAddress) { return FACTORY .interfaceBuilder() .setName(name) .setOwner(c) .setVrf(c.getDefaultVrf()) .setAddress(primaryAddress) .build(); }
@Test public void matchesType() { InterfacesSpecifier specifier = new InterfacesSpecifier("type:physical"); Configuration c = new Configuration("c", ConfigurationFormat.CISCO_IOS); Interface ifaceLoopback = Interface.builder().setName("Loopback0").setOwner(c).build(); Interface ifacePhysical = Interface.builder().setName("GigabitEthernet0").setOwner(c).build(); assertThat(specifier.matches(ifaceLoopback), equalTo(false)); assertThat(specifier.matches(ifacePhysical), equalTo(true)); }
@Before public void setup() { _nf = new NetworkFactory(); _cb = _nf.configurationBuilder() .setConfigurationFormat(ConfigurationFormat.CISCO_IOS) .setHostname(HOSTNAME); _c = _cb.build(); _vb = _nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME).setOwner(_c); _v = _vb.build(); _ib = _nf.interfaceBuilder().setVrf(_v).setOwner(_c).setName(INTERFACE_NAME); }