Merge "Use ClusteredDataTreeListener in hwvtepsb"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / ConfigActivator.java
1 /*
2  * Copyright (c) 2015 Red Hat, 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.ovsdb.openstack.netvirt;
10
11 import java.util.ArrayList;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14 import java.util.List;
15
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.EgressAclProvider;
24 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
25 import org.opendaylight.ovsdb.openstack.netvirt.api.GatewayMacResolver;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.GatewayMacResolverListener;
27 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
28 import org.opendaylight.ovsdb.openstack.netvirt.api.IngressAclProvider;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerProvider;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.MultiTenantAwareRouter;
32 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
33 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
34 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
35 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
36 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbInventoryListener;
37 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbInventoryService;
38 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
39 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityGroupCacheManger;
40 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
42 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
43 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
44 import org.opendaylight.ovsdb.openstack.netvirt.impl.BridgeConfigurationManagerImpl;
45 import org.opendaylight.ovsdb.openstack.netvirt.impl.ConfigurationServiceImpl;
46 import org.opendaylight.ovsdb.openstack.netvirt.impl.EventDispatcherImpl;
47 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
48 import org.opendaylight.ovsdb.openstack.netvirt.impl.NodeCacheManagerImpl;
49 import org.opendaylight.ovsdb.openstack.netvirt.impl.OpenstackRouter;
50 import org.opendaylight.ovsdb.openstack.netvirt.impl.OvsdbInventoryServiceImpl;
51 import org.opendaylight.ovsdb.openstack.netvirt.impl.ProviderNetworkManagerImpl;
52 import org.opendaylight.ovsdb.openstack.netvirt.impl.SecurityGroupCacheManagerImpl;
53 import org.opendaylight.ovsdb.openstack.netvirt.impl.SecurityServicesImpl;
54 import org.opendaylight.ovsdb.openstack.netvirt.impl.SouthboundImpl;
55 import org.opendaylight.ovsdb.openstack.netvirt.impl.TenantNetworkManagerImpl;
56 import org.opendaylight.ovsdb.openstack.netvirt.impl.VlanConfigurationCacheImpl;
57 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronFloatingIPCRUD;
58 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronLoadBalancerCRUD;
59 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronLoadBalancerPoolCRUD;
60 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
61 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
62 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
63 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronFirewallInterface;
64 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronFirewallPolicyInterface;
65 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronFirewallRuleInterface;
66 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronFloatingIPInterface;
67 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronLoadBalancerHealthMonitorInterface;
68 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronLoadBalancerInterface;
69 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronLoadBalancerListenerInterface;
70 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronLoadBalancerPoolInterface;
71 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronLoadBalancerPoolMemberInterface;
72 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronNetworkInterface;
73 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronPortInterface;
74 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronRouterInterface;
75 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronSecurityGroupInterface;
76 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronSecurityRuleInterface;
77 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl.NeutronSubnetInterface;
78 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronFirewallAware;
79 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronFirewallPolicyAware;
80 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronFirewallRuleAware;
81 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronFloatingIPAware;
82 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronLoadBalancerAware;
83 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronLoadBalancerPoolAware;
84 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronLoadBalancerPoolMemberAware;
85 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronNetworkAware;
86 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronPortAware;
87 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronRouterAware;
88 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronSecurityGroupAware;
89 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronSecurityRuleAware;
90 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronSubnetAware;
91 import org.opendaylight.ovsdb.utils.neutron.utils.NeutronModelsDataStoreHelper;
92 import org.osgi.framework.BundleActivator;
93 import org.osgi.framework.BundleContext;
94 import org.osgi.framework.ServiceReference;
95 import org.osgi.framework.ServiceRegistration;
96 import org.osgi.util.tracker.ServiceTracker;
97 import org.slf4j.Logger;
98 import org.slf4j.LoggerFactory;
99
100 public class ConfigActivator implements BundleActivator {
101     private static final Logger LOG = LoggerFactory.getLogger(ConfigActivator.class);
102     private List<ServiceRegistration<?>> translatorCRUDRegistrations = new ArrayList<>();
103     private List<Pair<Object, ServiceRegistration>> servicesAndRegistrations = new ArrayList<>();
104     private ProviderContext providerContext;
105     private boolean conntrackEnabled = false;
106
107     public ConfigActivator(ProviderContext providerContext) {
108         this.providerContext = providerContext;
109     }
110
111     @Override
112     public void start(BundleContext context) throws Exception {
113         LOG.info("ConfigActivator start:");
114         registerCRUDServiceProviders(context, this.providerContext);
115
116         ConfigurationServiceImpl configurationService = new ConfigurationServiceImpl();
117         registerService(context, new String[] {ConfigurationService.class.getName()},
118                 null, configurationService);
119
120         BridgeConfigurationManagerImpl bridgeConfigurationManager = new BridgeConfigurationManagerImpl();
121         registerService(context, new String[] {BridgeConfigurationManager.class.getName()},
122                 null, bridgeConfigurationManager);
123
124         final TenantNetworkManagerImpl tenantNetworkManager = new TenantNetworkManagerImpl();
125         registerService(context, new String[] {TenantNetworkManager.class.getName()},
126                 null, tenantNetworkManager);
127
128         VlanConfigurationCacheImpl vlanConfigurationCache = new VlanConfigurationCacheImpl();
129         registerService(context, new String[] {VlanConfigurationCache.class.getName()},
130                 null, vlanConfigurationCache);
131
132         FloatingIPHandler floatingIPHandler = new FloatingIPHandler();
133         registerAbstractHandlerService(context, new Class[] {INeutronFloatingIPAware.class},
134                 AbstractEvent.HandlerType.NEUTRON_FLOATING_IP, floatingIPHandler);
135
136         final NetworkHandler networkHandler = new NetworkHandler();
137         registerAbstractHandlerService(context, new Class[] {INeutronNetworkAware.class},
138                 AbstractEvent.HandlerType.NEUTRON_NETWORK, networkHandler);
139
140         SubnetHandler subnetHandler = new SubnetHandler();
141         registerAbstractHandlerService(context, new Class[] {INeutronSubnetAware.class},
142                 AbstractEvent.HandlerType.NEUTRON_SUBNET, subnetHandler);
143
144         PortHandler portHandler = new PortHandler();
145         registerAbstractHandlerService(context, new Class[] {INeutronPortAware.class},
146                 AbstractEvent.HandlerType.NEUTRON_PORT, portHandler);
147
148         RouterHandler routerHandler = new RouterHandler();
149         registerAbstractHandlerService(context, new Class[] {INeutronRouterAware.class},
150                 AbstractEvent.HandlerType.NEUTRON_ROUTER, routerHandler);
151
152         SouthboundHandler southboundHandler = new SouthboundHandler();
153         registerAbstractHandlerService(context, new Class[] {OvsdbInventoryListener.class, NodeCacheListener.class},
154                 AbstractEvent.HandlerType.SOUTHBOUND, southboundHandler);
155
156         final LBaaSHandler lBaaSHandler = new LBaaSHandler();
157         registerAbstractHandlerService(context, new Class[] {INeutronLoadBalancerAware.class, NodeCacheListener.class},
158                 AbstractEvent.HandlerType.NEUTRON_LOAD_BALANCER, lBaaSHandler);
159
160         final LBaaSPoolHandler lBaaSPoolHandler = new LBaaSPoolHandler();
161         registerAbstractHandlerService(context, new Class[] {INeutronLoadBalancerPoolAware.class},
162                 AbstractEvent.HandlerType.NEUTRON_LOAD_BALANCER_POOL, lBaaSPoolHandler);
163
164         final LBaaSPoolMemberHandler lBaaSPoolMemberHandler = new LBaaSPoolMemberHandler();
165         registerAbstractHandlerService(context, new Class[] {INeutronLoadBalancerPoolMemberAware.class},
166                 AbstractEvent.HandlerType.NEUTRON_LOAD_BALANCER_POOL_MEMBER, lBaaSPoolMemberHandler);
167
168         PortSecurityHandler portSecurityHandler = new PortSecurityHandler();
169         registerAbstractHandlerService(context,
170                 new Class[] {INeutronSecurityRuleAware.class, INeutronSecurityGroupAware.class},
171                 AbstractEvent.HandlerType.NEUTRON_PORT_SECURITY, portSecurityHandler);
172
173         final SecurityServicesImpl securityServices = new SecurityServicesImpl(conntrackEnabled);
174         registerService(context,
175                 new String[]{SecurityServicesManager.class.getName()}, null, securityServices);
176
177         final SecurityGroupCacheManger securityGroupCacheManger = new SecurityGroupCacheManagerImpl();
178         registerService(context,
179                         new String[]{SecurityGroupCacheManger.class.getName()}, null, securityGroupCacheManger);
180
181         registerService(context,
182                 new String[]{SecurityServicesManager.class.getName()}, null, securityServices);
183
184         FWaasHandler fWaasHandler = new FWaasHandler();
185         registerAbstractHandlerService(context,
186                 new Class[] {INeutronFirewallAware.class, INeutronFirewallRuleAware.class, INeutronFirewallPolicyAware.class},
187                 AbstractEvent.HandlerType.NEUTRON_FWAAS, fWaasHandler);
188
189         ProviderNetworkManagerImpl providerNetworkManager = new ProviderNetworkManagerImpl();
190         registerService(context,
191                 new String[]{NetworkingProviderManager.class.getName()}, null, providerNetworkManager);
192
193         EventDispatcherImpl eventDispatcher = new EventDispatcherImpl();
194         registerService(context,
195                 new String[]{EventDispatcher.class.getName()}, null, eventDispatcher);
196
197         Dictionary<String, Object> neutronL3AdapterProperties = new Hashtable<>();
198         neutronL3AdapterProperties.put(Constants.EVENT_HANDLER_TYPE_PROPERTY,
199                 AbstractEvent.HandlerType.NEUTRON_L3_ADAPTER);
200         final NeutronL3Adapter neutronL3Adapter = new NeutronL3Adapter(
201                 new NeutronModelsDataStoreHelper(this.providerContext.getSALService(DataBroker.class)));
202         registerService(context,
203                 new String[]{NeutronL3Adapter.class.getName(), GatewayMacResolverListener.class.getName()},
204                 neutronL3AdapterProperties, neutronL3Adapter);
205
206         OpenstackRouter openstackRouter = new OpenstackRouter();
207         registerService(context,
208                 new String[]{MultiTenantAwareRouter.class.getName()}, null, openstackRouter);
209
210         Southbound southbound = new SouthboundImpl(providerContext.getSALService(DataBroker.class));
211         registerService(context,
212                 new String[]{Southbound.class.getName()}, null, southbound);
213
214         NodeCacheManagerImpl nodeCacheManager = new NodeCacheManagerImpl();
215         registerAbstractHandlerService(context, new Class[] {NodeCacheManager.class},
216                 AbstractEvent.HandlerType.NODE, nodeCacheManager);
217
218         OvsdbInventoryServiceImpl ovsdbInventoryService = new OvsdbInventoryServiceImpl(providerContext);
219         registerService(context,
220                 new String[] {OvsdbInventoryService.class.getName()}, null, ovsdbInventoryService);
221
222         // Call .setDependencies() starting with the last service registered
223         for (int i = servicesAndRegistrations.size() - 1; i >= 0; i--) {
224             Pair<Object, ServiceRegistration> serviceAndRegistration = servicesAndRegistrations.get(i);
225             Object service = serviceAndRegistration.getLeft();
226             ServiceRegistration<?> serviceRegistration = serviceAndRegistration.getRight();
227             LOG.info("Setting dependencies on service {}/{}, {}", i, servicesAndRegistrations.size(),
228                     service.getClass());
229             if (service instanceof ConfigInterface) {
230                 ((ConfigInterface) service).setDependencies(
231                         serviceRegistration != null ? serviceRegistration.getReference() : null);
232                 LOG.info("Dependencies set");
233             } else {
234                 LOG.warn("Service isn't a ConfigInterface");
235             }
236         }
237
238         // TODO check if services are already available and setDependencies
239         // addingService may not be called if the service is already available when the ServiceTracker
240         // is started
241         trackService(context, INeutronNetworkCRUD.class, tenantNetworkManager, networkHandler, lBaaSHandler,
242                 lBaaSPoolHandler, lBaaSPoolMemberHandler, neutronL3Adapter);
243         trackService(context, INeutronSubnetCRUD.class, lBaaSHandler, lBaaSPoolHandler, lBaaSPoolMemberHandler,
244                 securityServices, neutronL3Adapter);
245         trackService(context, INeutronPortCRUD.class, tenantNetworkManager, lBaaSHandler, lBaaSPoolHandler,
246                 lBaaSPoolMemberHandler, securityServices, neutronL3Adapter);
247         trackService(context, INeutronFloatingIPCRUD.class, neutronL3Adapter);
248         trackService(context, INeutronLoadBalancerCRUD.class, lBaaSHandler, lBaaSPoolHandler, lBaaSPoolMemberHandler);
249         trackService(context, INeutronLoadBalancerPoolCRUD.class, lBaaSHandler, lBaaSPoolMemberHandler);
250         trackService(context, LoadBalancerProvider.class, lBaaSHandler, lBaaSPoolHandler, lBaaSPoolMemberHandler);
251         trackService(context, ArpProvider.class, neutronL3Adapter);
252         trackService(context, InboundNatProvider.class, neutronL3Adapter);
253         trackService(context, OutboundNatProvider.class, neutronL3Adapter);
254         trackService(context, RoutingProvider.class, neutronL3Adapter);
255         trackService(context, L3ForwardingProvider.class, neutronL3Adapter);
256         trackService(context, GatewayMacResolver.class, neutronL3Adapter);
257         trackService(context, IngressAclProvider.class, securityServices);
258         trackService(context, EgressAclProvider.class, securityServices);
259
260         // We no longer need to track the services, avoid keeping references around
261         servicesAndRegistrations.clear();
262     }
263
264     private void registerCRUDServiceProviders(BundleContext context,
265             ProviderContext providerContext) {
266         LOG.debug("Registering CRUD service providers");
267         NeutronRouterInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
268         NeutronPortInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
269         NeutronSubnetInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
270         NeutronNetworkInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
271         NeutronSecurityGroupInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
272         NeutronSecurityRuleInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
273         NeutronFirewallInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
274         NeutronFirewallPolicyInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
275         NeutronFirewallRuleInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
276         NeutronLoadBalancerInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
277         NeutronLoadBalancerPoolInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
278         NeutronLoadBalancerListenerInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
279         NeutronLoadBalancerHealthMonitorInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
280         NeutronLoadBalancerPoolMemberInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
281         NeutronFloatingIPInterface.registerNewInterface(context, providerContext, translatorCRUDRegistrations);
282     }
283
284     private void trackService(BundleContext context, final Class<?> clazz, final ConfigInterface... dependents) {
285         @SuppressWarnings("unchecked")
286         ServiceTracker tracker = new ServiceTracker(context, clazz, null) {
287             @Override
288             public Object addingService(ServiceReference reference) {
289                 LOG.info("addingService " + clazz.getName());
290                 Object service = context.getService(reference);
291                 if (service != null) {
292                     for (ConfigInterface dependent : dependents) {
293                         dependent.setDependencies(service);
294                     }
295                 }
296                 return service;
297             }
298         };
299         tracker.open();
300     }
301
302     private void registerAbstractHandlerService(BundleContext context, Class[] interfaces,
303                                                 AbstractEvent.HandlerType handlerType, AbstractHandler handler) {
304         Dictionary<String, Object> properties = new Hashtable<>();
305         properties.put(Constants.EVENT_HANDLER_TYPE_PROPERTY, handlerType);
306         String[] interfaceNames = new String[interfaces.length + 1];
307         for (int i = 0; i < interfaces.length; i++) {
308             interfaceNames[i] = interfaces[i].getName();
309         }
310         interfaceNames[interfaces.length] = AbstractHandler.class.getName();
311         registerService(context, interfaceNames, properties, handler);
312     }
313
314
315     @Override
316     public void stop(BundleContext context) throws Exception {
317         LOG.info("ConfigActivator stop");
318         // ServiceTrackers and services are already released when bundle stops,
319         // so we don't need to close the trackers or unregister the services
320     }
321
322     private ServiceRegistration<?> registerService(BundleContext bundleContext, String[] interfaces,
323                                                    Dictionary<String, Object> properties, Object impl) {
324         ServiceRegistration serviceRegistration = bundleContext.registerService(interfaces, impl, properties);
325         if (serviceRegistration == null) {
326             LOG.warn("Service registration for {} failed to return a ServiceRegistration instance", impl.getClass());
327         }
328         servicesAndRegistrations.add(Pair.of(impl, serviceRegistration));
329         return serviceRegistration;
330     }
331
332     public void setConntrackEnabled(boolean conntrackEnabled) {
333         this.conntrackEnabled = conntrackEnabled;
334     }
335 }