@Override public NetworkACLItem moveNetworkAclRuleToNewPosition(MoveNetworkAclItemCmd moveNetworkAclItemCmd) { String uuidRuleBeingMoved = moveNetworkAclItemCmd.getUuidRuleBeingMoved(); String nextAclRuleUuid = moveNetworkAclItemCmd.getNextAclRuleUuid(); String previousAclRuleUuid = moveNetworkAclItemCmd.getPreviousAclRuleUuid(); if (StringUtils.isBlank(previousAclRuleUuid) && StringUtils.isBlank(nextAclRuleUuid)) { throw new InvalidParameterValueException("Both previous and next ACL rule IDs cannot be blank."); } NetworkACLItemVO ruleBeingMoved = _networkACLItemDao.findByUuid(uuidRuleBeingMoved); if (ruleBeingMoved == null) { throw new InvalidParameterValueException(String.format("Could not find a rule with ID[%s]", uuidRuleBeingMoved)); } NetworkACLItemVO previousRule = retrieveAndValidateAclRule(previousAclRuleUuid); NetworkACLItemVO nextRule = retrieveAndValidateAclRule(nextAclRuleUuid); validateMoveAclRulesData(ruleBeingMoved, previousRule, nextRule); try { NetworkACLVO lockedAcl = _networkACLDao.acquireInLockTable(ruleBeingMoved.getAclId()); List<NetworkACLItemVO> allAclRules = getAllAclRulesSortedByNumber(lockedAcl.getId()); validateAclConsistency(moveNetworkAclItemCmd, lockedAcl, allAclRules); if (previousRule == null) { return moveRuleToTheTop(ruleBeingMoved, allAclRules); } if (nextRule == null) { return moveRuleToTheBottom(ruleBeingMoved, allAclRules); } return moveRuleBetweenAclRules(ruleBeingMoved, allAclRules, previousRule, nextRule); } finally { _networkACLDao.releaseFromLockTable(ruleBeingMoved.getAclId()); } }
/** * Updates a network ACL with the given values found in the {@link UpdateNetworkACLItemCmd} parameter. * First we will validate the network ACL rule provided in the command using {@link #validateNetworkAclRuleIdAndRetrieveIt(UpdateNetworkACLItemCmd)}. * Then, we validate the ACL itself using {@link #validateNetworkAcl(NetworkACL)}. If all of the validation is ok, we do the following. * <ul> * <li>Transfer new data to {@link NetworkACLItemVO} that is intended to be updated; * <li>Validate the ACL rule being updated using {@link #validateNetworkACLItem(NetworkACLItemVO)}. * </ul> * * After the validations and updating the POJO we execute the update in the database using {@link NetworkACLManagerImpl#updateNetworkACLItem(NetworkACLItemVO)}. * */ @Override public NetworkACLItem updateNetworkACLItem(UpdateNetworkACLItemCmd updateNetworkACLItemCmd) throws ResourceUnavailableException { NetworkACLItemVO networkACLItemVo = validateNetworkAclRuleIdAndRetrieveIt(updateNetworkACLItemCmd); NetworkACL acl = _networkAclMgr.getNetworkACL(networkACLItemVo.getAclId()); validateNetworkAcl(acl); transferDataToNetworkAclRulePojo(updateNetworkACLItemCmd, networkACLItemVo, acl); validateNetworkACLItem(networkACLItemVo); return _networkAclMgr.updateNetworkACLItem(networkACLItemVo); }
/** * Move the rule to the top of the ACL rule list. This means that the ACL rule being moved will receive the position '1'. * Also, if necessary other ACL rules will have their 'number' field updated to create room for the new top rule. */ protected NetworkACLItem moveRuleToTheTop(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules) { return updateAclRuleToNewPositionAndExecuteShiftIfNecessary(ruleBeingMoved, 1, allAclRules, 0); }
Long aclId = createAclListIfNeeded(createNetworkACLCmd); validateNetworkAcl(acl); validateAclRuleNumber(createNetworkACLCmd, acl); NetworkACLItem.Action ruleAction = validateAndCreateNetworkAclRuleAction(action); Integer number = createNetworkACLCmd.getNumber(); if (number == null) { networkACLItemVO.setDisplay(createNetworkACLCmd.isDisplay()); validateNetworkACLItem(networkACLItemVO); return _networkAclMgr.createNetworkACLItem(networkACLItemVO);
aclId = createAclListForNetworkAndReturnAclListId(createNetworkACLCmd, network);
updateAclRuleToNewPositionAndExecuteShiftIfNecessary(networkACLItemVO, newNumberFieldValueNextAclRule, allAclRules, i);
/** * Moves an ACL to the space between to other rules. If there is already enough room to accommodate the ACL rule being moved, we simply get the 'number' field from the previous ACL rule and add one, and then define this new value as the 'number' value for the ACL rule being moved. * Otherwise, we will need to make room. This process is executed via {@link #updateAclRuleToNewPositionAndExecuteShiftIfNecessary(NetworkACLItemVO, int, List, int)}, which will create the space between ACL rules if necessary. This involves shifting ACL rules to accommodate the rule being moved. */ protected NetworkACLItem moveRuleBetweenAclRules(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules, NetworkACLItemVO previousRule, NetworkACLItemVO nextRule) { if (previousRule.getNumber() + 1 != nextRule.getNumber()) { int newNumberFieldValue = previousRule.getNumber() + 1; for (NetworkACLItemVO networkACLItemVO : allAclRules) { if (networkACLItemVO.getNumber() == newNumberFieldValue) { throw new InvalidParameterValueException("There are some inconsistencies with the data you sent. The new position calculated already has a ACL rule on it."); } } ruleBeingMoved.setNumber(newNumberFieldValue); _networkACLItemDao.updateNumberFieldNetworkItem(ruleBeingMoved.getId(), newNumberFieldValue); return _networkACLItemDao.findById(ruleBeingMoved.getId()); } int positionToStartProcessing = 0; for (int i = 0; i < allAclRules.size(); i++) { if (allAclRules.get(i).getId() == previousRule.getId()) { positionToStartProcessing = i + 1; break; } } return updateAclRuleToNewPositionAndExecuteShiftIfNecessary(ruleBeingMoved, previousRule.getNumber() + 1, allAclRules, positionToStartProcessing); }