Added Yang code generation for Multipart messages
[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             serverLot.add(new TcpHandler(connConfig.getAddress(), connConfig.getPort()));
44         }
45     }
46
47     @Override
48     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
49         LOG.debug("setSwitchConnectionHanler");
50         this.switchConnectionHandler = switchConnectionHandler;
51     }
52
53     @Override
54     public Future<List<Boolean>> shutdown() {
55         LOG.debug("Shutdown summoned");
56         ListenableFuture<List<Boolean>> result = SettableFuture.create();
57         try {
58             List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>();
59             for (ServerFacade server : serverLot) {
60                 ListenableFuture<Boolean> shutdownFuture = server.shutdown();
61                 shutdownChain.add(shutdownFuture);
62             }
63             if (!shutdownChain.isEmpty()) {
64                 result = Futures.allAsList(shutdownChain);
65             } else {
66                 throw new IllegalStateException("No servers configured");
67             }
68         } catch (Exception e) {
69             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
70             exFuture.setException(e);
71             result = exFuture;
72         }
73         return result;
74     }
75
76     @Override
77     public Future<List<Boolean>> startup() {
78         LOG.debug("startup summoned");
79         ListenableFuture<List<Boolean>> result = SettableFuture.create();
80         try {
81             if (serverLot.isEmpty()) {
82                 throw new IllegalStateException("No servers configured");
83             }
84             for (ServerFacade server : serverLot) {
85                 if (server.getIsOnlineFuture().isDone()) {
86                     throw new IllegalStateException("Servers already running");
87                 }
88             }
89             if (switchConnectionHandler == null) {
90                 throw new IllegalStateException("switchConnectionHandler is not set");
91             }
92             List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
93             for (ServerFacade server : serverLot) {
94                 new Thread(server).start();
95                 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
96                 starterChain.add(isOnlineFuture);
97             }
98             if (!starterChain.isEmpty()) {
99                 result = Futures.allAsList(starterChain);
100             } else {
101                 throw new IllegalStateException("No servers configured");
102             }
103         } catch (Exception e) {
104             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
105             exFuture.setException(e);
106             result = exFuture;
107         }
108         return result;
109     }
110
111 }