Full <> clean-up
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / 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.providers;
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.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.*;
19 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
21 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
22 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestratorImpl;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services.*;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services.arp.GatewayMacResolverService;
26 import org.osgi.framework.BundleActivator;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.osgi.framework.ServiceRegistration;
30 import org.osgi.util.tracker.ServiceTracker;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class ConfigActivator implements BundleActivator {
35     private static final Logger LOG = LoggerFactory.getLogger(ConfigActivator.class);
36     private List<ServiceRegistration<?>> registrations = new ArrayList<>();
37     private ProviderContext providerContext;
38
39     public ConfigActivator(ProviderContext providerContext) {
40         this.providerContext = providerContext;
41     }
42
43     @Override
44     public void start(BundleContext context) throws Exception {
45         LOG.info("ConfigActivator start:");
46
47         PipelineOrchestratorImpl pipelineOrchestrator = new PipelineOrchestratorImpl();
48         registerService(context,
49                 new String[] {PipelineOrchestrator.class.getName(),NodeCacheListener.class.getName()},
50                 null, pipelineOrchestrator);
51
52         Dictionary<String, Object> of13ProviderProperties = new Hashtable<>();
53         of13ProviderProperties.put(Constants.SOUTHBOUND_PROTOCOL_PROPERTY, "ovsdb");
54         of13ProviderProperties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW13);
55         final OF13Provider of13Provider = new OF13Provider();
56         registerService(context,
57                 new String[] {NetworkingProvider.class.getName()},
58                 of13ProviderProperties, of13Provider);
59
60         ClassifierService classifierService = new ClassifierService();
61         registerService(context, ClassifierProvider.class.getName(),
62                 classifierService, Service.CLASSIFIER);
63
64         ArpResponderService arpResponderService = new ArpResponderService();
65         registerService(context, ArpProvider.class.getName(),
66                 arpResponderService, Service.ARP_RESPONDER);
67
68         InboundNatService inboundNatService = new InboundNatService();
69         registerService(context, InboundNatProvider.class.getName(),
70                 inboundNatService, Service.INBOUND_NAT);
71
72         IngressAclService ingressAclService = new IngressAclService();
73         registerService(context, IngressAclProvider.class.getName(),
74                 ingressAclService, Service.INGRESS_ACL);
75
76         LoadBalancerService loadBalancerService = new LoadBalancerService();
77         registerService(context, LoadBalancerProvider.class.getName(),
78                 loadBalancerService, Service.LOAD_BALANCER);
79
80         RoutingService routingService = new RoutingService();
81         registerService(context, RoutingProvider.class.getName(),
82                 routingService, Service.ROUTING);
83
84         L3ForwardingService l3ForwardingService = new L3ForwardingService();
85         registerService(context, L3ForwardingProvider.class.getName(),
86                 l3ForwardingService, Service.L3_FORWARDING);
87
88         L2RewriteService l2RewriteService = new L2RewriteService();
89         registerService(context, L2RewriteProvider.class.getName(),
90                 l2RewriteService, Service.L2_REWRITE);
91
92         L2ForwardingService l2ForwardingService = new L2ForwardingService();
93         registerService(context, L2ForwardingProvider.class.getName(),
94                 l2ForwardingService, Service.L2_FORWARDING);
95
96         EgressAclService egressAclService = new EgressAclService();
97         registerService(context, EgressAclProvider.class.getName(),
98                 egressAclService, Service.EGRESS_ACL);
99
100         OutboundNatService outboundNatService = new OutboundNatService();
101         registerService(context, OutboundNatProvider.class.getName(),
102                 outboundNatService, Service.OUTBOUND_NAT);
103
104         GatewayMacResolverService gatewayMacResolverService = new GatewayMacResolverService();
105         registerService(context, GatewayMacResolver.class.getName(),
106                 gatewayMacResolverService, Service.GATEWAY_RESOLVER);
107         getNotificationProviderService().registerNotificationListener(gatewayMacResolverService);
108
109
110         pipelineOrchestrator.setDependencies(context, null);
111         outboundNatService.setDependencies(context, null);
112         egressAclService.setDependencies(context, null);
113         l2ForwardingService.setDependencies(context, null);
114         l2RewriteService.setDependencies(context, null);
115         l3ForwardingService.setDependencies(context, null);
116         routingService.setDependencies(context, null);
117         loadBalancerService.setDependencies(context, null);
118         ingressAclService.setDependencies(context, null);
119         inboundNatService.setDependencies(context, null);
120         arpResponderService.setDependencies(context, null);
121         classifierService.setDependencies(context, null);
122         of13Provider.setDependencies(context, null);
123         gatewayMacResolverService.setDependencies(context, null);
124
125         @SuppressWarnings("unchecked")
126         ServiceTracker networkingProviderManagerTracker = new ServiceTracker(context,
127                 NetworkingProviderManager.class, null) {
128             @Override
129             public Object addingService(ServiceReference reference) {
130                 LOG.info("addingService NetworkingProviderManager");
131                 NetworkingProviderManager service =
132                         (NetworkingProviderManager) context.getService(reference);
133                 if (service != null) {
134                     of13Provider.setDependencies(service);
135                 }
136                 return service;
137             }
138         };
139         networkingProviderManagerTracker.open();
140     }
141
142     @Override
143     public void stop(BundleContext context) throws Exception {
144         LOG.info("ConfigActivator stop");
145         // ServiceTrackers and services are already released when bundle stops,
146         // so we don't need to close the trackers or unregister the services
147     }
148
149     private ServiceRegistration<?> registerService(BundleContext bundleContext, String[] interfaces,
150                                                    Dictionary<String, Object> properties, Object impl) {
151         ServiceRegistration<?> serviceRegistration = bundleContext.registerService(interfaces, impl, properties);
152         if (serviceRegistration != null) {
153             registrations.add(serviceRegistration);
154         }
155         return serviceRegistration;
156     }
157
158     private ServiceRegistration<?> registerService(BundleContext bundleContext, String interfaceClassName,
159                                                    Object impl, Object serviceProperty) {
160         Dictionary<String, Object> properties = new Hashtable<>();
161         properties.put(AbstractServiceInstance.SERVICE_PROPERTY, serviceProperty);
162         properties.put(Constants.PROVIDER_NAME_PROPERTY, OF13Provider.NAME);
163         return registerService(bundleContext,
164                 new String[] {AbstractServiceInstance.class.getName(), interfaceClassName},
165                 properties, impl);
166     }
167
168     private NotificationProviderService getNotificationProviderService(){
169         return this.providerContext.getSALService(NotificationProviderService.class);
170     }
171 }