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;
10 import io.netty.channel.Channel;
11 import io.netty.util.Timeout;
12 import io.netty.util.Timer;
13 import io.netty.util.TimerTask;
14 import io.netty.util.concurrent.Promise;
16 import java.util.List;
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.BGPSessionPreferences;
25 import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.NotifyBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Open;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.OpenBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocol;
37 import org.opendaylight.yangtools.yang.binding.Notification;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.annotations.VisibleForTesting;
42 import com.google.common.base.Preconditions;
44 public final class BGPSessionNegotiator extends AbstractSessionNegotiator<Notification, BGPSessionImpl> {
45 // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
46 // FIXME to actual value
47 protected static final int INITIAL_HOLDTIMER = 1;
52 * Negotiation has not started yet.
56 * We have sent our Open message, and are waiting for the peer's Open message.
60 * We have received the peer's Open message, which is acceptable, and we're waiting the acknowledgement of our
65 * The negotiation finished.
70 private static final Logger logger = LoggerFactory.getLogger(BGPSessionNegotiator.class);
71 private final BGPSessionListener listener;
72 private final Timer timer;
73 private final BGPSessionPreferences localPref;
76 private Open remotePref;
79 private State state = State.Idle;
82 private BGPSessionImpl session;
84 public BGPSessionNegotiator(final Timer timer, final Promise<BGPSessionImpl> promise, final Channel channel,
85 final BGPSessionPreferences initialPrefs, final BGPSessionListener listener) {
86 super(promise, channel);
87 this.listener = Preconditions.checkNotNull(listener);
88 this.localPref = Preconditions.checkNotNull(initialPrefs);
89 this.timer = Preconditions.checkNotNull(timer);
93 protected void startNegotiation() {
94 Preconditions.checkState(this.state == State.Idle);
95 this.channel.writeAndFlush(new OpenBuilder().setMyAsNumber(this.localPref.getMyAs()).setHoldTimer(this.localPref.getHoldTime()).setBgpIdentifier(
96 this.localPref.getBgpId()).setBgpParameters(this.localPref.getParams()).build());
97 this.state = State.OpenSent;
99 final Object lock = this;
100 this.timer.newTimeout(new TimerTask() {
102 public void run(final Timeout timeout) throws Exception {
103 synchronized (lock) {
104 if (BGPSessionNegotiator.this.state != State.Finished) {
105 negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
106 BGPSessionNegotiator.this.channel.writeAndFlush(new NotifyBuilder().setErrorCode(
107 BGPError.HOLD_TIMER_EXPIRED.getCode()).setErrorSubcode(BGPError.HOLD_TIMER_EXPIRED.getSubcode()).build());
108 BGPSessionNegotiator.this.state = State.Finished;
112 }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
116 protected synchronized void handleMessage(final Notification msg) {
117 logger.debug("Channel {} handling message in state {}", this.channel, this.state);
119 switch (this.state) {
122 final Notify fsm_error = new NotifyBuilder().setErrorCode(BGPError.FSM_ERROR.getCode()).setErrorSubcode(
123 BGPError.FSM_ERROR.getSubcode()).build();
124 this.channel.writeAndFlush(fsm_error);
126 if (msg instanceof Keepalive) {
127 negotiationSuccessful(this.session);
128 } else if (msg instanceof Notify) {
129 final Notify ntf = (Notify) msg;
130 negotiationFailed(new BGPDocumentedException("Peer refusal", BGPError.forValue(ntf.getErrorCode(), ntf.getErrorSubcode())));
132 this.state = State.Finished;
135 if (msg instanceof Open) {
136 final Open openObj = (Open) msg;
138 final List<BgpParameters> prefs = openObj.getBgpParameters();
139 if (prefs != null && !prefs.isEmpty()) {
140 for (final BgpParameters param : openObj.getBgpParameters()) {
141 final CParameters cap = param.getCParameters();
142 if (cap instanceof CMultiprotocol) {
143 if (((CMultiprotocol) cap).getMultiprotocolCapability().getAfi() == LinkstateAddressFamily.class
144 && ((CMultiprotocol) cap).getMultiprotocolCapability().getSafi() == LinkstateSubsequentAddressFamily.class) {
145 this.remotePref = openObj;
146 this.channel.writeAndFlush(new KeepaliveBuilder().build());
147 this.session = new BGPSessionImpl(this.timer, this.listener, this.channel, this.remotePref);
148 this.state = State.OpenConfirm;
149 logger.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, this.remotePref);
155 final Notify ntf = new NotifyBuilder().setErrorCode(BGPError.UNSPECIFIC_OPEN_ERROR.getCode()).setErrorSubcode(
156 BGPError.UNSPECIFIC_OPEN_ERROR.getSubcode()).build();
157 this.channel.writeAndFlush(ntf);
158 negotiationFailed(new BGPDocumentedException("Linkstate capability is not configured on router. Check the configuration of BGP speaker.", BGPError.forValue(
159 ntf.getErrorCode(), ntf.getErrorSubcode())));
160 this.state = State.Finished;
166 // Catch-all for unexpected message
167 logger.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
168 this.channel.writeAndFlush(new NotifyBuilder().setErrorCode(BGPError.FSM_ERROR.getCode()).setErrorSubcode(
169 BGPError.FSM_ERROR.getSubcode()).build());
170 negotiationFailed(new BGPDocumentedException("Unexpected message", BGPError.FSM_ERROR));
171 this.state = State.Finished;
174 public synchronized State getState() {