@Test(expected = InvalidParameterValueException.class) public void updateAffinityGroupVMRunning() throws ResourceInUseException { when(_acctMgr.finalizeOwner((Account) anyObject(), anyString(), anyLong(), anyLong())).thenReturn(acct); final UserVmVO vm = new UserVmVO(10L, "test", "test", 101L, HypervisorType.Any, 21L, false, false, DOMAIN_ID, 200L, 1, 5L, "", "test", 1L, "Manufacturer", OptimiseFor.Generic, false, "", MaintenancePolicy.LiveMigrate, 0L, "cdrom,hd,network"); vm.setState(VirtualMachine.State.Running); when(_vmDao.findById(10L)).thenReturn(vm); final List<Long> affinityGroupIds = new ArrayList<>(); affinityGroupIds.add(20L); _affinityService.updateVMAffinityGroups(10L, affinityGroupIds); }
@Override public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) { final UserVmVO vmInstance = _userVmDao.findById(vmId); if (vmInstance == null) { throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); } final Account caller = CallContext.current().getCallingAccount(); final Account owner = _accountMgr.getAccount(vmInstance.getAccountId()); for (final Long affinityGroupId : affinityGroupIds) { final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId); if (AffinityGroupVO == null) { throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); } else { _accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO); if ("ExplicitDedication".equals(AffinityGroupVO.getType())) { Boolean VmRunsOnDedicatedHost = false; final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId); final List<DedicatedResourceVO> DedicatedResourceList = result.first(); for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) { if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) { VmRunsOnDedicatedHost = true; } } if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group."); } } else { if (!State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating."); } } if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) { if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) { throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain"); } } } } _affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds); if (s_logger.isDebugEnabled()) { s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds); } return vmInstance; }
AffinityGroupServiceImpl extends ManagerBase implements AffinityGroupService, Manager, StateListener<State, VirtualMachine.Event, VirtualMachine> { @Override public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) { final UserVmVO vmInstance = _userVmDao.findById(vmId); if (vmInstance == null) { throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); } final Account caller = CallContext.current().getCallingAccount(); final Account owner = _accountMgr.getAccount(vmInstance.getAccountId()); for (final Long affinityGroupId : affinityGroupIds) { final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId); if (AffinityGroupVO == null) { throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); } else { _accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO); if ("ExplicitDedication".equals(AffinityGroupVO.getType())) { Boolean VmRunsOnDedicatedHost = false; final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId); final List<DedicatedResourceVO> DedicatedResourceList = result.first(); for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) { if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) { VmRunsOnDedicatedHost = true; } } if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group."); } } else { if (!State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating."); } } if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) { if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) { throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain"); } } } } _affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds); if (s_logger.isDebugEnabled()) { s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds); } return vmInstance; } }
AffinityGroupServiceImpl extends ManagerBase implements AffinityGroupService, Manager, StateListener<State, VirtualMachine.Event, VirtualMachine> { @Override public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) { final UserVmVO vmInstance = _userVmDao.findById(vmId); if (vmInstance == null) { throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); } final Account caller = CallContext.current().getCallingAccount(); final Account owner = _accountMgr.getAccount(vmInstance.getAccountId()); for (final Long affinityGroupId : affinityGroupIds) { final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId); if (AffinityGroupVO == null) { throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); } else { _accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO); if ("ExplicitDedication".equals(AffinityGroupVO.getType())) { Boolean VmRunsOnDedicatedHost = false; final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId); final List<DedicatedResourceVO> DedicatedResourceList = result.first(); for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) { if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) { VmRunsOnDedicatedHost = true; } } if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group."); } } else { if (!State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating."); } } if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) { if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) { throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain"); } } } } _affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds); if (s_logger.isDebugEnabled()) { s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds); } return vmInstance; }  }
AffinityGroupServiceImpl extends ManagerBase implements AffinityGroupService, Manager, StateListener<State, VirtualMachine.Event, VirtualMachine> { @Override public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) { final UserVmVO vmInstance = _userVmDao.findById(vmId); if (vmInstance == null) { throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); } final Account caller = CallContext.current().getCallingAccount(); final Account owner = _accountMgr.getAccount(vmInstance.getAccountId()); for (final Long affinityGroupId : affinityGroupIds) { final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId); if (AffinityGroupVO == null) { throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); } else { _accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO); if ("ExplicitDedication".equals(AffinityGroupVO.getType())) { Boolean VmRunsOnDedicatedHost = false; final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId); final List<DedicatedResourceVO> DedicatedResourceList = result.first(); for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) { if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) { VmRunsOnDedicatedHost = true; } } if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group."); } } else { if (!State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating."); } } if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) { if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) { throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain"); } } } } _affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds); if (s_logger.isDebugEnabled()) { s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds); } return vmInstance; }  List<AffinityGroupProcessor> getAffinityGroupProcessors(); void setAffinityGroupProcessors(final List<AffinityGroupProcessor> affinityProcessors); @Override String getName(); @Override boolean configure(final String name, final Map<String, Object> params); @DB @Override @ActionEvent(eventType = EventTypes.EVENT_AFFINITY_GROUP_CREATE, eventDescription = "Creating Affinity Group", create = true) AffinityGroup createAffinityGroup(final CreateAffinityGroupCmd createAffinityGroupCmd); @Override boolean start(); @Override boolean stop(); @DB @Override AffinityGroup createAffinityGroup(final String accountName, final Long projectId, final Long domainId, final String affinityGroupName, final String affinityGroupType,
                                             final String description); @Override boolean preStateTransitionEvent(final State oldState, final Event event, final State newState, final VirtualMachine vo, final boolean status, final Object opaque); @Override boolean postStateTransitionEvent(final Transition<State, Event> transition, final VirtualMachine vo, final boolean status, final Object opaque); @Override @DB @ActionEvent(eventType = EventTypes.EVENT_AFFINITY_GROUP_DELETE, eventDescription = "Deleting affinity group") boolean deleteAffinityGroup(final Long affinityGroupId, final String account, final Long projectId, final Long domainId, final String affinityGroupName); @Override List<String> listAffinityGroupTypes(); @Override boolean isAdminControlledGroup(final AffinityGroup group); @Override AffinityGroup getAffinityGroup(final Long groupId); @Override UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds); @Override boolean isAffinityGroupProcessorAvailable(final String affinityGroupType); @Override boolean isAffinityGroupAvailableInDomain(final long affinityGroupId, final long domainId); }
AffinityGroupServiceImpl extends ManagerBase implements AffinityGroupService, Manager, StateListener<State, VirtualMachine.Event, VirtualMachine> { @Override public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) { final UserVmVO vmInstance = _userVmDao.findById(vmId); if (vmInstance == null) { throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); } final Account caller = CallContext.current().getCallingAccount(); final Account owner = _accountMgr.getAccount(vmInstance.getAccountId()); for (final Long affinityGroupId : affinityGroupIds) { final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId); if (AffinityGroupVO == null) { throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); } else { _accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO); if ("ExplicitDedication".equals(AffinityGroupVO.getType())) { Boolean VmRunsOnDedicatedHost = false; final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId); final List<DedicatedResourceVO> DedicatedResourceList = result.first(); for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) { if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) { VmRunsOnDedicatedHost = true; } } if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group."); } } else { if (!State.Stopped.equals(vmInstance.getState())) { throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating."); } } if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) { if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) { throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain"); } } } } _affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds); if (s_logger.isDebugEnabled()) { s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds); } return vmInstance; }  List<AffinityGroupProcessor> getAffinityGroupProcessors(); void setAffinityGroupProcessors(final List<AffinityGroupProcessor> affinityProcessors); @Override String getName(); @Override boolean configure(final String name, final Map<String, Object> params); @DB @Override @ActionEvent(eventType = EventTypes.EVENT_AFFINITY_GROUP_CREATE, eventDescription = "Creating Affinity Group", create = true) AffinityGroup createAffinityGroup(final CreateAffinityGroupCmd createAffinityGroupCmd); @Override boolean start(); @Override boolean stop(); @DB @Override AffinityGroup createAffinityGroup(final String accountName, final Long projectId, final Long domainId, final String affinityGroupName, final String affinityGroupType,
                                             final String description); @Override boolean preStateTransitionEvent(final State oldState, final Event event, final State newState, final VirtualMachine vo, final boolean status, final Object opaque); @Override boolean postStateTransitionEvent(final Transition<State, Event> transition, final VirtualMachine vo, final boolean status, final Object opaque); @Override @DB @ActionEvent(eventType = EventTypes.EVENT_AFFINITY_GROUP_DELETE, eventDescription = "Deleting affinity group") boolean deleteAffinityGroup(final Long affinityGroupId, final String account, final Long projectId, final Long domainId, final String affinityGroupName); @Override List<String> listAffinityGroupTypes(); @Override boolean isAdminControlledGroup(final AffinityGroup group); @Override AffinityGroup getAffinityGroup(final Long groupId); @Override UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds); @Override boolean isAffinityGroupProcessorAvailable(final String affinityGroupType); @Override boolean isAffinityGroupAvailableInDomain(final long affinityGroupId, final long domainId); static final Logger s_logger; }