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.List;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.LinkedBlockingQueue;
14 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.collect.Lists;
43 public class ConnectionConductorImpl implements OpenflowProtocolListener,
44 SystemNotificationsListener, ConnectionConductor {
46 private static final Logger LOG = LoggerFactory
47 .getLogger(ConnectionConductorImpl.class);
49 private LinkedBlockingQueue<Exception> errorQueue = new LinkedBlockingQueue<>();
51 private final ConnectionAdapter connectionAdapter;
52 private final List<Short> versionOrder;
53 private ConnectionConductor.CONDUCTOR_STATE conductorState;
54 private Short version;
57 * @param connectionAdapter
59 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
60 this.connectionAdapter = connectionAdapter;
61 conductorState = CONDUCTOR_STATE.HANDSHAKING;
62 versionOrder = Lists.newArrayList((short) 0x04, (short) 0x01);
63 new Thread(new ErrorQueueHandler(errorQueue)).start();
68 connectionAdapter.setMessageListener(this);
69 connectionAdapter.setSystemListener(this);
73 public void onEchoRequestMessage(EchoRequestMessage echoRequestMessage) {
74 LOG.debug("echo request received: " + echoRequestMessage.getXid());
75 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
76 builder.setVersion(echoRequestMessage.getVersion());
77 builder.setXid(echoRequestMessage.getXid());
78 builder.setData(echoRequestMessage.getData());
80 connectionAdapter.echoReply(builder.build());
84 public void onErrorMessage(ErrorMessage errorMessage) {
85 // TODO Auto-generated method stub
86 LOG.debug("error received, type: " + errorMessage.getType()
87 + "; code: " + errorMessage.getCode());
91 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
92 // TODO Auto-generated method stub
93 LOG.debug("experimenter received, type: "
94 + experimenterMessage.getExpType());
98 public void onFlowRemovedMessage(FlowRemovedMessage arg0) {
99 // TODO Auto-generated method stub
103 public void onHelloMessage(HelloMessage hello) {
105 LOG.info("handshake STARTED");
106 checkState(CONDUCTOR_STATE.HANDSHAKING);
108 Short remoteVersion = hello.getVersion();
109 short proposedVersion;
111 proposedVersion = proposeVersion(remoteVersion);
112 } catch (Exception e) {
116 HelloInputBuilder helloBuilder = new HelloInputBuilder();
117 helloBuilder.setVersion(proposedVersion).setXid(hello.getXid());
118 LOG.debug("sending helloReply");
119 connectionAdapter.hello(helloBuilder.build());
121 if (proposedVersion != remoteVersion) {
122 // need to wait for another hello
124 // sent version is equal to remote --> version is negotiated
125 version = proposedVersion;
126 LOG.debug("version set: " + proposedVersion);
129 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
130 featuresBuilder.setVersion(version).setXid(hello.getXid());
131 Future<RpcResult<GetFeaturesOutput>> featuresFuture = connectionAdapter
132 .getFeatures(featuresBuilder.build());
133 LOG.debug("waiting for features");
134 RpcResult<GetFeaturesOutput> rpcFeatures;
136 rpcFeatures = featuresFuture.get(getMaxTimeout(),
137 TimeUnit.MILLISECONDS);
138 if (!rpcFeatures.isSuccessful()) {
139 LOG.error("obtained features problem: "
140 + rpcFeatures.getErrors());
142 LOG.debug("obtained features: datapathId="
143 + rpcFeatures.getResult().getDatapathId());
144 conductorState = CONDUCTOR_STATE.WORKING;
146 OFSessionUtil.registerSession(this, rpcFeatures.getResult(), version);
147 LOG.info("handshake SETTLED");
149 } catch (Exception e) {
156 * @return rpc-response timeout in [ms]
158 private long getMaxTimeout() {
159 // TODO:: get from configuration
166 private void handleException(Exception e) {
169 } catch (InterruptedException e1) {
170 LOG.error(e1.getMessage(), e1);
175 public void onMultipartReplyMessage(MultipartReplyMessage arg0) {
176 // TODO Auto-generated method stub
180 public void onMultipartRequestMessage(MultipartRequestMessage arg0) {
181 // TODO Auto-generated method stub
185 public void onPacketInMessage(PacketInMessage arg0) {
186 // TODO Auto-generated method stub
190 public void onPortStatusMessage(PortStatusMessage arg0) {
191 // TODO Auto-generated method stub
195 * @param conductorState
196 * the connectionState to set
199 public void setConductorState(CONDUCTOR_STATE conductorState) {
200 this.conductorState = conductorState;
204 public CONDUCTOR_STATE getConductorState() {
205 return conductorState;
211 private void checkState(CONDUCTOR_STATE expectedState) {
212 if (!conductorState.equals(expectedState)) {
213 throw new IllegalStateException("Expected state: " + expectedState
214 + ", actual state:" + conductorState);
219 public void onDisconnectEvent(DisconnectEvent arg0) {
220 // TODO Auto-generated method stub
223 protected short proposeVersion(short remoteVersion) {
224 Short proposal = null;
225 for (short offer : versionOrder) {
226 if (offer <= remoteVersion) {
231 if (proposal == null) {
232 throw new IllegalArgumentException("unsupported version: "
239 public Short getVersion() {