1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
3 package org.opendaylight.openflowjava.protocol.impl.connection;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.HashSet;
10 import java.util.concurrent.Future;
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;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.SettableFuture;
25 * Exposed class for server handling
27 * @author michal.polkorab
29 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
31 private static final Logger LOGGER = LoggerFactory
32 .getLogger(SwitchConnectionProviderImpl.class);
33 private SwitchConnectionHandler switchConnectionHandler;
34 private Set<ServerFacade> serverLot;
37 public void configure(Collection<ConnectionConfiguration> connConfigs) {
38 LOGGER.debug("Configuring ..");
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);
53 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
54 LOGGER.debug("setSwitchConnectionHandler");
55 this.switchConnectionHandler = switchConnectionHandler;
59 public Future<List<Boolean>> shutdown() {
60 LOGGER.debug("Shutdown summoned");
61 ListenableFuture<List<Boolean>> result = SettableFuture.create();
63 List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
64 for (ServerFacade server : serverLot) {
65 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
66 shutdownChain.add(shutdownFuture);
68 if (!shutdownChain.isEmpty()) {
69 result = Futures.allAsList(shutdownChain);
71 throw new IllegalStateException("No servers configured");
73 } catch (Exception e) {
74 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
75 exFuture.setException(e);
82 public Future<List<Boolean>> startup() {
83 LOGGER.debug("startup summoned");
84 ListenableFuture<List<Boolean>> result = SettableFuture.create();
86 if (serverLot.isEmpty()) {
87 throw new IllegalStateException("No servers configured");
89 for (ServerFacade server : serverLot) {
90 if (server.getIsOnlineFuture().isDone()) {
91 throw new IllegalStateException("Servers already running");
94 if (switchConnectionHandler == null) {
95 throw new IllegalStateException("switchConnectionHandler is not set");
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);
103 if (!starterChain.isEmpty()) {
104 result = Futures.allAsList(starterChain);
106 throw new IllegalStateException("No servers configured");
108 } catch (Exception e) {
109 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
110 exFuture.setException(e);
119 public Set<ServerFacade> getServerLot() {