/** Associate VRFs with hostnames of configs on which they sit */ @VisibleForTesting static Map<Vrf, String> initVrfHostnameMap(Map<String, Configuration> configurations) { Map<Vrf, String> vrfHostnames = new IdentityHashMap<>(); for (Configuration c : configurations.values()) { for (Vrf v : c.getVrfs().values()) { vrfHostnames.put(v, c.getHostname()); } } // Use unmodifiableMap to lock the implementation return Collections.unmodifiableMap(vrfHostnames); }
@Override protected Map<String, Vrf> featureValueOf(Configuration actual) { return actual.getVrfs(); } }
/** Returns the lowest IP across all interfaces for now. We'll improve it later. */ @JsonIgnore @Nullable public Ip getCanonicalIp() { return getVrfs().values().stream() .flatMap(v -> v.getInterfaces().values().stream()) .flatMap(i -> i.getAllAddresses().stream()) .map(InterfaceAddress::getIp) .min(Ip::compareTo) .orElse(null); }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { return nodes.stream() .map(n -> ctxt.getConfigs().get(n).getVrfs().values()) .flatMap(Collection::stream) // we have a stream of VRFs now .filter(v -> _pattern.matcher(v.getName()).matches()) .map(v -> v.getInterfaces().values()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } }
private Map<String, Set<String>> computeEnabledVrfs() { return toImmutableMap( _enabledNodes, Function.identity(), hostname -> { Set<String> disabledVrfs = _disabledVrfs.get(hostname); return _configurations.get(hostname).getVrfs().keySet().stream() .filter(vrfName -> disabledVrfs == null || !disabledVrfs.contains(vrfName)) .collect(ImmutableSet.toImmutableSet()); }); }
private void postProcessAggregatedInterfaces(Map<String, Configuration> configurations) { configurations .values() .forEach( c -> c.getVrfs() .values() .forEach(v -> postProcessAggregatedInterfacesHelper(v.getInterfaces()))); }
@Override public Set<Location> resolve(SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .flatMap(node -> node.getVrfs().values().stream()) .filter(vrf -> _pattern.matcher(vrf.getName()).matches()) .flatMap(this::getVrfLocations) .collect(ImmutableSet.toImmutableSet()); } }
/** Associate VNIs with VRFs that have {@link VniSettings} mentioning them */ @VisibleForTesting static Map<Integer, List<Vrf>> initVniVrfAssociations(Map<String, Configuration> configurations) { Map<Integer, ImmutableList.Builder<Vrf>> vrfsByVni = new HashMap<>(); for (Configuration c : configurations.values()) { for (Vrf v : c.getVrfs().values()) { for (VniSettings vniSettings : v.getVniSettings().values()) { vrfsByVni.computeIfAbsent(vniSettings.getVni(), vni -> ImmutableList.builder()).add(v); } } } return toImmutableMap( vrfsByVni, Entry::getKey, vrfsByVniEntry -> vrfsByVniEntry.getValue().build()); }
@VisibleForTesting static void addVlanVnis( Configuration c, IntegerSpace vlans, Map<Integer, ImmutableSet.Builder<NodeInterfacePair>> switchedVlanInterfaces, ImmutableMap.Builder<Integer, Integer> vlanVnisBuilder) { c.getVrfs() .values() .forEach( vrf -> vrf.getVniSettings().values().stream() .forEach( vniSettings -> tryAddVlanVni( vniSettings, vlans, vlanVnisBuilder, switchedVlanInterfaces))); }
VirtualRouter(@Nonnull final String name, @Nonnull final Node node) { _node = node; _c = node.getConfiguration(); _name = name; _vrf = _c.getVrfs().get(name); initRibs(); _bgpIncomingRoutes = new TreeMap<>(); _prefixTracer = new PrefixTracer(); _virtualEigrpProcesses = ImmutableMap.of(); }
public Environment build() { Vrf vrf = _configuration.getVrfs().get(_vrf); return new Environment( _buffered, _callExprContext, _callStatementContext, _configuration, _defaultAction, _defaultPolicy, _direction, _error, _intermediateBgpAttributes, _localDefaultAction, _originalRoute, _originalRoute6, _outputRoute, _peerAddress, _peerPrefix, _readFromIntermediateBgpAttributes, vrf, _writeToIntermediateBgpAttributes); } }
@Override public Vrf build() { String name = _name != null ? _name : generateName(); Vrf vrf = new Vrf(name); if (_owner != null) { _owner.getVrfs().put(name, vrf); } return vrf; }
private void postProcessOspfCosts(Map<String, Configuration> configurations) { configurations .values() .forEach( c -> c.getVrfs() .values() .forEach( vrf -> { // Compute OSPF interface costs where they are missing OspfProcess proc = vrf.getOspfProcess(); if (proc != null) { proc.initInterfaceCosts(c); } })); }
@Override protected Vrf featureValueOf(Configuration actual) { return actual.getVrfs().get(_name); } }
/** * Create a new node based on the configuration. Initializes virtual routers based on {@link * Configuration} VRFs. * * @param configuration the {@link Configuration} backing this node */ public Node(@Nonnull Configuration configuration) { _c = configuration; ImmutableSortedMap.Builder<String, VirtualRouter> b = ImmutableSortedMap.naturalOrder(); for (String vrfName : _c.getVrfs().keySet()) { VirtualRouter vr = new VirtualRouter(vrfName, this); b.put(vrfName, vr); } _virtualRouters = b.build(); }
@VisibleForTesting void processIsisInterfaceSettings( RoutingInstance routingInstance, boolean level1, boolean level2) { _c.getVrfs() .get(routingInstance.getName()) .getInterfaces() .forEach( (ifaceName, newIface) -> newIface.setIsis( toIsisInterfaceSettings( routingInstance.getIsisSettings(), routingInstance.getInterfaces().get(ifaceName), level1, level2))); }
@VisibleForTesting public static Multiset<Row> getAnswerRows( Map<String, Configuration> configurations, Set<String> nodes, Map<String, ColumnMetadata> columnMetadata, BgpPeerPropertySpecifier propertySpecifier) { Multiset<Row> rows = HashMultiset.create(); for (String nodeName : nodes) { for (Vrf vrf : configurations.get(nodeName).getVrfs().values()) { BgpProcess bgpProcess = vrf.getBgpProcess(); if (bgpProcess == null) { continue; } Node node = new Node(nodeName); for (BgpActivePeerConfig peer : bgpProcess.getActiveNeighbors().values()) { rows.add(getRow(node, vrf.getName(), peer, columnMetadata, propertySpecifier)); } for (BgpPassivePeerConfig peer : bgpProcess.getPassiveNeighbors().values()) { rows.add(getRow(node, vrf.getName(), peer, columnMetadata, propertySpecifier)); } } } return rows; }
private static SortedSet<VerboseEigrpEdge> getEigrpEdges( Map<String, Configuration> configs, Topology topology) { Network<EigrpInterface, EigrpEdge> eigrpTopology = EigrpTopology.initEigrpTopology(configs, topology); NetworkConfigurations nc = NetworkConfigurations.of(configs); SortedSet<VerboseEigrpEdge> eigrpEdges = new TreeSet<>(); for (Configuration c : configs.values()) { String hostname = c.getHostname(); for (Vrf vrf : c.getVrfs().values()) { eigrpEdges.addAll( vrf.getInterfaceNames().stream() .map(ifaceName -> new EigrpInterface(hostname, ifaceName, vrf.getName())) .filter(eigrpTopology.nodes()::contains) .flatMap(n -> eigrpTopology.inEdges(n).stream()) .map(edge -> new VerboseEigrpEdge(edge, edge.toIpEdge(nc))) .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()))); } } return eigrpEdges; }
@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()); }
@Test public void testVrfBuilder() { NetworkFactory nf = new NetworkFactory(); Vrf.Builder vb = nf.vrfBuilder(); Vrf v1 = vb.build(); Vrf v2 = vb.build(); Configuration c = nf.configurationBuilder().setConfigurationFormat(CONFIG_FORMAT).build(); vb.setOwner(c); Vrf v3 = vb.build(); assertThat(v1.getName(), is(notNullValue())); assertThat(v2.getName(), is(notNullValue())); assertThat(v1.getName(), not(equalTo(v2.getName()))); assertThat(c.getVrfs(), not(hasKey(v1.getName()))); assertThat(c.getVrfs(), not(hasKey(v2.getName()))); assertThat(c.getVrfs(), hasKey(v3.getName())); }