Skip to content

Commit 98e5091

Browse files
Documentation and unit tests
1 parent e904b05 commit 98e5091

4 files changed

Lines changed: 485 additions & 27 deletions

File tree

api/src/main/java/com/cloud/network/vpc/NetworkACLService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ public interface NetworkACLService {
9292

9393
NetworkACL updateNetworkACL(UpdateNetworkACLListCmd updateNetworkACLListCmd);
9494

95+
/**
96+
* Updates a network item ACL to a new position. This method allows users to inform between which ACLs the given ACL will be placed. Therefore, the 'number' field will be filled out by the system in the best way possible to place the ACL accordingly.
97+
*/
9598
NetworkACLItem moveNetworkAclRuleToNewPosition(MoveNetworkAclItemCmd moveNetworkAclItemCmd);
9699
}

engine/schema/src/main/java/com/cloud/network/vpc/NetworkACLItemDao.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ public interface NetworkACLItemDao extends GenericDao<NetworkACLItemVO, Long> {
3737

3838
void loadCidrs(NetworkACLItemVO item);
3939

40+
/**
41+
* Updated the network ACL item 'number' field.
42+
*
43+
* @param networkItemId is the ID of the network ACL rule that will have its 'number' field updated.
44+
* @param newNumberValue is the new value that will be assigned to the 'number' field.
45+
*/
4046
void updateNumberFieldNetworkItem(long networkItemId, int newNumberValue);
4147
}

server/src/main/java/com/cloud/network/vpc/NetworkACLServiceImpl.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,10 @@ public NetworkACLItem moveNetworkAclRuleToNewPosition(MoveNetworkAclItemCmd move
969969
return moveRuleBetweenAclRules(ruleBeingMoved, allAclRules, previousRule, nextRule);
970970
}
971971

972-
private List<NetworkACLItemVO> getAllAclRulesSortedByNumber(long aclId) {
972+
/**
973+
* Loads all ACL rules from given network ACL list. Then, the ACL rules will be sorted according to the 'number' field in ascending order.
974+
*/
975+
protected List<NetworkACLItemVO> getAllAclRulesSortedByNumber(long aclId) {
973976
List<NetworkACLItemVO> allAclRules = _networkACLItemDao.listByACL(aclId);
974977
Collections.sort(allAclRules, new Comparator<NetworkACLItemVO>() {
975978
@Override
@@ -980,7 +983,11 @@ public int compare(NetworkACLItemVO o1, NetworkACLItemVO o2) {
980983
return allAclRules;
981984
}
982985

983-
private NetworkACLItem moveRuleBetweenAclRules(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules, NetworkACLItemVO previousRule, NetworkACLItemVO nextRule) {
986+
/**
987+
* Mover 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.
988+
* 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.
989+
*/
990+
protected NetworkACLItem moveRuleBetweenAclRules(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules, NetworkACLItemVO previousRule, NetworkACLItemVO nextRule) {
984991
if (previousRule.getNumber() + 1 != nextRule.getNumber()) {
985992
int newNumberFieldValue = previousRule.getNumber() + 1;
986993
for (NetworkACLItemVO networkACLItemVO : allAclRules) {
@@ -1002,7 +1009,11 @@ private NetworkACLItem moveRuleBetweenAclRules(NetworkACLItemVO ruleBeingMoved,
10021009
return updateAclRuleToNewPositionAndExecuteShiftIfNecessary(ruleBeingMoved, previousRule.getNumber() + 1, allAclRules, positionToStartProcessing);
10031010
}
10041011

1005-
private NetworkACLItem moveRuleToTheBottom(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules) {
1012+
/**
1013+
* Moves a network ACL rule to the bottom of the list. This is executed by getting the 'number' field of the last ACL rule from the ACL list, and incrementing one.
1014+
* This new value is assigned to the network ACL being moved and updated in the database using {@link NetworkACLItemDao#updateNumberFieldNetworkItem(long, int)}.
1015+
*/
1016+
protected NetworkACLItem moveRuleToTheBottom(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules) {
10061017
NetworkACLItemVO lastAclRule = allAclRules.get(allAclRules.size() - 1);
10071018

10081019
int newNumberFieldValue = lastAclRule.getNumber() + 1;
@@ -1012,11 +1023,33 @@ private NetworkACLItem moveRuleToTheBottom(NetworkACLItemVO ruleBeingMoved, List
10121023
return _networkACLItemDao.findById(ruleBeingMoved.getId());
10131024
}
10141025

1015-
private NetworkACLItem moveRuleToTheTop(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules) {
1026+
/**
1027+
* Move the rule to the top of the ACL rule list. This means that the ACL rule being moved will receive the position '1'.
1028+
* Also, if necessary other ACL rules will have their 'number' field updated to create room for the new top rule.
1029+
*/
1030+
protected NetworkACLItem moveRuleToTheTop(NetworkACLItemVO ruleBeingMoved, List<NetworkACLItemVO> allAclRules) {
10161031
return updateAclRuleToNewPositionAndExecuteShiftIfNecessary(ruleBeingMoved, 1, allAclRules, 0);
10171032
}
10181033

1019-
private NetworkACLItem updateAclRuleToNewPositionAndExecuteShiftIfNecessary(NetworkACLItemVO ruleBeingMoved, int newNumberFieldValue, List<NetworkACLItemVO> allAclRules,
1034+
/**
1035+
* Updates the ACL rule number executing the shift on subsequent ACL rules if necessary.
1036+
* For example, if we have the following ACL rules:
1037+
* <ul>
1038+
* <li> ACL A - number 1
1039+
* <li> ACL B - number 2
1040+
* <li> ACL C - number 3
1041+
* <li> ACL D - number 12
1042+
* </ul>
1043+
* If we move 'ACL D' to a place between 'ACL A' and 'ACL B', this method will execute the shift needded to create the space for 'ACL D'.
1044+
* After applying this method, we will have the following condition.
1045+
* <ul>
1046+
* <li> ACL A - number 1
1047+
* <li> ACL D - number 2
1048+
* <li> ACL B - number 3
1049+
* <li> ACL C - number 4
1050+
* </ul>
1051+
*/
1052+
protected NetworkACLItem updateAclRuleToNewPositionAndExecuteShiftIfNecessary(NetworkACLItemVO ruleBeingMoved, int newNumberFieldValue, List<NetworkACLItemVO> allAclRules,
10201053
int indexToStartProcessing) {
10211054
ruleBeingMoved.setNumber(newNumberFieldValue);
10221055
for (int i = indexToStartProcessing; i < allAclRules.size(); i++) {
@@ -1034,7 +1067,11 @@ private NetworkACLItem updateAclRuleToNewPositionAndExecuteShiftIfNecessary(Netw
10341067
return _networkACLItemDao.findById(ruleBeingMoved.getId());
10351068
}
10361069

1037-
private NetworkACLItemVO retrieveAndValidateAclRule(String aclRuleUuid) {
1070+
/**
1071+
* Searches in the database for an ACL rule by its UUID.
1072+
* An {@link InvalidParameterValueException} is thrown if no ACL rule is found with the given UUID.
1073+
*/
1074+
protected NetworkACLItemVO retrieveAndValidateAclRule(String aclRuleUuid) {
10381075
if (StringUtils.isBlank(aclRuleUuid)) {
10391076
return null;
10401077
}
@@ -1045,7 +1082,16 @@ private NetworkACLItemVO retrieveAndValidateAclRule(String aclRuleUuid) {
10451082
return aclRule;
10461083
}
10471084

1048-
private void validateMoveAclRulesData(NetworkACLItemVO ruleBeingMoved, NetworkACLItemVO previousRule, NetworkACLItemVO nextRule) {
1085+
/**
1086+
* Validates if the data provided to move the ACL rule is supported by this implementation. The user needs to provide a valid ACL UUID, and at least one of the previous or the next ACL rule.
1087+
* The validation is as follows:
1088+
* <ul>
1089+
* <li> If both ACL rules 'previous' and 'next' are invalid, we throw an {@link InvalidParameterValueException};
1090+
* <li> informed previous and next ACL rules must have the same ACL ID as the rule being moved; otherwise, an {@link InvalidParameterValueException} is thrown;
1091+
* <li> then we check if the user trying to move ACL rules has access to the VPC, where the ACL rules are being applied.
1092+
* </ul>
1093+
*/
1094+
protected void validateMoveAclRulesData(NetworkACLItemVO ruleBeingMoved, NetworkACLItemVO previousRule, NetworkACLItemVO nextRule) {
10491095
if (nextRule == null && previousRule == null) {
10501096
throw new InvalidParameterValueException("Both previous and next ACL rule IDs cannot be invalid.");
10511097
}

0 commit comments

Comments
 (0)