Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / vpnservice / dhcpservice / 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 = getDpnInterfacesByNetwork(networkId);
56         for (Entry<BigInteger, List<String>> entry : elanDpnInterfacesByName.entrySet()) {
57             BigInteger dpnId = entry.getKey();
58             for (String interfaceName : entry.getValue()) {
59                 LOG.debug("Install Dhcp Entries for dpId: {} interface : {}", dpnId, interfaceName);
60                 DhcpAllocationPoolAddJob job = new DhcpAllocationPoolAddJob(txRunner, interfaceName);
61                 jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
62                         DhcpMConstants.RETRY_COUNT);
63             }
64         }
65     }
66
67     @Override
68     protected DhcpAllocationPoolListener getDataTreeChangeListener() {
69         return this;
70     }
71
72     @Override
73     protected InstanceIdentifier<AllocationPool> getWildCardPath() {
74         return InstanceIdentifier.builder(DhcpAllocationPool.class)//
75                 .child(Network.class).child(AllocationPool.class).build();
76     }
77
78     @Override
79     protected void remove(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
80         String networkId = key.firstKeyOf(Network.class).getNetworkId();
81         dhcpAllocationPoolManager.releaseIdAllocationPool(networkId, dataObjectModification);
82         Map<BigInteger, List<String>> elanDpnInterfacesByName = getDpnInterfacesByNetwork(networkId);
83         elanDpnInterfacesByName.values().forEach(interfaceNames -> interfaceNames.forEach(interfaceName -> {
84             DhcpAllocationPoolRemoveJob job = new DhcpAllocationPoolRemoveJob(txRunner, interfaceName);
85             jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job,
86                     DhcpMConstants.RETRY_COUNT);
87         }));
88     }
89
90     @Override
91     protected void update(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModificationBefore,
92             AllocationPool dataObjectModificationAfter) {
93         // TODO Auto-generated method stub
94
95     }
96
97     private Map<BigInteger, List<String>> getDpnInterfacesByNetwork(String networkId) {
98         Map<BigInteger, List<String>> elanDpnInterfacesByName = dhcpAllocationPoolManager
99                 .getElanDpnInterfacesByName(dataBroker, networkId);
100         return elanDpnInterfacesByName;
101     }
102 }