use of org.batfish.datamodel.SourceNat in project batfish by batfish.
the class BdpDataPlanePluginTest method testApplySourceNatSingleAclNoMatch.
@Test
public void testApplySourceNatSingleAclNoMatch() {
Flow flow = makeFlow();
SourceNat nat = new SourceNat();
nat.setAcl(makeAcl("reject", LineAction.REJECT));
nat.setPoolIpFirst(new Ip("4.5.6.7"));
Flow transformed = BdpEngine.applySourceNat(flow, singletonList(nat));
assertThat(transformed, is(flow));
}
use of org.batfish.datamodel.SourceNat in project batfish by batfish.
the class SynthesizerInputImplTest method testComputeSourceNats.
@Test
public void testComputeSourceNats() {
Configuration srcNode = _cb.build();
Configuration nextHop = _cb.build();
Vrf srcVrf = _vb.setOwner(srcNode).build();
Vrf nextHopVrf = _vb.setOwner(nextHop).build();
Ip ip11 = new Ip("1.0.0.0");
Ip ip12 = new Ip("1.0.0.10");
Ip ip21 = new Ip("2.0.0.0");
Ip ip22 = new Ip("2.0.0.10");
IpAccessList sourceNat1Acl = _aclb.setLines(ImmutableList.of()).setOwner(srcNode).build();
IpAccessList sourceNat2Acl = _aclb.build();
SourceNat sourceNat1 = _snb.setPoolIpFirst(ip11).setPoolIpLast(ip12).setAcl(sourceNat1Acl).build();
SourceNat sourceNat2 = _snb.setPoolIpFirst(ip21).setPoolIpLast(ip22).setAcl(sourceNat2Acl).build();
Interface srcInterfaceZeroSourceNats = _ib.setOwner(srcNode).setVrf(srcVrf).setSourceNats(ImmutableList.of()).build();
Interface srcInterfaceOneSourceNat = _ib.setSourceNats(ImmutableList.of(sourceNat1)).build();
Interface srcInterfaceTwoSourceNats = _ib.setSourceNats(ImmutableList.of(sourceNat1, sourceNat2)).build();
Interface nextHopInterface = _ib.setOwner(nextHop).setVrf(nextHopVrf).setSourceNats(ImmutableList.of()).build();
Edge forwardEdge1 = new Edge(srcInterfaceZeroSourceNats, nextHopInterface);
Edge forwardEdge2 = new Edge(srcInterfaceOneSourceNat, nextHopInterface);
Edge forwardEdge3 = new Edge(srcInterfaceTwoSourceNats, nextHopInterface);
Edge backEdge1 = new Edge(nextHopInterface, srcInterfaceZeroSourceNats);
Edge backEdge2 = new Edge(nextHopInterface, srcInterfaceOneSourceNat);
Edge backEdge3 = new Edge(nextHopInterface, srcInterfaceTwoSourceNats);
SynthesizerInput inputWithoutDataPlane = _inputBuilder.setConfigurations(ImmutableMap.of(srcNode.getName(), srcNode, nextHop.getName(), nextHop)).build();
SynthesizerInput inputWithDataPlane = _inputBuilder.setForwardingAnalysis(MockForwardingAnalysis.builder().build()).setTopology(new Topology(ImmutableSortedSet.of(forwardEdge1, forwardEdge2, forwardEdge3, backEdge1, backEdge2, backEdge3))).build();
assertThat(inputWithDataPlane, hasSourceNats(hasEntry(equalTo(srcNode.getName()), hasEntry(equalTo(srcInterfaceZeroSourceNats.getName()), equalTo(ImmutableList.of())))));
assertThat(inputWithDataPlane, hasSourceNats(hasEntry(equalTo(srcNode.getName()), hasEntry(equalTo(srcInterfaceOneSourceNat.getName()), equalTo(ImmutableList.of(immutableEntry(new AclPermit(srcNode.getName(), sourceNat1Acl.getName()), new RangeMatchExpr(TransformationHeaderField.NEW_SRC_IP, TransformationHeaderField.NEW_SRC_IP.getSize(), ImmutableSet.of(Range.closed(ip11.asLong(), ip12.asLong()))))))))));
assertThat(inputWithDataPlane, hasSourceNats(hasEntry(equalTo(srcNode.getName()), hasEntry(equalTo(srcInterfaceTwoSourceNats.getName()), equalTo(ImmutableList.of(immutableEntry(new AclPermit(srcNode.getName(), sourceNat1Acl.getName()), new RangeMatchExpr(TransformationHeaderField.NEW_SRC_IP, TransformationHeaderField.NEW_SRC_IP.getSize(), ImmutableSet.of(Range.closed(ip11.asLong(), ip12.asLong())))), immutableEntry(new AclPermit(srcNode.getName(), sourceNat2Acl.getName()), new RangeMatchExpr(TransformationHeaderField.NEW_SRC_IP, TransformationHeaderField.NEW_SRC_IP.getSize(), ImmutableSet.of(Range.closed(ip21.asLong(), ip22.asLong()))))))))));
assertThat(inputWithoutDataPlane, hasSourceNats(nullValue()));
}
use of org.batfish.datamodel.SourceNat in project batfish by batfish.
the class HostInterface method toInterface.
public Interface toInterface(Configuration configuration, Warnings warnings) {
String name = _canonicalName != null ? _canonicalName : _name;
Interface.Builder iface = Interface.builder().setName(name).setOwner(configuration).setActive(true).setAddresses(_address, _otherAddresses).setBandwidth(_bandwidth).setDeclaredNames(ImmutableSortedSet.of(_name)).setProxyArp(false).setVrf(configuration.getDefaultVrf());
if (_shared) {
SourceNat sourceNat = new SourceNat();
Ip publicIp = _address.getIp();
sourceNat.setPoolIpFirst(publicIp);
sourceNat.setPoolIpLast(publicIp);
iface.setSourceNats(ImmutableList.of(sourceNat));
}
return iface.build();
}
use of org.batfish.datamodel.SourceNat in project batfish by batfish.
the class BdpDataPlanePluginTest method testApplySourceNatFirstMatchWins.
@Test
public void testApplySourceNatFirstMatchWins() {
Flow flow = makeFlow();
SourceNat nat = new SourceNat();
nat.setAcl(makeAcl("firstAccept", LineAction.ACCEPT));
nat.setPoolIpFirst(new Ip("4.5.6.7"));
SourceNat secondNat = new SourceNat();
secondNat.setAcl(makeAcl("secondAccept", LineAction.ACCEPT));
secondNat.setPoolIpFirst(new Ip("4.5.6.8"));
Flow transformed = BdpEngine.applySourceNat(flow, Lists.newArrayList(nat, secondNat));
assertThat(transformed.getSrcIp(), equalTo(new Ip("4.5.6.7")));
}
use of org.batfish.datamodel.SourceNat in project batfish by batfish.
the class NodJobTest method setupConfigs.
private void setupConfigs() {
NetworkFactory nf = new NetworkFactory();
Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
Interface.Builder ib = nf.interfaceBuilder().setActive(true).setBandwidth(1E9d);
IpAccessList.Builder aclb = nf.aclBuilder();
IpAccessListLine.Builder acllb = IpAccessListLine.builder();
SourceNat.Builder snb = SourceNat.builder();
Vrf.Builder vb = nf.vrfBuilder();
_srcNode = cb.build();
_dstNode = cb.build();
_srcVrf = vb.setOwner(_srcNode).build();
_originateVrf = new OriginateVrf(_srcNode.getHostname(), _srcVrf.getName());
Vrf dstVrf = vb.setOwner(_dstNode).build();
Prefix p1 = Prefix.parse("1.0.0.0/31");
Ip poolIp1 = new Ip("1.0.0.10");
// apply NAT to all packets
IpAccessList sourceNat1Acl = aclb.setLines(ImmutableList.of(acllb.setSrcIps(ImmutableList.of(new IpWildcard("3.0.0.0/32"))).setAction(LineAction.ACCEPT).build())).setOwner(_srcNode).build();
SourceNat sourceNat1 = // Would be easier to understand, and Nuno says it will likely be more efficient.
snb.setPoolIpFirst(poolIp1).setPoolIpLast(poolIp1).setAcl(sourceNat1Acl).build();
ib.setOwner(_srcNode).setVrf(_srcVrf).setAddress(new InterfaceAddress(p1.getStartIp(), p1.getPrefixLength())).setSourceNats(ImmutableList.of(sourceNat1)).build();
ib.setOwner(_dstNode).setVrf(dstVrf).setAddress(new InterfaceAddress(p1.getEndIp(), p1.getPrefixLength())).setSourceNats(ImmutableList.of()).build();
// For the destination
Prefix pDest = Prefix.parse("2.0.0.0/32");
ib.setOwner(_dstNode).setVrf(dstVrf).setAddress(new InterfaceAddress(pDest.getEndIp(), pDest.getPrefixLength())).build();
StaticRoute.Builder bld = StaticRoute.builder().setNetwork(pDest);
_srcVrf.getStaticRoutes().add(bld.setNextHopIp(p1.getEndIp()).build());
_configs = ImmutableSortedMap.of(_srcNode.getName(), _srcNode, _dstNode.getName(), _dstNode);
}
Aggregations