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