Java 17 Upgrade
[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 jakarta.persistence.CascadeType;
26 import jakarta.persistence.Entity;
27 import jakarta.persistence.FetchType;
28 import jakarta.persistence.Inheritance;
29 import jakarta.persistence.InheritanceType;
30 import jakarta.persistence.JoinColumn;
31 import jakarta.persistence.JoinColumns;
32 import jakarta.persistence.OneToOne;
33 import jakarta.persistence.Table;
34 import java.util.List;
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     @JoinColumn(name = "serviceTemplatesName", referencedColumnName = "name")
62     @JoinColumn(name = "serviceTemplatesVersion", referencedColumnName = "version")
63     @Valid
64     private JpaToscaServiceTemplates serviceTemplates;
65
66     /**
67      * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and creates an empty TOSCA
68      * model.
69      */
70     public JpaToscaModel() {
71         this(new PfConceptKey());
72     }
73
74     /**
75      * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and creates an empty TOSCA
76      * model.
77      *
78      * @param key the TOSCA model key
79      */
80     public JpaToscaModel(final PfConceptKey key) {
81         this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
82     }
83
84     /**
85      * Constructor that initiates a {@link JpaToscaModel} with all its fields.
86      *
87      * @param key the TOSCA model key
88      * @param serviceTemplates the service templates in the event model
89      */
90     public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
91         super(key);
92         this.serviceTemplates = serviceTemplates;
93     }
94
95     /**
96      * Copy constructor.
97      *
98      * @param copyConcept the concept to copy from
99      */
100     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
101         super(copyConcept);
102         this.serviceTemplates = new JpaToscaServiceTemplates(copyConcept.serviceTemplates);
103     }
104
105     @Override
106     public void register() {
107         PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
108     }
109
110     @Override
111     public List<PfKey> getKeys() {
112         final List<PfKey> keyList = super.getKeys();
113
114         keyList.addAll(serviceTemplates.getKeys());
115
116         return keyList;
117     }
118
119     @Override
120     public void clean() {
121         super.clean();
122         serviceTemplates.clean();
123     }
124
125     @Override
126     public int compareTo(final PfConcept otherConcept) {
127         if (otherConcept == null) {
128             return -1;
129         }
130
131         if (this == otherConcept) {
132             return 0;
133         }
134
135         if (getClass() != otherConcept.getClass()) {
136             return getClass().getName().compareTo(otherConcept.getClass().getName());
137         }
138
139         final JpaToscaModel other = (JpaToscaModel) otherConcept;
140         int result = super.compareTo(other);
141         if (result != 0) {
142             return result;
143         }
144
145         return serviceTemplates.compareTo(other.serviceTemplates);
146     }
147 }