@Test public void testInvalidType() { thrown.expect(IllegalArgumentException.class); ExtendedCommunity.parse("1024:1:1"); }
@Nonnull public static ExtendedCommunity parse(String communityStr) { String[] parts = communityStr.trim().split(":"); checkArgument(parts.length == 3, "Invalid extended community string: %s", communityStr); long gaLong; long laLong; String subType = parts[0].toLowerCase(); String globalAdministrator = parts[1].toLowerCase(); String localAdministrator = parts[2].toLowerCase(); Byte typeByte = null; byte subTypeByte; if (subType.equals("target")) { subTypeByte = 0x02; } else if (subType.equals("origin")) { subTypeByte = 0x03; } else { Integer intVal = Ints.tryParse(subType); if (intVal != null && intVal <= 0xFFFF && intVal >= 0) { subTypeByte = (byte) (intVal & 0xFF); typeByte = (byte) ((intVal & 0xFF00) >> 8); } else { throw new IllegalArgumentException( String.format("Invalid extended community type: %s", communityStr)); } } laLong = Long.parseUnsignedLong(localAdministrator); String[] gaParts = globalAdministrator.split("\\."); if (gaParts.length == 4) { typeByte = firstNonNull(typeByte, (byte) 0x01); gaLong = Ip.parse(globalAdministrator).asLong(); checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else if (gaParts.length == 2) { typeByte = firstNonNull(typeByte, (byte) 0x02); int hi = Integer.parseUnsignedInt(gaParts[0]); int lo = Integer.parseUnsignedInt(gaParts[1]); checkArgument( hi <= 0xFFFF && lo <= 0xFFFF, "Invalid global administrator value %s", globalAdministrator); gaLong = ((long) hi) << 16 | lo; checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { if (globalAdministrator.endsWith("l")) { typeByte = firstNonNull(typeByte, (byte) 0x02); gaLong = Long.parseUnsignedLong( globalAdministrator.substring(0, globalAdministrator.length() - 1)); checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { gaLong = Long.parseUnsignedLong(globalAdministrator); if (gaLong <= 0xFFFFL) { checkArgument( laLong <= 0xFFFFFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x00); } else { checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x02); } } } return of((int) typeByte << 8 | (int) subTypeByte, gaLong, laLong); }
ExtendedCommunity extends Community { @Nonnull public static ExtendedCommunity parse(String communityStr) { String[] parts = communityStr.trim().split(":"); checkArgument(parts.length == 3, "Invalid extended community string: %s", communityStr); long gaLong; long laLong; String subType = parts[0].toLowerCase(); String globalAdministrator = parts[1].toLowerCase(); String localAdministrator = parts[2].toLowerCase(); Byte typeByte = null; byte subTypeByte; if (subType.equals("target")) { subTypeByte = 0x02; } else if (subType.equals("origin")) { subTypeByte = 0x03; } else { Integer intVal = Ints.tryParse(subType); if (intVal != null && intVal <= 0xFFFF && intVal >= 0) { subTypeByte = (byte) (intVal & 0xFF); typeByte = (byte) ((intVal & 0xFF00) >> 8); } else { throw new IllegalArgumentException( String.format("Invalid extended community type: %s", communityStr)); } } laLong = Long.parseUnsignedLong(localAdministrator); String[] gaParts = globalAdministrator.split("\\."); if (gaParts.length == 4) { typeByte = firstNonNull(typeByte, (byte) 0x01); gaLong = Ip.parse(globalAdministrator).asLong(); checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else if (gaParts.length == 2) { typeByte = firstNonNull(typeByte, (byte) 0x02); int hi = Integer.parseUnsignedInt(gaParts[0]); int lo = Integer.parseUnsignedInt(gaParts[1]); checkArgument( hi <= 0xFFFF && lo <= 0xFFFF, "Invalid global administrator value %s", globalAdministrator); gaLong = ((long) hi) << 16 | lo; checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { if (globalAdministrator.endsWith("l")) { typeByte = firstNonNull(typeByte, (byte) 0x02); gaLong = Long.parseUnsignedLong( globalAdministrator.substring(0, globalAdministrator.length() - 1)); checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { gaLong = Long.parseUnsignedLong(globalAdministrator); if (gaLong <= 0xFFFFL) { checkArgument( laLong <= 0xFFFFFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x00); } else { checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x02); } } } return of((int) typeByte << 8 | (int) subTypeByte, gaLong, laLong); } }
ExtendedCommunity extends Community { @Nonnull public static ExtendedCommunity parse(String communityStr) { String[] parts = communityStr.trim().split(":"); checkArgument(parts.length == 3, "Invalid extended community string: %s", communityStr); long gaLong; long laLong; String subType = parts[0].toLowerCase(); String globalAdministrator = parts[1].toLowerCase(); String localAdministrator = parts[2].toLowerCase(); Byte typeByte = null; byte subTypeByte; if (subType.equals("target")) { subTypeByte = 0x02; } else if (subType.equals("origin")) { subTypeByte = 0x03; } else { Integer intVal = Ints.tryParse(subType); if (intVal != null && intVal <= 0xFFFF && intVal >= 0) { subTypeByte = (byte) (intVal & 0xFF); typeByte = (byte) ((intVal & 0xFF00) >> 8); } else { throw new IllegalArgumentException( String.format("Invalid extended community type: %s", communityStr)); } } laLong = Long.parseUnsignedLong(localAdministrator); String[] gaParts = globalAdministrator.split("\\."); if (gaParts.length == 4) { typeByte = firstNonNull(typeByte, (byte) 0x01); gaLong = Ip.parse(globalAdministrator).asLong(); checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else if (gaParts.length == 2) { typeByte = firstNonNull(typeByte, (byte) 0x02); int hi = Integer.parseUnsignedInt(gaParts[0]); int lo = Integer.parseUnsignedInt(gaParts[1]); checkArgument( hi <= 0xFFFF && lo <= 0xFFFF, "Invalid global administrator value %s", globalAdministrator); gaLong = ((long) hi) << 16 | lo; checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { if (globalAdministrator.endsWith("l")) { typeByte = firstNonNull(typeByte, (byte) 0x02); gaLong = Long.parseUnsignedLong( globalAdministrator.substring(0, globalAdministrator.length() - 1)); checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { gaLong = Long.parseUnsignedLong(globalAdministrator); if (gaLong <= 0xFFFFL) { checkArgument( laLong <= 0xFFFFFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x00); } else { checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x02); } } } return of((int) typeByte << 8 | (int) subTypeByte, gaLong, laLong); } private  ExtendedCommunity(
      byte type, byte subType, long globalAdministrator, long localAdministrator); }
ExtendedCommunity extends Community { @Nonnull public static ExtendedCommunity parse(String communityStr) { String[] parts = communityStr.trim().split(":"); checkArgument(parts.length == 3, "Invalid extended community string: %s", communityStr); long gaLong; long laLong; String subType = parts[0].toLowerCase(); String globalAdministrator = parts[1].toLowerCase(); String localAdministrator = parts[2].toLowerCase(); Byte typeByte = null; byte subTypeByte; if (subType.equals("target")) { subTypeByte = 0x02; } else if (subType.equals("origin")) { subTypeByte = 0x03; } else { Integer intVal = Ints.tryParse(subType); if (intVal != null && intVal <= 0xFFFF && intVal >= 0) { subTypeByte = (byte) (intVal & 0xFF); typeByte = (byte) ((intVal & 0xFF00) >> 8); } else { throw new IllegalArgumentException( String.format("Invalid extended community type: %s", communityStr)); } } laLong = Long.parseUnsignedLong(localAdministrator); String[] gaParts = globalAdministrator.split("\\."); if (gaParts.length == 4) { typeByte = firstNonNull(typeByte, (byte) 0x01); gaLong = Ip.parse(globalAdministrator).asLong(); checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else if (gaParts.length == 2) { typeByte = firstNonNull(typeByte, (byte) 0x02); int hi = Integer.parseUnsignedInt(gaParts[0]); int lo = Integer.parseUnsignedInt(gaParts[1]); checkArgument( hi <= 0xFFFF && lo <= 0xFFFF, "Invalid global administrator value %s", globalAdministrator); gaLong = ((long) hi) << 16 | lo; checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { if (globalAdministrator.endsWith("l")) { typeByte = firstNonNull(typeByte, (byte) 0x02); gaLong = Long.parseUnsignedLong( globalAdministrator.substring(0, globalAdministrator.length() - 1)); checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { gaLong = Long.parseUnsignedLong(globalAdministrator); if (gaLong <= 0xFFFFL) { checkArgument( laLong <= 0xFFFFFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x00); } else { checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x02); } } } return of((int) typeByte << 8 | (int) subTypeByte, gaLong, laLong); } private  ExtendedCommunity(
      byte type, byte subType, long globalAdministrator, long localAdministrator); @Nonnull static ExtendedCommunity parse(String communityStr); @Nonnull static Optional<ExtendedCommunity> tryParse(String text); static ExtendedCommunity of(int type, long globalAdministrator, long localAdministrator); static ExtendedCommunity of(int type, Ip globalAdministrator, long localAdministrator); static ExtendedCommunity target(long asn, long value); @Override T accept(CommunityVisitor<T> visitor); @Override boolean isTransitive(); boolean isRouteOrigin(); boolean isRouteTarget(); boolean isVpnDistinguisher(); long getGlobalAdministrator(); long getLocalAdministrator(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); @Nonnull @Override String matchString(); @Override @Nonnull @JsonValue String toString(); }
ExtendedCommunity extends Community { @Nonnull public static ExtendedCommunity parse(String communityStr) { String[] parts = communityStr.trim().split(":"); checkArgument(parts.length == 3, "Invalid extended community string: %s", communityStr); long gaLong; long laLong; String subType = parts[0].toLowerCase(); String globalAdministrator = parts[1].toLowerCase(); String localAdministrator = parts[2].toLowerCase(); Byte typeByte = null; byte subTypeByte; if (subType.equals("target")) { subTypeByte = 0x02; } else if (subType.equals("origin")) { subTypeByte = 0x03; } else { Integer intVal = Ints.tryParse(subType); if (intVal != null && intVal <= 0xFFFF && intVal >= 0) { subTypeByte = (byte) (intVal & 0xFF); typeByte = (byte) ((intVal & 0xFF00) >> 8); } else { throw new IllegalArgumentException( String.format("Invalid extended community type: %s", communityStr)); } } laLong = Long.parseUnsignedLong(localAdministrator); String[] gaParts = globalAdministrator.split("\\."); if (gaParts.length == 4) { typeByte = firstNonNull(typeByte, (byte) 0x01); gaLong = Ip.parse(globalAdministrator).asLong(); checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else if (gaParts.length == 2) { typeByte = firstNonNull(typeByte, (byte) 0x02); int hi = Integer.parseUnsignedInt(gaParts[0]); int lo = Integer.parseUnsignedInt(gaParts[1]); checkArgument( hi <= 0xFFFF && lo <= 0xFFFF, "Invalid global administrator value %s", globalAdministrator); gaLong = ((long) hi) << 16 | lo; checkArgument(laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { if (globalAdministrator.endsWith("l")) { typeByte = firstNonNull(typeByte, (byte) 0x02); gaLong = Long.parseUnsignedLong( globalAdministrator.substring(0, globalAdministrator.length() - 1)); checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); } else { gaLong = Long.parseUnsignedLong(globalAdministrator); if (gaLong <= 0xFFFFL) { checkArgument( laLong <= 0xFFFFFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x00); } else { checkArgument( gaLong <= 0xFFFFFFFFL, "Invalid global administrator value %s", globalAdministrator); checkArgument( laLong <= 0xFFFFL, "Invalid local administrator value %s", localAdministrator); typeByte = firstNonNull(typeByte, (byte) 0x02); } } } return of((int) typeByte << 8 | (int) subTypeByte, gaLong, laLong); } private  ExtendedCommunity(
      byte type, byte subType, long globalAdministrator, long localAdministrator); @Nonnull static ExtendedCommunity parse(String communityStr); @Nonnull static Optional<ExtendedCommunity> tryParse(String text); static ExtendedCommunity of(int type, long globalAdministrator, long localAdministrator); static ExtendedCommunity of(int type, Ip globalAdministrator, long localAdministrator); static ExtendedCommunity target(long asn, long value); @Override T accept(CommunityVisitor<T> visitor); @Override boolean isTransitive(); boolean isRouteOrigin(); boolean isRouteTarget(); boolean isVpnDistinguisher(); long getGlobalAdministrator(); long getLocalAdministrator(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); @Nonnull @Override String matchString(); @Override @Nonnull @JsonValue String toString();  }