@Test public void testGetInterface() { Configuration c = new Configuration("foo", ConfigurationFormat.CISCO_IOS); Interface i = Interface.builder().setBandwidth(1e9).setName("eth0").build(); c.getAllInterfaces().put(i.getName(), i); NetworkConfigurations nc = NetworkConfigurations.of(ImmutableMap.of("foo", c)); assertThat(nc.getInterface("bar", "missingIface"), equalTo(Optional.empty())); assertThat(nc.getInterface("foo", "missingIface"), equalTo(Optional.empty())); assertThat(nc.getInterface("foo", "eth0").orElse(null), equalTo(i)); } }
@Test public void testBlacklisting() { Interface i = Interface.builder().setName("iface").build(); assertThat("Interface is not blacklisted", !i.getBlacklisted()); assertThat("Interface is not disabled", i.getActive()); i.blacklist(); assertThat("Interface is blacklisted", i.getBlacklisted()); assertThat("Interface is disabled", !i.getActive()); }
@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()); }
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 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))); } }
@Before public void setup() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); _c1 = cb.setHostname("c1").build(); _c2 = cb.setHostname("c2").build(); _aclb = nf.aclBuilder().setOwner(_c1); _aclb2 = nf.aclBuilder().setOwner(_c2); _c1.setIpSpaces(ImmutableSortedMap.of("ipSpace", Ip.parse("1.2.3.4").toIpSpace())); _c1.setInterfaces( ImmutableSortedMap.of( "iface", Interface.builder().setName("iface").build(), "iface2", Interface.builder().setName("iface2").build())); _c2.setInterfaces(ImmutableSortedMap.of("iface", Interface.builder().setName("iface").build())); }
@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()); }
@Before public void setup() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); _c1 = cb.setHostname("c1").build(); _c2 = cb.setHostname("c2").build(); _aclb = nf.aclBuilder().setOwner(_c1); _c1.setIpSpaces(ImmutableSortedMap.of("ipSpace", Ip.parse("1.2.3.4").toIpSpace())); _c1.setInterfaces( ImmutableSortedMap.of( "iface", Interface.builder().setName("iface").build(), "iface2", Interface.builder().setName("iface2").build())); _c2.setInterfaces(ImmutableSortedMap.of("iface", Interface.builder().setName("iface").build())); }
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 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 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 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()); }
@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)); }
public static Interface newInterface( String name, Configuration c, InterfaceAddress primaryAddress) { return FACTORY .interfaceBuilder() .setName(name) .setOwner(c) .setVrf(c.getDefaultVrf()) .setAddress(primaryAddress) .build(); }
/** 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 matchesVrf() { InterfacesSpecifier specifier = new InterfacesSpecifier("vrf:vrf1"); Interface vrf1Iface = Interface.builder().setName("iface").setVrf(new Vrf("vrf1")).build(); Interface vrf2Iface = Interface.builder().setName("iface").setVrf(new Vrf("vrf2")).build(); assertThat(specifier.matches(vrf1Iface), equalTo(true)); assertThat(specifier.matches(vrf2Iface), equalTo(false)); } }
@Before public void setup() { _nf = new NetworkFactory(); _cb = _nf.configurationBuilder() .setHostname(HOSTNAME) .setConfigurationFormat(ConfigurationFormat.CISCO_IOS); _ib = _nf.interfaceBuilder().setName(IFACE); _ab = _nf.aclBuilder().setName(ACLNAME); }
@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); }