Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaNodeTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2021, 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 jakarta.persistence.CascadeType;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.FetchType;
27 import jakarta.persistence.Inheritance;
28 import jakarta.persistence.InheritanceType;
29 import jakarta.persistence.JoinColumn;
30 import jakarta.persistence.OneToOne;
31 import jakarta.persistence.Table;
32 import jakarta.ws.rs.core.Response;
33 import java.io.Serial;
34 import java.util.Collections;
35 import java.util.LinkedHashMap;
36 import java.util.List;
37 import java.util.Map;
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.common.parameters.annotations.Valid;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfModelRuntimeException;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
51
52 /**
53  * Class to represent the node template in TOSCA definition.
54  */
55 @Entity
56 @Table(name = "ToscaNodeTemplate")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaToscaNodeTemplate extends JpaToscaWithTypeAndStringProperties<ToscaNodeTemplate> {
61     @Serial
62     private static final long serialVersionUID = 1675770231921107988L;
63
64     private static final StandardCoder STANDARD_CODER = new StandardCoder();
65
66     // formatter:off
67     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
68     @JoinColumn(name = "requirementsName", referencedColumnName = "name")
69     @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
70     @Valid
71     private JpaToscaRequirements requirements;
72
73     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
74     @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
75     @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
76     @Valid
77     private JpaToscaCapabilityAssignments capabilities;
78     // @formatter:on
79
80     /**
81      * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
82      */
83     public JpaToscaNodeTemplate() {
84         this(new PfConceptKey());
85     }
86
87     /**
88      * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
89      *
90      * @param key the key
91      */
92     public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
93         this(key, null);
94     }
95
96     /**
97      * Copy constructor.
98      *
99      * @param copyConcept the concept to copy from
100      */
101     public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
102         super(copyConcept);
103
104         this.requirements =
105                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
106         this.capabilities =
107                 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
108     }
109
110     /**
111      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
112      *
113      * @param key the key
114      * @param type the node template type
115      */
116     public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
117         super(key);
118     }
119
120     /**
121      * Authorative constructor.
122      *
123      * @param authorativeConcept the authorative concept to copy from
124      */
125     public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
126         this.fromAuthorative(authorativeConcept);
127     }
128
129     @Override
130     public ToscaNodeTemplate toAuthorative() {
131         var toscaNodeTemplate = new ToscaNodeTemplate();
132         super.setToscaEntity(toscaNodeTemplate);
133         super.toAuthorative();
134
135         if (requirements != null) {
136             toscaNodeTemplate.setRequirements(requirements.toAuthorative());
137         }
138
139         if (capabilities != null) {
140             toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
141             List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
142             for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
143                 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
144             }
145         }
146
147         return toscaNodeTemplate;
148     }
149
150     @Override
151     public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
152         super.fromAuthorative(toscaNodeTemplate);
153
154         if (toscaNodeTemplate.getRequirements() != null) {
155             requirements = new JpaToscaRequirements();
156             requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
157         }
158
159         if (toscaNodeTemplate.getCapabilities() != null) {
160             capabilities = new JpaToscaCapabilityAssignments();
161             capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
162         }
163     }
164
165     @Override
166     protected Object deserializePropertyValue(String propValue) {
167         try {
168             return STANDARD_CODER.decode(propValue, Object.class);
169         } catch (CoderException ce) {
170             String errorMessage = "error decoding property JSON value read from database: " + propValue;
171             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
172         }
173     }
174
175     @Override
176     protected String serializePropertyValue(Object propValue) {
177         try {
178             return STANDARD_CODER.encode(propValue);
179         } catch (CoderException ce) {
180             String errorMessage = "error encoding property JSON value for database: " + propValue;
181             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
182         }
183     }
184
185     @Override
186     public List<PfKey> getKeys() {
187         final List<PfKey> keyList = super.getKeys();
188
189         if (requirements != null) {
190             keyList.addAll(requirements.getKeys());
191         }
192
193         if (capabilities != null) {
194             keyList.addAll(capabilities.getKeys());
195         }
196
197         return keyList;
198     }
199
200     @Override
201     public void clean() {
202         super.clean();
203
204         if (requirements != null) {
205             requirements.clean();
206         }
207
208         if (capabilities != null) {
209             capabilities.clean();
210         }
211     }
212
213     @Override
214     public int compareTo(final PfConcept otherConcept) {
215         if (this == otherConcept) {
216             return 0;
217         }
218
219         int result = super.compareTo(otherConcept);
220         if (result != 0) {
221             return result;
222         }
223
224         final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
225
226         result = ObjectUtils.compare(requirements, other.requirements);
227         if (result != 0) {
228             return result;
229         }
230
231         return ObjectUtils.compare(capabilities, other.capabilities);
232     }
233 }