2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.Maps;
15 import java.util.Map.Entry;
17 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.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;
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.
32 public class BGPSynchronization {
34 private static final Logger LOG = LoggerFactory.getLogger(BGPSynchronization.class);
37 static class SyncVariables {
39 private boolean upd = false;
40 private boolean eor = false;
42 public void setUpd(final boolean upd) {
46 public void setEorTrue() {
50 public boolean getEor() {
54 public boolean getUpd() {
60 public final Map<TablesKey, SyncVariables> syncStorage = Maps.newHashMap();
62 private final BGPSessionListener listener;
64 public BGPSynchronization(final BGPSessionListener listener, final Set<TablesKey> types) {
65 this.listener = requireNonNull(listener);
67 for (final TablesKey type : types) {
68 this.syncStorage.put(type, new SyncVariables());
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.
78 * @param msg received Update message
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());
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());
95 if (pa.getMpUnreachNlri().getWithdrawnRoutes() == null) {
96 // EOR message contains only MPUnreach attribute and no NLRI
101 // true for empty Update Message
105 syncType(type, isEOR);
108 private void syncType(final TablesKey type, final boolean isEOR) {
109 final SyncVariables s = this.syncStorage.get(type);
111 LOG.warn("BGPTableType was not present in open message : {}", type);
117 this.listener.markUptodate(type);
118 LOG.info("BGP Synchronization finished for table {} ", type);
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
127 public void kaReceived() {
128 for (final Entry<TablesKey, SyncVariables> entry : this.syncStorage.entrySet()) {
129 final SyncVariables s = entry.getValue();
133 LOG.info("BGP Synchronization finished for table {} ", entry.getKey());
134 this.listener.markUptodate(entry.getKey());