063169f645938efcb156668f5dccdadc4d6c1ca9
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / actions / AsPathPrepend.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.Collections;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
15 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.action.BgpActionPolicy;
16 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
17 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetAsPathPrepend;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder;
26
27 /**
28  * Prepend local AS, one time(n times not supported yet).
29  */
30 public final class AsPathPrepend implements BgpActionPolicy<SetAsPathPrepend> {
31     @Override
32     public Attributes applyImportAction(
33             final RouteEntryBaseAttributes routeEntryInfo,
34             final BGPRouteEntryImportParameters routeEntryImportParameters,
35             final Attributes attributes,
36             final SetAsPathPrepend bgpActions) {
37         return null;
38     }
39
40     @Override
41     public Attributes applyExportAction(
42             final RouteEntryBaseAttributes routeEntryInfo,
43             final BGPRouteEntryExportParameters exportParameters,
44             final Attributes attributes,
45             final SetAsPathPrepend actions) {
46
47         final List<Segments> oldSegments = attributes.getAsPath().getSegments();
48         /*
49          * We need to check the first segment.
50          * If it has as-set then new as-sequence with local AS is prepended.
51          * If it has as-sequence, we may add local AS when it has less than 255 elements.
52          * Otherwise we need to create new as-sequence for local AS.
53          */
54         final ArrayList<AsNumber> newAsSequence = new ArrayList<>();
55         newAsSequence.add(new AsNumber(routeEntryInfo.getLocalAs()));
56
57         List<Segments> newSegments = new ArrayList<>();
58         if (oldSegments == null || oldSegments.isEmpty()) {
59             newSegments = Collections.singletonList(new SegmentsBuilder().setAsSequence(newAsSequence).build());
60         } else {
61             final Segments firstSegment = oldSegments.remove(0);
62             final List<AsNumber> firstAsSequence = firstSegment.getAsSequence();
63             if (firstAsSequence != null && firstAsSequence.size() < Values.UNSIGNED_BYTE_MAX_VALUE) {
64                 newAsSequence.addAll(firstAsSequence);
65                 newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
66             } else {
67                 newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build());
68                 newSegments.add(firstSegment);
69             }
70             newSegments.addAll(oldSegments);
71         }
72         return new AttributesBuilder(attributes).setAsPath(new AsPathBuilder()
73                 .setSegments(newSegments).build()).build();
74     }
75 }