Search in sources :

Example 6 with NicIpAliasVO

use of com.cloud.vm.dao.NicIpAliasVO in project cloudstack by apache.

the class ConfigurationManagerImpl method deleteVlanAndPublicIpRange.

@Override
@DB
public boolean deleteVlanAndPublicIpRange(final long userId, final long vlanDbId, final Account caller) {
    VlanVO vlanRange = _vlanDao.findById(vlanDbId);
    if (vlanRange == null) {
        throw new InvalidParameterValueException("Please specify a valid IP range id.");
    }
    boolean isAccountSpecific = false;
    final List<AccountVlanMapVO> acctVln = _accountVlanMapDao.listAccountVlanMapsByVlan(vlanRange.getId());
    // account_vlan_map.
    if (acctVln != null && !acctVln.isEmpty()) {
        isAccountSpecific = true;
    }
    boolean isDomainSpecific = false;
    List<DomainVlanMapVO> domainVlan = _domainVlanMapDao.listDomainVlanMapsByVlan(vlanRange.getId());
    // Check for domain wide pool. It will have an entry for domain_vlan_map.
    if (domainVlan != null && !domainVlan.isEmpty()) {
        isDomainSpecific = true;
    }
    // Check if the VLAN has any allocated public IPs
    final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
    if (isAccountSpecific) {
        int resourceCountToBeDecrement = 0;
        try {
            vlanRange = _vlanDao.acquireInLockTable(vlanDbId, 30);
            if (vlanRange == null) {
                throw new CloudRuntimeException("Unable to acquire vlan configuration: " + vlanDbId);
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("lock vlan " + vlanDbId + " is acquired");
            }
            for (final IPAddressVO ip : ips) {
                boolean success = true;
                if (ip.isOneToOneNat()) {
                    throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip + " belonging to the range is used for static nat purposes. Cleanup the rules first");
                }
                if (ip.isSourceNat()) {
                    throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip + " belonging to the range is a source nat ip for the network id=" + ip.getSourceNetworkId() + ". IP range with the source nat ip address can be removed either as a part of Network, or account removal");
                }
                if (_firewallDao.countRulesByIpId(ip.getId()) > 0) {
                    throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip + " belonging to the range has firewall rules applied. Cleanup the rules first");
                }
                if (ip.getAllocatedTime() != null) {
                    // This means IP is allocated
                    // release public ip address here
                    success = _ipAddrMgr.disassociatePublicIpAddress(ip.getId(), userId, caller);
                }
                if (!success) {
                    s_logger.warn("Some ip addresses failed to be released as a part of vlan " + vlanDbId + " removal");
                } else {
                    resourceCountToBeDecrement++;
                    final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getAccountId(), ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlanRange.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
                }
            }
        } finally {
            _vlanDao.releaseFromLockTable(vlanDbId);
            if (resourceCountToBeDecrement > 0) {
                // Making sure to decrement the count of only success operations above. For any reaason if disassociation fails then this number will vary from original range length.
                _resourceLimitMgr.decrementResourceCount(acctVln.get(0).getAccountId(), ResourceType.public_ip, new Long(resourceCountToBeDecrement));
            }
        }
    } else {
        // !isAccountSpecific
        final NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(vlanRange.getVlanGateway(), vlanRange.getNetworkId(), NicIpAlias.State.active);
        // check if the ipalias belongs to the vlan range being deleted.
        if (ipAlias != null && vlanDbId == _publicIpAddressDao.findByIpAndSourceNetworkId(vlanRange.getNetworkId(), ipAlias.getIp4Address()).getVlanId()) {
            throw new InvalidParameterValueException("Cannot delete vlan range " + vlanDbId + " as " + ipAlias.getIp4Address() + "is being used for providing dhcp service in this subnet. Delete all VMs in this subnet and try again");
        }
        final long allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
        if (allocIpCount > 0) {
            throw new InvalidParameterValueException(allocIpCount + "  Ips are in use. Cannot delete this vlan");
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            _publicIpAddressDao.deletePublicIPRange(vlanDbId);
            s_logger.debug(String.format("Delete Public IP Range (from user_ip_address, where vlan_db_d=%s)", vlanDbId));
            _vlanDao.remove(vlanDbId);
            s_logger.debug(String.format("Mark vlan as Remove vlan (vlan_db_id=%s)", vlanDbId));
            SearchBuilder<PodVlanMapVO> sb = podVlanMapDao.createSearchBuilder();
            sb.and("vlan_db_id", sb.entity().getVlanDbId(), SearchCriteria.Op.EQ);
            SearchCriteria<PodVlanMapVO> sc = sb.create();
            sc.setParameters("vlan_db_id", vlanDbId);
            podVlanMapDao.remove(sc);
            s_logger.debug(String.format("Delete vlan_db_id=%s in pod_vlan_map", vlanDbId));
        }
    });
    return true;
}
Also used : AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) SearchBuilder(com.cloud.utils.db.SearchBuilder) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) SearchCriteria(com.cloud.utils.db.SearchCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) DB(com.cloud.utils.db.DB)

Example 7 with NicIpAliasVO

use of com.cloud.vm.dao.NicIpAliasVO in project cloudstack by apache.

the class BasicNetworkVisitor method visit.

@Override
public boolean visit(final DhcpSubNetRules subnet) throws ResourceUnavailableException {
    final VirtualRouter router = subnet.getRouter();
    final Network network = subnet.getNetwork();
    final NicIpAliasVO nicAlias = subnet.getNicAlias();
    final String routerAliasIp = subnet.getRouterAliasIp();
    final Commands cmds = new Commands(Command.OnError.Stop);
    final List<IpAliasTO> ipaliasTo = new ArrayList<IpAliasTO>();
    ipaliasTo.add(new IpAliasTO(routerAliasIp, nicAlias.getNetmask(), nicAlias.getAliasCount().toString()));
    _commandSetupHelper.createIpAlias(router, ipaliasTo, nicAlias.getNetworkId(), cmds);
    // also add the required configuration to the dnsmasq for supporting
    // dhcp and dns on the new ip.
    _commandSetupHelper.configDnsMasq(router, network, cmds);
    return _networkGeneralHelper.sendCommandsToRouter(router, cmds);
}
Also used : Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) ArrayList(java.util.ArrayList) IpAliasTO(com.cloud.agent.api.routing.IpAliasTO) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) VirtualRouter(com.cloud.network.router.VirtualRouter)

Aggregations

NicIpAliasVO (com.cloud.vm.dao.NicIpAliasVO)7 ArrayList (java.util.ArrayList)5 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)4 TransactionStatus (com.cloud.utils.db.TransactionStatus)4 IpAliasTO (com.cloud.agent.api.routing.IpAliasTO)3 Network (com.cloud.network.Network)3 Commands (com.cloud.agent.manager.Commands)2 VlanVO (com.cloud.dc.VlanVO)2 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)2 PublicIp (com.cloud.network.addr.PublicIp)2 IPAddressVO (com.cloud.network.dao.IPAddressVO)2 DB (com.cloud.utils.db.DB)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 NicVO (com.cloud.vm.NicVO)2 DnsMasqConfigCommand (com.cloud.agent.api.routing.DnsMasqConfigCommand)1 DhcpTO (com.cloud.agent.api.to.DhcpTO)1 AccountVlanMapVO (com.cloud.dc.AccountVlanMapVO)1 DataCenter (com.cloud.dc.DataCenter)1 DataCenterVO (com.cloud.dc.DataCenterVO)1 DomainVlanMapVO (com.cloud.dc.DomainVlanMapVO)1