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.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.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;
60 * Class to represent the node template in TOSCA definition.
63 @Table(name = "ToscaNodeTemplate")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
68 implements PfAuthorative<ToscaNodeTemplate> {
69 private static final long serialVersionUID = 1675770231921107988L;
71 private static final StandardCoder STANDARD_CODER = new StandardCoder();
80 private Map<@NotNull String, @NotNull String> properties;
83 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
84 @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
85 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
87 private JpaToscaRequirements requirements;
89 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
90 @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
91 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")})
93 private JpaToscaCapabilityAssignments capabilities;
97 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
99 public JpaToscaNodeTemplate() {
100 this(new PfConceptKey());
104 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
108 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
115 * @param copyConcept the concept to copy from
117 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
119 this.type = copyConcept.type;
120 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
122 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
124 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
128 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
131 * @param type the node template type
133 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
139 * Authorative constructor.
141 * @param authorativeConcept the authorative concept to copy from
143 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
144 this.fromAuthorative(authorativeConcept);
148 public ToscaNodeTemplate toAuthorative() {
149 ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
150 super.setToscaEntity(toscaNodeTemplate);
151 super.toAuthorative();
153 toscaNodeTemplate.setType(type);
155 toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> {
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);
164 if (requirements != null) {
165 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
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);
176 return toscaNodeTemplate;
180 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
181 super.fromAuthorative(toscaNodeTemplate);
183 type = toscaNodeTemplate.getType();
185 properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> {
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);
194 if (toscaNodeTemplate.getRequirements() != null) {
195 requirements = new JpaToscaRequirements();
196 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
199 if (toscaNodeTemplate.getCapabilities() != null) {
200 capabilities = new JpaToscaCapabilityAssignments();
201 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
206 public List<PfKey> getKeys() {
207 final List<PfKey> keyList = super.getKeys();
209 if (requirements != null) {
210 keyList.addAll(requirements.getKeys());
213 if (capabilities != null) {
214 keyList.addAll(capabilities.getKeys());
221 public void clean() {
226 properties = PfUtils.mapMap(properties, String::trim);
228 if (requirements != null) {
229 requirements.clean();
232 if (capabilities != null) {
233 capabilities.clean();
238 public int compareTo(final PfConcept otherConcept) {
239 if (otherConcept == null) {
242 if (this == otherConcept) {
245 if (getClass() != otherConcept.getClass()) {
246 return getClass().getName().compareTo(otherConcept.getClass().getName());
249 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
250 int result = super.compareTo(other);
255 result = type.compareTo(other.type);
260 result = PfUtils.compareMaps(properties, other.properties);
265 result = ObjectUtils.compare(requirements, other.requirements);
270 return ObjectUtils.compare(capabilities, other.capabilities);