X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Fsimple%2Fconcepts%2FJpaToscaServiceTemplate.java;h=8f878c86ef351ced5872f3d934706d01ecb1603e;hb=938005505883cf7a636a8840e20e3dc8a0ad9176;hp=5f50e4a4393b62ff30000d80d9dc914d720a2024;hpb=891bffd9f13177d96ad26acdfa7148d09e1d682a;p=policy%2Fmodels.git diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index 5f50e4a43..8f878c86e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-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,32 +22,33 @@ package org.onap.policy.models.tosca.simple.concepts; import com.google.gson.annotations.SerializedName; - +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; +import java.io.Serial; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.OneToOne; -import javax.persistence.Table; - +import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - import org.apache.commons.lang3.ObjectUtils; -import org.onap.policy.common.utils.validation.Assertions; -import org.onap.policy.common.utils.validation.ParameterValidationUtils; +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.PfValidationMessage; -import org.onap.policy.models.base.PfValidationResult; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -55,34 +57,72 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; * * @author Liam Fallon (liam.fallon@est.tech) */ + @Entity @Table(name = "ToscaServiceTemplate") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) public class JpaToscaServiceTemplate extends JpaToscaEntityType - implements PfAuthorative { + implements PfAuthorative { + @Serial private static final long serialVersionUID = 8084846046148349401L; + public static final String DEFAULT_TOSCA_DEFINITIONS_VERSION = "tosca_simple_yaml_1_1_0"; public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; public static final String DEFAULT_VERSION = "1.0.0"; + // @formatter:off @Column @SerializedName("tosca_definitions_version") + @NotNull + @NotBlank private String toscaDefinitionsVersion; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "dataTypesName", referencedColumnName = "name") + @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version") @SerializedName("data_types") + @Valid private JpaToscaDataTypes dataTypes; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "capabilityTypesName", referencedColumnName = "name") + @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version") + @SerializedName("capability_types") + @Valid + private JpaToscaCapabilityTypes capabilityTypes; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "relationshipTypesName", referencedColumnName = "name") + @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version") + @SerializedName("relationship_types") + @Valid + private JpaToscaRelationshipTypes relationshipTypes; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "nodeTypesName", referencedColumnName = "name") + @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version") + @SerializedName("node_types") + @Valid + private JpaToscaNodeTypes nodeTypes; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "policyTypesName", referencedColumnName = "name") + @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version") @SerializedName("policy_types") + @Valid private JpaToscaPolicyTypes policyTypes; - @Column + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName") + @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion") + @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName") + @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName") @SerializedName("topology_template") + @Valid private JpaToscaTopologyTemplate topologyTemplate; - + // @formatter:on /** * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key. @@ -97,13 +137,13 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType Map flattenMap(List> list) { + Map result = new LinkedHashMap<>(); + + for (Map map : list) { + result.putAll(map); + } + + return result; + } + @Override public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) { super.fromAuthorative(toscaServiceTemplate); - if (getKey().getName() == PfKey.NULL_KEY_NAME) { + if (toscaServiceTemplate.getDefinedName() == null) { getKey().setName(DEFAULT_NAME); } - if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) { + if (toscaServiceTemplate.getDefinedVersion() == null) { getKey().setVersion(DEFAULT_VERSION); } @@ -169,15 +249,29 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType dataTypeKeyCollection, + final BeanValidationResult result) { + for (PfConceptKey dataTypeKey : dataTypeKeyCollection) { + if (dataTypes == null || dataTypes.get(dataTypeKey) == null) { + addResult(result, "data type", dataTypeKey.getId(), NOT_FOUND); + } + } + } - copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null); - copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null); - copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null); + /** + * Validate that all policy types referenced in policies exist. + * + * @param result where the results are added + */ + private void validatePolicyTypesInPolicies(BeanValidationResult result) { + if (topologyTemplate == null || topologyTemplate.getPolicies() == null + || topologyTemplate.getPolicies().getConceptMap().isEmpty()) { + return; + } - return copy; + if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) { + addResult(result, "policyTypes", policyTypes, + "no policy types are defined on the service template for the policies in the topology template"); + return; + } + + for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) { + if (policyTypes.get(policy.getType()) == null) { + addResult(result, "policy type", policy.getType().getId(), NOT_FOUND); + } + } } }