fix for BUG 609
[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.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15
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;
55
56 import com.google.common.util.concurrent.Futures;
57
58 /**
59  * @author mirehak
60  */
61 public class ConnectionConductorImpl implements OpenflowProtocolListener,
62         SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
63
64     protected static final Logger LOG = LoggerFactory
65             .getLogger(ConnectionConductorImpl.class);
66
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.
70      */
71     private boolean isBitmapNegotiationEnable = true;
72     protected ErrorHandler errorHandler;
73
74     private final ConnectionAdapter connectionAdapter;
75     private ConnectionConductor.CONDUCTOR_STATE conductorState;
76     private Short version;
77
78     protected SwitchConnectionDistinguisher auxiliaryKey;
79
80     protected SessionContext sessionContext;
81
82     private QueueKeeper<OfHeader, DataObject> queueKeeper;
83     private ExecutorService hsPool;
84     private HandshakeManager handshakeManager;
85
86     private boolean firstHelloProcessed;
87     
88     private PortFeaturesUtil portFeaturesUtils;
89
90     /**
91      * @param connectionAdapter
92      */
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();
103     }
104
105     @Override
106     public void init() {
107         connectionAdapter.setMessageListener(this);
108         connectionAdapter.setSystemListener(this);
109         connectionAdapter.setConnectionReadyListener(this);
110     }
111
112     @Override
113     public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
114         this.queueKeeper = queueKeeper;
115     }
116
117     /**
118      * @param errorHandler the errorHandler to set
119      */
120     @Override
121     public void setErrorHandler(ErrorHandler errorHandler) {
122         this.errorHandler = errorHandler;
123         handshakeManager.setErrorHandler(errorHandler);
124     }
125
126     @Override
127     public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
128         new Thread(new Runnable() {
129             @Override
130             public void run() {
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());
136
137                 getConnectionAdapter().echoReply(builder.build());
138             }
139         }).start();
140     }
141
142     @Override
143     public void onErrorMessage(ErrorMessage errorMessage) {
144         queueKeeper.push(errorMessage, this);
145     }
146
147     @Override
148     public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
149         queueKeeper.push(experimenterMessage, this);
150     }
151
152     @Override
153     public void onFlowRemovedMessage(FlowRemovedMessage message) {
154         queueKeeper.push(message, this);
155     }
156
157
158     /**
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.
166      */
167     @Override
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);
175     }
176
177     /**
178      * @return rpc-response timeout in [ms]
179      */
180     protected long getMaxTimeout() {
181         // TODO:: get from configuration
182         return 2000;
183     }
184
185     /**
186      * @return milliseconds
187      */
188     protected TimeUnit getMaxTimeoutUnit() {
189         // TODO:: get from configuration
190         return TimeUnit.MILLISECONDS;
191     }
192
193     @Override
194     public void onMultipartReplyMessage(MultipartReplyMessage message) {
195         queueKeeper.push(message, this);
196     }
197
198     @Override
199     public void onPacketInMessage(PacketInMessage message) {
200         queueKeeper.push(message, this, QueueKeeper.QueueType.UNORDERED);
201     }
202
203     @Override
204     public void onPortStatusMessage(PortStatusMessage message) {
205         processPortStatusMsg(message);
206         queueKeeper.push(message, this);
207     }
208     
209     protected void processPortStatusMsg(PortStatus msg) {
210         if (msg.getReason().getIntValue() == 2) {
211             updatePort(msg);
212         } else if (msg.getReason().getIntValue() == 0) {
213             updatePort(msg);
214         } else if (msg.getReason().getIntValue() == 1) {
215             deletePort(msg);
216         }
217     }
218     
219     protected void updatePort(PortStatus msg) {
220         Long portNumber = msg.getPortNo();        
221         Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
222         
223         if(portBandwidth == null) {
224             LOG.debug("can't get bandwidth info from port: {}, aborting port update", msg.toString());
225         } else {
226             this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
227             this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);                   
228         }            
229     }
230     
231     protected void deletePort(PortGrouping port) {
232         Long portNumber = port.getPortNo();
233         
234         this.getSessionContext().getPhysicalPorts().remove(portNumber);
235         this.getSessionContext().getPortsBandwidth().remove(portNumber);
236     }
237
238     @Override
239     public void onSwitchIdleEvent(SwitchIdleEvent notification) {
240         new Thread(new Runnable() {
241             @Override
242             public void run() {
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
246                     disconnect();
247                     OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
248                 } else {
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());
253
254                     Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
255                             .echo(builder.build());
256
257                     try {
258                         RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
259                                 getMaxTimeoutUnit());
260                         if (echoReplyValue.isSuccessful()) {
261                             setConductorState(CONDUCTOR_STATE.WORKING);
262                         } else {
263                             for (RpcError replyError : echoReplyValue.getErrors()) {
264                                 Throwable cause = replyError.getCause();
265                                 LOG.error(
266                                         "while receiving echoReply in TIMEOUTING state: "
267                                                 + cause.getMessage(), cause);
268                             }
269                             //switch issue occurred
270                             throw new Exception("switch issue occurred");
271                         }
272                     } catch (Exception e) {
273                         LOG.error("while waiting for echoReply in TIMEOUTING state: "
274                                 + e.getMessage());
275                         errorHandler.handleException(e, sessionContext);
276                         //switch is not responding
277                         disconnect();
278                         OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
279                     }
280                 }
281             }
282
283         }).start();
284     }
285
286     /**
287      * @param conductorState
288      *            the connectionState to set
289      */
290     @Override
291     public void setConductorState(CONDUCTOR_STATE conductorState) {
292         this.conductorState = conductorState;
293     }
294
295     @Override
296     public CONDUCTOR_STATE getConductorState() {
297         return conductorState;
298     }
299
300     /**
301      * @param handshaking
302      */
303     protected void checkState(CONDUCTOR_STATE expectedState) {
304         if (!conductorState.equals(expectedState)) {
305             throw new IllegalStateException("Expected state: " + expectedState
306                     + ", actual state:" + conductorState);
307         }
308     }
309
310     @Override
311     public void onDisconnectEvent(DisconnectEvent arg0) {
312         SessionManager sessionManager = OFSessionUtil.getSessionManager();
313         sessionManager.invalidateOnDisconnect(this);
314     }
315
316     @Override
317     public Short getVersion() {
318         return version;
319     }
320
321     @Override
322     public Future<Boolean> disconnect() {
323         LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
324
325         Future<Boolean> result = null;
326         if (connectionAdapter.isAlive()) {
327             result = connectionAdapter.disconnect();
328         } else {
329             LOG.debug("connection already disconnected");
330             result = Futures.immediateFuture(true);
331         }
332
333         return result;
334     }
335
336     @Override
337     public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
338         this.auxiliaryKey = auxiliaryKey;
339     }
340
341     @Override
342     public void setSessionContext(SessionContext sessionContext) {
343         this.sessionContext = sessionContext;
344     }
345
346     @Override
347     public SwitchConnectionDistinguisher getAuxiliaryKey() {
348         return auxiliaryKey;
349     }
350
351     @Override
352     public SessionContext getSessionContext() {
353         return sessionContext;
354     }
355
356     @Override
357     public ConnectionAdapter getConnectionAdapter() {
358         return connectionAdapter;
359     }
360
361     @Override
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;
369         } else {
370             LOG.debug("already touched by hello message");
371         }
372     }
373
374     @Override
375     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
376             Short negotiatedVersion) {
377         postHandshakeBasic(featureOutput, negotiatedVersion);
378         
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);
388         }
389         
390         requestDesc();
391         requestPorts();
392     }
393
394     /**
395      * used by tests
396      * @param featureOutput
397      * @param negotiatedVersion
398      */
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();
405     }
406
407     /*
408      *  Send an OFPMP_DESC request message to the switch
409      */
410
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());
419     }
420
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());
429     }
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());
436
437         MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild = 
438                 new MultipartRequestGroupFeaturesCaseBuilder();
439         mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
440
441         LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
442         getConnectionAdapter().multipartRequest(mprInput.build());
443         
444     }
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());
451
452         MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild =
453                 new MultipartRequestMeterFeaturesCaseBuilder();
454         mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
455
456         LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
457         getConnectionAdapter().multipartRequest(mprInput.build());
458         
459     }
460     /**
461      * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
462      */
463     public void setBitmapNegotiationEnable(
464             boolean isBitmapNegotiationEnable) {
465         this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
466     }
467
468     protected void shutdownPool() {
469         hsPool.shutdownNow();
470         LOG.debug("pool is terminated: {}", hsPool.isTerminated());
471     }
472 }