X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Frib-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fimpl%2FBGPPeer.java;h=4bb87598245fcd5757aff1dee9e7968465277605;hb=e5fbd2135b3f361d5383ed6f15aca6b1206ee715;hp=789e71bf4aff0ece70bb794208cf954367810773;hpb=ca047c2df4bd21816145cd1cef0871603304ab1b;p=bgpcep.git diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java index 789e71bf4a..4bb8759824 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.protocol.bgp.rib.impl; import com.google.common.base.MoreObjects; @@ -29,9 +28,9 @@ import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction; import org.opendaylight.controller.md.sal.common.api.data.TransactionChain; import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener; import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain; -import org.opendaylight.protocol.bgp.rib.impl.spi.AdjRIBsOutRegistration; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics; import org.opendaylight.protocol.bgp.rib.impl.spi.RIB; +import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext; import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer; import org.opendaylight.protocol.bgp.rib.spi.BGPSession; import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason; @@ -40,18 +39,20 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types. import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily; @@ -60,7 +61,6 @@ import org.opendaylight.yangtools.yang.binding.Notification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into * RIB actions. @@ -71,37 +71,36 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun @GuardedBy("this") private final Set tables = new HashSet<>(); - private final RIB rib; - private final String name; - @GuardedBy("this") private BGPSession session; @GuardedBy("this") private byte[] rawIdentifier; @GuardedBy("this") - private AdjRIBsOutRegistration reg; + private DOMTransactionChain chain; + @GuardedBy("this") + private AdjRibInWriter ribWriter; + private final RIB rib; + private final String name; private BGPPeerRuntimeRegistrator registrator; private BGPPeerRuntimeRegistration runtimeReg; private long sessionEstablishedCounter = 0L; - @GuardedBy("this") - private AdjRibInWriter ribWriter; - public BGPPeer(final String name, final RIB rib) { + this(name, rib, PeerRole.Ibgp); + } + + public BGPPeer(final String name, final RIB rib, final PeerRole role) { this.rib = Preconditions.checkNotNull(rib); this.name = name; - - final DOMTransactionChain chain = rib.createPeerChain(this); - // FIXME: make this configurable - final PeerRole role = PeerRole.Ibgp; - - this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), role, chain); + this.chain = rib.createPeerChain(this); + this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), role, this.chain); } @Override public synchronized void close() { dropConnection(); + this.chain.close(); // TODO should this perform cleanup ? } @@ -112,37 +111,45 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun return; } final Update message = (Update) msg; - this.rib.updateTables(this, message); + // update AdjRibs - final PathAttributes attrs = message.getPathAttributes(); + final Attributes attrs = message.getAttributes(); MpReachNlri mpReach = null; if (message.getNlri() != null) { mpReach = prefixesToMpReach(message); - } else if (attrs != null && attrs.getAugmentation(PathAttributes1.class) != null) { - mpReach = attrs.getAugmentation(PathAttributes1.class).getMpReachNlri(); + } else if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) { + mpReach = attrs.getAugmentation(Attributes1.class).getMpReachNlri(); } if (mpReach != null) { - this.ribWriter.updateRoutes(mpReach, attrs); - return; + this.ribWriter.updateRoutes(mpReach, nextHopToAttribute(attrs, mpReach)); } MpUnreachNlri mpUnreach = null; if (message.getWithdrawnRoutes() != null) { mpUnreach = prefixesToMpUnreach(message); - } else if (attrs != null && attrs.getAugmentation(PathAttributes2.class) != null) { - mpUnreach = attrs.getAugmentation(PathAttributes2.class).getMpUnreachNlri(); + } else if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) { + mpUnreach = attrs.getAugmentation(Attributes2.class).getMpUnreachNlri(); } if (mpUnreach != null) { this.ribWriter.removeRoutes(mpUnreach); } } + private static Attributes nextHopToAttribute(final Attributes attrs, final MpReachNlri mpReach) { + if (attrs.getCNextHop() == null && mpReach.getCNextHop() != null) { + final AttributesBuilder attributesBuilder = new AttributesBuilder(attrs); + attributesBuilder.setCNextHop(mpReach.getCNextHop()); + return attributesBuilder.build(); + } + return attrs; + } + /** * Creates MPReach for the prefixes to be handled in the same way as linkstate routes * * @param message Update message containing prefixes in NLRI * @return MpReachNlri with prefixes from the nlri field */ - private MpReachNlri prefixesToMpReach(final Update message) { + private static MpReachNlri prefixesToMpReach(final Update message) { final List prefixes = new ArrayList<>(); for (final Ipv4Prefix p : message.getNlri().getNlri()) { prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build()); @@ -152,8 +159,8 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun new AdvertizedRoutesBuilder().setDestinationType( new DestinationIpv4CaseBuilder().setDestinationIpv4( new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build()); - if (message.getPathAttributes() != null) { - b.setCNextHop(message.getPathAttributes().getCNextHop()); + if (message.getAttributes() != null) { + b.setCNextHop(message.getAttributes().getCNextHop()); } return b.build(); } @@ -164,55 +171,54 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun * @param message Update message containing withdrawn routes * @return MpUnreachNlri with prefixes from the withdrawn routes field */ - private MpUnreachNlri prefixesToMpUnreach(final Update message) { + private static MpUnreachNlri prefixesToMpUnreach(final Update message) { final List prefixes = new ArrayList<>(); for (final Ipv4Prefix p : message.getWithdrawnRoutes().getWithdrawnRoutes()) { prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build()); } return new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes( new WithdrawnRoutesBuilder().setDestinationType( - new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4( + new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4( new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build()).build(); } @Override public synchronized void onSessionUp(final BGPSession session) { LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes()); - this.session = session; this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress(); + final PeerId peerId = RouterIds.createPeerId(session.getBgpId()); + TablesKey key = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class); + this.tables.add(key); + createAdjRibOutListener(peerId, key, false); for (final BgpTableType t : session.getAdvertisedTableTypes()) { - final TablesKey key = new TablesKey(t.getAfi(), t.getSafi()); - - this.tables.add(key); - this.rib.initTable(this, key); + key = new TablesKey(t.getAfi(), t.getSafi()); + if (this.tables.add(key)) { + createAdjRibOutListener(peerId, key, true); + } } - this.ribWriter = this.ribWriter.transform(session.getBgpId(), this.rib.getRibSupportContext(), this.tables); - - // Not particularly nice, but what can - if (session instanceof BGPSessionImpl) { - this.reg = this.rib.registerRIBsOut(this, new SessionRIBsOut((BGPSessionImpl) session)); - } + this.ribWriter = this.ribWriter.transform(peerId, this.rib.getRibSupportContext(), this.tables, false); this.sessionEstablishedCounter++; if (this.registrator != null) { this.runtimeReg = this.registrator.register(this); } } - private synchronized void cleanup() { - // FIXME: BUG-196: support graceful restart - this.ribWriter.cleanTables(this.tables); - for (final TablesKey key : this.tables) { - this.rib.clearTable(this, key); - } + private void createAdjRibOutListener(final PeerId peerId, final TablesKey key, final boolean mpSupport) { + final RIBSupportContext context = this.rib.getRibSupportContext().getRIBSupportContext(key); - if (this.reg != null) { - this.reg.close(); - this.reg = null; + // not particularly nice + if (context != null && this.session instanceof BGPSessionImpl) { + AdjRibOutListener.create(peerId, key, this.rib.getYangRibId(), this.rib.getCodecsRegistry(), context.getRibSupport(), ((RIBImpl) this.rib).getService(), + ((BGPSessionImpl) this.session).getLimiter(), mpSupport); } + } + private synchronized void cleanup() { + // FIXME: BUG-196: support graceful restart + this.ribWriter.cleanTables(this.tables); this.tables.clear(); } @@ -244,10 +250,6 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun return this.name; } - protected RIB getRib() { - return this.rib; - } - @Override public void releaseConnection() { dropConnection(); @@ -261,7 +263,11 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun this.runtimeReg = null; } if (this.session != null) { - this.session.close(); + try { + this.session.close(); + } catch (final Exception e) { + LOG.warn("Error closing session with peer", e); + } this.session = null; } } @@ -317,12 +323,19 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRun @Override public void onTransactionChainFailed(final TransactionChain chain, final AsyncTransaction transaction, final Throwable cause) { - // TODO Auto-generated method stub - + LOG.error("Transaction chain failed.", cause); + dropConnection(); + this.chain.close(); + this.chain = this.rib.createPeerChain(this); } @Override public void onTransactionChainSuccessful(final TransactionChain chain) { - // TODO Auto-generated method stub + LOG.debug("Transaction chain {} successfull.", chain); + } + + @Override + public void markUptodate(final TablesKey tablesKey) { + this.ribWriter.markTableUptodate(tablesKey); } }