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=e07894cd8809236f69895693b0ffbcbdf2b71476;hpb=c8857aca1f6dee63b634559c0a06599fb53ecc99;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 e07894cd8..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,7 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * 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. @@ -22,34 +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 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.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.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.ToscaDataType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -58,35 +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_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0"; + 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. @@ -101,13 +137,13 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType()); - List> dataTypeMapList = dataTypes.toAuthorative(); - for (Map dataTypeMap : dataTypeMapList) { - toscaServiceTemplate.getDataTypes().putAll(dataTypeMap); - } + toscaServiceTemplate.setDataTypes(flattenMap(dataTypes.toAuthorative())); + } + + if (capabilityTypes != null) { + toscaServiceTemplate.setCapabilityTypes(flattenMap(capabilityTypes.toAuthorative())); + } + + if (relationshipTypes != null) { + toscaServiceTemplate.setRelationshipTypes(flattenMap(relationshipTypes.toAuthorative())); + } + + if (nodeTypes != null) { + toscaServiceTemplate.setNodeTypes(flattenMap(nodeTypes.toAuthorative())); } if (policyTypes != null) { - toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); - List> policyTypeMapList = policyTypes.toAuthorative(); - for (Map policyTypeMap : policyTypeMapList) { - toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap); - } + toscaServiceTemplate.setPolicyTypes(flattenMap(policyTypes.toAuthorative())); } if (topologyTemplate != null) { @@ -170,15 +217,31 @@ 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); } @@ -189,12 +252,26 @@ 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); + } + } + } + + /** + * 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; + } + + 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); + } + } } }