3c64bd79e1483a261880d61ed23e4995df5457fa
[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.BindingAwareBroker.RpcRegistration;
18 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
19 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronFloatingIpAware;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronNetworkAware;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronNetworkDao;
22 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronPortAware;
23 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronRouterAware;
24 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronSubnetAware;
25 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.group.NeutronSecurityGroupAware;
26 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.group.SecGroupDao;
27 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.NeutronGbpMapperServiceImpl;
28 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.NeutronSecurityRuleAware;
29 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.SecRuleDao;
30 import org.opendaylight.neutron.spi.INeutronFloatingIPAware;
31 import org.opendaylight.neutron.spi.INeutronNetworkAware;
32 import org.opendaylight.neutron.spi.INeutronPortAware;
33 import org.opendaylight.neutron.spi.INeutronRouterAware;
34 import org.opendaylight.neutron.spi.INeutronSecurityGroupAware;
35 import org.opendaylight.neutron.spi.INeutronSecurityRuleAware;
36 import org.opendaylight.neutron.spi.INeutronSubnetAware;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.NeutronGbpMapperService;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceRegistration;
41
42 public class NeutronMapper implements AutoCloseable {
43
44     private final List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
45     private final DataBroker dataProvider;
46     private final RpcProviderRegistry providerRegistry;
47     private final BundleContext context;
48     private final EndpointService epService;
49     private RpcRegistration<NeutronGbpMapperService> rpcRegistration;
50
51     public NeutronMapper(DataBroker dataProvider, RpcProviderRegistry rpcProvider, BundleContext context) {
52         this.dataProvider = checkNotNull(dataProvider);
53         this.providerRegistry = checkNotNull(rpcProvider);
54         this.context = checkNotNull(context);
55         this.epService = rpcProvider.getRpcService(EndpointService.class);
56         registerAwareProviders();
57     }
58
59     private void registerAwareProviders() {
60         SecGroupDao secGroupDao = new SecGroupDao();
61         SecRuleDao secRuleDao = new SecRuleDao();
62         NeutronNetworkDao networkDao = new NeutronNetworkDao();
63
64         ServiceRegistration<INeutronSubnetAware> neutronSubnetAwareRegistration =
65                 context.registerService(INeutronSubnetAware.class, new NeutronSubnetAware(dataProvider, networkDao), null);
66         registrations.add(neutronSubnetAwareRegistration);
67
68         NeutronSecurityRuleAware securityRuleAware = new NeutronSecurityRuleAware(dataProvider, secRuleDao, secGroupDao);
69         ServiceRegistration<INeutronSecurityRuleAware> neutronSecurityRuleAwareRegistration =
70                 context.registerService(INeutronSecurityRuleAware.class, securityRuleAware, null);
71         registrations.add(neutronSecurityRuleAwareRegistration);
72
73         NeutronSecurityGroupAware securityGroupAware = new NeutronSecurityGroupAware(dataProvider, securityRuleAware, secGroupDao);
74         ServiceRegistration<INeutronSecurityGroupAware> neutronSecurityGroupAwareRegistration =
75                 context.registerService(INeutronSecurityGroupAware.class, securityGroupAware, null);
76         registrations.add(neutronSecurityGroupAwareRegistration);
77
78         ServiceRegistration<INeutronNetworkAware> neutronNetworkAwareRegistration = context.registerService(
79                 INeutronNetworkAware.class, new NeutronNetworkAware(dataProvider, securityGroupAware, networkDao), null);
80         registrations.add(neutronNetworkAwareRegistration);
81
82         NeutronPortAware portAware =
83                 new NeutronPortAware(dataProvider, epService, securityRuleAware, securityGroupAware);
84         ServiceRegistration<INeutronPortAware> neutronPortAwareRegistration =
85                 context.registerService(INeutronPortAware.class, portAware, null);
86         registrations.add(neutronPortAwareRegistration);
87
88         NeutronRouterAware routerAware = new NeutronRouterAware(dataProvider, epService);
89         ServiceRegistration<INeutronRouterAware> neutronRouterAwareRegistration =
90                 context.registerService(INeutronRouterAware.class, routerAware, null);
91         registrations.add(neutronRouterAwareRegistration);
92
93         ServiceRegistration<INeutronFloatingIPAware> neutronFloatingIpAwareRegistration = context
94             .registerService(INeutronFloatingIPAware.class, new NeutronFloatingIpAware(dataProvider), null);
95         registrations.add(neutronFloatingIpAwareRegistration);
96
97         NeutronGbpMapperService neutronGbpMapperService = new NeutronGbpMapperServiceImpl(dataProvider, securityRuleAware);
98         rpcRegistration = providerRegistry.addRpcImplementation(NeutronGbpMapperService.class, neutronGbpMapperService);
99     }
100
101     /**
102      * @see java.lang.AutoCloseable#close()
103      */
104     @Override
105     public void close() throws Exception {
106         for (ServiceRegistration<?> registration : registrations) {
107             registration.unregister();
108         }
109         rpcRegistration.close();
110     }
111
112 }