AffinityGroupServiceImpl extends ManagerBase implements AffinityGroupService, Manager, StateListener<State, VirtualMachine.Event, VirtualMachine> { @Override public UserVm updateVMAffinityGroups(Long vmId, List<Long> affinityGroupIds) { UserVmVO vmInstance = _userVmDao.findById(vmId); if (vmInstance == null) { throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); } if (!vmInstance.getState().equals(State.Stopped)) { s_logger.warn("Unable to update affinity groups of the virtual machine " + vmInstance.toString() + " in state " + 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."); } Account caller = CallContext.current().getCallingAccount(); Account owner = _accountMgr.getAccount(vmInstance.getAccountId()); for (Long affinityGroupId : affinityGroupIds) { AffinityGroupVO ag = _affinityGroupDao.findById(affinityGroupId); if (ag == null) { throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); } else { _accountMgr.checkAccess(caller, null, true, owner, ag); if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) { if (ag.getAccountId() != owner.getAccountId()) { throw new PermissionDeniedException("Affinity Group " + ag + " does not belong to the VM's account"); } } } } _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(List<AffinityGroupProcessor> affinityProcessors); @DB @Override @ActionEvent(eventType = EventTypes.EVENT_AFFINITY_GROUP_CREATE, eventDescription = "Creating Affinity Group", create = true) AffinityGroup createAffinityGroup(CreateAffinityGroupCmd createAffinityGroupCmd); @DB @Override AffinityGroup createAffinityGroup(final String accountName, final Long projectId, final Long domainId, final String affinityGroupName, final String affinityGroupType,
        final String description); @DB @ActionEvent(eventType = EventTypes.EVENT_AFFINITY_GROUP_DELETE, eventDescription = "Deleting affinity group") boolean deleteAffinityGroup(Long affinityGroupId, String account, Long projectId, Long domainId, String affinityGroupName); @Override List<String> listAffinityGroupTypes(); @Override boolean isAdminControlledGroup(AffinityGroup group); @Override boolean configure(final String name, final Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override String getName(); @Override AffinityGroup getAffinityGroup(Long groupId); @Override boolean preStateTransitionEvent(State oldState, Event event, State newState, VirtualMachine vo, boolean status, Object opaque); @Override boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque); @Override UserVm updateVMAffinityGroups(Long vmId, List<Long> affinityGroupIds); @Override boolean isAffinityGroupProcessorAvailable(String affinityGroupType); @Override boolean isAffinityGroupAvailableInDomain(long affinityGroupId, long domainId); static final Logger s_logger; }
@Test(expected = InvalidParameterValueException.class) public void updateAffinityGroupVMRunning() throws ResourceInUseException { when(_acctMgr.finalizeOwner((Account)anyObject(), anyString(), anyLong(), anyLong())).thenReturn(acct); UserVmVO vm = new UserVmVO(10L, "test", "test", 101L, HypervisorType.Any, 21L, false, false, DOMAIN_ID, 200L, 1, 5L, "", "test", 1L); vm.setState(VirtualMachine.State.Running); when(_vmDao.findById(10L)).thenReturn(vm); List<Long> affinityGroupIds = new ArrayList<Long>(); affinityGroupIds.add(20L); _affinityService.updateVMAffinityGroups(10L, affinityGroupIds); }