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.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
19 import org.opendaylight.openflowplugin.openflow.core.IMessageListener;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
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.MultipartRequestMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.common.RpcError;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.common.collect.ImmutableMap;
52 import com.google.common.collect.Lists;
57 public class ConnectionConductorImpl implements OpenflowProtocolListener,
58 SystemNotificationsListener, ConnectionConductor {
60 private static final Logger LOG = LoggerFactory
61 .getLogger(ConnectionConductorImpl.class);
63 /* variable to make BitMap-based negotiation enabled / disabled.
64 * it will help while testing and isolating issues related to processing of
65 * BitMaps from switches.
67 private static final boolean isBitmapNegotiationEnable = true;
68 private LinkedBlockingQueue<Exception> errorQueue = new LinkedBlockingQueue<>();
70 private final ConnectionAdapter connectionAdapter;
71 private final List<Short> versionOrder;
72 private ConnectionConductor.CONDUCTOR_STATE conductorState;
73 private Short version;
75 private SwitchConnectionDistinguisher auxiliaryKey;
77 private SessionContext sessionContext;
79 private ImmutableMap<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping;
81 private boolean isFirstHelloNegotiation = true;
86 * @param connectionAdapter
88 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
89 this.connectionAdapter = connectionAdapter;
90 conductorState = CONDUCTOR_STATE.HANDSHAKING;
91 versionOrder = Lists.newArrayList((short) 0x04, (short) 0x01);
92 // TODO: add a thread pool to handle ErrorQueueHandler
93 new Thread(new ErrorQueueHandler(errorQueue)).start();
98 connectionAdapter.setMessageListener(this);
99 connectionAdapter.setSystemListener(this);
100 //TODO : Wait for library to provide interface from which we can send first hello message
101 // sendFirstHelloMessage();
106 * send first hello message to switch
108 private void sendFirstHelloMessage() {
109 short highestVersion = versionOrder.get(0);
111 HelloInputBuilder helloInputbuilder = new HelloInputBuilder();
112 helloInputbuilder.setVersion(highestVersion);
113 helloInputbuilder.setXid(helloXid);
114 if (isBitmapNegotiationEnable) {
115 int elementsCount = highestVersion / Integer.SIZE;
116 ElementsBuilder elementsBuilder = new ElementsBuilder();
118 List<Elements> elementList = new ArrayList<Elements>();
119 int orderIndex = versionOrder.size();
120 int value = versionOrder.get(--orderIndex);
121 for (int index = 0; index <= elementsCount; index++) {
122 List<Boolean> booleanList = new ArrayList<Boolean>();
123 for (int i = 0; i < Integer.SIZE; i++) {
124 if (value == ((index * Integer.SIZE) + i)) {
125 booleanList.add(true);
126 value = (orderIndex == 0) ? highestVersion : versionOrder.get(--orderIndex);
128 booleanList.add(false);
131 elementsBuilder.setType(HelloElementType.forValue(1));
132 elementsBuilder.setVersionBitmap(booleanList);
133 elementList.add(elementsBuilder.build());
135 helloInputbuilder.setElements(elementList);
136 LOG.debug("sending first hello message: version header={} , version bitmap={}", highestVersion, elementList);
138 LOG.debug("sending first hello message: version header={} ", highestVersion);
140 connectionAdapter.hello(helloInputbuilder.build());
145 public void onEchoRequestMessage(EchoRequestMessage echoRequestMessage) {
146 LOG.debug("echo request received: " + echoRequestMessage.getXid());
147 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
148 builder.setVersion(echoRequestMessage.getVersion());
149 builder.setXid(echoRequestMessage.getXid());
150 builder.setData(echoRequestMessage.getData());
152 connectionAdapter.echoReply(builder.build());
156 public void onErrorMessage(ErrorMessage errorMessage) {
157 // TODO Auto-generated method stub
158 LOG.debug("error received, type: " + errorMessage.getType()
159 + "; code: " + errorMessage.getCode());
163 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
164 // TODO Auto-generated method stub
165 LOG.debug("experimenter received, type: "
166 + experimenterMessage.getExpType());
170 public void onFlowRemovedMessage(FlowRemovedMessage arg0) {
171 // TODO Auto-generated method stub
176 * version negotiation happened as per following steps:
177 * 1. If HelloMessage version field has same version, continue connection processing.
178 * If HelloMessage version is lower than supported versions, just disconnect.
179 * 2. If HelloMessage contains bitmap and common version found in bitmap
180 * then continue connection processing. if no common version found, just disconnect.
181 * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
182 * If Hello message received again with not supported version, just disconnect.
184 * TODO: Better to handle handshake into a maintainable innerclass which uses State-Pattern.
187 public void onHelloMessage(HelloMessage hello) {
189 LOG.info("handshake STARTED");
190 checkState(CONDUCTOR_STATE.HANDSHAKING);
192 Short remoteVersion = hello.getVersion();
193 List<Elements> elements = hello.getElements();
194 long xid = hello.getXid();
195 short proposedVersion;
196 LOG.debug("Hello message version={} and bitmap={}", remoteVersion, elements);
198 // find the version from header version field
199 proposedVersion = proposeVersion(remoteVersion);
201 } catch (IllegalArgumentException e) {
203 connectionAdapter.disconnect();
207 // sent version is equal to remote --> version is negotiated
208 if (proposedVersion == remoteVersion) {
209 LOG.debug("sending helloReply as version in header is supported: {}", proposedVersion);
210 sendHelloReply(proposedVersion, ++xid);
211 postHandshake(proposedVersion, ++xid);
213 } else if (isBitmapNegotiationEnable && null != elements && 0 != elements.size()) {
215 // hello contains version bitmap, checking highest common
217 proposedVersion = proposeBitmapVersion(elements);
218 } catch (IllegalArgumentException ex) {
220 connectionAdapter.disconnect();
223 LOG.debug("sending helloReply for common bitmap version : {}", proposedVersion);
224 sendHelloReply(proposedVersion, ++xid);
225 postHandshake(proposedVersion, ++xid);
227 if (isFirstHelloNegotiation) {
228 isFirstHelloNegotiation = false;
229 LOG.debug("sending helloReply for lowest supported version : {}", proposedVersion);
230 // send hello reply with lower version number supported
231 sendHelloReply(proposedVersion, ++xid);
233 // terminate the connection.
234 LOG.debug("Version negotiation failed. unsupported version : {}", remoteVersion);
235 connectionAdapter.disconnect();
242 * @param proposedVersion
245 private void sendHelloReply(Short proposedVersion, Long xid)
247 HelloInputBuilder helloBuilder = new HelloInputBuilder();
248 helloBuilder.setVersion(proposedVersion).setXid(xid);
249 connectionAdapter.hello(helloBuilder.build());
254 * after handshake set features, register to session
255 * @param proposedVersion
258 private void postHandshake(Short proposedVersion, Long xid) {
260 version = proposedVersion;
261 LOG.debug("version set: " + proposedVersion);
263 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
264 featuresBuilder.setVersion(version).setXid(xid);
265 LOG.debug("sending feature request for version={} and xid={}", version, xid);
266 Future<RpcResult<GetFeaturesOutput>> featuresFuture = connectionAdapter
267 .getFeatures(featuresBuilder.build());
268 LOG.debug("waiting for features");
269 RpcResult<GetFeaturesOutput> rpcFeatures;
271 rpcFeatures = featuresFuture.get(getMaxTimeout(),
272 TimeUnit.MILLISECONDS);
273 if (!rpcFeatures.isSuccessful()) {
274 LOG.error("obtained features problem: {}"
275 , rpcFeatures.getErrors());
277 GetFeaturesOutput featureOutput = rpcFeatures.getResult();
278 LOG.debug("obtained features: datapathId={}"
279 , featureOutput.getDatapathId());
280 conductorState = CONDUCTOR_STATE.WORKING;
282 OFSessionUtil.registerSession(this,
283 featureOutput, version);
284 LOG.info("handshake SETTLED: datapathId={}, auxiliaryId={}", featureOutput.getDatapathId(), featureOutput.getAuxiliaryId());
286 } catch (Exception e) {
292 * @return rpc-response timeout in [ms]
294 private long getMaxTimeout() {
295 // TODO:: get from configuration
302 private void handleException(Exception e) {
305 } catch (InterruptedException e1) {
306 LOG.error(e1.getMessage(), e1);
311 public void onMultipartReplyMessage(MultipartReplyMessage arg0) {
312 // TODO Auto-generated method stub
316 public void onMultipartRequestMessage(MultipartRequestMessage arg0) {
317 // TODO Auto-generated method stub
321 public void onPacketInMessage(PacketInMessage message) {
322 notifyListeners(PacketInMessage.class, message);
326 public void onPortStatusMessage(PortStatusMessage arg0) {
327 // TODO Auto-generated method stub
331 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
332 if (!CONDUCTOR_STATE.WORKING.equals(conductorState)) {
333 // idle state in any other conductorState than WORKING means real
334 // problem and wont be handled by echoReply, but disconnection
335 OFSessionUtil.getSessionManager().invalidateOnDisconnect(this);
337 LOG.debug("first idle state occured");
338 EchoInputBuilder builder = new EchoInputBuilder();
339 builder.setVersion(version);
340 // TODO: get xid from sessionContext
343 Future<RpcResult<EchoOutput>> echoReplyFuture = connectionAdapter
344 .echo(builder.build());
347 // TODO: read timeout from config
348 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(5,
350 if (echoReplyValue.isSuccessful()) {
351 conductorState = CONDUCTOR_STATE.WORKING;
353 for (RpcError replyError : echoReplyValue.getErrors()) {
354 Throwable cause = replyError.getCause();
356 "while receiving echoReply in TIMEOUTING state: "
357 + cause.getMessage(), cause);
360 } catch (Exception e) {
361 LOG.error("while waiting for echoReply in TIMEOUTING state: "
362 + e.getMessage(), e);
368 * @param conductorState
369 * the connectionState to set
372 public void setConductorState(CONDUCTOR_STATE conductorState) {
373 this.conductorState = conductorState;
377 public CONDUCTOR_STATE getConductorState() {
378 return conductorState;
384 private void checkState(CONDUCTOR_STATE expectedState) {
385 if (!conductorState.equals(expectedState)) {
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);
398 * find supported version based on remoteVersion
399 * @param remoteVersion
402 protected short proposeVersion(short remoteVersion) {
403 Short proposal = null;
404 for (short offer : versionOrder) {
405 if (offer <= remoteVersion) {
410 if (proposal == null) {
411 throw new IllegalArgumentException("unsupported version: "
418 * find common highest supported bitmap version
422 protected short proposeBitmapVersion(List<Elements> list)
424 Short supportedHighestVersion = null;
425 if((null != list) && (0 != list.size()))
427 for(Elements element : list)
429 List<Boolean> bitmap = element.getVersionBitmap();
430 // check for version bitmap
431 for(short bitPos : versionOrder)
433 // with all the version it should work.
434 if(bitmap.get(bitPos % Integer.SIZE))
436 supportedHighestVersion = bitPos;
441 if(null == supportedHighestVersion)
443 throw new IllegalArgumentException("unsupported bitmap version.");
448 return supportedHighestVersion;
452 public Short getVersion() {
457 public Future<Boolean> disconnect() {
458 return connectionAdapter.disconnect();
462 public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
463 this.auxiliaryKey = auxiliaryKey;
467 public void setSessionContext(SessionContext sessionContext) {
468 this.sessionContext = sessionContext;
472 public SwitchConnectionDistinguisher getAuxiliaryKey() {
477 public SessionContext getSessionContext() {
478 return sessionContext;
482 * @param listenerMapping the listenerMapping to set
484 public void setListenerMapping(
485 ImmutableMap<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping) {
486 //TODO: adjust the listener interface
487 this.listenerMapping = listenerMapping;
494 private void notifyListeners(Class<? extends DataObject> messageType, DataObject message) {
495 Collection<IMDMessageListener> listeners = listenerMapping.get(messageType);
496 if (listeners != null) {
497 for (IMDMessageListener listener : listeners) {
498 //TODO : need to add unit-tests
499 //listener.receive(this.getAuxiliaryKey(), this.getSessionContext(), message);
505 public ConnectionAdapter getConnectionAdapter() {
506 return connectionAdapter;