use of com.cloud.network.security.SecurityGroupRules in project cloudstack by apache.
the class SecurityGroupResultObject method transposeNetworkGroups.
public static List<SecurityGroupResultObject> transposeNetworkGroups(List<? extends SecurityGroupRules> groups) {
List<SecurityGroupResultObject> resultObjects = new ArrayList<SecurityGroupResultObject>();
Map<Long, SecurityGroup> allowedSecurityGroups = new HashMap<Long, SecurityGroup>();
Map<Long, Account> accounts = new HashMap<Long, Account>();
if ((groups != null) && !groups.isEmpty()) {
List<SecurityGroupRuleResultObject> securityGroupRuleDataList = new ArrayList<SecurityGroupRuleResultObject>();
SecurityGroupResultObject currentGroup = null;
List<Long> processedGroups = new ArrayList<Long>();
for (SecurityGroupRules netGroupRule : groups) {
Long groupId = netGroupRule.getId();
if (!processedGroups.contains(groupId)) {
processedGroups.add(groupId);
if (currentGroup != null) {
if (!securityGroupRuleDataList.isEmpty()) {
currentGroup.setSecurityGroupRules(securityGroupRuleDataList);
securityGroupRuleDataList = new ArrayList<SecurityGroupRuleResultObject>();
}
resultObjects.add(currentGroup);
}
// start a new group
SecurityGroupResultObject groupResult = new SecurityGroupResultObject();
groupResult.setId(netGroupRule.getId());
groupResult.setName(netGroupRule.getName());
groupResult.setDescription(netGroupRule.getDescription());
groupResult.setDomainId(netGroupRule.getDomainId());
Account account = accounts.get(netGroupRule.getAccountId());
if (account == null) {
account = ApiDBUtils.findAccountById(netGroupRule.getAccountId());
accounts.put(account.getId(), account);
}
groupResult.setAccountId(account.getId());
groupResult.setAccountName(account.getAccountName());
currentGroup = groupResult;
}
if (netGroupRule.getRuleId() != null) {
// there's at least one securitygroup rule for this network group, add the securitygroup rule data
SecurityGroupRuleResultObject securityGroupRuleData = new SecurityGroupRuleResultObject();
securityGroupRuleData.setEndPort(netGroupRule.getEndPort());
securityGroupRuleData.setStartPort(netGroupRule.getStartPort());
securityGroupRuleData.setId(netGroupRule.getRuleId());
securityGroupRuleData.setProtocol(netGroupRule.getProtocol());
securityGroupRuleData.setRuleType(netGroupRule.getRuleType());
Long allowedSecurityGroupId = netGroupRule.getAllowedNetworkId();
if (allowedSecurityGroupId != null) {
SecurityGroup allowedSecurityGroup = allowedSecurityGroups.get(allowedSecurityGroupId);
if (allowedSecurityGroup == null) {
allowedSecurityGroup = ApiDBUtils.findSecurityGroupById(allowedSecurityGroupId);
allowedSecurityGroups.put(allowedSecurityGroupId, allowedSecurityGroup);
}
securityGroupRuleData.setAllowedSecurityGroup(allowedSecurityGroup.getName());
Account allowedAccount = accounts.get(allowedSecurityGroup.getAccountId());
if (allowedAccount == null) {
allowedAccount = ApiDBUtils.findAccountById(allowedSecurityGroup.getAccountId());
accounts.put(allowedAccount.getId(), allowedAccount);
}
securityGroupRuleData.setAllowedSecGroupAcct(allowedAccount.getAccountName());
} else if (netGroupRule.getAllowedSourceIpCidr() != null) {
securityGroupRuleData.setAllowedSourceIpCidr(netGroupRule.getAllowedSourceIpCidr());
}
securityGroupRuleDataList.add(securityGroupRuleData);
}
}
// all rules have been processed, add the final data into the list
if (currentGroup != null) {
if (!securityGroupRuleDataList.isEmpty()) {
currentGroup.setSecurityGroupRules(securityGroupRuleDataList);
}
resultObjects.add(currentGroup);
}
}
return resultObjects;
}
Aggregations