/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.models.tosca.simple.concepts;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import lombok.NonNull;
import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.common.utils.validation.ParameterValidationUtils;
+import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfValidationMessage;
import org.onap.policy.models.base.PfValidationResult;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
/**
* Class to represent the EntrySchema of list/map property in TOSCA definition.
@MappedSuperclass
@Data
@EqualsAndHashCode(callSuper = false)
-public class JpaToscaEntityType extends PfConcept {
+public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
private static final long serialVersionUID = -1330661834220739393L;
@EmbeddedId
private PfConceptKey derivedFrom;
@ElementCollection
- private Map<String, String> metadata;
+ private Map<String, String> metadata = new TreeMap<>();
@Column
private String description;
+
+ private transient T toscaEntity;
// @formatter:on
/**
*
* @param copyConcept the concept to copy from
*/
- public JpaToscaEntityType(final JpaToscaEntityType copyConcept) {
+ public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
super(copyConcept);
+ this.key = new PfConceptKey(copyConcept.key);
+ this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
+ this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
+ this.description = copyConcept.description;
+ }
+
+ /**
+ * Authorative constructor.
+ *
+ * @param authorativeConcept the authorative concept to copy from
+ */
+ public JpaToscaEntityType(final T authorativeConcept) {
+ this.fromAuthorative(authorativeConcept);
+ }
+
+ @Override
+ public T toAuthorative() {
+ toscaEntity.setName(getKey().getName());
+ toscaEntity.setVersion(getKey().getVersion());
+
+ if (derivedFrom != null) {
+ toscaEntity.setDerivedFrom(derivedFrom.getName());
+ }
+
+ if (description != null) {
+ toscaEntity.setDescription(description);
+ }
+
+ if (metadata != null) {
+ Map<String, String> metadataMap = new LinkedHashMap<>();
+
+ for (Entry<String, String> entry : metadata.entrySet()) {
+ metadataMap.put(entry.getKey(), entry.getValue());
+ }
+
+ toscaEntity.setMetadata(metadataMap);
+ }
+
+ return toscaEntity;
+ }
+
+ @Override
+ public void fromAuthorative(T toscaEntity) {
+ key = new PfConceptKey();
+
+ if (toscaEntity.getName() != null) {
+ key.setName(toscaEntity.getName());
+ }
+
+ if (toscaEntity.getVersion() != null) {
+ key.setVersion(toscaEntity.getVersion());
+ }
+
+ if (toscaEntity.getDerivedFrom() != null) {
+ // CHeck if the derived from field contains a name-version ID
+ if (toscaEntity.getDerivedFrom().contains(":")) {
+ derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
+ } else {
+ derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
+ }
+ }
+
+ if (toscaEntity.getDescription() != null) {
+ description = toscaEntity.getDescription();
+ }
+
+ if (toscaEntity.getMetadata() != null) {
+ metadata = new LinkedHashMap<>();
+
+ for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) {
+ metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
+ }
+ }
}
@Override
return 0;
}
if (getClass() != otherConcept.getClass()) {
- return this.hashCode() - otherConcept.hashCode();
+ return getClass().getName().compareTo(otherConcept.getClass().getName());
}
- final JpaToscaEntityType other = (JpaToscaEntityType) otherConcept;
+ @SuppressWarnings("unchecked")
+ final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
if (!key.equals(other.key)) {
return key.compareTo(other.key);
}
return ObjectUtils.compare(description, other.description);
}
-
- @Override
- public PfConcept copyTo(@NonNull PfConcept target) {
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfConcept.class);
-
- final JpaToscaEntityType copy = ((JpaToscaEntityType) copyObject);
- copy.setKey(new PfConceptKey(key));
- copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
-
- if (metadata != null) {
- final Map<String, String> newMatadata = new TreeMap<>();
- for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
- newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
- }
- copy.setMetadata(newMatadata);
- }
-
- copy.setDescription(description);
-
- return copy;
- }
}