a58818faa29d37744f216946a1cb31d6d6adf6bb
[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() :
38                 new UpdateBuilder()
39                         .setAttributes(new AttributesBuilder()
40                                 .addAugmentation(Attributes2.class, new Attributes2Builder()
41                                         .setMpUnreachNlri(new MpUnreachNlriBuilder()
42                                                 .setAfi(key.getAfi())
43                                                 .setSafi(key.getSafi())
44                                                 .build()).build()).build()).build();
45     }
46
47     /**
48      * Verify presence of EOR marker in UPDATE message.
49      *
50      * @param msg UPDATE message to be checked
51      * @return True if message contains EOR marker, otherwise return False
52      */
53     public static boolean isEndOfRib(final UpdateMessage msg) {
54         if (msg.getNlri() == null && msg.getWithdrawnRoutes() == null) {
55             if (msg.getAttributes() != null) {
56                 final Attributes2 pa = msg.getAttributes().augmentation(Attributes2.class);
57                 if (pa != null && msg.getAttributes().augmentation(Attributes1.class) == null) {
58                     //only MP_UNREACH_NLRI allowed in EOR
59                     if (pa.getMpUnreachNlri() != null && pa.getMpUnreachNlri().getWithdrawnRoutes() == null) {
60                         // EOR message contains only MPUnreach attribute and no NLRI
61                         return true;
62                     }
63                 }
64             } else {
65                 // true for empty IPv4 Unicast
66                 return true;
67             }
68         }
69         return false;
70     }
71
72     /**
73      * DTO for transferring LLGR advertizements.
74      *
75      * @deprecated This class is deprecated for refactoring.
76      */
77     // FIXME: there should be no need for this class, as we should be able to efficiently translate TableKey classes
78     //        and rely on yang-parser-api.
79     @Deprecated
80     public static final class LlGracefulRestartDTO {
81
82         private final TablesKey tableKey;
83         private final int staleTime;
84         private final boolean forwardingFlag;
85
86         public LlGracefulRestartDTO(final TablesKey tableKey, final int staleTime, final boolean forwardingFlag) {
87             this.tableKey = requireNonNull(tableKey);
88             this.staleTime = staleTime;
89             this.forwardingFlag = forwardingFlag;
90         }
91
92         public TablesKey getTableKey() {
93             return tableKey;
94         }
95
96         public int getStaleTime() {
97             return staleTime;
98         }
99
100         public boolean isForwarding() {
101             return forwardingFlag;
102         }
103     }
104 }