09c406ea6fd21b6232408a0b28939003211f2e34
[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 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.OneToOne;
33 import javax.persistence.Table;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.annotations.Valid;
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
44 /**
45  * A container class for a TOSCA model with multiple service templates. This class is a container class that allows a
46  * model with many service templates to be constructed that contains a well formed overall TOSCA model.
47  *
48  * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts.
49  */
50
51 @Entity
52 @Table(name = "ToscaModel")
53 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
54 @Data
55 @EqualsAndHashCode(callSuper = true)
56 public class JpaToscaModel extends PfModel {
57     private static final long serialVersionUID = 8800599637708309945L;
58
59     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
60     @JoinColumn(name = "serviceTemplatesName", referencedColumnName = "name")
61     @JoinColumn(name = "serviceTemplatesVersion", referencedColumnName = "version")
62     @Valid
63     private JpaToscaServiceTemplates serviceTemplates;
64
65     /**
66      * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and creates an empty TOSCA
67      * model.
68      */
69     public JpaToscaModel() {
70         this(new PfConceptKey());
71     }
72
73     /**
74      * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and creates an empty TOSCA
75      * model.
76      *
77      * @param key the TOSCA model key
78      */
79     public JpaToscaModel(final PfConceptKey key) {
80         this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
81     }
82
83     /**
84      * Constructor that initiates a {@link JpaToscaModel} with all its fields.
85      *
86      * @param key the TOSCA model key
87      * @param serviceTemplates the service templates in the event model
88      */
89     public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
90         super(key);
91         this.serviceTemplates = serviceTemplates;
92     }
93
94     /**
95      * Copy constructor.
96      *
97      * @param copyConcept the concept to copy from
98      */
99     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
100         super(copyConcept);
101         this.serviceTemplates = new JpaToscaServiceTemplates(copyConcept.serviceTemplates);
102     }
103
104     @Override
105     public void register() {
106         PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
107     }
108
109     @Override
110     public List<PfKey> getKeys() {
111         final List<PfKey> keyList = super.getKeys();
112
113         keyList.addAll(serviceTemplates.getKeys());
114
115         return keyList;
116     }
117
118     @Override
119     public void clean() {
120         super.clean();
121         serviceTemplates.clean();
122     }
123
124     @Override
125     public int compareTo(final PfConcept otherConcept) {
126         if (otherConcept == null) {
127             return -1;
128         }
129
130         if (this == otherConcept) {
131             return 0;
132         }
133
134         if (getClass() != otherConcept.getClass()) {
135             return getClass().getName().compareTo(otherConcept.getClass().getName());
136         }
137
138         final JpaToscaModel other = (JpaToscaModel) otherConcept;
139         int result = super.compareTo(other);
140         if (result != 0) {
141             return result;
142         }
143
144         return serviceTemplates.compareTo(other.serviceTemplates);
145     }
146 }