2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 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.Entries;
46 import org.onap.policy.common.parameters.annotations.Items;
47 import org.onap.policy.common.parameters.annotations.NotBlank;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.models.base.PfAuthorative;
53 import org.onap.policy.models.base.PfConcept;
54 import org.onap.policy.models.base.PfConceptKey;
55 import org.onap.policy.models.base.PfKey;
56 import org.onap.policy.models.base.PfModelRuntimeException;
57 import org.onap.policy.models.base.PfUtils;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
62 * Class to represent the node template in TOSCA definition.
65 @Table(name = "ToscaNodeTemplate")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @EqualsAndHashCode(callSuper = false)
69 public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
70 implements PfAuthorative<ToscaNodeTemplate> {
71 private static final long serialVersionUID = 1675770231921107988L;
73 private static final StandardCoder STANDARD_CODER = new StandardCoder();
82 @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
83 private Map<String, String> properties;
86 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
87 @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
88 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
90 private JpaToscaRequirements requirements;
92 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
93 @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
94 @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")})
96 private JpaToscaCapabilityAssignments capabilities;
100 * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
102 public JpaToscaNodeTemplate() {
103 this(new PfConceptKey());
107 * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
111 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
118 * @param copyConcept the concept to copy from
120 public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
122 this.type = copyConcept.type;
123 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
125 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
127 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
131 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
134 * @param type the node template type
136 public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
142 * Authorative constructor.
144 * @param authorativeConcept the authorative concept to copy from
146 public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
147 this.fromAuthorative(authorativeConcept);
151 public ToscaNodeTemplate toAuthorative() {
152 ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
153 super.setToscaEntity(toscaNodeTemplate);
154 super.toAuthorative();
156 toscaNodeTemplate.setType(type);
158 toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> {
160 return STANDARD_CODER.decode(property, Object.class);
161 } catch (CoderException ce) {
162 String errorMessage = "error decoding property JSON value read from database: " + property;
163 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
167 if (requirements != null) {
168 toscaNodeTemplate.setRequirements(requirements.toAuthorative());
171 if (capabilities != null) {
172 toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
173 List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
174 for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
175 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
179 return toscaNodeTemplate;
183 public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
184 super.fromAuthorative(toscaNodeTemplate);
186 type = toscaNodeTemplate.getType();
188 properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> {
190 return STANDARD_CODER.encode(property);
191 } catch (CoderException ce) {
192 String errorMessage = "error encoding property JSON value for database: " + property;
193 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
197 if (toscaNodeTemplate.getRequirements() != null) {
198 requirements = new JpaToscaRequirements();
199 requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
202 if (toscaNodeTemplate.getCapabilities() != null) {
203 capabilities = new JpaToscaCapabilityAssignments();
204 capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
209 public List<PfKey> getKeys() {
210 final List<PfKey> keyList = super.getKeys();
212 if (requirements != null) {
213 keyList.addAll(requirements.getKeys());
216 if (capabilities != null) {
217 keyList.addAll(capabilities.getKeys());
224 public void clean() {
229 properties = PfUtils.mapMap(properties, String::trim);
231 if (requirements != null) {
232 requirements.clean();
235 if (capabilities != null) {
236 capabilities.clean();
241 public int compareTo(final PfConcept otherConcept) {
242 if (otherConcept == null) {
245 if (this == otherConcept) {
248 if (getClass() != otherConcept.getClass()) {
249 return getClass().getName().compareTo(otherConcept.getClass().getName());
252 final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
253 int result = super.compareTo(other);
258 result = type.compareTo(other.type);
263 result = PfUtils.compareMaps(properties, other.properties);
268 result = ObjectUtils.compare(requirements, other.requirements);
273 return ObjectUtils.compare(capabilities, other.capabilities);