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;
8 import java.util.Iterator;
11 import java.util.concurrent.Future;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
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;
26 * @author michal.polkorab
28 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
30 private static final Logger LOG = LoggerFactory
31 .getLogger(SwitchConnectionProviderImpl.class);
32 private SwitchConnectionHandler switchConnectionHandler;
33 private Set<ServerFacade> serverLot;
36 public void configure(Collection<ConnectionConfiguration> connConfigs) {
37 LOG.debug("Configurating ..");
39 //TODO - configure servers according to configuration
40 serverLot = new HashSet<>();
41 for (Iterator<ConnectionConfiguration> iterator = connConfigs.iterator(); iterator.hasNext();) {
42 ConnectionConfiguration connConfig = iterator.next();
43 serverLot.add(new TcpHandler(connConfig.getAddress(), connConfig.getPort()));
48 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
49 LOG.debug("setSwitchConnectionHanler");
50 this.switchConnectionHandler = switchConnectionHandler;
54 public Future<List<Boolean>> shutdown() {
55 LOG.debug("Shutdown summoned");
56 ListenableFuture<List<Boolean>> result = SettableFuture.create();
58 List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
59 for (ServerFacade server : serverLot) {
60 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
61 shutdownChain.add(shutdownFuture);
63 if (!shutdownChain.isEmpty()) {
64 result = Futures.allAsList(shutdownChain);
66 throw new IllegalStateException("No servers configured");
68 } catch (Exception e) {
69 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
70 exFuture.setException(e);
77 public Future<List<Boolean>> startup() {
78 LOG.debug("startup summoned");
79 ListenableFuture<List<Boolean>> result = SettableFuture.create();
81 if (serverLot.isEmpty()) {
82 throw new IllegalStateException("No servers configured");
84 for (ServerFacade server : serverLot) {
85 if (server.getIsOnlineFuture().isDone()) {
86 throw new IllegalStateException("Servers already running");
89 if (switchConnectionHandler == null) {
90 throw new IllegalStateException("switchConnectionHandler is not set");
92 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
93 for (ServerFacade server : serverLot) {
94 new Thread(server).start();
95 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
96 starterChain.add(isOnlineFuture);
98 if (!starterChain.isEmpty()) {
99 result = Futures.allAsList(starterChain);
101 throw new IllegalStateException("No servers configured");
103 } catch (Exception e) {
104 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
105 exFuture.setException(e);