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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
19 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
20 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
21 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpAllocationPoolAddJob;
22 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpAllocationPoolRemoveJob;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.DhcpAllocationPool;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.Network;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.network.AllocationPool;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class DhcpAllocationPoolListener
31 extends AsyncDataTreeChangeListenerBase<AllocationPool, DhcpAllocationPoolListener> {
33 private static final Logger LOG = LoggerFactory.getLogger(DhcpAllocationPoolListener.class);
35 private final DhcpAllocationPoolManager dhcpAllocationPoolManager;
36 private final DataBroker dataBroker;
37 private final ManagedNewTransactionRunner txRunner;
38 private final JobCoordinator jobCoordinator;
40 public DhcpAllocationPoolListener(final DhcpAllocationPoolManager dhcpAllocationPoolManager,
41 final DataBroker dataBroker, final JobCoordinator jobCoordinator) {
42 super(AllocationPool.class, DhcpAllocationPoolListener.class);
43 this.dhcpAllocationPoolManager = dhcpAllocationPoolManager;
44 this.dataBroker = dataBroker;
45 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
46 this.jobCoordinator = jobCoordinator;
47 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
48 LOG.info("DhcpAllocationPoolListener initialized");
52 protected void add(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
53 String networkId = key.firstKeyOf(Network.class).getNetworkId();
54 dhcpAllocationPoolManager.createIdAllocationPool(networkId, dataObjectModification);
55 Map<BigInteger, List<String>> elanDpnInterfacesByName =
56 dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
57 for (Entry<BigInteger, List<String>> entry : elanDpnInterfacesByName.entrySet()) {
58 BigInteger dpnId = entry.getKey();
59 for (String interfaceName : entry.getValue()) {
60 LOG.debug("Install Dhcp Entries for dpId: {} interface : {}", dpnId, interfaceName);
61 DhcpAllocationPoolAddJob job = new DhcpAllocationPoolAddJob(txRunner, interfaceName);
62 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
63 DhcpMConstants.RETRY_COUNT);
69 protected DhcpAllocationPoolListener getDataTreeChangeListener() {
74 protected InstanceIdentifier<AllocationPool> getWildCardPath() {
75 return InstanceIdentifier.builder(DhcpAllocationPool.class)//
76 .child(Network.class).child(AllocationPool.class).build();
80 protected void remove(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
81 String networkId = key.firstKeyOf(Network.class).getNetworkId();
82 dhcpAllocationPoolManager.releaseIdAllocationPool(networkId, dataObjectModification);
83 Map<BigInteger, List<String>> elanDpnInterfacesByName =
84 dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
85 elanDpnInterfacesByName.values().forEach(interfaceNames -> interfaceNames.forEach(interfaceName -> {
86 DhcpAllocationPoolRemoveJob job = new DhcpAllocationPoolRemoveJob(txRunner, interfaceName);
87 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
88 DhcpMConstants.RETRY_COUNT);
93 protected void update(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModificationBefore,
94 AllocationPool dataObjectModificationAfter) {
95 // TODO Auto-generated method stub