ec26c3f230787b35ac6bfcf7240a273a7f0997dc
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / internal / Activator.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.openflowplugin.openflow.internal;
10
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15
16 import org.apache.felix.dm.Component;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider.ProviderFunctionality;
22 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
23 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
24 import org.opendaylight.controller.sal.core.ComponentActivatorAbstractBase;
25 import org.opendaylight.controller.sal.core.IContainerListener;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.flowprogrammer.IPluginInFlowProgrammerService;
28 import org.opendaylight.controller.sal.flowprogrammer.IPluginOutFlowProgrammerService;
29 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
30 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
31 import org.opendaylight.controller.sal.packet.IPluginInDataPacketService;
32 import org.opendaylight.controller.sal.packet.IPluginOutDataPacketService;
33 import org.opendaylight.controller.sal.reader.IPluginInReadService;
34 import org.opendaylight.controller.sal.reader.IPluginOutReadService;
35 import org.opendaylight.controller.sal.topology.IPluginInTopologyService;
36 import org.opendaylight.controller.sal.topology.IPluginOutTopologyService;
37 import org.opendaylight.controller.sal.utils.GlobalConstants;
38 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
39 import org.opendaylight.openflowplugin.openflow.IDataPacketListen;
40 import org.opendaylight.openflowplugin.openflow.IDataPacketMux;
41 import org.opendaylight.openflowplugin.openflow.IDiscoveryListener;
42 import org.opendaylight.openflowplugin.openflow.IFlowProgrammerNotifier;
43 import org.opendaylight.openflowplugin.openflow.IInventoryProvider;
44 import org.opendaylight.openflowplugin.openflow.IInventoryShimExternalListener;
45 import org.opendaylight.openflowplugin.openflow.IInventoryShimInternalListener;
46 import org.opendaylight.openflowplugin.openflow.IOFStatisticsListener;
47 import org.opendaylight.openflowplugin.openflow.IOFStatisticsManager;
48 import org.opendaylight.openflowplugin.openflow.IReadFilterInternalListener;
49 import org.opendaylight.openflowplugin.openflow.IReadServiceFilter;
50 import org.opendaylight.openflowplugin.openflow.IRefreshInternalProvider;
51 import org.opendaylight.openflowplugin.openflow.ITopologyServiceShimListener;
52 import org.opendaylight.openflowplugin.openflow.core.IController;
53 import org.opendaylight.openflowplugin.openflow.core.IMessageListener;
54 import org.opendaylight.openflowplugin.openflow.core.internal.Controller;
55 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
56 import org.opendaylight.openflowplugin.openflow.md.core.sal.OpenflowPluginProvider;
57 import org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager;
58 import org.opendaylight.yangtools.yang.binding.RpcService;
59 import org.osgi.framework.BundleContext;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  * Openflow protocol plugin Activator
65  */
66 public class Activator extends ComponentActivatorAbstractBase {
67     protected static final Logger logger = LoggerFactory.getLogger(Activator.class);
68
69     private OpenflowPluginProvider pluginProvider = new OpenflowPluginProvider();
70
71     /**
72      * Function called when the activator starts just after some initializations
73      * are done by the ComponentActivatorAbstractBase.
74      *
75      */
76     public void init() {
77     }
78
79     /**
80      * Function called when the activator stops just before the cleanup done by
81      * ComponentActivatorAbstractBase
82      *
83      */
84     public void destroy() {
85     }
86
87     @Override
88     public void start(BundleContext arg0) {
89         super.start(arg0);
90         pluginProvider.setContext(arg0);
91     }
92
93     /**
94      * Function that is used to communicate to dependency manager the list of
95      * known implementations for services inside a container
96      *
97      *
98      * @return An array containing all the CLASS objects that will be
99      *         instantiated in order to get an fully working implementation
100      *         Object
101      */
102     public Object[] getImplementations() {
103         Object[] res = { TopologyServices.class, DataPacketServices.class, InventoryService.class, ReadService.class,
104                 FlowProgrammerNotifier.class };
105         return res;
106     }
107
108     /**
109      * Function that is called when configuration of the dependencies is
110      * required.
111      *
112      * @param c
113      *            dependency manager Component object, used for configuring the
114      *            dependencies exported and imported
115      * @param imp
116      *            Implementation class that is being configured, needed as long
117      *            as the same routine can configure multiple implementations
118      * @param containerName
119      *            The containerName being configured, this allow also optional
120      *            per-container different behavior if needed, usually should not
121      *            be the case though.
122      */
123     public void configureInstance(Component c, Object imp, String containerName) {
124         if (imp.equals(TopologyServices.class)) {
125             // export the service to be used by SAL
126             c.setInterface(
127                     new String[] { IPluginInTopologyService.class.getName(),
128                             ITopologyServiceShimListener.class.getName() }, null);
129             // Hook the services coming in from SAL, as optional in
130             // case SAL is not yet there, could happen
131             c.add(createContainerServiceDependency(containerName).setService(IPluginOutTopologyService.class)
132                     .setCallbacks("setPluginOutTopologyService", "unsetPluginOutTopologyService").setRequired(false));
133             c.add(createServiceDependency().setService(IRefreshInternalProvider.class)
134                     .setCallbacks("setRefreshInternalProvider", "unsetRefreshInternalProvider").setRequired(false));
135         }
136
137         if (imp.equals(InventoryService.class)) {
138             // export the service
139             c.setInterface(new String[] { IPluginInInventoryService.class.getName(),
140                     IInventoryShimInternalListener.class.getName(), IInventoryProvider.class.getName() }, null);
141
142             // Now lets add a service dependency to make sure the
143             // provider of service exists
144             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
145                     .setCallbacks("setController", "unsetController").setRequired(true));
146             c.add(createContainerServiceDependency(containerName).setService(IPluginOutInventoryService.class)
147                     .setCallbacks("setPluginOutInventoryServices", "unsetPluginOutInventoryServices")
148                     .setRequired(false));
149         }
150
151         if (imp.equals(DataPacketServices.class)) {
152             // export the service to be used by SAL
153             Dictionary<String, Object> props = new Hashtable<String, Object>();
154             // Set the protocolPluginType property which will be used
155             // by SAL
156             props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), Node.NodeIDType.OPENFLOW);
157             c.setInterface(IPluginInDataPacketService.class.getName(), props);
158             // Hook the services coming in from SAL, as optional in
159             // case SAL is not yet there, could happen
160             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
161                     .setCallbacks("setController", "unsetController").setRequired(true));
162             // This is required for the transmission to happen properly
163             c.add(createServiceDependency().setService(IDataPacketMux.class)
164                     .setCallbacks("setIDataPacketMux", "unsetIDataPacketMux").setRequired(true));
165             c.add(createContainerServiceDependency(containerName).setService(IPluginOutDataPacketService.class)
166                     .setCallbacks("setPluginOutDataPacketService", "unsetPluginOutDataPacketService")
167                     .setRequired(false));
168             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
169                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
170                     .setRequired(false));
171         }
172
173         if (imp.equals(ReadService.class)) {
174             // export the service to be used by SAL
175             Dictionary<String, Object> props = new Hashtable<String, Object>();
176             // Set the protocolPluginType property which will be used
177             // by SAL
178             props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), Node.NodeIDType.OPENFLOW);
179             c.setInterface(
180                     new String[] { IReadFilterInternalListener.class.getName(), IPluginInReadService.class.getName() },
181                     props);
182
183             c.add(createServiceDependency().setService(IReadServiceFilter.class)
184                     .setCallbacks("setService", "unsetService").setRequired(true));
185
186             c.add(createContainerServiceDependency(containerName).setService(IPluginOutReadService.class)
187                     .setCallbacks("setPluginOutReadServices", "unsetPluginOutReadServices").setRequired(false));
188
189             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
190                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
191                     .setRequired(false));
192         }
193
194         if (imp.equals(FlowProgrammerNotifier.class)) {
195             // export the service to be used by SAL
196             Dictionary<String, Object> props = new Hashtable<String, Object>();
197             // Set the protocolPluginType property which will be used
198             // by SAL
199             props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), Node.NodeIDType.OPENFLOW);
200             c.setInterface(IFlowProgrammerNotifier.class.getName(), props);
201
202             c.add(createContainerServiceDependency(containerName).setService(IPluginOutFlowProgrammerService.class)
203                     .setCallbacks("setPluginOutFlowProgrammerService", "unsetPluginOutFlowProgrammerService")
204                     .setRequired(true));
205             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
206                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
207                     .setRequired(false));
208         }
209     }
210
211     /**
212      * Function that is used to communicate to dependency manager the list of
213      * known implementations for services that are container independent.
214      *
215      *
216      * @return An array containing all the CLASS objects that will be
217      *         instantiated in order to get an fully working implementation
218      *         Object
219      */
220     public Object[] getGlobalImplementations() {
221         Object[] res = { Controller.class, OFStatisticsManager.class, FlowProgrammerService.class,
222                 ReadServiceFilter.class, DiscoveryService.class, DataPacketMuxDemux.class, InventoryService.class,
223                 InventoryServiceShim.class, TopologyServiceShim.class, MDController.class, pluginProvider };
224         return res;
225     }
226
227     /**
228      * Function that is called when configuration of the dependencies is
229      * required.
230      *
231      * @param c
232      *            dependency manager Component object, used for configuring the
233      *            dependencies exported and imported
234      * @param imp
235      *            Implementation class that is being configured, needed as long
236      *            as the same routine can configure multiple implementations
237      */
238     public void configureGlobalInstance(Component c, Object imp) {
239
240         if (imp.equals(Controller.class)) {
241             logger.debug("Activator configureGlobalInstance( ) is called");
242             Dictionary<String, Object> props = new Hashtable<String, Object>();
243             props.put("name", "Controller");
244             props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), Node.NodeIDType.OPENFLOW);
245             c.setInterface(new String[] { IController.class.getName(), IPluginInConnectionService.class.getName() },
246                     props);
247         } else
248
249         if (imp.equals(FlowProgrammerService.class)) {
250             // export the service to be used by SAL
251             Dictionary<String, Object> props = new Hashtable<String, Object>();
252             // Set the protocolPluginType property which will be used
253             // by SAL
254             props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), Node.NodeIDType.OPENFLOW);
255             c.setInterface(
256                     new String[] { IPluginInFlowProgrammerService.class.getName(), IMessageListener.class.getName(),
257                             IContainerListener.class.getName(), IInventoryShimExternalListener.class.getName() }, props);
258
259             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
260                     .setCallbacks("setController", "unsetController").setRequired(true));
261
262             c.add(createServiceDependency().setService(IFlowProgrammerNotifier.class)
263                     .setCallbacks("setFlowProgrammerNotifier", "unsetsetFlowProgrammerNotifier").setRequired(false));
264
265             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
266                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
267                     .setRequired(false));
268         } else
269
270         if (imp.equals(ReadServiceFilter.class)) {
271
272             c.setInterface(new String[] { IReadServiceFilter.class.getName(), IContainerListener.class.getName(),
273                     IOFStatisticsListener.class.getName() }, null);
274
275             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
276                     .setCallbacks("setController", "unsetController").setRequired(true));
277             c.add(createServiceDependency().setService(IOFStatisticsManager.class)
278                     .setCallbacks("setService", "unsetService").setRequired(true));
279             c.add(createServiceDependency().setService(IReadFilterInternalListener.class)
280                     .setCallbacks("setReadFilterInternalListener", "unsetReadFilterInternalListener")
281                     .setRequired(false));
282         } else
283
284         if (imp.equals(OFStatisticsManager.class)) {
285
286             c.setInterface(
287                     new String[] { IOFStatisticsManager.class.getName(), IInventoryShimExternalListener.class.getName() },
288                     null);
289
290             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
291                     .setCallbacks("setController", "unsetController").setRequired(true));
292             c.add(createServiceDependency().setService(IOFStatisticsListener.class)
293                     .setCallbacks("setStatisticsListener", "unsetStatisticsListener").setRequired(false));
294         } else
295
296         if (imp.equals(DiscoveryService.class)) {
297             // export the service
298             c.setInterface(
299                     new String[] { IInventoryShimExternalListener.class.getName(), IDataPacketListen.class.getName(),
300                             IContainerListener.class.getName() }, null);
301
302             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
303                     .setCallbacks("setController", "unsetController").setRequired(true));
304             c.add(createContainerServiceDependency(GlobalConstants.DEFAULT.toString())
305                     .setService(IInventoryProvider.class)
306                     .setCallbacks("setInventoryProvider", "unsetInventoryProvider").setRequired(true));
307             c.add(createServiceDependency().setService(IDataPacketMux.class)
308                     .setCallbacks("setIDataPacketMux", "unsetIDataPacketMux").setRequired(true));
309             c.add(createServiceDependency().setService(IDiscoveryListener.class)
310                     .setCallbacks("setDiscoveryListener", "unsetDiscoveryListener").setRequired(true));
311             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
312                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
313                     .setRequired(false));
314         } else
315
316         // DataPacket mux/demux services, which is teh actual engine
317         // doing the packet switching
318         if (imp.equals(DataPacketMuxDemux.class)) {
319             c.setInterface(new String[] { IDataPacketMux.class.getName(), IContainerListener.class.getName(),
320                     IInventoryShimExternalListener.class.getName() }, null);
321
322             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
323                     .setCallbacks("setController", "unsetController").setRequired(true));
324             c.add(createServiceDependency().setService(IPluginOutDataPacketService.class)
325                     .setCallbacks("setPluginOutDataPacketService", "unsetPluginOutDataPacketService")
326                     .setRequired(false));
327             // See if there is any local packet dispatcher
328             c.add(createServiceDependency().setService(IDataPacketListen.class)
329                     .setCallbacks("setIDataPacketListen", "unsetIDataPacketListen").setRequired(false));
330             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
331                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
332                     .setRequired(false));
333         } else
334
335         if (imp.equals(InventoryService.class)) {
336             // export the service
337             Dictionary<String, Object> props = new Hashtable<String, Object>();
338             props.put("scope", "Global");
339
340             c.setInterface(new String[] { IPluginInInventoryService.class.getName(),
341                     IInventoryShimInternalListener.class.getName(), IInventoryProvider.class.getName() }, props);
342
343             // Now lets add a service dependency to make sure the
344             // provider of service exists
345             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
346                     .setCallbacks("setController", "unsetController").setRequired(true));
347             c.add(createServiceDependency().setService(IPluginOutInventoryService.class, "(scope=Global)")
348                     .setCallbacks("setPluginOutInventoryServices", "unsetPluginOutInventoryServices")
349                     .setRequired(false));
350         } else
351
352         if (imp.equals(InventoryServiceShim.class)) {
353             c.setInterface(new String[] { IContainerListener.class.getName(), IOFStatisticsListener.class.getName() },
354                     null);
355
356             c.add(createServiceDependency().setService(IController.class, "(name=Controller)")
357                     .setCallbacks("setController", "unsetController").setRequired(true));
358             c.add(createServiceDependency().setService(IInventoryShimInternalListener.class, "(!(scope=Global))")
359                     .setCallbacks("setInventoryShimInternalListener", "unsetInventoryShimInternalListener")
360                     .setRequired(true));
361             c.add(createServiceDependency().setService(IInventoryShimInternalListener.class, "(scope=Global)")
362                     .setCallbacks("setInventoryShimGlobalInternalListener", "unsetInventoryShimGlobalInternalListener")
363                     .setRequired(true));
364             c.add(createServiceDependency().setService(IInventoryShimExternalListener.class)
365                     .setCallbacks("setInventoryShimExternalListener", "unsetInventoryShimExternalListener")
366                     .setRequired(false));
367             c.add(createServiceDependency().setService(IPluginOutConnectionService.class)
368                     .setCallbacks("setIPluginOutConnectionService", "unsetIPluginOutConnectionService")
369                     .setRequired(false));
370         } else
371
372         if (imp.equals(TopologyServiceShim.class)) {
373             c.setInterface(new String[] { IDiscoveryListener.class.getName(), IContainerListener.class.getName(),
374                     IRefreshInternalProvider.class.getName(), IInventoryShimExternalListener.class.getName() }, null);
375             c.add(createServiceDependency().setService(ITopologyServiceShimListener.class)
376                     .setCallbacks("setTopologyServiceShimListener", "unsetTopologyServiceShimListener")
377                     .setRequired(true));
378             c.add(createServiceDependency().setService(IOFStatisticsManager.class)
379                     .setCallbacks("setStatisticsManager", "unsetStatisticsManager").setRequired(false));
380         } else
381
382          if (imp == pluginProvider) {
383             // c.setInterface(new String[] { IDiscoveryListener.class.getName(),
384             // IContainerListener.class.getName(),
385             // IRefreshInternalProvider.class.getName(),
386             // IInventoryShimExternalListener.class.getName() }, null);
387             c.add(createServiceDependency().setService(BindingAwareBroker.class)
388                     .setCallbacks("setBroker", "unsetBroker").setRequired(true));
389             c.add(createServiceDependency().setService(SwitchConnectionProvider.class)
390                     .setCallbacks("setSwitchConnectionProvider", "unsetSwitchConnectionProvider").setRequired(true));
391             logger.debug("configuring Binding Aware Provider");
392         }
393     }
394
395 }