X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Fsimple%2Fconcepts%2FJpaToscaPolicy.java;h=689d03506b2e985268e0c510be881fcab7474b30;hb=ce850be00732359bb04d8dd3666a1293dbf8f1ff;hp=7b4ffd164c7849e0c6a08cd50f9b7672100da026;hpb=b7909b8e74c0bc5920f5d0397a4371988df9e8b2;p=policy%2Fmodels.git 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 7b4ffd164..689d03506 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 @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * 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,7 +28,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; - import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; @@ -39,14 +38,11 @@ import javax.persistence.InheritanceType; import javax.persistence.Lob; import javax.persistence.Table; import javax.ws.rs.core.Response; - import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; @@ -89,10 +85,10 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P @ElementCollection @Lob - private Map properties; + private Map properties = new LinkedHashMap<>(); @ElementCollection - private List targets; + private List targets = new ArrayList<>(); // @formatter:on /** @@ -129,6 +125,9 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P */ public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) { super(copyConcept); + this.type = new PfConceptKey(copyConcept.type); + this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null); + this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new); } /** @@ -171,7 +170,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P propertyMap.put(entry.getKey(), coder.decode(entry.getValue(), Object.class)); } catch (CoderException ce) { String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey() - + ", value=" + entry.getValue(); + + ", value=" + entry.getValue(); throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); } } @@ -188,16 +187,18 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P if (toscaPolicy.getType() != null) { type.setName(toscaPolicy.getType()); - } - else { - type.setName(PfKey.NULL_KEY_NAME); + } else { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, + "PolicyType type not specified, the type of the PolicyType for this policy must be specified in " + + "the type field"); } if (toscaPolicy.getTypeVersion() != null) { type.setVersion(toscaPolicy.getTypeVersion()); - } - else { - type.setVersion(PfKey.NULL_KEY_VERSION); + } else { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, + "PolicyType version not specified, the version of the PolicyType for this policy must be specified in " + + "the type_version field"); } if (toscaPolicy.getProperties() != null) { @@ -215,7 +216,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P properties.put(propertyEntry.getKey(), coder.encode(propertyEntry.getValue())); } catch (CoderException ce) { String errorMessage = "error encoding property JSON value for database: key=" - + propertyEntry.getKey() + ", value=" + propertyEntry.getValue(); + + propertyEntry.getKey() + ", value=" + propertyEntry.getValue(); throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); } } @@ -228,7 +229,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P // Add the policy name and version fields to the metadata getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName()); - getMetadata().put(METADATA_POLICY_VERSION_TAG, Integer.toString(getKey().getMajorVersion())); + getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion()); } @Override @@ -261,15 +262,20 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { PfValidationResult result = super.validate(resultIn); + if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "key version is a null version")); + } + if (type == null || type.isNullKey()) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "type is null or a null key")); + "type is null or a null key")); } else { result = type.validate(result); } if (properties != null) { - result = validateProperties(result); + validateProperties(result); } if (targets != null) { @@ -282,22 +288,19 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P /** * Validate the policy properties. * - * @param result The result of validations up to now - * @return the validation result + * @param result where to put the validation results */ - private PfValidationResult validateProperties(final PfValidationResult resultIn) { - PfValidationResult result = resultIn; + private void validateProperties(final PfValidationResult result) { for (Entry propertyEntry : properties.entrySet()) { if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "policy property key may not be null ")); + "policy property key may not be null ")); } else if (propertyEntry.getValue() == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "policy property value may not be null ")); + "policy property value may not be null ")); } } - return result; } /** @@ -312,7 +315,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P for (PfConceptKey target : targets) { if (target == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "policy target may not be null ")); + "policy target may not be null ")); } else { result = target.validate(result); } @@ -350,33 +353,4 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P return PfUtils.compareObjects(targets, other.targets); } - - @Override - public PfConcept copyTo(@NonNull PfConcept target) { - final Object copyObject = target; - Assertions.instanceOf(copyObject, PfConcept.class); - - final JpaToscaPolicy copy = ((JpaToscaPolicy) copyObject); - super.copyTo(target); - - copy.setType(new PfConceptKey(type)); - - if (properties == null) { - copy.setProperties(null); - } else { - copy.setProperties(properties); - } - - if (targets == null) { - copy.setTargets(null); - } else { - final List newTargets = new ArrayList<>(); - for (final PfConceptKey oldTarget : targets) { - newTargets.add(new PfConceptKey(oldTarget)); - } - copy.setTargets(newTargets); - } - - return copy; - } }