512abb75b38eb0944693ce400f6a50b3ea1c94af
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaNodeType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2021 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.JoinColumn;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NoArgsConstructor;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.common.parameters.BeanValidationResult;
39 import org.onap.policy.common.parameters.annotations.Valid;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
44
45 /**
46  * Class to represent the node type in TOSCA definition.
47  */
48
49 @Entity
50 @Table(name = "ToscaNodeType")
51 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
52 @Data
53 @EqualsAndHashCode(callSuper = true)
54 @NoArgsConstructor
55 public class JpaToscaNodeType extends JpaToscaWithToscaProperties<ToscaNodeType> {
56     private static final long serialVersionUID = -563659852901842616L;
57
58
59     // formatter:off
60     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
61     @JoinColumn(name = "requirementsName", referencedColumnName = "name")
62     @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
63     // @formatter:on
64     @Valid
65     private JpaToscaRequirements requirements;
66
67     /**
68      * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
69      *
70      * @param key the key
71      */
72     public JpaToscaNodeType(@NonNull final PfConceptKey key) {
73         super(key);
74     }
75
76     /**
77      * Copy constructor.
78      *
79      * @param copyConcept the concept to copy from
80      */
81     public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
82         super(copyConcept);
83         this.requirements =
84                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
85     }
86
87     /**
88      * Authorative constructor.
89      *
90      * @param authorativeConcept the authorative concept to copy from
91      */
92     public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
93         super(authorativeConcept);
94     }
95
96     @Override
97     public ToscaNodeType toAuthorative() {
98         var toscaNodeType = new ToscaNodeType();
99         super.setToscaEntity(toscaNodeType);
100         super.toAuthorative();
101
102         if (requirements != null) {
103             toscaNodeType.setRequirements(requirements.toAuthorative());
104         }
105
106         return toscaNodeType;
107     }
108
109     @Override
110     public void fromAuthorative(final ToscaNodeType toscaNodeType) {
111         super.fromAuthorative(toscaNodeType);
112
113         if (toscaNodeType.getRequirements() != null) {
114             requirements = new JpaToscaRequirements();
115             requirements.fromAuthorative(toscaNodeType.getRequirements());
116         }
117     }
118
119     @Override
120     public List<PfKey> getKeys() {
121         final List<PfKey> keyList = super.getKeys();
122
123         if (requirements != null) {
124             keyList.addAll(requirements.getKeys());
125         }
126
127         return keyList;
128     }
129
130     @Override
131     public void clean() {
132         super.clean();
133
134         if (requirements != null) {
135             requirements.clean();
136         }
137     }
138
139     @Override
140     public BeanValidationResult validate(String fieldName) {
141         return validateWithKey(fieldName);
142     }
143
144     @Override
145     public int compareTo(final PfConcept otherConcept) {
146         if (this == otherConcept) {
147             return 0;
148         }
149
150         int result = super.compareTo(otherConcept);
151         if (result != 0) {
152             return result;
153         }
154
155         final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
156
157         return ObjectUtils.compare(requirements, other.requirements);
158     }
159 }