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