use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class AddIpToVmNicTest method testCreateFailure.
@Test
public void testCreateFailure() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
NetworkService networkService = Mockito.mock(NetworkService.class);
AddIpToVmNicCmd ipTonicCmd = Mockito.mock(AddIpToVmNicCmd.class);
Mockito.when(networkService.allocateSecondaryGuestIP(Matchers.anyLong(), Matchers.any())).thenReturn(null);
ipTonicCmd._networkService = networkService;
try {
ipTonicCmd.execute();
} catch (InsufficientAddressCapacityException e) {
throw new InvalidParameterValueException("Allocating guest ip for nic failed");
}
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class NetworkOrchestrator method allocate.
@Override
@DB
public void allocate(final VirtualMachineProfile vm, final LinkedHashMap<? extends Network, List<? extends NicProfile>> networks, final Map<String, Map<Integer, String>> extraDhcpOptions) throws InsufficientCapacityException, ConcurrentOperationException {
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientCapacityException>() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) throws InsufficientCapacityException {
if (s_logger.isTraceEnabled()) {
s_logger.trace(String.format("allocating networks for %s(template %s); %d networks", vm.getInstanceName(), vm.getTemplate().getUuid(), networks.size()));
}
int deviceId = 0;
int size;
size = determineNumberOfNicsRequired();
final boolean[] deviceIds = new boolean[size];
Arrays.fill(deviceIds, false);
List<Pair<Network, NicProfile>> profilesList = getOrderedNetworkNicProfileMapping(networks);
final List<NicProfile> nics = new ArrayList<NicProfile>(size);
NicProfile defaultNic = null;
Network nextNetwork = null;
for (Pair<Network, NicProfile> networkNicPair : profilesList) {
nextNetwork = networkNicPair.first();
Pair<NicProfile, Integer> newDeviceInfo = addRequestedNicToNicListWithDeviceNumberAndRetrieveDefaultDevice(networkNicPair.second(), deviceIds, deviceId, nextNetwork, nics, defaultNic);
defaultNic = newDeviceInfo.first();
deviceId = newDeviceInfo.second();
}
createExtraNics(size, nics, nextNetwork);
if (nics.size() == 1) {
nics.get(0).setDefaultNic(true);
}
}
/**
* private transaction method to check and add devices to the nic list and update the info
*/
Pair<NicProfile, Integer> addRequestedNicToNicListWithDeviceNumberAndRetrieveDefaultDevice(NicProfile requested, boolean[] deviceIds, int deviceId, Network nextNetwork, List<NicProfile> nics, NicProfile defaultNic) throws InsufficientAddressCapacityException, InsufficientVirtualNetworkCapacityException {
Pair<NicProfile, Integer> rc = new Pair<>(null, null);
Boolean isDefaultNic = false;
if (vm != null && requested != null && requested.isDefaultNic()) {
isDefaultNic = true;
}
while (deviceIds[deviceId] && deviceId < deviceIds.length) {
deviceId++;
}
final Pair<NicProfile, Integer> vmNicPair = allocateNic(requested, nextNetwork, isDefaultNic, deviceId, vm);
NicProfile vmNic = null;
if (vmNicPair != null) {
vmNic = vmNicPair.first();
if (vmNic == null) {
return rc;
}
deviceId = vmNicPair.second();
}
final int devId = vmNic.getDeviceId();
if (devId >= deviceIds.length) {
throw new IllegalArgumentException("Device id for nic is too large: " + vmNic);
}
if (deviceIds[devId]) {
throw new IllegalArgumentException("Conflicting device id for two different nics: " + vmNic);
}
deviceIds[devId] = true;
if (vmNic.isDefaultNic()) {
if (defaultNic != null) {
throw new IllegalArgumentException("You cannot specify two nics as default nics: nic 1 = " + defaultNic + "; nic 2 = " + vmNic);
}
defaultNic = vmNic;
}
nics.add(vmNic);
vm.addNic(vmNic);
saveExtraDhcpOptions(nextNetwork.getUuid(), vmNic.getId(), extraDhcpOptions);
rc.first(defaultNic);
rc.second(deviceId);
return rc;
}
/**
* private transaction method to get oredered list of Network and NicProfile pair
* @return ordered list of Network and NicProfile pair
* @param networks the map od networks to nic profiles list
*/
private List<Pair<Network, NicProfile>> getOrderedNetworkNicProfileMapping(final LinkedHashMap<? extends Network, List<? extends NicProfile>> networks) {
List<Pair<Network, NicProfile>> profilesList = new ArrayList<>();
for (final Map.Entry<? extends Network, List<? extends NicProfile>> network : networks.entrySet()) {
List<? extends NicProfile> requestedProfiles = network.getValue();
if (requestedProfiles == null) {
requestedProfiles = new ArrayList<NicProfile>();
}
if (requestedProfiles.isEmpty()) {
requestedProfiles.add(null);
}
for (final NicProfile requested : requestedProfiles) {
profilesList.add(new Pair<Network, NicProfile>(network.getKey(), requested));
}
}
profilesList.sort(new Comparator<Pair<Network, NicProfile>>() {
@Override
public int compare(Pair<Network, NicProfile> pair1, Pair<Network, NicProfile> pair2) {
int profile1Order = Integer.MAX_VALUE;
int profile2Order = Integer.MAX_VALUE;
if (pair1 != null && pair1.second() != null && pair1.second().getOrderIndex() != null) {
profile1Order = pair1.second().getOrderIndex();
}
if (pair2 != null && pair2.second() != null && pair2.second().getOrderIndex() != null) {
profile2Order = pair2.second().getOrderIndex();
}
return profile1Order - profile2Order;
}
});
return profilesList;
}
/**
* private transaction method to run over the objects and determine nic requirements
* @return the total numer of nics required
*/
private int determineNumberOfNicsRequired() {
int size = 0;
for (final Network ntwk : networks.keySet()) {
final List<? extends NicProfile> profiles = networks.get(ntwk);
if (profiles != null && !profiles.isEmpty()) {
size = size + profiles.size();
} else {
size = size + 1;
}
}
List<OVFNetworkTO> netprereqs = templateDeployAsIsDetailsDao.listNetworkRequirementsByTemplateId(vm.getTemplate().getId());
if (size < netprereqs.size()) {
size = netprereqs.size();
}
return size;
}
/**
* private transaction method to add nics as required
* @param size the number needed
* @param nics the list of nics present
* @param finalNetwork the network to add the nics to
* @throws InsufficientVirtualNetworkCapacityException great
* @throws InsufficientAddressCapacityException also magnificent, as the name sugests
*/
private void createExtraNics(int size, List<NicProfile> nics, Network finalNetwork) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
if (nics.size() != size) {
s_logger.warn("Number of nics " + nics.size() + " doesn't match number of requested nics " + size);
if (nics.size() > size) {
throw new CloudRuntimeException("Number of nics " + nics.size() + " doesn't match number of requested networks " + size);
} else {
if (finalNetwork == null) {
throw new CloudRuntimeException(String.format("can not assign network to %d remaining required NICs", size - nics.size()));
}
// create extra
for (int extraNicNum = nics.size(); extraNicNum < size; extraNicNum++) {
final Pair<NicProfile, Integer> vmNicPair = allocateNic(new NicProfile(), finalNetwork, false, extraNicNum, vm);
}
}
}
}
});
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class NetworkOrchestrator method configureNicProfileBasedOnRequestedIp.
/**
* If the requested IPv4 address from the NicProfile was configured then it configures the IPv4 address, Netmask and Gateway to deploy the VM with the requested IP.
*/
protected void configureNicProfileBasedOnRequestedIp(NicProfile requestedNicProfile, NicProfile nicProfile, Network network) {
if (requestedNicProfile == null) {
return;
}
String requestedIpv4Address = requestedNicProfile.getRequestedIPv4();
if (requestedIpv4Address == null) {
return;
}
if (!NetUtils.isValidIp4(requestedIpv4Address)) {
throw new InvalidParameterValueException(String.format("The requested [IPv4 address='%s'] is not a valid IP address", requestedIpv4Address));
}
VlanVO vlanVo = _vlanDao.findByNetworkIdAndIpv4(network.getId(), requestedIpv4Address);
if (vlanVo == null) {
throw new InvalidParameterValueException(String.format("Trying to configure a Nic with the requested [IPv4='%s'] but cannot find a Vlan for the [network id='%s']", requestedIpv4Address, network.getId()));
}
String ipv4Gateway = vlanVo.getVlanGateway();
String ipv4Netmask = vlanVo.getVlanNetmask();
if (!NetUtils.isValidIp4(ipv4Gateway)) {
throw new InvalidParameterValueException(String.format("The [IPv4Gateway='%s'] from [VlanId='%s'] is not valid", ipv4Gateway, vlanVo.getId()));
}
if (!NetUtils.isValidIp4Netmask(ipv4Netmask)) {
throw new InvalidParameterValueException(String.format("The [IPv4Netmask='%s'] from [VlanId='%s'] is not valid", ipv4Netmask, vlanVo.getId()));
}
acquireLockAndCheckIfIpv4IsFree(network, requestedIpv4Address);
nicProfile.setIPv4Address(requestedIpv4Address);
nicProfile.setIPv4Gateway(ipv4Gateway);
nicProfile.setIPv4Netmask(ipv4Netmask);
if (nicProfile.getMacAddress() == null) {
try {
String macAddress = _networkModel.getNextAvailableMacAddressInNetwork(network.getId());
nicProfile.setMacAddress(macAddress);
} catch (InsufficientAddressCapacityException e) {
throw new CloudRuntimeException(String.format("Cannot get next available mac address in [network id='%s']", network.getId()), e);
}
}
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class ContrailGuru method reserve.
/**
* Allocate the ip address (and mac) for the specified VM device.
*/
@Override
public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
s_logger.debug("reserve NicProfile on network id: " + network.getId() + " " + network.getName());
s_logger.debug("deviceId: " + nic.getDeviceId());
NicVO nicVO = _nicDao.findById(nic.getId());
assert nicVO != null;
VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(network.getUuid(), _manager.getCanonicalName(network), network.getTrafficType());
/* Network must have been implemented */
assert vnModel != null;
VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
if (vmModel == null) {
VMInstanceVO vmVo = (VMInstanceVO) vm.getVirtualMachine();
vmModel = new VirtualMachineModel(vmVo, vm.getUuid());
vmModel.setProperties(_manager.getModelController(), vmVo);
}
VMInterfaceModel vmiModel = vmModel.getVMInterface(nicVO.getUuid());
if (vmiModel == null) {
vmiModel = new VMInterfaceModel(nicVO.getUuid());
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
}
try {
vmiModel.build(_manager.getModelController(), (VMInstanceVO) vm.getVirtualMachine(), nicVO);
vmiModel.setActive();
} catch (IOException ex) {
s_logger.error("virtual-machine-interface set", ex);
return;
}
InstanceIpModel ipModel = vmiModel.getInstanceIp();
if (ipModel == null) {
ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
ipModel.addToVMInterface(vmiModel);
} else {
s_logger.debug("Reuse existing instance-ip object on " + ipModel.getName());
}
if (nic.getIPv4Address() != null) {
s_logger.debug("Nic using existing IP address " + nic.getIPv4Address());
ipModel.setAddress(nic.getIPv4Address());
}
try {
vmModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("virtual-machine update", ex);
return;
}
_manager.getDatabase().getVirtualMachines().add(vmModel);
VirtualMachineInterface vmi = vmiModel.getVMInterface();
// allocate mac address
if (nic.getMacAddress() == null) {
MacAddressesType macs = vmi.getMacAddresses();
if (macs == null) {
s_logger.debug("no mac address is allocated for Nic " + nicVO.getUuid());
} else {
s_logger.info("VMI " + _manager.getVifNameByVmUuid(vm.getUuid(), nicVO.getDeviceId()) + " got mac address: " + macs.getMacAddress().get(0));
nic.setMacAddress(macs.getMacAddress().get(0));
}
}
if (nic.getIPv4Address() == null) {
s_logger.debug("Allocated IP address " + ipModel.getAddress());
nic.setIPv4Address(ipModel.getAddress());
if (network.getCidr() != null) {
nic.setIPv4Netmask(NetUtils.cidr2Netmask(network.getCidr()));
}
nic.setIPv4Gateway(network.getGateway());
nic.setFormat(AddressFormat.Ip4);
}
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class ControlNetworkGuru method reserve.
@Override
public void reserve(NicProfile nic, Network config, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
assert nic.getTrafficType() == TrafficType.Control;
// we have to get management/private ip for the control nic for vmware/hyperv due ssh issues.
HypervisorType hType = vm.getHypervisorType();
if (((hType == HypervisorType.VMware) || (hType == HypervisorType.Hyperv)) && isRouterVm(vm)) {
super.reserve(nic, config, vm, dest, context);
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
nic.setMacAddress(mac);
return;
}
String ip = _dcDao.allocateLinkLocalIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId(), context.getReservationId());
if (ip == null) {
throw new InsufficientAddressCapacityException("Insufficient link local address capacity", DataCenter.class, dest.getDataCenter().getId());
}
String netmask = NetUtils.cidr2Netmask(_cidr);
s_logger.debug(String.format("Reserved NIC for %s [ipv4:%s netmask:%s gateway:%s]", vm.getInstanceName(), ip, netmask, _gateway));
nic.setIPv4Address(ip);
nic.setMacAddress(NetUtils.long2Mac(NetUtils.ip2Long(ip) | (14l << 40)));
nic.setIPv4Netmask(netmask);
nic.setFormat(AddressFormat.Ip4);
nic.setIPv4Gateway(_gateway);
}
Aggregations