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.base.Preconditions;
11 import com.google.common.collect.Maps;
13 import java.util.Map.Entry;
15 import org.opendaylight.protocol.bgp.parser.BGPSession;
16 import org.opendaylight.protocol.bgp.parser.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.message.rev130919.UpdateBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * BGP speaker (without Graceful restart capability) sends KeepAlive message after sending all initial Update messages
32 * with certain AFI/SAFI. For each AFI/SAFI, it sends one KA message. As it is undetermined which KA message belongs to
33 * which AFI/SAFI, an algorithm needed to be implemented.
35 public class BGPSynchronization {
37 private static final Logger LOG = LoggerFactory.getLogger(BGPSynchronization.class);
39 private static class SyncVariables {
41 private boolean upd = false;
42 private boolean eor = false;
44 public void setUpd(final boolean upd) {
48 public void setEorTrue() {
52 public boolean getEor() {
56 public boolean getUpd() {
61 private final Map<TablesKey, SyncVariables> syncStorage = Maps.newHashMap();
63 private final BGPSessionListener listener;
65 private final BGPSession session;
67 public BGPSynchronization(final BGPSession bgpSession, final BGPSessionListener listener, final Set<TablesKey> types) {
68 this.listener = Preconditions.checkNotNull(listener);
69 this.session = Preconditions.checkNotNull(bgpSession);
71 for (final TablesKey type : types) {
72 this.syncStorage.put(type, new SyncVariables());
77 * For each received Update message, the upd sync variable needs to be updated to true, for particular AFI/SAFI
78 * combination. Currently we only assume Unicast SAFI. From the Update message we have to extract the AFI. Each
79 * Update message can contain BGP Object with one type of AFI. If the object is BGP Link, BGP Node or BGPPrefix<?>
80 * the AFI is Linkstate. In case of BGPRoute, the AFI depends on the IP Address of the prefix.
82 * @param msg received Update message
84 public void updReceived(final Update msg) {
85 TablesKey type = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
86 boolean isEOR = false;
87 if (msg.getNlri() == null && msg.getWithdrawnRoutes() == null) {
88 if (msg.getPathAttributes() != null) {
89 if (msg.getPathAttributes().getAugmentation(PathAttributes1.class) != null) {
90 final PathAttributes1 pa = msg.getPathAttributes().getAugmentation(PathAttributes1.class);
91 if (pa.getMpReachNlri() != null) {
92 type = new TablesKey(pa.getMpReachNlri().getAfi(), pa.getMpReachNlri().getSafi());
94 } else if (msg.getPathAttributes().getAugmentation(PathAttributes2.class) != null) {
95 final PathAttributes2 pa = msg.getPathAttributes().getAugmentation(PathAttributes2.class);
96 if (pa.getMpUnreachNlri() != null) {
97 type = new TablesKey(pa.getMpUnreachNlri().getAfi(), pa.getMpUnreachNlri().getSafi());
104 final SyncVariables s = this.syncStorage.get(type);
106 LOG.warn("BGPTableType was not present in open message : {}", type);
116 * This method is called, when the second KA message is received. It checks each AFI/SAFI sync variables. If they
117 * are all false, which means, that there was at least one update message followed by one KA, the EOR is sent to
120 public void kaReceived() {
121 for (final Entry<TablesKey, SyncVariables> entry : this.syncStorage.entrySet()) {
122 final SyncVariables s = entry.getValue();
126 LOG.info("BGP Synchronization finished for table {} ", entry.getKey());
127 final Update up = generateEOR(entry.getKey());
128 LOG.debug("Sending synchronization message: {}", up);
129 this.listener.onMessage(this.session, up);
136 private Update generateEOR(final TablesKey type) {
137 if (type.getAfi().equals(Ipv4AddressFamily.class) && type.getSafi().equals(UnicastSubsequentAddressFamily.class)) {
138 return new UpdateBuilder().build();
140 return new UpdateBuilder().setPathAttributes(
141 new PathAttributesBuilder().addAugmentation(
142 PathAttributes1.class,
143 new PathAttributes1Builder().setMpReachNlri(
144 new MpReachNlriBuilder().setAfi(type.getAfi()).setSafi(type.getSafi()).build()).build()).build()).build();