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.rev130918.Update;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.UpdateBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.BgpTableType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes1Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.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;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.Maps;
34 * BGP speaker (without Graceful restart capability) sends KeepAlive message after sending all initial Update messages
35 * with certain AFI/SAFI. For each AFI/SAFI, it sends one KA message. As it is undetermined which KA message belongs to
36 * which AFI/SAFI, an algorithm needed to be implemented.
38 public class BGPSynchronization {
40 private static final Logger logger = LoggerFactory.getLogger(BGPSynchronization.class);
42 private static class SyncVariables {
44 private boolean upd = false;
45 private boolean eor = false;
47 public void setUpd(final boolean upd) {
51 public void setEorTrue() {
55 public boolean getEor() {
59 public boolean getUpd() {
64 private final Map<BgpTableType, SyncVariables> syncStorage = Maps.newHashMap();
66 private final BGPSessionListener listener;
68 private final BGPSession session;
70 public BGPSynchronization(final BGPSession bgpSession, final BGPSessionListener listener, final Set<BgpTableType> types) {
71 this.listener = Preconditions.checkNotNull(listener);
72 this.session = Preconditions.checkNotNull(bgpSession);
74 for (final BgpTableType type : types) {
75 this.syncStorage.put(type, new SyncVariables());
80 * For each received Update message, the upd sync variable needs to be updated to true, for particular AFI/SAFI
81 * combination. Currently we only assume Unicast SAFI. From the Update message we have to extract the AFI. Each
82 * Update message can contain BGP Object with one type of AFI. If the object is BGP Link, BGP Node or BGPPrefix<?>
83 * the AFI is Linkstate. In case of BGPRoute, the AFI depends on the IP Address of the prefix.
85 * @param msg received Update message
87 public void updReceived(final Update msg) {
88 TablesKey type = null;
89 if (msg.getNlri() != null || msg.getWithdrawnRoutes() != null) {
90 type = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
91 } else if (msg.getPathAttributes().getAugmentation(PathAttributes1.class) != null) {
92 PathAttributes1 pa = msg.getPathAttributes().getAugmentation(PathAttributes1.class);
93 if (pa.getMpReachNlri() != null) {
94 type = new TablesKey(pa.getMpReachNlri().getAfi(), pa.getMpReachNlri().getSafi());
96 } else if (msg.getPathAttributes().getAugmentation(PathAttributes2.class) != null) {
97 PathAttributes2 pa = msg.getPathAttributes().getAugmentation(PathAttributes2.class);
98 if (pa.getMpUnreachNlri() != null) {
99 type = new TablesKey(pa.getMpUnreachNlri().getAfi(), pa.getMpUnreachNlri().getSafi());
102 final SyncVariables s = this.syncStorage.get(type);
104 logger.warn("BGPTableType was not present in open message : {}", type);
111 * This method is called, when the second KA message is received. It checks each AFI/SAFI sync variables. If they
112 * are all false, which means, that there was at least one update message followed by one KA, the EOR is sent to
115 public void kaReceived() {
116 for (final Entry<BgpTableType, SyncVariables> entry : this.syncStorage.entrySet()) {
117 final SyncVariables s = entry.getValue();
121 final Update up = generateEOR(entry.getKey());
122 logger.debug("Sending synchronization message: {}", up);
123 this.listener.onMessage(this.session, up);
130 private Update generateEOR(final BgpTableType type) {
131 if (type.getAfi().equals(Ipv4AddressFamily.class) && type.getSafi().equals(UnicastSubsequentAddressFamily.class)) {
132 return new UpdateBuilder().build();
134 return new UpdateBuilder().setPathAttributes(new PathAttributesBuilder().addAugmentation(PathAttributes1.class, new PathAttributes1Builder().setMpReachNlri(new MpReachNlriBuilder().setAfi(type.getAfi()).setSafi(type.getSafi()).build()).build()).build()).build();