changes for config subsystem - BUG 754
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / SwitchConnectionProviderImpl.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9
10 package org.opendaylight.openflowjava.protocol.impl.connection;
11
12 import java.util.concurrent.Future;
13
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;
21
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.SettableFuture;
24
25 /**
26  * Exposed class for server handling
27  * @author mirehak
28  * @author michal.polkorab
29  */
30 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
31     
32     private static final Logger LOGGER = LoggerFactory
33             .getLogger(SwitchConnectionProviderImpl.class);
34     private SwitchConnectionHandler switchConnectionHandler;
35     private ServerFacade serverFacade;
36     private ConnectionConfiguration connConfig;
37
38     @Override
39     public void setConfiguration(ConnectionConfiguration connConfig) {
40         this.connConfig = connConfig;
41     }
42
43     @Override
44     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
45         LOGGER.debug("setSwitchConnectionHandler");
46         this.switchConnectionHandler = switchConnectionHandler;
47     }
48
49     @Override
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();
54         return result;
55     }
56
57     @Override
58     public ListenableFuture<Boolean> startup() {
59         LOGGER.debug("Startup summoned");
60         serverFacade = createAndConfigureServer();
61         
62         LOGGER.debug("Starting ..");
63         ListenableFuture<Boolean> result = null;
64         try {
65             if (serverFacade == null) {
66                 throw new IllegalStateException("No server configured");
67             }
68             if (serverFacade.getIsOnlineFuture().isDone()) {
69                 throw new IllegalStateException("Server already running");
70             }
71             if (switchConnectionHandler == null) {
72                 throw new IllegalStateException("switchConnectionHandler is not set");
73             }
74             new Thread(serverFacade).start();
75             result = serverFacade.getIsOnlineFuture();
76         } catch (Exception e) {
77             SettableFuture<Boolean> exResult = SettableFuture.create();
78             exResult.setException(e);
79             result = exResult;
80         }
81         return result;
82     }
83
84     /**
85      * @return
86      */
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);
94         
95         return server;
96     }
97
98     /**
99      * @return servers
100      */
101     public ServerFacade getServerFacade() {
102         return serverFacade;
103     }
104
105     @Override
106     public void close() throws Exception {
107         shutdown();
108     }
109 }