Skip to content

Commit

Permalink
Merge pull request #3971 from atlanhq/arpitbulklinkingmaster2
Browse files Browse the repository at this point in the history
some improvements
  • Loading branch information
arpit-at authored Jan 13, 2025
2 parents 10f04d5 + ead1fd1 commit 7a4607f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand All @@ -44,7 +46,7 @@
public class BusinessPolicyRequest implements Serializable {
private static final long serialVersionUID = 1L;

private List<AssetComplianceInfo> data;
private List<AssetComplianceInfo> data = new ArrayList<>();

public List<AssetComplianceInfo> getData() {
return data;
Expand All @@ -64,17 +66,16 @@ public String toString() {
public static class AssetComplianceInfo implements Serializable {
private static final long serialVersionUID = 1L;

//guids for asset
private String assetId;

//guids for complaint rules
private Set<String> complaintRules;
private Set<String> addCompliantGUIDs = new HashSet<>();

//guids for non complaint rules
private Set<String> nonComplaintRules;
private Set<String> removeCompliantGUIDs = new HashSet<>();

//guids for complaint policies
private Set<String> complaintPolicies;

private Set<String> addNonCompliantGUIDs = new HashSet<>();

private Set<String> removeNonCompliantGUIDs = new HashSet<>();


public String getAssetId() {
Expand All @@ -85,39 +86,48 @@ public void setAssetId(String assetId) {
this.assetId = assetId;
}

public Set<String> getComplaintRules() {
return complaintRules;
public Set<String> getAddCompliantGUIDs() {
return addCompliantGUIDs;
}

public void setComplaintRules(Set<String> complaintRules) {
this.complaintRules = complaintRules;
public void setAddCompliantGUIDs(Set<String> addCompliantGUIDs) {
this.addCompliantGUIDs = addCompliantGUIDs;
}

public Set<String> getNonComplaintRules() {
return nonComplaintRules;
public Set<String> getRemoveCompliantGUIDs() {
return removeCompliantGUIDs;
}

public void setNonComplaintRules(Set<String> nonComplaintRules) {
this.nonComplaintRules = nonComplaintRules;
public void setRemoveCompliantGUIDs(Set<String> removeCompliantGUIDs) {
this.removeCompliantGUIDs = removeCompliantGUIDs;
}

public Set<String> getComplaintPolicies() {
return complaintPolicies;
public Set<String> getAddNonCompliantGUIDs() {
return addNonCompliantGUIDs;
}

public void setComplaintPolicies(Set<String> complaintPolicies) {
this.complaintPolicies = complaintPolicies;
public void setAddNonCompliantGUIDs(Set<String> addNonCompliantGUIDs) {
this.addNonCompliantGUIDs = addNonCompliantGUIDs;
}

public Set<String> getRemoveNonCompliantGUIDs() {
return removeNonCompliantGUIDs;
}

public void setRemoveNonCompliantGUIDs(Set<String> removeNonCompliantGUIDs) {
this.removeNonCompliantGUIDs = removeNonCompliantGUIDs;
}

@Override
public String toString() {
return "AssetComplianceInfo{" +
"assetId='" + assetId + '\'' +
", complaintRules=" + complaintRules +
", nonComplaintRules=" + nonComplaintRules +
", complaintPolicies=" + complaintPolicies +
'}';
final StringBuilder sb = new StringBuilder("AssetComplianceInfo{");
sb.append("assetId='").append(assetId).append('\'');
sb.append(", addCompliantGUIDs=").append(addCompliantGUIDs);
sb.append(", removeCompliantGUIDs=").append(removeCompliantGUIDs);
sb.append(", addNonCompliantGUIDs=").append(addNonCompliantGUIDs);
sb.append(", removeNonCompliantGUIDs=").append(removeNonCompliantGUIDs);
sb.append('}');
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4779,21 +4779,27 @@ public AtlasVertex linkBusinessPolicy(final BusinessPolicyRequest.AssetComplianc
Set<String> existingNonCompliant = getVertexPolicies(vertex, NON_COMPLIANT_ASSET_POLICY_GUIDS);

// Retrieve new policies
Set<String> newCompliantRulesGUIDs = data.getComplaintRules();
Set<String> newNonCompliantRulesGUIDs = data.getNonComplaintRules();
Set<String> newCompliantPolicyGUIDs = data.getComplaintPolicies();
Set<String> addCompliantGUIDs = getOrCreateEmptySet(data.getAddCompliantGUIDs());
Set<String> addNonCompliantGUIDs = getOrCreateEmptySet(data.getAddNonCompliantGUIDs());
Set<String> removeCompliantGUIDs = getOrCreateEmptySet(data.getRemoveCompliantGUIDs());
Set<String> removeNonCompliantGUIDs = getOrCreateEmptySet(data.getRemoveNonCompliantGUIDs());

// Calculate effective compliant and non-compliant policies
Set<String> effectiveCompliantGUIDs = calculateEffectivePolicies(existingCompliant, newCompliantRulesGUIDs, newCompliantPolicyGUIDs);
Set<String> effectiveNonCompliantGUIDs = calculateEffectivePolicies(existingNonCompliant, newNonCompliantRulesGUIDs);

// Update vertex properties
updateVertexPolicies(vertex, ASSET_POLICY_GUIDS, effectiveCompliantGUIDs);
updateVertexPolicies(vertex, NON_COMPLIANT_ASSET_POLICY_GUIDS, effectiveNonCompliantGUIDs);
addToAttribute(vertex, ASSET_POLICY_GUIDS, addCompliantGUIDs);
removeFromAttribute(vertex, ASSET_POLICY_GUIDS, removeCompliantGUIDs);


addToAttribute(vertex, NON_COMPLIANT_ASSET_POLICY_GUIDS, addNonCompliantGUIDs);
removeFromAttribute(vertex, NON_COMPLIANT_ASSET_POLICY_GUIDS, removeNonCompliantGUIDs);

// Count and set policies
Set<String> effectiveCompliantGUIDs = getVertexPolicies(vertex, ASSET_POLICY_GUIDS);
Set<String> effectiveNonCompliantGUIDs = getVertexPolicies(vertex, NON_COMPLIANT_ASSET_POLICY_GUIDS);

int compliantPolicyCount = countPoliciesExcluding(effectiveCompliantGUIDs, "rule");
int nonCompliantPolicyCount = countPoliciesExcluding(effectiveNonCompliantGUIDs, "rule");

int totalPolicyCount = compliantPolicyCount + nonCompliantPolicyCount;

vertex.setProperty(ASSET_POLICIES_COUNT, totalPolicyCount);
Expand All @@ -4807,21 +4813,28 @@ public AtlasVertex linkBusinessPolicy(final BusinessPolicyRequest.AssetComplianc
return vertex;
}

@SafeVarargs
private final Set<String> calculateEffectivePolicies(Set<String> existing, Set<String>... additions) {
Set<String> effective = new HashSet<>(existing);
for (Set<String> addition : additions) {
effective.addAll(addition);
}
return effective;
private static Set<String> getOrCreateEmptySet(Set<String> input) {
return input == null ? new HashSet<>() : input;
}

private void updateVertexPolicies(AtlasVertex vertex, String propertyKey, Set<String> policies) {
if(ASSET_POLICY_GUIDS.equals(propertyKey)){
policies.forEach(policyGuid -> vertex.setProperty(ASSET_POLICY_GUIDS, policyGuid));
}else {
policies.forEach(policyGuid -> vertex.setProperty(NON_COMPLIANT_ASSET_POLICY_GUIDS, policyGuid));
}
private void addToAttribute(AtlasVertex vertex, String propertyKey, Set<String> policies) {
String targetProperty = determineTargetProperty(propertyKey);
policies.stream()
.filter(StringUtils::isNotEmpty)
.forEach(policyGuid -> vertex.setProperty(targetProperty, policyGuid));
}

private void removeFromAttribute(AtlasVertex vertex, String propertyKey, Set<String> policies) {
String targetProperty = determineTargetProperty(propertyKey);
policies.stream()
.filter(StringUtils::isNotEmpty)
.forEach(policyGuid -> vertex.removePropertyValue(targetProperty, policyGuid));
}

private String determineTargetProperty(String propertyKey) {
return ASSET_POLICY_GUIDS.equals(propertyKey)
? ASSET_POLICY_GUIDS
: NON_COMPLIANT_ASSET_POLICY_GUIDS;
}

private int countPoliciesExcluding(Set<String> policies, String substring) {
Expand Down

0 comments on commit 7a4607f

Please sign in to comment.