X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=bgp%2Frib-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fimpl%2FAbstractBGPSessionNegotiator.java;fp=bgp%2Frib-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fimpl%2FBGPSessionNegotiator.java;h=3eebcb1fcfaac1a81d946dac1ee3f2078ee445ed;hb=5b242f296eaf7979e08ca55d99cdba803973b85f;hp=d8e445e18f51dc10379faf213bb1985b8c794521;hpb=930efdaafb076e2648da35fcbb2e06149090d9db;p=bgpcep.git diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPSessionNegotiator.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java similarity index 60% rename from bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPSessionNegotiator.java rename to bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java index d8e445e18f..3eebcb1fcf 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPSessionNegotiator.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java @@ -1,46 +1,47 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * 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.annotations.VisibleForTesting; import com.google.common.base.Preconditions; - import io.netty.channel.Channel; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; import io.netty.util.concurrent.Promise; - -import java.util.List; import java.util.concurrent.TimeUnit; - import javax.annotation.concurrent.GuardedBy; - -import org.opendaylight.protocol.bgp.parser.AsNumberUtil; import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; import org.opendaylight.protocol.bgp.parser.BGPError; import org.opendaylight.protocol.bgp.parser.BGPSessionListener; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator; import org.opendaylight.protocol.framework.AbstractSessionNegotiator; import org.opendaylight.protocol.util.Values; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters; import org.opendaylight.yangtools.yang.binding.Notification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public final class BGPSessionNegotiator extends AbstractSessionNegotiator { +/** + * Bgp Session negotiator. Common for local -> remote and remote -> local connections. + * One difference is session validation performed by injected BGPSessionValidator when OPEN message is received. + */ +public abstract class AbstractBGPSessionNegotiator extends AbstractSessionNegotiator { // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2 protected static final int INITIAL_HOLDTIMER = 4; @@ -70,11 +71,10 @@ public final class BGPSessionNegotiator extends AbstractSessionNegotiator promise, final Channel channel, - final BGPSessionPreferences initialPrefs, final AsNumber remoteAs, final BGPSessionListener listener) { + public AbstractBGPSessionNegotiator(final Timer timer, final Promise promise, final Channel channel, + final BGPPeerRegistry registry, final BGPSessionValidator sessionValidator) { super(promise, channel); - this.listener = Preconditions.checkNotNull(listener); - this.localPref = Preconditions.checkNotNull(initialPrefs); - this.remoteAs = Preconditions.checkNotNull(remoteAs); + this.registry = registry; + this.sessionValidator = sessionValidator; this.timer = Preconditions.checkNotNull(timer); } @Override protected void startNegotiation() { Preconditions.checkState(this.state == State.Idle); - int as = this.localPref.getMyAs().getValue().intValue(); + + // Check if peer is configured in registry before retrieving preferences + if (registry.isPeerConfigured(getRemoteIp()) == false) { + final BGPDocumentedException cause = new BGPDocumentedException( + "BGP peer with ip: " + getRemoteIp() + + " not configured, check configured peers in : " + + registry, BGPError.CEASE); + negotiationFailed(cause); + return; + } + + final BGPSessionPreferences preferences = getPreferences(); + + int as = preferences.getMyAs().getValue().intValue(); // Set as AS_TRANS if the value is bigger than 2B if (as > Values.UNSIGNED_SHORT_MAX_VALUE) { as = AS_TRANS; } - this.sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(this.localPref.getHoldTime()).setBgpIdentifier( - this.localPref.getBgpId()).setBgpParameters(this.localPref.getParams()).build()); + this.sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(preferences.getHoldTime()).setBgpIdentifier( + preferences.getBgpId()).setBgpParameters(preferences.getParams()).build()); this.state = State.OpenSent; final Object lock = this; @@ -108,16 +120,24 @@ public final class BGPSessionNegotiator extends AbstractSessionNegotiator prefs = openObj.getBgpParameters(); - if (prefs != null && !prefs.isEmpty()) { - if (!prefs.containsAll(this.localPref.getParams())) { - LOG.info("BGP Open message session parameters differ, session still accepted."); - } + try { + final BGPSessionListener peer = registry.getPeer(getRemoteIp(), getSourceId(openObj, getPreferences()), getDestinationId(openObj, getPreferences())); this.sendMessage(new KeepaliveBuilder().build()); - this.session = new BGPSessionImpl(this.timer, this.listener, this.channel, openObj, this.localPref.getHoldTime()); + this.session = new BGPSessionImpl(this.timer, peer, this.channel, openObj, getPreferences().getHoldTime()); this.state = State.OpenConfirm; LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, openObj); - return; + } catch (final BGPDocumentedException e) { + LOG.warn("Channel {} negotiation failed", this.channel, e); + negotiationFailed(e); } + } - this.sendMessage(buildErrorNotify(BGPError.UNSPECIFIC_OPEN_ERROR)); - negotiationFailed(new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR)); + private void negotiationFailed(final BGPDocumentedException e) { + LOG.warn("Channel {} negotiation failed: {}", this.channel, e.getMessage()); + this.sendMessage(buildErrorNotify(e.getError())); + super.negotiationFailed(e); this.state = State.Finished; } + /** + * @return BGP Id of device that accepted the connection + */ + protected abstract Ipv4Address getDestinationId(final Open openMsg, final BGPSessionPreferences preferences); + + /** + * @return BGP Id of device that initiated the connection + */ + protected abstract Ipv4Address getSourceId(final Open openMsg, final BGPSessionPreferences preferences); + public synchronized State getState() { return this.state; }