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
9 package org.opendaylight.openflowplugin.openflow.md.core;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
18 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
21 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.common.util.concurrent.Futures;
51 public class ConnectionConductorImpl implements OpenflowProtocolListener,
52 SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
54 protected static final Logger LOG = LoggerFactory
55 .getLogger(ConnectionConductorImpl.class);
57 /* variable to make BitMap-based negotiation enabled / disabled.
58 * it will help while testing and isolating issues related to processing of
59 * BitMaps from switches.
61 protected boolean isBitmapNegotiationEnable = true;
62 protected ErrorHandler errorHandler;
64 private final ConnectionAdapter connectionAdapter;
65 private ConnectionConductor.CONDUCTOR_STATE conductorState;
66 private Short version;
68 private SwitchConnectionDistinguisher auxiliaryKey;
70 private SessionContext sessionContext;
72 protected boolean isFirstHelloNegotiation = true;
73 protected Short lastProposedVersion = null;
75 private QueueKeeper<OfHeader, DataObject> queueKeeper;
76 private ExecutorService hsPool;
77 private HandshakeManager handshakeManager;
80 * @param connectionAdapter
82 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
83 this.connectionAdapter = connectionAdapter;
84 conductorState = CONDUCTOR_STATE.HANDSHAKING;
85 hsPool = Executors.newFixedThreadPool(1);
86 handshakeManager = new HandshakeManagerImpl(connectionAdapter,
87 ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
88 handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
89 handshakeManager.setHandshakeListener(this);
94 connectionAdapter.setMessageListener(this);
95 connectionAdapter.setSystemListener(this);
96 connectionAdapter.setConnectionReadyListener(this);
100 public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
101 this.queueKeeper = queueKeeper;
105 * @param errorHandler the errorHandler to set
108 public void setErrorHandler(ErrorHandler errorHandler) {
109 this.errorHandler = errorHandler;
110 handshakeManager.setErrorHandler(errorHandler);
114 public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
115 new Thread(new Runnable() {
118 LOG.debug("echo request received: " + echoRequestMessage.getXid());
119 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
120 builder.setVersion(echoRequestMessage.getVersion());
121 builder.setXid(echoRequestMessage.getXid());
122 builder.setData(echoRequestMessage.getData());
124 getConnectionAdapter().echoReply(builder.build());
130 public void onErrorMessage(ErrorMessage errorMessage) {
131 queueKeeper.push(ErrorMessage.class, errorMessage, this);
135 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
136 queueKeeper.push(ExperimenterMessage.class, experimenterMessage, this);
140 public void onFlowRemovedMessage(FlowRemovedMessage message) {
141 queueKeeper.push(FlowRemovedMessage.class, message, this);
146 * version negotiation happened as per following steps:
147 * 1. If HelloMessage version field has same version, continue connection processing.
148 * If HelloMessage version is lower than supported versions, just disconnect.
149 * 2. If HelloMessage contains bitmap and common version found in bitmap
150 * then continue connection processing. if no common version found, just disconnect.
151 * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
152 * 4. If Hello message received again with not supported version, just disconnect.
154 * TODO: Better to handle handshake into a maintainable innerclass which uses State-Pattern.
157 public synchronized void onHelloMessage(final HelloMessage hello) {
158 LOG.debug("processing HELLO.xid{}", hello.getXid());
159 checkState(CONDUCTOR_STATE.HANDSHAKING);
160 handshakeManager.setReceivedHello(hello);
161 hsPool.execute(handshakeManager);
165 * @return rpc-response timeout in [ms]
167 protected long getMaxTimeout() {
168 // TODO:: get from configuration
173 * @return milliseconds
175 protected TimeUnit getMaxTimeoutUnit() {
176 // TODO:: get from configuration
177 return TimeUnit.MILLISECONDS;
181 public void onMultipartReplyMessage(MultipartReplyMessage message) {
182 queueKeeper.push(MultipartReplyMessage.class, message, this);
186 public void onMultipartRequestMessage(MultipartRequestMessage message) {
187 queueKeeper.push(MultipartRequestMessage.class, message, this);
191 public void onPacketInMessage(PacketInMessage message) {
192 queueKeeper.push(PacketInMessage.class, message, this);
196 public void onPortStatusMessage(PortStatusMessage message) {
197 this.getSessionContext().processPortStatusMsg(message);
198 queueKeeper.push(PortStatusMessage.class, message, this);
202 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
203 new Thread(new Runnable() {
206 if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
207 // idle state in any other conductorState than WORKING means real
208 // problem and wont be handled by echoReply, but disconnection
210 OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
212 LOG.debug("first idle state occured");
213 EchoInputBuilder builder = new EchoInputBuilder();
214 builder.setVersion(getVersion());
215 builder.setXid(getSessionContext().getNextXid());
217 Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
218 .echo(builder.build());
221 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
222 getMaxTimeoutUnit());
223 if (echoReplyValue.isSuccessful()) {
224 setConductorState(CONDUCTOR_STATE.WORKING);
226 for (RpcError replyError : echoReplyValue.getErrors()) {
227 Throwable cause = replyError.getCause();
229 "while receiving echoReply in TIMEOUTING state: "
230 + cause.getMessage(), cause);
232 //switch issue occurred
233 throw new Exception("switch issue occurred");
235 } catch (Exception e) {
236 LOG.error("while waiting for echoReply in TIMEOUTING state: "
237 + e.getMessage(), e);
238 //switch is not responding
240 OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
249 * @param conductorState
250 * the connectionState to set
253 public void setConductorState(CONDUCTOR_STATE conductorState) {
254 this.conductorState = conductorState;
258 public CONDUCTOR_STATE getConductorState() {
259 return conductorState;
265 protected void checkState(CONDUCTOR_STATE expectedState) {
266 if (!conductorState.equals(expectedState)) {
267 throw new IllegalStateException("Expected state: " + expectedState
268 + ", actual state:" + conductorState);
273 public void onDisconnectEvent(DisconnectEvent arg0) {
274 SessionManager sessionManager = OFSessionUtil.getSessionManager();
275 sessionManager.invalidateOnDisconnect(this);
279 public Short getVersion() {
284 public Future<Boolean> disconnect() {
285 LOG.info("disconnecting: sessionCtx="+sessionContext+"|auxId="+auxiliaryKey);
287 Future<Boolean> result = null;
288 if (connectionAdapter.isAlive()) {
289 result = connectionAdapter.disconnect();
291 LOG.debug("connection already disconnected");
292 result = Futures.immediateFuture(true);
299 public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
300 this.auxiliaryKey = auxiliaryKey;
304 public void setSessionContext(SessionContext sessionContext) {
305 this.sessionContext = sessionContext;
309 public SwitchConnectionDistinguisher getAuxiliaryKey() {
314 public SessionContext getSessionContext() {
315 return sessionContext;
319 public ConnectionAdapter getConnectionAdapter() {
320 return connectionAdapter;
324 public void onConnectionReady() {
325 LOG.debug("connection is ready-to-use");
326 hsPool.execute(handshakeManager);
330 public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
331 Short negotiatedVersion) {
332 version = negotiatedVersion;
333 conductorState = CONDUCTOR_STATE.WORKING;
335 OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
339 * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
341 public void setBitmapNegotiationEnable(
342 boolean isBitmapNegotiationEnable) {
343 this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
346 protected void shutdownPool() {
347 hsPool.shutdownNow();
348 LOG.debug("pool is terminated: {}", hsPool.isTerminated());