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.concepts;
23 import java.util.List;
25 import javax.persistence.CascadeType;
26 import javax.persistence.Entity;
27 import javax.persistence.Inheritance;
28 import javax.persistence.InheritanceType;
29 import javax.persistence.OneToOne;
30 import javax.persistence.Table;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
36 import org.onap.policy.common.utils.validation.Assertions;
37 import org.onap.policy.models.base.PfConcept;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfKey;
40 import org.onap.policy.models.base.PfModel;
41 import org.onap.policy.models.base.PfModelService;
42 import org.onap.policy.models.base.PfValidationResult;
45 * A container class for a TOSCA model with multiple service templates. This class is a container
46 * class that allows a model with many service templates to be constructed that contains a well
47 * formed overall TOSCA model.
49 * <p>Validation runs {@link ToscaModel} validation on the model and all its sub concepts.
53 @Table(name = "ToscaModel")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @EqualsAndHashCode(callSuper = true)
57 public class ToscaModel extends PfModel {
58 private static final long serialVersionUID = 8800599637708309945L;
60 @OneToOne(cascade = CascadeType.ALL)
61 private ToscaServiceTemplates serviceTemplates;
64 * The Default Constructor creates a {@link ToscaModel} object with a null concept key and
65 * creates an empty TOSCA model.
68 this(new PfConceptKey());
72 * The Key Constructor creates a {@link ToscaModel} object with the given concept key and
73 * creates an empty TOSCA model.
75 * @param key the TOSCA model key
77 public ToscaModel(final PfConceptKey key) {
78 this(key, new ToscaServiceTemplates(new PfConceptKey()));
82 * Constructor that initiates a {@link ToscaModel} with all its fields.
84 * @param key the TOSCA model key
85 * @param serviceTemplates the service templates in the event model
87 public ToscaModel(@NonNull final PfConceptKey key, @NonNull final ToscaServiceTemplates serviceTemplates) {
89 this.serviceTemplates = serviceTemplates;
95 * @param copyConcept the concept to copy from
97 public ToscaModel(@NonNull final ToscaModel copyConcept) {
102 public void register() {
103 PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
107 public List<PfKey> getKeys() {
108 final List<PfKey> keyList = super.getKeys();
110 keyList.addAll(serviceTemplates.getKeys());
116 public void clean() {
118 serviceTemplates.clean();
122 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
123 PfValidationResult result = super.validate(resultIn);
125 return serviceTemplates.validate(result);
129 public int compareTo(final PfConcept otherConcept) {
130 if (otherConcept == null) {
134 if (this == otherConcept) {
138 if (getClass() != otherConcept.getClass()) {
139 return this.hashCode() - otherConcept.hashCode();
142 final ToscaModel other = (ToscaModel) otherConcept;
143 if (!super.equals(other)) {
144 return super.compareTo(other);
147 return serviceTemplates.compareTo(other.serviceTemplates);
151 public PfConcept copyTo(@NonNull final PfConcept targetObject) {
152 Assertions.instanceOf(targetObject, ToscaModel.class);
154 final ToscaModel copy = ((ToscaModel) targetObject);
155 super.copyTo(targetObject);
156 copy.setServiceTemplates(new ToscaServiceTemplates(serviceTemplates));