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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
14 import java.util.Map.Entry;
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;
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.
31 public class BGPSynchronization {
33 private static final Logger LOG = LoggerFactory.getLogger(BGPSynchronization.class);
36 static class SyncVariables {
38 private boolean upd = false;
39 private boolean eor = false;
41 public void setUpd(final boolean upd) {
45 public void setEorTrue() {
49 public boolean getEor() {
53 public boolean getUpd() {
59 public final Map<TablesKey, SyncVariables> syncStorage = Maps.newHashMap();
61 private final BGPSessionListener listener;
63 public BGPSynchronization(final BGPSessionListener listener, final Set<TablesKey> types) {
64 this.listener = Preconditions.checkNotNull(listener);
66 for (final TablesKey type : types) {
67 this.syncStorage.put(type, new SyncVariables());
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.
77 * @param msg received Update message
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());
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());
94 if (pa.getMpUnreachNlri().getWithdrawnRoutes() == null) {
95 // EOR message contains only MPUnreach attribute and no NLRI
100 // true for empty Update Message
104 syncType(type, isEOR);
107 private void syncType(final TablesKey type, final boolean isEOR) {
108 final SyncVariables s = this.syncStorage.get(type);
110 LOG.warn("BGPTableType was not present in open message : {}", type);
116 this.listener.markUptodate(type);
117 LOG.info("BGP Synchronization finished for table {} ", type);
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
126 public void kaReceived() {
127 for (final Entry<TablesKey, SyncVariables> entry : this.syncStorage.entrySet()) {
128 final SyncVariables s = entry.getValue();
132 LOG.info("BGP Synchronization finished for table {} ", entry.getKey());
133 this.listener.markUptodate(entry.getKey());