TlsDetector pipeline engaging parameterizable
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / SwitchConnectionProviderImpl.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2
3 package org.opendaylight.openflowjava.protocol.impl.connection;
4
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.concurrent.Future;
11
12 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
14 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
15 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
16 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.SettableFuture;
23
24 /**
25  * Exposed class for server handling
26  * @author mirehak
27  * @author michal.polkorab
28  */
29 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
30     
31     private static final Logger LOGGER = LoggerFactory
32             .getLogger(SwitchConnectionProviderImpl.class);
33     private SwitchConnectionHandler switchConnectionHandler;
34     private Set<ServerFacade> serverLot;
35
36     @Override
37     public void configure(Collection<ConnectionConfiguration> connConfigs) {
38         LOGGER.debug("Configuring ..");
39
40         //TODO - configure servers according to configuration
41         serverLot = new HashSet<>();
42         for (ConnectionConfiguration connConfig : connConfigs) {
43             TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
44             server.setSwitchConnectionHandler(switchConnectionHandler);
45             server.setSwitchIdleTimeout(connConfig.getSwitchIdleTimeout());
46             boolean tlsSupported = FEATURE_SUPPORT.REQUIRED.equals(connConfig.getTlsSupport());
47             server.setEncryption(tlsSupported);
48             serverLot.add(server);
49         }
50     }
51
52     @Override
53     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
54         LOGGER.debug("setSwitchConnectionHandler");
55         this.switchConnectionHandler = switchConnectionHandler;
56     }
57
58     @Override
59     public Future<List<Boolean>> shutdown() {
60         LOGGER.debug("Shutdown summoned");
61         ListenableFuture<List<Boolean>> result = SettableFuture.create();
62         try {
63             List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
64             for (ServerFacade server : serverLot) {
65                 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
66                 shutdownChain.add(shutdownFuture);
67             }
68             if (!shutdownChain.isEmpty()) {
69                 result = Futures.allAsList(shutdownChain);
70             } else {
71                 throw new IllegalStateException("No servers configured");
72             }
73         } catch (Exception e) {
74             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
75             exFuture.setException(e);
76             result = exFuture;
77         }
78         return result;
79     }
80
81     @Override
82     public Future<List<Boolean>> startup() {
83         LOGGER.debug("startup summoned");
84         ListenableFuture<List<Boolean>> result = SettableFuture.create();
85         try {
86             if (serverLot.isEmpty()) {
87                 throw new IllegalStateException("No servers configured");
88             }
89             for (ServerFacade server : serverLot) {
90                 if (server.getIsOnlineFuture().isDone()) {
91                     throw new IllegalStateException("Servers already running");
92                 }
93             }
94             if (switchConnectionHandler == null) {
95                 throw new IllegalStateException("switchConnectionHandler is not set");
96             }
97             List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
98             for (ServerFacade server : serverLot) {
99                 new Thread(server).start();
100                 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
101                 starterChain.add(isOnlineFuture);
102             }
103             if (!starterChain.isEmpty()) {
104                 result = Futures.allAsList(starterChain);
105             } else {
106                 throw new IllegalStateException("No servers configured");
107             }
108         } catch (Exception e) {
109             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
110             exFuture.setException(e);
111             result = exFuture;
112         }
113         return result;
114     }
115
116     /**
117      * @return servers
118      */
119     public Set<ServerFacade> getServerLot() {
120         return serverLot;
121     }
122
123 }