Skip to content

Commit

Permalink
Merge pull request #136 from batfish/unstable
Browse files Browse the repository at this point in the history
Unstable
  • Loading branch information
arifogel committed May 26, 2017
2 parents d9478e8 + 49ad278 commit 8a7b14c
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.batfish.datamodel;

import org.batfish.common.Pair;
import org.batfish.datamodel.collections.VerboseNodeInterfacePair;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class VerboseEdge
extends Pair<VerboseNodeInterfacePair, VerboseNodeInterfacePair> {
private static final String EDGE_VAR = "edgesummary";

private static final String INT1_VAR = "node1interface";

private static final String INT2_VAR = "node2interface";

private static final String NODE1_VAR = "node1";

private static final String NODE2_VAR = "node2";

private static final long serialVersionUID = 1L;

protected final Edge edge;

@JsonCreator
public VerboseEdge(@JsonProperty(NODE1_VAR) Configuration node1,
@JsonProperty(INT1_VAR) Interface int1,
@JsonProperty(NODE2_VAR) Configuration node2,
@JsonProperty(INT2_VAR) Interface int2,
@JsonProperty(EDGE_VAR) Edge e) {
this(new VerboseNodeInterfacePair(node1, int1),
new VerboseNodeInterfacePair(node2, int2), e);
}

public VerboseEdge(VerboseNodeInterfacePair p1, VerboseNodeInterfacePair p2,
Edge e) {
super(p1, p2);
this.edge = e;
}

@JsonProperty(EDGE_VAR)
public Edge getEdgeSummary() {
return edge;
}

@JsonProperty(INT1_VAR)
public Interface getInt1() {
return _first.getInterface();
}

@JsonProperty(INT2_VAR)
public Interface getInt2() {
return _second.getInterface();
}

@JsonIgnore
public VerboseNodeInterfacePair getInterface1() {
return _first;
}

@JsonIgnore
public VerboseNodeInterfacePair getInterface2() {
return _second;
}

@JsonProperty(NODE1_VAR)
public Configuration getNode1() {
return _first.getHost();
}

@JsonProperty(NODE2_VAR)
public Configuration getNode2() {
return _second.getHost();
}

@Override
public String toString() {
return "<" + getNode1() + ":" + getInt1() + ", " + getNode2() + ":"
+ getInt2() + ">";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.batfish.datamodel.collections;

import org.batfish.common.Pair;
import org.batfish.datamodel.Configuration;
import org.batfish.datamodel.Interface;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class VerboseNodeInterfacePair extends Pair<Configuration, Interface> {

private static final String HOST_VAR = "host";

private static final String INTERFACE_VAR = "interface";
/**
*
*/
private static final long serialVersionUID = 1L;

@JsonCreator
public VerboseNodeInterfacePair(@JsonProperty(HOST_VAR) Configuration node,
@JsonProperty(INTERFACE_VAR) Interface iface) {
super(node, iface);
}

@JsonProperty(HOST_VAR)
public Configuration getHost() {
return _first;
}

@JsonProperty(INTERFACE_VAR)
public Interface getInterface() {
return _second;
}

@Override
public String toString() {
return _first + ":" + _second;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.batfish.datamodel.BgpProcess;
import org.batfish.datamodel.Configuration;
import org.batfish.datamodel.Edge;
import org.batfish.datamodel.Interface;
import org.batfish.datamodel.VerboseEdge;
import org.batfish.datamodel.Ip;
import org.batfish.datamodel.NeighborType;
import org.batfish.datamodel.OspfNeighbor;
Expand All @@ -38,6 +40,8 @@ public static class NeighborsAnswerElement implements AnswerElement {

private final static String OSPF_NEIGHBORS_VAR = "ospfNeighbors";

private final static String VERBOSE_LAN_NEIGHBORS_VAR = "verboseLanNeighbors";

private SortedSet<IpEdge> _ebgpNeighbors;

private SortedSet<IpEdge> _ibgpNeighbors;
Expand All @@ -46,6 +50,8 @@ public static class NeighborsAnswerElement implements AnswerElement {

private SortedSet<IpEdge> _ospfNeighbors;

private SortedSet<VerboseEdge> _verboseLanNeighbors;

public void addLanEdge(Edge edge) {
_lanNeighbors.add(edge);
}
Expand All @@ -70,6 +76,11 @@ public SortedSet<IpEdge> getOspfNeighbors() {
return _ospfNeighbors;
}

@JsonProperty(VERBOSE_LAN_NEIGHBORS_VAR)
public SortedSet<VerboseEdge> getVerboseLanNeighbors() {
return _verboseLanNeighbors;
}

public void initEbgpNeighbors() {
_ebgpNeighbors = new TreeSet<>();
}
Expand All @@ -86,6 +97,10 @@ public void initOspfNeighbors() {
_ospfNeighbors = new TreeSet<>();
}

public void initVerboseLanNeighbors() {
_verboseLanNeighbors = new TreeSet<>();
}

@Override
public String prettyPrint() {
StringBuilder sb = new StringBuilder("Results for neighbors\n");
Expand All @@ -97,6 +112,13 @@ public String prettyPrint() {
}
}

if (_verboseLanNeighbors != null) {
sb.append(" LAN neighbors\n");
for (VerboseEdge edge : _verboseLanNeighbors) {
sb.append(" " + edge.toString() + "\n");
}
}

if (_ebgpNeighbors != null) {
sb.append(" eBGP Neighbors\n");
for (IpEdge ipEdge : _ebgpNeighbors) {
Expand Down Expand Up @@ -141,6 +163,12 @@ public void setOspfNeighbors(SortedSet<IpEdge> ospfNeighbors) {
_ospfNeighbors = ospfNeighbors;
}

@JsonProperty(VERBOSE_LAN_NEIGHBORS_VAR)
public void setVerboseLanNeighbors(
SortedSet<VerboseEdge> verboseLanNeighbors) {
_verboseLanNeighbors = verboseLanNeighbors;
}

}

public static class NeighborsAnswerer extends Answerer {
Expand Down Expand Up @@ -311,14 +339,28 @@ public AnswerElement answer() {

if (question.getNeighborTypes().isEmpty()
|| question.getNeighborTypes().contains(NeighborType.LAN)) {
answerElement.initLanNeighbors();
initTopology(configurations);
SortedSet<Edge> matchingEdges = new TreeSet<>();
for (Edge edge : _topology.getEdges()) {
Matcher node1Matcher = node1Regex.matcher(edge.getNode1());
Matcher node2Matcher = node2Regex.matcher(edge.getNode2());
if (node1Matcher.matches() && node2Matcher.matches()) {
answerElement.addLanEdge(edge);
matchingEdges.add(edge);
}
}
if (!question.getVerbose()) {
answerElement.setLanNeighbors(matchingEdges);
}
else {
SortedSet<VerboseEdge> vMatchingEdges = new TreeSet<>();
for (Edge edge : matchingEdges) {
Configuration n1 = configurations.get(edge.getNode1());
Interface i1 = n1.getInterfaces().get(edge.getInt1());
Configuration n2 = configurations.get(edge.getNode2());
Interface i2 = n2.getInterfaces().get(edge.getInt2());
vMatchingEdges.add(new VerboseEdge(n1, i1, n2, i2, edge));
}
answerElement.setVerboseLanNeighbors(vMatchingEdges);
}
}

Expand Down Expand Up @@ -370,6 +412,11 @@ private void initTopology(Map<String, Configuration> configurations) {
* @param node2Regex
* Regular expression to match the nodes names for the other end of
* the pair. Default is '.*' (all nodes).
* @param verbose
* Boolean indicating whether full information about the
* nodes/interfaces that make up each neighbor pair is requested.
* Default is false, indicating that only the names of
* nodes/interfaces is returned.
*
* @example bf_answer("Neighbors", neighborType=["ebgp", "ibgp"]
* node1Regex="as1.*", node2Regex="as2.*") Shows all eBGP and iBGP
Expand All @@ -385,16 +432,21 @@ public static class NeighborsQuestion extends Question {

private static final String NODE2_REGEX_VAR = "node2Regex";

private static final String VERBOSE_VAR = "verbose";

private SortedSet<NeighborType> _neighborTypes;

private String _node1Regex;

private String _node2Regex;

private boolean _verbose;

public NeighborsQuestion() {
_node1Regex = ".*";
_node2Regex = ".*";
_neighborTypes = new TreeSet<>();
_verbose = false;
}

@Override
Expand Down Expand Up @@ -427,13 +479,19 @@ public boolean getTraffic() {
return false;
}

@JsonProperty(VERBOSE_VAR)
public boolean getVerbose() {
return _verbose;
}

@Override
public String prettyPrint() {
try {
String retString = String.format(
"neighbors %s%s=%s | %s=%s | %s=%s", prettyPrintBase(),
NODE1_REGEX_VAR, _node1Regex, NODE2_REGEX_VAR, _node2Regex,
NEIGHBOR_TYPES_VAR, _neighborTypes.toString());
"neighbors %s%s=%s | %s=%s | %s=%s | %s=%b",
prettyPrintBase(), NODE1_REGEX_VAR, _node1Regex,
NODE2_REGEX_VAR, _node2Regex, NEIGHBOR_TYPES_VAR,
_neighborTypes.toString(), VERBOSE_VAR, _verbose);
return retString;
}
catch (Exception e) {
Expand Down Expand Up @@ -464,6 +522,10 @@ public void setNode2Regex(String regex) {
_node2Regex = regex;
}

@JsonProperty(VERBOSE_VAR)
public void setVerbose(boolean verbose) {
_verbose = verbose;
}
}

@Override
Expand Down

0 comments on commit 8a7b14c

Please sign in to comment.