Search in sources :

Example 76 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class ImplicitPlannerTest method checkWhenDcInAvoidList.

@Test
public void checkWhenDcInAvoidList() throws InsufficientServerCapacityException {
    final Zone zone = mock(Zone.class);
    final ExcludeList avoids = mock(ExcludeList.class);
    final VirtualMachineProfileImpl vmProfile = mock(VirtualMachineProfileImpl.class);
    final VMInstanceVO vm = mock(VMInstanceVO.class);
    final DataCenterDeployment plan = mock(DataCenterDeployment.class);
    when(avoids.shouldAvoid(zone)).thenReturn(true);
    when(vmProfile.getVirtualMachine()).thenReturn(vm);
    when(vm.getDataCenterId()).thenReturn(1L);
    when(zoneRepository.findById(1L)).thenReturn(Optional.of(zone));
    final List<Long> clusterList = planner.orderClusters(vmProfile, plan, avoids);
    assertTrue("Cluster list should be null/empty if the dc is in avoid list", (clusterList == null || clusterList.isEmpty()));
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) Zone(com.cloud.db.model.Zone) VMInstanceVO(com.cloud.vm.VMInstanceVO) Test(org.junit.Test)

Example 77 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class ConsoleProxyManagerTest method getDefaultNetwork.

@Test
public void getDefaultNetwork() {
    final Zone zone = mock(Zone.class);
    when(zone.getNetworkType()).thenReturn(NetworkType.Advanced);
    when(zoneRepository.findById(Mockito.anyLong())).thenReturn(Optional.of(zone));
    final NetworkVO network = Mockito.mock(NetworkVO.class);
    final NetworkVO badNetwork = Mockito.mock(NetworkVO.class);
    when(_networkDao.listByZoneAndTrafficType(anyLong(), eq(TrafficType.Public))).thenReturn(Collections.singletonList(network));
    when(_networkDao.listByZoneAndTrafficType(anyLong(), not(eq(TrafficType.Public)))).thenReturn(Collections.singletonList(badNetwork));
    final NetworkVO returnedNetwork = cpvmManager.getDefaultNetworkForAdvancedZone(zone);
    Assert.assertNotNull(returnedNetwork);
    Assert.assertEquals(network, returnedNetwork);
    Assert.assertNotEquals(badNetwork, returnedNetwork);
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) Zone(com.cloud.db.model.Zone) Test(org.junit.Test)

Example 78 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method orchestrateReboot.

private void orchestrateReboot(final String vmUuid, final Map<VirtualMachineProfile.Param, Object> params) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
    final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    // if there are active vm snapshots task, state change is not allowed
    if (_vmSnapshotMgr.hasActiveVMSnapshotTasks(vm.getId())) {
        s_logger.error("Unable to reboot VM " + vm + " due to: " + vm.getInstanceName() + " has active VM snapshots tasks");
        throw new CloudRuntimeException("Unable to reboot VM " + vm + " due to: " + vm.getInstanceName() + " has active VM snapshots tasks");
    }
    final Zone zone = _zoneRepository.findById(vm.getDataCenterId()).orElse(null);
    final Host host = _hostDao.findById(vm.getHostId());
    if (host == null) {
        // Should findById throw an Exception is the host is not found?
        throw new CloudRuntimeException("Unable to retrieve host with id " + vm.getHostId());
    }
    final Cluster cluster = _entityMgr.findById(Cluster.class, host.getClusterId());
    final Pod pod = _entityMgr.findById(Pod.class, host.getPodId());
    final DeployDestination dest = new DeployDestination(zone, pod, cluster, host);
    try {
        final Commands cmds = new Commands(Command.OnError.Stop);
        cmds.addCommand(new RebootCommand(vm.getInstanceName(), getExecuteInSequence(vm.getHypervisorType())));
        _agentMgr.send(host.getId(), cmds);
        final Answer rebootAnswer = cmds.getAnswer(RebootAnswer.class);
        if (rebootAnswer != null && rebootAnswer.getResult()) {
            return;
        }
        s_logger.info("Unable to reboot VM " + vm + " on " + dest.getHost() + " due to " + (rebootAnswer == null ? " no reboot answer" : rebootAnswer.getDetails()));
    } catch (final OperationTimedoutException e) {
        s_logger.warn("Unable to send the reboot command to host " + dest.getHost() + " for the vm " + vm + " due to operation timeout", e);
        throw new CloudRuntimeException("Failed to reboot the vm on host " + dest.getHost());
    }
}
Also used : UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) AgentControlAnswer(com.cloud.legacymodel.communication.answer.AgentControlAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.legacymodel.communication.answer.ClusterVMMetaDataSyncAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) RebootAnswer(com.cloud.legacymodel.communication.answer.RebootAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) StopAnswer(com.cloud.legacymodel.communication.answer.StopAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) RebootCommand(com.cloud.legacymodel.communication.command.RebootCommand) Pod(com.cloud.legacymodel.dc.Pod) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) TimeZone(java.util.TimeZone) Zone(com.cloud.db.model.Zone) DeployDestination(com.cloud.deploy.DeployDestination) Commands(com.cloud.agent.manager.Commands) Cluster(com.cloud.legacymodel.dc.Cluster) Host(com.cloud.legacymodel.dc.Host)

Example 79 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method orchestrateAddVmToNetwork.

private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Network network, final NicProfile requested) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final CallContext cctx = CallContext.current();
    s_logger.debug("Orchestrating add vm " + vm + " to network " + network + " with requested nic profile " + requested);
    final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
    final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
    final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    final Zone zone = _zoneRepository.findById(network.getDataCenterId()).orElse(null);
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(zone, null, null, host);
    // check vm state
    if (vm.getState() == State.Running) {
        // 1) allocate and prepare nic
        final NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile, true);
        // 2) Convert vmProfile to vmTO
        final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
        final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
        // 3) Convert nicProfile to NicTO
        final NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType());
        // 4) plug the nic to the vm
        s_logger.debug("Plugging nic for vm " + vm + " in network " + network);
        boolean result = false;
        try {
            result = plugNic(network, nicTO, vmTO, context, dest);
            if (result) {
                s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm  is a part of network now");
                final long isDefault = nic.isDefaultNic() ? 1 : 0;
                // insert nic's Id into DB as resource_name
                return nic;
            } else {
                s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network);
                return null;
            }
        } finally {
            if (!result) {
                s_logger.debug("Removing nic " + nic + " from vm " + vmProfile.getVirtualMachine() + " as nic plug failed on the backend");
                _networkMgr.removeNic(vmProfile, _nicsDao.findById(nic.getId()));
            }
        }
    } else if (vm.getState() == State.Stopped) {
        // 1) allocate nic
        return _networkMgr.createNicForVm(network, requested, context, vmProfile, false);
    } else {
        s_logger.warn("Unable to add vm " + vm + " to network  " + network);
        throw new ResourceUnavailableException("Unable to add vm " + vm + " to network, is not in the right state", DataCenter.class, vm.getDataCenterId());
    }
}
Also used : TimeZone(java.util.TimeZone) Zone(com.cloud.db.model.Zone) Host(com.cloud.legacymodel.dc.Host) CallContext(com.cloud.context.CallContext) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DataCenter(com.cloud.legacymodel.dc.DataCenter) DeployDestination(com.cloud.deploy.DeployDestination) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) NicTO(com.cloud.legacymodel.to.NicTO)

Example 80 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method createPhysicalNetwork.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_PHYSICAL_NETWORK_CREATE, eventDescription = "Creating Physical Network", create = true)
public PhysicalNetwork createPhysicalNetwork(final Long zoneId, final String vnetRange, final String networkSpeed, final List<String> isolationMethods, final String broadcastDomainRangeStr, final Long domainId, final List<String> tags, final String name) {
    // Check if zone exists
    if (zoneId == null) {
        throw new InvalidParameterValueException("Please specify a valid zone.");
    }
    final Zone zone = zoneRepository.findById(zoneId).orElse(null);
    if (zone == null) {
        throw new InvalidParameterValueException("Please specify a valid zone.");
    }
    if (AllocationState.Enabled == zone.getAllocationState()) {
        // TBD: Send uuid instead of zoneId; may have to hardcode tablename in call to addProxyObject().
        throw new PermissionDeniedException("Cannot create PhysicalNetwork since the Zone is currently enabled, zone Id: " + zoneId);
    }
    final NetworkType zoneType = zone.getNetworkType();
    if (zoneType == NetworkType.Basic) {
        if (!_physicalNetworkDao.listByZone(zoneId).isEmpty()) {
            // TBD: Send uuid instead of zoneId; may have to hardcode tablename in call to addProxyObject().
            throw new CloudRuntimeException("Cannot add the physical network to basic zone id: " + zoneId + ", there is a physical network already existing in this basic " + "Zone");
        }
    }
    if (tags != null && tags.size() > 1) {
        throw new InvalidParameterException("Only one tag can be specified for a physical network at this time");
    }
    if (isolationMethods != null && isolationMethods.size() > 1) {
        throw new InvalidParameterException("Only one isolationMethod can be specified for a physical network at this time");
    }
    if (vnetRange != null) {
        // Verify zone type
        if (zoneType == NetworkType.Basic) {
            throw new InvalidParameterValueException("Can't add vnet range to the physical network in the zone that supports " + zoneType + " network");
        }
    }
    BroadcastDomainRange broadcastDomainRange = null;
    if (broadcastDomainRangeStr != null && !broadcastDomainRangeStr.isEmpty()) {
        try {
            broadcastDomainRange = PhysicalNetwork.BroadcastDomainRange.valueOf(broadcastDomainRangeStr.toUpperCase());
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve broadcastDomainRange '" + broadcastDomainRangeStr + "' to a supported value {Pod or Zone}");
        }
        // in Acton release you can specify only Zone broadcastdomain type in Advance zone, and Pod in Basic
        if (zoneType == NetworkType.Basic && broadcastDomainRange != null && broadcastDomainRange != BroadcastDomainRange.POD) {
            throw new InvalidParameterValueException("Basic zone can have broadcast domain type of value " + BroadcastDomainRange.POD + " only");
        } else if (zoneType == NetworkType.Advanced && broadcastDomainRange != null && broadcastDomainRange != BroadcastDomainRange.ZONE) {
            throw new InvalidParameterValueException("Advance zone can have broadcast domain type of value " + BroadcastDomainRange.ZONE + " only");
        }
    }
    if (broadcastDomainRange == null) {
        if (zoneType == NetworkType.Basic) {
            broadcastDomainRange = PhysicalNetwork.BroadcastDomainRange.POD;
        } else {
            broadcastDomainRange = PhysicalNetwork.BroadcastDomainRange.ZONE;
        }
    }
    try {
        final BroadcastDomainRange broadcastDomainRangeFinal = broadcastDomainRange;
        return Transaction.execute(new TransactionCallback<PhysicalNetworkVO>() {

            @Override
            public PhysicalNetworkVO doInTransaction(final TransactionStatus status) {
                // Create the new physical network in the database
                final long id = _physicalNetworkDao.getNextInSequence(Long.class, "id");
                PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(id, zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRangeFinal, name);
                pNetwork.setTags(tags);
                pNetwork.setIsolationMethods(isolationMethods);
                pNetwork = _physicalNetworkDao.persist(pNetwork);
                // Add vnet entries for the new zone if zone type is Advanced
                if (vnetRange != null) {
                    addOrRemoveVnets(vnetRange.split(","), pNetwork);
                }
                // add VirtualRouter as the default network service provider
                addDefaultVirtualRouterToPhysicalNetwork(pNetwork.getId());
                // add VPCVirtualRouter as the default network service provider
                addDefaultVpcVirtualRouterToPhysicalNetwork(pNetwork.getId());
                return pNetwork;
            }
        });
    } catch (final Exception ex) {
        s_logger.warn("Exception: ", ex);
        throw new CloudRuntimeException("Fail to create a physical network");
    }
}
Also used : Zone(com.cloud.db.model.Zone) BroadcastDomainRange(com.cloud.network.PhysicalNetwork.BroadcastDomainRange) TransactionStatus(com.cloud.utils.db.TransactionStatus) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) SQLException(java.sql.SQLException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) UnknownHostException(java.net.UnknownHostException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) UnsupportedServiceException(com.cloud.legacymodel.exceptions.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) InvalidParameterException(java.security.InvalidParameterException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NetworkType(com.cloud.model.enumeration.NetworkType) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

Zone (com.cloud.db.model.Zone)109 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)34 ArrayList (java.util.ArrayList)34 DomainRouterVO (com.cloud.vm.DomainRouterVO)28 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)26 Network (com.cloud.legacymodel.network.Network)25 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)23 Account (com.cloud.legacymodel.user.Account)23 NetworkVO (com.cloud.network.dao.NetworkVO)23 NetworkTopology (com.cloud.network.topology.NetworkTopology)23 DeployDestination (com.cloud.deploy.DeployDestination)18 NicProfile (com.cloud.vm.NicProfile)17 List (java.util.List)17 HostPodVO (com.cloud.dc.HostPodVO)16 HostVO (com.cloud.host.HostVO)16 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)14 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)14 DB (com.cloud.utils.db.DB)14 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)12 NetworkOffering (com.cloud.offering.NetworkOffering)11