DeviceFlowRegistry holds FlowDescriptor instead of FlowId
[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     private Long rpcRequestsQuota;
53
54     public OpenFlowPluginProviderImpl(final Long rpcRequestsQuota) {
55         this.rpcRequestsQuota = rpcRequestsQuota;
56     }
57
58     @Override
59     public void onSessionInitiated(final ProviderContext providerContextArg) {
60         providerContext = providerContextArg;
61
62         connectionManager = new ConnectionManagerImpl();
63         deviceManager = new DeviceManagerImpl(providerContext.getSALService(DataBroker.class));
64         statisticsManager = new StatisticsManagerImpl();
65         rpcManager = new RpcManagerImpl(providerContext, rpcRequestsQuota);
66
67         connectionManager.setDeviceConnectedHandler(deviceManager);
68         deviceManager.setDeviceInitializationPhaseHandler(statisticsManager);
69         statisticsManager.setDeviceInitializationPhaseHandler(rpcManager);
70         rpcManager.setDeviceInitializationPhaseHandler(deviceManager);
71
72         TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
73         startSwitchConnections();
74     }
75
76     private void startSwitchConnections() {
77         final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
78         for (final SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
79             switchConnectionPrv.setSwitchConnectionHandler(connectionManager);
80             final ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
81             starterChain.add(isOnlineFuture);
82         }
83
84         final ListenableFuture<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
85         Futures.addCallback(srvStarted, new FutureCallback<List<Boolean>>() {
86             @Override
87             public void onSuccess(final List<Boolean> result) {
88                 LOG.info("All switchConnectionProviders are up and running ({}).",
89                         result.size());
90             }
91
92             @Override
93             public void onFailure(final Throwable t) {
94                 LOG.warn("Some switchConnectionProviders failed to start.", t);
95             }
96         });
97     }
98
99     @Override
100     public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
101         this.switchConnectionProviders = switchConnectionProviders;
102     }
103
104     @Override
105     public void setRole(final OfpRole role) {
106         this.role = role;
107     }
108
109
110     @Override
111     public void setBindingAwareBroker(final BindingAwareBroker bindingAwareBroker) {
112         this.bindingAwareBroker = bindingAwareBroker;
113
114     }
115
116     @Override
117     public void initialize() {
118         Preconditions.checkNotNull(bindingAwareBroker, "missing bindingAwareBroker");
119         bindingAwareBroker.registerProvider(this);
120     }
121
122     @Override
123     public void close() throws Exception {
124         //TODO: close all contexts, switchConnections (, managers)
125     }
126 }