Copyright update
[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.ArrayList;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.concurrent.Future;
18
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;
26
27 import com.google.common.util.concurrent.Futures;
28 import com.google.common.util.concurrent.ListenableFuture;
29 import com.google.common.util.concurrent.SettableFuture;
30
31 /**
32  * Exposed class for server handling
33  * @author mirehak
34  * @author michal.polkorab
35  */
36 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
37     
38     private static final Logger LOGGER = LoggerFactory
39             .getLogger(SwitchConnectionProviderImpl.class);
40     private SwitchConnectionHandler switchConnectionHandler;
41     private Set<ServerFacade> serverLot;
42
43     @Override
44     public void configure(Collection<ConnectionConfiguration> connConfigs) {
45         LOGGER.debug("Configuring ..");
46
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);
56         }
57     }
58
59     @Override
60     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
61         LOGGER.debug("setSwitchConnectionHandler");
62         this.switchConnectionHandler = switchConnectionHandler;
63     }
64
65     @Override
66     public Future<List<Boolean>> shutdown() {
67         LOGGER.debug("Shutdown summoned");
68         ListenableFuture<List<Boolean>> result = SettableFuture.create();
69         try {
70             List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
71             for (ServerFacade server : serverLot) {
72                 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
73                 shutdownChain.add(shutdownFuture);
74             }
75             if (!shutdownChain.isEmpty()) {
76                 result = Futures.allAsList(shutdownChain);
77             } else {
78                 throw new IllegalStateException("No servers configured");
79             }
80         } catch (Exception e) {
81             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
82             exFuture.setException(e);
83             result = exFuture;
84         }
85         return result;
86     }
87
88     @Override
89     public Future<List<Boolean>> startup() {
90         LOGGER.debug("startup summoned");
91         ListenableFuture<List<Boolean>> result = SettableFuture.create();
92         try {
93             if (serverLot.isEmpty()) {
94                 throw new IllegalStateException("No servers configured");
95             }
96             for (ServerFacade server : serverLot) {
97                 if (server.getIsOnlineFuture().isDone()) {
98                     throw new IllegalStateException("Servers already running");
99                 }
100             }
101             if (switchConnectionHandler == null) {
102                 throw new IllegalStateException("switchConnectionHandler is not set");
103             }
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);
109             }
110             if (!starterChain.isEmpty()) {
111                 result = Futures.allAsList(starterChain);
112             } else {
113                 throw new IllegalStateException("No servers configured");
114             }
115         } catch (Exception e) {
116             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
117             exFuture.setException(e);
118             result = exFuture;
119         }
120         return result;
121     }
122
123     /**
124      * @return servers
125      */
126     public Set<ServerFacade> getServerLot() {
127         return serverLot;
128     }
129
130 }