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