2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.dhcpservice;
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;
32 public class DhcpInterfaceConfigListener
33 extends AsyncDataTreeChangeListenerBase<Interface, DhcpInterfaceConfigListener> {
35 private static final Logger LOG = LoggerFactory.getLogger(DhcpInterfaceConfigListener.class);
37 private final ManagedNewTransactionRunner txRunner;
38 private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
39 private final DhcpManager dhcpManager;
40 private final JobCoordinator jobCoordinator;
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);
56 LOG.info("DhcpInterfaceConfigListener Closed");
60 protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
61 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(del.getName()), () -> {
62 IfTunnel tunnelInterface = del.getAugmentation(IfTunnel.class);
63 IfL2vlan vlanInterface = del.getAugmentation(IfL2vlan.class);
64 String interfaceName = del.getName();
65 if (tunnelInterface != null && !tunnelInterface.isInternal()) {
66 IpAddress tunnelIp = tunnelInterface.getTunnelDestination();
67 ParentRefs interfce = del.getAugmentation(ParentRefs.class);
68 if (interfce != null) {
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Calling handleTunnelStateDown for tunnelIp {} and interface {}",
71 tunnelIp, interfaceName);
73 return dhcpExternalTunnelManager.handleTunnelStateDown(tunnelIp,
74 interfce.getDatapathNodeIdentifier());
77 if (vlanInterface != null) {
78 return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
79 tx -> DhcpServiceUtils.unbindDhcpService(interfaceName, tx)));
81 return Collections.emptyList();
82 }, DhcpMConstants.RETRY_COUNT);
86 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
87 // Handled in update () DhcpInterfaceEventListener
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.getAugmentation(IfL2vlan.class);
95 if (vlanInterface == null) {
96 return Collections.emptyList();
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);
106 return Collections.emptyList();
107 }, DhcpMConstants.RETRY_COUNT);
111 protected InstanceIdentifier<Interface> getWildCardPath() {
112 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
116 protected DhcpInterfaceConfigListener getDataTreeChangeListener() {
117 return DhcpInterfaceConfigListener.this;