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 com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.EventListener;
14 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import java.util.stream.Collectors;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import javax.annotation.PostConstruct;
22 import javax.annotation.PreDestroy;
23 import javax.inject.Inject;
24 import javax.inject.Singleton;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
30 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
31 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.DhcpAllocationPool;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.Network;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.NetworkKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp_allocation_pool.rev161214.dhcp_allocation_pool.network.AllocationPool;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 public class DhcpAllocationPoolManager implements AutoCloseable, EventListener {
64 private static final Logger LOG = LoggerFactory.getLogger(DhcpAllocationPoolManager.class);
66 private DhcpAllocationPoolListener dhcpAllocationPoolListener;
68 private final DataBroker dataBroker;
69 private final IdManagerService idManager;
70 private final DhcpserviceConfig config;
71 private final JobCoordinator jobCoordinator;
74 public DhcpAllocationPoolManager(final DataBroker dataBroker, final IdManagerService idManager,
75 final DhcpserviceConfig config, final JobCoordinator jobCoordinator) {
76 this.dataBroker = dataBroker;
77 this.idManager = idManager;
79 this.jobCoordinator = jobCoordinator;
84 if (config.isDhcpDynamicAllocationPoolEnabled()) {
85 dhcpAllocationPoolListener = new DhcpAllocationPoolListener(this, dataBroker, jobCoordinator);
86 LOG.info("DHCP Allocation Pool Service initialized");
93 LOG.info("{} close", getClass().getSimpleName());
94 if (dhcpAllocationPoolListener != null) {
95 dhcpAllocationPoolListener.close();
100 public IpAddress getIpAllocation(String networkId, AllocationPool pool, String macAddress) {
101 String poolIdKey = getPoolKeyIdByAllocationPool(networkId, pool);
102 long allocatedIpLong = createIdAllocation(poolIdKey, macAddress);
103 LOG.debug("allocated id {} for mac {}, from pool {}", allocatedIpLong, macAddress, poolIdKey);
104 return allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong) : null;
107 public void releaseIpAllocation(String networkId, AllocationPool pool, String macAddress) {
108 String poolIdKey = getPoolKeyIdByAllocationPool(networkId, pool);
109 LOG.debug("going to release id for mac {}, from pool {}", macAddress, poolIdKey);
110 releaseIdAllocation(poolIdKey, macAddress);
114 public AllocationPool getAllocationPoolByNetwork(String networkId) throws ReadFailedException {
115 InstanceIdentifier<Network> network = InstanceIdentifier.builder(DhcpAllocationPool.class)
116 .child(Network.class, new NetworkKey(networkId)).build();
117 Optional<Network> optionalNetworkConfData = SingleTransactionDataBroker.syncReadOptional(dataBroker,
118 LogicalDatastoreType.CONFIGURATION, network);
119 if (!optionalNetworkConfData.isPresent()) {
120 LOG.info("No network configuration data for network {}", networkId);
123 Network networkConfData = optionalNetworkConfData.get();
124 List<AllocationPool> allocationPoolList = networkConfData.getAllocationPool();
125 // if network has allocation pool list - get the first element
126 // as we have no info about a specific subnet
127 if (allocationPoolList != null && !allocationPoolList.isEmpty()) {
128 return allocationPoolList.get(0);
130 LOG.warn("No allocation pools for network {}", networkId);
136 public Map<BigInteger, List<String>> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) {
137 InstanceIdentifier<ElanDpnInterfacesList> elanDpnIfacesIid = InstanceIdentifier.builder(ElanDpnInterfaces.class)
138 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
139 Optional<ElanDpnInterfacesList> elanDpnIfacesOpc = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
141 if (!elanDpnIfacesOpc.isPresent()) {
142 LOG.warn("Could not find DpnInterfaces for elan {}", elanInstanceName);
143 return Collections.emptyMap();
146 return elanDpnIfacesOpc.get().nonnullDpnInterfaces().stream()
147 .collect(Collectors.toMap(DpnInterfaces::getDpId,
148 value -> value.getInterfaces() != null ? value.getInterfaces() : Collections.emptyList()));
152 public String getNetworkByPort(String portUuid) throws ReadFailedException {
153 InstanceIdentifier<ElanInterface> elanInterfaceName = InstanceIdentifier.builder(ElanInterfaces.class)
154 .child(ElanInterface.class, new ElanInterfaceKey(portUuid)).build();
155 Optional<ElanInterface> optionalElanInterface = SingleTransactionDataBroker.syncReadOptional(dataBroker,
156 LogicalDatastoreType.CONFIGURATION, elanInterfaceName);
157 if (!optionalElanInterface.isPresent()) {
158 LOG.info("No elan interface data for port {}", portUuid);
161 ElanInterface elanInterface = optionalElanInterface.get();
162 return elanInterface.getElanInstanceName();
165 private static String getPoolKeyIdByAllocationPool(String networkId, AllocationPool pool) {
166 return "dhcpAllocationPool." + networkId + "." + pool.getSubnet().stringValue();
169 private long createIdAllocation(String groupIdKey, String idKey) {
170 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(groupIdKey).setIdKey(idKey).build();
172 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
173 RpcResult<AllocateIdOutput> rpcResult = result.get();
174 return rpcResult.getResult().getIdValue();
175 } catch (NullPointerException | InterruptedException | ExecutionException e) {
176 LOG.trace("Failed to allocate id for DHCP Allocation Pool Service", e);
181 private void releaseIdAllocation(String groupIdKey, String idKey) {
182 ReleaseIdInput getIdInput = new ReleaseIdInputBuilder().setPoolName(groupIdKey).setIdKey(idKey).build();
183 JdkFutures.addErrorLogging(idManager.releaseId(getIdInput), LOG, "Release Id");
186 protected void createIdAllocationPool(String networkId, AllocationPool pool) {
187 String poolName = getPoolKeyIdByAllocationPool(networkId, pool);
188 long low = DhcpServiceUtils.convertIpToLong(pool.getAllocateFrom());
189 long high = DhcpServiceUtils.convertIpToLong(pool.getAllocateTo());
190 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(poolName).setLow(low).setHigh(high)
193 Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
194 if (result != null && result.get().isSuccessful()) {
195 LOG.info("DHCP Allocation Pool Service : Created IdPool name {}", poolName);
197 LOG.error("DHCP Allocation Pool Service : Unable to create IdPool name {}", poolName);
199 } catch (InterruptedException | ExecutionException e) {
200 LOG.error("Failed to create Pool for DHCP Allocation Pool Service", e);
204 protected void releaseIdAllocationPool(String networkId, AllocationPool pool) {
205 String poolName = getPoolKeyIdByAllocationPool(networkId, pool);
206 DeleteIdPoolInput deletePool = new DeleteIdPoolInputBuilder().setPoolName(poolName).build();
208 Future<RpcResult<DeleteIdPoolOutput>> result = idManager.deleteIdPool(deletePool);
209 if (result != null && result.get().isSuccessful()) {
210 LOG.info("DHCP Allocation Pool Service : Deleted IdPool name {}", poolName);
212 LOG.error("DHCP Allocation Pool Service : Unable to delete IdPool name {}", poolName);
214 } catch (InterruptedException | ExecutionException e) {
215 LOG.error("Failed to delete Pool for DHCP Allocation Pool Service", e);