protected IPAssignment allocateIp(IpAddress ipAddress, Network network) {
Instance instance = getInstanceForPrimaryIp(ipAddress);
IPAssignment ip = null;
String requestedIp = null;
if (instance != null) {
String allocatedIpAddress = DataAccessor
.fieldString(instance, InstanceConstants.FIELD_ALLOCATED_IP_ADDRESS);
if (allocatedIpAddress != null) {
ip = new IPAssignment(allocatedIpAddress, null);
}
requestedIp = DataAccessor.fieldString(instance, InstanceConstants.FIELD_REQUESTED_IP_ADDRESS);
}
if (ip == null) {
List<String> list = new ArrayList<>();
if (!StringUtils.isEmpty(requestedIp)) {
list = Arrays.asList(StringUtils.stripAll(StringUtils.split(requestedIp, ",")));
}
ip = networkService.assignIpAddress(network, ipAddress, list);
if (ip == null) {
objectProcessManager.scheduleStandardProcess(StandardProcess.DEACTIVATE, ipAddress, null);
throw new ResourceExhaustionException("IP allocation error", "Failed to allocate IP from subnet", ipAddress);
}
}
return ip;
}