NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpAllocationPoolListener.java
1 /*
2  * Copyright (c) 2017 Hewlett Packard Enterprise, Co. 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 package org.opendaylight.netvirt.dhcpservice;
9
10 import java.math.BigInteger;
11 import java.util.List;
12 import java.util.Map;
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;
32
33 public class DhcpAllocationPoolListener extends AbstractAsyncDataTreeChangeListener<AllocationPool> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(DhcpAllocationPoolListener.class);
36
37     private final DhcpAllocationPoolManager dhcpAllocationPoolManager;
38     private final DataBroker dataBroker;
39     private final ManagedNewTransactionRunner txRunner;
40     private final JobCoordinator jobCoordinator;
41
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");
52     }
53
54     @Override
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);
67             }
68         }
69     }
70
71     @Override
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);
81         }));
82     }
83
84     @Override
85     public void update(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModificationBefore,
86             AllocationPool dataObjectModificationAfter) {
87         // TODO Auto-generated method stub
88
89     }
90
91     @Override
92     @PreDestroy
93     public void close() {
94         super.close();
95         Executors.shutdownAndAwaitTermination(getExecutorService());
96     }
97 }