Merge "Rpc manager not needed to instantiate DeviceManager"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImpl.java
1 /*
2  * Copyright (c) 2015 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.openflowplugin.impl;
10
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
23 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
26 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
28 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
29 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
30 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
31 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
32 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.api.types.rev150327.OfpRole;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Created by Martin Bobak <mbobak@cisco.com> on 27.3.2015.
39  */
40 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider {
41
42     protected static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
43
44     private DeviceManager deviceManager;
45     private RpcManager rpcManager;
46     private StatisticsManager statisticsManager;
47     private ConnectionManager connectionManager;
48     private BindingAwareBroker bindingAwareBroker;
49     private ProviderContext providerContext;
50     private OfpRole role;
51     private Collection<SwitchConnectionProvider> switchConnectionProviders;
52
53     @Override
54     public void onSessionInitiated(final ProviderContext providerContextArg) {
55         providerContext = providerContextArg;
56
57         connectionManager = new ConnectionManagerImpl();
58         deviceManager = new DeviceManagerImpl(providerContext.getSALService(DataBroker.class));
59         statisticsManager = new StatisticsManagerImpl();
60         rpcManager = new RpcManagerImpl(providerContext);
61
62         connectionManager.setDeviceConnectedHandler(deviceManager);
63         deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
64         statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
65         rpcManager.setDeviceInitializationPhaseHandler(deviceManager);
66
67         TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
68         startSwitchConnections();
69     }
70
71     private void startSwitchConnections() {
72         final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
73         for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
74             switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
75             final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
76             starterChain.add(isOnlineFuture);
77         }
78
79         final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
80         Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
81             @Override
82             public void onSuccess(final List<Boolean> result) {
83                 LOG.info("All switchConnectionProviders are up and running ({}).",
84                         result.size());
85             }
86
87             @Override
88             public void onFailure(final Throwable t) {
89                 LOG.warn("Some switchConnectionProviders failed to start.", t);
90             }
91         });
92     }
93
94     @Override
95     public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
96         this.switchConnectionProviders = switchConnectionProviders;
97     }
98
99     @Override
100     public void setRole(final OfpRole role) {
101         this.role = role;
102     }
103
104     @Override
105     public void setBindingAwareBroker(final BindingAwareBroker bindingAwareBroker) {
106         this.bindingAwareBroker = bindingAwareBroker;
107
108     }
109
110     @Override
111     public void initialize() {
112         Preconditions.checkNotNull(bindingAwareBroker, "missing bindingAwareBroker");
113         bindingAwareBroker.registerProvider(this);
114     }
115
116     @Override
117     public void close() throws Exception {
118         //TODO: close all contexts, switchConnections (, managers)
119     }
120 }