* ============LICENSE_START=======================================================
* ONAP Policy Model
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* 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.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
-
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
+import javax.persistence.Lob;
import javax.persistence.Table;
-
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
-
-import org.onap.policy.common.utils.validation.Assertions;
+import org.apache.commons.collections4.CollectionUtils;
+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.PfReferenceKey;
import org.onap.policy.models.base.PfUtils;
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.ToscaDataType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
/**
* Class to represent custom data type in TOSCA definition.
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = true)
-public class JpaToscaDataType extends JpaToscaEntityType {
+public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
private static final long serialVersionUID = -3922690413436539164L;
@ElementCollection
private List<JpaToscaConstraint> constraints;
@ElementCollection
- private List<JpaToscaProperty> properties;
+ @Lob
+ private Map<String, JpaToscaProperty> properties;
/**
* The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
*/
public JpaToscaDataType(final JpaToscaDataType copyConcept) {
super(copyConcept);
+ // Constraints are immutable
+ this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
+ this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
+ }
+
+ /**
+ * Authorative constructor.
+ *
+ * @param authorativeConcept the authorative concept to copy from
+ */
+ public JpaToscaDataType(final ToscaDataType authorativeConcept) {
+ this.fromAuthorative(authorativeConcept);
}
@Override
- public List<PfKey> getKeys() {
- final List<PfKey> keyList = super.getKeys();
+ public ToscaDataType toAuthorative() {
+ ToscaDataType toscaDataType = new ToscaDataType();
+ super.setToscaEntity(toscaDataType);
+ super.toAuthorative();
- if (constraints != null) {
- for (JpaToscaConstraint constraint : constraints) {
- keyList.addAll(constraint.getKeys());
+ toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
+ toscaDataType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
+
+ return toscaDataType;
+ }
+
+ @Override
+ public void fromAuthorative(final ToscaDataType toscaDataType) {
+ super.fromAuthorative(toscaDataType);
+
+ constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
+
+ if (toscaDataType.getProperties() != null) {
+ properties = new LinkedHashMap<>();
+ for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
+ JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
+ jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
+ properties.put(toscaPropertyEntry.getKey(), jpaProperty);
}
}
+ }
+
+ @Override
+ public List<PfKey> getKeys() {
+ final List<PfKey> keyList = super.getKeys();
if (properties != null) {
- for (JpaToscaProperty property : properties) {
+ for (JpaToscaProperty property : properties.values()) {
keyList.addAll(property.getKeys());
}
}
public void clean() {
super.clean();
- if (constraints != null) {
- for (JpaToscaConstraint constraint : constraints) {
- constraint.clean();
- }
- }
-
if (properties != null) {
- for (JpaToscaProperty property : properties) {
+ for (JpaToscaProperty property : properties.values()) {
property.clean();
}
}
PfValidationResult result = super.validate(resultIn);
if (constraints != null) {
- result = validateConstraints(result);
+ validateConstraints(result);
}
if (properties != null) {
/**
* Validate the constraints.
*
- * @param result The result of validations up to now
- * @return the validation result
+ * @param result where to put the validation results
*/
- private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
- PfValidationResult result = resultIn;
-
+ private void validateConstraints(@NonNull final PfValidationResult result) {
for (JpaToscaConstraint constraint : constraints) {
if (constraint == null) {
result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
"data type constraint may not be null "));
- } else {
- result = constraint.validate(result);
}
}
- return result;
}
/**
* Validate the properties.
*
- * @param result The result of validations up to now
+ * @param resultIn The result of validations up to now
* @return the validation result
*/
private PfValidationResult validateProperties(final PfValidationResult resultIn) {
PfValidationResult result = resultIn;
- for (JpaToscaProperty property : properties) {
+ for (JpaToscaProperty property : properties.values()) {
if (property == null) {
result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
"data type property may not be null "));
return 0;
}
if (getClass() != otherConcept.getClass()) {
- return this.hashCode() - otherConcept.hashCode();
+ return getClass().getName().compareTo(otherConcept.getClass().getName());
}
final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
- if (!super.equals(other)) {
- return super.compareTo(other);
- }
-
- int result = PfUtils.compareObjects(constraints, other.constraints);
+ int result = super.compareTo(other);
if (result != 0) {
return result;
}
- result = PfUtils.compareObjects(properties, other.properties);
+ result = PfUtils.compareCollections(constraints, other.constraints);
if (result != 0) {
return result;
}
- return 0;
+ return PfUtils.compareMaps(properties, other.properties);
}
- @Override
- public PfConcept copyTo(@NonNull PfConcept target) {
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfConcept.class);
+ /**
+ * Get the data types referenced in a data type.
+ *
+ * @return the data types referenced in a data type
+ */
+ public Collection<PfConceptKey> getReferencedDataTypes() {
+ if (properties == null) {
+ return CollectionUtils.emptyCollection();
+ }
- final JpaToscaDataType copy = ((JpaToscaDataType) copyObject);
- super.copyTo(target);
+ Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
- if (constraints == null) {
- copy.setConstraints(null);
- }
- else {
- final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
- for (final JpaToscaConstraint constraint : constraints) {
- newConstraints.add(constraint); // Constraints are immutable
- }
- copy.setConstraints(newConstraints);
- }
+ for (JpaToscaProperty property : properties.values()) {
+ referencedDataTypes.add(property.getType());
- if (properties == null) {
- copy.setProperties(null);
- }
- else {
- final List<JpaToscaProperty> newProperties = new ArrayList<>();
- for (final JpaToscaProperty property : properties) {
- newProperties.add(new JpaToscaProperty(property));
+ if (property.getEntrySchema() != null) {
+ referencedDataTypes.add(property.getEntrySchema().getType());
}
- copy.setProperties(newProperties);
}
- return copy;
+ referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
+
+ return referencedDataTypes;
}
-}
\ No newline at end of file
+}