2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.connection;
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;
17 import java.util.concurrent.Future;
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;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import com.google.common.util.concurrent.SettableFuture;
34 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
36 private static final Logger LOG = LoggerFactory
37 .getLogger(SwitchConnectionProviderImpl.class);
38 private SwitchConnectionHandler switchConnectionHandler;
39 private Set<ServerFacade> serverLot;
42 public void configure(Collection<ConnectionConfiguration> connConfigs) {
43 LOG.debug("configurating ..");
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()));
54 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
55 LOG.debug("setSwitchConnectionHanler");
56 this.switchConnectionHandler = switchConnectionHandler;
60 public Future<Boolean> shutdown() {
61 LOG.debug("shutdown summoned");
62 for (ServerFacade server : serverLot) {
69 public Future<List<Boolean>> startup() {
70 LOG.debug("startup summoned");
71 ListenableFuture<List<Boolean>> result = SettableFuture.create();
73 //TODO - check config, status of servers
74 if (switchConnectionHandler == null) {
75 throw new IllegalStateException("switchConnectionHandler is not set");
79 List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
80 for (ServerFacade server : serverLot) {
81 new Thread(server).start();
82 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
83 starterChain.add(isOnlineFuture);
86 if (!starterChain.isEmpty()) {
87 result = Futures.allAsList(starterChain);
89 throw new IllegalStateException("no servers configured");
91 } catch (Exception e) {
92 SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
93 exFuture.setException(e);