@Test public void testReverseRuleSet() { Prefix p1 = Prefix.parse("1.1.1.1/24"); Prefix p2 = Prefix.parse("2.2.2.2/24"); Prefix p3 = Prefix.parse("3.3.3.3/24"); NatRuleMatch srcMatch = new NatRuleMatchSrcAddr(p1); NatRuleMatch srcportMatch = new NatRuleMatchSrcPort(10000, 30000); NatRuleMatch dstMatch = new NatRuleMatchDstAddr(p2); NatRuleThen thenPrefix = new NatRuleThenPrefix(p3, DESTINATION); NatRule rule0 = new NatRule("rule0"); rule0.getMatches().add(srcMatch); rule0.getMatches().add(srcportMatch); rule0.getMatches().add(dstMatch); rule0.setThen(thenPrefix); NatRuleSet ruleSet = new NatRuleSet("ruleset"); ruleSet.getFromLocation().setInterface("fromInterface"); ruleSet.getToLocation().setInterface("toInterface"); ruleSet.getRules().add(rule0); NatRuleSet reversedRuleSet = ReverseStaticNat.reverseRuleSet(ruleSet); assertThat(reversedRuleSet.getName(), equalTo("ruleset")); assertThat( reversedRuleSet.getFromLocation(), equalTo(NatPacketLocation.interfaceLocation("toInterface"))); assertThat( reversedRuleSet.getToLocation(), equalTo(NatPacketLocation.interfaceLocation("fromInterface"))); assertThat(reversedRuleSet.getRules(), hasSize(1)); NatRule reversedRule = reversedRuleSet.getRules().get(0); assertThat(reversedRule.getName(), equalTo("rule0")); assertThat(reversedRule.getThen(), equalTo(new NatRuleThenPrefix(p2, SOURCE))); assertThat( reversedRule.getMatches(), contains( new NatRuleMatchDstAddr(p1), new NatRuleMatchDstPort(10000, 30000), new NatRuleMatchSrcAddr(p3))); ruleSet.getFromLocation().setZone("fromZone"); ruleSet.getToLocation().setZone("toZone"); reversedRuleSet = ReverseStaticNat.reverseRuleSet(ruleSet); assertThat( reversedRuleSet.getFromLocation(), equalTo(NatPacketLocation.zoneLocation("toZone"))); assertThat( reversedRuleSet.getToLocation(), equalTo(NatPacketLocation.zoneLocation("fromZone"))); ruleSet.getFromLocation().setRoutingInstance("fromRI"); ruleSet.getToLocation().setRoutingInstance("toRI"); reversedRuleSet = ReverseStaticNat.reverseRuleSet(ruleSet); assertThat( reversedRuleSet.getFromLocation(), equalTo(NatPacketLocation.routingInstanceLocation("toRI"))); assertThat( reversedRuleSet.getToLocation(), equalTo(NatPacketLocation.routingInstanceLocation("fromRI"))); }
@VisibleForTesting static NatRuleSet reverseRuleSet(NatRuleSet ruleSet) { NatRuleSet reversedRuleSet = new NatRuleSet(ruleSet.getName()); NatPacketLocation fromLoc = ruleSet.getFromLocation(); NatPacketLocation toLoc = ruleSet.getToLocation(); reversedRuleSet.getToLocation().set(fromLoc.getType(), fromLoc.getName()); reversedRuleSet.getFromLocation().set(toLoc.getType(), toLoc.getName()); ruleSet.getRules().forEach(rule -> reversedRuleSet.getRules().add(reverseRule(rule))); return reversedRuleSet; }
ReverseStaticNat { @VisibleForTesting static NatRuleSet reverseRuleSet(NatRuleSet ruleSet) { NatRuleSet reversedRuleSet = new NatRuleSet(ruleSet.getName()); NatPacketLocation fromLoc = ruleSet.getFromLocation(); NatPacketLocation toLoc = ruleSet.getToLocation(); reversedRuleSet.getToLocation().set(fromLoc.getType(), fromLoc.getName()); reversedRuleSet.getFromLocation().set(toLoc.getType(), toLoc.getName()); ruleSet.getRules().forEach(rule -> reversedRuleSet.getRules().add(reverseRule(rule))); return reversedRuleSet; } }
ReverseStaticNat { @VisibleForTesting static NatRuleSet reverseRuleSet(NatRuleSet ruleSet) { NatRuleSet reversedRuleSet = new NatRuleSet(ruleSet.getName()); NatPacketLocation fromLoc = ruleSet.getFromLocation(); NatPacketLocation toLoc = ruleSet.getToLocation(); reversedRuleSet.getToLocation().set(fromLoc.getType(), fromLoc.getName()); reversedRuleSet.getFromLocation().set(toLoc.getType(), toLoc.getName()); ruleSet.getRules().forEach(rule -> reversedRuleSet.getRules().add(reverseRule(rule))); return reversedRuleSet; }  }
ReverseStaticNat { @VisibleForTesting static NatRuleSet reverseRuleSet(NatRuleSet ruleSet) { NatRuleSet reversedRuleSet = new NatRuleSet(ruleSet.getName()); NatPacketLocation fromLoc = ruleSet.getFromLocation(); NatPacketLocation toLoc = ruleSet.getToLocation(); reversedRuleSet.getToLocation().set(fromLoc.getType(), fromLoc.getName()); reversedRuleSet.getFromLocation().set(toLoc.getType(), toLoc.getName()); ruleSet.getRules().forEach(rule -> reversedRuleSet.getRules().add(reverseRule(rule))); return reversedRuleSet; }   }
ReverseStaticNat { @VisibleForTesting static NatRuleSet reverseRuleSet(NatRuleSet ruleSet) { NatRuleSet reversedRuleSet = new NatRuleSet(ruleSet.getName()); NatPacketLocation fromLoc = ruleSet.getFromLocation(); NatPacketLocation toLoc = ruleSet.getToLocation(); reversedRuleSet.getToLocation().set(fromLoc.getType(), fromLoc.getName()); reversedRuleSet.getFromLocation().set(toLoc.getType(), toLoc.getName()); ruleSet.getRules().forEach(rule -> reversedRuleSet.getRules().add(reverseRule(rule))); return reversedRuleSet; }    }