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=109f1f0a873f26f9576e29a9b59ea2e896f9ce9c;hb=e53df8d3f8ab0464b0876bdb339fa91dc9085cd2;hp=904db849c62d84be7791f765430a3ab16bc8ba72;hpb=b7909b8e74c0bc5920f5d0397a4371988df9e8b2;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 904db849c..109f1f0a8 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 java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; - +import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import javax.persistence.OneToOne; import javax.persistence.Table; - 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,6 +57,7 @@ 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) @@ -64,26 +67,73 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType { private static final long serialVersionUID = 8084846046148349401L; - public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0"; + public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "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) + @JoinColumns({ + @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) + @JoinColumns({ + @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) + @JoinColumns({ + @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) + @JoinColumns({ + @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) + @JoinColumns({ + @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) + @JoinColumns({ + @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. @@ -119,6 +169,18 @@ 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); } @@ -170,15 +260,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); + } + } + } - final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject); - super.copyTo(target); - copy.setToscaDefinitionsVersion(toscaDefinitionsVersion); + /** + * 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; + } - copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null); - copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null); - copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null); + 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; + } - return copy; + for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) { + if (policyTypes.get(policy.getType()) == null) { + addResult(result, "policy type", policy.getType().getId(), NOT_FOUND); + } + } } }