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.List;
25 import javax.persistence.Column;
26 import javax.persistence.EmbeddedId;
27 import javax.persistence.Entity;
28 import javax.persistence.Inheritance;
29 import javax.persistence.InheritanceType;
30 import javax.persistence.Table;
32 import lombok.EqualsAndHashCode;
33 import lombok.NonNull;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
37 import org.onap.policy.models.base.PfAuthorative;
38 import org.onap.policy.models.base.PfConcept;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfReferenceKey;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
45 * Class to represent the parameter in TOSCA definition.
48 @Table(name = "ToscaParameter")
49 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
51 @EqualsAndHashCode(callSuper = false)
52 public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaParameter> {
53 private static final long serialVersionUID = 1675770231921107988L;
56 private PfReferenceKey key;
59 private PfConceptKey type;
65 * The Default Constructor creates a {@link JpaToscaParameter} object with a null key.
67 public JpaToscaParameter() {
68 this(new PfReferenceKey());
72 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
76 public JpaToscaParameter(@NonNull final PfReferenceKey key) {
77 this(key, new PfConceptKey());
81 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
84 * @param type the key of the parameter type
86 public JpaToscaParameter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
94 * @param copyConcept the concept to copy from
96 public JpaToscaParameter(final JpaToscaParameter copyConcept) {
98 this.key = new PfReferenceKey(copyConcept.key);
99 this.type = new PfConceptKey(copyConcept.type);
100 this.value = copyConcept.value;
104 * Authorative constructor.
106 * @param authorativeConcept the authorative concept to copy from
108 public JpaToscaParameter(final ToscaParameter authorativeConcept) {
109 this.fromAuthorative(authorativeConcept);
113 public ToscaParameter toAuthorative() {
114 ToscaParameter toscaParameter = new ToscaParameter();
116 toscaParameter.setName(key.getLocalName());
118 toscaParameter.setType(type.getName());
119 toscaParameter.setTypeVersion(type.getVersion());
121 if (!StringUtils.isBlank(value)) {
122 toscaParameter.setValue(new YamlJsonTranslator().fromYaml(value, Object.class));
125 return toscaParameter;
129 public void fromAuthorative(ToscaParameter toscaParameter) {
130 this.setKey(new PfReferenceKey());
131 getKey().setLocalName(toscaParameter.getName());
133 if (toscaParameter.getTypeVersion() != null) {
134 type = new PfConceptKey(toscaParameter.getType(), toscaParameter.getTypeVersion());
136 type = new PfConceptKey(toscaParameter.getType(), PfKey.NULL_KEY_VERSION);
139 value = new YamlJsonTranslator().toYaml(toscaParameter.getValue());
143 public List<PfKey> getKeys() {
144 final List<PfKey> keyList = getKey().getKeys();
146 keyList.addAll(type.getKeys());
152 public void clean() {
158 value = value.trim();
163 public BeanValidationResult validate(String fieldName) {
164 BeanValidationResult result = new BeanValidationResult(fieldName, this);
166 result.addResult(validateKeyNotNull("key", key));
167 result.addResult(validateKeyNotNull("type", type));
173 public int compareTo(final PfConcept otherConcept) {
174 if (otherConcept == null) {
177 if (this == otherConcept) {
180 if (getClass() != otherConcept.getClass()) {
181 return getClass().getName().compareTo(otherConcept.getClass().getName());
184 final JpaToscaParameter other = (JpaToscaParameter) otherConcept;
185 int result = key.compareTo(other.key);
190 return value.compareTo(other.value);