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