Fixup Augmentable and Identifiable methods changing
[netvirt.git] / 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 java.util.Collections;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
15 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
17 import org.opendaylight.genius.mdsalutil.NwConstants;
18 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
19 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class DhcpInterfaceConfigListener
33         extends AsyncDataTreeChangeListenerBase<Interface, DhcpInterfaceConfigListener> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(DhcpInterfaceConfigListener.class);
36
37     private final ManagedNewTransactionRunner txRunner;
38     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
39     private final DhcpManager dhcpManager;
40     private final JobCoordinator jobCoordinator;
41
42     public DhcpInterfaceConfigListener(DataBroker dataBroker,
43             DhcpExternalTunnelManager dhcpExternalTunnelManager, DhcpManager dhcpManager,
44             JobCoordinator jobCoordinator) {
45         super(Interface.class, DhcpInterfaceConfigListener.class);
46         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
47         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
48         this.dhcpManager = dhcpManager;
49         this.jobCoordinator = jobCoordinator;
50         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
51     }
52
53     @Override
54     public void close() {
55         super.close();
56         LOG.info("DhcpInterfaceConfigListener Closed");
57     }
58
59     @Override
60     protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
61         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(del.getName()), () -> {
62             IfTunnel tunnelInterface = del.augmentation(IfTunnel.class);
63             IfL2vlan vlanInterface = del.augmentation(IfL2vlan.class);
64             String interfaceName = del.getName();
65             if (tunnelInterface != null && !tunnelInterface.isInternal()) {
66                 IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
67                 ParentRefs interfce = del.augmentation(ParentRefs.class);
68                 if (interfce != null) {
69                     if (LOG.isTraceEnabled()) {
70                         LOG.trace("Calling handleTunnelStateDown for tunnelIp {} and interface {}",
71                                 tunnelIp, interfaceName);
72                     }
73                     return dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp,
74                             interfce.getDatapathNodeIdentifier());
75                 }
76             }
77             if (vlanInterface != null) {
78                 return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
79                     tx -> DhcpServiceUtils.unbindDhcpService(interfaceName, tx)));
80             }
81             return Collections.emptyList();
82         }, DhcpMConstants.RETRY_COUNT);
83     }
84
85     @Override
86     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
87         // Handled in update () DhcpInterfaceEventListener
88     }
89
90     @Override
91     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
92         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(add.getName()), () -> {
93             String interfaceName = add.getName();
94             IfL2vlan vlanInterface = add.augmentation(IfL2vlan.class);
95             if (vlanInterface == null) {
96                 return Collections.emptyList();
97             }
98             Port port = dhcpManager.getNeutronPort(interfaceName);
99             Subnet subnet = dhcpManager.getNeutronSubnet(port);
100             if (null != subnet && subnet.isEnableDhcp()) {
101                 return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
102                     LOG.debug("Binding DHCP service for interface {}", interfaceName);
103                     DhcpServiceUtils.bindDhcpService(interfaceName, NwConstants.DHCP_TABLE, tx);
104                 }));
105             }
106             return Collections.emptyList();
107         }, DhcpMConstants.RETRY_COUNT);
108     }
109
110     @Override
111     protected InstanceIdentifier<Interface> getWildCardPath() {
112         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
113     }
114
115     @Override
116     protected DhcpInterfaceConfigListener getDataTreeChangeListener() {
117         return DhcpInterfaceConfigListener.this;
118     }
119 }