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.concurrent.Future;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
15 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
16 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
17 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
18 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.SettableFuture;
26 * Exposed class for server handling
28 * @author michal.polkorab
30 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
32 private static final Logger LOGGER = LoggerFactory
33 .getLogger(SwitchConnectionProviderImpl.class);
34 private SwitchConnectionHandler switchConnectionHandler;
35 private ServerFacade serverFacade;
36 private ConnectionConfiguration connConfig;
39 public void setConfiguration(ConnectionConfiguration connConfig) {
40 this.connConfig = connConfig;
44 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
45 LOGGER.debug("setSwitchConnectionHandler");
46 this.switchConnectionHandler = switchConnectionHandler;
50 public ListenableFuture<Boolean> shutdown() {
51 LOGGER.debug("Shutdown summoned");
52 //TODO: provide exception in case of: not started, not configured (already stopped)
53 ListenableFuture<Boolean> result = serverFacade.shutdown();
58 public ListenableFuture<Boolean> startup() {
59 LOGGER.debug("Startup summoned");
60 serverFacade = createAndConfigureServer();
62 LOGGER.debug("Starting ..");
63 ListenableFuture<Boolean> result = null;
65 if (serverFacade == null) {
66 throw new IllegalStateException("No server configured");
68 if (serverFacade.getIsOnlineFuture().isDone()) {
69 throw new IllegalStateException("Server already running");
71 if (switchConnectionHandler == null) {
72 throw new IllegalStateException("switchConnectionHandler is not set");
74 new Thread(serverFacade).start();
75 result = serverFacade.getIsOnlineFuture();
76 } catch (Exception e) {
77 SettableFuture<Boolean> exResult = SettableFuture.create();
78 exResult.setException(e);
87 private TcpHandler createAndConfigureServer() {
88 LOGGER.debug("Configuring ..");
89 TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
90 server.setSwitchConnectionHandler(switchConnectionHandler);
91 server.setSwitchIdleTimeout(connConfig.getSwitchIdleTimeout());
92 boolean tlsSupported = FEATURE_SUPPORT.REQUIRED.equals(connConfig.getTlsSupport());
93 server.setEncryption(tlsSupported);
101 public ServerFacade getServerFacade() {
106 public void close() throws Exception {