Added support for switch idle state
[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.List;
9 import java.util.Set;
10 import java.util.concurrent.Future;
11
12 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
13 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
14 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
15 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
22
23 /**
24  * @author mirehak
25  * @author michal.polkorab
26  */
27 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
28     
29     private static final Logger LOGGER = LoggerFactory
30             .getLogger(SwitchConnectionProviderImpl.class);
31     private SwitchConnectionHandler switchConnectionHandler;
32     private Set<ServerFacade> serverLot;
33
34     @Override
35     public void configure(Collection<ConnectionConfiguration> connConfigs) {
36         LOGGER.debug("Configuring ..");
37
38         //TODO - configure servers according to configuration
39         serverLot = new HashSet<>();
40         for (ConnectionConfiguration connConfig : connConfigs) {
41             TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
42             server.setSwitchConnectionHandler(switchConnectionHandler);
43             server.setSwitchIdleTimeout(connConfig.getSwitchIdleTimeout());
44             serverLot.add(server);
45         }
46     }
47
48     @Override
49     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
50         LOGGER.debug("setSwitchConnectionHandler");
51         this.switchConnectionHandler = switchConnectionHandler;
52     }
53
54     @Override
55     public Future<List<Boolean>> shutdown() {
56         LOGGER.debug("Shutdown summoned");
57         ListenableFuture<List<Boolean>> result = SettableFuture.create();
58         try {
59             List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
60             for (ServerFacade server : serverLot) {
61                 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
62                 shutdownChain.add(shutdownFuture);
63             }
64             if (!shutdownChain.isEmpty()) {
65                 result = Futures.allAsList(shutdownChain);
66             } else {
67                 throw new IllegalStateException("No servers configured");
68             }
69         } catch (Exception e) {
70             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
71             exFuture.setException(e);
72             result = exFuture;
73         }
74         return result;
75     }
76
77     @Override
78     public Future<List<Boolean>> startup() {
79         LOGGER.debug("startup summoned");
80         ListenableFuture<List<Boolean>> result = SettableFuture.create();
81         try {
82             if (serverLot.isEmpty()) {
83                 throw new IllegalStateException("No servers configured");
84             }
85             for (ServerFacade server : serverLot) {
86                 if (server.getIsOnlineFuture().isDone()) {
87                     throw new IllegalStateException("Servers already running");
88                 }
89             }
90             if (switchConnectionHandler == null) {
91                 throw new IllegalStateException("switchConnectionHandler is not set");
92             }
93             List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
94             for (ServerFacade server : serverLot) {
95                 new Thread(server).start();
96                 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
97                 starterChain.add(isOnlineFuture);
98             }
99             if (!starterChain.isEmpty()) {
100                 result = Futures.allAsList(starterChain);
101             } else {
102                 throw new IllegalStateException("No servers configured");
103             }
104         } catch (Exception e) {
105             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
106             exFuture.setException(e);
107             result = exFuture;
108         }
109         return result;
110     }
111
112 }