sessionManager proposal
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImpl.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core;
10
11 import java.util.List;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.LinkedBlockingQueue;
14 import java.util.concurrent.TimeUnit;
15
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;
37
38 import com.google.common.collect.Lists;
39
40 /**
41  * @author mirehak
42  */
43 public class ConnectionConductorImpl implements OpenflowProtocolListener,
44         SystemNotificationsListener, ConnectionConductor {
45
46     private static final Logger LOG = LoggerFactory
47             .getLogger(ConnectionConductorImpl.class);
48
49     private LinkedBlockingQueue<Exception> errorQueue = new LinkedBlockingQueue<>();
50
51     private final ConnectionAdapter connectionAdapter;
52     private final List<Short> versionOrder;
53     private ConnectionConductor.CONDUCTOR_STATE conductorState;
54     private Short version;
55
56     /**
57      * @param connectionAdapter
58      */
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();
64     }
65
66     @Override
67     public void init() {
68         connectionAdapter.setMessageListener(this);
69         connectionAdapter.setSystemListener(this);
70     }
71
72     @Override
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());
79
80         connectionAdapter.echoReply(builder.build());
81     }
82
83     @Override
84     public void onErrorMessage(ErrorMessage errorMessage) {
85         // TODO Auto-generated method stub
86         LOG.debug("error received, type: " + errorMessage.getType()
87                 + "; code: " + errorMessage.getCode());
88     }
89
90     @Override
91     public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
92         // TODO Auto-generated method stub
93         LOG.debug("experimenter received, type: "
94                 + experimenterMessage.getExpType());
95     }
96
97     @Override
98     public void onFlowRemovedMessage(FlowRemovedMessage arg0) {
99         // TODO Auto-generated method stub
100     }
101
102     @Override
103     public void onHelloMessage(HelloMessage hello) {
104         // do handshake
105         LOG.info("handshake STARTED");
106         checkState(CONDUCTOR_STATE.HANDSHAKING);
107
108         Short remoteVersion = hello.getVersion();
109         short proposedVersion;
110         try {
111             proposedVersion = proposeVersion(remoteVersion);
112         } catch (Exception e) {
113             handleException(e);
114             throw e;
115         }
116         HelloInputBuilder helloBuilder = new HelloInputBuilder();
117         helloBuilder.setVersion(proposedVersion).setXid(hello.getXid());
118         LOG.debug("sending helloReply");
119         connectionAdapter.hello(helloBuilder.build());
120
121         if (proposedVersion != remoteVersion) {
122             // need to wait for another hello
123         } else {
124             // sent version is equal to remote --> version is negotiated
125             version = proposedVersion;
126             LOG.debug("version set: " + proposedVersion);
127
128             // request features
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;
135             try {
136                 rpcFeatures = featuresFuture.get(getMaxTimeout(),
137                         TimeUnit.MILLISECONDS);
138                 if (!rpcFeatures.isSuccessful()) {
139                     LOG.error("obtained features problem: "
140                             + rpcFeatures.getErrors());
141                 } else {
142                     LOG.debug("obtained features: datapathId="
143                             + rpcFeatures.getResult().getDatapathId());
144                     conductorState = CONDUCTOR_STATE.WORKING;
145
146                     OFSessionUtil.registerSession(this, rpcFeatures.getResult(), version);
147                     LOG.info("handshake SETTLED");
148                 }
149             } catch (Exception e) {
150                 handleException(e);
151             }
152         }
153     }
154
155     /**
156      * @return rpc-response timeout in [ms]
157      */
158     private long getMaxTimeout() {
159         // TODO:: get from configuration
160         return 2000;
161     }
162
163     /**
164      * @param e
165      */
166     private void handleException(Exception e) {
167         try {
168             errorQueue.put(e);
169         } catch (InterruptedException e1) {
170             LOG.error(e1.getMessage(), e1);
171         }
172     }
173
174     @Override
175     public void onMultipartReplyMessage(MultipartReplyMessage arg0) {
176         // TODO Auto-generated method stub
177     }
178
179     @Override
180     public void onMultipartRequestMessage(MultipartRequestMessage arg0) {
181         // TODO Auto-generated method stub
182     }
183
184     @Override
185     public void onPacketInMessage(PacketInMessage arg0) {
186         // TODO Auto-generated method stub
187     }
188
189     @Override
190     public void onPortStatusMessage(PortStatusMessage arg0) {
191         // TODO Auto-generated method stub
192     }
193
194     /**
195      * @param conductorState
196      *            the connectionState to set
197      */
198     @Override
199     public void setConductorState(CONDUCTOR_STATE conductorState) {
200         this.conductorState = conductorState;
201     }
202
203     @Override
204     public CONDUCTOR_STATE getConductorState() {
205         return conductorState;
206     }
207
208     /**
209      * @param handshaking
210      */
211     private void checkState(CONDUCTOR_STATE expectedState) {
212         if (!conductorState.equals(expectedState)) {
213             throw new IllegalStateException("Expected state: " + expectedState
214                     + ", actual state:" + conductorState);
215         }
216     }
217
218     @Override
219     public void onDisconnectEvent(DisconnectEvent arg0) {
220         // TODO Auto-generated method stub
221     }
222
223     protected short proposeVersion(short remoteVersion) {
224         Short proposal = null;
225         for (short offer : versionOrder) {
226             if (offer <= remoteVersion) {
227                 proposal = offer;
228                 break;
229             }
230         }
231         if (proposal == null) {
232             throw new IllegalArgumentException("unsupported version: "
233                     + remoteVersion);
234         }
235         return proposal;
236     }
237
238     @Override
239     public Short getVersion() {
240         return version;
241     }
242 }