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