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