2 * Copyright (c) 2014 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
9 package org.opendaylight.protocol.bgp.rib.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
14 import io.netty.channel.Channel;
15 import io.netty.util.concurrent.Promise;
17 import java.util.concurrent.TimeUnit;
19 import javax.annotation.concurrent.GuardedBy;
21 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
22 import org.opendaylight.protocol.bgp.parser.BGPError;
23 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
27 import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
28 import org.opendaylight.protocol.util.Values;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
37 import org.opendaylight.yangtools.yang.binding.Notification;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Bgp Session negotiator. Common for local -> remote and remote -> local connections.
43 * One difference is session validation performed by injected BGPSessionValidator when OPEN message is received.
45 public abstract class AbstractBGPSessionNegotiator extends AbstractSessionNegotiator<Notification, BGPSessionImpl> {
46 // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
47 protected static final int INITIAL_HOLDTIMER = 4;
50 * @see <a href="http://tools.ietf.org/html/rfc6793">BGP Support for 4-Octet AS Number Space</a>
52 private static final int AS_TRANS = 23456;
57 * Negotiation has not started yet.
61 * We have sent our Open message, and are waiting for the peer's Open message.
65 * We have received the peer's Open message, which is acceptable, and we're waiting the acknowledgement of our
70 * The negotiation finished.
75 private static final Logger LOG = LoggerFactory.getLogger(AbstractBGPSessionNegotiator.class);
76 private final BGPPeerRegistry registry;
77 private final BGPSessionValidator sessionValidator;
80 private State state = State.Idle;
83 private BGPSessionImpl session;
85 public AbstractBGPSessionNegotiator(final Promise<BGPSessionImpl> promise, final Channel channel,
86 final BGPPeerRegistry registry, final BGPSessionValidator sessionValidator) {
87 super(promise, channel);
88 this.registry = registry;
89 this.sessionValidator = sessionValidator;
93 protected void startNegotiation() {
94 Preconditions.checkState(this.state == State.Idle);
96 // Check if peer is configured in registry before retrieving preferences
97 if (!registry.isPeerConfigured(getRemoteIp())) {
98 final BGPDocumentedException cause = new BGPDocumentedException(
99 "BGP peer with ip: " + getRemoteIp()
100 + " not configured, check configured peers in : "
101 + registry, BGPError.CEASE);
102 negotiationFailed(cause);
106 final BGPSessionPreferences preferences = getPreferences();
108 int as = preferences.getMyAs().getValue().intValue();
109 // Set as AS_TRANS if the value is bigger than 2B
110 if (as > Values.UNSIGNED_SHORT_MAX_VALUE) {
113 this.sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(preferences.getHoldTime()).setBgpIdentifier(
114 preferences.getBgpId()).setBgpParameters(preferences.getParams()).build());
115 this.state = State.OpenSent;
117 this.channel.eventLoop().schedule(new Runnable() {
120 if (AbstractBGPSessionNegotiator.this.state != State.Finished) {
121 AbstractBGPSessionNegotiator.this.sendMessage(buildErrorNotify(BGPError.HOLD_TIMER_EXPIRED));
122 negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
123 AbstractBGPSessionNegotiator.this.state = State.Finished;
126 }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
129 private BGPSessionPreferences getPreferences() {
130 return registry.getPeerPreferences(getRemoteIp());
133 private IpAddress getRemoteIp() {
134 return StrictBGPPeerRegistry.getIpAddress(channel.remoteAddress());
138 protected synchronized void handleMessage(final Notification msg) {
139 LOG.debug("Channel {} handling message in state {}", this.channel, this.state);
141 switch (this.state) {
144 this.sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
147 if (msg instanceof Keepalive) {
148 negotiationSuccessful(this.session);
149 LOG.info("BGP Session with peer {} established successfully.", this.channel);
150 } else if (msg instanceof Notify) {
151 final Notify ntf = (Notify) msg;
152 negotiationFailed(new BGPDocumentedException("Peer refusal", BGPError.forValue(ntf.getErrorCode(), ntf.getErrorSubcode())));
154 this.state = State.Finished;
157 if (msg instanceof Open) {
158 final Open openObj = (Open) msg;
165 // Catch-all for unexpected message
166 LOG.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
167 this.sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
168 negotiationFailed(new BGPDocumentedException("Unexpected message", BGPError.FSM_ERROR));
169 this.state = State.Finished;
172 private static Notify buildErrorNotify(final BGPError err) {
173 return new NotifyBuilder().setErrorCode(err.getCode()).setErrorSubcode(err.getSubcode()).build();
176 private void handleOpen(final Open openObj) {
178 sessionValidator.validate(openObj, getPreferences());
179 } catch (final BGPDocumentedException e) {
180 negotiationFailed(e);
185 final BGPSessionListener peer = registry.getPeer(getRemoteIp(), getSourceId(openObj, getPreferences()), getDestinationId(openObj, getPreferences()));
186 this.sendMessage(new KeepaliveBuilder().build());
187 this.session = new BGPSessionImpl(peer, this.channel, openObj, getPreferences().getHoldTime());
188 this.state = State.OpenConfirm;
189 LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, openObj);
190 } catch (final BGPDocumentedException e) {
191 LOG.warn("Channel {} negotiation failed", this.channel, e);
192 negotiationFailed(e);
196 private void negotiationFailed(final BGPDocumentedException e) {
197 LOG.warn("Channel {} negotiation failed: {}", this.channel, e.getMessage());
198 this.sendMessage(buildErrorNotify(e.getError()));
199 super.negotiationFailed(e);
200 this.state = State.Finished;
204 * @return BGP Id of device that accepted the connection
206 protected abstract Ipv4Address getDestinationId(final Open openMsg, final BGPSessionPreferences preferences);
209 * @return BGP Id of device that initiated the connection
211 protected abstract Ipv4Address getSourceId(final Open openMsg, final BGPSessionPreferences preferences);
213 public synchronized State getState() {