@JsonCreator private static Configuration makeConfiguration( @JsonProperty(PROP_NAME) String hostname, @Nullable @JsonProperty(PROP_CONFIGURATION_FORMAT) ConfigurationFormat configurationFormat) { if (configurationFormat == null) { throw new BatfishException("Configuration format cannot be null"); } return new Configuration(hostname, configurationFormat); }
@Override public List<Configuration> toVendorIndependentConfigurations() throws VendorConversionException { _c = new Configuration(_hostname, _vendor); _c.setDefaultCrossZoneAction(LineAction.PERMIT); _c.setDefaultInboundAction(LineAction.PERMIT); return ImmutableList.of(_c); } }
/** Non-string objects should be mapped to their string forms when the schema is String */ @Test public void convertTypeIfNeededNonStringToString() { Object propertyValueObject = PropertySpecifier.convertTypeIfNeeded( new Configuration("cname", ConfigurationFormat.CISCO_IOS), Schema.STRING); assertThat(propertyValueObject, equalTo("cname")); }
@Override public Configuration build() { String name = _hostname != null ? _hostname : generateName(); Configuration configuration = new Configuration(name, _configurationFormat); if (_defaultCrossZoneAction != null) { configuration.setDefaultCrossZoneAction(_defaultCrossZoneAction); } if (_defaultInboundAction != null) { configuration.setDefaultInboundAction(_defaultInboundAction); } configuration.setDomainName(_domainName); return configuration; }
private static Configuration createTestConfiguration( String nodeName, ConfigurationFormat configFormat, String... interfaceNames) { Configuration config = new Configuration(nodeName, configFormat); for (String interfaceName : interfaceNames) { config.getAllInterfaces().put(interfaceName, new Interface(interfaceName, config)); } return config; }
/** Collection of non-strings should be converted to collection of strings when desired */ @Test public void convertTypeIfNeededNonStringCollectionToStringCollection() { Object propertyValueCollection = PropertySpecifier.convertTypeIfNeeded( ImmutableList.of(new Configuration("cname", ConfigurationFormat.CISCO_IOS)), Schema.list(Schema.STRING)); assertThat(propertyValueCollection, equalTo(ImmutableList.of("cname"))); }
@Test public void testGetVrf() { Configuration c = new Configuration("foo", ConfigurationFormat.CISCO_IOS); c.getVrfs().put("fooVRF", new Vrf("fooVRF")); NetworkConfigurations nc = NetworkConfigurations.of(ImmutableMap.of("foo", c)); // Missing values are null assertThat(nc.getVrf("missingHostname", "missingVRF"), equalTo(Optional.empty())); assertThat(nc.getVrf("missingHostname", "fooVRF"), equalTo(Optional.empty())); assertThat(nc.getVrf("foo", "missingVRF"), equalTo(Optional.empty())); // Actual get succeeds assertThat("VRF is present", nc.getVrf("foo", "fooVRF").isPresent()); }
@Override public SpecifierContext specifierContext() { Configuration c1 = new Configuration("h", ConfigurationFormat.CISCO_IOS); Configuration c2 = new Configuration("h2", ConfigurationFormat.CISCO_IOS); return MockSpecifierContext.builder() .setConfigs(ImmutableSortedMap.of("h", c1, "h2", c2)) .build(); } }
@Override public SpecifierContext specifierContext() { Configuration c1 = new Configuration("h", ConfigurationFormat.CISCO_IOS); Configuration c2 = new Configuration("h2", ConfigurationFormat.CISCO_IOS); return MockSpecifierContext.builder() .setConfigs(ImmutableSortedMap.of("h", c1, "h2", c2)) .build(); } }
@Test public void roundTripConfigurationsSucceeds() { NetworkId network = new NetworkId("network"); SnapshotId snapshot = new SnapshotId("snapshot"); Map<String, Configuration> configs = new HashMap<>(); configs.put("node1", new Configuration("node1", ConfigurationFormat.CISCO_IOS)); _storage.storeConfigurations( configs, new ConvertConfigurationAnswerElement(), network, snapshot); Map<String, Configuration> deserialized = _storage.loadConfigurations(network, snapshot); assertThat(deserialized, not(nullValue())); assertThat(deserialized.keySet(), equalTo(Sets.newHashSet("node1"))); }
@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 fillPropertyListEmpty() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); String property = NodePropertySpecifier.NTP_SERVERS; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with an empty list assertThat(row.build(), equalTo(Row.builder().put(property, new LinkedList<String>()).build())); }
@Test public void fillPropertyNull() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); String property = NodePropertySpecifier.NTP_SOURCE_INTERFACE; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with null and the schemas shouldn't be assertThat(row.build(), equalTo(Row.builder().put(property, null).build())); } }
@Test public void testEvaluate() { DisjunctionChain dc = new DisjunctionChain(ImmutableList.of(BooleanExprs.FALSE, BooleanExprs.TRUE)); // Test that OR is evaluated correctly assertThat( dc.evaluate( Environment.builder(new Configuration("host", ConfigurationFormat.JUNIPER)) .setVrf(Configuration.DEFAULT_VRF_NAME) .build()) .getBooleanValue(), equalTo(true)); }
@Test public void fillPropertyListNonEmpty() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setNtpServers(ImmutableSortedSet.of("sa", "sb")); String property = NodePropertySpecifier.NTP_SERVERS; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with the right list and the schemas map should be List<String> assertThat( row.build(), equalTo(Row.builder().put(property, ImmutableList.of("sa", "sb")).build())); }
@Test public void fillPropertyMap() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setInterfaces(ImmutableSortedMap.of("i1", new Interface("i1"))); String property = NodePropertySpecifier.INTERFACES; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with the right list and the schemas map should be List<String> assertThat(row.build(), equalTo(Row.builder().put(property, ImmutableList.of("i1")).build())); }
private static Environment newTestEnvironment(BgpRoute.Builder outputRoute) { Configuration c = new Configuration("host", ConfigurationFormat.CISCO_IOS); return Environment.builder(c).setVrf("vrf").setOutputRoute(outputRoute).build(); }
@Test public void testEvaluate() { ConjunctionChain cc = new ConjunctionChain(ImmutableList.of(BooleanExprs.FALSE, BooleanExprs.TRUE)); // Test that AND is evaluated correctly assertThat( cc.evaluate( Environment.builder(new Configuration("host", ConfigurationFormat.JUNIPER)) .setVrf(Configuration.DEFAULT_VRF_NAME) .build()) .getBooleanValue(), equalTo(false)); }
@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)); }
private static boolean evaluateIsClassful(Prefix network) { Configuration c = new Configuration("host", ConfigurationFormat.CISCO_IOS); ConnectedRoute route = new ConnectedRoute(network, "Ethernet0"); Environment env = Environment.builder(c).setVrf("vrf").setOriginalRoute(route).build(); Result res = RouteIsClassful.instance().evaluate(env); return res.getBooleanValue(); }