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 java.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import javax.persistence.CascadeType;
29 import javax.persistence.Entity;
30 import javax.persistence.FetchType;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.JoinColumns;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.ws.rs.core.Response;
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> {
61 private static final long serialVersionUID = 1675770231921107988L;
63 private static final StandardCoder STANDARD_CODER = new StandardCoder();
66 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
68 @JoinColumn(name = "requirementsName", referencedColumnName = "name"),
69 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
72 private JpaToscaRequirements requirements;
74 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
76 @JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
77 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
80 private JpaToscaCapabilityAssignments capabilities;
84 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
86 public JpaToscaNodeTemplate() {
87 this(new PfConceptKey());
91 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
95 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
102 * @param copyConcept the concept to copy from
104 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
108 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
110 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
114 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
117 * @param type the node template type
119 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
124 * Authorative constructor.
126 * @param authorativeConcept the authorative concept to copy from
128 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
129 this.fromAuthorative(authorativeConcept);
133 public ToscaNodeTemplate toAuthorative() {
134 var toscaNodeTemplate = new ToscaNodeTemplate();
135 super.setToscaEntity(toscaNodeTemplate);
136 super.toAuthorative();
138 if (requirements != null) {
139 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
142 if (capabilities != null) {
143 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
144 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
145 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
146 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
150 return toscaNodeTemplate;
154 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
155 super.fromAuthorative(toscaNodeTemplate);
157 if (toscaNodeTemplate.getRequirements() != null) {
158 requirements = new JpaToscaRequirements();
159 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
162 if (toscaNodeTemplate.getCapabilities() != null) {
163 capabilities = new JpaToscaCapabilityAssignments();
164 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
169 protected Object deserializePropertyValue(String propValue) {
171 return STANDARD_CODER.decode(propValue, Object.class);
172 } catch (CoderException ce) {
173 String errorMessage = "error decoding property JSON value read from database: " + propValue;
174 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
179 protected String serializePropertyValue(Object propValue) {
181 return STANDARD_CODER.encode(propValue);
182 } catch (CoderException ce) {
183 String errorMessage = "error encoding property JSON value for database: " + propValue;
184 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
189 public List<PfKey> getKeys() {
190 final List<PfKey> keyList = super.getKeys();
192 if (requirements != null) {
193 keyList.addAll(requirements.getKeys());
196 if (capabilities != null) {
197 keyList.addAll(capabilities.getKeys());
204 public void clean() {
207 if (requirements != null) {
208 requirements.clean();
211 if (capabilities != null) {
212 capabilities.clean();
217 public int compareTo(final PfConcept otherConcept) {
218 if (this == otherConcept) {
222 int result = super.compareTo(otherConcept);
227 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
229 result = ObjectUtils.compare(requirements, other.requirements);
234 return ObjectUtils.compare(capabilities, other.capabilities);