b3a89a4ff9bb3d90d99cb50278985398f466a566
[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         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         super.start(context);
93         this.context = Preconditions.checkNotNull(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                 flow,
104                 inventory,
105                 dataPacket,
106                 nodeFactory,
107                 nodeConnectorFactory,
108                 topology,
109                 tpProvider,
110         };
111     }
112
113     @Override
114     protected void configureGlobalInstance(final Component c, final Object imp) {
115         if (imp instanceof DataPacketAdapter) {
116             _configure((DataPacketAdapter)imp, c);
117         } else if (imp instanceof FlowProgrammerAdapter) {
118             _configure((FlowProgrammerAdapter)imp, c);
119         } else if (imp instanceof InventoryAndReadAdapter) {
120             _configure((InventoryAndReadAdapter)imp, c);
121         } else if (imp instanceof ComponentActivator) {
122             _configure((ComponentActivator)imp, c);
123         } else if (imp instanceof MDSalNodeConnectorFactory) {
124             _configure((MDSalNodeConnectorFactory)imp, c);
125         } else if (imp instanceof MDSalNodeFactory) {
126             _configure((MDSalNodeFactory)imp, c);
127         } else if (imp instanceof TopologyAdapter) {
128             _configure((TopologyAdapter)imp, c);
129         } else if (imp instanceof TopologyProvider) {
130             _configure((TopologyProvider)imp, c);
131         } else {
132             throw new IllegalArgumentException(String.format("Unhandled implementation class %s", imp.getClass()));
133         }
134     }
135
136     @Override
137     protected Object[] getImplementations() {
138         return new Object[] {
139                 dataPacketService,
140         };
141     }
142
143     @Override
144     protected void configureInstance(final Component c, final Object imp, final String containerName) {
145         if (imp instanceof ComponentActivator) {
146             _instanceConfigure((ComponentActivator)imp, c, containerName);
147         } else if (imp instanceof DataPacketServiceAdapter) {
148             _instanceConfigure((DataPacketServiceAdapter)imp, c, containerName);
149         } else {
150             throw new IllegalArgumentException(String.format("Unhandled implementation class %s", imp.getClass()));
151         }
152     }
153
154     private void _configure(final MDSalNodeFactory imp, final Component it) {
155         it.setInterface(INodeFactory.class.getName(), properties());
156     }
157
158     private void _configure(final MDSalNodeConnectorFactory imp, final Component it) {
159         it.setInterface(INodeConnectorFactory.class.getName(), properties());
160     }
161
162     private void _configure(final ComponentActivator imp, final Component it) {
163         it.add(createServiceDependency()
164                 .setService(BindingAwareBroker.class)
165                 .setCallbacks("setBroker", "setBroker")
166                 .setRequired(true));
167     }
168
169     private void _configure(final DataPacketAdapter imp, final Component it) {
170         it.add(createServiceDependency()
171                 .setService(IPluginOutDataPacketService.class)
172                 .setCallbacks("setDataPacketPublisher", "setDataPacketPublisher")
173                 .setRequired(false));
174     }
175
176     private void _configure(final FlowProgrammerAdapter imp, final Component it) {
177         it.setInterface(IPluginInFlowProgrammerService.class.getName(), properties());
178         it.add(createServiceDependency()
179                 .setService(IPluginOutFlowProgrammerService.class)
180                 .setCallbacks("setFlowProgrammerPublisher", "setFlowProgrammerPublisher")
181                 .setRequired(false));
182         it.add(createServiceDependency()
183                 .setService(IClusterGlobalServices.class)
184                 .setCallbacks("setClusterGlobalServices", "unsetClusterGlobalServices")
185                 .setRequired(false));
186     }
187
188     private void _instanceConfigure(final DataPacketServiceAdapter imp, final Component it, final String containerName) {
189         it.setInterface(IPluginInDataPacketService.class.getName(), properties());
190     }
191
192     private void _instanceConfigure(final ComponentActivator imp, final Component it, final String containerName) {
193         // No-op
194     }
195
196     private void _configure(final InventoryAndReadAdapter imp, final Component it) {
197         it.setInterface(new String[] {
198                 IPluginInInventoryService.class.getName(),
199                 IPluginInReadService.class.getName(),
200         }, properties());
201
202         it.add(createServiceDependency()
203                 .setService(IPluginOutReadService.class)
204                 .setCallbacks("setReadPublisher", "unsetReadPublisher")
205                 .setRequired(false));
206         it.add(createServiceDependency()
207                 .setService(IPluginOutInventoryService.class)
208                 .setCallbacks("setInventoryPublisher", "unsetInventoryPublisher")
209                 .setRequired(false));
210         it.add(createServiceDependency()
211                 .setService(IDiscoveryService.class)
212                 .setCallbacks("setDiscoveryPublisher", "setDiscoveryPublisher")
213                 .setRequired(false));
214     }
215
216     private void _configure(final TopologyAdapter imp, final Component it) {
217         it.setInterface(IPluginInTopologyService.class.getName(), properties());
218
219         it.add(createServiceDependency()
220                 .setService(IPluginOutTopologyService.class)
221                 .setCallbacks("setTopologyPublisher", "setTopologyPublisher")
222                 .setRequired(false));
223     }
224
225     private void _configure(final TopologyProvider imp, final Component it) {
226         it.add(createServiceDependency()
227                 .setService(IPluginOutTopologyService.class)
228                 .setCallbacks("setTopologyPublisher", "setTopologyPublisher")
229                 .setRequired(false));
230     }
231
232     private Dictionary<String,Object> properties() {
233         final Hashtable<String,Object> props = new Hashtable<String, Object>();
234         props.put(GlobalConstants.PROTOCOLPLUGINTYPE.toString(), NodeMapping.MD_SAL_TYPE);
235         props.put("protocolName", NodeMapping.MD_SAL_TYPE);
236         return props;
237     }
238 }