062fe7da2b8646fd6a6aff7e2e0525d7cc0d3978
[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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions;
10
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;
20
21 class AbstractPrependAsPath {
22
23     final Attributes prependAS(final Attributes attributes, final AsNumber as) {
24         final List<Segments> oldSegments = attributes.getAsPath().getSegments();
25         /*
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.
30          */
31         final ArrayList<AsNumber> newAsSequence = new ArrayList<>();
32         newAsSequence.add(new AsNumber(as));
33
34         List<Segments> newSegments = new ArrayList<>();
35         if (oldSegments == null || oldSegments.isEmpty()) {
36             newSegments = new ArrayList<>();
37             newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
38         } else {
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());
44             } else {
45                 newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
46                 newSegments.add(firstSegment);
47             }
48             newSegments.addAll(oldSegments);
49         }
50         return new AttributesBuilder(attributes).setAsPath(new AsPathBuilder()
51                 .setSegments(newSegments).build()).build();
52     }
53 }