2 * Copyright (c) 2013, 2015 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.openflowplugin.openflow.md.core;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.LinkedBlockingQueue;
15 import java.util.concurrent.ThreadPoolExecutor;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.ErrorHandler;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
29 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
30 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper;
31 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper.QueueType;
32 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueProcessor;
33 import org.opendaylight.openflowplugin.api.openflow.md.queue.WaterMarkListener;
34 import org.opendaylight.openflowplugin.api.openflow.md.queue.WaterMarkListenerImpl;
35 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
36 import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
37 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperFactory;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
57 import org.opendaylight.yangtools.yang.binding.DataObject;
58 import org.opendaylight.yangtools.yang.common.RpcError;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
66 public class ConnectionConductorImpl implements OpenflowProtocolListener,
67 SystemNotificationsListener, ConnectionConductor,
68 ConnectionReadyListener, HandshakeListener, NotificationEnqueuer,
74 private static final int INGRESS_QUEUE_MAX_SIZE = 200;
76 protected static final Logger LOG = LoggerFactory
77 .getLogger(ConnectionConductorImpl.class);
80 * variable to make BitMap-based negotiation enabled / disabled. it will
81 * help while testing and isolating issues related to processing of BitMaps
84 private boolean isBitmapNegotiationEnable = true;
85 protected ErrorHandler errorHandler;
87 private final ConnectionAdapter connectionAdapter;
88 private ConnectionConductor.CONDUCTOR_STATE conductorState;
89 private Short version;
91 protected SwitchConnectionDistinguisher auxiliaryKey;
93 protected SessionContext sessionContext;
95 private QueueProcessor<OfHeader, DataObject> queueProcessor;
96 private QueueKeeper<OfHeader> queue;
97 private ThreadPoolExecutor hsPool;
98 private HandshakeManager handshakeManager;
100 private boolean firstHelloProcessed;
102 private PortFeaturesUtil portFeaturesUtils;
104 private int conductorId;
106 private int ingressMaxQueueSize;
107 private HandshakeContext handshakeContext;
110 * @param connectionAdapter connection adaptor for switch
112 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
113 this(connectionAdapter, INGRESS_QUEUE_MAX_SIZE);
117 * @param connectionAdapter connection adaptor for switch
118 * @param ingressMaxQueueSize ingress queue limit (blocking)
120 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter,
121 int ingressMaxQueueSize) {
122 this.connectionAdapter = connectionAdapter;
123 this.ingressMaxQueueSize = ingressMaxQueueSize;
124 conductorState = CONDUCTOR_STATE.HANDSHAKING;
125 firstHelloProcessed = false;
126 handshakeManager = new HandshakeManagerImpl(connectionAdapter,
127 ConnectionConductor.VERSION_ORDER.get(0),
128 ConnectionConductor.VERSION_ORDER);
129 handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
130 handshakeManager.setHandshakeListener(this);
131 portFeaturesUtils = PortFeaturesUtil.getInstance();
136 int handshakeThreadLimit = 1;
137 hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit,
138 handshakeThreadLimit, 0L, TimeUnit.MILLISECONDS,
139 new LinkedBlockingQueue<Runnable>(), "OFHandshake-"
142 connectionAdapter.setMessageListener(this);
143 connectionAdapter.setSystemListener(this);
144 connectionAdapter.setConnectionReadyListener(this);
145 WaterMarkListener waterMarkListener = new WaterMarkListenerImpl(
147 queue = QueueKeeperFactory.createFairQueueKeeper(queueProcessor,
148 ingressMaxQueueSize, waterMarkListener);
152 public void setQueueProcessor(
153 QueueProcessor<OfHeader, DataObject> queueProcessor) {
154 this.queueProcessor = queueProcessor;
158 * @param errorHandler the errorHandler to set
161 public void setErrorHandler(ErrorHandler errorHandler) {
162 this.errorHandler = errorHandler;
163 handshakeManager.setErrorHandler(errorHandler);
167 public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
168 new Thread(new Runnable() {
171 LOG.debug("echo request received: "
172 + echoRequestMessage.getXid());
173 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
174 builder.setVersion(echoRequestMessage.getVersion());
175 builder.setXid(echoRequestMessage.getXid());
176 builder.setData(echoRequestMessage.getData());
178 getConnectionAdapter().echoReply(builder.build());
184 public void onErrorMessage(ErrorMessage errorMessage) {
185 enqueueMessage(errorMessage);
191 private void enqueueMessage(OfHeader message) {
192 enqueueMessage(message, QueueType.DEFAULT);
196 public void enqueueNotification(NotificationQueueWrapper notification) {
197 enqueueMessage(notification);
202 * @param queueType enqueue type
204 private void enqueueMessage(OfHeader message, QueueType queueType) {
205 queue.push(message, this, queueType);
209 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
210 enqueueMessage(experimenterMessage);
214 public void onFlowRemovedMessage(FlowRemovedMessage message) {
215 enqueueMessage(message);
219 * version negotiation happened as per following steps: 1. If HelloMessage
220 * version field has same version, continue connection processing. If
221 * HelloMessage version is lower than supported versions, just disconnect.
222 * 2. If HelloMessage contains bitmap and common version found in bitmap
223 * then continue connection processing. if no common version found, just
224 * disconnect. 3. If HelloMessage version is not supported, send
225 * HelloMessage with lower supported version. 4. If Hello message received
226 * again with not supported version, just disconnect.
229 public void onHelloMessage(final HelloMessage hello) {
230 LOG.debug("processing HELLO.xid: {}", hello.getXid());
231 firstHelloProcessed = true;
232 checkState(CONDUCTOR_STATE.HANDSHAKING);
233 HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
234 hello, handshakeManager, connectionAdapter);
235 hsPool.submit(handshakeStepWrapper);
239 * @return rpc-response timeout in [ms]
241 protected long getMaxTimeout() {
242 // TODO:: get from configuration
247 * @return milliseconds
249 protected TimeUnit getMaxTimeoutUnit() {
250 // TODO:: get from configuration
251 return TimeUnit.MILLISECONDS;
255 public void onMultipartReplyMessage(MultipartReplyMessage message) {
256 enqueueMessage(message);
260 public void onPacketInMessage(PacketInMessage message) {
261 enqueueMessage(message, QueueKeeper.QueueType.UNORDERED);
265 public void onPortStatusMessage(PortStatusMessage message) {
267 processPortStatusMsg(message);
269 enqueueMessage(message);
273 protected void processPortStatusMsg(PortStatus msg) {
274 if (msg.getReason().getIntValue() == 2) {
276 } else if (msg.getReason().getIntValue() == 0) {
278 } else if (msg.getReason().getIntValue() == 1) {
283 protected void updatePort(PortStatus msg) {
284 Long portNumber = msg.getPortNo();
285 Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
287 if (portBandwidth == null) {
289 "can't get bandwidth info from port: {}, aborting port update",
292 if (null != this.sessionContext) {
293 //FIXME these two properties are never used in code
294 this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
295 this.getSessionContext().getPortsBandwidth()
296 .put(portNumber, portBandwidth);
298 LOG.warn("Trying to process update port message before session context was created.");
303 protected void deletePort(PortGrouping port) {
304 Long portNumber = port.getPortNo();
306 this.getSessionContext().getPhysicalPorts().remove(portNumber);
307 this.getSessionContext().getPortsBandwidth().remove(portNumber);
311 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
312 new Thread(new Runnable() {
315 if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
316 // idle state in any other conductorState than WORKING means
318 // problem and wont be handled by echoReply, but
321 OFSessionUtil.getSessionManager().invalidateOnDisconnect(
322 ConnectionConductorImpl.this);
325 "first idle state occured, sessionCtx={}|auxId={}",
326 sessionContext, auxiliaryKey);
327 EchoInputBuilder builder = new EchoInputBuilder();
328 builder.setVersion(getVersion());
329 builder.setXid(getSessionContext().getNextXid());
331 Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
332 .echo(builder.build());
335 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture
336 .get(getMaxTimeout(), getMaxTimeoutUnit());
337 if (echoReplyValue.isSuccessful()) {
338 setConductorState(CONDUCTOR_STATE.WORKING);
340 for (RpcError replyError : echoReplyValue
342 Throwable cause = replyError.getCause();
344 "while receiving echoReply in TIMEOUTING state: "
345 + cause.getMessage(), cause);
347 // switch issue occurred
348 throw new Exception("switch issue occurred");
350 } catch (Exception e) {
351 LOG.error("while waiting for echoReply in TIMEOUTING state: "
353 errorHandler.handleException(e, sessionContext);
354 // switch is not responding
356 OFSessionUtil.getSessionManager()
357 .invalidateOnDisconnect(
358 ConnectionConductorImpl.this);
367 * @param conductorState the connectionState to set
370 public void setConductorState(CONDUCTOR_STATE conductorState) {
371 this.conductorState = conductorState;
375 public CONDUCTOR_STATE getConductorState() {
376 return conductorState;
380 * @param expectedState connection conductor state
382 protected void checkState(CONDUCTOR_STATE expectedState) {
383 if (!conductorState.equals(expectedState)) {
384 LOG.warn("State of connection to switch {} is not correct, "
385 + "terminating the connection", connectionAdapter.getRemoteAddress());
386 throw new IllegalStateException("Expected state: " + expectedState
387 + ", actual state:" + conductorState);
392 public void onDisconnectEvent(DisconnectEvent arg0) {
393 SessionManager sessionManager = OFSessionUtil.getSessionManager();
394 sessionManager.invalidateOnDisconnect(this);
399 public Short getVersion() {
404 public Future<Boolean> disconnect() {
405 LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext,
408 Future<Boolean> result = null;
409 if (connectionAdapter.isAlive()) {
410 result = connectionAdapter.disconnect();
412 LOG.debug("connection already disconnected");
413 result = Futures.immediateFuture(true);
420 public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
421 this.auxiliaryKey = auxiliaryKey;
425 public void setSessionContext(SessionContext sessionContext) {
426 this.sessionContext = sessionContext;
430 public SwitchConnectionDistinguisher getAuxiliaryKey() {
435 public SessionContext getSessionContext() {
436 return sessionContext;
440 public ConnectionAdapter getConnectionAdapter() {
441 return connectionAdapter;
445 public void onConnectionReady() {
446 LOG.debug("connection is ready-to-use");
447 if (!firstHelloProcessed) {
448 checkState(CONDUCTOR_STATE.HANDSHAKING);
449 HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
450 null, handshakeManager, connectionAdapter);
451 hsPool.execute(handshakeStepWrapper);
452 firstHelloProcessed = true;
454 LOG.debug("already touched by hello message");
459 public void onHandshakeSuccessful(GetFeaturesOutput featureOutput,
460 Short negotiatedVersion) {
461 postHandshakeBasic(featureOutput, negotiatedVersion);
465 public void onHandshakeFailure() {
466 LOG.info("OF handshake failed, doing cleanup.");
473 * @param featureOutput feature request output
474 * @param negotiatedVersion negotiated openflow connection version
476 protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
477 Short negotiatedVersion) {
478 version = negotiatedVersion;
479 if (version == OFConstants.OFP_VERSION_1_0) {
480 // Because the GetFeaturesOutput contains information about the port
481 // in OF1.0 (that we would otherwise get from the PortDesc) we have
483 // it up for parsing to convert into a NodeConnectorUpdate
485 // BUG-1988 - this must be the first item in queue in order not to
486 // get behind link-up message
487 enqueueMessage(featureOutput);
490 SessionContext sessionContext = OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
493 conductorState = CONDUCTOR_STATE.WORKING;
494 QueueKeeperFactory.plugQueue(queueProcessor, queue);
498 * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
500 public void setBitmapNegotiationEnable(boolean isBitmapNegotiationEnable) {
501 this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
505 public void setId(int conductorId) {
506 this.conductorId = conductorId;
510 public void close() {
511 conductorState = CONDUCTOR_STATE.RIP;
512 if (handshakeContext != null) {
514 handshakeContext.close();
515 } catch (Exception e) {
516 LOG.warn("Closing handshake context failed: {}", e.getMessage());
517 LOG.debug("Detail in hanshake context close:", e);
520 //This condition will occure when Old Helium openflowplugin implementation will be used.
521 shutdownPoolPolitely();
525 private void shutdownPoolPolitely() {
526 LOG.debug("Terminating handshake pool for node {}", connectionAdapter.getRemoteAddress());
529 hsPool.awaitTermination(1, TimeUnit.SECONDS);
530 } catch (InterruptedException e) {
531 LOG.debug("Error while awaiting termination of pool. Will force shutdown now.");
534 if (!hsPool.isTerminated()) {
535 hsPool.shutdownNow();
537 LOG.debug("is handshake pool for node {} is terminated : {}",
538 connectionAdapter.getRemoteAddress(), hsPool.isTerminated());
543 public void setHandshakeContext(HandshakeContext handshakeContext) {
544 this.handshakeContext = handshakeContext;
548 ThreadPoolExecutor getHsPool() {