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 java.util.List;
25 import javax.persistence.CascadeType;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.Inheritance;
29 import javax.persistence.InheritanceType;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
37 import org.onap.policy.common.utils.validation.Assertions;
38 import org.onap.policy.models.base.PfConcept;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfModel;
42 import org.onap.policy.models.base.PfModelService;
43 import org.onap.policy.models.base.PfValidationResult;
46 * A container class for a TOSCA model with multiple service templates. This class is a container
47 * class that allows a model with many service templates to be constructed that contains a well
48 * formed overall TOSCA model.
50 * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts.
54 @Table(name = "ToscaModel")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @EqualsAndHashCode(callSuper = true)
58 public class JpaToscaModel extends PfModel {
59 private static final long serialVersionUID = 8800599637708309945L;
61 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
62 private JpaToscaServiceTemplates serviceTemplates;
65 * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and
66 * creates an empty TOSCA model.
68 public JpaToscaModel() {
69 this(new PfConceptKey());
73 * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and
74 * creates an empty TOSCA model.
76 * @param key the TOSCA model key
78 public JpaToscaModel(final PfConceptKey key) {
79 this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
83 * Constructor that initiates a {@link JpaToscaModel} with all its fields.
85 * @param key the TOSCA model key
86 * @param serviceTemplates the service templates in the event model
88 public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
90 this.serviceTemplates = serviceTemplates;
96 * @param copyConcept the concept to copy from
98 public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
103 public void register() {
104 PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
108 public List<PfKey> getKeys() {
109 final List<PfKey> keyList = super.getKeys();
111 keyList.addAll(serviceTemplates.getKeys());
117 public void clean() {
119 serviceTemplates.clean();
123 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
124 PfValidationResult result = super.validate(resultIn);
126 return serviceTemplates.validate(result);
130 public int compareTo(final PfConcept otherConcept) {
131 if (otherConcept == null) {
135 if (this == otherConcept) {
139 if (getClass() != otherConcept.getClass()) {
140 return this.hashCode() - otherConcept.hashCode();
143 final JpaToscaModel other = (JpaToscaModel) otherConcept;
144 if (!super.equals(other)) {
145 return super.compareTo(other);
148 return serviceTemplates.compareTo(other.serviceTemplates);
152 public PfConcept copyTo(@NonNull final PfConcept targetObject) {
153 Assertions.instanceOf(targetObject, JpaToscaModel.class);
155 final JpaToscaModel copy = ((JpaToscaModel) targetObject);
156 super.copyTo(targetObject);
157 copy.setServiceTemplates(new JpaToscaServiceTemplates(serviceTemplates));