Search in sources :

Example 1 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class LoadBalanceRuleHandlerTest method testFindElbVmWithCapacityWhenIpAddrIsNull.

@Test
public void testFindElbVmWithCapacityWhenIpAddrIsNull() throws Exception {
    IPAddressVO ipAddr = null;
    DomainRouterVO actual = loadBalanceRuleHandler.findElbVmWithCapacity(ipAddr);
    assertNull(actual);
}
Also used : IPAddressVO(com.cloud.network.dao.IPAddressVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) Test(org.junit.Test)

Example 2 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class LoadBalanceRuleHandlerTest method testFindElbVmWithCapacityWhenThereAreUnusedElbVmsAndOneMatchesThePodId.

@Test
public void testFindElbVmWithCapacityWhenThereAreUnusedElbVmsAndOneMatchesThePodId() throws Exception {
    Long podId = 1L;
    IPAddressVO ipAddrMock = mock(IPAddressVO.class);
    when(ipAddrMock.getVlanId()).thenReturn(podId);
    PodVlanMapVO podVlanMapVoMock = mock(PodVlanMapVO.class);
    when(podVlanMapVoMock.getPodId()).thenReturn(podId);
    when(podVlanMapDao.listPodVlanMapsByVlan(podId)).thenReturn(podVlanMapVoMock);
    DomainRouterVO unusedElbVmThatMatchesPodId = mock(DomainRouterVO.class);
    when(unusedElbVmThatMatchesPodId.getPodIdToDeployIn()).thenReturn(podId);
    List<DomainRouterVO> unusedElbVms = Arrays.asList(new DomainRouterVO[] { unusedElbVmThatMatchesPodId, mock(DomainRouterVO.class) });
    when(this.elasticLbVmMapDao.listUnusedElbVms()).thenReturn(unusedElbVms);
    DomainRouterVO expected = unusedElbVmThatMatchesPodId;
    DomainRouterVO actual = loadBalanceRuleHandler.findElbVmWithCapacity(ipAddrMock);
    assertNotNull(actual);
    assertEquals(expected, actual);
}
Also used : PodVlanMapVO(com.cloud.dc.PodVlanMapVO) IPAddressVO(com.cloud.network.dao.IPAddressVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) Test(org.junit.Test)

Example 3 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class LoadBalanceRuleHandler method releaseIp.

private void releaseIp(final long ipId, final long userId, final Account caller) {
    s_logger.info("ELB: Release public IP for loadbalancing " + ipId);
    final IPAddressVO ipvo = _ipAddressDao.findById(ipId);
    ipvo.setAssociatedWithNetworkId(null);
    _ipAddressDao.update(ipvo.getId(), ipvo);
    _ipAddrMgr.disassociatePublicIpAddress(ipId, userId, caller);
    _ipAddressDao.unassignIpAddress(ipId);
}
Also used : IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 4 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class NetworkProviderTest method createFloatingIp.

public IPAddressVO createFloatingIp(Network network, UserVm vm) throws Exception {
    BaseCmd cmd = new AssociateIPAddrCmd();
    BaseCmd proxy = ComponentContext.inject(cmd);
    Account system = _accountMgr.getSystemAccount();
    DataCenter zone = _server.getZone();
    ManagementServerMock.setParameter(proxy, "accountName", BaseCmd.CommandType.STRING, system.getAccountName());
    ManagementServerMock.setParameter(proxy, "domainId", BaseCmd.CommandType.LONG, Domain.ROOT_DOMAIN);
    ManagementServerMock.setParameter(proxy, "zoneId", BaseCmd.CommandType.LONG, zone.getId());
    ManagementServerMock.setParameter(proxy, "networkId", BaseCmd.CommandType.LONG, network.getId());
    try {
        ((AssociateIPAddrCmd) cmd).create();
        ((AssociateIPAddrCmd) cmd).execute();
    } catch (Exception e) {
        s_logger.debug("AssociateIPAddrCmd exception: " + e);
        e.printStackTrace();
        throw e;
    }
    SearchBuilder<IPAddressVO> searchBuilder = _ipAddressDao.createSearchBuilder();
    searchBuilder.and("sourceNat", searchBuilder.entity().isSourceNat(), Op.EQ);
    searchBuilder.and("network", searchBuilder.entity().getAssociatedWithNetworkId(), Op.EQ);
    searchBuilder.and("dataCenterId", searchBuilder.entity().getDataCenterId(), Op.EQ);
    searchBuilder.and("associatedWithVmId", searchBuilder.entity().getAssociatedWithVmId(), Op.NULL);
    SearchCriteria<IPAddressVO> sc = searchBuilder.create();
    sc.setParameters("sourceNat", false);
    sc.setParameters("network", network.getId());
    List<IPAddressVO> publicIps = _ipAddressDao.search(sc, null);
    assertNotNull(publicIps);
    cmd = new EnableStaticNatCmd();
    proxy = ComponentContext.inject(cmd);
    ManagementServerMock.setParameter(proxy, "ipAddressId", BaseCmd.CommandType.LONG, publicIps.get(0).getId());
    ManagementServerMock.setParameter(proxy, "networkId", BaseCmd.CommandType.LONG, network.getId());
    ManagementServerMock.setParameter(proxy, "virtualMachineId", BaseCmd.CommandType.LONG, vm.getId());
    try {
        proxy.execute();
    } catch (Exception e) {
        s_logger.debug("EnableStaticNatCmd exception: " + e);
        e.printStackTrace();
        throw e;
    }
    return publicIps.get(0);
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) AssociateIPAddrCmd(org.apache.cloudstack.api.command.user.address.AssociateIPAddrCmd) IPAddressVO(com.cloud.network.dao.IPAddressVO) EnableStaticNatCmd(org.apache.cloudstack.api.command.user.nat.EnableStaticNatCmd) BaseCmd(org.apache.cloudstack.api.BaseCmd) CloudException(com.cloud.exception.CloudException) IOException(java.io.IOException)

Example 5 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO 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> domainVln = _domainVlanMapDao.listDomainVlanMapsByVlan(vlanRange.getId());
    // Check for domain wide pool. It will have an entry for domain_vlan_map.
    if (domainVln != null && !domainVln.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++;
                    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(), 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);
            _vlanDao.remove(vlanDbId);
        }
    });
    return true;
}
Also used : AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) 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)

Aggregations

IPAddressVO (com.cloud.network.dao.IPAddressVO)109 ArrayList (java.util.ArrayList)43 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)42 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)39 Account (com.cloud.user.Account)37 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 DB (com.cloud.utils.db.DB)28 TransactionStatus (com.cloud.utils.db.TransactionStatus)26 Network (com.cloud.network.Network)25 PublicIp (com.cloud.network.addr.PublicIp)22 DataCenter (com.cloud.dc.DataCenter)17 VlanVO (com.cloud.dc.VlanVO)16 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)16 List (java.util.List)15 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)14 Ip (com.cloud.utils.net.Ip)14 NetworkOffering (com.cloud.offering.NetworkOffering)13 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)13 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)12 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)11