diff --git a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java index 34dd63e8..4c584a9a 100644 --- a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java +++ b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java @@ -19,11 +19,13 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -70,17 +72,15 @@ public Map download() { } /** - * deletes all rules for Switzerland - * + * deletes the rules with the given IDs + * @param identifiers IDs of the rules to delete * @return successfully deleted rule IDs */ - public RulesSyncResult deleteAll(){ - logger.info("Deleting all Swiss rules"); - Map rules = download(); - Set ruleIds = rules.keySet(); + public RulesSyncResult deleteRules(Collection identifiers){ + logger.info("Deleting {} rules", identifiers); List deletedRuleIds = new ArrayList<>(); List failedRuleIds = new ArrayList<>(); - ruleIds.forEach(ruleId -> { + identifiers.forEach(ruleId -> { try { // sign payload String cms = null; @@ -115,7 +115,7 @@ public RulesSyncResult deleteAll(){ } }); - logger.info("Finished uploading Swiss rules"); + logger.info("Finished deleting rules. {} succeeded, {} failed", deletedRuleIds.size(), failedRuleIds.size()); return new RulesSyncResult(deletedRuleIds, failedRuleIds); } @@ -126,7 +126,7 @@ public RulesSyncResult deleteAll(){ * @return successfully uploaded rule ids */ public RulesSyncResult upload(JsonNode rules) { - deleteAll(); + Set existingRules = download().keySet(); logger.info("Uploading Swiss rules"); List uploadedRuleIds = new ArrayList<>(); List failedRuleIds = new ArrayList<>(); @@ -137,6 +137,7 @@ public RulesSyncResult upload(JsonNode rules) { Entry ruleArray = fieldIterator.next(); for (var rule : ruleArray.getValue()) { String ruleId = ruleArray.getKey(); + existingRules.remove(ruleId); try { // sign payload String cms = null; @@ -170,7 +171,6 @@ public RulesSyncResult upload(JsonNode rules) { logger.error("[FAILED CMS] {}", cms); logger.error("Upload of rule {} failed", ruleId, e); } - continue; } } catch (Exception ex) { failedRuleIds.add(ruleId); @@ -179,6 +179,13 @@ public RulesSyncResult upload(JsonNode rules) { } } logger.info("Finished uploading Swiss rules"); + if (failedRuleIds.isEmpty()) { + logger.info( + "Deleting {} remote rules that no longer exist locally", existingRules.size()); + deleteRules(existingRules); + }else{ + logger.warn("There were upload failures. Skipping rule deletion"); + } return new RulesSyncResult(uploadedRuleIds, failedRuleIds); } }