@Test public void testReverseRule() { 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); NatRule reversedRule = ReverseStaticNat.reverseRule(rule0); 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))); NatRule rule1 = new NatRule("rule1"); rule1.getMatches().add(srcMatch); rule1.getMatches().add(dstMatch); rule1.setThen(thenPrefix); reversedRule = ReverseStaticNat.reverseRule(rule1); assertThat(reversedRule.getName(), equalTo("rule1")); assertThat(reversedRule.getThen(), equalTo(new NatRuleThenPrefix(p2, SOURCE))); assertThat( reversedRule.getMatches(), contains(new NatRuleMatchDstAddr(p1), new NatRuleMatchSrcAddr(p3))); NatRule rule2 = new NatRule("rule2"); rule2.getMatches().add(dstMatch); rule2.setThen(thenPrefix); reversedRule = ReverseStaticNat.reverseRule(rule2); assertThat(reversedRule.getName(), equalTo("rule2")); assertThat(reversedRule.getThen(), equalTo(new NatRuleThenPrefix(p2, SOURCE))); assertThat(reversedRule.getMatches(), contains(new NatRuleMatchSrcAddr(p3))); }
@VisibleForTesting static NatRule reverseRule(NatRule rule) { NatRule reversedRule = new NatRule(rule.getName()); List<NatRuleMatch> matchList = rule.getMatches(); matchList.forEach( match -> { NatRuleMatch reversedDstMatch = reverseMatchToMatch(match); NatRuleThen reversedThen = reverseMatchToThen(match); if (reversedDstMatch != null) { reversedRule.getMatches().add(reversedDstMatch); } if (reversedThen != null) { assert reversedRule.getThen() == null; reversedRule.setThen(reversedThen); } }); reversedRule.getMatches().add(thenToMatch(rule.getThen())); return reversedRule; }
ReverseStaticNat { @VisibleForTesting static NatRule reverseRule(NatRule rule) { NatRule reversedRule = new NatRule(rule.getName()); List<NatRuleMatch> matchList = rule.getMatches(); matchList.forEach( match -> { NatRuleMatch reversedDstMatch = reverseMatchToMatch(match); NatRuleThen reversedThen = reverseMatchToThen(match); if (reversedDstMatch != null) { reversedRule.getMatches().add(reversedDstMatch); } if (reversedThen != null) { assert reversedRule.getThen() == null; reversedRule.setThen(reversedThen); } }); reversedRule.getMatches().add(thenToMatch(rule.getThen())); return reversedRule; } }
ReverseStaticNat { @VisibleForTesting static NatRule reverseRule(NatRule rule) { NatRule reversedRule = new NatRule(rule.getName()); List<NatRuleMatch> matchList = rule.getMatches(); matchList.forEach( match -> { NatRuleMatch reversedDstMatch = reverseMatchToMatch(match); NatRuleThen reversedThen = reverseMatchToThen(match); if (reversedDstMatch != null) { reversedRule.getMatches().add(reversedDstMatch); } if (reversedThen != null) { assert reversedRule.getThen() == null; reversedRule.setThen(reversedThen); } }); reversedRule.getMatches().add(thenToMatch(rule.getThen())); return reversedRule; }  }
ReverseStaticNat { @VisibleForTesting static NatRule reverseRule(NatRule rule) { NatRule reversedRule = new NatRule(rule.getName()); List<NatRuleMatch> matchList = rule.getMatches(); matchList.forEach( match -> { NatRuleMatch reversedDstMatch = reverseMatchToMatch(match); NatRuleThen reversedThen = reverseMatchToThen(match); if (reversedDstMatch != null) { reversedRule.getMatches().add(reversedDstMatch); } if (reversedThen != null) { assert reversedRule.getThen() == null; reversedRule.setThen(reversedThen); } }); reversedRule.getMatches().add(thenToMatch(rule.getThen())); return reversedRule; }   }
ReverseStaticNat { @VisibleForTesting static NatRule reverseRule(NatRule rule) { NatRule reversedRule = new NatRule(rule.getName()); List<NatRuleMatch> matchList = rule.getMatches(); matchList.forEach( match -> { NatRuleMatch reversedDstMatch = reverseMatchToMatch(match); NatRuleThen reversedThen = reverseMatchToThen(match); if (reversedDstMatch != null) { reversedRule.getMatches().add(reversedDstMatch); } if (reversedThen != null) { assert reversedRule.getThen() == null; reversedRule.setThen(reversedThen); } }); reversedRule.getMatches().add(thenToMatch(rule.getThen())); return reversedRule; }    }