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
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.Lob;
37 import javax.persistence.OneToOne;
38 import javax.persistence.Table;
39 import javax.ws.rs.core.Response;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43 import org.apache.commons.lang3.ObjectUtils;
44 import org.onap.policy.common.parameters.annotations.NotBlank;
45 import org.onap.policy.common.parameters.annotations.NotNull;
46 import org.onap.policy.common.parameters.annotations.Valid;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfModelRuntimeException;
54 import org.onap.policy.models.base.PfUtils;
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();
79 private Map<@NotNull String, @NotNull String> properties;
82 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
83 @JoinColumn(name = "requirementsName", referencedColumnName = "name")
84 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
86 private JpaToscaRequirements requirements;
88 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
89 @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
90 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
92 private JpaToscaCapabilityAssignments capabilities;
96 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
98 public JpaToscaNodeTemplate() {
99 this(new PfConceptKey());
103 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
107 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
114 * @param copyConcept the concept to copy from
116 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
118 this.type = copyConcept.type;
119 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
121 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
123 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
127 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
130 * @param type the node template type
132 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
138 * Authorative constructor.
140 * @param authorativeConcept the authorative concept to copy from
142 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
143 this.fromAuthorative(authorativeConcept);
147 public ToscaNodeTemplate toAuthorative() {
148 ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
149 super.setToscaEntity(toscaNodeTemplate);
150 super.toAuthorative();
152 toscaNodeTemplate.setType(type);
154 toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> {
156 return STANDARD_CODER.decode(property, Object.class);
157 } catch (CoderException ce) {
158 String errorMessage = "error decoding property JSON value read from database: " + property;
159 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
163 if (requirements != null) {
164 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
167 if (capabilities != null) {
168 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
169 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
170 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
171 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
175 return toscaNodeTemplate;
179 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
180 super.fromAuthorative(toscaNodeTemplate);
182 type = toscaNodeTemplate.getType();
184 properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> {
186 return STANDARD_CODER.encode(property);
187 } catch (CoderException ce) {
188 String errorMessage = "error encoding property JSON value for database: " + property;
189 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
193 if (toscaNodeTemplate.getRequirements() != null) {
194 requirements = new JpaToscaRequirements();
195 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
198 if (toscaNodeTemplate.getCapabilities() != null) {
199 capabilities = new JpaToscaCapabilityAssignments();
200 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
205 public List<PfKey> getKeys() {
206 final List<PfKey> keyList = super.getKeys();
208 if (requirements != null) {
209 keyList.addAll(requirements.getKeys());
212 if (capabilities != null) {
213 keyList.addAll(capabilities.getKeys());
220 public void clean() {
225 properties = PfUtils.mapMap(properties, String::trim);
227 if (requirements != null) {
228 requirements.clean();
231 if (capabilities != null) {
232 capabilities.clean();
237 public int compareTo(final PfConcept otherConcept) {
238 if (otherConcept == null) {
241 if (this == otherConcept) {
244 if (getClass() != otherConcept.getClass()) {
245 return getClass().getName().compareTo(otherConcept.getClass().getName());
248 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
249 int result = super.compareTo(other);
254 result = type.compareTo(other.type);
259 result = PfUtils.compareMaps(properties, other.properties);
264 result = ObjectUtils.compare(requirements, other.requirements);
269 return ObjectUtils.compare(capabilities, other.capabilities);