2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.models.tosca.simple.concepts;
 
  24 import com.google.gson.annotations.SerializedName;
 
  25 import java.util.List;
 
  26 import javax.persistence.CascadeType;
 
  27 import javax.persistence.Column;
 
  28 import javax.persistence.Entity;
 
  29 import javax.persistence.FetchType;
 
  30 import javax.persistence.Inheritance;
 
  31 import javax.persistence.InheritanceType;
 
  32 import javax.persistence.OneToOne;
 
  33 import javax.persistence.Table;
 
  35 import lombok.EqualsAndHashCode;
 
  36 import lombok.NonNull;
 
  37 import org.apache.commons.lang3.ObjectUtils;
 
  38 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
 
  39 import org.onap.policy.models.base.PfAuthorative;
 
  40 import org.onap.policy.models.base.PfConcept;
 
  41 import org.onap.policy.models.base.PfConceptKey;
 
  42 import org.onap.policy.models.base.PfKey;
 
  43 import org.onap.policy.models.base.PfValidationMessage;
 
  44 import org.onap.policy.models.base.PfValidationResult;
 
  45 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
  46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  49  * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
 
  52  * @author Liam Fallon (liam.fallon@est.tech)
 
  55 @Table(name = "ToscaServiceTemplate")
 
  56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 
  58 @EqualsAndHashCode(callSuper = true)
 
  59 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
 
  60         implements PfAuthorative<ToscaServiceTemplate> {
 
  61     private static final long serialVersionUID = 8084846046148349401L;
 
  63     public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0";
 
  64     public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
 
  65     public static final String DEFAULT_VERSION = "1.0.0";
 
  68     @SerializedName("tosca_definitions_version")
 
  69     private String toscaDefinitionsVersion;
 
  71     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
 
  72     @SerializedName("data_types")
 
  73     private JpaToscaDataTypes dataTypes;
 
  75     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
 
  76     @SerializedName("policy_types")
 
  77     private JpaToscaPolicyTypes policyTypes;
 
  80     @SerializedName("topology_template")
 
  81     private JpaToscaTopologyTemplate topologyTemplate;
 
  85      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
 
  87     public JpaToscaServiceTemplate() {
 
  88         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
 
  92      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
 
  96     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
 
  97         this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
 
 101      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
 
 104      * @param toscaDefinitionsVersion the TOSCA version string
 
 106     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
 
 108         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
 
 114      * @param copyConcept the concept to copy from
 
 116     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
 
 118         this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
 
 119         this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
 
 120         this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
 
 121         this.topologyTemplate = (copyConcept.topologyTemplate != null
 
 122                         ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate) : null);
 
 126      * Authorative constructor.
 
 128      * @param authorativeConcept the authorative concept to copy from
 
 130     public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
 
 131         this.fromAuthorative(authorativeConcept);
 
 135     public ToscaServiceTemplate toAuthorative() {
 
 136         final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
 
 138         super.setToscaEntity(toscaServiceTemplate);
 
 139         super.toAuthorative();
 
 141         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
 
 143         if (dataTypes != null) {
 
 144             toscaServiceTemplate.setDataTypes(dataTypes.toAuthorative());
 
 147         if (policyTypes != null) {
 
 148             toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative());
 
 151         if (topologyTemplate != null) {
 
 152             toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
 
 155         return toscaServiceTemplate;
 
 159     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
 
 160         super.fromAuthorative(toscaServiceTemplate);
 
 162         if (getKey().getName() == PfKey.NULL_KEY_NAME) {
 
 163             getKey().setName(DEFAULT_NAME);
 
 166         if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
 
 167             getKey().setVersion(DEFAULT_VERSION);
 
 170         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
 
 172         if (toscaServiceTemplate.getDataTypes() != null) {
 
 173             dataTypes = new JpaToscaDataTypes();
 
 174             dataTypes.fromAuthorative(toscaServiceTemplate.getDataTypes());
 
 177         if (toscaServiceTemplate.getPolicyTypes() != null) {
 
 178             policyTypes = new JpaToscaPolicyTypes();
 
 179             policyTypes.fromAuthorative(toscaServiceTemplate.getPolicyTypes());
 
 183         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
 
 184             topologyTemplate = new JpaToscaTopologyTemplate();
 
 185             topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
 
 190     public List<PfKey> getKeys() {
 
 191         final List<PfKey> keyList = super.getKeys();
 
 193         if (dataTypes != null) {
 
 194             keyList.addAll(dataTypes.getKeys());
 
 197         if (policyTypes != null) {
 
 198             keyList.addAll(policyTypes.getKeys());
 
 201         if (topologyTemplate != null) {
 
 202             keyList.addAll(topologyTemplate.getKeys());
 
 209     public void clean() {
 
 210         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
 
 212         if (dataTypes != null) {
 
 216         if (policyTypes != null) {
 
 220         if (topologyTemplate != null) {
 
 221             topologyTemplate.clean();
 
 226     public PfValidationResult validate(final PfValidationResult resultIn) {
 
 227         PfValidationResult result = super.validate(resultIn);
 
 229         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
 
 230             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
 
 231                     "service template tosca definitions version may not be null"));
 
 234         if (dataTypes != null) {
 
 235             result = dataTypes.validate(result);
 
 238         if (policyTypes != null) {
 
 239             result = policyTypes.validate(result);
 
 242         return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
 
 246     public int compareTo(final PfConcept otherConcept) {
 
 247         if (otherConcept == null) {
 
 250         if (this == otherConcept) {
 
 253         if (getClass() != otherConcept.getClass()) {
 
 254             return getClass().getName().compareTo(otherConcept.getClass().getName());
 
 257         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
 
 258         if (!super.equals(other)) {
 
 259             return super.compareTo(other);
 
 262         int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
 
 267         result = ObjectUtils.compare(dataTypes, other.dataTypes);
 
 272         result = ObjectUtils.compare(policyTypes, other.policyTypes);
 
 277         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);