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.SwitchConnectionHandler;
14 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
15 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
25 * @author michal.polkorab
27 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
29 private static final Logger LOGGER = LoggerFactory
30 .getLogger(SwitchConnectionProviderImpl.class);
31 private SwitchConnectionHandler switchConnectionHandler;
32 private Set<ServerFacade> serverLot;
35 public void configure(Collection<ConnectionConfiguration> connConfigs) {
36 LOGGER.debug("Configuring ..");
38 //TODO - configure servers according to configuration
39 serverLot = new HashSet<>();
40 for (ConnectionConfiguration connConfig : connConfigs) {
41 TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
42 server.setSwitchConnectionHandler(switchConnectionHandler);
43 server.setSwitchIdleTimeout(connConfig.getSwitchIdleTimeout());
44 serverLot.add(server);
49 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
50 LOGGER.debug("setSwitchConnectionHandler");
51 this.switchConnectionHandler = switchConnectionHandler;
55 public Future<List<Boolean>> shutdown() {
56 LOGGER.debug("Shutdown summoned");
57 ListenableFuture<List<Boolean>> result = SettableFuture.create();
59 List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
60 for (ServerFacade server : serverLot) {
61 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
62 shutdownChain.add(shutdownFuture);
64 if (!shutdownChain.isEmpty()) {
65 result = Futures.allAsList(shutdownChain);
67 throw new IllegalStateException("No servers configured");
69 } catch (Exception e) {
70 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
71 exFuture.setException(e);
78 public Future<List<Boolean>> startup() {
79 LOGGER.debug("startup summoned");
80 ListenableFuture<List<Boolean>> result = SettableFuture.create();
82 if (serverLot.isEmpty()) {
83 throw new IllegalStateException("No servers configured");
85 for (ServerFacade server : serverLot) {
86 if (server.getIsOnlineFuture().isDone()) {
87 throw new IllegalStateException("Servers already running");
90 if (switchConnectionHandler == null) {
91 throw new IllegalStateException("switchConnectionHandler is not set");
93 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
94 for (ServerFacade server : serverLot) {
95 new Thread(server).start();
96 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
97 starterChain.add(isOnlineFuture);
99 if (!starterChain.isEmpty()) {
100 result = Futures.allAsList(starterChain);
102 throw new IllegalStateException("No servers configured");
104 } catch (Exception e) {
105 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
106 exFuture.setException(e);
112 public Set<ServerFacade> getServerLot() {