Improved Facade & integration test
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / SwitchConnectionProviderImpl.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2
3 package org.opendaylight.openflowjava.protocol.impl.connection;
4
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Set;
11 import java.util.concurrent.Future;
12
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;
19
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.SettableFuture;
23
24 /**
25  * @author mirehak
26  * @author michal.polkorab
27  */
28 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
29     
30     private static final Logger LOG = LoggerFactory
31             .getLogger(SwitchConnectionProviderImpl.class);
32     private SwitchConnectionHandler switchConnectionHandler;
33     private Set<ServerFacade> serverLot;
34
35     @Override
36     public void configure(Collection<ConnectionConfiguration> connConfigs) {
37         LOG.debug("Configurating ..");
38
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             TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
44             server.setSwitchConnectionHandler(switchConnectionHandler);
45             serverLot.add(server);
46         }
47     }
48
49     @Override
50     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
51         LOG.debug("setSwitchConnectionHanler");
52         this.switchConnectionHandler = switchConnectionHandler;
53     }
54
55     @Override
56     public Future<List<Boolean>> shutdown() {
57         LOG.debug("Shutdown summoned");
58         ListenableFuture<List<Boolean>> result = SettableFuture.create();
59         try {
60             List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
61             for (ServerFacade server : serverLot) {
62                 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
63                 shutdownChain.add(shutdownFuture);
64             }
65             if (!shutdownChain.isEmpty()) {
66                 result = Futures.allAsList(shutdownChain);
67             } else {
68                 throw new IllegalStateException("No servers configured");
69             }
70         } catch (Exception e) {
71             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
72             exFuture.setException(e);
73             result = exFuture;
74         }
75         return result;
76     }
77
78     @Override
79     public Future<List<Boolean>> startup() {
80         LOG.debug("startup summoned");
81         ListenableFuture<List<Boolean>> result = SettableFuture.create();
82         try {
83             if (serverLot.isEmpty()) {
84                 throw new IllegalStateException("No servers configured");
85             }
86             for (ServerFacade server : serverLot) {
87                 if (server.getIsOnlineFuture().isDone()) {
88                     throw new IllegalStateException("Servers already running");
89                 }
90             }
91             if (switchConnectionHandler == null) {
92                 throw new IllegalStateException("switchConnectionHandler is not set");
93             }
94             List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
95             for (ServerFacade server : serverLot) {
96                 new Thread(server).start();
97                 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
98                 starterChain.add(isOnlineFuture);
99             }
100             if (!starterChain.isEmpty()) {
101                 result = Futures.allAsList(starterChain);
102             } else {
103                 throw new IllegalStateException("No servers configured");
104             }
105         } catch (Exception e) {
106             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
107             exFuture.setException(e);
108             result = exFuture;
109         }
110         return result;
111     }
112
113 }