From 5cd377c68b1f8e0520b7030b887e8a90803e2a3a Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 12 Feb 2021 13:55:31 -0500 Subject: Fix duplicate code in Jpa classes Addressed the following issues: - remove duplicate code; extracted a common class containing properties Issue-ID: POLICY-2905 Change-Id: I80c423ab45c0c6823d338a034762d222ec334288 Signed-off-by: Jim Hahn --- .../concepts/ToscaCapabilityAssignment.java | 4 +- .../authorative/concepts/ToscaCapabilityType.java | 6 +- .../tosca/authorative/concepts/ToscaDataType.java | 7 +- .../authorative/concepts/ToscaNodeTemplate.java | 5 +- .../tosca/authorative/concepts/ToscaNodeType.java | 4 +- .../tosca/authorative/concepts/ToscaPolicy.java | 16 +- .../authorative/concepts/ToscaPolicyType.java | 15 +- .../concepts/ToscaRelationshipType.java | 6 +- .../authorative/concepts/ToscaRequirement.java | 5 +- .../concepts/ToscaWithObjectProperties.java | 54 ++++++ .../concepts/ToscaWithToscaProperties.java | 53 +++++ .../concepts/JpaToscaCapabilityAssignment.java | 44 ++--- .../simple/concepts/JpaToscaCapabilityType.java | 141 +------------- .../tosca/simple/concepts/JpaToscaDataType.java | 106 +--------- .../simple/concepts/JpaToscaNodeTemplate.java | 68 +++---- .../tosca/simple/concepts/JpaToscaNodeType.java | 102 +--------- .../tosca/simple/concepts/JpaToscaPolicy.java | 67 +++---- .../tosca/simple/concepts/JpaToscaPolicyType.java | 101 +--------- .../simple/concepts/JpaToscaRelationshipType.java | 140 +------------- .../tosca/simple/concepts/JpaToscaRequirement.java | 63 ++---- .../concepts/JpaToscaWithStringProperties.java | 162 ++++++++++++++++ .../concepts/JpaToscaWithToscaProperties.java | 201 +++++++++++++++++++ .../concepts/ToscaWithObjectPropertiesTest.java | 45 +++++ .../concepts/JpaToscaWithStringPropertiesTest.java | 214 +++++++++++++++++++++ .../models/tosca/simple/concepts/TestPojos.java | 7 +- 25 files changed, 870 insertions(+), 766 deletions(-) create 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/ToscaWithToscaProperties.java create 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/JpaToscaWithToscaProperties.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java (limited to 'models-tosca') 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 d4925be6d..2d9cf9a39 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,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. + * Modifications 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. @@ -29,8 +30,7 @@ import lombok.NoArgsConstructor; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaCapabilityAssignment extends ToscaEntity { - private Map properties; +public class ToscaCapabilityAssignment extends ToscaWithObjectProperties { private Map attributes; private List occurrences; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityType.java index dd8fb05f4..ad8da07ae 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaCapabilityType.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2020 Nordix Foundation. + * Modifications 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. @@ -22,7 +23,6 @@ package org.onap.policy.models.tosca.authorative.concepts; -import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -30,6 +30,6 @@ import lombok.NoArgsConstructor; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaCapabilityType extends ToscaEntity { - private Map properties; +public class ToscaCapabilityType extends ToscaWithToscaProperties { + } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java index 6ea8804ca..6705e5248 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,7 +24,6 @@ package org.onap.policy.models.tosca.authorative.concepts; import java.util.List; -import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -37,8 +36,6 @@ import lombok.NoArgsConstructor; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaDataType extends ToscaEntity { +public class ToscaDataType extends ToscaWithToscaProperties { private List constraints; - - private Map properties; } 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 d470447e6..c4bc84cd8 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 @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * 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"); * you may not use this file except in compliance with the License. @@ -34,9 +35,8 @@ import lombok.NonNull; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaNodeTemplate extends ToscaEntity { +public class ToscaNodeTemplate extends ToscaWithObjectProperties { private String type; - private Map properties; private List> requirements; private Map capabilities; @@ -50,7 +50,6 @@ public class ToscaNodeTemplate extends ToscaEntity { this.type = copyObject.type; - this.properties = (copyObject.properties != null ? new LinkedHashMap<>(copyObject.properties) : null); 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/ToscaNodeType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeType.java index 5a4c94126..6c03623d7 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaNodeType.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2020 Nordix Foundation. + * Modifications 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. @@ -31,7 +32,6 @@ import lombok.NoArgsConstructor; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaNodeType extends ToscaEntity { - private Map properties; +public class ToscaNodeType extends ToscaWithToscaProperties { private List> requirements; } 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 491b478a4..b3a38050a 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,9 +25,6 @@ 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 java.util.Map.Entry; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -43,15 +40,13 @@ import lombok.ToString; @EqualsAndHashCode(callSuper = true) @NoArgsConstructor @ToString(callSuper = true) -public class ToscaPolicy extends ToscaEntity { +public class ToscaPolicy extends ToscaWithObjectProperties { private String type; @ApiModelProperty(name = "type_version") @SerializedName("type_version") private String typeVersion; - private Map properties; - /** * Copy constructor. * @@ -62,13 +57,6 @@ public class ToscaPolicy extends ToscaEntity { this.type = copyObject.type; this.typeVersion = copyObject.typeVersion; - - if (copyObject.properties != null) { - properties = new LinkedHashMap<>(); - for (final Entry propertyEntry : copyObject.properties.entrySet()) { - properties.put(propertyEntry.getKey(), propertyEntry.getValue()); - } - } } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java index af8f5fa90..671ba3285 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,9 +23,6 @@ package org.onap.policy.models.tosca.authorative.concepts; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -39,8 +36,7 @@ import lombok.NonNull; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaPolicyType extends ToscaEntity { - private Map properties; +public class ToscaPolicyType extends ToscaWithToscaProperties { /** * Copy Constructor. @@ -49,12 +45,5 @@ public class ToscaPolicyType extends ToscaEntity { */ public ToscaPolicyType(@NonNull ToscaPolicyType copyObject) { super(copyObject); - - if (copyObject.properties != null) { - properties = new LinkedHashMap<>(); - for (final Entry propertyEntry : copyObject.properties.entrySet()) { - properties.put(propertyEntry.getKey(), propertyEntry.getValue()); - } - } } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRelationshipType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRelationshipType.java index 9e43d8954..6b9415f25 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRelationshipType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaRelationshipType.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2020 Nordix Foundation. + * Modifications 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. @@ -22,7 +23,6 @@ package org.onap.policy.models.tosca.authorative.concepts; -import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -30,6 +30,6 @@ import lombok.NoArgsConstructor; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaRelationshipType extends ToscaEntity { - private Map properties; +public class ToscaRelationshipType extends ToscaWithToscaProperties { + } 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 f633e67b7..166b81174 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,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. + * Modifications 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. @@ -21,16 +22,14 @@ package org.onap.policy.models.tosca.authorative.concepts; import java.util.List; -import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; @Data @EqualsAndHashCode(callSuper = true) -public class ToscaRequirement extends ToscaEntity { +public class ToscaRequirement extends ToscaWithObjectProperties { private String capability; private String node; private String relationship; private List occurrences; - private Map properties; } 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 new file mode 100644 index 000000000..f6cf24f61 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectProperties.java @@ -0,0 +1,54 @@ +/* + * ============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/ToscaWithToscaProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithToscaProperties.java new file mode 100644 index 000000000..49332bd52 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithToscaProperties.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * 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; + +/** + * Class to represent TOSCA classes containing TOSCA properties. + */ +@Data +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +public class ToscaWithToscaProperties extends ToscaEntity { + private Map properties; + + /** + * Copy Constructor. + * + * @param copyObject object to copy from + */ + public ToscaWithToscaProperties(@NonNull ToscaWithToscaProperties 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/simple/concepts/JpaToscaCapabilityAssignment.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java index 1247a3e5a..bb5cf5a73 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 @@ -36,7 +36,6 @@ import lombok.EqualsAndHashCode; import lombok.NonNull; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.utils.coder.YamlJsonTranslator; -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; @@ -51,8 +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 JpaToscaEntityType - implements PfAuthorative { +public class JpaToscaCapabilityAssignment extends JpaToscaWithStringProperties { private static final long serialVersionUID = 1675770231921107988L; @@ -61,10 +59,6 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType properties; - @ElementCollection @Lob private Map<@NotNull String, @NotNull String> attributes; @@ -95,7 +89,6 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType(copyConcept.properties); this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes); this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences); } @@ -106,8 +99,7 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType YAML_JSON_TRANSLATOR.fromYaml(property, Object.class))); - toscaCapabilityAssignment.setAttributes( PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class))); @@ -137,8 +126,6 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType { @@ -150,38 +137,35 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType - implements PfAuthorative { +@NoArgsConstructor +public class JpaToscaCapabilityType extends JpaToscaWithToscaProperties { private static final long serialVersionUID = -563659852901842616L; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties; - - /** - * The Default Constructor creates a {@link JpaToscaCapabilityType} object with a null key. - */ - public JpaToscaCapabilityType() { - this(new PfConceptKey()); - } - /** * The Key Constructor creates a {@link JpaToscaCapabilityType} object with the given concept key. * @@ -91,7 +62,6 @@ public class JpaToscaCapabilityType extends JpaToscaEntityType(copyConcept.properties); } /** @@ -100,114 +70,17 @@ public class JpaToscaCapabilityType extends JpaToscaEntityType(); - for (Entry toscaPropertyEntry : toscaCapabilityType.getProperties().entrySet()) { - JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); - jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); - properties.put(toscaPropertyEntry.getKey(), jpaProperty); - } - } - } - - @Override - public List getKeys() { - final List keyList = super.getKeys(); - - PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys())); - - - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - keyList.addAll(property.getKeys()); - } - } - - return keyList; - } - - @Override - public void clean() { - super.clean(); - - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - property.clean(); - } - } + super.setToscaEntity(new ToscaCapabilityType()); + return super.toAuthorative(); } @Override public BeanValidationResult validate(@NonNull String fieldName) { - BeanValidationResult result = super.validate(fieldName); - - validateKeyVersionNotNull(result, "key", getKey()); - - return result; - } - - @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { - return 0; - } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - - final JpaToscaCapabilityType other = (JpaToscaCapabilityType) otherConcept; - int result = super.compareTo(other); - if (result != 0) { - return result; - } - - return PfUtils.compareMaps(properties, other.properties); - } - - /** - * Get the data types referenced in a capability type. - * - * @return the data types referenced in a capability type - */ - public Collection getReferencedDataTypes() { - if (properties == null) { - return CollectionUtils.emptyCollection(); - } - - Set referencedDataTypes = new LinkedHashSet<>(); - - for (JpaToscaProperty property : properties.values()) { - referencedDataTypes.add(property.getType()); - - if (property.getEntrySchema() != null) { - referencedDataTypes.add(property.getEntrySchema().getType()); - } - } - - referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes()); - - return referencedDataTypes; + return validateWithKey(fieldName); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java index 515753167..bcbf40086 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java @@ -24,34 +24,22 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; -import javax.persistence.Lob; import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import lombok.NonNull; -import org.apache.commons.collections4.CollectionUtils; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.parameters.annotations.Valid; -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.PfReferenceKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; -import org.onap.policy.models.tosca.utils.ToscaUtils; /** * Class to represent custom data type in TOSCA definition. @@ -64,23 +52,13 @@ import org.onap.policy.models.tosca.utils.ToscaUtils; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaDataType extends JpaToscaEntityType implements PfAuthorative { +@NoArgsConstructor +public class JpaToscaDataType extends JpaToscaWithToscaProperties { private static final long serialVersionUID = -3922690413436539164L; @ElementCollection private List<@NotNull @Valid JpaToscaConstraint> constraints; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties; - - /** - * The Default Constructor creates a {@link JpaToscaDataType} object with a null key. - */ - public JpaToscaDataType() { - this(new PfConceptKey()); - } - /** * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key. * @@ -99,7 +77,6 @@ public class JpaToscaDataType extends JpaToscaEntityType implemen super(copyConcept); // Constraints are immutable this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null); - this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new); } /** @@ -108,7 +85,7 @@ public class JpaToscaDataType extends JpaToscaEntityType implemen * @param authorativeConcept the authorative concept to copy from */ public JpaToscaDataType(final ToscaDataType authorativeConcept) { - this.fromAuthorative(authorativeConcept); + super(authorativeConcept); } @Override @@ -118,7 +95,6 @@ public class JpaToscaDataType extends JpaToscaEntityType implemen super.toAuthorative(); toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative)); - toscaDataType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); return toscaDataType; } @@ -128,89 +104,21 @@ public class JpaToscaDataType extends JpaToscaEntityType implemen super.fromAuthorative(toscaDataType); constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance); - - if (toscaDataType.getProperties() != null) { - properties = new LinkedHashMap<>(); - for (Entry toscaPropertyEntry : toscaDataType.getProperties().entrySet()) { - JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); - jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); - properties.put(toscaPropertyEntry.getKey(), jpaProperty); - } - } - } - - @Override - public List getKeys() { - final List keyList = super.getKeys(); - - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - keyList.addAll(property.getKeys()); - } - } - - return keyList; - } - - @Override - public void clean() { - super.clean(); - - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - property.clean(); - } - } } @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } if (this == otherConcept) { return 0; } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - - final JpaToscaDataType other = (JpaToscaDataType) otherConcept; - int result = super.compareTo(other); - if (result != 0) { - return result; - } - result = PfUtils.compareCollections(constraints, other.constraints); + int result = super.compareTo(otherConcept); if (result != 0) { return result; } - return PfUtils.compareMaps(properties, other.properties); - } - - /** - * Get the data types referenced in a data type. - * - * @return the data types referenced in a data type - */ - public Collection getReferencedDataTypes() { - if (properties == null) { - return CollectionUtils.emptyCollection(); - } - - Set referencedDataTypes = new LinkedHashSet<>(); - - for (JpaToscaProperty property : properties.values()) { - referencedDataTypes.add(property.getType()); - - if (property.getEntrySchema() != null) { - referencedDataTypes.add(property.getEntrySchema().getType()); - } - } - - referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes()); + final JpaToscaDataType other = (JpaToscaDataType) otherConcept; - return referencedDataTypes; + return PfUtils.compareCollections(constraints, other.constraints); } } 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 c28a1aefb..9507a9d2f 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 @@ -27,13 +27,11 @@ import java.util.List; import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; -import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; -import javax.persistence.Lob; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.ws.rs.core.Response; @@ -46,12 +44,10 @@ 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; -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.tosca.authorative.concepts.ToscaCapabilityAssignment; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; @@ -63,8 +59,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaNodeTemplate extends JpaToscaEntityType - implements PfAuthorative { +public class JpaToscaNodeTemplate extends JpaToscaWithStringProperties { private static final long serialVersionUID = 1675770231921107988L; private static final StandardCoder STANDARD_CODER = new StandardCoder(); @@ -74,10 +69,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType @NotBlank private String type; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull String> properties; - // formatter:off @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "requirementsName", referencedColumnName = "name") @@ -116,7 +107,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) { super(copyConcept); this.type = copyConcept.type; - this.properties = PfUtils.mapMap(copyConcept.properties, String::new); this.requirements = (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null); this.capabilities = @@ -151,15 +141,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType toscaNodeTemplate.setType(type); - toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> { - try { - return STANDARD_CODER.decode(property, Object.class); - } catch (CoderException ce) { - String errorMessage = "error decoding property JSON value read from database: " + property; - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); - } - })); - if (requirements != null) { toscaNodeTemplate.setRequirements(requirements.toAuthorative()); } @@ -181,15 +162,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType type = toscaNodeTemplate.getType(); - properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> { - try { - return STANDARD_CODER.encode(property); - } catch (CoderException ce) { - String errorMessage = "error encoding property JSON value for database: " + property; - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); - } - }); - if (toscaNodeTemplate.getRequirements() != null) { requirements = new JpaToscaRequirements(); requirements.fromAuthorative(toscaNodeTemplate.getRequirements()); @@ -201,6 +173,26 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType } } + @Override + protected Object deserializePropertyValue(String propValue) { + try { + return STANDARD_CODER.decode(propValue, Object.class); + } catch (CoderException ce) { + String errorMessage = "error decoding property JSON value read from database: " + propValue; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } + } + + @Override + protected String serializePropertyValue(Object propValue) { + try { + return STANDARD_CODER.encode(propValue); + } catch (CoderException ce) { + String errorMessage = "error encoding property JSON value for database: " + propValue; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } + } + @Override public List getKeys() { final List keyList = super.getKeys(); @@ -222,8 +214,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType type = type.trim(); - properties = PfUtils.mapMap(properties, String::trim); - if (requirements != null) { requirements.clean(); } @@ -235,28 +225,18 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } if (this == otherConcept) { return 0; } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept; - int result = super.compareTo(other); + int result = super.compareTo(otherConcept); if (result != 0) { return result; } - result = type.compareTo(other.type); - if (result != 0) { - return result; - } + final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept; - result = PfUtils.compareMaps(properties, other.properties); + result = type.compareTo(other.type); if (result != 0) { return result; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java index 86be4b318..ee0f68e0a 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java @@ -21,40 +21,26 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; import javax.persistence.CascadeType; -import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; -import javax.persistence.Lob; import javax.persistence.OneToOne; import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import lombok.NonNull; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.parameters.annotations.Valid; -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.PfReferenceKey; -import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; -import org.onap.policy.models.tosca.utils.ToscaUtils; /** * Class to represent the node type in TOSCA definition. @@ -65,13 +51,10 @@ import org.onap.policy.models.tosca.utils.ToscaUtils; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaNodeType extends JpaToscaEntityType implements PfAuthorative { +@NoArgsConstructor +public class JpaToscaNodeType extends JpaToscaWithToscaProperties { private static final long serialVersionUID = -563659852901842616L; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties; - // formatter:off @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @@ -81,13 +64,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen @Valid private JpaToscaRequirements requirements; - /** - * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key. - */ - public JpaToscaNodeType() { - this(new PfConceptKey()); - } - /** * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key. * @@ -104,7 +80,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen */ public JpaToscaNodeType(final JpaToscaNodeType copyConcept) { super(copyConcept); - this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new); this.requirements = (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null); } @@ -115,7 +90,7 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen * @param authorativeConcept the authorative concept to copy from */ public JpaToscaNodeType(final ToscaNodeType authorativeConcept) { - this.fromAuthorative(authorativeConcept); + super(authorativeConcept); } @Override @@ -124,8 +99,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen super.setToscaEntity(toscaNodeType); super.toAuthorative(); - toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); - if (requirements != null) { toscaNodeType.setRequirements(requirements.toAuthorative()); } @@ -137,16 +110,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen public void fromAuthorative(final ToscaNodeType toscaNodeType) { super.fromAuthorative(toscaNodeType); - // Set properties - if (toscaNodeType.getProperties() != null) { - properties = new LinkedHashMap<>(); - for (Entry toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) { - JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); - jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); - properties.put(toscaPropertyEntry.getKey(), jpaProperty); - } - } - if (toscaNodeType.getRequirements() != null) { requirements = new JpaToscaRequirements(); requirements.fromAuthorative(toscaNodeType.getRequirements()); @@ -157,12 +120,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen public List getKeys() { final List keyList = super.getKeys(); - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - keyList.addAll(property.getKeys()); - } - } - if (requirements != null) { keyList.addAll(requirements.getKeys()); } @@ -174,12 +131,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen public void clean() { super.clean(); - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - property.clean(); - } - } - if (requirements != null) { requirements.clean(); } @@ -187,61 +138,22 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen @Override public BeanValidationResult validate(String fieldName) { - BeanValidationResult result = super.validate(fieldName); - - validateKeyVersionNotNull(result, "key", getKey()); - - return result; + return validateWithKey(fieldName); } @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } if (this == otherConcept) { return 0; } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept; - int result = super.compareTo(other); + int result = super.compareTo(otherConcept); if (result != 0) { return result; } - result = PfUtils.compareMaps(properties, other.properties); - if (result != 0) { - return result; - } + final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept; return ObjectUtils.compare(requirements, other.requirements); } - - /** - * Get the data types referenced in a node type. - * - * @return the data types referenced in a node type - */ - public Collection getReferencedDataTypes() { - if (properties == null) { - return CollectionUtils.emptyCollection(); - } - - Set referencedDataTypes = new LinkedHashSet<>(); - - for (JpaToscaProperty property : properties.values()) { - referencedDataTypes.add(property.getType()); - - if (property.getEntrySchema() != null) { - referencedDataTypes.add(property.getEntrySchema().getType()); - } - } - - referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes()); - - return referencedDataTypes; - } } 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 11f1199c0..dae3527ee 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 @@ -25,26 +25,22 @@ 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.Entity; import javax.persistence.Inheritance; 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.parameters.BeanValidationResult; -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; -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; @@ -64,7 +60,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaPolicy extends JpaToscaEntityType implements PfAuthorative { +public class JpaToscaPolicy extends JpaToscaWithStringProperties { private static final long serialVersionUID = 3265174757061982805L; // Tags for metadata @@ -81,10 +77,6 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P @NotNull private PfConceptKey type; - @ElementCollection - @Lob - private Map<@NotNull @NotBlank String, @NotNull String> properties; - @ElementCollection private List<@NotNull @Valid PfConceptKey> targets; // @formatter:on @@ -124,7 +116,6 @@ 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); } @@ -153,15 +144,6 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P toscaPolicy.setTypeVersion(null); } - toscaPolicy.setProperties(PfUtils.mapMap(properties, property -> { - try { - return STANDARD_CODER.decode(property, Object.class); - } catch (CoderException ce) { - String errorMessage = "error decoding property JSON value read from database: " + property; - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); - } - })); - return toscaPolicy; } @@ -185,15 +167,6 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P + " in the type_version field"); } - properties = PfUtils.mapMap(toscaPolicy.getProperties(), property -> { - try { - return STANDARD_CODER.encode(property); - } catch (CoderException ce) { - String errorMessage = "error encoding property JSON value for database: " + property; - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); - } - }); - // Add the property metadata if it doesn't exist already if (toscaPolicy.getMetadata() == null) { setMetadata(new LinkedHashMap<>()); @@ -204,6 +177,26 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion()); } + @Override + protected Object deserializePropertyValue(String propValue) { + try { + return STANDARD_CODER.decode(propValue, Object.class); + } catch (CoderException ce) { + String errorMessage = "error decoding property JSON value read from database: " + propValue; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } + } + + @Override + protected String serializePropertyValue(Object propValue) { + try { + return STANDARD_CODER.encode(propValue); + } catch (CoderException ce) { + String errorMessage = "error encoding property JSON value for database: " + propValue; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } + } + @Override public List getKeys() { final List keyList = super.getKeys(); @@ -241,30 +234,18 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { return 0; } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - - final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept; - int result = super.compareTo(other); + int result = super.compareTo(otherConcept); if (result != 0) { return result; } - result = type.compareTo(other.type); - if (result != 0) { - return result; - } + final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept; - result = PfUtils.compareMaps(properties, other.properties); + result = type.compareTo(other.type); if (result != 0) { return result; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java index ddde08985..8e40384d8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java @@ -23,35 +23,24 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; -import javax.persistence.Lob; import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import lombok.NonNull; -import org.apache.commons.collections4.CollectionUtils; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.parameters.annotations.Valid; -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.PfReferenceKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; -import org.onap.policy.models.tosca.utils.ToscaUtils; /** * Class to represent the policy type in TOSCA definition. @@ -65,26 +54,16 @@ import org.onap.policy.models.tosca.utils.ToscaUtils; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaPolicyType extends JpaToscaEntityType implements PfAuthorative { +@NoArgsConstructor +public class JpaToscaPolicyType extends JpaToscaWithToscaProperties { private static final long serialVersionUID = -563659852901842616L; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties; - @ElementCollection private List<@NotNull @Valid PfConceptKey> targets; @ElementCollection private List<@NotNull @Valid JpaToscaTrigger> triggers; - /** - * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key. - */ - public JpaToscaPolicyType() { - this(new PfConceptKey()); - } - /** * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key. * @@ -101,7 +80,6 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl */ public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) { super(copyConcept); - this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new); this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new); this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new); } @@ -112,7 +90,7 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl * @param authorativeConcept the authorative concept to copy from */ public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) { - this.fromAuthorative(authorativeConcept); + super(authorativeConcept); } @Override @@ -121,7 +99,7 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl super.setToscaEntity(toscaPolicyType); super.toAuthorative(); - toscaPolicyType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); + // TODO need to copy targets & triggers? return toscaPolicyType; } @@ -130,27 +108,13 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl public void fromAuthorative(final ToscaPolicyType toscaPolicyType) { super.fromAuthorative(toscaPolicyType); - // Set properties - if (toscaPolicyType.getProperties() != null) { - properties = new LinkedHashMap<>(); - for (Entry toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) { - JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); - jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); - properties.put(toscaPropertyEntry.getKey(), jpaProperty); - } - } + // TODO need to copy targets & triggers? } @Override public List getKeys() { final List keyList = super.getKeys(); - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - keyList.addAll(property.getKeys()); - } - } - if (targets != null) { keyList.addAll(targets); } @@ -168,12 +132,6 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl public void clean() { super.clean(); - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - property.clean(); - } - } - if (targets != null) { for (PfConceptKey target : targets) { target.clean(); @@ -188,36 +146,22 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl } @Override - public BeanValidationResult validate(String fieldName) { - BeanValidationResult result = super.validate(fieldName); - - validateKeyVersionNotNull(result, "key", getKey()); - - return result; + public BeanValidationResult validate(@NonNull String fieldName) { + return validateWithKey(fieldName); } @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } if (this == otherConcept) { return 0; } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept; - int result = super.compareTo(other); + int result = super.compareTo(otherConcept); if (result != 0) { return result; } - result = PfUtils.compareMaps(properties, other.properties); - if (result != 0) { - return result; - } + final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept; result = PfUtils.compareCollections(targets, other.targets); if (result != 0) { @@ -226,29 +170,4 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl return PfUtils.compareCollections(triggers, other.triggers); } - - /** - * Get the data types referenced in a policy type. - * - * @return the data types referenced in a policy type - */ - public Collection getReferencedDataTypes() { - if (properties == null) { - return CollectionUtils.emptyCollection(); - } - - Set referencedDataTypes = new LinkedHashSet<>(); - - for (JpaToscaProperty property : properties.values()) { - referencedDataTypes.add(property.getType()); - - if (property.getEntrySchema() != null) { - referencedDataTypes.add(property.getEntrySchema().getType()); - } - } - - referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes()); - - return referencedDataTypes; - } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java index 1c7ead32f..118211153 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java @@ -21,35 +21,17 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; -import javax.persistence.Lob; import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import lombok.NonNull; -import org.apache.commons.collections4.CollectionUtils; import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.common.parameters.annotations.Valid; -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.PfReferenceKey; -import org.onap.policy.models.base.PfUtils; -import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType; -import org.onap.policy.models.tosca.utils.ToscaUtils; /** * Class to represent the relationship type in TOSCA definition. @@ -60,21 +42,10 @@ import org.onap.policy.models.tosca.utils.ToscaUtils; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaRelationshipType extends JpaToscaEntityType - implements PfAuthorative { +@NoArgsConstructor +public class JpaToscaRelationshipType extends JpaToscaWithToscaProperties { private static final long serialVersionUID = -563659852901842616L; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties; - - /** - * The Default Constructor creates a {@link JpaToscaRelationshipType} object with a null key. - */ - public JpaToscaRelationshipType() { - this(new PfConceptKey()); - } - /** * The Key Constructor creates a {@link JpaToscaRelationshipType} object with the given concept key. * @@ -91,7 +62,6 @@ public class JpaToscaRelationshipType extends JpaToscaEntityType(); - for (Entry toscaPropertyEntry : toscaRelationshipType.getProperties().entrySet()) { - JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); - jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); - properties.put(toscaPropertyEntry.getKey(), jpaProperty); - } - } - } - - @Override - public List getKeys() { - final List keyList = super.getKeys(); - - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - keyList.addAll(property.getKeys()); - } - } - - return keyList; - } - - @Override - public void clean() { - super.clean(); - - if (properties != null) { - for (JpaToscaProperty property : properties.values()) { - property.clean(); - } - } - } - - @Override - public BeanValidationResult validate(String fieldName) { - BeanValidationResult result = super.validate(fieldName); - - validateKeyVersionNotNull(result, "key", getKey()); - - return result; - } - - @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { - return 0; - } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - - final JpaToscaRelationshipType other = (JpaToscaRelationshipType) otherConcept; - int result = super.compareTo(other); - if (result != 0) { - return result; - } - - return PfUtils.compareMaps(properties, other.properties); - } - - /** - * Get the data types referenced in a relationship type. - * - * @return the data types referenced in a relationship type - */ - public Collection getReferencedDataTypes() { - if (properties == null) { - return CollectionUtils.emptyCollection(); - } - - Set referencedDataTypes = new LinkedHashSet<>(); - - for (JpaToscaProperty property : properties.values()) { - referencedDataTypes.add(property.getType()); - - if (property.getEntrySchema() != null) { - referencedDataTypes.add(property.getEntrySchema().getType()); - } - } - - referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes()); - - return referencedDataTypes; + public BeanValidationResult validate(@NonNull String fieldName) { + return validateWithKey(fieldName); } } 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 aeb93b3e7..eeae03dd7 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 @@ -24,22 +24,18 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; -import javax.persistence.Lob; import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.utils.coder.YamlJsonTranslator; -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; @@ -57,12 +53,12 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaRequirement extends JpaToscaEntityType - implements PfAuthorative { +public class JpaToscaRequirement extends JpaToscaWithStringProperties { private static final long serialVersionUID = 2785481541573683089L; private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED"; private static final Integer JPA_UNBOUNDED_VALUE = -1; + private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator(); @Column private String capability; @@ -76,10 +72,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType @ElementCollection private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences; - @ElementCollection - @Lob - private Map<@NotNull String, @NotNull String> properties; - /** * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key. */ @@ -107,7 +99,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType this.node = copyConcept.node; this.relationship = copyConcept.relationship; this.occurrences = new ArrayList<>(copyConcept.occurrences); - this.properties = PfUtils.mapMap(copyConcept.properties, String::new); } /** @@ -116,8 +107,7 @@ public class JpaToscaRequirement extends JpaToscaEntityType * @param authorativeConcept the authorative concept to copy from */ public JpaToscaRequirement(final ToscaRequirement authorativeConcept) { - super(new PfConceptKey()); - this.fromAuthorative(authorativeConcept); + super(authorativeConcept); } @Override @@ -142,16 +132,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType toscaRequirement.setOccurrences(occurrencesList); } - if (properties != null) { - Map propertiesMap = new LinkedHashMap<>(); - - for (Map.Entry entry : properties.entrySet()) { - propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class)); - } - - toscaRequirement.setProperties(propertiesMap); - } - return toscaRequirement; } @@ -173,15 +153,16 @@ public class JpaToscaRequirement extends JpaToscaEntityType } } } + } - if (toscaRequirement.getProperties() != null) { - properties = new LinkedHashMap<>(); - for (Map.Entry toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) { - String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue()); - properties.put(toscaPropertyEntry.getKey(), jpaProperty); - } - } + @Override + protected Object deserializePropertyValue(String propValue) { + return YAML_TRANSLATOR.fromYaml(propValue, Object.class); + } + @Override + protected String serializePropertyValue(Object propValue) { + return YAML_TRANSLATOR.toYaml(propValue); } @Override @@ -191,30 +172,21 @@ public class JpaToscaRequirement extends JpaToscaEntityType capability = capability.trim(); node = node.trim(); relationship = relationship.trim(); - - properties = PfUtils.mapMap(properties, String::trim); } @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { return 0; } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - - final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept; - int result = super.compareTo(other); + int result = super.compareTo(otherConcept); if (result != 0) { return result; } + final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept; + result = capability.compareTo(other.capability); if (result != 0) { return result; @@ -230,11 +202,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType return result; } - result = PfUtils.compareCollections(occurrences, other.occurrences); - if (result != 0) { - return result; - } - - return PfUtils.compareMaps(properties, other.properties); + return PfUtils.compareCollections(occurrences, other.occurrences); } } 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 new file mode 100644 index 000000000..54bd54073 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java @@ -0,0 +1,162 @@ +/*- + * ============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. + */ + public JpaToscaWithStringProperties() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaWithStringProperties} object with the + * given concept key. + * + * @param key the key + */ + public JpaToscaWithStringProperties(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public 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 + */ + public 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/JpaToscaWithToscaProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaProperties.java new file mode 100644 index 000000000..873d900b0 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaProperties.java @@ -0,0 +1,201 @@ +/*- + * ============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.Collection; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import javax.persistence.ElementCollection; +import javax.persistence.Lob; +import javax.persistence.MappedSuperclass; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.apache.commons.collections4.CollectionUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +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.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.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; +import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * Class to represent JPA TOSCA classes containing TOSCA properties. + */ +@MappedSuperclass +@Data +@EqualsAndHashCode(callSuper = true) +public abstract class JpaToscaWithToscaProperties extends JpaToscaEntityType + implements PfAuthorative { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map<@NotNull @NotBlank String, @NotNull @Valid JpaToscaProperty> properties; + + /** + * The Default Constructor creates a {@link JpaToscaWithToscaProperties} object with a + * null key. + */ + public JpaToscaWithToscaProperties() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaWithToscaProperties} object with the + * given concept key. + * + * @param key the key + */ + public JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties copyConcept) { + super(copyConcept); + this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaWithToscaProperties(final T authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public void fromAuthorative(final T authorativeConcept) { + super.fromAuthorative(authorativeConcept); + + // Set properties + if (authorativeConcept.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Entry toscaPropertyEntry : authorativeConcept.getProperties().entrySet()) { + JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); + jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + } + + @Override + public T toAuthorative() { + T tosca = super.toAuthorative(); + + tosca.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); + + return tosca; + } + + @Override + public List getKeys() { + final List keyList = super.getKeys(); + + PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys())); + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + property.clean(); + } + } + } + + /** + * 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(@NonNull 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 JpaToscaWithToscaProperties other = (JpaToscaWithToscaProperties) otherConcept; + + return PfUtils.compareMaps(properties, other.properties); + } + + /** + * Get the referenced data types. + * + * @return the referenced data types + */ + public Collection getReferencedDataTypes() { + if (properties == null) { + return CollectionUtils.emptyCollection(); + } + + Set referencedDataTypes = new LinkedHashSet<>(); + + for (JpaToscaProperty property : properties.values()) { + referencedDataTypes.add(property.getType()); + + if (property.getEntrySchema() != null) { + referencedDataTypes.add(property.getEntrySchema().getType()); + } + } + + referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes()); + + return referencedDataTypes; + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java new file mode 100644 index 000000000..7186a3ff0 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.Map; +import org.junit.Test; + +public class ToscaWithObjectPropertiesTest { + + @Test + public void testCopyConstructor() { + ToscaWithObjectProperties tosca = new ToscaWithObjectProperties(); + assertEquals(tosca, new ToscaWithObjectProperties(tosca)); + + tosca.setProperties(Map.of("abc", 10, "def", "world")); + assertEquals(tosca, new ToscaWithObjectProperties(tosca)); + + assertNotEquals(tosca, new ToscaWithObjectProperties()); + + assertThatThrownBy(() -> new ToscaWithObjectProperties(null)).hasMessageContaining("copyObject") + .hasMessageContaining("is null"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java new file mode 100644 index 000000000..23e0e6a7e --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java @@ -0,0 +1,214 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.simple.concepts; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; + +import java.util.List; +import java.util.Map; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaWithObjectProperties; + +public class JpaToscaWithStringPropertiesTest { + private static final String SOME_DESCRIPTION = "some description"; + private static final String KEY1 = "abc"; + private static final String KEY2 = "def"; + private static final String STRING1 = "10"; + private static final String STRING2 = "20"; + private static final int INT1 = 10; + private static final int INT2 = 20; + + private MyJpa jpa; + + @Before + public void setUp() { + jpa = new MyJpa(); + } + + @Test + public void testGetKeys() { + PfConceptKey key = new PfConceptKey("bye", "9.8.7"); + + jpa = new MyJpa(key); + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + + // properties should be ignored + assertThat(jpa.getKeys()).isEqualTo(List.of(key)); + } + + @Test + public void testClean() { + jpa.setDescription(" some description "); + jpa.setProperties(Map.of(KEY1, "10 ", KEY2, " 20")); + + jpa.clean(); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2)); + } + + @Test + public void testToAuthorative() { + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + + MyTosca tosca = jpa.toAuthorative(); + assertEquals(SOME_DESCRIPTION, tosca.getDescription()); + assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, INT1, KEY2, INT2)); + } + + @Test + public void testFromAuthorative() { + MyTosca tosca = new MyTosca(); + tosca.setDescription(SOME_DESCRIPTION); + + jpa.fromAuthorative(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isNull(); + + tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2)); + + jpa = new MyJpa(); + jpa.fromAuthorative(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2)); + } + + @Test + public void testCompareTo() { + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + + assertThat(jpa.compareTo(null)).isNegative(); + assertThat(jpa.compareTo(jpa)).isZero(); + assertThat(jpa.compareTo(new PfConceptKey())).isNotZero(); + + MyJpa jpa2 = new MyJpa(); + jpa2.setDescription(SOME_DESCRIPTION); + jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + assertThat(jpa.compareTo(jpa2)).isZero(); + + jpa2.setProperties(Map.of(KEY1, STRING1)); + assertThat(jpa.compareTo(jpa2)).isNotZero(); + } + + @Test + public void testJpaToscaWithStringProperties() { + assertThat(jpa.getProperties()).isNull(); + assertThat(jpa.getKey().isNullKey()).isTrue(); + + } + + @Test + public void testJpaToscaWithStringPropertiesPfConceptKey() { + PfConceptKey key = new PfConceptKey("hello", "1.2.3"); + + jpa = new MyJpa(key); + assertEquals(key, jpa.getKey()); + } + + @Test + public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() { + jpa.setDescription(SOME_DESCRIPTION); + assertEquals(jpa, new MyJpa(jpa)); + + jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + assertEquals(jpa, new MyJpa(jpa)); + } + + @Test + public void testJpaToscaWithStringPropertiesT() { + MyTosca tosca = new MyTosca(); + tosca.setName("world"); + tosca.setVersion("3.2.1"); + tosca.setDescription(SOME_DESCRIPTION); + tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2)); + + jpa = new MyJpa(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2)); + assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey()); + } + + @Test + public void testValidateWithKey() { + // null key - should fail + jpa.setText("some text"); + assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse(); + + // valid + jpa.setKey(new PfConceptKey("xyz", "2.3.4")); + assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue(); + + // null text - bean validator should fail + jpa.setText(null); + assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse(); + } + + private static class MyTosca extends ToscaWithObjectProperties { + + } + + @NoArgsConstructor + protected static class MyJpa extends JpaToscaWithStringProperties { + private static final long serialVersionUID = 1L; + + @NotNull + @Getter + @Setter + private String text; + + public MyJpa(MyJpa jpa) { + super(jpa); + } + + public MyJpa(PfConceptKey key) { + super(key); + } + + public MyJpa(MyTosca tosca) { + super(tosca); + } + + @Override + public MyTosca toAuthorative() { + this.setToscaEntity(new MyTosca()); + return super.toAuthorative(); + } + + @Override + protected Object deserializePropertyValue(String propValue) { + return Integer.parseInt(propValue); + } + + @Override + protected String serializePropertyValue(Object propValue) { + return propValue.toString(); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java index 9e8edc77c..250a819e6 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -53,6 +53,9 @@ public class TestPojos { .build(); // @formatter:on - validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + validator.validate(POJO_PACKAGE, + new FilterPackageInfo(), + pc -> !pc.getName().startsWith("Test"), + pc -> !pc.getName().endsWith("Test")); } } -- cgit 1.2.3-korg