From 4e60bb128db2a72224a84c73735c0162960a44b6 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 16 May 2019 16:28:39 +0000 Subject: Allow policy type prefix on policy guard policy id In order to identify the policy type of a guard policy, we need to have the first part of the policy id as the policy type identifier prefix. Issue-ID: POLICY-1776 Change-Id: I1d6a3c4b122d978ff7ef40ce76ced7f160ebd4fa Signed-off-by: liamfallon --- .../legacy/mapping/LegacyGuardPolicyMapper.java | 23 +++++++++++++--- .../provider/LegacyProvider4LegacyGuardTest.java | 31 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java index 8fd883722..1c414ee17 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -56,17 +56,17 @@ public class LegacyGuardPolicyMapper private static final Map GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>(); static { - GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout", + GUARD_POLICY_TYPE_MAP.put("guard.frequency.", new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0")); - GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", + GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); - GUARD_POLICY_TYPE_MAP.put("guard.blacklist", + GUARD_POLICY_TYPE_MAP.put("guard.blacklist.", new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); } @Override public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) { - PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId()); + PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput); if (guardPolicyType == null) { String errorMessage = "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown"; @@ -151,4 +151,19 @@ public class LegacyGuardPolicyMapper return legacyGuardPolicyOutputMap; } + + private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) { + final String policyId = legacyGuardPolicyInput.getPolicyId(); + if (policyId == null) { + return null; + } + + for (Entry guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) { + if (policyId.startsWith(guardPolicyTypeEntry.getKey())) { + return guardPolicyTypeEntry.getValue(); + } + } + + return null; + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java index 6ddf1aeeb..2aadcd7b2 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java @@ -183,6 +183,35 @@ public class LegacyProvider4LegacyGuardTest { assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); } + @Test + public void testPolicyCreateBad() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(pfDao, null); + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + createPolicyTypes(); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + originalGip.setPolicyId("i.do.not.exist"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(pfDao, originalGip); + }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown"); + } + @Test public void testPolicyUpdate() throws Exception { assertThatThrownBy(() -> { @@ -293,7 +322,7 @@ public class LegacyProvider4LegacyGuardTest { }).hasMessage("no policy found for policy ID: guard.frequency.scaleout"); LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput(); - otherGip.setPolicyId("guard.blacklist"); + otherGip.setPolicyId("guard.blacklist.b0"); otherGip.setPolicyVersion("1"); otherGip.setContent(new LegacyGuardPolicyContent()); -- cgit 1.2.3-korg