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