001c39f0750fb637ab7609fffeaf9a47160b718b
[netvirt.git] / vpnservice / ipv6service / impl / src / main / java / org / opendaylight / netvirt / ipv6service / NeutronSubnetChangeListener.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netvirt.ipv6service;
9
10 import com.google.common.collect.ImmutableBiMap;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.netvirt.ipv6service.utils.Ipv6Constants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class NeutronSubnetChangeListener extends AbstractDataChangeListener<Subnet> implements AutoCloseable {
34     private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
35     private static final ImmutableBiMap<Class<? extends IpVersionBase>,String> IPV_MAP
36             = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>,String>()
37             .put(IpVersionV4.class, Ipv6Constants.IP_VERSION_V4)
38             .put(IpVersionV6.class, Ipv6Constants.IP_VERSION_V6)
39             .build();
40
41     private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,String> DHCPV6_MAP
42             = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>,String>()
43             .put(Dhcpv6Off.class,Ipv6Constants.DHCPV6_OFF)
44             .put(Dhcpv6Stateful.class,Ipv6Constants.IPV6_DHCPV6_STATEFUL)
45             .put(Dhcpv6Slaac.class,Ipv6Constants.IPV6_SLAAC)
46             .put(Dhcpv6Stateless.class,Ipv6Constants.IPV6_DHCPV6_STATELESS)
47             .build();
48
49     private ListenerRegistration<DataChangeListener> listenerRegistration;
50     private IfMgr ifMgr;
51     private final DataBroker broker;
52
53     public NeutronSubnetChangeListener(final DataBroker db) {
54         super(Subnet.class);
55         broker = db;
56         this.ifMgr = IfMgr.getIfMgrInstance();
57         registerListener(db);
58     }
59
60     @Override
61     public void close() throws Exception {
62         if (listenerRegistration != null) {
63             listenerRegistration.close();
64             listenerRegistration = null;
65         }
66         LOG.info("Neutron Subnet listener Closed");
67     }
68
69     private void registerListener(final DataBroker db) {
70         listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
71                 InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet.class),
72                 NeutronSubnetChangeListener.this, DataChangeScope.SUBTREE);
73     }
74
75     @Override
76     protected void add(InstanceIdentifier<Subnet> identifier, Subnet input) {
77         LOG.info("Add Subnet notification handler is invoked...");
78         String ipv6AddrMode = "";
79         if (input.getIpv6AddressMode() != null) {
80             ipv6AddrMode = DHCPV6_MAP.get(input.getIpv6AddressMode());
81         }
82         String ipv6RaMode = "";
83         if (input.getIpv6RaMode() != null) {
84             ipv6RaMode = DHCPV6_MAP.get(input.getIpv6RaMode());
85         }
86         ifMgr.addSubnet(input.getUuid(), input.getName(), input.getNetworkId(),
87                 input.getTenantId(), input.getGatewayIp(), IPV_MAP.get(input.getIpVersion()),
88                 input.getCidr(), ipv6AddrMode, ipv6RaMode);
89     }
90
91     @Override
92     protected void remove(InstanceIdentifier<Subnet> identifier, Subnet input) {
93         LOG.info("Remove Subnet notification handler is invoked...");
94         ifMgr.removeSubnet(input.getUuid());
95     }
96
97     @Override
98     protected void update(InstanceIdentifier<Subnet> identifier, Subnet original, Subnet update) {
99         LOG.info("Update Subnet notification handler is invoked...");
100     }
101 }