Merge "Minor fixes for floating ip NAT flows - Use apply actions instead of write...
[netvirt.git] / vpnservice / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpInterfaceConfigListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.netvirt.dhcpservice;
10
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class DhcpInterfaceConfigListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
27
28     private static final Logger logger = LoggerFactory.getLogger(DhcpInterfaceConfigListener.class);
29
30     private ListenerRegistration<DataChangeListener> listenerRegistration;
31
32     private final DataBroker dataBroker;
33     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
34
35     public DhcpInterfaceConfigListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpExternalTunnelManager) {
36         super(Interface.class);
37         this.dataBroker = dataBroker;
38         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
39     }
40
41     public void init() {
42         registerListener();
43     }
44
45     private void registerListener() {
46         try {
47             listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
48                     getWildCardPath(), DhcpInterfaceConfigListener.this, DataChangeScope.SUBTREE);
49         } catch (final Exception e) {
50             logger.error("DhcpInterfaceEventListener DataChange listener registration fail!", e);
51             throw new IllegalStateException("DhcpInterfaceEventListener registration Listener failed.", e);
52         }
53     }
54
55     private InstanceIdentifier<Interface> getWildCardPath() {
56         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
57     }
58
59     @Override
60     public void close() throws Exception {
61         if (listenerRegistration != null) {
62             listenerRegistration.close();
63         }
64         logger.info("DhcpInterfaceConfigListener Closed");
65     }
66
67     @Override
68     protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
69         IfTunnel tunnelInterface = del.getAugmentation(IfTunnel.class);
70         if (tunnelInterface != null && !tunnelInterface.isInternal()) {
71             IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
72             ParentRefs interfce = del.getAugmentation(ParentRefs.class);
73             if (interfce != null) {
74                 dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp, interfce.getDatapathNodeIdentifier());
75             }
76         }
77     }
78
79     @Override
80     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
81         // Handled in update () DhcpInterfaceEventListener
82     }
83
84     @Override
85     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
86         // Handled in add() DhcpInterfaceEventListener
87     }
88 }