* ONAP Policy Model
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * 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.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
/**
* Class to represent the policy type in TOSCA definition.
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = true)
-public class JpaToscaPolicyType extends JpaToscaEntityType {
+public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
private static final long serialVersionUID = -563659852901842616L;
@ElementCollection
- private List<JpaToscaProperty> properties;
+ @Lob
+ private Map<String, JpaToscaProperty> properties;
@ElementCollection
private List<PfConceptKey> targets;
*/
public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
super(copyConcept);
+ this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
+ this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
+ this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
+ }
+
+ /**
+ * Authorative constructor.
+ *
+ * @param authorativeConcept the authorative concept to copy from
+ */
+ public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
+ this.fromAuthorative(authorativeConcept);
}
+ @Override
+ public ToscaPolicyType toAuthorative() {
+ ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
+ super.setToscaEntity(toscaPolicyType);
+ super.toAuthorative();
+
+ toscaPolicyType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
+
+ return toscaPolicyType;
+ }
+
+ @Override
+ public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
+ super.fromAuthorative(toscaPolicyType);
+
+ // Set properties
+ if (toscaPolicyType.getProperties() != null) {
+ properties = new LinkedHashMap<>();
+ for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.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());
}
}
super.clean();
if (properties != null) {
- for (JpaToscaProperty property : properties) {
+ for (JpaToscaProperty property : properties.values()) {
property.clean();
}
}
public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
PfValidationResult result = super.validate(resultIn);
+ if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
+ result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
+ "key version is a null version"));
+ }
+
if (properties != null) {
result = validateProperties(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,
"policy property may not be null "));
return 0;
}
if (getClass() != otherConcept.getClass()) {
- return this.hashCode() - otherConcept.hashCode();
+ return getClass().getName().compareTo(otherConcept.getClass().getName());
}
final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
- if (!super.equals(other)) {
- return super.compareTo(other);
+ int result = super.compareTo(other);
+ if (result != 0) {
+ return result;
}
- int retVal = PfUtils.compareObjects(properties, other.properties);
- if (retVal != 0) {
- return retVal;
+ result = PfUtils.compareMaps(properties, other.properties);
+ if (result != 0) {
+ return result;
}
- retVal = PfUtils.compareObjects(targets, other.targets);
- if (retVal != 0) {
- return retVal;
+ result = PfUtils.compareCollections(targets, other.targets);
+ if (result != 0) {
+ return result;
}
- return PfUtils.compareObjects(triggers, other.triggers);
+ return PfUtils.compareCollections(triggers, other.triggers);
}
- @Override
- public PfConcept copyTo(@NonNull PfConcept target) {
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfConcept.class);
-
- final JpaToscaPolicyType copy = ((JpaToscaPolicyType) copyObject);
- super.copyTo(target);
-
- final List<JpaToscaProperty> newProperties = new ArrayList<>();
-
+ /**
+ * Get the data types referenced in a policy type.
+ *
+ * @return the data types referenced in a policy type
+ */
+ public Collection<PfConceptKey> getReferencedDataTypes() {
if (properties == null) {
- copy.setProperties(null);
- } else {
- for (final JpaToscaProperty property : properties) {
- newProperties.add(new JpaToscaProperty(property));
- }
- copy.setProperties(newProperties);
+ return CollectionUtils.emptyCollection();
}
- if (targets == null) {
- copy.setTargets(null);
- } else {
- final List<PfConceptKey> newTargets = new ArrayList<>();
- for (final PfConceptKey oldTarget : targets) {
- newTargets.add(new PfConceptKey(oldTarget));
- }
- copy.setTargets(newTargets);
- }
+ Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
+
+ for (JpaToscaProperty property : properties.values()) {
+ referencedDataTypes.add(property.getType());
- if (triggers == null) {
- copy.setTargets(null);
- } else {
- final List<JpaToscaTrigger> newTriggers = new ArrayList<>();
- for (final JpaToscaTrigger trigger : triggers) {
- newTriggers.add(new JpaToscaTrigger(trigger));
+ if (property.getEntrySchema() != null) {
+ referencedDataTypes.add(property.getEntrySchema().getType());
}
- copy.setTriggers(newTriggers);
}
- return copy;
+ referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
+
+ return referencedDataTypes;
}
}