2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import java.util.List;
24 import javax.persistence.Column;
25 import javax.persistence.EmbeddedId;
26 import javax.persistence.Entity;
27 import javax.persistence.Inheritance;
28 import javax.persistence.InheritanceType;
29 import javax.persistence.Table;
31 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
35 import org.onap.policy.models.base.PfAuthorative;
36 import org.onap.policy.models.base.PfConcept;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.base.PfValidationMessage;
41 import org.onap.policy.models.base.PfValidationResult;
42 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
46 * Class to represent the parameter in TOSCA definition.
49 @Table(name = "ToscaParameter")
50 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaParameter> {
54 private static final long serialVersionUID = 1675770231921107988L;
57 private PfReferenceKey key;
60 private PfConceptKey type;
66 * The Default Constructor creates a {@link JpaToscaParameter} object with a null key.
68 public JpaToscaParameter() {
69 this(new PfReferenceKey());
73 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
77 public JpaToscaParameter(@NonNull final PfReferenceKey key) {
78 this(key, new PfConceptKey());
82 * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
85 * @param type the key of the parameter type
87 public JpaToscaParameter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
95 * @param copyConcept the concept to copy from
97 public JpaToscaParameter(final JpaToscaParameter copyConcept) {
99 this.key = new PfReferenceKey(copyConcept.key);
100 this.type = new PfConceptKey(copyConcept.type);
101 this.value = copyConcept.value;
105 * Authorative constructor.
107 * @param authorativeConcept the authorative concept to copy from
109 public JpaToscaParameter(final ToscaParameter authorativeConcept) {
110 this.fromAuthorative(authorativeConcept);
114 public ToscaParameter toAuthorative() {
115 ToscaParameter toscaParameter = new ToscaParameter();
117 toscaParameter.setName(key.getLocalName());
119 toscaParameter.setType(type.getName());
120 toscaParameter.setTypeVersion(type.getVersion());
122 if (!StringUtils.isBlank(value)) {
123 toscaParameter.setValue(new YamlJsonTranslator().fromYaml(value, Object.class));
126 return toscaParameter;
130 public void fromAuthorative(ToscaParameter toscaParameter) {
131 this.setKey(new PfReferenceKey());
132 getKey().setLocalName(toscaParameter.getName());
134 if (toscaParameter.getTypeVersion() != null) {
135 type = new PfConceptKey(toscaParameter.getType(), toscaParameter.getTypeVersion());
137 type = new PfConceptKey(toscaParameter.getType(), PfKey.NULL_KEY_VERSION);
140 value = new YamlJsonTranslator().toYaml(toscaParameter.getValue());
144 public List<PfKey> getKeys() {
145 final List<PfKey> keyList = getKey().getKeys();
147 keyList.addAll(type.getKeys());
153 public void clean() {
159 value = value.trim();
164 public PfValidationResult validate(final PfValidationResult resultIn) {
165 PfValidationResult result = resultIn;
167 if (key.isNullKey()) {
168 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
169 "paremeter key is a null key"));
172 result = key.validate(result);
174 if (type == null || type.isNullKey()) {
175 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
176 "parameter type may not be null"));
183 public int compareTo(final PfConcept otherConcept) {
184 if (otherConcept == null) {
187 if (this == otherConcept) {
190 if (getClass() != otherConcept.getClass()) {
191 return getClass().getName().compareTo(otherConcept.getClass().getName());
194 final JpaToscaParameter other = (JpaToscaParameter) otherConcept;
195 int result = key.compareTo(other.key);
200 return value.compareTo(other.value);