Java Code Examples for com.google.common.collect.ImmutableMultiset#of()

The following examples show how to use com.google.common.collect.ImmutableMultiset#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterTableAnswererTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void selectColumns() {
  Multiset<Row> rows =
      ImmutableMultiset.of(
          Row.builder().put("col1", null).put("col2", 12).put("col3", 13).build(),
          Row.builder().put("col1", 41).put("col2", 24).put("col3", 32).build());

  Multiset<Row> selectedRows = FilterTableAnswerer.selectColumns(ImmutableSet.of("col1"), rows);

  // only col1 should remain with expected values
  assertThat(
      selectedRows,
      equalTo(
          new ImmutableMultiset.Builder<Row>()
              .add(Row.builder().put("col1", 41).build())
              .add(Row.builder().put("col1", null).build())
              .build()));
}
 
Example 2
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void testUndefinedReference() {

  ExprAclLine aclLine =
      ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("???")).build();
  _aclb.setLines(ImmutableList.of(aclLine)).setName("acl").build();

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  // Construct the expected result. Should find an undefined ACL result.
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl"))
              .put(COL_UNREACHABLE_LINE, aclLine.toString())
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of())
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, UNDEFINED_REFERENCE)
              .build());
  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 3
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithIcmpType() {
  // First line accepts IP 1.2.3.4
  // Second line accepts same but only ICMP of type 8
  List<AclLine> lines =
      ImmutableList.of(
          ExprAclLine.acceptingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Ip.parse("1.2.3.4").toIpSpace()).build()),
          ExprAclLine.acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(Ip.parse("1.2.3.4").toIpSpace())
                  .setIpProtocols(ImmutableSet.of(IpProtocol.ICMP))
                  .setIcmpTypes(ImmutableList.of(new SubRange(8)))
                  .build()));
  _aclb.setLines(lines).setName("acl").build();
  List<String> lineNames = lines.stream().map(Object::toString).collect(Collectors.toList());

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  // Construct the expected result. First line should block second.
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl"))
              .put(COL_UNREACHABLE_LINE, lineNames.get(1))
              .put(COL_UNREACHABLE_LINE_ACTION, LineAction.PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, BLOCKING_LINES)
              .build());
  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 4
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testIpWildcards() {
  // First line accepts src IPs 1.2.3.4/30
  // Second line accepts src IPs 1.2.3.4/32
  List<AclLine> lines =
      ImmutableList.of(
          ExprAclLine.acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(
                      IpWildcard.create(Prefix.create(Ip.parse("1.2.3.4"), 30)).toIpSpace())
                  .build()),
          ExprAclLine.acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(IpWildcard.create(Ip.parse("1.2.3.4").toPrefix()).toIpSpace())
                  .build()),
          ExprAclLine.acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(
                      IpWildcard.create(Prefix.create(Ip.parse("1.2.3.4"), 28)).toIpSpace())
                  .build()));
  _aclb.setLines(lines).setName("acl").build();
  List<String> lineNames = lines.stream().map(Object::toString).collect(Collectors.toList());

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  // Construct the expected result. First line should block second.
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl"))
              .put(COL_UNREACHABLE_LINE, lineNames.get(1))
              .put(COL_UNREACHABLE_LINE_ACTION, LineAction.PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, BLOCKING_LINES)
              .build());
  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 5
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testCycleAppearsOnce() {
  // acl1 permits anything acl2 permits... twice
  // acl2 permits anything acl1 permits... twice
  _aclb
      .setLines(
          ImmutableList.of(
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl2")).build(),
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl2")).build()))
      .setName("acl1")
      .build();
  _aclb
      .setLines(
          ImmutableList.of(
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build(),
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build()))
      .setName("acl2")
      .build();

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  // Construct the expected result. Should find only one cycle result.
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl1, acl2"))
              .put(COL_UNREACHABLE_LINE, null)
              .put(COL_UNREACHABLE_LINE_ACTION, null)
              .put(COL_BLOCKING_LINES, null)
              .put(COL_DIFF_ACTION, null)
              .put(COL_REASON, CYCLICAL_REFERENCE)
              .put(COL_ADDITIONAL_INFO, "Cyclic references in node 'c1': acl1 -> acl2 -> acl1")
              .build());
  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 6
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleCoveringLines() {
  List<AclLine> aclLines =
      ImmutableList.of(
          acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(IpWildcard.parse("1.0.0.0:0.0.0.0").toIpSpace())
                  .build()),
          acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(IpWildcard.parse("1.0.0.1:0.0.0.0").toIpSpace())
                  .build()),
          acceptingHeaderSpace(
              HeaderSpace.builder()
                  .setSrcIps(IpWildcard.parse("1.0.0.0:0.0.0.1").toIpSpace())
                  .build()));
  IpAccessList acl = _aclb.setLines(aclLines).setName("acl").build();
  List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  /*
   Construct the expected result. Line 2 should be blocked by both previous lines.
  */
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
              .put(COL_UNREACHABLE_LINE, lineNames.get(2))
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0), lineNames.get(1)))
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, BLOCKING_LINES)
              .build());

  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 7
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithSrcInterfaceReference() {
  List<AclLine> aclLines =
      ImmutableList.of(
          ExprAclLine.accepting()
              .setMatchCondition(new MatchSrcInterface(ImmutableList.of("iface", "iface2")))
              .build(),
          ExprAclLine.accepting()
              .setMatchCondition(new MatchSrcInterface(ImmutableList.of("iface")))
              .build());
  IpAccessList acl = _aclb.setLines(aclLines).setName("acl").build();
  List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  /* Construct the expected result. Line 1 should be blocked by line 0. */
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
              .put(COL_UNREACHABLE_LINE, lineNames.get(1))
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, BLOCKING_LINES)
              .build());

  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 8
Source File: ImmutableMultisetDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
@Override
protected ImmutableMultiset<Object> _createEmpty(DeserializationContext ctxt) throws IOException {
    return ImmutableMultiset.of();
}
 
Example 9
Source File: ImmutableMultisetDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
@Override
protected ImmutableMultiset<Object> _createWithSingleElement(DeserializationContext ctxt, Object value) throws IOException {
    return ImmutableMultiset.of(value);
}
 
Example 10
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Multiset<String> getAssignedNameCounts() {
  return ImmutableMultiset.of();
}
 
Example 11
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testCircularReferences() {
  // acl0 permits anything acl1 permits
  // acl1 permits anything acl2 permits, plus 1 other line to avoid acl3's line being unmatchable
  // acl2 permits anything acl0 permits
  // acl3 permits anything acl1 permits (not part of cycle)
  _aclb
      .setLines(
          ImmutableList.of(
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build()))
      .setName("acl0")
      .build();
  _aclb
      .setLines(
          ImmutableList.of(
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl2")).build(),
              acceptingHeaderSpace(
                  HeaderSpace.builder()
                      .setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace())
                      .build())))
      .setName("acl1")
      .build();
  _aclb
      .setLines(
          ImmutableList.of(
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl0")).build()))
      .setName("acl2")
      .build();
  _aclb
      .setLines(
          ImmutableList.of(
              ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build()))
      .setName("acl3")
      .build();

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  // Construct the expected result. Should find a single cycle result.
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl0, acl1, acl2"))
              .put(COL_UNREACHABLE_LINE, null)
              .put(COL_UNREACHABLE_LINE_ACTION, null)
              .put(COL_BLOCKING_LINES, null)
              .put(COL_DIFF_ACTION, null)
              .put(COL_REASON, CYCLICAL_REFERENCE)
              .put(
                  COL_ADDITIONAL_INFO,
                  "Cyclic references in node 'c1': acl0 -> acl1 -> acl2 -> acl0")
              .build());
  assertThat(answer.getRows().getData(), equalTo(expected));
}
 
Example 12
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndirection() {
  /*
   Referenced ACL contains 1 line: Permit 1.0.0.0/24
   Main ACL contains 2 lines:
   0. Permit anything that referenced ACL permits
   1. Permit 1.0.0.0/24
  */
  List<AclLine> referencedAclLines =
      ImmutableList.of(
          acceptingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()));
  IpAccessList referencedAcl = _aclb.setLines(referencedAclLines).setName("acl1").build();

  List<AclLine> aclLines =
      ImmutableList.of(
          ExprAclLine.accepting()
              .setMatchCondition(new PermittedByAcl(referencedAcl.getName()))
              .build(),
          acceptingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()));
  IpAccessList acl = _aclb.setLines(aclLines).setName("acl2").build();
  List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());

  /*
   Runs two questions:
   1. General ACL reachability (referenced ACL won't be encoded after first NoD step)
   2. Reachability specifically for main ACL (referenced ACL won't be encoded at all)
   Will test that both give the same result.
  */
  TableAnswerElement generalAnswer = answer(new FilterLineReachabilityQuestion());
  TableAnswerElement specificAnswer = answer(new FilterLineReachabilityQuestion(acl.getName()));

  // Construct the expected result. Should find line 1 to be blocked by line 0 in main ACL.
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
              .put(COL_UNREACHABLE_LINE, lineNames.get(1))
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, BLOCKING_LINES)
              .build());

  assertThat(generalAnswer.getRows().getData(), equalTo(expected));
  assertThat(specificAnswer.getRows().getData(), equalTo(expected));
}
 
Example 13
Source File: FilterLineReachabilityTest.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndependentlyUnmatchableLines() {
  /*
  Construct ACL with lines:
  0. Reject 1.0.0.0/24 (unblocked)
  1. Accept 1.0.0.0/24 (blocked by line 0)
  2. Accept [empty set] (unmatchable)
  3. Accept 1.0.0.0/32 (blocked by line 0)
  4. Accept 1.2.3.4/32 (unblocked)
   */
  List<AclLine> aclLines =
      ImmutableList.of(
          rejectingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()),
          acceptingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()),
          ExprAclLine.accepting().setMatchCondition(FalseExpr.INSTANCE).build(),
          acceptingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/32").toIpSpace()).build()),
          acceptingHeaderSpace(
              HeaderSpace.builder().setSrcIps(Prefix.parse("1.2.3.4/32").toIpSpace()).build()));
  IpAccessList acl = _aclb.setLines(aclLines).setName("acl").build();
  List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());

  TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());

  // Construct the expected result
  Multiset<Row> expected =
      ImmutableMultiset.of(
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
              .put(COL_UNREACHABLE_LINE, lineNames.get(1))
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
              .put(COL_DIFF_ACTION, true)
              .put(COL_REASON, BLOCKING_LINES)
              .build(),
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
              .put(COL_UNREACHABLE_LINE, lineNames.get(2))
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of())
              .put(COL_DIFF_ACTION, false)
              .put(COL_REASON, INDEPENDENTLY_UNMATCHABLE)
              .build(),
          Row.builder(COLUMN_METADATA)
              .put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
              .put(COL_UNREACHABLE_LINE, lineNames.get(3))
              .put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
              .put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
              .put(COL_DIFF_ACTION, true)
              .put(COL_REASON, BLOCKING_LINES)
              .build());

  assertThat(answer.getRows().getData(), equalTo(expected));
}