X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=dhcpservice%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetvirt%2Fdhcpservice%2FDhcpAllocationPoolManager.java;h=15c93189bcb0657ed62419c1a1061f078e2d2f52;hb=2f0569ed75ef8a1fb60f992d19c8bbdf92ff45bf;hp=42e8b29b137020acdf71ad024b3238f77714886e;hpb=e81277ac57d2a80c4b211da5b7865de0af23a080;p=netvirt.git diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java index 42e8b29b13..15c93189bc 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java @@ -7,11 +7,11 @@ */ package org.opendaylight.netvirt.dhcpservice; -import com.google.common.base.Optional; -import java.math.BigInteger; +import java.util.Collections; import java.util.EventListener; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.stream.Collectors; @@ -19,13 +19,13 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Singleton; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker; -import org.opendaylight.genius.mdsalutil.MDSALUtil; import org.opendaylight.infrautils.jobcoordinator.JobCoordinator; import org.opendaylight.infrautils.utils.concurrent.JdkFutures; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder; @@ -53,6 +53,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.Uint64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,13 +94,12 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { } } + @Nullable public IpAddress getIpAllocation(String networkId, AllocationPool pool, String macAddress) { String poolIdKey = getPoolKeyIdByAllocationPool(networkId, pool); long allocatedIpLong = createIdAllocation(poolIdKey, macAddress); LOG.debug("allocated id {} for mac {}, from pool {}", allocatedIpLong, macAddress, poolIdKey); - IpAddress allocatedIpAddress = allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong) - : null; - return allocatedIpAddress; + return allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong) : null; } public void releaseIpAllocation(String networkId, AllocationPool pool, String macAddress) { @@ -108,7 +108,8 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { releaseIdAllocation(poolIdKey, macAddress); } - public AllocationPool getAllocationPoolByNetwork(String networkId) throws ReadFailedException { + @Nullable + public AllocationPool getAllocationPoolByNetwork(String networkId) throws ExecutionException, InterruptedException { InstanceIdentifier network = InstanceIdentifier.builder(DhcpAllocationPool.class) .child(Network.class, new NetworkKey(networkId)).build(); Optional optionalNetworkConfData = SingleTransactionDataBroker.syncReadOptional(dataBroker, @@ -129,21 +130,31 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { } } - public Map> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) { + @NonNull + public Map> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) { InstanceIdentifier elanDpnIfacesIid = InstanceIdentifier.builder(ElanDpnInterfaces.class) .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build(); - Optional elanDpnIfacesOpc = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, - elanDpnIfacesIid); + Optional elanDpnIfacesOpc; + try { + elanDpnIfacesOpc = SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.OPERATIONAL, + elanDpnIfacesIid); + } catch (ExecutionException | InterruptedException e) { + LOG.error("getElanDpnInterfacesByName: Exception while reading the ElanDpnInterfacesList DS for the " + + "elan-instance {}", elanInstanceName, e); + return Collections.emptyMap(); + } if (!elanDpnIfacesOpc.isPresent()) { LOG.warn("Could not find DpnInterfaces for elan {}", elanInstanceName); - return null; + return Collections.emptyMap(); } - return elanDpnIfacesOpc.get().getDpnInterfaces().stream() - .collect(Collectors.toMap(DpnInterfaces::getDpId, DpnInterfaces::getInterfaces)); + return elanDpnIfacesOpc.get().nonnullDpnInterfaces().stream() + .collect(Collectors.toMap(DpnInterfaces::getDpId, + value -> value.getInterfaces() != null ? value.getInterfaces() : Collections.emptyList())); } - public String getNetworkByPort(String portUuid) throws ReadFailedException { + @Nullable + public String getNetworkByPort(String portUuid) throws ExecutionException, InterruptedException { InstanceIdentifier elanInterfaceName = InstanceIdentifier.builder(ElanInterfaces.class) .child(ElanInterface.class, new ElanInterfaceKey(portUuid)).build(); Optional optionalElanInterface = SingleTransactionDataBroker.syncReadOptional(dataBroker, @@ -156,8 +167,8 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { return elanInterface.getElanInstanceName(); } - private String getPoolKeyIdByAllocationPool(String networkId, AllocationPool pool) { - return "dhcpAllocationPool." + networkId + "." + String.valueOf(pool.getSubnet().getValue()); + private static String getPoolKeyIdByAllocationPool(String networkId, AllocationPool pool) { + return "dhcpAllocationPool." + networkId + "." + pool.getSubnet().stringValue(); } private long createIdAllocation(String groupIdKey, String idKey) { @@ -165,7 +176,7 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { try { Future> result = idManager.allocateId(getIdInput); RpcResult rpcResult = result.get(); - return rpcResult.getResult().getIdValue(); + return rpcResult.getResult().getIdValue().toJava(); } catch (NullPointerException | InterruptedException | ExecutionException e) { LOG.trace("Failed to allocate id for DHCP Allocation Pool Service", e); }