Add UT for SouthboundMapper and SouthboundProvider
[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 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.neutron.spi.*;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.*;
19 import org.opendaylight.ovsdb.openstack.netvirt.impl.*;
20 import org.osgi.framework.BundleActivator;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
23 import org.osgi.framework.ServiceRegistration;
24 import org.osgi.util.tracker.ServiceTracker;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class ConfigActivator implements BundleActivator {
29     private static final Logger LOG = LoggerFactory.getLogger(ConfigActivator.class);
30     private List<ServiceRegistration<?>> registrations = new ArrayList<>();
31     private List<Object> services = new ArrayList<>();
32     private ProviderContext providerContext;
33
34     public ConfigActivator(ProviderContext providerContext) {
35         this.providerContext = providerContext;
36     }
37
38     @Override
39     public void start(BundleContext context) throws Exception {
40         LOG.info("ConfigActivator start:");
41
42         ConfigurationServiceImpl configurationService = new ConfigurationServiceImpl();
43         registerService(context, new String[] {ConfigurationService.class.getName()},
44                 null, configurationService);
45
46         BridgeConfigurationManagerImpl bridgeConfigurationManager = new BridgeConfigurationManagerImpl();
47         registerService(context, new String[] {BridgeConfigurationManager.class.getName()},
48                 null, bridgeConfigurationManager);
49
50         final TenantNetworkManagerImpl tenantNetworkManager = new TenantNetworkManagerImpl();
51         registerService(context, new String[] {TenantNetworkManager.class.getName()},
52                 null, tenantNetworkManager);
53
54         VlanConfigurationCacheImpl vlanConfigurationCache = new VlanConfigurationCacheImpl();
55         registerService(context, new String[] {VlanConfigurationCache.class.getName()},
56                 null, vlanConfigurationCache);
57
58         FloatingIPHandler floatingIPHandler = new FloatingIPHandler();
59         registerAbstractHandlerService(context, new Class[] {INeutronFloatingIPAware.class},
60                 AbstractEvent.HandlerType.NEUTRON_FLOATING_IP, floatingIPHandler);
61
62         final NetworkHandler networkHandler = new NetworkHandler();
63         registerAbstractHandlerService(context, new Class[] {INeutronNetworkAware.class},
64                 AbstractEvent.HandlerType.NEUTRON_NETWORK, networkHandler);
65
66         SubnetHandler subnetHandler = new SubnetHandler();
67         registerAbstractHandlerService(context, new Class[] {INeutronSubnetAware.class},
68                 AbstractEvent.HandlerType.NEUTRON_SUBNET, subnetHandler);
69
70         PortHandler portHandler = new PortHandler();
71         registerAbstractHandlerService(context, new Class[] {INeutronPortAware.class},
72                 AbstractEvent.HandlerType.NEUTRON_PORT, portHandler);
73
74         RouterHandler routerHandler = new RouterHandler();
75         registerAbstractHandlerService(context, new Class[] {INeutronRouterAware.class},
76                 AbstractEvent.HandlerType.NEUTRON_ROUTER, routerHandler);
77
78         SouthboundHandler southboundHandler = new SouthboundHandler();
79         registerAbstractHandlerService(context, new Class[] {OvsdbInventoryListener.class, NodeCacheListener.class},
80                 AbstractEvent.HandlerType.SOUTHBOUND, southboundHandler);
81
82         final LBaaSHandler lBaaSHandler = new LBaaSHandler();
83         registerAbstractHandlerService(context, new Class[] {INeutronLoadBalancerAware.class, NodeCacheListener.class},
84                 AbstractEvent.HandlerType.NEUTRON_LOAD_BALANCER, lBaaSHandler);
85
86         final LBaaSPoolHandler lBaaSPoolHandler = new LBaaSPoolHandler();
87         registerAbstractHandlerService(context, new Class[] {INeutronLoadBalancerPoolAware.class},
88                 AbstractEvent.HandlerType.NEUTRON_LOAD_BALANCER_POOL, lBaaSPoolHandler);
89
90         final LBaaSPoolMemberHandler lBaaSPoolMemberHandler = new LBaaSPoolMemberHandler();
91         registerAbstractHandlerService(context, new Class[] {INeutronLoadBalancerPoolMemberAware.class},
92                 AbstractEvent.HandlerType.NEUTRON_LOAD_BALANCER_POOL_MEMBER, lBaaSPoolMemberHandler);
93
94         PortSecurityHandler portSecurityHandler = new PortSecurityHandler();
95         registerAbstractHandlerService(context,
96                 new Class[] {INeutronSecurityRuleAware.class, INeutronSecurityGroupAware.class},
97                 AbstractEvent.HandlerType.NEUTRON_PORT_SECURITY, portSecurityHandler);
98
99         final SecurityServicesImpl securityServices = new SecurityServicesImpl();
100         registerService(context,
101                 new String[]{SecurityServicesManager.class.getName()}, null, securityServices);
102
103         FWaasHandler fWaasHandler = new FWaasHandler();
104         registerAbstractHandlerService(context,
105                 new Class[] {INeutronFirewallAware.class, INeutronFirewallRuleAware.class, INeutronFirewallPolicyAware.class},
106                 AbstractEvent.HandlerType.NEUTRON_FWAAS, fWaasHandler);
107
108         ProviderNetworkManagerImpl providerNetworkManager = new ProviderNetworkManagerImpl();
109         registerService(context,
110                 new String[]{NetworkingProviderManager.class.getName()}, null, providerNetworkManager);
111
112         EventDispatcherImpl eventDispatcher = new EventDispatcherImpl();
113         registerService(context,
114                 new String[]{EventDispatcher.class.getName()}, null, eventDispatcher);
115
116         final NeutronL3Adapter neutronL3Adapter = new NeutronL3Adapter();
117         registerService(context,
118                 new String[]{NeutronL3Adapter.class.getName()}, null, neutronL3Adapter);
119
120         OpenstackRouter openstackRouter = new OpenstackRouter();
121         registerService(context,
122                 new String[]{MultiTenantAwareRouter.class.getName()}, null, openstackRouter);
123
124         Southbound southbound = new SouthboundImpl(providerContext.getSALService(DataBroker.class));
125         registerService(context,
126                 new String[]{Southbound.class.getName()}, null, southbound);
127
128         NodeCacheManagerImpl nodeCacheManager = new NodeCacheManagerImpl();
129         registerAbstractHandlerService(context, new Class[] {NodeCacheManager.class},
130                 AbstractEvent.HandlerType.NODE, nodeCacheManager);
131
132         OvsdbInventoryServiceImpl ovsdbInventoryService = new OvsdbInventoryServiceImpl(providerContext);
133         registerService(context,
134                 new String[] {OvsdbInventoryService.class.getName()}, null, ovsdbInventoryService);
135
136         // Call .setDependencies() starting with the last service registered
137         for (int i = services.size() - 1; i >= 0; i--) {
138             Object service = services.get(i);
139             if (service instanceof ConfigInterface) {
140                 ((ConfigInterface) service).setDependencies(context, null);
141             }
142         }
143
144         // TODO check if services are already available and setDependencies
145         // addingService may not be called if the service is already available when the ServiceTracker
146         // is started
147         trackService(context, INeutronNetworkCRUD.class, tenantNetworkManager, networkHandler, lBaaSHandler,
148                 lBaaSPoolHandler, lBaaSPoolMemberHandler, neutronL3Adapter);
149         trackService(context, INeutronSubnetCRUD.class, lBaaSHandler, lBaaSPoolHandler, lBaaSPoolMemberHandler,
150                 securityServices, neutronL3Adapter);
151         trackService(context, INeutronPortCRUD.class, tenantNetworkManager, lBaaSHandler, lBaaSPoolHandler,
152                 lBaaSPoolMemberHandler, securityServices, neutronL3Adapter);
153         trackService(context, INeutronLoadBalancerCRUD.class, lBaaSHandler, lBaaSPoolHandler, lBaaSPoolMemberHandler);
154         trackService(context, INeutronLoadBalancerPoolCRUD.class, lBaaSHandler, lBaaSPoolMemberHandler);
155         trackService(context, LoadBalancerProvider.class, lBaaSHandler, lBaaSPoolHandler, lBaaSPoolMemberHandler);
156         trackService(context, ArpProvider.class, neutronL3Adapter);
157         trackService(context, InboundNatProvider.class, neutronL3Adapter);
158         trackService(context, OutboundNatProvider.class, neutronL3Adapter);
159         trackService(context, RoutingProvider.class, neutronL3Adapter);
160         trackService(context, L3ForwardingProvider.class, neutronL3Adapter);
161         trackService(context, GatewayMacResolver.class, neutronL3Adapter);
162
163         // We no longer need to track the services, avoid keeping references around
164         services.clear();
165     }
166
167     private void trackService(BundleContext context, final Class<?> clazz, final ConfigInterface... dependents) {
168         @SuppressWarnings("unchecked")
169         ServiceTracker tracker = new ServiceTracker(context, clazz, null) {
170             @Override
171             public Object addingService(ServiceReference reference) {
172                 LOG.info("addingService " + clazz.getName());
173                 Object service = context.getService(reference);
174                 if (service != null) {
175                     for (ConfigInterface dependent : dependents) {
176                         dependent.setDependencies(service);
177                     }
178                 }
179                 return service;
180             }
181         };
182         tracker.open();
183     }
184
185     private void registerAbstractHandlerService(BundleContext context, Class[] interfaces,
186                                                 AbstractEvent.HandlerType handlerType, AbstractHandler handler) {
187         Dictionary<String, Object> properties = new Hashtable<>();
188         properties.put(Constants.EVENT_HANDLER_TYPE_PROPERTY, handlerType);
189         String[] interfaceNames = new String[interfaces.length + 1];
190         for (int i = 0; i < interfaces.length; i++) {
191             interfaceNames[i] = interfaces[i].getName();
192         }
193         interfaceNames[interfaces.length] = AbstractHandler.class.getName();
194         registerService(context, interfaceNames, properties, handler);
195     }
196
197
198     @Override
199     public void stop(BundleContext context) throws Exception {
200         LOG.info("ConfigActivator stop");
201         // ServiceTrackers and services are already released when bundle stops,
202         // so we don't need to close the trackers or unregister the services
203     }
204
205     private ServiceRegistration<?> registerService(BundleContext bundleContext, String[] interfaces,
206                                                    Dictionary<String, Object> properties, Object impl) {
207         services.add(impl);
208         ServiceRegistration<?> serviceRegistration = bundleContext.registerService(interfaces, impl, properties);
209         if (serviceRegistration != null) {
210             registrations.add(serviceRegistration);
211         }
212         return serviceRegistration;
213     }
214 }