Suppressed WARN log of forwardingrules-manager
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / HandshakeContextImpl.java
1 /**
2  * Copyright (c) 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 package org.opendaylight.openflowplugin.impl.connection;
9
10 import java.util.concurrent.ThreadPoolExecutor;
11 import java.util.concurrent.TimeUnit;
12 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
13 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  *
19  */
20 public class HandshakeContextImpl implements HandshakeContext {
21
22     private static final Logger LOG = LoggerFactory.getLogger(HandshakeContextImpl.class);
23
24     private ThreadPoolExecutor handshakePool;
25     private HandshakeManager handshakeManager;
26
27     /**
28      * @param handshakePool
29      * @param handshakeManager
30      */
31     public HandshakeContextImpl(ThreadPoolExecutor handshakePool, HandshakeManager handshakeManager) {
32         this.handshakePool = handshakePool;
33         this.handshakeManager = handshakeManager;
34     }
35
36     @Override
37     public HandshakeManager getHandshakeManager() {
38         return handshakeManager;
39     }
40
41     @Override
42     public ThreadPoolExecutor getHandshakePool() {
43         return handshakePool;
44     }
45
46     @Override
47     public void close() throws Exception {
48         shutdownPoolPolitely();
49     }
50
51     private void shutdownPoolPolitely() {
52         LOG.debug("terminating handshake pool");
53         handshakePool.shutdown();
54         try {
55             handshakePool.awaitTermination(1, TimeUnit.SECONDS);
56         } catch (InterruptedException e) {
57             LOG.error("Error while awaiting termination on pool. Will use shutdownNow method.");
58         } finally {
59             handshakePool.purge();
60             if (! handshakePool.isTerminated()) {
61                 handshakePool.shutdownNow();
62             }
63             LOG.debug("pool is terminated: {}", handshakePool.isTerminated());
64         }
65     }
66 }