From b477554c29b2d666e7bb0bc860afacc6e935c337 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 18 Feb 2020 16:14:59 +0000 Subject: Add safe entity delete, fix multiple entity get This review implements checks on whether entities are being used or referenced propr to allowing deletes. The review also fixes bugs on multiple entity get returning 4040 not found Issue-ID: POLICY-1402 Change-Id: I9cebb9a873098740e9ff4be6284d6307e19838bd Signed-off-by: liamfallon --- .../tosca/legacy/provider/LegacyProvider.java | 2 +- .../concepts/JpaToscaConstraintValidValues.java | 5 +- .../tosca/simple/concepts/JpaToscaEntrySchema.java | 6 +- .../tosca/simple/concepts/JpaToscaPolicy.java | 2 +- .../tosca/simple/concepts/JpaToscaProperty.java | 9 ++- .../tosca/simple/provider/SimpleToscaProvider.java | 78 ++++++++++++++++++++-- .../onap/policy/models/tosca/utils/ToscaUtils.java | 8 ++- 7 files changed, 93 insertions(+), 17 deletions(-) (limited to 'models-tosca/src/main/java') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java index 09cc6c0ca..dc8affc77 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java @@ -53,7 +53,7 @@ import org.slf4j.LoggerFactory; public class LegacyProvider { private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class); - private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; + public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; // Recurring constants private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: "; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java index 2a121573c..664855e42 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; import java.util.List; + import javax.persistence.ElementCollection; import lombok.EqualsAndHashCode; @@ -77,8 +78,8 @@ public class JpaToscaConstraintValidValues extends JpaToscaConstraint { @Override public void fromAuthorative(final ToscaConstraint toscaConstraint) { + validValues = new ArrayList<>(); if (toscaConstraint.getValidValues() != null) { - validValues = new ArrayList<>(); validValues.addAll(toscaConstraint.getValidValues()); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java index 881d87c4b..b432486ae 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java @@ -3,7 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,7 +47,6 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntrySchema; - /** * Class to represent the EntrySchema of list/map property in TOSCA definition. * @@ -72,7 +71,7 @@ public class JpaToscaEntrySchema private String description; @ElementCollection - private List constraints; + private List constraints = new ArrayList<>(); /** * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields. @@ -170,7 +169,6 @@ public class JpaToscaEntrySchema this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank")); } - if (constraints != null) { for (JpaToscaConstraint constraint : constraints) { if (constraint == null) { diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java index b500a8bc9..b5da49751 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java @@ -88,7 +88,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P @ElementCollection @Lob - private Map properties; + private Map properties = new LinkedHashMap<>(); @ElementCollection private List targets = new ArrayList<>(); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java index 0e8201f0f..a2127f37c 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java @@ -3,7 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; @@ -35,9 +36,11 @@ import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; + import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; + import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; @@ -85,13 +88,13 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative constraints; + private List constraints = new ArrayList<>(); @Column private JpaToscaEntrySchema entrySchema; @ElementCollection - private Map metadata; + private Map metadata = new LinkedHashMap<>(); /** * The Default Constructor creates a {@link JpaToscaProperty} object with a null key. diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index c537bbcb5..a3e18ca41 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -37,8 +37,10 @@ import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; @@ -58,8 +60,11 @@ public class SimpleToscaProvider { private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); // Recurring string constants + private static final String DATA_TYPE = "data type "; + private static final String POLICY_TYPE = "policy type "; private static final String SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE = "service template not found in database"; private static final String DO_NOT_EXIST = " do not exist"; + private static final String NOT_FOUND = " not found"; /** * Get Service Template. @@ -226,7 +231,32 @@ public class SimpleToscaProvider { throws PfModelException { LOGGER.debug("->deleteDataType: key={}", dataTypeKey); - JpaToscaServiceTemplate serviceTemplate = getDataTypes(dao, dataTypeKey.getName(), dataTypeKey.getVersion()); + JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doDataTypesExist(serviceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no data types found"); + } + + JpaToscaDataType dataType4Deletion = serviceTemplate.getDataTypes().get(dataTypeKey); + if (dataType4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, DATA_TYPE + dataTypeKey.getId() + NOT_FOUND); + } + + for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) { + if (dataType.getReferencedDataTypes().contains(dataTypeKey)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, DATA_TYPE + dataTypeKey.getId() + + " is in use, it is referenced in data type " + dataType.getId()); + } + } + + if (ToscaUtils.doPolicyTypesExist(serviceTemplate)) { + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { + if (policyType.getReferencedDataTypes().contains(dataTypeKey)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, DATA_TYPE + dataTypeKey.getId() + + " is in use, it is referenced in policy type " + policyType.getId()); + } + } + } dao.delete(JpaToscaDataType.class, dataTypeKey); @@ -355,8 +385,37 @@ public class SimpleToscaProvider { throws PfModelException { LOGGER.debug("->deletePolicyType: key={}", policyTypeKey); - JpaToscaServiceTemplate serviceTemplate = - getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion()); + JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policy types found"); + } + + JpaToscaEntityType policyType4Deletion = + serviceTemplate.getPolicyTypes().get(policyTypeKey); + if (policyType4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, + POLICY_TYPE + policyTypeKey.getId() + NOT_FOUND); + } + + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { + Collection> ancestorList = ToscaUtils + .getEntityTypeAncestors(serviceTemplate.getPolicyTypes(), policyType, new PfValidationResult()); + + if (ancestorList.contains(policyType4Deletion)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE + policyTypeKey.getId() + + " is in use, it is referenced in policy type " + policyType.getId()); + } + } + + if (ToscaUtils.doPoliciesExist(serviceTemplate)) { + for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) { + if (policyTypeKey.equals(policy.getType())) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE + + policyTypeKey.getId() + " is in use, it is referenced in policy " + policy.getId()); + } + } + } dao.delete(JpaToscaPolicyType.class, policyTypeKey); @@ -476,7 +535,16 @@ public class SimpleToscaProvider { throws PfModelException { LOGGER.debug("->deletePolicy: key={}", policyKey); - JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion()); + JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doPoliciesExist(serviceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policies found"); + } + + JpaToscaPolicy policy4Deletion = serviceTemplate.getTopologyTemplate().getPolicies().get(policyKey); + if (policy4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "policy " + policyKey.getId() + NOT_FOUND); + } dao.delete(JpaToscaPolicy.class, policyKey); @@ -507,7 +575,7 @@ public class SimpleToscaProvider { if (policyType == null) { String errorMessage = - "policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist"; + POLICY_TYPE + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist"; throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java index b75273e5e..77633bd27 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -211,7 +211,7 @@ public final class ToscaUtils { } /** - * Find all the ancestors of an entity type. + * getLatestPolicyTypeVersion Find all the ancestors of an entity type. * * @param entityTypes the set of entity types that exist * @param entityType the entity type for which to get the parents @@ -227,6 +227,12 @@ public final class ToscaUtils { return CollectionUtils.emptyCollection(); } + if (entityType.getKey().equals(parentEntityTypeKey)) { + result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class, + ValidationResult.INVALID, "entity cannot be an ancestor of itself")); + throw new PfModelRuntimeException(Response.Status.CONFLICT, result.toString()); + } + @SuppressWarnings("unchecked") Set> ancestorEntitySet = (Set>) entityTypes .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion()); -- cgit 1.2.3-korg