a322c167fc821d9daee0bd4e0721938679d116ad
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.List;
24
25 import javax.persistence.CascadeType;
26 import javax.persistence.Entity;
27 import javax.persistence.Inheritance;
28 import javax.persistence.InheritanceType;
29 import javax.persistence.OneToOne;
30 import javax.persistence.Table;
31
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35
36 import org.onap.policy.common.utils.validation.Assertions;
37 import org.onap.policy.models.base.PfConcept;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfKey;
40 import org.onap.policy.models.base.PfModel;
41 import org.onap.policy.models.base.PfModelService;
42 import org.onap.policy.models.base.PfValidationResult;
43
44 /**
45  * A container class for a TOSCA model with multiple service templates. This class is a container
46  * class that allows a model with many service templates to be constructed that contains a well
47  * 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(cascade = CascadeType.ALL)
61     private JpaToscaServiceTemplates serviceTemplates;
62
63     /**
64      * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and
65      * creates an empty TOSCA model.
66      */
67     public JpaToscaModel() {
68         this(new PfConceptKey());
69     }
70
71     /**
72      * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and
73      * creates an empty TOSCA model.
74      *
75      * @param key the TOSCA model key
76      */
77     public JpaToscaModel(final PfConceptKey key) {
78         this(key, new JpaToscaServiceTemplates(new PfConceptKey()));
79     }
80
81     /**
82      * Constructor that initiates a {@link JpaToscaModel} with all its fields.
83      *
84      * @param key the TOSCA model key
85      * @param serviceTemplates the service templates in the event model
86      */
87     public JpaToscaModel(@NonNull final PfConceptKey key, @NonNull final JpaToscaServiceTemplates serviceTemplates) {
88         super(key);
89         this.serviceTemplates = serviceTemplates;
90     }
91
92     /**
93      * Copy constructor.
94      *
95      * @param copyConcept the concept to copy from
96      */
97     public JpaToscaModel(@NonNull final JpaToscaModel copyConcept) {
98         super(copyConcept);
99     }
100
101     @Override
102     public void register() {
103         PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates());
104     }
105
106     @Override
107     public List<PfKey> getKeys() {
108         final List<PfKey> keyList = super.getKeys();
109
110         keyList.addAll(serviceTemplates.getKeys());
111
112         return keyList;
113     }
114
115     @Override
116     public void clean() {
117         super.clean();
118         serviceTemplates.clean();
119     }
120
121     @Override
122     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
123         PfValidationResult result = super.validate(resultIn);
124
125         return serviceTemplates.validate(result);
126     }
127
128     @Override
129     public int compareTo(final PfConcept otherConcept) {
130         if (otherConcept == null) {
131             return -1;
132         }
133
134         if (this == otherConcept) {
135             return 0;
136         }
137
138         if (getClass() != otherConcept.getClass()) {
139             return this.hashCode() - otherConcept.hashCode();
140         }
141
142         final JpaToscaModel other = (JpaToscaModel) otherConcept;
143         if (!super.equals(other)) {
144             return super.compareTo(other);
145         }
146
147         return serviceTemplates.compareTo(other.serviceTemplates);
148     }
149
150     @Override
151     public PfConcept copyTo(@NonNull final PfConcept targetObject) {
152         Assertions.instanceOf(targetObject, JpaToscaModel.class);
153
154         final JpaToscaModel copy = ((JpaToscaModel) targetObject);
155         super.copyTo(targetObject);
156         copy.setServiceTemplates(new JpaToscaServiceTemplates(serviceTemplates));
157
158         return copy;
159     }
160 }