use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments in project bgpcep by opendaylight.
the class MatchAsPathSetHandler method matchAsPathSetCondition.
private boolean matchAsPathSetCondition(final AsPath asPath, final String asPathSetName, final MatchSetOptionsType matchSetOptions) {
if (asPath == null) {
return false;
}
final AsPathSet asPathSetFilter = this.sets.getUnchecked(StringUtils.substringBetween(asPathSetName, "=\"", "\""));
final List<Segments> segments = asPath.getSegments();
if (asPathSetFilter == null || segments == null) {
return false;
}
final List<AsNumber> l1 = segments.stream().map(AsPathSegment::getAsSequence).filter(Objects::nonNull).flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList());
final List<AsNumber> l2 = segments.stream().map(AsPathSegment::getAsSet).filter(Objects::nonNull).flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList());
List<AsNumber> allAs = Stream.of(l1, l2).flatMap(Collection::stream).collect(Collectors.toList());
final List<AsNumber> asPathSetFilterList = asPathSetFilter.getAsPathSetMember();
if (matchSetOptions.equals(MatchSetOptionsType.ALL)) {
return allAs.containsAll(asPathSetFilterList) && asPathSetFilterList.containsAll(allAs);
}
final boolean noneInCommon = Collections.disjoint(allAs, asPathSetFilterList);
if (matchSetOptions.equals(MatchSetOptionsType.ANY)) {
return !noneInCommon;
}
// (matchSetOptions.equals(MatchSetOptionsType.INVERT))
return noneInCommon;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments in project bgpcep by opendaylight.
the class AbstractPrependAsPath method prependAS.
private static List<Segments> prependAS(final List<Segments> oldSegments, final AsNumber as) {
if (oldSegments == null || oldSegments.isEmpty()) {
return ImmutableList.of(singleSequence(as));
}
/*
* We need to check the first segment.
* If it has as-set then new as-sequence with local AS is prepended.
* If it has as-sequence, we may add local AS when it has less than 255 elements.
* Otherwise we need to create new as-sequence for local AS.
*/
final Iterator<Segments> it = oldSegments.iterator();
final Segments firstSegment = it.next();
final List<AsNumber> firstAsSequence = firstSegment.getAsSequence();
final List<Segments> newSegments;
if (firstAsSequence != null && firstAsSequence.size() < Values.UNSIGNED_BYTE_MAX_VALUE) {
final ArrayList<AsNumber> newAsSequence = new ArrayList<>(firstAsSequence.size() + 1);
newAsSequence.add(as);
newAsSequence.addAll(firstAsSequence);
newSegments = new ArrayList<>(oldSegments.size());
newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
} else {
newSegments = new ArrayList<>(oldSegments.size() + 1);
newSegments.add(singleSequence(as));
newSegments.add(firstSegment);
}
it.forEachRemaining(newSegments::add);
return newSegments;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments in project bgpcep by opendaylight.
the class AsPathAttributeParser method parseAsPath.
/**
* Parses AS_PATH from bytes.
*
* @param refCache ReferenceCache shared reference of object
* @param buffer bytes to be parsed
* @return new ASPath object
* @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
*/
private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer, final RevisedErrorHandling errorHandling) throws BGPDocumentedException, BGPTreatAsWithdrawException {
if (!buffer.isReadable()) {
return EMPTY;
}
final List<Segments> ases = new ArrayList<>();
boolean isSequence = false;
for (int readable = buffer.readableBytes(); readable != 0; readable = buffer.readableBytes()) {
if (readable < 2) {
throw errorHandling.reportError(BGPError.AS_PATH_MALFORMED, "Insufficient AS PATH segment header length %s", readable);
}
final int type = buffer.readUnsignedByte();
final SegmentType segmentType = AsPathSegmentParser.parseType(type);
if (segmentType == null) {
throw errorHandling.reportError(BGPError.AS_PATH_MALFORMED, "Unknown AS PATH segment type %s", type);
}
final int count = buffer.readUnsignedByte();
if (count == 0 && errorHandling != RevisedErrorHandling.NONE) {
throw new BGPTreatAsWithdrawException(BGPError.AS_PATH_MALFORMED, "Empty AS_PATH segment");
}
// We read 2 bytes of header at this point
readable -= 2;
final int segmentLength = count * AsPathSegmentParser.AS_NUMBER_LENGTH;
if (segmentLength > readable) {
throw errorHandling.reportError(BGPError.AS_PATH_MALFORMED, "Calculated segment length %s would overflow available buffer %s", segmentLength, readable);
}
final List<AsNumber> asList = AsPathSegmentParser.parseAsSegment(refCache, count, buffer.readSlice(segmentLength));
if (segmentType == SegmentType.AS_SEQUENCE) {
ases.add(new SegmentsBuilder().setAsSequence(asList).build());
isSequence = true;
} else {
ases.add(new SegmentsBuilder().setAsSet(asList).build());
}
}
if (!isSequence) {
throw errorHandling.reportError(BGPError.AS_PATH_MALFORMED, "AS_SEQUENCE must be present in AS_PATH attribute.");
}
return new AsPathBuilder().setSegments(ImmutableList.copyOf(ases)).build();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments in project bgpcep by opendaylight.
the class BGPMessageParserMockTest method fillMessages.
/**
* Helper method to fill messages variable.
*
* @param asn this parameter is passed to ASNumber constructor
*/
private static Update fillMessages(final long asn) {
final UpdateBuilder builder = new UpdateBuilder();
final List<Segments> asPath = new ArrayList<>();
asPath.add(new SegmentsBuilder().setAsSequence(Lists.newArrayList(new AsNumber(Uint32.valueOf(asn)))).build());
final CNextHop nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(new Ipv6AddressNoZone("2001:db8::1")).setLinkLocal(new Ipv6AddressNoZone("fe80::c001:bff:fe7e:0")).build()).build();
final Ipv6Prefix pref1 = new Ipv6Prefix("2001:db8:1:2::/64");
final Ipv6Prefix pref2 = new Ipv6Prefix("2001:db8:1:1::/64");
final Ipv6Prefix pref3 = new Ipv6Prefix("2001:db8:1::/64");
final AttributesBuilder paBuilder = new AttributesBuilder();
paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
final MpReachNlriBuilder mpReachBuilder = new MpReachNlriBuilder();
mpReachBuilder.setAfi(Ipv6AddressFamily.class);
mpReachBuilder.setSafi(UnicastSubsequentAddressFamily.class);
mpReachBuilder.setCNextHop(nextHop);
mpReachBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(Lists.newArrayList(new Ipv6PrefixesBuilder().setPrefix(pref1).build(), new Ipv6PrefixesBuilder().setPrefix(pref2).build(), new Ipv6PrefixesBuilder().setPrefix(pref3).build())).build()).build()).build());
paBuilder.addAugmentation(new AttributesReachBuilder().setMpReachNlri(mpReachBuilder.build()).build());
builder.setAttributes(paBuilder.build());
return builder.build();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments in project bgpcep by opendaylight.
the class BestPathStateImpl method extractSegments.
private List<Segments> extractSegments(final UnkeyedListNode segments, final NamespaceSpecificIds ids) {
// list segments
final List<Segments> extracted = new ArrayList<>();
for (final UnkeyedListEntryNode segment : segments.body()) {
final SegmentsBuilder sb = new SegmentsBuilder();
// We are expecting that segment contains either as-sequence or as-set,
// so just one of them will be set, other would be null
sb.setAsSequence(extractAsList(segment, ids.asSeqNid)).setAsSet(extractAsList(segment, ids.asSetNid));
extracted.add(sb.build());
}
return extracted;
}
Aggregations