2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.models.tosca.simple.concepts;
 
  23 import com.google.gson.annotations.SerializedName;
 
  25 import java.util.List;
 
  27 import javax.persistence.CascadeType;
 
  28 import javax.persistence.Column;
 
  29 import javax.persistence.Entity;
 
  30 import javax.persistence.FetchType;
 
  31 import javax.persistence.Inheritance;
 
  32 import javax.persistence.InheritanceType;
 
  33 import javax.persistence.OneToOne;
 
  34 import javax.persistence.Table;
 
  37 import lombok.EqualsAndHashCode;
 
  38 import lombok.NonNull;
 
  40 import org.apache.commons.lang3.ObjectUtils;
 
  41 import org.onap.policy.common.utils.validation.Assertions;
 
  42 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
 
  43 import org.onap.policy.models.base.PfAuthorative;
 
  44 import org.onap.policy.models.base.PfConcept;
 
  45 import org.onap.policy.models.base.PfConceptKey;
 
  46 import org.onap.policy.models.base.PfKey;
 
  47 import org.onap.policy.models.base.PfValidationMessage;
 
  48 import org.onap.policy.models.base.PfValidationResult;
 
  49 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
  50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  53  * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
 
  56  * @author Liam Fallon (liam.fallon@est.tech)
 
  59 @Table(name = "ToscaServiceTemplate")
 
  60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 
  62 @EqualsAndHashCode(callSuper = true)
 
  63 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
 
  64         implements PfAuthorative<ToscaServiceTemplate> {
 
  65     private static final long serialVersionUID = 8084846046148349401L;
 
  67     public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0";
 
  68     public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
 
  69     public static final String DEFAULT_VERSION = "1.0.0";
 
  72     @SerializedName("tosca_definitions_version")
 
  73     private String toscaDefinitionsVersion;
 
  75     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
 
  76     @SerializedName("data_types")
 
  77     private JpaToscaDataTypes dataTypes;
 
  79     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
 
  80     @SerializedName("policy_types")
 
  81     private JpaToscaPolicyTypes policyTypes;
 
  84     @SerializedName("topology_template")
 
  85     private JpaToscaTopologyTemplate topologyTemplate;
 
  89      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
 
  91     public JpaToscaServiceTemplate() {
 
  92         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
 
  96      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
 
 100     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
 
 101         this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
 
 105      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
 
 108      * @param toscaDefinitionsVersion the TOSCA version string
 
 110     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
 
 112         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
 
 118      * @param copyConcept the concept to copy from
 
 120     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
 
 125      * Authorative constructor.
 
 127      * @param authorativeConcept the authorative concept to copy from
 
 129     public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
 
 130         this.fromAuthorative(authorativeConcept);
 
 134     public ToscaServiceTemplate toAuthorative() {
 
 135         final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
 
 137         super.setToscaEntity(toscaServiceTemplate);
 
 138         super.toAuthorative();
 
 140         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
 
 142         if (dataTypes != null) {
 
 143             toscaServiceTemplate.setDataTypes(dataTypes.toAuthorative());
 
 146         if (policyTypes != null) {
 
 147             toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative());
 
 150         if (topologyTemplate != null) {
 
 151             toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
 
 154         return toscaServiceTemplate;
 
 158     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
 
 159         super.fromAuthorative(toscaServiceTemplate);
 
 161         if (getKey().getName() == PfKey.NULL_KEY_NAME) {
 
 162             getKey().setName(DEFAULT_NAME);
 
 165         if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
 
 166             getKey().setVersion(DEFAULT_VERSION);
 
 169         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
 
 171         if (toscaServiceTemplate.getDataTypes() != null) {
 
 172             dataTypes = new JpaToscaDataTypes();
 
 173             dataTypes.fromAuthorative(toscaServiceTemplate.getDataTypes());
 
 176         if (toscaServiceTemplate.getPolicyTypes() != null) {
 
 177             policyTypes = new JpaToscaPolicyTypes();
 
 178             policyTypes.fromAuthorative(toscaServiceTemplate.getPolicyTypes());
 
 182         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
 
 183             topologyTemplate = new JpaToscaTopologyTemplate();
 
 184             topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
 
 189     public List<PfKey> getKeys() {
 
 190         final List<PfKey> keyList = super.getKeys();
 
 192         if (dataTypes != null) {
 
 193             keyList.addAll(dataTypes.getKeys());
 
 196         if (policyTypes != null) {
 
 197             keyList.addAll(policyTypes.getKeys());
 
 200         if (topologyTemplate != null) {
 
 201             keyList.addAll(topologyTemplate.getKeys());
 
 208     public void clean() {
 
 209         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
 
 211         if (dataTypes != null) {
 
 215         if (policyTypes != null) {
 
 219         if (topologyTemplate != null) {
 
 220             topologyTemplate.clean();
 
 225     public PfValidationResult validate(final PfValidationResult resultIn) {
 
 226         PfValidationResult result = super.validate(resultIn);
 
 228         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
 
 229             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
 
 230                     "service template tosca definitions version may not be null"));
 
 233         if (dataTypes != null) {
 
 234             result = dataTypes.validate(result);
 
 237         if (policyTypes != null) {
 
 238             result = policyTypes.validate(result);
 
 241         return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
 
 245     public int compareTo(final PfConcept otherConcept) {
 
 246         if (otherConcept == null) {
 
 249         if (this == otherConcept) {
 
 252         if (getClass() != otherConcept.getClass()) {
 
 253             return this.hashCode() - otherConcept.hashCode();
 
 256         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
 
 257         if (!super.equals(other)) {
 
 258             return super.compareTo(other);
 
 261         int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
 
 266         result = ObjectUtils.compare(dataTypes, other.dataTypes);
 
 271         result = ObjectUtils.compare(policyTypes, other.policyTypes);
 
 276         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
 
 280     public PfConcept copyTo(@NonNull PfConcept target) {
 
 281         final Object copyObject = target;
 
 282         Assertions.instanceOf(copyObject, PfConcept.class);
 
 284         final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject);
 
 285         super.copyTo(target);
 
 286         copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
 
 288         copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null);
 
 289         copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null);
 
 290         copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null);