add lib-plugin interaction implementations on library side
[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.List;
15 import java.util.Set;
16 import java.util.concurrent.Future;
17
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
19 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
20 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
21 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.SettableFuture;
28
29 /**
30  * @author mirehak
31  *
32  */
33 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
34     
35     private static final Logger LOG = LoggerFactory
36             .getLogger(SwitchConnectionProviderImpl.class);
37     private SwitchConnectionHandler switchConnectionHandler;
38     private Set<ServerFacade> serverLot;
39
40     @Override
41     public void configure(Collection<ConnectionConfiguration> connConfigs) {
42         LOG.debug("configurating ..");
43
44         //TODO - add and configure servers according to configuration
45         serverLot = new HashSet<>();
46         serverLot.add(new TcpHandler(6633));
47     }
48
49     @Override
50     public void setSwitchConnectionListener(SwitchConnectionHandler switchConnectionHandler) {
51         this.switchConnectionHandler = switchConnectionHandler;
52     }
53
54     @Override
55     public Future<Boolean> shutdown() {
56         LOG.debug("shutdown summoned");
57         for (ServerFacade server : serverLot) {
58             server.shutdown();
59         }
60         return null;
61     }
62
63     @Override
64     public Future<List<Boolean>> startup() {
65         LOG.debug("startup summoned");
66         ListenableFuture<List<Boolean>> result = SettableFuture.create();
67         try {
68             //TODO - check config, status of servers
69             if (switchConnectionHandler == null) {
70                 throw new IllegalStateException("switchConnectionHandler is not set");
71             }
72             
73             // starting
74             List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
75             for (ServerFacade server : serverLot) {
76                 new Thread(server).start();
77                 ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
78                 starterChain.add(isOnlineFuture);
79             }
80             
81             if (!starterChain.isEmpty()) {
82                 result = Futures.allAsList(starterChain);
83             } else {
84                 throw new IllegalStateException("no servers configured");
85             }
86         } catch (Exception e) {
87             SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
88             exFuture.setException(e);
89             result = exFuture;
90         }
91         return result;
92     }
93
94 }