Introduced neutron-mapper
[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.NeutronNetworkAware;
19 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronPortAware;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronSecurityGroupAware;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronSubnetAware;
22 import org.opendaylight.neutron.spi.INeutronNetworkAware;
23 import org.opendaylight.neutron.spi.INeutronPortAware;
24 import org.opendaylight.neutron.spi.INeutronSecurityGroupAware;
25 import org.opendaylight.neutron.spi.INeutronSubnetAware;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceRegistration;
29
30 public class NeutronMapper implements AutoCloseable {
31
32     private final List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
33
34     public NeutronMapper(DataBroker dataProvider, RpcProviderRegistry rpcProvider, BundleContext context) {
35         checkNotNull(dataProvider);
36         checkNotNull(rpcProvider);
37         checkNotNull(context);
38         EndpointService epService = rpcProvider.getRpcService(EndpointService.class);
39
40         registerAwareProviders(dataProvider, epService, context);
41     }
42
43     private void registerAwareProviders(DataBroker dataProvider, EndpointService epService, BundleContext context) {
44         ServiceRegistration<INeutronNetworkAware> neutronNetworkAwareRegistration = context.registerService(
45             INeutronNetworkAware.class, new NeutronNetworkAware(dataProvider), null);
46         registrations.add(neutronNetworkAwareRegistration);
47
48         ServiceRegistration<INeutronSubnetAware> neutronSubnetAwareRegistration = context.registerService(
49             INeutronSubnetAware.class, new NeutronSubnetAware(dataProvider), null);
50         registrations.add(neutronSubnetAwareRegistration);
51
52         ServiceRegistration<INeutronPortAware> neutronPortAwareRegistration = context.registerService(
53             INeutronPortAware.class, new NeutronPortAware(dataProvider, epService), null);
54         registrations.add(neutronPortAwareRegistration);
55
56         ServiceRegistration<INeutronSecurityGroupAware> neutronSecurityGroupAwareRegistration = context.registerService(
57             INeutronSecurityGroupAware.class, new NeutronSecurityGroupAware(dataProvider), null);
58         registrations.add(neutronSecurityGroupAwareRegistration);
59     }
60
61     /**
62      * @see java.lang.AutoCloseable#close()
63      */
64     @Override
65     public void close() throws Exception {
66         for (ServiceRegistration<?> registration : registrations) {
67             registration.unregister();
68         }
69     }
70
71 }