Search in sources :

Example 21 with DataCenter

use of com.cloud.dc.DataCenter in project cloudstack by apache.

the class GloboDnsElement method release.

@Override
@DB
public boolean release(final Network network, NicProfile nic, final VirtualMachineProfile vm, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
    if (!isTypeSupported(vm.getType())) {
        s_logger.info("GloboDNS only manages records for VMs of type User, ConsoleProxy and DomainRouter. VM " + vm + " is " + vm.getType());
        return false;
    }
    Long zoneId = network.getDataCenterId();
    final DataCenter zone = _dcDao.findById(zoneId);
    if (zone == null) {
        throw new CloudRuntimeException("Could not find zone associated to this network");
    }
    RemoveRecordCommand cmd = new RemoveRecordCommand(hostNameOfVirtualMachine(vm), nic.getIPv4Address(), network.getNetworkDomain(), GloboDNSOverride.value());
    callCommand(cmd, zoneId);
    return true;
}
Also used : DataCenter(com.cloud.dc.DataCenter) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoveRecordCommand(com.globo.globodns.cloudstack.commands.RemoveRecordCommand) DB(com.cloud.utils.db.DB)

Example 22 with DataCenter

use of com.cloud.dc.DataCenter in project cloudstack by apache.

the class GloboDnsElement method implement.

@Override
@DB
public boolean implement(final Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    Long zoneId = network.getDataCenterId();
    DataCenter zone = _dcDao.findById(zoneId);
    if (zone == null) {
        throw new CloudRuntimeException("Could not find zone associated to this network");
    }
    CreateOrUpdateDomainCommand cmd = new CreateOrUpdateDomainCommand(network.getNetworkDomain(), GloboDNSTemplateId.value());
    callCommand(cmd, zoneId);
    return true;
}
Also used : DataCenter(com.cloud.dc.DataCenter) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateOrUpdateDomainCommand(com.globo.globodns.cloudstack.commands.CreateOrUpdateDomainCommand) DB(com.cloud.utils.db.DB)

Example 23 with DataCenter

use of com.cloud.dc.DataCenter in project cloudstack by apache.

the class NiciraNvpGuestNetworkGuruTest method testImplementURIException.

@Test
public void testImplementURIException() throws InsufficientVirtualNetworkCapacityException {
    final PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
    when(physnetdao.findById((Long) any())).thenReturn(physnet);
    when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
    when(physnet.getId()).thenReturn(NETWORK_ID);
    final NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
    when(nvpdao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NiciraNvpDeviceVO[] { device }));
    when(device.getId()).thenReturn(1L);
    final NetworkOffering offering = mock(NetworkOffering.class);
    when(offering.getId()).thenReturn(NETWORK_ID);
    when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
    when(offering.getGuestType()).thenReturn(GuestType.Isolated);
    when(nosd.areServicesSupportedByNetworkOffering(NETWORK_ID, Service.Connectivity)).thenReturn(false);
    mock(DeploymentPlan.class);
    final NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(State.Implementing);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    final DeployDestination dest = mock(DeployDestination.class);
    final DataCenter dc = mock(DataCenter.class);
    when(dest.getDataCenter()).thenReturn(dc);
    final HostVO niciraHost = mock(HostVO.class);
    when(hostdao.findById(anyLong())).thenReturn(niciraHost);
    when(niciraHost.getDetail("transportzoneuuid")).thenReturn("aaaa");
    when(niciraHost.getDetail("transportzoneisotype")).thenReturn("stt");
    when(niciraHost.getId()).thenReturn(NETWORK_ID);
    when(netmodel.findPhysicalNetworkId(anyLong(), (String) any(), (TrafficType) any())).thenReturn(NETWORK_ID);
    final Domain dom = mock(Domain.class);
    when(dom.getName()).thenReturn("domain");
    final Account acc = mock(Account.class);
    when(acc.getAccountName()).thenReturn("accountname");
    final ReservationContext res = mock(ReservationContext.class);
    when(res.getDomain()).thenReturn(dom);
    when(res.getAccount()).thenReturn(acc);
    final CreateLogicalSwitchAnswer answer = mock(CreateLogicalSwitchAnswer.class);
    when(answer.getResult()).thenReturn(true);
    //when(answer.getLogicalSwitchUuid()).thenReturn("aaaaa");
    when(agentmgr.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
    final Network implementednetwork = guru.implement(network, offering, dest, res);
    assertTrue(implementednetwork == null);
    verify(agentmgr, times(1)).easySend(eq(NETWORK_ID), (Command) any());
}
Also used : Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) CreateLogicalSwitchAnswer(com.cloud.agent.api.CreateLogicalSwitchAnswer) NetworkOffering(com.cloud.offering.NetworkOffering) NiciraNvpDeviceVO(com.cloud.network.NiciraNvpDeviceVO) HostVO(com.cloud.host.HostVO) ReservationContext(com.cloud.vm.ReservationContext) DataCenter(com.cloud.dc.DataCenter) DeployDestination(com.cloud.deploy.DeployDestination) Network(com.cloud.network.Network) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) Domain(com.cloud.domain.Domain) Test(org.junit.Test)

Example 24 with DataCenter

use of com.cloud.dc.DataCenter in project cloudstack by apache.

the class UserVmManagerImpl method updateNicIpForVirtualMachine.

@Override
public UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd) {
    Long nicId = cmd.getNicId();
    String ipaddr = cmd.getIpaddress();
    Account caller = CallContext.current().getCallingAccount();
    //check whether the nic belongs to user vm.
    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");
    }
    UserVm vm = _vmDao.findById(nicVO.getInstanceId());
    if (vm == null) {
        throw new InvalidParameterValueException("There is no vm with the nic");
    }
    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);
    }
    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) {
        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);
    Account ipOwner = _accountDao.findByIdIncludingRemoved(vm.getAccountId());
    // verify ip address
    s_logger.debug("Calling the ip allocation ...");
    DataCenter dc = _dcDao.findById(network.getDataCenterId());
    if (dc == null) {
        throw new InvalidParameterValueException("There is no dc with the nic");
    }
    if (dc.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Isolated) {
        try {
            ipaddr = _ipAddrMgr.allocateGuestIP(network, ipaddr);
        } catch (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)) {
            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) {
            User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
            ReservationContext context = new ReservationContextImpl(null, null, callerUser, caller);
            DeployDestination dest = new DeployDestination(_dcDao.findById(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 (Exception ex) {
                s_logger.warn("Failed to implement network " + network + " elements and resources as a part of vm nic ip update due to ", ex);
                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)) {
                    IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmId(vm.getId());
                    if (oldIP != null) {
                        oldIP.setVmIp(nicVO.getIPv4Address());
                        _ipAddressDao.persist(oldIP);
                    }
                }
                throw e;
            }
        }
    } else if (dc.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 (dc.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(TransactionStatus status) {
                        _ipAddrMgr.markIpAsUnavailable(ip.getId());
                        _ipAddressDao.unassignIpAddress(ip.getId());
                    }
                });
            }
        } catch (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) 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) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) UserVm(com.cloud.uservm.UserVm) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.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 25 with DataCenter

use of com.cloud.dc.DataCenter in project cloudstack by apache.

the class ExternalGuestNetworkGuru method reserve.

@Override
public void reserve(NicProfile nic, Network config, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
    assert (nic.getReservationStrategy() == ReservationStrategy.Start) : "What can I do for nics that are not allocated at start? ";
    DataCenter dc = _dcDao.findById(config.getDataCenterId());
    if (_networkModel.networkIsConfiguredForExternalNetworking(config.getDataCenterId(), config.getId())) {
        nic.setBroadcastUri(config.getBroadcastUri());
        nic.setIsolationUri(config.getBroadcastUri());
        nic.setIPv4Dns1(dc.getDns1());
        nic.setIPv4Dns2(dc.getDns2());
        nic.setIPv4Netmask(NetUtils.cidr2Netmask(config.getCidr()));
        long cidrAddress = NetUtils.ip2Long(config.getCidr().split("/")[0]);
        int cidrSize = getGloballyConfiguredCidrSize();
        nic.setIPv4Gateway(config.getGateway());
        if (nic.getIPv4Address() == null) {
            String guestIp = _ipAddrMgr.acquireGuestIpAddress(config, null);
            if (guestIp == null) {
                throw new InsufficientVirtualNetworkCapacityException("Unable to acquire guest IP address for network " + config, DataCenter.class, dc.getId());
            }
            nic.setIPv4Address(guestIp);
        } else {
            long ipMask = NetUtils.ip2Long(nic.getIPv4Address()) & ~(0xffffffffffffffffl << (32 - cidrSize));
            nic.setIPv4Address(NetUtils.long2Ip(cidrAddress | ipMask));
        }
    } else {
        super.reserve(nic, config, vm, dest, context);
    }
}
Also used : DataCenter(com.cloud.dc.DataCenter) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException)

Aggregations

DataCenter (com.cloud.dc.DataCenter)144 Account (com.cloud.user.Account)50 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)37 NetworkVO (com.cloud.network.dao.NetworkVO)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)32 Network (com.cloud.network.Network)30 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)27 ArrayList (java.util.ArrayList)27 DeployDestination (com.cloud.deploy.DeployDestination)25 NetworkOffering (com.cloud.offering.NetworkOffering)23 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)17 IPAddressVO (com.cloud.network.dao.IPAddressVO)17 DB (com.cloud.utils.db.DB)17 Domain (com.cloud.domain.Domain)16 ReservationContext (com.cloud.vm.ReservationContext)16 HostVO (com.cloud.host.HostVO)15 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)13 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)13 PhysicalNetwork (com.cloud.network.PhysicalNetwork)11 ServiceOffering (com.cloud.offering.ServiceOffering)11