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