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.Future;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.ThreadPoolExecutor;
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.api.OFConstants;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
23 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
24 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
25 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper.QueueType;
26 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperFactory;
27 import org.opendaylight.openflowplugin.openflow.md.queue.QueueProcessor;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortDescCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
56 import org.opendaylight.yangtools.yang.binding.DataObject;
57 import org.opendaylight.yangtools.yang.common.RpcError;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 import com.google.common.util.concurrent.Futures;
67 public class ConnectionConductorImpl implements OpenflowProtocolListener,
68 SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
70 /** ingress queue limit */
71 private static final int INGRESS_QUEUE_MAX_SIZE = 200;
73 protected static final Logger LOG = LoggerFactory
74 .getLogger(ConnectionConductorImpl.class);
76 /* variable to make BitMap-based negotiation enabled / disabled.
77 * it will help while testing and isolating issues related to processing of
78 * BitMaps from switches.
80 private boolean isBitmapNegotiationEnable = true;
81 protected ErrorHandler errorHandler;
83 private final ConnectionAdapter connectionAdapter;
84 private ConnectionConductor.CONDUCTOR_STATE conductorState;
85 private Short version;
87 protected SwitchConnectionDistinguisher auxiliaryKey;
89 protected SessionContext sessionContext;
91 private QueueProcessor<OfHeader, DataObject> queueProcessor;
92 private QueueKeeper<OfHeader> queue;
93 private ThreadPoolExecutor hsPool;
94 private HandshakeManager handshakeManager;
96 private boolean firstHelloProcessed;
98 private PortFeaturesUtil portFeaturesUtils;
100 private int conductorId;
102 private int ingressMaxQueueSize;
106 * @param connectionAdapter
108 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
109 this(connectionAdapter, INGRESS_QUEUE_MAX_SIZE);
113 * @param connectionAdapter
114 * @param ingressMaxQueueSize ingress queue limit (blocking)
116 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter, int ingressMaxQueueSize) {
117 this.connectionAdapter = connectionAdapter;
118 this.ingressMaxQueueSize = ingressMaxQueueSize;
119 conductorState = CONDUCTOR_STATE.HANDSHAKING;
120 firstHelloProcessed = false;
121 handshakeManager = new HandshakeManagerImpl(connectionAdapter,
122 ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
123 handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
124 handshakeManager.setHandshakeListener(this);
125 portFeaturesUtils = PortFeaturesUtil.getInstance();
130 int handshakeThreadLimit = 1;
131 hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit , handshakeThreadLimit, 0L,
132 TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
133 "OFHandshake-"+conductorId);
135 connectionAdapter.setMessageListener(this);
136 connectionAdapter.setSystemListener(this);
137 connectionAdapter.setConnectionReadyListener(this);
138 queue = QueueKeeperFactory.createFairQueueKeeper(queueProcessor, ingressMaxQueueSize);
142 public void setQueueProcessor(QueueProcessor<OfHeader, DataObject> queueProcessor) {
143 this.queueProcessor = queueProcessor;
147 * @param errorHandler the errorHandler to set
150 public void setErrorHandler(ErrorHandler errorHandler) {
151 this.errorHandler = errorHandler;
152 handshakeManager.setErrorHandler(errorHandler);
156 public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
157 new Thread(new Runnable() {
160 LOG.debug("echo request received: " + echoRequestMessage.getXid());
161 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
162 builder.setVersion(echoRequestMessage.getVersion());
163 builder.setXid(echoRequestMessage.getXid());
164 builder.setData(echoRequestMessage.getData());
166 getConnectionAdapter().echoReply(builder.build());
172 public void onErrorMessage(ErrorMessage errorMessage) {
173 enqueueMessage(errorMessage);
180 private void enqueueMessage(OfHeader message) {
181 enqueueMessage(message, QueueType.DEFAULT);
186 * @param queueType enqueue type
188 private void enqueueMessage(OfHeader message, QueueType queueType) {
189 queue.push(message, this, queueType);
193 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
194 enqueueMessage(experimenterMessage);
198 public void onFlowRemovedMessage(FlowRemovedMessage message) {
199 enqueueMessage(message);
204 * version negotiation happened as per following steps:
205 * 1. If HelloMessage version field has same version, continue connection processing.
206 * If HelloMessage version is lower than supported versions, just disconnect.
207 * 2. If HelloMessage contains bitmap and common version found in bitmap
208 * then continue connection processing. if no common version found, just disconnect.
209 * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
210 * 4. If Hello message received again with not supported version, just disconnect.
213 public void onHelloMessage(final HelloMessage hello) {
214 LOG.debug("processing HELLO.xid: {}", hello.getXid());
215 firstHelloProcessed = true;
216 checkState(CONDUCTOR_STATE.HANDSHAKING);
217 HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
218 hello, handshakeManager, connectionAdapter);
219 hsPool.submit(handshakeStepWrapper);
223 * @return rpc-response timeout in [ms]
225 protected long getMaxTimeout() {
226 // TODO:: get from configuration
231 * @return milliseconds
233 protected TimeUnit getMaxTimeoutUnit() {
234 // TODO:: get from configuration
235 return TimeUnit.MILLISECONDS;
239 public void onMultipartReplyMessage(MultipartReplyMessage message) {
240 enqueueMessage(message);
244 public void onPacketInMessage(PacketInMessage message) {
245 enqueueMessage(message, QueueKeeper.QueueType.UNORDERED);
249 public void onPortStatusMessage(PortStatusMessage message) {
250 processPortStatusMsg(message);
251 enqueueMessage(message);
254 protected void processPortStatusMsg(PortStatus msg) {
255 if (msg.getReason().getIntValue() == 2) {
257 } else if (msg.getReason().getIntValue() == 0) {
259 } else if (msg.getReason().getIntValue() == 1) {
264 protected void updatePort(PortStatus msg) {
265 Long portNumber = msg.getPortNo();
266 Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
268 if(portBandwidth == null) {
269 LOG.debug("can't get bandwidth info from port: {}, aborting port update", msg.toString());
271 this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
272 this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);
276 protected void deletePort(PortGrouping port) {
277 Long portNumber = port.getPortNo();
279 this.getSessionContext().getPhysicalPorts().remove(portNumber);
280 this.getSessionContext().getPortsBandwidth().remove(portNumber);
284 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
285 new Thread(new Runnable() {
288 if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
289 // idle state in any other conductorState than WORKING means real
290 // problem and wont be handled by echoReply, but disconnection
292 OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
294 LOG.debug("first idle state occured, sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
295 EchoInputBuilder builder = new EchoInputBuilder();
296 builder.setVersion(getVersion());
297 builder.setXid(getSessionContext().getNextXid());
299 Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
300 .echo(builder.build());
303 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
304 getMaxTimeoutUnit());
305 if (echoReplyValue.isSuccessful()) {
306 setConductorState(CONDUCTOR_STATE.WORKING);
308 for (RpcError replyError : echoReplyValue.getErrors()) {
309 Throwable cause = replyError.getCause();
311 "while receiving echoReply in TIMEOUTING state: "
312 + cause.getMessage(), cause);
314 //switch issue occurred
315 throw new Exception("switch issue occurred");
317 } catch (Exception e) {
318 LOG.error("while waiting for echoReply in TIMEOUTING state: "
320 errorHandler.handleException(e, sessionContext);
321 //switch is not responding
323 OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
332 * @param conductorState
333 * the connectionState to set
336 public void setConductorState(CONDUCTOR_STATE conductorState) {
337 this.conductorState = conductorState;
341 public CONDUCTOR_STATE getConductorState() {
342 return conductorState;
346 * @param expectedState
348 protected void checkState(CONDUCTOR_STATE expectedState) {
349 if (!conductorState.equals(expectedState)) {
350 throw new IllegalStateException("Expected state: " + expectedState
351 + ", actual state:" + conductorState);
356 public void onDisconnectEvent(DisconnectEvent arg0) {
357 SessionManager sessionManager = OFSessionUtil.getSessionManager();
358 sessionManager.invalidateOnDisconnect(this);
362 public Short getVersion() {
367 public Future<Boolean> disconnect() {
368 LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
370 Future<Boolean> result = null;
371 if (connectionAdapter.isAlive()) {
372 result = connectionAdapter.disconnect();
374 LOG.debug("connection already disconnected");
375 result = Futures.immediateFuture(true);
382 public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
383 this.auxiliaryKey = auxiliaryKey;
387 public void setSessionContext(SessionContext sessionContext) {
388 this.sessionContext = sessionContext;
392 public SwitchConnectionDistinguisher getAuxiliaryKey() {
397 public SessionContext getSessionContext() {
398 return sessionContext;
402 public ConnectionAdapter getConnectionAdapter() {
403 return connectionAdapter;
407 public void onConnectionReady() {
408 LOG.debug("connection is ready-to-use");
409 if (! firstHelloProcessed) {
410 HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
411 null, handshakeManager, connectionAdapter);
412 hsPool.execute(handshakeStepWrapper);
413 firstHelloProcessed = true;
415 LOG.debug("already touched by hello message");
420 public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
421 Short negotiatedVersion) {
422 postHandshakeBasic(featureOutput, negotiatedVersion);
424 // post-handshake actions
425 if(version == OFConstants.OFP_VERSION_1_3){
428 requestGroupFeatures();
429 requestMeterFeatures();
430 } else if (version == OFConstants.OFP_VERSION_1_0) {
431 // Because the GetFeaturesOutput contains information about the port
432 // in OF1.0 (that we would otherwise get from the PortDesc) we have to pass
433 // it up for parsing to convert into a NodeConnectorUpdate
434 enqueueMessage(featureOutput);
442 * @param featureOutput
443 * @param negotiatedVersion
445 protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
446 Short negotiatedVersion) {
447 version = negotiatedVersion;
448 OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
451 conductorState = CONDUCTOR_STATE.WORKING;
452 QueueKeeperFactory.plugQueue(queueProcessor, queue);
455 private void setDefaultConfig(){
456 SetConfigInputBuilder builder = new SetConfigInputBuilder();
457 builder.setVersion(getVersion());
458 builder.setXid(getSessionContext().getNextXid());
459 SwitchConfigFlag flag = SwitchConfigFlag.FRAGNORMAL;
460 builder.setFlags(flag);
461 builder.setMissSendLen(OFConstants.OFPCML_NO_BUFFER);
462 getConnectionAdapter().setConfig(builder.build());
466 * Send an OFPMP_DESC request message to the switch
468 private void requestDesc() {
469 MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
470 builder.setType(MultipartType.OFPMPDESC);
471 builder.setVersion(getVersion());
472 builder.setFlags(new MultipartRequestFlags(false));
473 builder.setMultipartRequestBody(new MultipartRequestDescCaseBuilder().build());
474 builder.setXid(getSessionContext().getNextXid());
475 getConnectionAdapter().multipartRequest(builder.build());
478 private void requestPorts() {
479 MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
480 builder.setType(MultipartType.OFPMPPORTDESC);
481 builder.setVersion(getVersion());
482 builder.setFlags(new MultipartRequestFlags(false));
483 builder.setMultipartRequestBody(new MultipartRequestPortDescCaseBuilder().build());
484 builder.setXid(getSessionContext().getNextXid());
485 getConnectionAdapter().multipartRequest(builder.build());
487 private void requestGroupFeatures(){
488 MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
489 mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
490 mprInput.setVersion(getVersion());
491 mprInput.setFlags(new MultipartRequestFlags(false));
492 mprInput.setXid(getSessionContext().getNextXid());
494 MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild =
495 new MultipartRequestGroupFeaturesCaseBuilder();
496 mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
498 LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
499 getConnectionAdapter().multipartRequest(mprInput.build());
502 private void requestMeterFeatures(){
503 MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
504 mprInput.setType(MultipartType.OFPMPMETERFEATURES);
505 mprInput.setVersion(getVersion());
506 mprInput.setFlags(new MultipartRequestFlags(false));
507 mprInput.setXid(getSessionContext().getNextXid());
509 MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild =
510 new MultipartRequestMeterFeaturesCaseBuilder();
511 mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
513 LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
514 getConnectionAdapter().multipartRequest(mprInput.build());
518 * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
520 public void setBitmapNegotiationEnable(
521 boolean isBitmapNegotiationEnable) {
522 this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
525 protected void shutdownPool() {
526 hsPool.shutdownNow();
527 LOG.debug("pool is terminated: {}", hsPool.isTerminated());
531 public void setId(int conductorId) {
532 this.conductorId = conductorId;