2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021, 2023 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 jakarta.persistence.CascadeType;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.FetchType;
27 import jakarta.persistence.Inheritance;
28 import jakarta.persistence.InheritanceType;
29 import jakarta.persistence.JoinColumn;
30 import jakarta.persistence.OneToOne;
31 import jakarta.persistence.Table;
32 import jakarta.ws.rs.core.Response;
33 import java.io.Serial;
34 import java.util.Collections;
35 import java.util.LinkedHashMap;
36 import java.util.List;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.common.parameters.annotations.Valid;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfModelRuntimeException;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53 * Class to represent the node template in TOSCA definition.
56 @Table(name = "ToscaNodeTemplate")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaToscaNodeTemplate extends JpaToscaWithTypeAndStringProperties<ToscaNodeTemplate> {
62 private static final long serialVersionUID = 1675770231921107988L;
64 private static final StandardCoder STANDARD_CODER = new StandardCoder();
67 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
68 @JoinColumn(name = "requirementsName", referencedColumnName = "name")
69 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
71 private JpaToscaRequirements requirements;
73 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
74 @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
75 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
77 private JpaToscaCapabilityAssignments capabilities;
81 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
83 public JpaToscaNodeTemplate() {
84 this(new PfConceptKey());
88 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
92 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
99 * @param copyConcept the concept to copy from
101 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
105 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
107 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
111 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
114 * @param type the node template type
116 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
121 * Authorative constructor.
123 * @param authorativeConcept the authorative concept to copy from
125 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
126 this.fromAuthorative(authorativeConcept);
130 public ToscaNodeTemplate toAuthorative() {
131 var toscaNodeTemplate = new ToscaNodeTemplate();
132 super.setToscaEntity(toscaNodeTemplate);
133 super.toAuthorative();
135 if (requirements != null) {
136 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
139 if (capabilities != null) {
140 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
141 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
142 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
143 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
147 return toscaNodeTemplate;
151 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
152 super.fromAuthorative(toscaNodeTemplate);
154 if (toscaNodeTemplate.getRequirements() != null) {
155 requirements = new JpaToscaRequirements();
156 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
159 if (toscaNodeTemplate.getCapabilities() != null) {
160 capabilities = new JpaToscaCapabilityAssignments();
161 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
166 protected Object deserializePropertyValue(String propValue) {
168 return STANDARD_CODER.decode(propValue, Object.class);
169 } catch (CoderException ce) {
170 String errorMessage = "error decoding property JSON value read from database: " + propValue;
171 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
176 protected String serializePropertyValue(Object propValue) {
178 return STANDARD_CODER.encode(propValue);
179 } catch (CoderException ce) {
180 String errorMessage = "error encoding property JSON value for database: " + propValue;
181 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
186 public List<PfKey> getKeys() {
187 final List<PfKey> keyList = super.getKeys();
189 if (requirements != null) {
190 keyList.addAll(requirements.getKeys());
193 if (capabilities != null) {
194 keyList.addAll(capabilities.getKeys());
201 public void clean() {
204 if (requirements != null) {
205 requirements.clean();
208 if (capabilities != null) {
209 capabilities.clean();
214 public int compareTo(final PfConcept otherConcept) {
215 if (this == otherConcept) {
219 int result = super.compareTo(otherConcept);
224 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
226 result = ObjectUtils.compare(requirements, other.requirements);
231 return ObjectUtils.compare(capabilities, other.capabilities);