Merge "Use odlparent configuration for jacoco"
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / NeutronMapper.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.neutron.mapper;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronFloatingIpAware;
19 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronNetworkAware;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronPortAware;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronRouterAware;
22 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronSubnetAware;
23 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.group.NeutronSecurityGroupAware;
24 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.group.SecGroupDao;
25 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.NeutronSecurityRuleAware;
26 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.SecRuleDao;
27 import org.opendaylight.neutron.spi.INeutronFloatingIPAware;
28 import org.opendaylight.neutron.spi.INeutronNetworkAware;
29 import org.opendaylight.neutron.spi.INeutronPortAware;
30 import org.opendaylight.neutron.spi.INeutronRouterAware;
31 import org.opendaylight.neutron.spi.INeutronSecurityGroupAware;
32 import org.opendaylight.neutron.spi.INeutronSecurityRuleAware;
33 import org.opendaylight.neutron.spi.INeutronSubnetAware;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37
38 public class NeutronMapper implements AutoCloseable {
39
40     private final List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
41
42     public NeutronMapper(DataBroker dataProvider, RpcProviderRegistry rpcProvider, BundleContext context) {
43         checkNotNull(dataProvider);
44         checkNotNull(rpcProvider);
45         checkNotNull(context);
46         EndpointService epService = rpcProvider.getRpcService(EndpointService.class);
47         registerAwareProviders(dataProvider, epService, context);
48     }
49
50     private void registerAwareProviders(DataBroker dataProvider, EndpointService epService, BundleContext context) {
51         ServiceRegistration<INeutronNetworkAware> neutronNetworkAwareRegistration =
52                 context.registerService(INeutronNetworkAware.class, new NeutronNetworkAware(dataProvider), null);
53         registrations.add(neutronNetworkAwareRegistration);
54
55         ServiceRegistration<INeutronSubnetAware> neutronSubnetAwareRegistration =
56                 context.registerService(INeutronSubnetAware.class, new NeutronSubnetAware(dataProvider), null);
57         registrations.add(neutronSubnetAwareRegistration);
58
59         SecGroupDao secGroupDao = new SecGroupDao();
60         SecRuleDao secRuleDao = new SecRuleDao();
61         NeutronSecurityRuleAware securityRuleAware = new NeutronSecurityRuleAware(dataProvider, secRuleDao, secGroupDao);
62         ServiceRegistration<INeutronSecurityRuleAware> neutronSecurityRuleAwareRegistration =
63                 context.registerService(INeutronSecurityRuleAware.class, securityRuleAware, null);
64         registrations.add(neutronSecurityRuleAwareRegistration);
65
66         NeutronSecurityGroupAware securityGroupAware = new NeutronSecurityGroupAware(dataProvider, securityRuleAware, secGroupDao);
67         ServiceRegistration<INeutronSecurityGroupAware> neutronSecurityGroupAwareRegistration =
68                 context.registerService(INeutronSecurityGroupAware.class, securityGroupAware, null);
69         registrations.add(neutronSecurityGroupAwareRegistration);
70
71         NeutronPortAware portAware =
72                 new NeutronPortAware(dataProvider, epService, securityRuleAware, securityGroupAware);
73         ServiceRegistration<INeutronPortAware> neutronPortAwareRegistration =
74                 context.registerService(INeutronPortAware.class, portAware, null);
75         registrations.add(neutronPortAwareRegistration);
76
77         NeutronRouterAware routerAware = new NeutronRouterAware(dataProvider, epService, securityRuleAware);
78         ServiceRegistration<INeutronRouterAware> neutronRouterAwareRegistration =
79                 context.registerService(INeutronRouterAware.class, routerAware, null);
80         registrations.add(neutronRouterAwareRegistration);
81
82         ServiceRegistration<INeutronFloatingIPAware> neutronFloatingIpAwareRegistration = context
83             .registerService(INeutronFloatingIPAware.class, new NeutronFloatingIpAware(dataProvider, epService), null);
84         registrations.add(neutronFloatingIpAwareRegistration);
85     }
86
87     /**
88      * @see java.lang.AutoCloseable#close()
89      */
90     @Override
91     public void close() throws Exception {
92         for (ServiceRegistration<?> registration : registrations) {
93             registration.unregister();
94         }
95     }
96
97 }