Bug 3751 registration of INeutronSecurityRuleAware service
[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.NeutronSecurityGroupAware;
23 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronSecurityRuleAware;
24 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronSubnetAware;
25 import org.opendaylight.neutron.spi.INeutronFloatingIPAware;
26 import org.opendaylight.neutron.spi.INeutronNetworkAware;
27 import org.opendaylight.neutron.spi.INeutronPortAware;
28 import org.opendaylight.neutron.spi.INeutronRouterAware;
29 import org.opendaylight.neutron.spi.INeutronSecurityGroupAware;
30 import org.opendaylight.neutron.spi.INeutronSecurityRuleAware;
31 import org.opendaylight.neutron.spi.INeutronSubnetAware;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.ServiceRegistration;
35
36 public class NeutronMapper implements AutoCloseable {
37
38     private final List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
39
40     public NeutronMapper(DataBroker dataProvider, RpcProviderRegistry rpcProvider, BundleContext context) {
41         checkNotNull(dataProvider);
42         checkNotNull(rpcProvider);
43         checkNotNull(context);
44         EndpointService epService = rpcProvider.getRpcService(EndpointService.class);
45
46         registerAwareProviders(dataProvider, epService, context);
47     }
48
49     private void registerAwareProviders(DataBroker dataProvider, EndpointService epService, BundleContext context) {
50         ServiceRegistration<INeutronNetworkAware> neutronNetworkAwareRegistration = context.registerService(
51                 INeutronNetworkAware.class, new NeutronNetworkAware(dataProvider), null);
52         registrations.add(neutronNetworkAwareRegistration);
53
54         ServiceRegistration<INeutronSubnetAware> neutronSubnetAwareRegistration = context.registerService(
55                 INeutronSubnetAware.class, new NeutronSubnetAware(dataProvider), null);
56         registrations.add(neutronSubnetAwareRegistration);
57
58         ServiceRegistration<INeutronPortAware> neutronPortAwareRegistration = context.registerService(
59                 INeutronPortAware.class, new NeutronPortAware(dataProvider, epService), null);
60         registrations.add(neutronPortAwareRegistration);
61
62         ServiceRegistration<INeutronSecurityGroupAware> neutronSecurityGroupAwareRegistration = context.registerService(
63                 INeutronSecurityGroupAware.class, new NeutronSecurityGroupAware(dataProvider), null);
64         registrations.add(neutronSecurityGroupAwareRegistration);
65
66         ServiceRegistration<INeutronSecurityRuleAware> neutronSecurityRuleAwareRegistration = context.registerService(
67                 INeutronSecurityRuleAware.class, new NeutronSecurityRuleAware(dataProvider), null);
68         registrations.add(neutronSecurityRuleAwareRegistration);
69
70         NeutronRouterAware.init(dataProvider, epService);
71         ServiceRegistration<INeutronRouterAware> neutronRouterAwareRegistration = context.registerService(
72                 INeutronRouterAware.class, NeutronRouterAware.getInstance(), null);
73         registrations.add(neutronRouterAwareRegistration);
74
75         ServiceRegistration<INeutronFloatingIPAware> neutronFloatingIpAwareRegistration = context.registerService(
76                 INeutronFloatingIPAware.class, new NeutronFloatingIpAware(dataProvider, epService), null);
77         registrations.add(neutronFloatingIpAwareRegistration);
78     }
79
80     /**
81      * @see java.lang.AutoCloseable#close()
82      */
83     @Override
84     public void close() throws Exception {
85         for (ServiceRegistration<?> registration : registrations) {
86             registration.unregister();
87         }
88     }
89
90 }