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.opendaylight.yangtools.yang.common.Uint64;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class DhcpAllocationPoolListener
32 extends AsyncDataTreeChangeListenerBase<AllocationPool, DhcpAllocationPoolListener> {
34 private static final Logger LOG = LoggerFactory.getLogger(DhcpAllocationPoolListener.class);
36 private final DhcpAllocationPoolManager dhcpAllocationPoolManager;
37 private final DataBroker dataBroker;
38 private final ManagedNewTransactionRunner txRunner;
39 private final JobCoordinator jobCoordinator;
41 public DhcpAllocationPoolListener(final DhcpAllocationPoolManager dhcpAllocationPoolManager,
42 final DataBroker dataBroker, final JobCoordinator jobCoordinator) {
43 super(AllocationPool.class, DhcpAllocationPoolListener.class);
44 this.dhcpAllocationPoolManager = dhcpAllocationPoolManager;
45 this.dataBroker = dataBroker;
46 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
47 this.jobCoordinator = jobCoordinator;
48 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
49 LOG.info("DhcpAllocationPoolListener initialized");
53 protected void add(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
54 String networkId = key.firstKeyOf(Network.class).getNetworkId();
55 dhcpAllocationPoolManager.createIdAllocationPool(networkId, dataObjectModification);
56 Map<Uint64, List<String>> elanDpnInterfacesByName =
57 dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
58 for (Entry<Uint64, List<String>> entry : elanDpnInterfacesByName.entrySet()) {
59 BigInteger dpnId = entry.getKey().toJava();
60 for (String interfaceName : entry.getValue()) {
61 LOG.debug("Install Dhcp Entries for dpId: {} interface : {}", dpnId, interfaceName);
62 DhcpAllocationPoolAddJob job = new DhcpAllocationPoolAddJob(txRunner, interfaceName);
63 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
64 DhcpMConstants.RETRY_COUNT);
70 protected DhcpAllocationPoolListener getDataTreeChangeListener() {
75 protected InstanceIdentifier<AllocationPool> getWildCardPath() {
76 return InstanceIdentifier.builder(DhcpAllocationPool.class)//
77 .child(Network.class).child(AllocationPool.class).build();
81 protected void remove(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
82 String networkId = key.firstKeyOf(Network.class).getNetworkId();
83 dhcpAllocationPoolManager.releaseIdAllocationPool(networkId, dataObjectModification);
84 Map<Uint64, List<String>> elanDpnInterfacesByName =
85 dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
86 elanDpnInterfacesByName.values().forEach(interfaceNames -> interfaceNames.forEach(interfaceName -> {
87 DhcpAllocationPoolRemoveJob job = new DhcpAllocationPoolRemoveJob(txRunner, interfaceName);
88 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
89 DhcpMConstants.RETRY_COUNT);
94 protected void update(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModificationBefore,
95 AllocationPool dataObjectModificationAfter) {
96 // TODO Auto-generated method stub