* 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 static java.util.Objects.requireNonNull;
import io.netty.util.concurrent.Promise;
import io.netty.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
-import javax.annotation.concurrent.GuardedBy;
+import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.checkerframework.checker.lock.qual.Holding;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
import org.opendaylight.protocol.bgp.rib.spi.SessionNegotiator;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.protocol.util.Values;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Keepalive;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.KeepaliveBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
import org.opendaylight.yangtools.yang.binding.Notification;
+import org.opendaylight.yangtools.yang.common.Uint16;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
private static final int INITIAL_HOLDTIMER = 4;
- /**
- * @see <a href="http://tools.ietf.org/html/rfc6793">BGP Support for 4-Octet AS Number Space</a>
- */
- private static final int AS_TRANS = 23456;
+ // <a href="http://tools.ietf.org/html/rfc6793">BGP Support for 4-Octet AS Number Space</a>
+ @VisibleForTesting
+ static final Uint16 AS_TRANS = Uint16.valueOf(23456).intern();
private static final Logger LOG = LoggerFactory.getLogger(AbstractBGPSessionNegotiator.class);
private final BGPPeerRegistry registry;
private final Promise<BGPSessionImpl> promise;
this.registry = registry;
}
+ @SuppressWarnings("checkstyle:illegalCatch")
private synchronized void startNegotiation() {
if (!(this.state == State.IDLE || this.state == State.OPEN_CONFIRM)) {
return;
}
// Open can be sent first either from ODL (IDLE) or from peer (OPEN_CONFIRM)
- final IpAddress remoteIp = getRemoteIp();
+ final IpAddressNoZone remoteIp = getRemoteIp();
try {
// Check if peer is configured in registry before retrieving preferences
if (!this.registry.isPeerConfigured(remoteIp)) {
}
final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
-
- 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;
- }
+ final Uint16 as = openASNumber(preferences.getMyAs().getValue().longValue());
sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(preferences.getHoldTime()).setBgpIdentifier(
preferences.getBgpId()).setBgpParameters(preferences.getParams()).build());
if (this.state != State.FINISHED) {
}
}
- private IpAddress getRemoteIp() {
- final IpAddress remoteIp = StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress());
- if (remoteIp.getIpv6Address() != null) {
- return new IpAddress(Ipv6Util.getFullForm(remoteIp.getIpv6Address()));
+ private IpAddressNoZone getRemoteIp() {
+ final IpAddressNoZone remoteIp = StrictBGPPeerRegistry.getIpAddress(this.channel.remoteAddress());
+ if (remoteIp.getIpv6AddressNoZone() != null) {
+ return new IpAddressNoZone(Ipv6Util.getFullForm(remoteIp.getIpv6AddressNoZone()));
}
return remoteIp;
}
break;
case OPEN_CONFIRM:
if (msg instanceof Keepalive) {
- negotiationSuccessful(this.session);
+ negotiationSuccessful();
LOG.info("BGP Session with peer {} established successfully.", this.channel);
} else if (msg instanceof Notify) {
final Notify ntf = (Notify) msg;
}
private synchronized void handleOpen(final Open openObj) {
- final IpAddress remoteIp = getRemoteIp();
+ final IpAddressNoZone remoteIp = getRemoteIp();
final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
try {
final BGPSessionListener peer = this.registry.getPeer(remoteIp, getSourceId(openObj, preferences),
}
}
- private synchronized void negotiationFailed(final Throwable e) {
- LOG.warn("Channel {} negotiation failed: {}", this.channel, e.getMessage());
- if (e instanceof BGPDocumentedException) {
+ private synchronized void negotiationFailed(final Throwable cause) {
+ LOG.warn("Channel {} negotiation failed: {}", this.channel, cause.getMessage());
+ if (cause instanceof BGPDocumentedException) {
// although sendMessage() can also result in calling this method, it won't create a cycle.
// In case sendMessage() fails to deliver the message, this method gets called with different
// exception (definitely not with BGPDocumentedException).
- sendMessage(buildErrorNotify(((BGPDocumentedException) e).getError(),
- ((BGPDocumentedException) e).getData()));
+ sendMessage(buildErrorNotify(((BGPDocumentedException) cause).getError(),
+ ((BGPDocumentedException) cause).getData()));
}
if (this.state == State.OPEN_CONFIRM) {
this.registry.removePeerSession(getRemoteIp());
}
- negotiationFailedCloseChannel(e);
+ negotiationFailedCloseChannel(cause);
this.state = State.FINISHED;
}
/**
+ * Get destination identifier.
+ *
* @param openMsg Open message received from remote BGP speaker
* @param preferences Local BGP speaker preferences
* @return BGP Id of device that accepted the connection
*/
- protected abstract Ipv4Address getDestinationId(Open openMsg, BGPSessionPreferences preferences);
+ protected abstract Ipv4AddressNoZone getDestinationId(Open openMsg, BGPSessionPreferences preferences);
/**
+ * Get source identifier.
+ *
* @param openMsg Open message received from remote BGP speaker
* @param preferences Local BGP speaker preferences
* @return BGP Id of device that accepted the connection
*/
- protected abstract Ipv4Address getSourceId(Open openMsg, BGPSessionPreferences preferences);
+ protected abstract Ipv4AddressNoZone getSourceId(Open openMsg, BGPSessionPreferences preferences);
public synchronized State getState() {
return this.state;
}
- private void negotiationSuccessful(final BGPSessionImpl session) {
+ @Holding("this")
+ private void negotiationSuccessful() {
LOG.debug("Negotiation on channel {} successful with session {}", this.channel, session);
this.channel.pipeline().replace(this, "session", session);
this.promise.setSuccess(session);
}
@Override
+ @SuppressWarnings("checkstyle:illegalCatch")
public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
LOG.debug("Negotiation read invoked on channel {}", this.channel);
try {
LOG.info("Unexpected error during negotiation", cause);
negotiationFailedCloseChannel(cause);
}
+
+ @VisibleForTesting
+ static Uint16 openASNumber(final long configuredASNumber) {
+ // Return AS_TRANS if the value is bigger than 2B.
+ return configuredASNumber > Values.UNSIGNED_SHORT_MAX_VALUE ? AS_TRANS : Uint16.valueOf(configuredASNumber);
+ }
}