Search in sources :

Example 1 with InsufficientAddressCapacityException

use of com.cloud.exception.InsufficientAddressCapacityException in project CloudStack-archive by CloudStack-extras.

the class CreateLoadBalancerRuleCmd method create.

@Override
public void create() {
    //cidr list parameter is deprecated
    if (cidrlist != null) {
        throw new InvalidParameterValueException("Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command");
    }
    try {
        LoadBalancer result = _lbService.createLoadBalancerRule(this, getOpenFirewall());
        this.setEntityId(result.getId());
    } catch (NetworkRuleConflictException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, e.getMessage());
    } catch (InsufficientAddressCapacityException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, e.getMessage());
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) LoadBalancer(com.cloud.network.rules.LoadBalancer) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException)

Example 2 with InsufficientAddressCapacityException

use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.

the class NetworkServiceImpl method allocateSecondaryGuestIP.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_SECONDARY_IP_ASSIGN, eventDescription = "assigning secondary ip to nic", create = true)
public NicSecondaryIp allocateSecondaryGuestIP(final long nicId, String requestedIp) throws InsufficientAddressCapacityException {
    Account caller = CallContext.current().getCallingAccount();
    //check whether the nic belongs to user vm.
    final NicVO nicVO = _nicDao.findById(nicId);
    if (nicVO == null) {
        throw new InvalidParameterValueException("There is no nic for the " + nicId);
    }
    if (nicVO.getVmType() != VirtualMachine.Type.User) {
        throw new InvalidParameterValueException("The nic is not belongs to user vm");
    }
    VirtualMachine vm = _userVmDao.findById(nicVO.getInstanceId());
    if (vm == null) {
        throw new InvalidParameterValueException("There is no vm with the nic");
    }
    final long networkId = nicVO.getNetworkId();
    final Account ipOwner = _accountMgr.getAccount(vm.getAccountId());
    // verify permissions
    _accountMgr.checkAccess(caller, null, true, vm);
    Network network = _networksDao.findById(networkId);
    if (network == null) {
        throw new InvalidParameterValueException("Invalid network id is given");
    }
    int maxAllowedIpsPerNic = NumbersUtil.parseInt(_configDao.getValue(Config.MaxNumberOfSecondaryIPsPerNIC.key()), 10);
    Long nicWiseIpCount = _nicSecondaryIpDao.countByNicId(nicId);
    if (nicWiseIpCount.intValue() >= maxAllowedIpsPerNic) {
        s_logger.error("Maximum Number of Ips \"vm.network.nic.max.secondary.ipaddresses = \"" + maxAllowedIpsPerNic + " per Nic has been crossed for the nic " + nicId + ".");
        throw new InsufficientAddressCapacityException("Maximum Number of Ips per Nic has been crossed.", Nic.class, nicId);
    }
    s_logger.debug("Calling the ip allocation ...");
    String ipaddr = null;
    //Isolated network can exist in Basic zone only, so no need to verify the zone type
    if (network.getGuestType() == Network.GuestType.Isolated) {
        try {
            ipaddr = _ipAddrMgr.allocateGuestIP(network, requestedIp);
        } catch (InsufficientAddressCapacityException e) {
            throw new InvalidParameterValueException("Allocating guest ip for nic failed");
        }
    } else if (network.getGuestType() == Network.GuestType.Shared) {
        //for basic zone, need to provide the podId to ensure proper ip alloation
        Long podId = null;
        DataCenter dc = _dcDao.findById(network.getDataCenterId());
        if (dc.getNetworkType() == NetworkType.Basic) {
            VMInstanceVO vmi = (VMInstanceVO) vm;
            podId = vmi.getPodIdToDeployIn();
            if (podId == null) {
                throw new InvalidParameterValueException("vm pod id is null in Basic zone; can't decide the range for ip allocation");
            }
        }
        try {
            ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, requestedIp);
            if (ipaddr == null) {
                throw new InvalidParameterValueException("Allocating ip to guest nic " + nicId + " failed");
            }
        } catch (InsufficientAddressCapacityException e) {
            s_logger.error("Allocating ip to guest nic " + nicId + " failed");
            return null;
        }
    } else {
        s_logger.error("AddIpToVMNic is not supported in this network...");
        return null;
    }
    if (ipaddr != null) {
        // we got the ip addr so up the nics table and secodary ip
        final String addrFinal = ipaddr;
        long id = Transaction.execute(new TransactionCallback<Long>() {

            @Override
            public Long doInTransaction(TransactionStatus status) {
                boolean nicSecondaryIpSet = nicVO.getSecondaryIp();
                if (!nicSecondaryIpSet) {
                    nicVO.setSecondaryIp(true);
                    // commit when previously set ??
                    s_logger.debug("Setting nics table ...");
                    _nicDao.update(nicId, nicVO);
                }
                s_logger.debug("Setting nic_secondary_ip table ...");
                Long vmId = nicVO.getInstanceId();
                NicSecondaryIpVO secondaryIpVO = new NicSecondaryIpVO(nicId, addrFinal, vmId, ipOwner.getId(), ipOwner.getDomainId(), networkId);
                _nicSecondaryIpDao.persist(secondaryIpVO);
                return secondaryIpVO.getId();
            }
        });
        return getNicSecondaryIp(id);
    } else {
        return null;
    }
}
Also used : Account(com.cloud.user.Account) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) VMInstanceVO(com.cloud.vm.VMInstanceVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NicVO(com.cloud.vm.NicVO) VirtualMachine(com.cloud.vm.VirtualMachine) ActionEvent(com.cloud.event.ActionEvent)

Example 3 with InsufficientAddressCapacityException

use of com.cloud.exception.InsufficientAddressCapacityException in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method updateNicIpForVirtualMachine.

@Override
public UserVm updateNicIpForVirtualMachine(final UpdateVmNicIpCmd cmd) {
    final Long nicId = cmd.getNicId();
    String ipaddr = cmd.getIpaddress();
    final Account caller = CallContext.current().getCallingAccount();
    // check whether the nic belongs to user vm.
    final NicVO nicVO = _nicDao.findById(nicId);
    if (nicVO == null) {
        throw new InvalidParameterValueException("There is no nic for the " + nicId);
    }
    if (nicVO.getVmType() != VirtualMachine.Type.User) {
        throw new InvalidParameterValueException("The nic is not belongs to user vm");
    }
    final UserVm vm = _vmDao.findById(nicVO.getInstanceId());
    if (vm == null) {
        throw new InvalidParameterValueException("There is no vm with the nic");
    }
    final Network network = _networkDao.findById(nicVO.getNetworkId());
    if (network == null) {
        throw new InvalidParameterValueException("There is no network with the nic");
    }
    // Don't allow to update vm nic ip if network is not in Implemented/Setup/Allocated state
    if (!(network.getState() == Network.State.Allocated || network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup)) {
        throw new InvalidParameterValueException("Network is not in the right state to update vm nic ip. Correct states are: " + Network.State.Allocated + ", " + Network.State.Implemented + ", " + Network.State.Setup);
    }
    final NetworkOfferingVO offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
    if (offering == null) {
        throw new InvalidParameterValueException("There is no network offering with the network");
    }
    if (!_networkModel.listNetworkOfferingServices(offering.getId()).isEmpty() && vm.getState() != State.Stopped) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Stopped, unable to update the vm nic having the specified id");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    // verify permissions
    _accountMgr.checkAccess(caller, null, true, vm);
    final Account ipOwner = _accountDao.findByIdIncludingRemoved(vm.getAccountId());
    // verify ip address
    s_logger.debug("Calling the ip allocation ...");
    final Zone zone = zoneRepository.findOne(network.getDataCenterId());
    if (zone == null) {
        throw new InvalidParameterValueException("There is no dc with the nic");
    }
    if (zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Isolated) {
        try {
            ipaddr = _ipAddrMgr.allocateGuestIP(network, ipaddr);
        } catch (final InsufficientAddressCapacityException e) {
            throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity");
        }
        if (ipaddr == null) {
            throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
        }
        if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.StaticNat)) {
            final IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmId(vm.getId());
            if (oldIP != null) {
                oldIP.setVmIp(ipaddr);
                _ipAddressDao.persist(oldIP);
            }
        }
        // implementing the network elements and resources as a part of vm nic ip update if network has services and it is in Implemented state
        if (!_networkModel.listNetworkOfferingServices(offering.getId()).isEmpty() && network.getState() == Network.State.Implemented) {
            final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
            final ReservationContext context = new ReservationContextImpl(null, null, callerUser, caller);
            final DeployDestination dest = new DeployDestination(zoneRepository.findOne(network.getDataCenterId()), null, null, null);
            s_logger.debug("Implementing the network " + network + " elements and resources as a part of vm nic ip update");
            try {
                // implement the network elements and rules again
                _networkMgr.implementNetworkElementsAndResources(dest, context, network, offering);
            } catch (final Exception ex) {
                s_logger.warn("Failed to implement network " + network + " elements and resources as a part of vm nic ip update due to ", ex);
                final CloudRuntimeException e = new CloudRuntimeException("Failed to implement network (with specified id) elements and resources as a part of vm nic ip " + "update");
                e.addProxyObject(network.getUuid(), "networkId");
                // restore to old ip address
                if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.StaticNat)) {
                    final IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmId(vm.getId());
                    if (oldIP != null) {
                        oldIP.setVmIp(nicVO.getIPv4Address());
                        _ipAddressDao.persist(oldIP);
                    }
                }
                throw e;
            }
        }
    } else if (zone.getNetworkType() == NetworkType.Basic || network.getGuestType() == Network.GuestType.Shared) {
        // handle the basic networks here
        // for basic zone, need to provide the podId to ensure proper ip alloation
        Long podId = null;
        if (zone.getNetworkType() == NetworkType.Basic) {
            podId = vm.getPodIdToDeployIn();
            if (podId == null) {
                throw new InvalidParameterValueException("vm pod id is null in Basic zone; can't decide the range for ip allocation");
            }
        }
        try {
            ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, ipaddr);
            if (ipaddr == null) {
                throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
            }
            final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nicVO.getNetworkId(), nicVO.getIPv4Address());
            if (ip != null) {
                Transaction.execute(new TransactionCallbackNoReturn() {

                    @Override
                    public void doInTransactionWithoutResult(final TransactionStatus status) {
                        _ipAddrMgr.markIpAsUnavailable(ip.getId());
                        _ipAddressDao.unassignIpAddress(ip.getId());
                    }
                });
            }
        } catch (final InsufficientAddressCapacityException e) {
            s_logger.error("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity");
            return null;
        }
    } else {
        s_logger.error("UpdateVmNicIpCmd is not supported in this network...");
        return null;
    }
    // update nic ipaddress
    nicVO.setIPv4Address(ipaddr);
    _nicDao.persist(nicVO);
    return vm;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) Zone(com.cloud.db.model.Zone) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) UserVm(com.cloud.uservm.UserVm) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 4 with InsufficientAddressCapacityException

use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.

the class DhcpSubNetRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
    final UserVmVO vm = userVmDao.findById(_profile.getId());
    userVmDao.loadDetails(vm);
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    // check if this is not the primary subnet.
    final NicVO domrGuestNic = nicDao.findByInstanceIdAndIpAddressAndVmtype(_router.getId(), nicDao.getIpAddress(_nic.getNetworkId(), _router.getId()), VirtualMachine.Type.DomainRouter);
    // networks.
    if (!NetUtils.sameSubnet(domrGuestNic.getIPv4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
        final NicIpAliasDao nicIpAliasDao = visitor.getVirtualNetworkApplianceFactory().getNicIpAliasDao();
        final List<NicIpAliasVO> aliasIps = nicIpAliasDao.listByNetworkIdAndState(domrGuestNic.getNetworkId(), NicIpAlias.State.active);
        boolean ipInVmsubnet = false;
        for (final NicIpAliasVO alias : aliasIps) {
            // check if any of the alias ips belongs to the Vm's subnet.
            if (NetUtils.sameSubnet(alias.getIp4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
                ipInVmsubnet = true;
                break;
            }
        }
        PublicIp routerPublicIP = null;
        DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
        final DataCenter dc = dcDao.findById(_router.getDataCenterId());
        if (ipInVmsubnet == false) {
            try {
                if (_network.getTrafficType() == TrafficType.Guest && _network.getGuestType() == GuestType.Shared) {
                    HostPodDao podDao = visitor.getVirtualNetworkApplianceFactory().getPodDao();
                    podDao.findById(vm.getPodIdToDeployIn());
                    final Account caller = CallContext.current().getCallingAccount();
                    VlanDao vlanDao = visitor.getVirtualNetworkApplianceFactory().getVlanDao();
                    final List<VlanVO> vlanList = vlanDao.listVlansByNetworkIdAndGateway(_network.getId(), _nic.getIPv4Gateway());
                    final List<Long> vlanDbIdList = new ArrayList<Long>();
                    for (final VlanVO vlan : vlanList) {
                        vlanDbIdList.add(vlan.getId());
                    }
                    IpAddressManager ipAddrMgr = visitor.getVirtualNetworkApplianceFactory().getIpAddrMgr();
                    if (dc.getNetworkType() == NetworkType.Basic) {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), vm.getPodIdToDeployIn(), caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, _nic.getIPv4Gateway(), false);
                    } else {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, _nic.getIPv4Gateway(), false);
                    }
                    _routerAliasIp = routerPublicIP.getAddress().addr();
                }
            } catch (final InsufficientAddressCapacityException e) {
                s_logger.info(e.getMessage());
                s_logger.info("unable to configure dhcp for this VM.");
                return false;
            }
            // this means we did not create an IP alias on the router.
            _nicAlias = new NicIpAliasVO(domrGuestNic.getId(), _routerAliasIp, _router.getId(), CallContext.current().getCallingAccountId(), _network.getDomainId(), _nic.getNetworkId(), _nic.getIPv4Gateway(), _nic.getIPv4Netmask());
            _nicAlias.setAliasCount(routerPublicIP.getIpMacAddress());
            nicIpAliasDao.persist(_nicAlias);
            final boolean result = visitor.visit(this);
            if (result == false) {
                final NicIpAliasVO ipAliasVO = nicIpAliasDao.findByInstanceIdAndNetworkId(_network.getId(), _router.getId());
                final PublicIp routerPublicIPFinal = routerPublicIP;
                Transaction.execute(new TransactionCallbackNoReturn() {

                    @Override
                    public void doInTransactionWithoutResult(final TransactionStatus status) {
                        nicIpAliasDao.expunge(ipAliasVO.getId());
                        IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
                        ipAddressDao.unassignIpAddress(routerPublicIPFinal.getId());
                    }
                });
                throw new CloudRuntimeException("failed to configure ip alias on the router as a part of dhcp config");
            }
        }
        return true;
    }
    return true;
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) NicDao(com.cloud.vm.dao.NicDao) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) NicIpAliasDao(com.cloud.vm.dao.NicIpAliasDao) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UserVmDao(com.cloud.vm.dao.UserVmDao) VlanVO(com.cloud.dc.VlanVO) NicVO(com.cloud.vm.NicVO) VlanDao(com.cloud.dc.dao.VlanDao) PublicIp(com.cloud.network.addr.PublicIp) IPAddressDao(com.cloud.network.dao.IPAddressDao) DataCenterDao(com.cloud.dc.dao.DataCenterDao) HostPodDao(com.cloud.dc.dao.HostPodDao) IpAddressManager(com.cloud.network.IpAddressManager) DataCenter(com.cloud.dc.DataCenter)

Example 5 with InsufficientAddressCapacityException

use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.

the class DirectNetworkGuru method allocate.

@Override
public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
    DataCenter dc = _dcDao.findById(network.getDataCenterId());
    if (nic == null) {
        nic = new NicProfile(ReservationStrategy.Create, null, null, null, null);
    } else if (nic.getIPv4Address() == null && nic.getIPv6Address() == null) {
        nic.setReservationStrategy(ReservationStrategy.Start);
    } else {
        nic.setReservationStrategy(ReservationStrategy.Create);
    }
    allocateDirectIp(nic, network, vm, dc, nic.getRequestedIPv4(), nic.getRequestedIPv6());
    nic.setReservationStrategy(ReservationStrategy.Create);
    if (nic.getMacAddress() == null) {
        nic.setMacAddress(_networkModel.getNextAvailableMacAddressInNetwork(network.getId()));
        if (nic.getMacAddress() == null) {
            throw new InsufficientAddressCapacityException("Unable to allocate more mac addresses", Network.class, network.getId());
        }
    }
    return nic;
}
Also used : DataCenter(com.cloud.dc.DataCenter) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) NicProfile(com.cloud.vm.NicProfile)

Aggregations

InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)60 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)22 IPAddressVO (com.cloud.network.dao.IPAddressVO)16 TransactionStatus (com.cloud.utils.db.TransactionStatus)15 Account (com.cloud.user.Account)14 DataCenter (com.cloud.dc.DataCenter)13 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)12 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)12 NicProfile (com.cloud.vm.NicProfile)12 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)11 DB (com.cloud.utils.db.DB)11 VlanVO (com.cloud.dc.VlanVO)9 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)9 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)8 PublicIp (com.cloud.network.addr.PublicIp)8 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)8 NicVO (com.cloud.vm.NicVO)8 ArrayList (java.util.ArrayList)8 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)7 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)7