send EOR marker after initial route exchange
[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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateMessage;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
21
22 public final class BgpPeerUtil {
23
24     private BgpPeerUtil() {
25         throw new UnsupportedOperationException();
26     }
27
28     /**
29      * Creates UPDATE message that contains EOR marker for specified address family.
30      *
31      * @param key of family for which we need EOR
32      * @return UPDATE message with EOR marker
33      */
34     public static Update createEndOfRib(final TablesKey key) {
35         return key.getAfi() == Ipv4AddressFamily.class && key.getSafi() == UnicastSubsequentAddressFamily.class
36                 ? new UpdateBuilder().build() :
37                 new UpdateBuilder()
38                         .setAttributes(new AttributesBuilder()
39                                 .addAugmentation(Attributes2.class, new Attributes2Builder()
40                                         .setMpUnreachNlri(new MpUnreachNlriBuilder()
41                                                 .setAfi(key.getAfi())
42                                                 .setSafi(key.getSafi())
43                                                 .build()).build()).build()).build();
44     }
45
46     /**
47      * Verify presence of EOR marker in UPDATE message.
48      *
49      * @param msg UPDATE message to be checked
50      * @return True if message contains EOR marker, otherwise return False
51      */
52     public static boolean isEndOfRib(final UpdateMessage msg) {
53         if (msg.getNlri() == null && msg.getWithdrawnRoutes() == null) {
54             if (msg.getAttributes() != null) {
55                 final Attributes2 pa = msg.getAttributes().augmentation(Attributes2.class);
56                 if (pa != null && msg.getAttributes().augmentation(Attributes1.class) == null) {
57                     //only MP_UNREACH_NLRI allowed in EOR
58                     if (pa.getMpUnreachNlri() != null && pa.getMpUnreachNlri().getWithdrawnRoutes() == null) {
59                         // EOR message contains only MPUnreach attribute and no NLRI
60                         return true;
61                     }
62                 }
63             } else {
64                 // true for empty IPv4 Unicast
65                 return true;
66             }
67         }
68         return false;
69     }
70
71 }