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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.collect.Lists;
42 public class ConnectionConductorImpl implements OpenflowProtocolListener,
43 SystemNotificationsListener, ConnectionConductor {
45 private static final Logger LOG = LoggerFactory
46 .getLogger(ConnectionConductorImpl.class);
48 private LinkedBlockingQueue<Exception> errorQueue = new LinkedBlockingQueue<>();
50 private final ConnectionAdapter connectionAdapter;
51 private final List<Short> versionOrder;
52 private ConnectionConductor.CONDUCTOR_STATE conductorState;
53 private Short version;
55 private GetFeaturesOutput features;
58 * @param connectionAdapter
60 public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
61 this.connectionAdapter = connectionAdapter;
62 conductorState = CONDUCTOR_STATE.HANDSHAKING;
63 versionOrder = Lists.newArrayList((short) 0x04, (short) 0x01);
64 new Thread(new ErrorQueueHandler(errorQueue)).start();
69 connectionAdapter.setMessageListener(this);
70 connectionAdapter.setSystemListener(this);
74 public void onEchoRequestMessage(EchoRequestMessage arg0) {
75 LOG.debug("echo request received: " + arg0.getXid());
76 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
77 builder.setVersion(arg0.getVersion());
78 builder.setXid(arg0.getXid());
79 builder.setData(arg0.getData());
81 connectionAdapter.echoReply(builder.build());
85 public void onErrorMessage(ErrorMessage errorMessage) {
86 // TODO Auto-generated method stub
87 LOG.debug("error received, type: " + errorMessage.getType()
88 + "; code: " + errorMessage.getCode());
92 public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
93 // TODO Auto-generated method stub
94 LOG.debug("experimenter received, type: "
95 + experimenterMessage.getExpType());
99 public void onFlowRemovedMessage(FlowRemovedMessage arg0) {
100 // TODO Auto-generated method stub
104 public void onHelloMessage(HelloMessage hello) {
106 LOG.info("handshake STARTED");
107 checkState(CONDUCTOR_STATE.HANDSHAKING);
109 Short remoteVersion = hello.getVersion();
110 short proposedVersion;
112 proposedVersion = proposeVersion(remoteVersion);
113 } catch (Exception e) {
117 HelloInputBuilder helloBuilder = new HelloInputBuilder();
118 helloBuilder.setVersion(proposedVersion).setXid(hello.getXid());
119 LOG.debug("sending helloReply");
120 connectionAdapter.hello(helloBuilder.build());
122 if (proposedVersion != remoteVersion) {
123 // need to wait for another hello
125 // sent version is equal to remote --> version is negotiated
126 version = proposedVersion;
127 LOG.debug("version set: " + proposedVersion);
130 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
131 featuresBuilder.setVersion(version).setXid(hello.getXid());
132 Future<RpcResult<GetFeaturesOutput>> featuresFuture = connectionAdapter
133 .getFeatures(featuresBuilder.build());
134 LOG.debug("waiting for features");
135 RpcResult<GetFeaturesOutput> rpcFeatures;
137 rpcFeatures = featuresFuture.get(getMaxTimeout(),
138 TimeUnit.MILLISECONDS);
139 LOG.debug("obtained features: datapathId="
140 + rpcFeatures.getResult().getDatapathId());
141 conductorState = CONDUCTOR_STATE.WORKING;
142 this.features = rpcFeatures.getResult();
143 LOG.info("handshake SETTLED");
144 } catch (Exception e) {
151 * @return rpc-response timeout in [ms]
153 private long getMaxTimeout() {
154 // TODO:: get from configuration
161 private void handleException(Exception e) {
164 } catch (InterruptedException e1) {
165 LOG.error(e1.getMessage(), e1);
170 public void onMultipartReplyMessage(MultipartReplyMessage arg0) {
171 // TODO Auto-generated method stub
175 public void onMultipartRequestMessage(MultipartRequestMessage arg0) {
176 // TODO Auto-generated method stub
180 public void onPacketInMessage(PacketInMessage arg0) {
181 // TODO Auto-generated method stub
185 public void onPortStatusMessage(PortStatusMessage arg0) {
186 // TODO Auto-generated method stub
190 * @param conductorState
191 * the connectionState to set
194 public void setConductorState(CONDUCTOR_STATE conductorState) {
195 this.conductorState = conductorState;
199 public CONDUCTOR_STATE getConductorState() {
200 return conductorState;
206 private void checkState(CONDUCTOR_STATE expectedState) {
207 if (!conductorState.equals(expectedState)) {
208 throw new IllegalStateException("Expected state: " + expectedState
209 + ", actual state:" + conductorState);
214 public void onDisconnectEvent(DisconnectEvent arg0) {
215 // TODO Auto-generated method stub
218 protected short proposeVersion(short remoteVersion) {
219 Short proposal = null;
220 for (short offer : versionOrder) {
221 if (offer <= remoteVersion) {
226 if (proposal == null) {
227 throw new IllegalArgumentException("unsupported version: "
234 public Short getVersion() {
239 public GetFeaturesOutput getFeatures() {