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.OFConstants;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
23 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortDescCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.common.RpcError;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import com.google.common.util.concurrent.Futures;
61 public class ConnectionConductorImpl implements OpenflowProtocolListener,
62 SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
64 protected static final Logger LOG = LoggerFactory
65 .getLogger(ConnectionConductorImpl.class);
67 /* variable to make BitMap-based negotiation enabled / disabled.
68 * it will help while testing and isolating issues related to processing of
69 * BitMaps from switches.
71 private boolean isBitmapNegotiationEnable = true;
72 protected ErrorHandler errorHandler;
74 private final ConnectionAdapter connectionAdapter;
75 private ConnectionConductor.CONDUCTOR_STATE conductorState;
76 private Short version;
78 protected SwitchConnectionDistinguisher auxiliaryKey;
80 protected SessionContext sessionContext;
82 private QueueKeeper<OfHeader, DataObject> queueKeeper;
83 private ExecutorService hsPool;
84 private HandshakeManager handshakeManager;
86 private boolean firstHelloProcessed;
88 private PortFeaturesUtil portFeaturesUtils;
91 * @param connectionAdapter
93 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
94 this.connectionAdapter = connectionAdapter;
95 conductorState = CONDUCTOR_STATE.HANDSHAKING;
96 hsPool = Executors.newFixedThreadPool(1);
97 firstHelloProcessed = false;
98 handshakeManager = new HandshakeManagerImpl(connectionAdapter,
99 ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
100 handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
101 handshakeManager.setHandshakeListener(this);
102 portFeaturesUtils = PortFeaturesUtil.getInstance();
107 connectionAdapter.setMessageListener(this);
108 connectionAdapter.setSystemListener(this);
109 connectionAdapter.setConnectionReadyListener(this);
113 public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
114 this.queueKeeper = queueKeeper;
118 * @param errorHandler the errorHandler to set
121 public void setErrorHandler(ErrorHandler errorHandler) {
122 this.errorHandler = errorHandler;
123 handshakeManager.setErrorHandler(errorHandler);
127 public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
128 new Thread(new Runnable() {
131 LOG.debug("echo request received: " + echoRequestMessage.getXid());
132 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
133 builder.setVersion(echoRequestMessage.getVersion());
134 builder.setXid(echoRequestMessage.getXid());
135 builder.setData(echoRequestMessage.getData());
137 getConnectionAdapter().echoReply(builder.build());
143 public void onErrorMessage(ErrorMessage errorMessage) {
144 queueKeeper.push(errorMessage, this);
148 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
149 queueKeeper.push(experimenterMessage, this);
153 public void onFlowRemovedMessage(FlowRemovedMessage message) {
154 queueKeeper.push(message, this);
159 * version negotiation happened as per following steps:
160 * 1. If HelloMessage version field has same version, continue connection processing.
161 * If HelloMessage version is lower than supported versions, just disconnect.
162 * 2. If HelloMessage contains bitmap and common version found in bitmap
163 * then continue connection processing. if no common version found, just disconnect.
164 * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
165 * 4. If Hello message received again with not supported version, just disconnect.
168 public synchronized void onHelloMessage(final HelloMessage hello) {
169 LOG.debug("processing HELLO.xid: {}", hello.getXid());
170 firstHelloProcessed = true;
171 checkState(CONDUCTOR_STATE.HANDSHAKING);
172 HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
173 hello, handshakeManager, connectionAdapter);
174 hsPool.submit(handshakeStepWrapper);
178 * @return rpc-response timeout in [ms]
180 protected long getMaxTimeout() {
181 // TODO:: get from configuration
186 * @return milliseconds
188 protected TimeUnit getMaxTimeoutUnit() {
189 // TODO:: get from configuration
190 return TimeUnit.MILLISECONDS;
194 public void onMultipartReplyMessage(MultipartReplyMessage message) {
195 queueKeeper.push(message, this);
199 public void onPacketInMessage(PacketInMessage message) {
200 queueKeeper.push(message, this, QueueKeeper.QueueType.UNORDERED);
204 public void onPortStatusMessage(PortStatusMessage message) {
205 processPortStatusMsg(message);
206 queueKeeper.push(message, this);
209 protected void processPortStatusMsg(PortStatus msg) {
210 if (msg.getReason().getIntValue() == 2) {
212 } else if (msg.getReason().getIntValue() == 0) {
214 } else if (msg.getReason().getIntValue() == 1) {
219 protected void updatePort(PortStatus msg) {
220 Long portNumber = msg.getPortNo();
221 Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
223 if(portBandwidth == null) {
224 LOG.debug("can't get bandwidth info from port: {}, aborting port update", msg.toString());
226 this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
227 this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);
231 protected void deletePort(PortGrouping port) {
232 Long portNumber = port.getPortNo();
234 this.getSessionContext().getPhysicalPorts().remove(portNumber);
235 this.getSessionContext().getPortsBandwidth().remove(portNumber);
239 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
240 new Thread(new Runnable() {
243 if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
244 // idle state in any other conductorState than WORKING means real
245 // problem and wont be handled by echoReply, but disconnection
247 OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
249 LOG.debug("first idle state occured, sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
250 EchoInputBuilder builder = new EchoInputBuilder();
251 builder.setVersion(getVersion());
252 builder.setXid(getSessionContext().getNextXid());
254 Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
255 .echo(builder.build());
258 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
259 getMaxTimeoutUnit());
260 if (echoReplyValue.isSuccessful()) {
261 setConductorState(CONDUCTOR_STATE.WORKING);
263 for (RpcError replyError : echoReplyValue.getErrors()) {
264 Throwable cause = replyError.getCause();
266 "while receiving echoReply in TIMEOUTING state: "
267 + cause.getMessage(), cause);
269 //switch issue occurred
270 throw new Exception("switch issue occurred");
272 } catch (Exception e) {
273 LOG.error("while waiting for echoReply in TIMEOUTING state: "
275 errorHandler.handleException(e, sessionContext);
276 //switch is not responding
278 OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
287 * @param conductorState
288 * the connectionState to set
291 public void setConductorState(CONDUCTOR_STATE conductorState) {
292 this.conductorState = conductorState;
296 public CONDUCTOR_STATE getConductorState() {
297 return conductorState;
303 protected void checkState(CONDUCTOR_STATE expectedState) {
304 if (!conductorState.equals(expectedState)) {
305 throw new IllegalStateException("Expected state: " + expectedState
306 + ", actual state:" + conductorState);
311 public void onDisconnectEvent(DisconnectEvent arg0) {
312 SessionManager sessionManager = OFSessionUtil.getSessionManager();
313 sessionManager.invalidateOnDisconnect(this);
317 public Short getVersion() {
322 public Future<Boolean> disconnect() {
323 LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
325 Future<Boolean> result = null;
326 if (connectionAdapter.isAlive()) {
327 result = connectionAdapter.disconnect();
329 LOG.debug("connection already disconnected");
330 result = Futures.immediateFuture(true);
337 public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
338 this.auxiliaryKey = auxiliaryKey;
342 public void setSessionContext(SessionContext sessionContext) {
343 this.sessionContext = sessionContext;
347 public SwitchConnectionDistinguisher getAuxiliaryKey() {
352 public SessionContext getSessionContext() {
353 return sessionContext;
357 public ConnectionAdapter getConnectionAdapter() {
358 return connectionAdapter;
362 public synchronized void onConnectionReady() {
363 LOG.debug("connection is ready-to-use");
364 if (! firstHelloProcessed) {
365 HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
366 null, handshakeManager, connectionAdapter);
367 hsPool.execute(handshakeStepWrapper);
368 firstHelloProcessed = true;
370 LOG.debug("already touched by hello message");
375 public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
376 Short negotiatedVersion) {
377 postHandshakeBasic(featureOutput, negotiatedVersion);
379 // post-handshake actions
380 if(version == OFConstants.OFP_VERSION_1_3){
381 requestGroupFeatures();
382 requestMeterFeatures();
383 } else if (version == OFConstants.OFP_VERSION_1_0) {
384 // Because the GetFeaturesOutput contains information about the port
385 // in OF1.0 (that we would otherwise get from the PortDesc) we have to pass
386 // it up for parsing to convert into a NodeConnectorUpdate
387 queueKeeper.push(featureOutput, this);
396 * @param featureOutput
397 * @param negotiatedVersion
399 protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
400 Short negotiatedVersion) {
401 version = negotiatedVersion;
402 conductorState = CONDUCTOR_STATE.WORKING;
403 OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
404 hsPool.shutdownNow();
408 * Send an OFPMP_DESC request message to the switch
411 private void requestDesc() {
412 MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
413 builder.setType(MultipartType.OFPMPDESC);
414 builder.setVersion(getVersion());
415 builder.setFlags(new MultipartRequestFlags(false));
416 builder.setMultipartRequestBody(new MultipartRequestDescCaseBuilder().build());
417 builder.setXid(getSessionContext().getNextXid());
418 getConnectionAdapter().multipartRequest(builder.build());
421 private void requestPorts() {
422 MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
423 builder.setType(MultipartType.OFPMPPORTDESC);
424 builder.setVersion(getVersion());
425 builder.setFlags(new MultipartRequestFlags(false));
426 builder.setMultipartRequestBody(new MultipartRequestPortDescCaseBuilder().build());
427 builder.setXid(getSessionContext().getNextXid());
428 getConnectionAdapter().multipartRequest(builder.build());
430 private void requestGroupFeatures(){
431 MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
432 mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
433 mprInput.setVersion(getVersion());
434 mprInput.setFlags(new MultipartRequestFlags(false));
435 mprInput.setXid(getSessionContext().getNextXid());
437 MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild =
438 new MultipartRequestGroupFeaturesCaseBuilder();
439 mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
441 LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
442 getConnectionAdapter().multipartRequest(mprInput.build());
445 private void requestMeterFeatures(){
446 MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
447 mprInput.setType(MultipartType.OFPMPMETERFEATURES);
448 mprInput.setVersion(getVersion());
449 mprInput.setFlags(new MultipartRequestFlags(false));
450 mprInput.setXid(getSessionContext().getNextXid());
452 MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild =
453 new MultipartRequestMeterFeaturesCaseBuilder();
454 mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
456 LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
457 getConnectionAdapter().multipartRequest(mprInput.build());
461 * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
463 public void setBitmapNegotiationEnable(
464 boolean isBitmapNegotiationEnable) {
465 this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
468 protected void shutdownPool() {
469 hsPool.shutdownNow();
470 LOG.debug("pool is terminated: {}", hsPool.isTerminated());