3b4d6bafbdef04a1e9514306b9ba149afc645bca
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImpl.java
1 /**
2  * Copyright (c) 2013, 2015 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 com.google.common.annotations.VisibleForTesting;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.LinkedBlockingQueue;
15 import java.util.concurrent.ThreadPoolExecutor;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.ErrorHandler;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
29 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
30 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper;
31 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper.QueueType;
32 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueProcessor;
33 import org.opendaylight.openflowplugin.api.openflow.md.queue.WaterMarkListener;
34 import org.opendaylight.openflowplugin.api.openflow.md.queue.WaterMarkListenerImpl;
35 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
36 import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
37 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperFactory;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
57 import org.opendaylight.yangtools.yang.binding.DataObject;
58 import org.opendaylight.yangtools.yang.common.RpcError;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  * @author mirehak
65  */
66 public class ConnectionConductorImpl implements OpenflowProtocolListener,
67         SystemNotificationsListener, ConnectionConductor,
68         ConnectionReadyListener, HandshakeListener, NotificationEnqueuer,
69         AutoCloseable {
70
71     /**
72      * ingress queue limit
73      */
74     private static final int INGRESS_QUEUE_MAX_SIZE = 200;
75
76     protected static final Logger LOG = LoggerFactory
77             .getLogger(ConnectionConductorImpl.class);
78
79     /*
80      * variable to make BitMap-based negotiation enabled / disabled. it will
81      * help while testing and isolating issues related to processing of BitMaps
82      * from switches.
83      */
84     private boolean isBitmapNegotiationEnable = true;
85     protected ErrorHandler errorHandler;
86
87     private final ConnectionAdapter connectionAdapter;
88     private ConnectionConductor.CONDUCTOR_STATE conductorState;
89     private Short version;
90
91     protected SwitchConnectionDistinguisher auxiliaryKey;
92
93     protected SessionContext sessionContext;
94
95     private QueueProcessor<OfHeader, DataObject> queueProcessor;
96     private QueueKeeper<OfHeader> queue;
97     private ThreadPoolExecutor hsPool;
98     private HandshakeManager handshakeManager;
99
100     private boolean firstHelloProcessed;
101
102     private PortFeaturesUtil portFeaturesUtils;
103
104     private int conductorId;
105
106     private int ingressMaxQueueSize;
107     private HandshakeContext handshakeContext;
108
109     /**
110      * @param connectionAdapter connection adaptor for switch
111      */
112     public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
113         this(connectionAdapter, INGRESS_QUEUE_MAX_SIZE);
114     }
115
116     /**
117      * @param connectionAdapter connection adaptor for switch
118      * @param ingressMaxQueueSize ingress queue limit (blocking)
119      */
120     public ConnectionConductorImpl(ConnectionAdapter connectionAdapter,
121                                    int ingressMaxQueueSize) {
122         this.connectionAdapter = connectionAdapter;
123         this.ingressMaxQueueSize = ingressMaxQueueSize;
124         conductorState = CONDUCTOR_STATE.HANDSHAKING;
125         firstHelloProcessed = false;
126         handshakeManager = new HandshakeManagerImpl(connectionAdapter,
127                 ConnectionConductor.VERSION_ORDER.get(0),
128                 ConnectionConductor.VERSION_ORDER);
129         handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
130         handshakeManager.setHandshakeListener(this);
131         portFeaturesUtils = PortFeaturesUtil.getInstance();
132     }
133
134     @Override
135     public void init() {
136         int handshakeThreadLimit = 1;
137         hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit,
138                 handshakeThreadLimit, 0L, TimeUnit.MILLISECONDS,
139                 new LinkedBlockingQueue<Runnable>(), "OFHandshake-"
140                 + conductorId);
141
142         connectionAdapter.setMessageListener(this);
143         connectionAdapter.setSystemListener(this);
144         connectionAdapter.setConnectionReadyListener(this);
145         WaterMarkListener waterMarkListener = new WaterMarkListenerImpl(
146                 connectionAdapter);
147         queue = QueueKeeperFactory.createFairQueueKeeper(queueProcessor,
148                 ingressMaxQueueSize, waterMarkListener);
149     }
150
151     @Override
152     public void setQueueProcessor(
153             QueueProcessor<OfHeader, DataObject> queueProcessor) {
154         this.queueProcessor = queueProcessor;
155     }
156
157     /**
158      * @param errorHandler the errorHandler to set
159      */
160     @Override
161     public void setErrorHandler(ErrorHandler errorHandler) {
162         this.errorHandler = errorHandler;
163         handshakeManager.setErrorHandler(errorHandler);
164     }
165
166     @Override
167     public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
168         new Thread(new Runnable() {
169             @Override
170             public void run() {
171                 LOG.debug("echo request received: "
172                         + echoRequestMessage.getXid());
173                 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
174                 builder.setVersion(echoRequestMessage.getVersion());
175                 builder.setXid(echoRequestMessage.getXid());
176                 builder.setData(echoRequestMessage.getData());
177
178                 getConnectionAdapter().echoReply(builder.build());
179             }
180         }).start();
181     }
182
183     @Override
184     public void onErrorMessage(ErrorMessage errorMessage) {
185         enqueueMessage(errorMessage);
186     }
187
188     /**
189      * @param message
190      */
191     private void enqueueMessage(OfHeader message) {
192         enqueueMessage(message, QueueType.DEFAULT);
193     }
194
195     @Override
196     public void enqueueNotification(NotificationQueueWrapper notification) {
197         enqueueMessage(notification);
198     }
199
200     /**
201      * @param message
202      * @param queueType enqueue type
203      */
204     private void enqueueMessage(OfHeader message, QueueType queueType) {
205         queue.push(message, this, queueType);
206     }
207
208     @Override
209     public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
210         enqueueMessage(experimenterMessage);
211     }
212
213     @Override
214     public void onFlowRemovedMessage(FlowRemovedMessage message) {
215         enqueueMessage(message);
216     }
217
218     /**
219      * version negotiation happened as per following steps: 1. If HelloMessage
220      * version field has same version, continue connection processing. If
221      * HelloMessage version is lower than supported versions, just disconnect.
222      * 2. If HelloMessage contains bitmap and common version found in bitmap
223      * then continue connection processing. if no common version found, just
224      * disconnect. 3. If HelloMessage version is not supported, send
225      * HelloMessage with lower supported version. 4. If Hello message received
226      * again with not supported version, just disconnect.
227      */
228     @Override
229     public void onHelloMessage(final HelloMessage hello) {
230         LOG.debug("processing HELLO.xid: {}", hello.getXid());
231         firstHelloProcessed = true;
232         checkState(CONDUCTOR_STATE.HANDSHAKING);
233         HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
234                 hello, handshakeManager, connectionAdapter);
235         hsPool.submit(handshakeStepWrapper);
236     }
237
238     /**
239      * @return rpc-response timeout in [ms]
240      */
241     protected long getMaxTimeout() {
242         // TODO:: get from configuration
243         return 2000;
244     }
245
246     /**
247      * @return milliseconds
248      */
249     protected TimeUnit getMaxTimeoutUnit() {
250         // TODO:: get from configuration
251         return TimeUnit.MILLISECONDS;
252     }
253
254     @Override
255     public void onMultipartReplyMessage(MultipartReplyMessage message) {
256         enqueueMessage(message);
257     }
258
259     @Override
260     public void onPacketInMessage(PacketInMessage message) {
261         enqueueMessage(message, QueueKeeper.QueueType.UNORDERED);
262     }
263
264     @Override
265     public void onPortStatusMessage(PortStatusMessage message) {
266         try {
267             processPortStatusMsg(message);
268         } finally {
269             enqueueMessage(message);
270         }
271     }
272
273     protected void processPortStatusMsg(PortStatus msg) {
274         if (msg.getReason().getIntValue() == 2) {
275             updatePort(msg);
276         } else if (msg.getReason().getIntValue() == 0) {
277             updatePort(msg);
278         } else if (msg.getReason().getIntValue() == 1) {
279             deletePort(msg);
280         }
281     }
282
283     protected void updatePort(PortStatus msg) {
284         Long portNumber = msg.getPortNo();
285         Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
286
287         if (portBandwidth == null) {
288             LOG.debug(
289                     "can't get bandwidth info from port: {}, aborting port update",
290                     msg.toString());
291         } else {
292             if (null != this.sessionContext) {
293                 //FIXME these two properties are never used in code
294                 this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
295                 this.getSessionContext().getPortsBandwidth()
296                         .put(portNumber, portBandwidth);
297             } else {
298                 LOG.warn("Trying to process update port message before session context was created.");
299             }
300         }
301     }
302
303     protected void deletePort(PortGrouping port) {
304         Long portNumber = port.getPortNo();
305
306         this.getSessionContext().getPhysicalPorts().remove(portNumber);
307         this.getSessionContext().getPortsBandwidth().remove(portNumber);
308     }
309
310     @Override
311     public void onSwitchIdleEvent(SwitchIdleEvent notification) {
312         new Thread(new Runnable() {
313             @Override
314             public void run() {
315                 if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
316                     // idle state in any other conductorState than WORKING means
317                     // real
318                     // problem and wont be handled by echoReply, but
319                     // disconnection
320                     disconnect();
321                     OFSessionUtil.getSessionManager().invalidateOnDisconnect(
322                             ConnectionConductorImpl.this);
323                 } else {
324                     LOG.debug(
325                             "first idle state occured, sessionCtx={}|auxId={}",
326                             sessionContext, auxiliaryKey);
327                     EchoInputBuilder builder = new EchoInputBuilder();
328                     builder.setVersion(getVersion());
329                     builder.setXid(getSessionContext().getNextXid());
330
331                     Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
332                             .echo(builder.build());
333
334                     try {
335                         RpcResult<EchoOutput> echoReplyValue = echoReplyFuture
336                                 .get(getMaxTimeout(), getMaxTimeoutUnit());
337                         if (echoReplyValue.isSuccessful()) {
338                             setConductorState(CONDUCTOR_STATE.WORKING);
339                         } else {
340                             for (RpcError replyError : echoReplyValue
341                                     .getErrors()) {
342                                 Throwable cause = replyError.getCause();
343                                 LOG.error(
344                                         "while receiving echoReply in TIMEOUTING state: "
345                                                 + cause.getMessage(), cause);
346                             }
347                             // switch issue occurred
348                             throw new Exception("switch issue occurred");
349                         }
350                     } catch (Exception e) {
351                         LOG.error("while waiting for echoReply in TIMEOUTING state: "
352                                 + e.getMessage());
353                         errorHandler.handleException(e, sessionContext);
354                         // switch is not responding
355                         disconnect();
356                         OFSessionUtil.getSessionManager()
357                                 .invalidateOnDisconnect(
358                                         ConnectionConductorImpl.this);
359                     }
360                 }
361             }
362
363         }).start();
364     }
365
366     /**
367      * @param conductorState the connectionState to set
368      */
369     @Override
370     public void setConductorState(CONDUCTOR_STATE conductorState) {
371         this.conductorState = conductorState;
372     }
373
374     @Override
375     public CONDUCTOR_STATE getConductorState() {
376         return conductorState;
377     }
378
379     /**
380      * @param expectedState connection conductor state
381      */
382     protected void checkState(CONDUCTOR_STATE expectedState) {
383         if (!conductorState.equals(expectedState)) {
384             LOG.warn("State of connection to switch {} is not correct, "
385                     + "terminating the connection", connectionAdapter.getRemoteAddress());
386             throw new IllegalStateException("Expected state: " + expectedState
387                     + ", actual state:" + conductorState);
388         }
389     }
390
391     @Override
392     public void onDisconnectEvent(DisconnectEvent arg0) {
393         SessionManager sessionManager = OFSessionUtil.getSessionManager();
394         sessionManager.invalidateOnDisconnect(this);
395         close();
396     }
397
398     @Override
399     public Short getVersion() {
400         return version;
401     }
402
403     @Override
404     public Future<Boolean> disconnect() {
405         LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext,
406                 auxiliaryKey);
407
408         Future<Boolean> result = null;
409         if (connectionAdapter.isAlive()) {
410             result = connectionAdapter.disconnect();
411         } else {
412             LOG.debug("connection already disconnected");
413             result = Futures.immediateFuture(true);
414         }
415         close();
416         return result;
417     }
418
419     @Override
420     public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
421         this.auxiliaryKey = auxiliaryKey;
422     }
423
424     @Override
425     public void setSessionContext(SessionContext sessionContext) {
426         this.sessionContext = sessionContext;
427     }
428
429     @Override
430     public SwitchConnectionDistinguisher getAuxiliaryKey() {
431         return auxiliaryKey;
432     }
433
434     @Override
435     public SessionContext getSessionContext() {
436         return sessionContext;
437     }
438
439     @Override
440     public ConnectionAdapter getConnectionAdapter() {
441         return connectionAdapter;
442     }
443
444     @Override
445     public void onConnectionReady() {
446         LOG.debug("connection is ready-to-use");
447         if (!firstHelloProcessed) {
448             checkState(CONDUCTOR_STATE.HANDSHAKING);
449             HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
450                     null, handshakeManager, connectionAdapter);
451             hsPool.execute(handshakeStepWrapper);
452             firstHelloProcessed = true;
453         } else {
454             LOG.debug("already touched by hello message");
455         }
456     }
457
458     @Override
459     public void onHandshakeSuccessful(GetFeaturesOutput featureOutput,
460                                       Short negotiatedVersion) {
461         postHandshakeBasic(featureOutput, negotiatedVersion);
462     }
463
464     @Override
465     public void onHandshakeFailure() {
466         LOG.info("OF handshake failed, doing cleanup.");
467         close();
468     }
469
470     /**
471      * used by tests
472      *
473      * @param featureOutput feature request output
474      * @param negotiatedVersion negotiated openflow connection version
475      */
476     protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
477                                       Short negotiatedVersion) {
478         version = negotiatedVersion;
479         if (version == OFConstants.OFP_VERSION_1_0) {
480             // Because the GetFeaturesOutput contains information about the port
481             // in OF1.0 (that we would otherwise get from the PortDesc) we have
482             // to pass
483             // it up for parsing to convert into a NodeConnectorUpdate
484             //
485             // BUG-1988 - this must be the first item in queue in order not to
486             // get behind link-up message
487             enqueueMessage(featureOutput);
488         }
489
490         SessionContext sessionContext =  OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
491         hsPool.shutdown();
492         hsPool.purge();
493         conductorState = CONDUCTOR_STATE.WORKING;
494         QueueKeeperFactory.plugQueue(queueProcessor, queue);
495     }
496
497     /**
498      * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
499      */
500     public void setBitmapNegotiationEnable(boolean isBitmapNegotiationEnable) {
501         this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
502     }
503
504     @Override
505     public void setId(int conductorId) {
506         this.conductorId = conductorId;
507     }
508
509     @Override
510     public void close() {
511         conductorState = CONDUCTOR_STATE.RIP;
512         if (handshakeContext != null) {
513             try {
514                 handshakeContext.close();
515             } catch (Exception e) {
516                 LOG.warn("Closing handshake context failed: {}", e.getMessage());
517                 LOG.debug("Detail in hanshake context close:", e);
518             }
519         } else {
520             //This condition will occure when Old Helium openflowplugin implementation will be used.
521             shutdownPoolPolitely();
522         }
523     }
524
525     private void shutdownPoolPolitely() {
526         LOG.debug("Terminating handshake pool for node {}", connectionAdapter.getRemoteAddress());
527         hsPool.shutdown();
528         try {
529             hsPool.awaitTermination(1, TimeUnit.SECONDS);
530         } catch (InterruptedException e) {
531             LOG.debug("Error while awaiting termination of pool. Will force shutdown now.");
532         } finally {
533             hsPool.purge();
534             if (!hsPool.isTerminated()) {
535                 hsPool.shutdownNow();
536             }
537             LOG.debug("is handshake pool for node {} is terminated : {}",
538                     connectionAdapter.getRemoteAddress(), hsPool.isTerminated());
539         }
540     }
541
542     @Override
543     public void setHandshakeContext(HandshakeContext handshakeContext) {
544         this.handshakeContext = handshakeContext;
545     }
546
547     @VisibleForTesting
548     ThreadPoolExecutor getHsPool() {
549         return hsPool;
550     }
551 }