5f72e2ba85fea94d0de65b4e35261e02806c6cf3
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020,2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.tosca.simple.concepts;
24
25 import java.util.List;
26 import javax.persistence.CascadeType;
27 import javax.persistence.Entity;
28 import javax.persistence.FetchType;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.JoinColumns;
33 import javax.persistence.OneToOne;
34 import javax.persistence.Table;
35 import lombok.Data;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.onap.policy.common.parameters.annotations.Valid;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfModel;
43 import org.onap.policy.models.base.PfModelService;
44
45 /**
46  * A container class for a TOSCA model with multiple service templates. This class is a container class that allows a
47  * model with many service templates to be constructed that contains a well formed overall TOSCA model.
48  *
49  * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts.
50  */
51
52 @Entity
53 @Table(name = "ToscaModel")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @Data
56 @EqualsAndHashCode(callSuper = true)
57 public class JpaToscaModel extends PfModel {
58     private static final long serialVersionUID = 8800599637708309945L;
59
60     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
61     @JoinColumns({
62         @JoinColumn(name = "serviceTemplatesName", referencedColumnName = "name"),
63         @JoinColumn(name = "serviceTemplatesVersion", referencedColumnName = "version")
64     })
65     @Valid
66     private JpaToscaServiceTemplates serviceTemplates;
67
68     /**
69      * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and creates an empty TOSCA
70      * model.
71      */
72     public JpaToscaModel() {
73         this(new PfConceptKey());
74     }
75
76     /**
77      * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and creates an empty TOSCA
78      * model.
79      *
80      * @param key the TOSCA model key
81      */
82     public JpaToscaModel(final PfConceptKey key) {
83         this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
84     }
85
86     /**
87      * Constructor that initiates a {@link JpaToscaModel} with all its fields.
88      *
89      * @param key the TOSCA model key
90      * @param serviceTemplates the service templates in the event model
91      */
92     public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
93         super(key);
94         this.serviceTemplates = serviceTemplates;
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
103         super(copyConcept);
104         this.serviceTemplates = new JpaToscaServiceTemplates(copyConcept.serviceTemplates);
105     }
106
107     @Override
108     public void register() {
109         PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
110     }
111
112     @Override
113     public List<PfKey> getKeys() {
114         final List<PfKey> keyList = super.getKeys();
115
116         keyList.addAll(serviceTemplates.getKeys());
117
118         return keyList;
119     }
120
121     @Override
122     public void clean() {
123         super.clean();
124         serviceTemplates.clean();
125     }
126
127     @Override
128     public int compareTo(final PfConcept otherConcept) {
129         if (otherConcept == null) {
130             return -1;
131         }
132
133         if (this == otherConcept) {
134             return 0;
135         }
136
137         if (getClass() != otherConcept.getClass()) {
138             return getClass().getName().compareTo(otherConcept.getClass().getName());
139         }
140
141         final JpaToscaModel other = (JpaToscaModel) otherConcept;
142         int result = super.compareTo(other);
143         if (result != 0) {
144             return result;
145         }
146
147         return serviceTemplates.compareTo(other.serviceTemplates);
148     }
149 }