6a8e3c0e5bc62ee437ce2661f755d288e4caab55
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / ComponentActivator.java
1 /**
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.compatibility;
9
10 import com.google.common.base.Preconditions;
11 import org.apache.felix.dm.Component;
12 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.compatibility.adsal.DataPacketServiceAdapter;
16 import org.opendaylight.controller.sal.compatibility.topology.TopologyAdapter;
17 import org.opendaylight.controller.sal.compatibility.topology.TopologyProvider;
18 import org.opendaylight.controller.sal.core.ComponentActivatorAbstractBase;
19 import org.opendaylight.controller.sal.core.Node.NodeIDType;
20 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
21 import org.opendaylight.controller.sal.discovery.IDiscoveryService;
22 import org.opendaylight.controller.sal.flowprogrammer.IPluginInFlowProgrammerService;
23 import org.opendaylight.controller.sal.flowprogrammer.IPluginOutFlowProgrammerService;
24 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
25 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
26 import org.opendaylight.controller.sal.packet.IPluginInDataPacketService;
27 import org.opendaylight.controller.sal.packet.IPluginOutDataPacketService;
28 import org.opendaylight.controller.sal.reader.IPluginInReadService;
29 import org.opendaylight.controller.sal.reader.IPluginOutReadService;
30 import org.opendaylight.controller.sal.topology.IPluginInTopologyService;
31 import org.opendaylight.controller.sal.topology.IPluginOutTopologyService;
32 import org.opendaylight.controller.sal.utils.GlobalConstants;
33 import org.opendaylight.controller.sal.utils.INodeConnectorFactory;
34 import org.opendaylight.controller.sal.utils.INodeFactory;
35 import org.osgi.framework.BundleContext;
36
37 import java.util.Dictionary;
38 import java.util.Hashtable;
39
40 public class ComponentActivator extends ComponentActivatorAbstractBase {
41     private final INodeConnectorFactory nodeConnectorFactory = new MDSalNodeConnectorFactory();
42     private final DataPacketServiceAdapter dataPacketService = new DataPacketServiceAdapter();
43     private final InventoryAndReadAdapter inventory = new InventoryAndReadAdapter();
44     private final FlowProgrammerAdapter flow = new FlowProgrammerAdapter();
45     private final DataPacketAdapter dataPacket = new DataPacketAdapter();
46     private final TopologyProvider tpProvider = new TopologyProvider();
47     private final INodeFactory nodeFactory = new MDSalNodeFactory();
48     private final TopologyAdapter topology = new TopologyAdapter();
49     private BundleContext context;
50
51     public INodeConnectorFactory getNodeConnectorFactory() {
52         return nodeConnectorFactory;
53     }
54
55     public DataPacketServiceAdapter getDataPacketService() {
56         return dataPacketService;
57     }
58
59     public InventoryAndReadAdapter getInventory() {
60         return inventory;
61     }
62
63     public FlowProgrammerAdapter getFlow() {
64         return flow;
65     }
66
67     public DataPacketAdapter getDataPacket() {
68         return dataPacket;
69     }
70
71     public TopologyProvider getTpProvider() {
72         return tpProvider;
73     }
74
75     public INodeFactory getNodeFactory() {
76         return nodeFactory;
77     }
78
79     public TopologyAdapter getTopology() {
80         return topology;
81     }
82
83     @Override
84     protected void init() {
85         // TODO: deprecated, should be removed soon
86         NodeIDType.registerIDType(NodeMapping.MD_SAL_TYPE, String.class);
87         NodeConnectorIDType.registerIDType(NodeMapping.MD_SAL_TYPE, String.class, NodeMapping.MD_SAL_TYPE);
88     }
89
90     @Override
91     public void start(final BundleContext context) {
92         this.context = Preconditions.checkNotNull(context);
93         super.start(context);
94     }
95
96     public ProviderContext setBroker(final BindingAwareBroker broker) {
97         return broker.registerProvider(new SalCompatibilityProvider(this), context);
98     }
99
100     @Override
101     protected Object[] getGlobalImplementations() {
102         return new Object[] {
103                 this, // Used for setBroker callback
104                 flow,
105                 inventory,
106                 dataPacket,
107                 nodeFactory,
108                 nodeConnectorFactory,
109                 topology,
110                 tpProvider
111         };
112     }
113
114     @Override
115     protected void configureGlobalInstance(final Component c, final Object imp) {
116         if (imp instanceof DataPacketAdapter) {
117             _configure((DataPacketAdapter)imp, c);
118         } else if (imp instanceof FlowProgrammerAdapter) {
119             _configure((FlowProgrammerAdapter)imp, c);
120         } else if (imp instanceof InventoryAndReadAdapter) {
121             _configure((InventoryAndReadAdapter)imp, c);
122         } else if (imp instanceof ComponentActivator) {
123             _configure((ComponentActivator)imp, c);
124         } else if (imp instanceof MDSalNodeConnectorFactory) {
125             _configure((MDSalNodeConnectorFactory)imp, c);
126         } else if (imp instanceof MDSalNodeFactory) {
127             _configure((MDSalNodeFactory)imp, c);
128         } else if (imp instanceof TopologyAdapter) {
129             _configure((TopologyAdapter)imp, c);
130         } else if (imp instanceof TopologyProvider) {
131             _configure((TopologyProvider)imp, c);
132         } else {
133             throw new IllegalArgumentException(String.format("Unhandled implementation class %s", imp.getClass()));
134         }
135     }
136
137     @Override
138     protected Object[] getImplementations() {
139         return new Object[] {
140                 dataPacketService,
141                 inventory,
142         };
143     }
144
145     @Override
146     protected void configureInstance(final Component c, final Object imp, final String containerName) {
147         if (imp instanceof ComponentActivator) {
148             _instanceConfigure((ComponentActivator)imp, c, containerName);
149         } else if (imp instanceof DataPacketServiceAdapter) {
150             _instanceConfigure((DataPacketServiceAdapter)imp, c, containerName);
151         } else if (imp instanceof InventoryAndReadAdapter) {
152             _instanceConfigure((InventoryAndReadAdapter)imp, c, containerName);
153         } else {
154             throw new IllegalArgumentException(String.format("Unhandled implementation class %s", imp.getClass()));
155         }
156     }
157
158     private void _configure(final MDSalNodeFactory imp, final Component it) {
159         it.setInterface(INodeFactory.class.getName(), properties());
160     }
161
162     private void _configure(final MDSalNodeConnectorFactory imp, final Component it) {
163         it.setInterface(INodeConnectorFactory.class.getName(), properties());
164     }
165
166     private void _configure(final ComponentActivator imp, final Component it) {
167         it.add(createServiceDependency()
168                 .setService(BindingAwareBroker.class)
169                 .setCallbacks("setBroker", "setBroker")
170                 .setRequired(true));
171     }
172
173     private void _configure(final DataPacketAdapter imp, final Component it) {
174         it.add(createServiceDependency()
175                 .setService(IPluginOutDataPacketService.class)
176                 .setCallbacks("setDataPacketPublisher", "setDataPacketPublisher")
177                 .setRequired(false));
178     }
179
180     private void _configure(final FlowProgrammerAdapter imp, final Component it) {
181         it.setInterface(IPluginInFlowProgrammerService.class.getName(), properties());
182         it.add(createServiceDependency()
183                 .setService(IPluginOutFlowProgrammerService.class)
184                 .setCallbacks("setFlowProgrammerPublisher", "setFlowProgrammerPublisher")
185                 .setRequired(false));
186         it.add(createServiceDependency()
187                 .setService(IClusterGlobalServices.class)
188                 .setCallbacks("setClusterGlobalServices", "unsetClusterGlobalServices")
189                 .setRequired(false));
190     }
191
192     private void _instanceConfigure(final DataPacketServiceAdapter imp, final Component it, final String containerName) {
193         it.setInterface(IPluginInDataPacketService.class.getName(), properties());
194     }
195
196     private void _instanceConfigure(final ComponentActivator imp, final Component it, final String containerName) {
197         // No-op
198     }
199
200     private void _configure(final InventoryAndReadAdapter imp, final Component it) {
201         it.setInterface(new String[] {
202                 IPluginInInventoryService.class.getName(),
203                 IPluginInReadService.class.getName(),
204         }, properties());
205
206         it.add(createServiceDependency()
207                 .setService(IPluginOutReadService.class)
208                 .setCallbacks("setReadPublisher", "unsetReadPublisher")
209                 .setRequired(false));
210         it.add(createServiceDependency()
211                 .setService(IPluginOutInventoryService.class)
212                 .setCallbacks("setInventoryPublisher", "unsetInventoryPublisher")
213                 .setRequired(false));
214         it.add(createServiceDependency()
215                 .setService(IDiscoveryService.class)
216                 .setCallbacks("setDiscoveryPublisher", "setDiscoveryPublisher")
217                 .setRequired(false));
218         it.add(createServiceDependency()
219                 .setService(BindingAwareBroker.class)
220                 .setRequired(true));
221     }
222
223     private void _instanceConfigure(final InventoryAndReadAdapter imp, final Component it, String containerName) {
224         it.setInterface(new String[] {
225                 IPluginInInventoryService.class.getName(),
226                 IPluginInReadService.class.getName(),
227         }, properties());
228
229         it.add(createServiceDependency()
230                 .setService(IPluginOutReadService.class)
231                 .setCallbacks("setReadPublisher", "unsetReadPublisher")
232                 .setRequired(false));
233         it.add(createServiceDependency()
234                 .setService(IPluginOutInventoryService.class)
235                 .setCallbacks("setInventoryPublisher", "unsetInventoryPublisher")
236                 .setRequired(false));
237         it.add(createServiceDependency()
238                 .setService(BindingAwareBroker.class)
239                 .setRequired(true));
240     }
241
242     private void _configure(final TopologyAdapter imp, final Component it) {
243         it.setInterface(IPluginInTopologyService.class.getName(), properties());
244
245         it.add(createServiceDependency()
246                 .setService(IPluginOutTopologyService.class)
247                 .setCallbacks("setTopologyPublisher", "setTopologyPublisher")
248                 .setRequired(false));
249     }
250
251     private void _configure(final TopologyProvider imp, final Component it) {
252         it.add(createServiceDependency()
253                 .setService(IPluginOutTopologyService.class)
254                 .setCallbacks("setTopologyPublisher", "setTopologyPublisher")
255                 .setRequired(false));
256     }
257
258     private Dictionary<String,Object> properties() {
259         final Hashtable<String,Object> props = new Hashtable<String, Object>();
260         props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), NodeIDType.OPENFLOW);
261         props.put("protocolName", NodeIDType.OPENFLOW);
262         return props;
263     }
264 }