cc9b1bc960bf4fa126b7abafca41e1b1f6bfb702
[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 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
44  * class that allows a model with many service templates to be constructed that contains a well
45  * formed overall TOSCA model.
46  *
47  * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts.
48  */
49
50 @Entity
51 @Table(name = "ToscaModel")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @Data
54 @EqualsAndHashCode(callSuper = true)
55 public class JpaToscaModel extends PfModel {
56     private static final long serialVersionUID = 8800599637708309945L;
57
58     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
59     private JpaToscaServiceTemplates serviceTemplates;
60
61     /**
62      * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and
63      * creates an empty TOSCA model.
64      */
65     public JpaToscaModel() {
66         this(new PfConceptKey());
67     }
68
69     /**
70      * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and
71      * creates an empty TOSCA model.
72      *
73      * @param key the TOSCA model key
74      */
75     public JpaToscaModel(final PfConceptKey key) {
76         this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
77     }
78
79     /**
80      * Constructor that initiates a {@link JpaToscaModel} with all its fields.
81      *
82      * @param key the TOSCA model key
83      * @param serviceTemplates the service templates in the event model
84      */
85     public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
86         super(key);
87         this.serviceTemplates = serviceTemplates;
88     }
89
90     /**
91      * Copy constructor.
92      *
93      * @param copyConcept the concept to copy from
94      */
95     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
96         super(copyConcept);
97         this.serviceTemplates = new JpaToscaServiceTemplates(copyConcept.serviceTemplates);
98     }
99
100     @Override
101     public void register() {
102         PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
103     }
104
105     @Override
106     public List<PfKey> getKeys() {
107         final List<PfKey> keyList = super.getKeys();
108
109         keyList.addAll(serviceTemplates.getKeys());
110
111         return keyList;
112     }
113
114     @Override
115     public void clean() {
116         super.clean();
117         serviceTemplates.clean();
118     }
119
120     @Override
121     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
122         PfValidationResult result = super.validate(resultIn);
123
124         return serviceTemplates.validate(result);
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         if (!super.equals(other)) {
143             return super.compareTo(other);
144         }
145
146         return serviceTemplates.compareTo(other.serviceTemplates);
147     }
148 }