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.openflowplugin.impl.connection;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.util.List;
19 import java.util.Objects;
20 import java.util.concurrent.Future;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.ErrorHandler;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
27 import org.opendaylight.openflowplugin.impl.common.DeviceConnectionRateLimiter;
28 import org.opendaylight.openflowplugin.impl.util.MessageFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
34 import org.opendaylight.yangtools.yang.common.RpcError;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class HandshakeManagerImpl implements HandshakeManager {
41 private static final long ACTIVE_XID = 20L;
43 private static final Logger LOG = LoggerFactory.getLogger(HandshakeManagerImpl.class);
45 private Short lastProposedVersion;
46 private Short lastReceivedVersion;
47 private final List<Short> versionOrder;
49 private final ConnectionAdapter connectionAdapter;
50 private Short version;
51 private final ErrorHandler errorHandler;
53 private final Short highestVersion;
55 private Long activeXid;
57 private final HandshakeListener handshakeListener;
59 private boolean useVersionBitmap; // not final just for unit test
61 private final DeviceConnectionRateLimiter deviceConnectionRateLimiter;
66 * @param connectionAdapter connection adaptor for switch
67 * @param highestVersion highest openflow version
68 * @param versionOrder list of version in order for connection protocol negotiation
69 * @param errorHandler the ErrorHandler
70 * @param handshakeListener the HandshakeListener
71 * @param useVersionBitmap should use negotiation bit map
73 public HandshakeManagerImpl(ConnectionAdapter connectionAdapter, Short highestVersion, List<Short> versionOrder,
74 ErrorHandler errorHandler, HandshakeListener handshakeListener,
75 boolean useVersionBitmap, DeviceConnectionRateLimiter deviceConnectionRateLimiter) {
76 this.highestVersion = highestVersion;
77 this.versionOrder = versionOrder;
78 this.connectionAdapter = connectionAdapter;
79 this.errorHandler = errorHandler;
80 this.handshakeListener = handshakeListener;
81 this.useVersionBitmap = useVersionBitmap;
82 this.deviceConnectionRateLimiter = deviceConnectionRateLimiter;
86 @SuppressWarnings("checkstyle:IllegalCatch")
87 public synchronized void shake(HelloMessage receivedHello) {
89 if (version != null) {
90 // Some switches respond with a second HELLO acknowledging our HELLO
91 // but we've already completed the handshake based on the negotiated
92 // version and have registered this switch.
93 LOG.debug("Hello recieved after handshake already settled ... ignoring.");
97 LOG.trace("handshake STARTED");
98 setActiveXid(ACTIVE_XID);
101 if (receivedHello == null) {
102 // first Hello sending
103 sendHelloMessage(highestVersion, getNextXid());
104 lastProposedVersion = highestVersion;
105 LOG.trace("ret - firstHello+wait");
109 // process the 2. and later hellos
110 Short remoteVersion = receivedHello.getVersion();
111 List<Elements> elements = receivedHello.getElements();
112 setActiveXid(receivedHello.getXid());
113 List<Boolean> remoteVersionBitmap = MessageFactory.digVersions(elements);
114 LOG.debug("Hello message: version={}, xid={}, bitmap={}", remoteVersion, receivedHello.getXid(),
115 remoteVersionBitmap);
117 if (useVersionBitmap && remoteVersionBitmap != null) {
118 // versionBitmap on both sides -> ONE STEP DECISION
119 handleVersionBitmapNegotiation(elements);
121 // versionBitmap missing at least on one side -> STEP-BY-STEP NEGOTIATION applying
122 handleStepByStepVersionNegotiation(remoteVersion);
124 } catch (Exception ex) {
125 errorHandler.handleException(ex);
126 LOG.trace("ret - shake fail - closing");
127 handshakeListener.onHandshakeFailure();
132 * Handles the version negotiation step by step.
134 * @param remoteVersion remote version
135 * @throws Exception exception
137 @SuppressWarnings("checkstyle:IllegalCatch")
138 private void handleStepByStepVersionNegotiation(final Short remoteVersion) throws Exception {
139 LOG.debug("remoteVersion:{} lastProposedVersion:{}, highestVersion:{}", remoteVersion, lastProposedVersion,
142 if (lastProposedVersion == null) {
143 // first hello has not been sent yet, send it and either wait for next remote
144 // version or proceed
145 lastProposedVersion = proposeNextVersion(remoteVersion);
146 final Long nextHelloXid = getNextXid();
147 ListenableFuture<Void> helloResult = sendHelloMessage(lastProposedVersion, nextHelloXid);
148 Futures.addCallback(helloResult, new FutureCallback<Void>() {
150 public void onSuccess(Void result) {
152 stepByStepVersionSubStep(remoteVersion);
153 } catch (Exception e) {
154 errorHandler.handleException(e);
155 handshakeListener.onHandshakeFailure();
160 public void onFailure(Throwable throwable) {
161 LOG.info("hello sending seriously failed [{}]", nextHelloXid);
162 LOG.trace("detail of hello send problem", throwable);
164 }, MoreExecutors.directExecutor());
166 stepByStepVersionSubStep(remoteVersion);
170 private void stepByStepVersionSubStep(Short remoteVersion) throws Exception {
171 if (remoteVersion.equals(lastProposedVersion)) {
172 postHandshake(lastProposedVersion, getNextXid());
173 LOG.trace("ret - OK - switch answered with lastProposedVersion");
175 checkNegotiationStalling(remoteVersion);
177 if (remoteVersion > (lastProposedVersion == null ? highestVersion : lastProposedVersion)) {
178 // wait for next version
179 LOG.trace("ret - wait");
181 //propose lower version
182 handleLowerVersionProposal(remoteVersion);
188 * Handles a proposal for a lower version.
190 * @param remoteVersion remote version
191 * @throws Exception exception
193 private void handleLowerVersionProposal(Short remoteVersion) throws Exception {
194 Short proposedVersion;
195 // find the version from header version field
196 proposedVersion = proposeNextVersion(remoteVersion);
197 lastProposedVersion = proposedVersion;
198 sendHelloMessage(proposedVersion, getNextXid());
200 if (!Objects.equals(proposedVersion, remoteVersion)) {
201 LOG.trace("ret - sent+wait");
203 LOG.trace("ret - sent+OK");
204 postHandshake(proposedVersion, getNextXid());
209 * Handles the negotiation of the version bitmap.
211 * @param elements version elements
212 * @throws Exception exception
214 private void handleVersionBitmapNegotiation(List<Elements> elements) throws Exception {
215 final Short proposedVersion = proposeCommonBitmapVersion(elements);
216 if (lastProposedVersion == null) {
217 // first hello has not been sent yet
218 Long nexHelloXid = getNextXid();
219 ListenableFuture<Void> helloDone = sendHelloMessage(proposedVersion, nexHelloXid);
220 Futures.addCallback(helloDone, new FutureCallback<Void>() {
222 public void onSuccess(Void result) {
223 LOG.trace("ret - DONE - versionBitmap");
224 postHandshake(proposedVersion, getNextXid());
228 public void onFailure(Throwable throwable) {
231 }, MoreExecutors.directExecutor());
232 LOG.trace("next proposal [{}] with versionBitmap hooked ..", nexHelloXid);
234 LOG.trace("ret - DONE - versionBitmap");
235 postHandshake(proposedVersion, getNextXid());
239 private Long getNextXid() {
244 private void setActiveXid(Long xid) {
245 this.activeXid = xid;
249 * Checks negotiation stalling.
251 * @param remoteVersion remove version
253 private void checkNegotiationStalling(Short remoteVersion) {
254 if (lastReceivedVersion != null && lastReceivedVersion.equals(remoteVersion)) {
255 throw new IllegalStateException("version negotiation stalled: version = " + remoteVersion);
257 lastReceivedVersion = remoteVersion;
261 public Short getVersion() {
266 * find common highest supported bitmap version.
268 * @param list bitmap list
269 * @return proposed bitmap value
271 protected Short proposeCommonBitmapVersion(List<Elements> list) {
272 Short supportedHighestVersion = null;
273 if (null != list && 0 != list.size()) {
274 for (Elements element : list) {
275 List<Boolean> bitmap = element.getVersionBitmap();
276 // check for version bitmap
277 for (short bitPos : OFConstants.VERSION_ORDER) {
278 // with all the version it should work.
279 if (bitmap.get(bitPos % Integer.SIZE)) {
280 supportedHighestVersion = bitPos;
286 if (null == supportedHighestVersion) {
287 LOG.trace("versionBitmap: no common version found");
288 throw new IllegalArgumentException("no common version found in versionBitmap");
292 return supportedHighestVersion;
296 * find supported version based on remoteVersion.
298 * @param remoteVersion openflow version supported by remote entity
299 * @return openflow version
301 protected short proposeNextVersion(short remoteVersion) {
302 Short proposal = null;
303 for (short offer : versionOrder) {
304 if (offer <= remoteVersion) {
309 if (proposal == null) {
310 throw new IllegalArgumentException(
311 "no equal or lower version found, unsupported version: " + remoteVersion);
317 * send hello reply without versionBitmap.
319 * @param helloVersion initial hello version for openflow connection negotiation
320 * @param helloXid transaction id
322 private ListenableFuture<Void> sendHelloMessage(Short helloVersion, final Long helloXid) throws Exception {
325 HelloInput helloInput = MessageFactory.createHelloInput(helloVersion, helloXid, versionOrder);
327 final SettableFuture<Void> resultFtr = SettableFuture.create();
329 LOG.debug("sending hello message: version{}, xid={}, version bitmap={}", helloVersion, helloXid,
330 MessageFactory.digVersions(helloInput.getElements()));
332 Future<RpcResult<Void>> helloResult = connectionAdapter.hello(helloInput);
334 ListenableFuture<RpcResult<Void>> rpcResultListenableFuture = JdkFutureAdapters.listenInPoolThread(helloResult);
335 Futures.addCallback(rpcResultListenableFuture, new FutureCallback<RpcResult<Void>>() {
337 public void onSuccess(@Nonnull RpcResult<Void> result) {
338 if (result.isSuccessful()) {
339 LOG.debug("hello successfully sent, xid={}, addr={}", helloXid,
340 connectionAdapter.getRemoteAddress());
343 for (RpcError error : result.getErrors()) {
344 LOG.debug("hello sending failed [{}]: i:{} s:{} m:{}, addr:{}", helloXid, error.getInfo(),
345 error.getSeverity(), error.getMessage(), connectionAdapter.getRemoteAddress());
346 if (error.getCause() != null) {
347 LOG.trace("DETAIL of sending hello failure", error.getCause());
350 resultFtr.cancel(false);
351 handshakeListener.onHandshakeFailure();
356 public void onFailure(Throwable throwable) {
357 LOG.warn("sending of hello failed seriously [{}, addr:{}]: {}", helloXid,
358 connectionAdapter.getRemoteAddress(), throwable.getMessage());
359 LOG.trace("DETAIL of sending of hello failure:", throwable);
360 resultFtr.cancel(false);
361 handshakeListener.onHandshakeFailure();
363 }, MoreExecutors.directExecutor());
364 LOG.trace("sending hello message [{}] - result hooked ..", helloXid);
370 * after handshake set features, register to session.
372 * @param proposedVersion proposed openflow version
373 * @param xid transaction id
375 protected void postHandshake(final Short proposedVersion, final Long xid) {
377 version = proposedVersion;
379 LOG.debug("version set: {}", proposedVersion);
381 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
382 featuresBuilder.setVersion(version).setXid(xid);
383 LOG.debug("sending feature request for version={} and xid={}", version, xid);
384 Future<RpcResult<GetFeaturesOutput>> featuresFuture = connectionAdapter.getFeatures(featuresBuilder.build());
386 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(featuresFuture),
387 new FutureCallback<RpcResult<GetFeaturesOutput>>() {
389 public void onSuccess(@Nonnull RpcResult<GetFeaturesOutput> rpcFeatures) {
390 LOG.trace("features are back");
391 if (rpcFeatures.isSuccessful()) {
392 GetFeaturesOutput featureOutput = rpcFeatures.getResult();
393 if (!deviceConnectionRateLimiter.tryAquire()) {
394 LOG.warn("Openflowplugin hit the device connection rate limit threshold. Denying"
395 + " the connection from device {}", featureOutput.getDatapathId());
396 connectionAdapter.disconnect();
400 LOG.debug("obtained features: datapathId={}", featureOutput.getDatapathId());
401 LOG.debug("obtained features: auxiliaryId={}", featureOutput.getAuxiliaryId());
402 LOG.trace("handshake SETTLED: version={}, datapathId={}, auxiliaryId={}",
403 version, featureOutput.getDatapathId(),
404 featureOutput.getAuxiliaryId());
405 handshakeListener.onHandshakeSuccessful(featureOutput, proposedVersion);
408 LOG.warn("issuing disconnect during handshake [{}]",
409 connectionAdapter.getRemoteAddress());
410 for (RpcError rpcError : rpcFeatures.getErrors()) {
411 LOG.debug("handshake - features failure [{}]: i:{} | m:{} | s:{}", xid,
412 rpcError.getInfo(), rpcError.getMessage(), rpcError.getSeverity(),
413 rpcError.getCause());
415 handshakeListener.onHandshakeFailure();
418 LOG.debug("postHandshake DONE");
422 public void onFailure(Throwable throwable) {
423 LOG.warn("getting feature failed seriously [{}, addr:{}]: {}", xid,
424 connectionAdapter.getRemoteAddress(), throwable.getMessage());
425 LOG.trace("DETAIL of sending of hello failure:", throwable);
427 }, MoreExecutors.directExecutor());
428 LOG.debug("future features [{}] hooked ..", xid);
432 * Method for unit testing, only.
433 * This method is not thread safe and can only safely be used from a test.
436 @SuppressFBWarnings("IS2_INCONSISTENT_SYNC") // because shake() is synchronized
437 void setUseVersionBitmap(boolean useVersionBitmap) {
438 this.useVersionBitmap = useVersionBitmap;