2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.LinkedHashMap;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.TreeMap;
29 import javax.persistence.AttributeOverride;
30 import javax.persistence.AttributeOverrides;
31 import javax.persistence.Column;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.MappedSuperclass;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
51 * Class to represent the EntrySchema of list/map property in TOSCA definition.
55 @EqualsAndHashCode(callSuper = false)
56 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
57 private static final long serialVersionUID = -1330661834220739393L;
60 private PfConceptKey key;
65 @AttributeOverride(name = "name",
66 column = @Column(name = "derived_from_name")),
67 @AttributeOverride(name = "version",
68 column = @Column(name = "derived_from_version"))
70 private PfConceptKey derivedFrom;
73 private Map<String, String> metadata;
76 private String description;
78 private transient T toscaEntity;
82 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
84 public JpaToscaEntityType() {
85 this(new PfConceptKey());
89 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
93 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
100 * @param copyConcept the concept to copy from
102 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
104 this.key = new PfConceptKey(copyConcept.key);
105 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
106 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
107 this.description = copyConcept.description;
111 * Authorative constructor.
113 * @param authorativeConcept the authorative concept to copy from
115 public JpaToscaEntityType(final T authorativeConcept) {
116 this.fromAuthorative(authorativeConcept);
120 public T toAuthorative() {
121 toscaEntity.setName(getKey().getName());
122 toscaEntity.setVersion(getKey().getVersion());
124 if (derivedFrom != null) {
125 toscaEntity.setDerivedFrom(derivedFrom.getId());
128 if (description != null) {
129 toscaEntity.setDescription(description);
132 if (metadata != null) {
133 Map<String, String> metadataMap = new LinkedHashMap<>();
135 for (Entry<String, String> entry : metadata.entrySet()) {
136 metadataMap.put(entry.getKey(), entry.getValue());
139 toscaEntity.setMetadata(metadataMap);
146 public void fromAuthorative(T toscaEntity) {
147 key = new PfConceptKey();
149 if (toscaEntity.getName() != null) {
150 key.setName(toscaEntity.getName());
153 if (toscaEntity.getVersion() != null) {
154 key.setVersion(toscaEntity.getVersion());
158 if (toscaEntity.getDerivedFrom() != null) {
159 // CHeck if the derived from field contains a name-version ID
160 if (toscaEntity.getDerivedFrom().contains(":")) {
161 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
163 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
167 if (toscaEntity.getDescription() != null) {
168 description = toscaEntity.getDescription();
171 if (toscaEntity.getMetadata() != null) {
172 metadata = new LinkedHashMap<>();
174 for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) {
175 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
181 public List<PfKey> getKeys() {
182 final List<PfKey> keyList = getKey().getKeys();
183 if (derivedFrom != null) {
184 keyList.addAll(derivedFrom.getKeys());
190 public void clean() {
193 if (derivedFrom != null) {
197 if (metadata != null) {
198 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
199 metadataEntry.setValue(metadataEntry.getValue().trim());
203 description = (description != null ? description.trim() : null);
207 public PfValidationResult validate(PfValidationResult resultIn) {
208 PfValidationResult result = resultIn;
210 if (key.isNullKey()) {
211 result.addValidationMessage(
212 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
215 result = key.validate(result);
217 if (derivedFrom != null && derivedFrom.isNullKey()) {
218 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219 "derived from key is a null key"));
222 if (metadata != null) {
223 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
224 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
225 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
226 "property metadata key may not be null"));
228 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
229 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
230 "property metadata value may not be null"));
235 if (description != null && description.trim().length() == 0) {
236 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
237 "property description may not be blank"));
244 public int compareTo(final PfConcept otherConcept) {
245 if (otherConcept == null) {
248 if (this == otherConcept) {
251 if (getClass() != otherConcept.getClass()) {
252 return getClass().getName().compareTo(otherConcept.getClass().getName());
255 @SuppressWarnings("unchecked")
256 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
257 if (!key.equals(other.key)) {
258 return key.compareTo(other.key);
261 int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
266 result = PfUtils.compareObjects(metadata, other.metadata);
271 return ObjectUtils.compare(description, other.description);