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.Entity;
31 import javax.persistence.FetchType;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.JoinColumn;
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.NotBlank;
43 import org.onap.policy.common.parameters.annotations.NotNull;
44 import org.onap.policy.common.parameters.annotations.Valid;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfModelRuntimeException;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
55 * Class to represent the node template in TOSCA definition.
58 @Table(name = "ToscaNodeTemplate")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaToscaNodeTemplate extends JpaToscaWithStringProperties<ToscaNodeTemplate> {
63 private static final long serialVersionUID = 1675770231921107988L;
65 private static final StandardCoder STANDARD_CODER = new StandardCoder();
73 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
74 @JoinColumn(name = "requirementsName", referencedColumnName = "name")
75 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
77 private JpaToscaRequirements requirements;
79 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
80 @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
81 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
83 private JpaToscaCapabilityAssignments capabilities;
87 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
89 public JpaToscaNodeTemplate() {
90 this(new PfConceptKey());
94 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
98 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
105 * @param copyConcept the concept to copy from
107 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
109 this.type = copyConcept.type;
111 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
113 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
117 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
120 * @param type the node template type
122 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
128 * Authorative constructor.
130 * @param authorativeConcept the authorative concept to copy from
132 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
133 this.fromAuthorative(authorativeConcept);
137 public ToscaNodeTemplate toAuthorative() {
138 ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
139 super.setToscaEntity(toscaNodeTemplate);
140 super.toAuthorative();
142 toscaNodeTemplate.setType(type);
144 if (requirements != null) {
145 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
148 if (capabilities != null) {
149 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
150 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
151 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
152 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
156 return toscaNodeTemplate;
160 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
161 super.fromAuthorative(toscaNodeTemplate);
163 type = toscaNodeTemplate.getType();
165 if (toscaNodeTemplate.getRequirements() != null) {
166 requirements = new JpaToscaRequirements();
167 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
170 if (toscaNodeTemplate.getCapabilities() != null) {
171 capabilities = new JpaToscaCapabilityAssignments();
172 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
177 protected Object deserializePropertyValue(String propValue) {
179 return STANDARD_CODER.decode(propValue, Object.class);
180 } catch (CoderException ce) {
181 String errorMessage = "error decoding property JSON value read from database: " + propValue;
182 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
187 protected String serializePropertyValue(Object propValue) {
189 return STANDARD_CODER.encode(propValue);
190 } catch (CoderException ce) {
191 String errorMessage = "error encoding property JSON value for database: " + propValue;
192 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
197 public List<PfKey> getKeys() {
198 final List<PfKey> keyList = super.getKeys();
200 if (requirements != null) {
201 keyList.addAll(requirements.getKeys());
204 if (capabilities != null) {
205 keyList.addAll(capabilities.getKeys());
212 public void clean() {
217 if (requirements != null) {
218 requirements.clean();
221 if (capabilities != null) {
222 capabilities.clean();
227 public int compareTo(final PfConcept otherConcept) {
228 if (this == otherConcept) {
232 int result = super.compareTo(otherConcept);
237 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
239 result = type.compareTo(other.type);
244 result = ObjectUtils.compare(requirements, other.requirements);
249 return ObjectUtils.compare(capabilities, other.capabilities);