Skip to content

Commit

Permalink
refactor convertl3evpn to smaller helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkala committed Oct 13, 2023
1 parent 5846cef commit fd32735
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.batfish.datamodel.acl.AclLineMatchExprs.or;
import static org.batfish.datamodel.bgp.AllowRemoteAsOutMode.ALWAYS;
import static org.batfish.datamodel.bgp.AllowRemoteAsOutMode.EXCEPT_FIRST;
import static org.batfish.datamodel.bgp.AllowRemoteAsOutMode.NEVER;
import static org.batfish.datamodel.bgp.LocalOriginationTypeTieBreaker.NO_PREFERENCE;
import static org.batfish.datamodel.bgp.NextHopIpTieBreaker.HIGHEST_NEXT_HOP_IP;
import static org.batfish.datamodel.routing_policy.statement.Statements.ReturnFalse;
Expand Down Expand Up @@ -828,7 +829,7 @@ private NavigableMap<String, AuthenticationKeyChain> convertAuthenticationKeyCha
evpnAfBuilder
.setPropagateUnmatched(true)
.setAddressFamilyCapabilities(
AddressFamilyCapabilities.builder().setAllowRemoteAsOut(ALWAYS).build());
AddressFamilyCapabilities.builder().setAllowRemoteAsOut(NEVER).build());
evpnAfBuilder.setL3Vnis(convertL3Evpn().build());
neighbor.setEvpnAddressFamily(evpnAfBuilder.build());
}
Expand Down Expand Up @@ -2104,128 +2105,89 @@ private IkePhase1Proposal toIkePhase1Proposal(IkeProposal ikeProposal) {
return groupsBuilder.build();
}

private @Nullable ImmutableSet.Builder<Layer3VniConfig> convertL3Evpn() {
private @Nonnull ImmutableSet.Builder<Layer3VniConfig> convertL3Evpn() {
ImmutableSet.Builder<Layer3VniConfig> l3Vnis = ImmutableSet.builder();
for (Vlan vxlan : _masterLogicalSystem.getNamedVlans().values()) {
String l3Interface = vxlan.getL3Interface();

Check warning on line 2111 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2111

Added line #L2111 was not covered by tests
// vniId and l3interface must be defined for a l3vni.
if (vxlan.getVniId() == null || l3Interface == null) {
continue;
}

ExtendedCommunity routeTarget = determineRouteTarget(vxlan);

Check warning on line 2116 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2116

Added line #L2116 was not covered by tests

if (routeTarget == null) {
long num = Long.decode("0x10" + Integer.toHexString(vxlan.getVniId()));
routeTarget = getDefaultRouteTarget(num);

Check warning on line 2120 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2119-L2120

Added lines #L2119 - L2120 were not covered by tests
}

String createdRd = determineRouteDistinguisher(vxlan, l3Interface);

Check warning on line 2123 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2123

Added line #L2123 was not covered by tests

l3Vnis.add(buildLayer3VniConfig(vxlan, routeTarget, createdRd, l3Interface));
}

Check warning on line 2126 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2125-L2126

Added lines #L2125 - L2126 were not covered by tests

return l3Vnis;
}

private ExtendedCommunity determineRouteTarget(Vlan vxlan) {
ExtendedCommunity routeTarget = null;

Check warning on line 2132 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2132

Added line #L2132 was not covered by tests
if (_masterLogicalSystem.getVniOptions() != null
&& _masterLogicalSystem.getVniOptions().containsKey(vxlan.getVniId())) {
VniOptions vniOptions = _masterLogicalSystem.getVniOptions().get(vxlan.getVniId());

Check warning on line 2135 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2135

Added line #L2135 was not covered by tests
if (vniOptions.getVrfTargetCommunityorAuto().isAuto()) {
routeTarget = generateAutoRouteTarget(vxlan.getVniId());

Check warning on line 2137 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2137

Added line #L2137 was not covered by tests
} else {
// RT can be set at: evpn vni-options, RI, or switch-options.
ExtendedCommunity routeTarget = null;
// If route target is set under vni-options stanza it takes precedence
if (_masterLogicalSystem.getVniOptions() != null) {
if (_masterLogicalSystem.getVniOptions().get(vxlan.getVniId()) != null) {
if (_masterLogicalSystem
.getVniOptions()
.get(vxlan.getVniId())
.getVrfTargetCommunityorAuto()
.isAuto()) {
// RT-Auto is used.
// https://datatracker.ietf.org/doc/html/draft-ietf-bess-evpn-overlay-01#section-5.1.2.1
// format of ASN:x while ‘x’ is called the local administrator (4 octets long). ‘x’ is
// derived:
// The last 3 octets are used for the service ID – the VNI. (VNI -> Hex) 3 octets
// long, pad as necessary.
// The first octet is divided into several fields – bit 1 (0 for auto derived RT), the
// next 3 bits identify the type (1 for VXLAN), and the last 4 bits identify the
// domain ID (0, for this case). So this gives us 0x0001 0x0000, which makes it 0x10.
// In total, 0x10 and (hex of vni), thus 0x10<hex of vni> then -> decimal value.
long num = Long.decode("0x10" + Integer.toHexString(vxlan.getVniId()));
routeTarget =
ExtendedCommunity.parse(
"target:"
+ _masterLogicalSystem.getDefaultRoutingInstance().getAs()
+ ":"
+ num);
} else {
// RT is manually defined under vni-options
routeTarget =
_masterLogicalSystem
.getVniOptions()
.get(vxlan.getVniId())
.getVrfTargetCommunityorAuto()
.getExtendedCommunity();
}
}
routeTarget = vniOptions.getVrfTargetCommunityorAuto().getExtendedCommunity();

Check warning on line 2139 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2139

Added line #L2139 was not covered by tests
}
}
return routeTarget;

Check warning on line 2142 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2142

Added line #L2142 was not covered by tests
}

// Check for RT config under RI
} else if (getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance() != null) {
// TODO: Currently can't get RT from RI
continue;
// Finally, check for RT under switch-options
} else {
if (_masterLogicalSystem.getSwitchOptions().getVrfTargetCommunityorAuto().isAuto()) {
long num = Long.decode("0x10" + Integer.toHexString(vxlan.getVniId()));
routeTarget =
ExtendedCommunity.parse(
"target:"
+ _masterLogicalSystem.getDefaultRoutingInstance().getAs()
+ ":"
+ num);
} else {
routeTarget =
ExtendedCommunity.parse(
_masterLogicalSystem
.getSwitchOptions()
.getVrfTargetCommunityorAuto()
.toString());
}
}
if (routeTarget == null) {
long num = Long.decode("0x10" + Integer.toHexString(vxlan.getVniId()));
routeTarget =
ExtendedCommunity.parse(
"target:" + _masterLogicalSystem.getDefaultRoutingInstance().getAs() + ":" + num);
}
// Check if RD is manually configured under RI.
if (getInterfaceOrUnitByName(l3Interface)
private ExtendedCommunity generateAutoRouteTarget(int vniId) {
long num = Long.decode("0x10" + Integer.toHexString(vniId));
return ExtendedCommunity.parse(
"target:" + _masterLogicalSystem.getDefaultRoutingInstance().getAs() + ":" + num);

Check warning on line 2148 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2146-L2148

Added lines #L2146 - L2148 were not covered by tests
}

private ExtendedCommunity getDefaultRouteTarget(long num) {
return ExtendedCommunity.parse(
"target:" + _masterLogicalSystem.getDefaultRoutingInstance().getAs() + ":" + num);

Check warning on line 2153 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2152-L2153

Added lines #L2152 - L2153 were not covered by tests
}

private String determineRouteDistinguisher(Vlan vxlan, String l3Interface) {
String createdRd = null;

Check warning on line 2157 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2157

Added line #L2157 was not covered by tests
if (getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance() != null
&& getInterfaceOrUnitByName(l3Interface)
.get()
.getRoutingInstance()

Check warning on line 2161 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2159-L2161

Added lines #L2159 - L2161 were not covered by tests
.getRouteDistinguisherId()
!= null) {
String createdRd =
getInterfaceOrUnitByName(l3Interface)
.get()
.getRoutingInstance()
.getRouteDistinguisherId()
+ ":"
+ getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance().getAs();
l3Vnis.add(
Layer3VniConfig.builder()
.setAdvertiseV4Unicast(true)
.setVni(vxlan.getVniId())
.setRouteTarget(routeTarget)
.setImportRouteTarget(routeTarget.toString())
.setRouteDistinguisher(RouteDistinguisher.parse(createdRd))
.setVrf(
getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance().getName())
.build());
} else {
// RI is used, but RD is not manually configured. Junos takes route-distinguisher-id +
// random number to generate unique RD.
Random random = new Random();
int number = random.ints(4096, 9999).findFirst().getAsInt();
if (vxlan.getVlanId() != null) {
number = vxlan.getVlanId();
}
Ip rdIp = _masterLogicalSystem.getDefaultRoutingInstance().getRouteDistinguisherId();
String createdRd = rdIp.toString() + ":" + number;
l3Vnis.add(
Layer3VniConfig.builder()
.setAdvertiseV4Unicast(true)
.setVni(vxlan.getVniId())
.setRouteTarget(routeTarget)
.setImportRouteTarget(routeTarget.toString())
.setRouteDistinguisher(RouteDistinguisher.parse(createdRd))
.setVrf(
getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance().getName())
.build());
}
createdRd =
getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance().getRouteDistinguisherId()

Check warning on line 2165 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2164-L2165

Added lines #L2164 - L2165 were not covered by tests
+ ":"
+ getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance().getAs();

Check warning on line 2167 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2167

Added line #L2167 was not covered by tests
}
if (createdRd == null) {
Random random = new Random();
int number = random.ints(4096, 9999).findFirst().getAsInt();

Check warning on line 2171 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2170-L2171

Added lines #L2170 - L2171 were not covered by tests
if (vxlan.getVlanId() != null) {
number = vxlan.getVlanId();

Check warning on line 2173 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2173

Added line #L2173 was not covered by tests
}
Ip rdIp = _masterLogicalSystem.getDefaultRoutingInstance().getRouteDistinguisherId();
createdRd = rdIp.toString() + ":" + number;

Check warning on line 2176 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2175-L2176

Added lines #L2175 - L2176 were not covered by tests
}
return l3Vnis;
return createdRd;

Check warning on line 2178 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2178

Added line #L2178 was not covered by tests
}

private Layer3VniConfig buildLayer3VniConfig(
Vlan vxlan, ExtendedCommunity routeTarget, String createdRd, String l3Interface) {
return Layer3VniConfig.builder()
.setAdvertiseV4Unicast(true)
.setVni(vxlan.getVniId())
.setRouteTarget(routeTarget)
.setImportRouteTarget(routeTarget.toString())
.setRouteDistinguisher(RouteDistinguisher.parse(createdRd))
.setVrf(getInterfaceOrUnitByName(l3Interface).get().getRoutingInstance().getName())
.build();

Check warning on line 2190 in projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java

View check run for this annotation

Codecov / codecov/patch

projects/batfish/src/main/java/org/batfish/representation/juniper/JuniperConfiguration.java#L2183-L2190

Added lines #L2183 - L2190 were not covered by tests
}

private void convertL3Vni() {
Expand Down
12 changes: 6 additions & 6 deletions tests/parsing-tests/unit-tests-vimodel.ref
Original file line number Diff line number Diff line change
Expand Up @@ -40390,7 +40390,7 @@
"advertiseExternal" : false,
"advertiseInactive" : false,
"allowLocalAsIn" : false,
"allowRemoteAsOut" : "ALWAYS",
"allowRemoteAsOut" : "NEVER",
"sendCommunity" : false,
"sendExtendedCommunity" : false
},
Expand Down Expand Up @@ -109947,7 +109947,7 @@
"advertiseExternal" : false,
"advertiseInactive" : false,
"allowLocalAsIn" : false,
"allowRemoteAsOut" : "ALWAYS",
"allowRemoteAsOut" : "NEVER",
"sendCommunity" : false,
"sendExtendedCommunity" : false
},
Expand Down Expand Up @@ -130171,7 +130171,7 @@
"advertiseExternal" : false,
"advertiseInactive" : false,
"allowLocalAsIn" : false,
"allowRemoteAsOut" : "ALWAYS",
"allowRemoteAsOut" : "NEVER",
"sendCommunity" : false,
"sendExtendedCommunity" : false
},
Expand Down Expand Up @@ -130219,7 +130219,7 @@
"advertiseExternal" : false,
"advertiseInactive" : false,
"allowLocalAsIn" : false,
"allowRemoteAsOut" : "ALWAYS",
"allowRemoteAsOut" : "NEVER",
"sendCommunity" : false,
"sendExtendedCommunity" : false
},
Expand Down Expand Up @@ -130267,7 +130267,7 @@
"advertiseExternal" : false,
"advertiseInactive" : false,
"allowLocalAsIn" : false,
"allowRemoteAsOut" : "ALWAYS",
"allowRemoteAsOut" : "NEVER",
"sendCommunity" : false,
"sendExtendedCommunity" : false
},
Expand Down Expand Up @@ -130315,7 +130315,7 @@
"advertiseExternal" : false,
"advertiseInactive" : false,
"allowLocalAsIn" : false,
"allowRemoteAsOut" : "ALWAYS",
"allowRemoteAsOut" : "NEVER",
"sendCommunity" : false,
"sendExtendedCommunity" : false
},
Expand Down

0 comments on commit fd32735

Please sign in to comment.