dhcpservice: drop nullToEmpty and reqNonNullOrElse
[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 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;
29
30 public class DhcpAllocationPoolListener
31         extends AsyncDataTreeChangeListenerBase<AllocationPool, DhcpAllocationPoolListener> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(DhcpAllocationPoolListener.class);
34
35     private final DhcpAllocationPoolManager dhcpAllocationPoolManager;
36     private final DataBroker dataBroker;
37     private final ManagedNewTransactionRunner txRunner;
38     private final JobCoordinator jobCoordinator;
39
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");
49     }
50
51     @Override
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);
64             }
65         }
66     }
67
68     @Override
69     protected DhcpAllocationPoolListener getDataTreeChangeListener() {
70         return this;
71     }
72
73     @Override
74     protected InstanceIdentifier<AllocationPool> getWildCardPath() {
75         return InstanceIdentifier.builder(DhcpAllocationPool.class)//
76                 .child(Network.class).child(AllocationPool.class).build();
77     }
78
79     @Override
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);
89         }));
90     }
91
92     @Override
93     protected void update(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModificationBefore,
94             AllocationPool dataObjectModificationAfter) {
95         // TODO Auto-generated method stub
96
97     }
98 }