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) { ip = networkService.assignIpAddress(network, ipAddress, requestedIp); if (ip == null) { objectProcessManager.scheduleStandardProcess(StandardProcess.DEACTIVATE, ipAddress, null); throw new ResourceExhaustionException( "IP allocation error", "Failed to allocate IP from subnet", ipAddress); } } return ip; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { IpAddress ipAddress = (IpAddress) state.getResource(); Network network = objectManager.loadResource(Network.class, ipAddress.getNetworkId()); if (!networkService.shouldAssignIpAddress(network)) { return null; } String assignedIp = ipAddress.getAddress(); Long subnetId = ipAddress.getSubnetId(); IPAssignment assignment = allocateIp(ipAddress, network); if (assignment != null) { assignedIp = assignment.getIpAddress(); if (assignment.getSubnet() != null) { subnetId = assignment.getSubnet().getId(); } } return new HandlerResult( IP_ADDRESS.ADDRESS, assignedIp, IP_ADDRESS.SUBNET_ID, subnetId, IP_ADDRESS.NAME, StringUtils.isBlank(ipAddress.getName()) ? assignedIp : ipAddress.getName()); }