Update MRI projects for Aluminium
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BgpPeerUtil.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.rib.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateMessage;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
23
24 public final class BgpPeerUtil {
25     private BgpPeerUtil() {
26         // Hidden on purpose
27     }
28
29     /**
30      * Creates UPDATE message that contains EOR marker for specified address family.
31      *
32      * @param key of family for which we need EOR
33      * @return UPDATE message with EOR marker
34      */
35     public static Update createEndOfRib(final TablesKey key) {
36         return key.getAfi() == Ipv4AddressFamily.class && key.getSafi() == UnicastSubsequentAddressFamily.class
37                 ? new UpdateBuilder().build() : new UpdateBuilder()
38                         .setAttributes(new AttributesBuilder()
39                             .addAugmentation(new Attributes2Builder()
40                                 .setMpUnreachNlri(new MpUnreachNlriBuilder()
41                                     .setAfi(key.getAfi())
42                                     .setSafi(key.getSafi())
43                                     .build())
44                                 .build())
45                             .build())
46                         .build();
47     }
48
49     /**
50      * Verify presence of EOR marker in UPDATE message.
51      *
52      * @param msg UPDATE message to be checked
53      * @return True if message contains EOR marker, otherwise return False
54      */
55     public static boolean isEndOfRib(final UpdateMessage msg) {
56         if (msg.getNlri() == null && msg.getWithdrawnRoutes() == null) {
57             if (msg.getAttributes() != null) {
58                 final Attributes2 pa = msg.getAttributes().augmentation(Attributes2.class);
59                 if (pa != null && msg.getAttributes().augmentation(Attributes1.class) == null) {
60                     //only MP_UNREACH_NLRI allowed in EOR
61                     if (pa.getMpUnreachNlri() != null && pa.getMpUnreachNlri().getWithdrawnRoutes() == null) {
62                         // EOR message contains only MPUnreach attribute and no NLRI
63                         return true;
64                     }
65                 }
66             } else {
67                 // true for empty IPv4 Unicast
68                 return true;
69             }
70         }
71         return false;
72     }
73
74     /**
75      * DTO for transferring LLGR advertizements.
76      *
77      * @deprecated This class is deprecated for refactoring.
78      */
79     // FIXME: there should be no need for this class, as we should be able to efficiently translate TableKey classes
80     //        and rely on yang-parser-api.
81     @Deprecated
82     public static final class LlGracefulRestartDTO {
83
84         private final TablesKey tableKey;
85         private final int staleTime;
86         private final boolean forwardingFlag;
87
88         public LlGracefulRestartDTO(final TablesKey tableKey, final int staleTime, final boolean forwardingFlag) {
89             this.tableKey = requireNonNull(tableKey);
90             this.staleTime = staleTime;
91             this.forwardingFlag = forwardingFlag;
92         }
93
94         public TablesKey getTableKey() {
95             return tableKey;
96         }
97
98         public int getStaleTime() {
99             return staleTime;
100         }
101
102         public boolean isForwarding() {
103             return forwardingFlag;
104         }
105     }
106 }