@Test public void testGetDynamicPeerRowTwoCompatiblePeers() { Ip localIp = Ip.parse("1.1.1.1"); Prefix localAddress = localIp.toPrefix(); Prefix remotePrefix = Prefix.parse("2.2.2.0/24"); LongSpace remoteAsns = LongSpace.of(Range.closed(2L, 3L)); BgpPeerConfigId peerId = new BgpPeerConfigId("c1", "vrf1", remotePrefix, true); BgpPassivePeerConfig peer = BgpPassivePeerConfig.builder() .setLocalIp(Ip.AUTO) .setPeerPrefix(remotePrefix) .setLocalAs(1L) .setRemoteAsns(remoteAsns) .setIpv4UnicastAddressFamily(Ipv4UnicastAddressFamily.builder().build()) .build(); Ip remote1Ip = Ip.parse("2.2.2.1"); BgpPeerConfigId remote1Id = new BgpPeerConfigId("c2", "vrf2", localAddress, false); BgpActivePeerConfig remote1 = BgpActivePeerConfig.builder() .setLocalIp(remote1Ip) .setPeerAddress(localIp) .setLocalAs(2L) .setRemoteAs(1L) .setIpv4UnicastAddressFamily(Ipv4UnicastAddressFamily.builder().build()) .build(); Ip remote2Ip = Ip.parse("2.2.2.2"); BgpPeerConfigId remote2Id = new BgpPeerConfigId("c3", "vrf3", localAddress, false); BgpActivePeerConfig remote2 = BgpActivePeerConfig.builder() .setLocalIp(remote2Ip) .setPeerAddress(localIp) .setLocalAs(3L) .setRemoteAs(1L) .setIpv4UnicastAddressFamily(Ipv4UnicastAddressFamily.builder().build()) .build(); MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredTopology = ValueGraphBuilder.directed().allowsSelfLoops(false).build(); configuredTopology.putEdgeValue( peerId, remote1Id, BgpSessionProperties.from(remote1, peer, true)); configuredTopology.putEdgeValue( remote1Id, peerId, BgpSessionProperties.from(remote1, peer, false)); configuredTopology.putEdgeValue( peerId, remote2Id, BgpSessionProperties.from(remote2, peer, true)); configuredTopology.putEdgeValue( remote2Id, peerId, BgpSessionProperties.from(remote2, peer, false)); MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> establishedTopology = ValueGraphBuilder.directed().allowsSelfLoops(false).build(); establishedTopology.putEdgeValue( peerId, remote1Id, BgpSessionProperties.from(remote1, peer, true)); establishedTopology.putEdgeValue( remote1Id, peerId, BgpSessionProperties.from(remote1, peer, false)); NetworkConfigurations nc = NetworkConfigurations.of( createConfigurations( ImmutableList.of(remote1Id, remote2Id), ImmutableList.of(remote1, remote2))); List<Row> rows = getPassivePeerRows(peerId, peer, nc, configuredTopology, establishedTopology); Row.RowBuilder expectedRowBuilder = Row.builder() .put(COL_LOCAL_AS, 1L) .put(COL_LOCAL_IP, localIp) .put(COL_NODE, new Node("c1")) .put(COL_SESSION_TYPE, SessionType.EBGP_SINGLEHOP) .put(COL_VRF, "vrf1") .put(COL_LOCAL_INTERFACE, null) .put(COL_REMOTE_INTERFACE, null) .put(COL_ADDRESS_FAMILIES, ImmutableSet.of(Type.IPV4_UNICAST)); Row expected1 = expectedRowBuilder .put(COL_ESTABLISHED_STATUS, BgpSessionStatus.ESTABLISHED) .put(COL_REMOTE_AS, "2") .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, remote1Ip)) .put(COL_REMOTE_NODE, new Node("c2")) .build(); Row expected2 = expectedRowBuilder .put(COL_ESTABLISHED_STATUS, BgpSessionStatus.NOT_ESTABLISHED) .put(COL_REMOTE_AS, "3") .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, remote2Ip)) .put(COL_REMOTE_NODE, new Node("c3")) .build(); assertThat(rows, containsInAnyOrder(expected1, expected2)); }
@Nonnull @VisibleForTesting static List<Row> getPassivePeerRows( BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, NetworkConfigurations nc, ValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredTopology, ValueGraph<BgpPeerConfigId, BgpSessionProperties> establishedTopology) { Row.TypedRowBuilder rb = Row.builder(METADATA_MAP) .put(COL_ADDRESS_FAMILIES, ImmutableSet.of()) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, passivePeer.getRemoteAsns().toString()) .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()); if (getLocallyBrokenStatus(passivePeer) != null) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_COMPATIBLE).build()); } Set<BgpPeerConfigId> compatibleRemotes = configuredTopology.adjacentNodes(passiveId); if (compatibleRemotes.isEmpty()) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_ESTABLISHED).build()); } Set<BgpPeerConfigId> establishedRemotes = establishedTopology.nodes().contains(passiveId) ? establishedTopology.adjacentNodes(passiveId) : ImmutableSet.of(); return compatibleRemotes.stream() .map( remoteId -> { BgpSessionProperties sessionProps = configuredTopology.edgeValue(passiveId, remoteId).orElse(null); assert sessionProps != null; BgpActivePeerConfig activeRemote = nc.getBgpPointToPointPeerConfig(remoteId); assert activeRemote != null; BgpSessionStatus status = establishedRemotes.contains(remoteId) ? ESTABLISHED : NOT_ESTABLISHED; return rb.put(COL_ESTABLISHED_STATUS, status) .put(COL_ADDRESS_FAMILIES, sessionProps.getAddressFamilies()) .put(COL_LOCAL_IP, sessionProps.getTailIp()) .put(COL_LOCAL_AS, sessionProps.getTailAs()) .put(COL_REMOTE_AS, Long.toString(sessionProps.getHeadAs())) .put(COL_REMOTE_NODE, new Node(remoteId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, sessionProps.getHeadIp())) .put(COL_SESSION_TYPE, sessionProps.getSessionType()) .build(); }) .collect(ImmutableList.toImmutableList()); }
BgpSessionStatusAnswerer extends Answerer { @Nonnull @VisibleForTesting static List<Row> getPassivePeerRows( BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, NetworkConfigurations nc, ValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredTopology, ValueGraph<BgpPeerConfigId, BgpSessionProperties> establishedTopology) { Row.TypedRowBuilder rb = Row.builder(METADATA_MAP) .put(COL_ADDRESS_FAMILIES, ImmutableSet.of()) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, passivePeer.getRemoteAsns().toString()) .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()); if (getLocallyBrokenStatus(passivePeer) != null) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_COMPATIBLE).build()); } Set<BgpPeerConfigId> compatibleRemotes = configuredTopology.adjacentNodes(passiveId); if (compatibleRemotes.isEmpty()) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_ESTABLISHED).build()); } Set<BgpPeerConfigId> establishedRemotes = establishedTopology.nodes().contains(passiveId) ? establishedTopology.adjacentNodes(passiveId) : ImmutableSet.of(); return compatibleRemotes.stream() .map( remoteId -> { BgpSessionProperties sessionProps = configuredTopology.edgeValue(passiveId, remoteId).orElse(null); assert sessionProps != null; BgpActivePeerConfig activeRemote = nc.getBgpPointToPointPeerConfig(remoteId); assert activeRemote != null; BgpSessionStatus status = establishedRemotes.contains(remoteId) ? ESTABLISHED : NOT_ESTABLISHED; return rb.put(COL_ESTABLISHED_STATUS, status) .put(COL_ADDRESS_FAMILIES, sessionProps.getAddressFamilies()) .put(COL_LOCAL_IP, sessionProps.getTailIp()) .put(COL_LOCAL_AS, sessionProps.getTailAs()) .put(COL_REMOTE_AS, Long.toString(sessionProps.getHeadAs())) .put(COL_REMOTE_NODE, new Node(remoteId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, sessionProps.getHeadIp())) .put(COL_SESSION_TYPE, sessionProps.getSessionType()) .build(); }) .collect(ImmutableList.toImmutableList()); } }
BgpSessionStatusAnswerer extends Answerer { @Nonnull @VisibleForTesting static List<Row> getPassivePeerRows( BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, NetworkConfigurations nc, ValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredTopology, ValueGraph<BgpPeerConfigId, BgpSessionProperties> establishedTopology) { Row.TypedRowBuilder rb = Row.builder(METADATA_MAP) .put(COL_ADDRESS_FAMILIES, ImmutableSet.of()) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, passivePeer.getRemoteAsns().toString()) .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()); if (getLocallyBrokenStatus(passivePeer) != null) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_COMPATIBLE).build()); } Set<BgpPeerConfigId> compatibleRemotes = configuredTopology.adjacentNodes(passiveId); if (compatibleRemotes.isEmpty()) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_ESTABLISHED).build()); } Set<BgpPeerConfigId> establishedRemotes = establishedTopology.nodes().contains(passiveId) ? establishedTopology.adjacentNodes(passiveId) : ImmutableSet.of(); return compatibleRemotes.stream() .map( remoteId -> { BgpSessionProperties sessionProps = configuredTopology.edgeValue(passiveId, remoteId).orElse(null); assert sessionProps != null; BgpActivePeerConfig activeRemote = nc.getBgpPointToPointPeerConfig(remoteId); assert activeRemote != null; BgpSessionStatus status = establishedRemotes.contains(remoteId) ? ESTABLISHED : NOT_ESTABLISHED; return rb.put(COL_ESTABLISHED_STATUS, status) .put(COL_ADDRESS_FAMILIES, sessionProps.getAddressFamilies()) .put(COL_LOCAL_IP, sessionProps.getTailIp()) .put(COL_LOCAL_AS, sessionProps.getTailAs()) .put(COL_REMOTE_AS, Long.toString(sessionProps.getHeadAs())) .put(COL_REMOTE_NODE, new Node(remoteId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, sessionProps.getHeadIp())) .put(COL_SESSION_TYPE, sessionProps.getSessionType()) .build(); }) .collect(ImmutableList.toImmutableList()); } BgpSessionStatusAnswerer(Question question, IBatfish batfish); }
BgpSessionStatusAnswerer extends Answerer { @Nonnull @VisibleForTesting static List<Row> getPassivePeerRows( BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, NetworkConfigurations nc, ValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredTopology, ValueGraph<BgpPeerConfigId, BgpSessionProperties> establishedTopology) { Row.TypedRowBuilder rb = Row.builder(METADATA_MAP) .put(COL_ADDRESS_FAMILIES, ImmutableSet.of()) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, passivePeer.getRemoteAsns().toString()) .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()); if (getLocallyBrokenStatus(passivePeer) != null) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_COMPATIBLE).build()); } Set<BgpPeerConfigId> compatibleRemotes = configuredTopology.adjacentNodes(passiveId); if (compatibleRemotes.isEmpty()) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_ESTABLISHED).build()); } Set<BgpPeerConfigId> establishedRemotes = establishedTopology.nodes().contains(passiveId) ? establishedTopology.adjacentNodes(passiveId) : ImmutableSet.of(); return compatibleRemotes.stream() .map( remoteId -> { BgpSessionProperties sessionProps = configuredTopology.edgeValue(passiveId, remoteId).orElse(null); assert sessionProps != null; BgpActivePeerConfig activeRemote = nc.getBgpPointToPointPeerConfig(remoteId); assert activeRemote != null; BgpSessionStatus status = establishedRemotes.contains(remoteId) ? ESTABLISHED : NOT_ESTABLISHED; return rb.put(COL_ESTABLISHED_STATUS, status) .put(COL_ADDRESS_FAMILIES, sessionProps.getAddressFamilies()) .put(COL_LOCAL_IP, sessionProps.getTailIp()) .put(COL_LOCAL_AS, sessionProps.getTailAs()) .put(COL_REMOTE_AS, Long.toString(sessionProps.getHeadAs())) .put(COL_REMOTE_NODE, new Node(remoteId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, sessionProps.getHeadIp())) .put(COL_SESSION_TYPE, sessionProps.getSessionType()) .build(); }) .collect(ImmutableList.toImmutableList()); } BgpSessionStatusAnswerer(Question question, IBatfish batfish); @Override AnswerElement answer(NetworkSnapshot snapshot); }
BgpSessionStatusAnswerer extends Answerer { @Nonnull @VisibleForTesting static List<Row> getPassivePeerRows( BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, NetworkConfigurations nc, ValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredTopology, ValueGraph<BgpPeerConfigId, BgpSessionProperties> establishedTopology) { Row.TypedRowBuilder rb = Row.builder(METADATA_MAP) .put(COL_ADDRESS_FAMILIES, ImmutableSet.of()) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, passivePeer.getRemoteAsns().toString()) .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()); if (getLocallyBrokenStatus(passivePeer) != null) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_COMPATIBLE).build()); } Set<BgpPeerConfigId> compatibleRemotes = configuredTopology.adjacentNodes(passiveId); if (compatibleRemotes.isEmpty()) { return ImmutableList.of(rb.put(COL_ESTABLISHED_STATUS, NOT_ESTABLISHED).build()); } Set<BgpPeerConfigId> establishedRemotes = establishedTopology.nodes().contains(passiveId) ? establishedTopology.adjacentNodes(passiveId) : ImmutableSet.of(); return compatibleRemotes.stream() .map( remoteId -> { BgpSessionProperties sessionProps = configuredTopology.edgeValue(passiveId, remoteId).orElse(null); assert sessionProps != null; BgpActivePeerConfig activeRemote = nc.getBgpPointToPointPeerConfig(remoteId); assert activeRemote != null; BgpSessionStatus status = establishedRemotes.contains(remoteId) ? ESTABLISHED : NOT_ESTABLISHED; return rb.put(COL_ESTABLISHED_STATUS, status) .put(COL_ADDRESS_FAMILIES, sessionProps.getAddressFamilies()) .put(COL_LOCAL_IP, sessionProps.getTailIp()) .put(COL_LOCAL_AS, sessionProps.getTailAs()) .put(COL_REMOTE_AS, Long.toString(sessionProps.getHeadAs())) .put(COL_REMOTE_NODE, new Node(remoteId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, sessionProps.getHeadIp())) .put(COL_SESSION_TYPE, sessionProps.getSessionType()) .build(); }) .collect(ImmutableList.toImmutableList()); } BgpSessionStatusAnswerer(Question question, IBatfish batfish); @Override AnswerElement answer(NetworkSnapshot snapshot);  }