2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
10 package org.opendaylight.openflowjava.protocol.impl.connection;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.concurrent.Future;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
20 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
21 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
22 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
23 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.util.concurrent.Futures;
28 import com.google.common.util.concurrent.ListenableFuture;
29 import com.google.common.util.concurrent.SettableFuture;
32 * Exposed class for server handling
34 * @author michal.polkorab
36 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
38 private static final Logger LOGGER = LoggerFactory
39 .getLogger(SwitchConnectionProviderImpl.class);
40 private SwitchConnectionHandler switchConnectionHandler;
41 private Set<ServerFacade> serverLot;
44 public void configure(Collection<ConnectionConfiguration> connConfigs) {
45 LOGGER.debug("Configuring ..");
47 //TODO - configure servers according to configuration
48 serverLot = new HashSet<>();
49 for (ConnectionConfiguration connConfig : connConfigs) {
50 TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
51 server.setSwitchConnectionHandler(switchConnectionHandler);
52 server.setSwitchIdleTimeout(connConfig.getSwitchIdleTimeout());
53 boolean tlsSupported = FEATURE_SUPPORT.REQUIRED.equals(connConfig.getTlsSupport());
54 server.setEncryption(tlsSupported);
55 serverLot.add(server);
60 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
61 LOGGER.debug("setSwitchConnectionHandler");
62 this.switchConnectionHandler = switchConnectionHandler;
66 public Future<List<Boolean>> shutdown() {
67 LOGGER.debug("Shutdown summoned");
68 ListenableFuture<List<Boolean>> result = SettableFuture.create();
70 List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
71 for (ServerFacade server : serverLot) {
72 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
73 shutdownChain.add(shutdownFuture);
75 if (!shutdownChain.isEmpty()) {
76 result = Futures.allAsList(shutdownChain);
78 throw new IllegalStateException("No servers configured");
80 } catch (Exception e) {
81 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
82 exFuture.setException(e);
89 public Future<List<Boolean>> startup() {
90 LOGGER.debug("startup summoned");
91 ListenableFuture<List<Boolean>> result = SettableFuture.create();
93 if (serverLot.isEmpty()) {
94 throw new IllegalStateException("No servers configured");
96 for (ServerFacade server : serverLot) {
97 if (server.getIsOnlineFuture().isDone()) {
98 throw new IllegalStateException("Servers already running");
101 if (switchConnectionHandler == null) {
102 throw new IllegalStateException("switchConnectionHandler is not set");
104 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
105 for (ServerFacade server : serverLot) {
106 new Thread(server).start();
107 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
108 starterChain.add(isOnlineFuture);
110 if (!starterChain.isEmpty()) {
111 result = Futures.allAsList(starterChain);
113 throw new IllegalStateException("No servers configured");
115 } catch (Exception e) {
116 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
117 exFuture.setException(e);
126 public Set<ServerFacade> getServerLot() {