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.concepts.BGPObject;
15 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
16 import org.opendaylight.protocol.bgp.parser.BGPLink;
17 import org.opendaylight.protocol.bgp.parser.BGPNode;
18 import org.opendaylight.protocol.bgp.parser.BGPPrefix;
19 import org.opendaylight.protocol.bgp.parser.BGPRoute;
20 import org.opendaylight.protocol.bgp.parser.BGPSession;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
23 import org.opendaylight.protocol.bgp.parser.BGPUpdateSynchronized;
24 import org.opendaylight.protocol.bgp.util.BGPIPv4RouteImpl;
25 import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.Maps;
38 * BGP speaker (without Graceful restart capability) sends KeepAlive message after sending all initial Update messages
39 * with certain AFI/SAFI. For each AFI/SAFI, it sends one KA message. As it is undetermined which KA message belongs to
40 * which AFI/SAFI, an algorithm needed to be implemented.
42 public class BGPSynchronization {
44 private static final Logger logger = LoggerFactory.getLogger(BGPSynchronization.class);
46 private static class SyncVariables {
48 private boolean upd = false;
49 private boolean eor = false;
51 public void setUpd(final boolean upd) {
55 public void setEorTrue() {
59 public boolean getEor() {
63 public boolean getUpd() {
68 private final Map<BGPTableType, SyncVariables> syncStorage = Maps.newHashMap();
70 private final BGPSessionListener listener;
72 private final BGPSession session;
74 public BGPSynchronization(final BGPSession session, final BGPSessionListener listener, final Set<BGPTableType> types) {
75 this.listener = Preconditions.checkNotNull(listener);
76 this.session = Preconditions.checkNotNull(session);
78 for (final BGPTableType type : types) {
79 this.syncStorage.put(type, new SyncVariables());
84 * For each received Update message, the upd sync variable needs to be updated to true, for particular AFI/SAFI
85 * combination. Currently we only assume Unicast SAFI. From the Update message we have to extract the AFI. Each
86 * Update message can contain BGP Object with one type of AFI. If the object is BGP Link, BGP Node or BGPPrefix<?>
87 * the AFI is Linkstate. In case of BGPRoute, the AFI depends on the IP Address of the prefix.
89 * @param msg received Update message
91 public void updReceived(final BGPUpdateMessage msg) {
92 BGPTableType type = null;
93 if (!msg.getAddedObjects().isEmpty()) {
94 final BGPObject obj = msg.getAddedObjects().iterator().next();
95 if (obj instanceof BGPRoute) {
96 if ((BGPRoute) obj instanceof BGPIPv4RouteImpl) {
97 type = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
98 } else if ((BGPRoute) obj instanceof BGPIPv6RouteImpl) {
99 type = new BGPTableType(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class);
101 } else if (obj instanceof BGPLink || obj instanceof BGPNode || obj instanceof BGPPrefix<?>) {
102 type = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
105 final SyncVariables s = this.syncStorage.get(type);
107 logger.warn("BGPTableType was not present in open message : {}", type);
114 * This method is called, when the second KA message is received. It checks each AFI/SAFI sync variables. If they
115 * are all false, which means, that there was at least one update message followed by one KA, the EOR is sent to
118 public void kaReceived() {
119 for (final Entry<BGPTableType, SyncVariables> entry : this.syncStorage.entrySet()) {
120 final SyncVariables s = entry.getValue();
124 final BGPUpdateSynchronized up = generateEOR(entry.getKey());
125 logger.debug("Sending synchronization message: {}", up);
126 this.listener.onMessage(this.session, up);
133 private BGPUpdateSynchronized generateEOR(final BGPTableType type) {
134 return new BGPUpdateSynchronizedImpl(type);