Bump versions by x.y.(z+1)
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSynchronization.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.Set;
16 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * BGP speaker (without Graceful restart capability) sends KeepAlive message after sending all initial Update messages
28  * with certain AFI/SAFI. For each AFI/SAFI, it sends one KA message. As it is undetermined which KA message belongs to
29  * which AFI/SAFI, an algorithm needed to be implemented.
30  */
31 public class BGPSynchronization {
32
33     private static final Logger LOG = LoggerFactory.getLogger(BGPSynchronization.class);
34
35     @VisibleForTesting
36     static class SyncVariables {
37
38         private boolean upd = false;
39         private boolean eor = false;
40
41         public void setUpd(final boolean upd) {
42             this.upd = upd;
43         }
44
45         public void setEorTrue() {
46             this.eor = true;
47         }
48
49         public boolean getEor() {
50             return this.eor;
51         }
52
53         public boolean getUpd() {
54             return this.upd;
55         }
56     }
57
58     @VisibleForTesting
59     public final Map<TablesKey, SyncVariables> syncStorage = Maps.newHashMap();
60
61     private final BGPSessionListener listener;
62
63     public BGPSynchronization(final BGPSessionListener listener, final Set<TablesKey> types) {
64         this.listener = Preconditions.checkNotNull(listener);
65
66         for (final TablesKey type : types) {
67             this.syncStorage.put(type, new SyncVariables());
68         }
69     }
70
71     /**
72      * For each received Update message, the upd sync variable needs to be updated to true, for particular AFI/SAFI
73      * combination. Currently we only assume Unicast SAFI. From the Update message we have to extract the AFI. Each
74      * Update message can contain BGP Object with one type of AFI. If the object is BGP Link, BGP Node or a BGPPrefix
75      * the AFI is Linkstate. In case of BGPRoute, the AFI depends on the IP Address of the prefix.
76      *
77      * @param msg received Update message
78      */
79     public void updReceived(final Update msg) {
80         TablesKey type = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
81         boolean isEOR = false;
82         if (msg.getNlri() == null && msg.getWithdrawnRoutes() == null) {
83             if (msg.getAttributes() != null) {
84                 if (msg.getAttributes().getAugmentation(Attributes1.class) != null) {
85                     final Attributes1 pa = msg.getAttributes().getAugmentation(Attributes1.class);
86                     if (pa.getMpReachNlri() != null) {
87                         type = new TablesKey(pa.getMpReachNlri().getAfi(), pa.getMpReachNlri().getSafi());
88                     }
89                 } else if (msg.getAttributes().getAugmentation(Attributes2.class) != null) {
90                     final Attributes2 pa = msg.getAttributes().getAugmentation(Attributes2.class);
91                     if (pa.getMpUnreachNlri() != null) {
92                         type = new TablesKey(pa.getMpUnreachNlri().getAfi(), pa.getMpUnreachNlri().getSafi());
93                     }
94                     if (pa.getMpUnreachNlri().getWithdrawnRoutes() == null) {
95                         // EOR message contains only MPUnreach attribute and no NLRI
96                         isEOR = true;
97                     }
98                 }
99             } else {
100                 // true for empty Update Message
101                 isEOR = true;
102             }
103         }
104         syncType(type, isEOR);
105     }
106
107     private void syncType(final TablesKey type, final boolean isEOR) {
108         final SyncVariables s = this.syncStorage.get(type);
109         if (s == null) {
110             LOG.warn("BGPTableType was not present in open message : {}", type);
111             return;
112         }
113         s.setUpd(true);
114         if (isEOR) {
115             s.setEorTrue();
116             this.listener.markUptodate(type);
117             LOG.info("BGP Synchronization finished for table {} ", type);
118         }
119     }
120
121     /**
122      * This method is called, when the second KA message is received. It checks each AFI/SAFI sync variables. If they
123      * are all false, which means, that there was at least one update message followed by one KA, the EOR is sent to
124      * session.
125      */
126     public void kaReceived() {
127         for (final Entry<TablesKey, SyncVariables> entry : this.syncStorage.entrySet()) {
128             final SyncVariables s = entry.getValue();
129             if (!s.getEor()) {
130                 if (!s.getUpd()) {
131                     s.setEorTrue();
132                     LOG.info("BGP Synchronization finished for table {} ", entry.getKey());
133                     this.listener.markUptodate(entry.getKey());
134                 }
135                 s.setUpd(false);
136             }
137         }
138     }
139 }