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.message.rev130919.Keepalive;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.annotations.VisibleForTesting;
38 import com.google.common.base.Preconditions;
40 public final class BGPSessionNegotiator extends AbstractSessionNegotiator<Notification, BGPSessionImpl> {
41 // 4 minutes recommended in http://tools.ietf.org/html/rfc4271#section-8.2.2
42 protected static final int INITIAL_HOLDTIMER = 4;
47 * Negotiation has not started yet.
51 * We have sent our Open message, and are waiting for the peer's Open message.
55 * We have received the peer's Open message, which is acceptable, and we're waiting the acknowledgement of our
60 * The negotiation finished.
65 private static final Logger LOG = LoggerFactory.getLogger(BGPSessionNegotiator.class);
66 private final BGPSessionListener listener;
67 private final Timer timer;
68 private final BGPSessionPreferences localPref;
71 private Open remotePref;
74 private State state = State.Idle;
77 private BGPSessionImpl session;
79 public BGPSessionNegotiator(final Timer timer, final Promise<BGPSessionImpl> promise, final Channel channel,
80 final BGPSessionPreferences initialPrefs, final BGPSessionListener listener) {
81 super(promise, channel);
82 this.listener = Preconditions.checkNotNull(listener);
83 this.localPref = Preconditions.checkNotNull(initialPrefs);
84 this.timer = Preconditions.checkNotNull(timer);
88 protected void startNegotiation() {
89 Preconditions.checkState(this.state == State.Idle);
90 this.sendMessage(new OpenBuilder().setMyAsNumber(this.localPref.getMyAs().getValue().intValue()).setHoldTimer(
91 this.localPref.getHoldTime()).setBgpIdentifier(this.localPref.getBgpId()).setBgpParameters(this.localPref.getParams()).build());
92 this.state = State.OpenSent;
94 final Object lock = this;
95 this.timer.newTimeout(new TimerTask() {
97 public void run(final Timeout timeout) throws Exception {
99 if (BGPSessionNegotiator.this.state != State.Finished) {
100 BGPSessionNegotiator.this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.HOLD_TIMER_EXPIRED.getCode()).setErrorSubcode(
101 BGPError.HOLD_TIMER_EXPIRED.getSubcode()).build());
102 negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
103 BGPSessionNegotiator.this.state = State.Finished;
107 }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
111 protected synchronized void handleMessage(final Notification msg) {
112 LOG.debug("Channel {} handling message in state {}", this.channel, this.state);
114 switch (this.state) {
117 final Notify fsmError = new NotifyBuilder().setErrorCode(BGPError.FSM_ERROR.getCode()).setErrorSubcode(
118 BGPError.FSM_ERROR.getSubcode()).build();
119 this.sendMessage(fsmError);
121 if (msg instanceof Keepalive) {
122 negotiationSuccessful(this.session);
123 } else if (msg instanceof Notify) {
124 final Notify ntf = (Notify) msg;
125 negotiationFailed(new BGPDocumentedException("Peer refusal", BGPError.forValue(ntf.getErrorCode(), ntf.getErrorSubcode())));
127 this.state = State.Finished;
130 if (msg instanceof Open) {
131 final Open openObj = (Open) msg;
138 // Catch-all for unexpected message
139 LOG.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
140 this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.FSM_ERROR.getCode()).setErrorSubcode(BGPError.FSM_ERROR.getSubcode()).build());
141 negotiationFailed(new BGPDocumentedException("Unexpected message", BGPError.FSM_ERROR));
142 this.state = State.Finished;
145 private void handleOpen(final Open openObj) {
146 final List<BgpParameters> prefs = openObj.getBgpParameters();
147 if (prefs != null && !prefs.isEmpty()) {
148 if (!prefs.containsAll(this.localPref.getParams())) {
149 LOG.info("Open message unacceptable. Check the configuration of BGP speaker.");
151 this.remotePref = openObj;
152 this.sendMessage(new KeepaliveBuilder().build());
153 this.session = new BGPSessionImpl(this.timer, this.listener, this.channel, this.remotePref);
154 this.state = State.OpenConfirm;
155 LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, this.remotePref);
158 final Notify ntf = new NotifyBuilder().setErrorCode(BGPError.UNSPECIFIC_OPEN_ERROR.getCode()).setErrorSubcode(
159 BGPError.UNSPECIFIC_OPEN_ERROR.getSubcode()).build();
160 this.sendMessage(ntf);
161 negotiationFailed(new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.forValue(
162 ntf.getErrorCode(), ntf.getErrorSubcode())));
163 this.state = State.Finished;
166 public synchronized State getState() {