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