From ed11e882d9128616156a2be08744e5a9bdb01111 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 2 Mar 2021 10:55:31 +0000 Subject: Refactor models for common type handling Currently we have handling for "type" and "type_version" on TOSCA Policy class. However, the concept of a "type" and "type_version" also exists on the ToscaCapabilityAssignment, the ToscaNodeTemplate, and the ToscaRequriement classes. This review makes the type handling on Policy generic, thus extending it to the other three types. Issue-ID: POLICY-2983 Change-Id: Ia20e3a8c485f4841257075df08e0784eac415770 Signed-off-by: liamfallon --- .../concepts/ToscaCapabilityAssignment.java | 26 ++- .../authorative/concepts/ToscaNodeTemplate.java | 7 +- .../tosca/authorative/concepts/ToscaPolicy.java | 33 +-- .../authorative/concepts/ToscaRequirement.java | 27 ++- .../concepts/ToscaWithObjectProperties.java | 54 ----- .../concepts/ToscaWithTypeAndObjectProperties.java | 84 ++++++++ .../concepts/JpaToscaCapabilityAssignment.java | 4 +- .../simple/concepts/JpaToscaNodeTemplate.java | 26 +-- .../tosca/simple/concepts/JpaToscaPolicy.java | 58 +----- .../tosca/simple/concepts/JpaToscaRequirement.java | 4 +- .../concepts/JpaToscaWithStringProperties.java | 162 --------------- .../JpaToscaWithTypeAndStringProperties.java | 226 +++++++++++++++++++++ .../onap/policy/models/tosca/utils/ToscaUtils.java | 28 +-- 13 files changed, 390 insertions(+), 349 deletions(-) delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectProperties.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithTypeAndObjectProperties.java delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithTypeAndStringProperties.java (limited to 'models-tosca/src/main/java/org') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityAssignment.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityAssignment.java index 2d9cf9a39..11ffc044b 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityAssignment.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityAssignment.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020-2021 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,16 +21,38 @@ package org.onap.policy.models.tosca.authorative.concepts; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.ToString; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaCapabilityAssignment extends ToscaWithObjectProperties { +@ToString(callSuper = true) +public class ToscaCapabilityAssignment extends ToscaWithTypeAndObjectProperties { private Map attributes; private List occurrences; + + /** + * Copy constructor. + * + * @param copyObject object to copy + */ + public ToscaCapabilityAssignment(@NonNull ToscaCapabilityAssignment copyObject) { + super(copyObject); + + if (copyObject.attributes != null) { + attributes = new LinkedHashMap<>(copyObject.attributes); + } + + if (copyObject.occurrences != null) { + occurrences = new ArrayList<>(copyObject.occurrences); + } + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeTemplate.java index c4bc84cd8..c95836de6 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeTemplate.java @@ -31,12 +31,13 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.ToString; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaNodeTemplate extends ToscaWithObjectProperties { - private String type; +@ToString(callSuper = true) +public class ToscaNodeTemplate extends ToscaWithTypeAndObjectProperties { private List> requirements; private Map capabilities; @@ -48,8 +49,6 @@ public class ToscaNodeTemplate extends ToscaWithObjectProperties { public ToscaNodeTemplate(@NonNull ToscaNodeTemplate copyObject) { super(copyObject); - this.type = copyObject.type; - this.requirements = (copyObject.requirements != null ? new ArrayList<>(copyObject.requirements) : null); this.capabilities = (copyObject.capabilities != null ? new LinkedHashMap<>(copyObject.capabilities) : null); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java index b3a38050a..00d783d2d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java @@ -23,8 +23,6 @@ package org.onap.policy.models.tosca.authorative.concepts; -import com.google.gson.annotations.SerializedName; -import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -40,40 +38,13 @@ import lombok.ToString; @EqualsAndHashCode(callSuper = true) @NoArgsConstructor @ToString(callSuper = true) -public class ToscaPolicy extends ToscaWithObjectProperties { - private String type; - - @ApiModelProperty(name = "type_version") - @SerializedName("type_version") - private String typeVersion; - +public class ToscaPolicy extends ToscaWithTypeAndObjectProperties { /** * Copy constructor. * - * @param copyObject the obejct to copy from. + * @param copyObject object to copy */ public ToscaPolicy(@NonNull ToscaPolicy copyObject) { super(copyObject); - - this.type = copyObject.type; - this.typeVersion = copyObject.typeVersion; - } - - /** - * Gets the identifier for this policy. - * - * @return this policy's identifier - */ - public ToscaConceptIdentifier getIdentifier() { - return new ToscaConceptIdentifier(getName(), getVersion()); - } - - /** - * Gets the type identifier for this policy. - * - * @return this policy's type identifier - */ - public ToscaConceptIdentifier getTypeIdentifier() { - return new ToscaConceptIdentifier(getType(), getTypeVersion()); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRequirement.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRequirement.java index 166b81174..d2c3b83a3 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRequirement.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRequirement.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020-2021 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,15 +21,38 @@ package org.onap.policy.models.tosca.authorative.concepts; +import java.util.ArrayList; import java.util.List; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.ToString; @Data @EqualsAndHashCode(callSuper = true) -public class ToscaRequirement extends ToscaWithObjectProperties { +@NoArgsConstructor +@ToString(callSuper = true) +public class ToscaRequirement extends ToscaWithTypeAndObjectProperties { private String capability; private String node; private String relationship; private List occurrences; + + /** + * Copy constructor. + * + * @param copyObject object to copy + */ + public ToscaRequirement(@NonNull ToscaRequirement copyObject) { + super(copyObject); + + this.capability = copyObject.capability; + this.node = copyObject.node; + this.relationship = copyObject.relationship; + + if (copyObject.occurrences != null) { + occurrences = new ArrayList<>(copyObject.occurrences); + } + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectProperties.java deleted file mode 100644 index f6cf24f61..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectProperties.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.authorative.concepts; - -import java.util.LinkedHashMap; -import java.util.Map; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; -import lombok.NonNull; -import lombok.ToString; - -/** - * Class to represent TOSCA classes containing property maps whose values are generic - * Objects. - */ -@Data -@EqualsAndHashCode(callSuper = true) -@NoArgsConstructor -@ToString -public class ToscaWithObjectProperties extends ToscaEntity { - private Map properties; - - /** - * Cop[y constructor. - * - * @param copyObject object to copy - */ - public ToscaWithObjectProperties(@NonNull ToscaWithObjectProperties copyObject) { - super(copyObject); - - if (copyObject.properties != null) { - properties = new LinkedHashMap<>(copyObject.properties); - } - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithTypeAndObjectProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithTypeAndObjectProperties.java new file mode 100644 index 000000000..0bcb1cf54 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithTypeAndObjectProperties.java @@ -0,0 +1,84 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import com.google.gson.annotations.SerializedName; +import io.swagger.annotations.ApiModelProperty; +import java.util.LinkedHashMap; +import java.util.Map; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.ToString; + +/** + * Class to represent TOSCA classes containing property maps whose values are generic + * Objects. + */ +@Data +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +@ToString +public class ToscaWithTypeAndObjectProperties extends ToscaEntity { + private String type; + + @ApiModelProperty(name = "type_version") + @SerializedName("type_version") + private String typeVersion; + + private Map properties; + + /** + * Copy constructor. + * + * @param copyObject object to copy + */ + public ToscaWithTypeAndObjectProperties(@NonNull ToscaWithTypeAndObjectProperties copyObject) { + super(copyObject); + + this.type = copyObject.type; + this.typeVersion = copyObject.typeVersion; + + if (copyObject.properties != null) { + properties = new LinkedHashMap<>(copyObject.properties); + } + } + + /** + * Gets the identifier for this policy. + * + * @return this policy's identifier + */ + public ToscaConceptIdentifier getIdentifier() { + return new ToscaConceptIdentifier(getName(), getVersion()); + } + + /** + * Gets the type identifier for this policy. + * + * @return this policy's type identifier + */ + public ToscaConceptIdentifier getTypeIdentifier() { + return new ToscaConceptIdentifier(getType(), getTypeVersion()); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java index bb5cf5a73..76508dabe 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020-2021 Nordix Foundation. * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,7 +50,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignme @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaCapabilityAssignment extends JpaToscaWithStringProperties { +public class JpaToscaCapabilityAssignment extends JpaToscaWithTypeAndStringProperties { private static final long serialVersionUID = 1675770231921107988L; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java index 9507a9d2f..f6cfc1258 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020-2021 Nordix Foundation. * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.persistence.CascadeType; -import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Inheritance; @@ -39,8 +38,6 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.parameters.annotations.Valid; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -59,16 +56,11 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaNodeTemplate extends JpaToscaWithStringProperties { +public class JpaToscaNodeTemplate extends JpaToscaWithTypeAndStringProperties { private static final long serialVersionUID = 1675770231921107988L; private static final StandardCoder STANDARD_CODER = new StandardCoder(); - @Column - @NotNull - @NotBlank - private String type; - // formatter:off @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "requirementsName", referencedColumnName = "name") @@ -106,7 +98,7 @@ public class JpaToscaNodeTemplate extends JpaToscaWithStringProperties { +public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties { private static final long serialVersionUID = 3265174757061982805L; // Tags for metadata @@ -69,23 +66,14 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { private static final StandardCoder STANDARD_CODER = new StandardCoder(); - // @formatter:off - @Column - @AttributeOverride(name = "name", column = @Column(name = "type_name")) - @AttributeOverride(name = "version", column = @Column(name = "type_version")) - @VerifyKey - @NotNull - private PfConceptKey type; - @ElementCollection private List<@NotNull @Valid PfConceptKey> targets; - // @formatter:on /** * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key. */ public JpaToscaPolicy() { - this(new PfConceptKey()); + super(); } /** @@ -94,7 +82,7 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { * @param key the key */ public JpaToscaPolicy(@NonNull final PfConceptKey key) { - this(key, new PfConceptKey()); + super(key, new PfConceptKey()); } /** @@ -104,8 +92,7 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { * @param type the type of the policy */ public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) { - super(key); - this.type = type; + super(key, type); } /** @@ -115,7 +102,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { */ public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) { super(copyConcept); - this.type = new PfConceptKey(copyConcept.type); this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new); } @@ -126,7 +112,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { */ public JpaToscaPolicy(final ToscaPolicy authorativeConcept) { super(new PfConceptKey()); - type = new PfConceptKey(); this.fromAuthorative(authorativeConcept); } @@ -136,14 +121,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { super.setToscaEntity(toscaPolicy); super.toAuthorative(); - toscaPolicy.setType(type.getName()); - - if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) { - toscaPolicy.setTypeVersion(type.getVersion()); - } else { - toscaPolicy.setTypeVersion(null); - } - return toscaPolicy; } @@ -151,22 +128,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) { super.fromAuthorative(toscaPolicy); - if (toscaPolicy.getType() != null) { - type.setName(toscaPolicy.getType()); - } 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 { - 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"); - } - // Add the property metadata if it doesn't exist already if (toscaPolicy.getMetadata() == null) { setMetadata(new LinkedHashMap<>()); @@ -201,8 +162,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { public List getKeys() { final List keyList = super.getKeys(); - keyList.addAll(type.getKeys()); - if (targets != null) { keyList.addAll(targets); } @@ -214,8 +173,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { public void clean() { super.clean(); - type.clean(); - if (targets != null) { for (PfConceptKey target : targets) { target.clean(); @@ -245,11 +202,6 @@ public class JpaToscaPolicy extends JpaToscaWithStringProperties { final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept; - result = type.compareTo(other.type); - if (result != 0) { - return result; - } - return PfUtils.compareCollections(targets, other.targets); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java index eeae03dd7..20b6aff96 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java @@ -3,7 +3,7 @@ * ONAP Requirement Model * ================================================================================ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaRequirement extends JpaToscaWithStringProperties { +public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties { private static final long serialVersionUID = 2785481541573683089L; private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED"; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java deleted file mode 100644 index afe4a84d1..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java +++ /dev/null @@ -1,162 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.concepts; - -import java.util.LinkedHashMap; -import java.util.Map; -import javax.persistence.ElementCollection; -import javax.persistence.Lob; -import javax.persistence.MappedSuperclass; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NonNull; -import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.models.base.PfAuthorative; -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfUtils; -import org.onap.policy.models.tosca.authorative.concepts.ToscaWithObjectProperties; - -/** - * Class to represent JPA TOSCA classes containing property maps whose values are Strings. - */ -@MappedSuperclass -@Data -@EqualsAndHashCode(callSuper = true) -public abstract class JpaToscaWithStringProperties extends JpaToscaEntityType - implements PfAuthorative { - - private static final long serialVersionUID = 2785481541573683089L; - - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull String> properties; - - /** - * The Default Constructor creates a {@link JpaToscaWithStringProperties} object with - * a null key. - */ - protected JpaToscaWithStringProperties() { - this(new PfConceptKey()); - } - - /** - * The Key Constructor creates a {@link JpaToscaWithStringProperties} object with the - * given concept key. - * - * @param key the key - */ - protected JpaToscaWithStringProperties(@NonNull final PfConceptKey key) { - super(key); - } - - /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - protected JpaToscaWithStringProperties(@NonNull final JpaToscaWithStringProperties copyConcept) { - super(copyConcept); - this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null); - } - - /** - * Authorative constructor. - * - * @param authorativeConcept the authorative concept to copy from - */ - protected JpaToscaWithStringProperties(final T authorativeConcept) { - super(new PfConceptKey()); - this.fromAuthorative(authorativeConcept); - } - - @Override - public T toAuthorative() { - T tosca = super.toAuthorative(); - - tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue)); - - return tosca; - } - - @Override - public void fromAuthorative(@NonNull final T authorativeConcept) { - super.fromAuthorative(authorativeConcept); - - properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue); - } - - /** - * Deserializes a property value. - * - * @param propValue value to be deserialized - * @return the deserialized property value - */ - protected abstract Object deserializePropertyValue(String propValue); - - /** - * Serializes a property value. - * - * @param propValue value to be serialized - * @return the serialized property value - */ - protected abstract String serializePropertyValue(Object propValue); - - - @Override - public void clean() { - super.clean(); - - properties = PfUtils.mapMap(properties, String::trim); - } - - /** - * Validates the fields of the object, including its key. - * - * @param fieldName name of the field containing this - * @return the result, or {@code null} - */ - protected BeanValidationResult validateWithKey(String fieldName) { - BeanValidationResult result = super.validate(fieldName); - - validateKeyVersionNotNull(result, "key", getKey()); - - return result; - } - - @Override - public int compareTo(final PfConcept otherConcept) { - if (this == otherConcept) { - return 0; - } - - int result = super.compareTo(otherConcept); - if (result != 0) { - return result; - } - - @SuppressWarnings("unchecked") - final JpaToscaWithStringProperties other = (JpaToscaWithStringProperties) otherConcept; - - return PfUtils.compareMaps(properties, other.properties); - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithTypeAndStringProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithTypeAndStringProperties.java new file mode 100644 index 000000000..1ba63cad5 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithTypeAndStringProperties.java @@ -0,0 +1,226 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.simple.concepts; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.persistence.AttributeOverride; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Lob; +import javax.persistence.MappedSuperclass; +import javax.ws.rs.core.Response; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.validation.annotations.VerifyKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties; + +/** + * Class to represent JPA TOSCA classes containing property maps whose values are Strings. + */ +@MappedSuperclass +@Data +@EqualsAndHashCode(callSuper = true) +public abstract class JpaToscaWithTypeAndStringProperties + extends JpaToscaEntityType implements PfAuthorative { + + private static final long serialVersionUID = 2785481541573683089L; + + @Column + @AttributeOverride(name = "name", column = @Column(name = "type_name")) + @AttributeOverride(name = "version", column = @Column(name = "type_version")) + @VerifyKey + @NotNull + private PfConceptKey type; + + @ElementCollection + @Lob + private Map<@NotNull String, @NotNull String> properties; + + /** + * The Default Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with a null key. + */ + protected JpaToscaWithTypeAndStringProperties() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with the given concept key. + * + * @param key the key + */ + protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key) { + this(key, new PfConceptKey()); + } + + /** + * The full Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with all mandatory fields. + * + * @param key the key + * @param type the type of the policy + */ + protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) { + super(key); + this.type = type; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + protected JpaToscaWithTypeAndStringProperties(@NonNull final JpaToscaWithTypeAndStringProperties copyConcept) { + super(copyConcept); + this.type = new PfConceptKey(copyConcept.type); + this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + protected JpaToscaWithTypeAndStringProperties(final T authorativeConcept) { + super(new PfConceptKey()); + type = new PfConceptKey(); + this.fromAuthorative(authorativeConcept); + } + + @Override + public T toAuthorative() { + T tosca = super.toAuthorative(); + + tosca.setType(type.getName()); + + if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) { + tosca.setTypeVersion(type.getVersion()); + } else { + tosca.setTypeVersion(null); + } + + tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue)); + + return tosca; + } + + @Override + public void fromAuthorative(@NonNull final T authorativeConcept) { + super.fromAuthorative(authorativeConcept); + + if (authorativeConcept.getType() != null) { + type.setName(authorativeConcept.getType()); + } else { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, + "Type not specified, the type of this TOSCA entity must be specified in the type field"); + } + + if (authorativeConcept.getTypeVersion() != null) { + type.setVersion(authorativeConcept.getTypeVersion()); + } else { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, + "Version not specified, the version of this TOSCA entity must be specified" + + " in the type_version field"); + } + + properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue); + } + + /** + * Deserializes a property value. + * + * @param propValue value to be deserialized + * @return the deserialized property value + */ + protected abstract Object deserializePropertyValue(String propValue); + + /** + * Serializes a property value. + * + * @param propValue value to be serialized + * @return the serialized property value + */ + protected abstract String serializePropertyValue(Object propValue); + + @Override + public List getKeys() { + final List keyList = super.getKeys(); + + keyList.addAll(type.getKeys()); + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + type.clean(); + + properties = PfUtils.mapMap(properties, String::trim); + } + + /** + * Validates the fields of the object, including its key. + * + * @param fieldName name of the field containing this + * @return the result, or {@code null} + */ + protected BeanValidationResult validateWithKey(String fieldName) { + BeanValidationResult result = super.validate(fieldName); + + validateKeyVersionNotNull(result, "key", getKey()); + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (this == otherConcept) { + return 0; + } + + int result = super.compareTo(otherConcept); + if (result != 0) { + return result; + } + + @SuppressWarnings("unchecked") + final JpaToscaWithTypeAndStringProperties other = (JpaToscaWithTypeAndStringProperties) otherConcept; + + result = type.compareTo(other.type); + if (result != 0) { + return result; + } + + return PfUtils.compareMaps(properties, other.properties); + } +} 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 b806e4152..5dda6ecfc 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -145,7 +145,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing policy types to be checked */ public static void assertExist(final JpaToscaServiceTemplate serviceTemplate, - final Function checkerFunction) { + final Function checkerFunction) { String message = checkerFunction.apply(serviceTemplate); if (message != null) { throw new PfModelRuntimeException(Response.Status.NOT_FOUND, message); @@ -158,7 +158,7 @@ public final class ToscaUtils { * @param serviceTemplate the service template containing policy types to be checked */ public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate, - final Function checkerFunction) { + final Function checkerFunction) { return checkerFunction.apply(serviceTemplate) == null; } @@ -220,8 +220,8 @@ public final class ToscaUtils { * @return the entity set containing the ancestors of the incoming entity */ public static Collection> getEntityTypeAncestors( - @NonNull PfConceptContainer entityTypes, - @NonNull JpaToscaEntityType entityType, @NonNull final BeanValidationResult result) { + @NonNull PfConceptContainer entityTypes, + @NonNull JpaToscaEntityType entityType, @NonNull final BeanValidationResult result) { PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom(); if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) { @@ -230,17 +230,17 @@ public final class ToscaUtils { if (entityType.getKey().equals(parentEntityTypeKey)) { result.addResult(new ObjectValidationResult("entity type", entityType.getKey().getId(), - ValidationStatus.INVALID, "ancestor of itself")); + ValidationStatus.INVALID, "ancestor of itself")); throw new PfModelRuntimeException(Response.Status.CONFLICT, result.getResult()); } @SuppressWarnings("unchecked") Set> ancestorEntitySet = (Set>) entityTypes - .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion()); + .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion()); Set> ancestorEntitySetToReturn = new HashSet<>(ancestorEntitySet); if (ancestorEntitySet.isEmpty()) { - result.addResult(new ObjectValidationResult("parent", parentEntityTypeKey.getId(), - ValidationStatus.INVALID, Validated.NOT_FOUND)); + result.addResult(new ObjectValidationResult("parent", parentEntityTypeKey.getId(), ValidationStatus.INVALID, + Validated.NOT_FOUND)); } else { for (JpaToscaEntityType filteredEntityType : ancestorEntitySet) { ancestorEntitySetToReturn.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result)); @@ -257,18 +257,18 @@ public final class ToscaUtils { * @param entityVersion the version of the entity */ public static void getEntityTree( - @NonNull final PfConceptContainer entityTypes, - final String entityName, final String entityVersion) { + @NonNull final PfConceptContainer entityTypes, + final String entityName, final String entityVersion) { BeanValidationResult result = new BeanValidationResult("entity", entityName); @SuppressWarnings("unchecked") Set> filteredEntitySet = - (Set>) entityTypes.getAllNamesAndVersions(entityName, entityVersion); + (Set>) entityTypes.getAllNamesAndVersions(entityName, entityVersion); Set> filteredEntitySetToReturn = new HashSet<>(filteredEntitySet); for (JpaToscaEntityType filteredEntityType : filteredEntitySet) { filteredEntitySetToReturn - .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result)); + .addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result)); } if (!result.isValid()) { @@ -276,6 +276,6 @@ public final class ToscaUtils { } entityTypes.getConceptMap().entrySet() - .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue())); + .removeIf(entityEntry -> !filteredEntitySetToReturn.contains(entityEntry.getValue())); } } -- cgit 1.2.3-korg