Rework prependAS()
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / actions / AbstractPrependAsPath.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import org.opendaylight.protocol.util.Values;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
21
22 abstract class AbstractPrependAsPath {
23
24     static final Attributes prependAS(final Attributes attributes, final AsNumber as) {
25         return new AttributesBuilder(attributes)
26                 .setAsPath(new AsPathBuilder()
27                     .setSegments(prependAS(attributes.getAsPath().getSegments(), as))
28                     .build())
29                 .build();
30     }
31
32     private static List<Segments> prependAS(final List<Segments> oldSegments, final AsNumber as) {
33         if (oldSegments == null || oldSegments.isEmpty()) {
34             return ImmutableList.of(singleSequence(as));
35         }
36
37         /*
38          * We need to check the first segment.
39          * If it has as-set then new as-sequence with local AS is prepended.
40          * If it has as-sequence, we may add local AS when it has less than 255 elements.
41          * Otherwise we need to create new as-sequence for local AS.
42          */
43         final Iterator<Segments> it = oldSegments.iterator();
44         final Segments firstSegment = it.next();
45         final List<AsNumber> firstAsSequence = firstSegment.getAsSequence();
46
47         final List<Segments> newSegments;
48         if (firstAsSequence != null && firstAsSequence.size() < Values.UNSIGNED_BYTE_MAX_VALUE) {
49             final ArrayList<AsNumber> newAsSequence = new ArrayList<>(firstAsSequence.size() + 1);
50             newAsSequence.add(as);
51             newAsSequence.addAll(firstAsSequence);
52
53             newSegments = new ArrayList<>(oldSegments.size());
54             newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
55         } else {
56             newSegments = new ArrayList<>(oldSegments.size() + 1);
57             newSegments.add(singleSequence(as));
58             newSegments.add(firstSegment);
59         }
60
61         it.forEachRemaining(newSegments::add);
62         return newSegments;
63     }
64
65     private static Segments singleSequence(final AsNumber as) {
66         return new SegmentsBuilder().setAsSequence(ImmutableList.of(as)).build();
67     }
68 }