@Test public void testReverseNat() { 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); Nat staticNat = new Nat(Type.STATIC); staticNat.getRuleSets().put("ruleset", ruleSet); Nat reversedNat = ReverseStaticNat.reverseNat(staticNat); assertThat(reversedNat.getType(), equalTo(Type.STATIC)); assertThat(reversedNat.getRuleSets().keySet(), hasSize(1)); NatRuleSet reversedRuleSet = reversedNat.getRuleSets().get("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))); }
static Nat reverseNat(Nat nat) { checkArgument(nat.getType() == STATIC, "Cannot reverse nat with type " + nat.getType()); checkArgument(nat.getPools().isEmpty(), "Static nat should not have pools"); Nat reversedNat = new Nat(STATIC); reversedNat.setDefaultFromPort(nat.getDefaultFromPort()); reversedNat.setDefaultToPort(nat.getDefaultToPort()); reversedNat .getRuleSets() .putAll( nat.getRuleSets().entrySet().stream() .collect( Collectors.toMap(Entry::getKey, entry -> reverseRuleSet(entry.getValue())))); return reversedNat; }
ReverseStaticNat { static Nat reverseNat(Nat nat) { checkArgument(nat.getType() == STATIC, "Cannot reverse nat with type " + nat.getType()); checkArgument(nat.getPools().isEmpty(), "Static nat should not have pools"); Nat reversedNat = new Nat(STATIC); reversedNat.setDefaultFromPort(nat.getDefaultFromPort()); reversedNat.setDefaultToPort(nat.getDefaultToPort()); reversedNat .getRuleSets() .putAll( nat.getRuleSets().entrySet().stream() .collect( Collectors.toMap(Entry::getKey, entry -> reverseRuleSet(entry.getValue())))); return reversedNat; } }
ReverseStaticNat { static Nat reverseNat(Nat nat) { checkArgument(nat.getType() == STATIC, "Cannot reverse nat with type " + nat.getType()); checkArgument(nat.getPools().isEmpty(), "Static nat should not have pools"); Nat reversedNat = new Nat(STATIC); reversedNat.setDefaultFromPort(nat.getDefaultFromPort()); reversedNat.setDefaultToPort(nat.getDefaultToPort()); reversedNat .getRuleSets() .putAll( nat.getRuleSets().entrySet().stream() .collect( Collectors.toMap(Entry::getKey, entry -> reverseRuleSet(entry.getValue())))); return reversedNat; }  }
ReverseStaticNat { static Nat reverseNat(Nat nat) { checkArgument(nat.getType() == STATIC, "Cannot reverse nat with type " + nat.getType()); checkArgument(nat.getPools().isEmpty(), "Static nat should not have pools"); Nat reversedNat = new Nat(STATIC); reversedNat.setDefaultFromPort(nat.getDefaultFromPort()); reversedNat.setDefaultToPort(nat.getDefaultToPort()); reversedNat .getRuleSets() .putAll( nat.getRuleSets().entrySet().stream() .collect( Collectors.toMap(Entry::getKey, entry -> reverseRuleSet(entry.getValue())))); return reversedNat; }   }
ReverseStaticNat { static Nat reverseNat(Nat nat) { checkArgument(nat.getType() == STATIC, "Cannot reverse nat with type " + nat.getType()); checkArgument(nat.getPools().isEmpty(), "Static nat should not have pools"); Nat reversedNat = new Nat(STATIC); reversedNat.setDefaultFromPort(nat.getDefaultFromPort()); reversedNat.setDefaultToPort(nat.getDefaultToPort()); reversedNat .getRuleSets() .putAll( nat.getRuleSets().entrySet().stream() .collect( Collectors.toMap(Entry::getKey, entry -> reverseRuleSet(entry.getValue())))); return reversedNat; }    }