*/
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;
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.genius.mdsalutil.MDSALDataStoreUtils;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
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;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
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;
}
}
+ @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) {
releaseIdAllocation(poolIdKey, macAddress);
}
- public AllocationPool getAllocationPoolByNetwork(String networkId) {
+ @Nullable
+ public AllocationPool getAllocationPoolByNetwork(String networkId) throws ExecutionException, InterruptedException {
InstanceIdentifier<Network> network = InstanceIdentifier.builder(DhcpAllocationPool.class)
.child(Network.class, new NetworkKey(networkId)).build();
- Optional<Network> optionalNetworkConfData = MDSALDataStoreUtils.read(dataBroker,
+ Optional<Network> optionalNetworkConfData = SingleTransactionDataBroker.syncReadOptional(dataBroker,
LogicalDatastoreType.CONFIGURATION, network);
if (!optionalNetworkConfData.isPresent()) {
LOG.info("No network configuration data for network {}", networkId);
}
}
- public Map<BigInteger, List<String>> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) {
+ @NonNull
+ public Map<Uint64, List<String>> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) {
InstanceIdentifier<ElanDpnInterfacesList> elanDpnIfacesIid = InstanceIdentifier.builder(ElanDpnInterfaces.class)
.child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
- Optional<ElanDpnInterfacesList> elanDpnIfacesOpc = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
- elanDpnIfacesIid);
+ Optional<ElanDpnInterfacesList> 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));
- }
-
- public AllocationPool getAllocationPoolByPort(String portUuid) {
- String elanInstanceName = getNetworkByPort(portUuid);
- AllocationPool allocPool = elanInstanceName != null ? getAllocationPoolByNetwork(elanInstanceName) : null;
- return allocPool;
+ return elanDpnIfacesOpc.get().nonnullDpnInterfaces().stream()
+ .collect(Collectors.toMap(DpnInterfaces::getDpId,
+ value -> value.getInterfaces() != null ? value.getInterfaces() : Collections.emptyList()));
}
- public String getNetworkByPort(String portUuid) {
+ @Nullable
+ public String getNetworkByPort(String portUuid) throws ExecutionException, InterruptedException {
InstanceIdentifier<ElanInterface> elanInterfaceName = InstanceIdentifier.builder(ElanInterfaces.class)
.child(ElanInterface.class, new ElanInterfaceKey(portUuid)).build();
- Optional<ElanInterface> optionalElanInterface = MDSALDataStoreUtils.read(dataBroker,
+ Optional<ElanInterface> optionalElanInterface = SingleTransactionDataBroker.syncReadOptional(dataBroker,
LogicalDatastoreType.CONFIGURATION, elanInterfaceName);
if (!optionalElanInterface.isPresent()) {
LOG.info("No elan interface data for port {}", portUuid);
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) {
try {
Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
RpcResult<AllocateIdOutput> 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);
}
CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(poolName).setLow(low).setHigh(high)
.build();
try {
- Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
+ Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
if (result != null && result.get().isSuccessful()) {
LOG.info("DHCP Allocation Pool Service : Created IdPool name {}", poolName);
} else {
String poolName = getPoolKeyIdByAllocationPool(networkId, pool);
DeleteIdPoolInput deletePool = new DeleteIdPoolInputBuilder().setPoolName(poolName).build();
try {
- Future<RpcResult<Void>> result = idManager.deleteIdPool(deletePool);
+ Future<RpcResult<DeleteIdPoolOutput>> result = idManager.deleteIdPool(deletePool);
if (result != null && result.get().isSuccessful()) {
LOG.info("DHCP Allocation Pool Service : Deleted IdPool name {}", poolName);
} else {