@Test public void testToTransformationStep() { Ip ip = Ip.parse("1.1.1.1"); int port = 10000; NatPool poolWithoutPAT = new NatPool(); poolWithoutPAT.setFromAddress(ip); poolWithoutPAT.setToAddress(ip); poolWithoutPAT.setPortAddressTranslation(NoPortTranslation.INSTANCE); NatPool poolWithPAT = new NatPool(); poolWithPAT.setFromAddress(ip); poolWithPAT.setToAddress(ip); poolWithPAT.setPortAddressTranslation(new PatPool(port, port)); NatPool poolWithDefaultPAT = new NatPool(); poolWithDefaultPAT.setFromAddress(ip); poolWithDefaultPAT.setToAddress(ip); Nat snat = new Nat(Type.SOURCE); snat.getPools().put("POOL1", poolWithoutPAT); snat.getPools().put("POOL2", poolWithPAT); snat.getPools().put("POOL3", poolWithDefaultPAT); NatRuleThenPool then = new NatRuleThenPool("POOL1"); List<TransformationStep> steps = then.toTransformationSteps(snat, null, ip, null); assertThat(steps, contains(new AssignIpAddressFromPool(SOURCE_NAT, IpField.SOURCE, ip, ip))); then = new NatRuleThenPool("POOL2"); steps = then.toTransformationSteps(snat, null, ip, null); assertThat( steps, contains( new AssignIpAddressFromPool(SOURCE_NAT, IpField.SOURCE, ip, ip), new AssignPortFromPool(SOURCE_NAT, PortField.SOURCE, port, port))); then = new NatRuleThenPool("POOL3"); steps = then.toTransformationSteps(snat, null, ip, null); assertThat( steps, contains( new AssignIpAddressFromPool(SOURCE_NAT, IpField.SOURCE, ip, ip), new AssignPortFromPool( SOURCE_NAT, PortField.SOURCE, Nat.DEFAULT_FROM_PORT, Nat.DEFAULT_TO_PORT))); snat.setDefaultFromPort(10000); snat.setDefaultToPort(20000); steps = then.toTransformationSteps(snat, null, ip, null); assertThat( steps, contains( new AssignIpAddressFromPool(SOURCE_NAT, IpField.SOURCE, ip, ip), new AssignPortFromPool(SOURCE_NAT, PortField.SOURCE, 10000, 20000))); }
@Override public List<TransformationStep> toTransformationSteps( Nat nat, @Nullable Map<String, AddressBookEntry> addressBookEntryMap, Ip interfaceIp, Warnings warnings) { checkArgument( (nat.getType() == SOURCE || nat.getType() == DESTINATION), "Interface actions can only be used in source nat and dest nat"); TransformationType type = nat.getType().toTransformationType(); IpField ipField = nat.getType() == SOURCE ? IpField.SOURCE : IpField.DESTINATION; NatPool pool = nat.getPools().get(_poolName); if (pool == null) { return ImmutableList.of(); } Ip from = pool.getFromAddress(); Ip to = pool.getToAddress(); if (from.asLong() > to.asLong()) { warnings.redFlag(String.format("NAT pool %s is invalid: %s - %s", _poolName, from, to)); return ImmutableList.of(); } ImmutableList.Builder<TransformationStep> builder = new Builder<>(); builder.add(new AssignIpAddressFromPool(type, ipField, from, to)); PortAddressTranslation pat = pool.getPortAddressTranslation(); if (pat != null) { PortField portField = nat.getType() == SOURCE ? PortField.SOURCE : PortField.DESTINATION; Optional<TransformationStep> patStep = pat.toTransformationStep(type, portField); patStep.ifPresent(builder::add); } else if (type == SOURCE_NAT) { builder.add( new AssignPortFromPool( type, PortField.SOURCE, nat.getDefaultFromPort(), nat.getDefaultToPort())); } return builder.build(); }
NatRuleThenPool implements NatRuleThen, Serializable { @Override public List<TransformationStep> toTransformationSteps( Nat nat, @Nullable Map<String, AddressBookEntry> addressBookEntryMap, Ip interfaceIp, Warnings warnings) { checkArgument( (nat.getType() == SOURCE || nat.getType() == DESTINATION), "Interface actions can only be used in source nat and dest nat"); TransformationType type = nat.getType().toTransformationType(); IpField ipField = nat.getType() == SOURCE ? IpField.SOURCE : IpField.DESTINATION; NatPool pool = nat.getPools().get(_poolName); if (pool == null) { return ImmutableList.of(); } Ip from = pool.getFromAddress(); Ip to = pool.getToAddress(); if (from.asLong() > to.asLong()) { warnings.redFlag(String.format("NAT pool %s is invalid: %s - %s", _poolName, from, to)); return ImmutableList.of(); } ImmutableList.Builder<TransformationStep> builder = new Builder<>(); builder.add(new AssignIpAddressFromPool(type, ipField, from, to)); PortAddressTranslation pat = pool.getPortAddressTranslation(); if (pat != null) { PortField portField = nat.getType() == SOURCE ? PortField.SOURCE : PortField.DESTINATION; Optional<TransformationStep> patStep = pat.toTransformationStep(type, portField); patStep.ifPresent(builder::add); } else if (type == SOURCE_NAT) { builder.add( new AssignPortFromPool( type, PortField.SOURCE, nat.getDefaultFromPort(), nat.getDefaultToPort())); } return builder.build(); } }
NatRuleThenPool implements NatRuleThen, Serializable { @Override public List<TransformationStep> toTransformationSteps( Nat nat, @Nullable Map<String, AddressBookEntry> addressBookEntryMap, Ip interfaceIp, Warnings warnings) { checkArgument( (nat.getType() == SOURCE || nat.getType() == DESTINATION), "Interface actions can only be used in source nat and dest nat"); TransformationType type = nat.getType().toTransformationType(); IpField ipField = nat.getType() == SOURCE ? IpField.SOURCE : IpField.DESTINATION; NatPool pool = nat.getPools().get(_poolName); if (pool == null) { return ImmutableList.of(); } Ip from = pool.getFromAddress(); Ip to = pool.getToAddress(); if (from.asLong() > to.asLong()) { warnings.redFlag(String.format("NAT pool %s is invalid: %s - %s", _poolName, from, to)); return ImmutableList.of(); } ImmutableList.Builder<TransformationStep> builder = new Builder<>(); builder.add(new AssignIpAddressFromPool(type, ipField, from, to)); PortAddressTranslation pat = pool.getPortAddressTranslation(); if (pat != null) { PortField portField = nat.getType() == SOURCE ? PortField.SOURCE : PortField.DESTINATION; Optional<TransformationStep> patStep = pat.toTransformationStep(type, portField); patStep.ifPresent(builder::add); } else if (type == SOURCE_NAT) { builder.add( new AssignPortFromPool( type, PortField.SOURCE, nat.getDefaultFromPort(), nat.getDefaultToPort())); } return builder.build(); } NatRuleThenPool(String poolName); }
NatRuleThenPool implements NatRuleThen, Serializable { @Override public List<TransformationStep> toTransformationSteps( Nat nat, @Nullable Map<String, AddressBookEntry> addressBookEntryMap, Ip interfaceIp, Warnings warnings) { checkArgument( (nat.getType() == SOURCE || nat.getType() == DESTINATION), "Interface actions can only be used in source nat and dest nat"); TransformationType type = nat.getType().toTransformationType(); IpField ipField = nat.getType() == SOURCE ? IpField.SOURCE : IpField.DESTINATION; NatPool pool = nat.getPools().get(_poolName); if (pool == null) { return ImmutableList.of(); } Ip from = pool.getFromAddress(); Ip to = pool.getToAddress(); if (from.asLong() > to.asLong()) { warnings.redFlag(String.format("NAT pool %s is invalid: %s - %s", _poolName, from, to)); return ImmutableList.of(); } ImmutableList.Builder<TransformationStep> builder = new Builder<>(); builder.add(new AssignIpAddressFromPool(type, ipField, from, to)); PortAddressTranslation pat = pool.getPortAddressTranslation(); if (pat != null) { PortField portField = nat.getType() == SOURCE ? PortField.SOURCE : PortField.DESTINATION; Optional<TransformationStep> patStep = pat.toTransformationStep(type, portField); patStep.ifPresent(builder::add); } else if (type == SOURCE_NAT) { builder.add( new AssignPortFromPool( type, PortField.SOURCE, nat.getDefaultFromPort(), nat.getDefaultToPort())); } return builder.build(); } NatRuleThenPool(String poolName); @Override boolean equals(Object o); @Nonnull String getPoolName(); @Override int hashCode(); @Override List<TransformationStep> toTransformationSteps(
      Nat nat,
      @Nullable Map<String, AddressBookEntry> addressBookEntryMap,
      Ip interfaceIp,
      Warnings warnings); }
NatRuleThenPool implements NatRuleThen, Serializable { @Override public List<TransformationStep> toTransformationSteps( Nat nat, @Nullable Map<String, AddressBookEntry> addressBookEntryMap, Ip interfaceIp, Warnings warnings) { checkArgument( (nat.getType() == SOURCE || nat.getType() == DESTINATION), "Interface actions can only be used in source nat and dest nat"); TransformationType type = nat.getType().toTransformationType(); IpField ipField = nat.getType() == SOURCE ? IpField.SOURCE : IpField.DESTINATION; NatPool pool = nat.getPools().get(_poolName); if (pool == null) { return ImmutableList.of(); } Ip from = pool.getFromAddress(); Ip to = pool.getToAddress(); if (from.asLong() > to.asLong()) { warnings.redFlag(String.format("NAT pool %s is invalid: %s - %s", _poolName, from, to)); return ImmutableList.of(); } ImmutableList.Builder<TransformationStep> builder = new Builder<>(); builder.add(new AssignIpAddressFromPool(type, ipField, from, to)); PortAddressTranslation pat = pool.getPortAddressTranslation(); if (pat != null) { PortField portField = nat.getType() == SOURCE ? PortField.SOURCE : PortField.DESTINATION; Optional<TransformationStep> patStep = pat.toTransformationStep(type, portField); patStep.ifPresent(builder::add); } else if (type == SOURCE_NAT) { builder.add( new AssignPortFromPool( type, PortField.SOURCE, nat.getDefaultFromPort(), nat.getDefaultToPort())); } return builder.build(); } NatRuleThenPool(String poolName); @Override boolean equals(Object o); @Nonnull String getPoolName(); @Override int hashCode(); @Override List<TransformationStep> toTransformationSteps(
      Nat nat,
      @Nullable Map<String, AddressBookEntry> addressBookEntryMap,
      Ip interfaceIp,
      Warnings warnings);  }