2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 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.OneToOne;
35 import javax.persistence.Table;
36 import javax.ws.rs.core.Response;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.common.parameters.annotations.Valid;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
52 * Class to represent the node template in TOSCA definition.
55 @Table(name = "ToscaNodeTemplate")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaToscaNodeTemplate extends JpaToscaWithTypeAndStringProperties<ToscaNodeTemplate> {
60 private static final long serialVersionUID = 1675770231921107988L;
62 private static final StandardCoder STANDARD_CODER = new StandardCoder();
65 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
66 @JoinColumn(name = "requirementsName", referencedColumnName = "name")
67 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
69 private JpaToscaRequirements requirements;
71 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
72 @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
73 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
75 private JpaToscaCapabilityAssignments capabilities;
79 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
81 public JpaToscaNodeTemplate() {
82 this(new PfConceptKey());
86 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
90 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
97 * @param copyConcept the concept to copy from
99 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
103 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
105 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
109 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
112 * @param type the node template type
114 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
119 * Authorative constructor.
121 * @param authorativeConcept the authorative concept to copy from
123 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
124 this.fromAuthorative(authorativeConcept);
128 public ToscaNodeTemplate toAuthorative() {
129 var toscaNodeTemplate = new ToscaNodeTemplate();
130 super.setToscaEntity(toscaNodeTemplate);
131 super.toAuthorative();
133 if (requirements != null) {
134 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
137 if (capabilities != null) {
138 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
139 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
140 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
141 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
145 return toscaNodeTemplate;
149 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
150 super.fromAuthorative(toscaNodeTemplate);
152 if (toscaNodeTemplate.getRequirements() != null) {
153 requirements = new JpaToscaRequirements();
154 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
157 if (toscaNodeTemplate.getCapabilities() != null) {
158 capabilities = new JpaToscaCapabilityAssignments();
159 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
164 protected Object deserializePropertyValue(String propValue) {
166 return STANDARD_CODER.decode(propValue, Object.class);
167 } catch (CoderException ce) {
168 String errorMessage = "error decoding property JSON value read from database: " + propValue;
169 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
174 protected String serializePropertyValue(Object propValue) {
176 return STANDARD_CODER.encode(propValue);
177 } catch (CoderException ce) {
178 String errorMessage = "error encoding property JSON value for database: " + propValue;
179 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
184 public List<PfKey> getKeys() {
185 final List<PfKey> keyList = super.getKeys();
187 if (requirements != null) {
188 keyList.addAll(requirements.getKeys());
191 if (capabilities != null) {
192 keyList.addAll(capabilities.getKeys());
199 public void clean() {
202 if (requirements != null) {
203 requirements.clean();
206 if (capabilities != null) {
207 capabilities.clean();
212 public int compareTo(final PfConcept otherConcept) {
213 if (this == otherConcept) {
217 int result = super.compareTo(otherConcept);
222 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
224 result = ObjectUtils.compare(requirements, other.requirements);
229 return ObjectUtils.compare(capabilities, other.capabilities);