2 * Copyright (c) 2017 Hewlett Packard Enterprise, Co. 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
8 package org.opendaylight.netvirt.dhcpservice;
10 import java.math.BigInteger;
11 import java.util.List;
13 import java.util.Map.Entry;
14 import javax.annotation.PreDestroy;
15 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
17 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
18 import org.opendaylight.infrautils.utils.concurrent.Executors;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
22 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpAllocationPoolAddJob;
23 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpAllocationPoolRemoveJob;
24 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.DhcpAllocationPool;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.Network;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.network.AllocationPool;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.Uint64;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class DhcpAllocationPoolListener extends AbstractAsyncDataTreeChangeListener<AllocationPool> {
35 private static final Logger LOG = LoggerFactory.getLogger(DhcpAllocationPoolListener.class);
37 private final DhcpAllocationPoolManager dhcpAllocationPoolManager;
38 private final DataBroker dataBroker;
39 private final ManagedNewTransactionRunner txRunner;
40 private final JobCoordinator jobCoordinator;
42 public DhcpAllocationPoolListener(final DhcpAllocationPoolManager dhcpAllocationPoolManager,
43 final DataBroker dataBroker, final JobCoordinator jobCoordinator) {
44 super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(DhcpAllocationPool.class)
45 .child(Network.class).child(AllocationPool.class),
46 Executors.newListeningSingleThreadExecutor("DhcpAllocationPoolListener", LOG));
47 this.dhcpAllocationPoolManager = dhcpAllocationPoolManager;
48 this.dataBroker = dataBroker;
49 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
50 this.jobCoordinator = jobCoordinator;
51 LOG.info("DhcpAllocationPoolListener initialized");
55 public void add(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
56 String networkId = key.firstKeyOf(Network.class).getNetworkId();
57 dhcpAllocationPoolManager.createIdAllocationPool(networkId, dataObjectModification);
58 Map<Uint64, List<String>> elanDpnInterfacesByName =
59 dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
60 for (Entry<Uint64, List<String>> entry : elanDpnInterfacesByName.entrySet()) {
61 BigInteger dpnId = entry.getKey().toJava();
62 for (String interfaceName : entry.getValue()) {
63 LOG.debug("Install Dhcp Entries for dpId: {} interface : {}", dpnId, interfaceName);
64 DhcpAllocationPoolAddJob job = new DhcpAllocationPoolAddJob(txRunner, interfaceName);
65 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
66 DhcpMConstants.RETRY_COUNT);
72 public void remove(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
73 String networkId = key.firstKeyOf(Network.class).getNetworkId();
74 dhcpAllocationPoolManager.releaseIdAllocationPool(networkId, dataObjectModification);
75 Map<Uint64, List<String>> elanDpnInterfacesByName =
76 dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
77 elanDpnInterfacesByName.values().forEach(interfaceNames -> interfaceNames.forEach(interfaceName -> {
78 DhcpAllocationPoolRemoveJob job = new DhcpAllocationPoolRemoveJob(txRunner, interfaceName);
79 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
80 DhcpMConstants.RETRY_COUNT);
85 public void update(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModificationBefore,
86 AllocationPool dataObjectModificationAfter) {
87 // TODO Auto-generated method stub
95 Executors.shutdownAndAwaitTermination(getExecutorService());