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