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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import java.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
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;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
44 import org.apache.commons.lang3.ObjectUtils;
45 import org.onap.policy.common.parameters.BeanValidationResult;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfModelRuntimeException;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.Validated;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
59 * Class to represent the node template in TOSCA definition.
62 @Table(name = "ToscaNodeTemplate")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
67 implements PfAuthorative<ToscaNodeTemplate> {
68 private static final long serialVersionUID = 1675770231921107988L;
70 private static final StandardCoder STANDARD_CODER = new StandardCoder();
77 private Map<String, String> properties;
80 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
81 @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
82 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
83 private JpaToscaRequirements requirements;
85 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
86 @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
87 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")})
88 private JpaToscaCapabilityAssignments capabilities;
92 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
94 public JpaToscaNodeTemplate() {
95 this(new PfConceptKey());
99 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
103 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
110 * @param copyConcept the concept to copy from
112 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
114 this.type = copyConcept.type;
115 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
117 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
119 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
123 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
126 * @param type the node template type
128 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
134 * Authorative constructor.
136 * @param authorativeConcept the authorative concept to copy from
138 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
139 this.fromAuthorative(authorativeConcept);
143 public ToscaNodeTemplate toAuthorative() {
144 ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
145 super.setToscaEntity(toscaNodeTemplate);
146 super.toAuthorative();
148 toscaNodeTemplate.setType(type);
150 toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> {
152 return STANDARD_CODER.decode(property, Object.class);
153 } catch (CoderException ce) {
154 String errorMessage = "error decoding property JSON value read from database: " + property;
155 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
159 if (requirements != null) {
160 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
163 if (capabilities != null) {
164 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
165 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
166 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
167 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
171 return toscaNodeTemplate;
175 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
176 super.fromAuthorative(toscaNodeTemplate);
178 type = toscaNodeTemplate.getType();
180 properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> {
182 return STANDARD_CODER.encode(property);
183 } catch (CoderException ce) {
184 String errorMessage = "error encoding property JSON value for database: " + property;
185 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
189 if (toscaNodeTemplate.getRequirements() != null) {
190 requirements = new JpaToscaRequirements();
191 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
194 if (toscaNodeTemplate.getCapabilities() != null) {
195 capabilities = new JpaToscaCapabilityAssignments();
196 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
201 public List<PfKey> getKeys() {
202 final List<PfKey> keyList = super.getKeys();
204 if (requirements != null) {
205 keyList.addAll(requirements.getKeys());
208 if (capabilities != null) {
209 keyList.addAll(capabilities.getKeys());
216 public void clean() {
221 properties = PfUtils.mapMap(properties, String::trim);
223 if (requirements != null) {
224 requirements.clean();
227 if (capabilities != null) {
228 capabilities.clean();
233 public BeanValidationResult validate(String fieldName) {
234 BeanValidationResult result = super.validate(fieldName);
236 result.addResult(validateNotBlank("type", type, true));
238 validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
239 validateOptional(result, "requirements", requirements);
240 validateOptional(result, "capabilities", capabilities);
246 public int compareTo(final PfConcept otherConcept) {
247 if (otherConcept == null) {
250 if (this == otherConcept) {
253 if (getClass() != otherConcept.getClass()) {
254 return getClass().getName().compareTo(otherConcept.getClass().getName());
257 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
258 int result = super.compareTo(other);
263 result = type.compareTo(other.type);
268 result = PfUtils.compareMaps(properties, other.properties);
273 result = ObjectUtils.compare(requirements, other.requirements);
278 return ObjectUtils.compare(capabilities, other.capabilities);