2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.protocol.util.Values;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder;
21 class AbstractPrependAsPath {
23 final Attributes prependAS(final Attributes attributes, final AsNumber as) {
24 final List<Segments> oldSegments = attributes.getAsPath().getSegments();
26 * We need to check the first segment.
27 * If it has as-set then new as-sequence with local AS is prepended.
28 * If it has as-sequence, we may add local AS when it has less than 255 elements.
29 * Otherwise we need to create new as-sequence for local AS.
31 final ArrayList<AsNumber> newAsSequence = new ArrayList<>();
32 newAsSequence.add(new AsNumber(as));
34 List<Segments> newSegments = new ArrayList<>();
35 if (oldSegments == null || oldSegments.isEmpty()) {
36 newSegments = new ArrayList<>();
37 newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
39 final Segments firstSegment = oldSegments.remove(0);
40 final List<AsNumber> firstAsSequence = firstSegment.getAsSequence();
41 if (firstAsSequence != null && firstAsSequence.size() < Values.UNSIGNED_BYTE_MAX_VALUE) {
42 newAsSequence.addAll(firstAsSequence);
43 newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
45 newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
46 newSegments.add(firstSegment);
48 newSegments.addAll(oldSegments);
50 return new AttributesBuilder(attributes).setAsPath(new AsPathBuilder()
51 .setSegments(newSegments).build()).build();