/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2020 Nordix Foundation.
+ * Copyright (C) 2020-2021, 2023 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021 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 jakarta.persistence.CascadeType;
+import jakarta.persistence.Entity;
+import jakarta.persistence.FetchType;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.JoinColumn;
+import jakarta.persistence.OneToOne;
+import jakarta.persistence.Table;
+import jakarta.ws.rs.core.Response;
+import java.io.Serial;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import javax.persistence.CascadeType;
-import javax.persistence.Column;
-import javax.persistence.ElementCollection;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.JoinColumn;
-import javax.persistence.JoinColumns;
-import javax.persistence.Lob;
-import javax.persistence.OneToOne;
-import javax.persistence.Table;
-import javax.ws.rs.core.Response;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import org.apache.commons.lang3.ObjectUtils;
-import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.parameters.annotations.Valid;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
-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.PfModelRuntimeException;
-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.ToscaCapabilityAssignment;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = false)
-public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
- implements PfAuthorative<ToscaNodeTemplate> {
+public class JpaToscaNodeTemplate extends JpaToscaWithTypeAndStringProperties<ToscaNodeTemplate> {
+ @Serial
private static final long serialVersionUID = 1675770231921107988L;
private static final StandardCoder STANDARD_CODER = new StandardCoder();
- @Column
- private String type;
-
- @ElementCollection
- @Lob
- private Map<String, String> properties;
-
// formatter:off
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
- @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
- @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
+ @JoinColumn(name = "requirementsName", referencedColumnName = "name")
+ @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
+ @Valid
private JpaToscaRequirements requirements;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
- @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
- @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")})
+ @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
+ @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
+ @Valid
private JpaToscaCapabilityAssignments capabilities;
// @formatter:on
*/
public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
super(copyConcept);
- this.type = copyConcept.type;
- this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
+
this.requirements =
(copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
this.capabilities =
*/
public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
super(key);
- this.type = type;
}
/**
@Override
public ToscaNodeTemplate toAuthorative() {
- ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
+ var toscaNodeTemplate = new ToscaNodeTemplate();
super.setToscaEntity(toscaNodeTemplate);
super.toAuthorative();
- toscaNodeTemplate.setType(type);
-
- toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> {
- try {
- return STANDARD_CODER.decode(property, Object.class);
- } catch (CoderException ce) {
- String errorMessage = "error decoding property JSON value read from database: " + property;
- throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
- }
- }));
-
if (requirements != null) {
toscaNodeTemplate.setRequirements(requirements.toAuthorative());
}
public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
super.fromAuthorative(toscaNodeTemplate);
- type = toscaNodeTemplate.getType();
-
- properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> {
- try {
- return STANDARD_CODER.encode(property);
- } catch (CoderException ce) {
- String errorMessage = "error encoding property JSON value for database: " + property;
- throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
- }
- });
-
if (toscaNodeTemplate.getRequirements() != null) {
requirements = new JpaToscaRequirements();
requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
}
}
+ @Override
+ protected Object deserializePropertyValue(String propValue) {
+ try {
+ return STANDARD_CODER.decode(propValue, Object.class);
+ } catch (CoderException ce) {
+ String errorMessage = "error decoding property JSON value read from database: " + propValue;
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
+ }
+ }
+
+ @Override
+ protected String serializePropertyValue(Object propValue) {
+ try {
+ return STANDARD_CODER.encode(propValue);
+ } catch (CoderException ce) {
+ String errorMessage = "error encoding property JSON value for database: " + propValue;
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
+ }
+ }
+
@Override
public List<PfKey> getKeys() {
final List<PfKey> keyList = super.getKeys();
public void clean() {
super.clean();
- type = type.trim();
-
- properties = PfUtils.mapMap(properties, String::trim);
-
if (requirements != null) {
requirements.clean();
}
}
}
- @Override
- public PfValidationResult validate(final PfValidationResult resultIn) {
- PfValidationResult result = super.validate(resultIn);
-
- if (StringUtils.isBlank(type)) {
- result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
- "node template type may not be null"));
- }
-
- if (properties != null) {
- result.append(validateProperties(new PfValidationResult()));
- }
-
- if (requirements != null) {
- result.append(requirements.validate(result));
- }
-
- if (capabilities != null) {
- result.append(validateProperties(capabilities.validate(result)));
- }
-
- return result;
- }
-
- /**
- * Validate the properties.
- *
- * @param resultIn The result of validations up to now
- * @return the validation result
- */
- private PfValidationResult validateProperties(final PfValidationResult resultIn) {
- PfValidationResult result = resultIn;
-
- for (String property : properties.values()) {
- if (property == null) {
- result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
- "topology template property may not be null "));
- }
- }
- return result;
- }
-
@Override
public int compareTo(final PfConcept otherConcept) {
- if (otherConcept == null) {
- return -1;
- }
if (this == otherConcept) {
return 0;
}
- if (getClass() != otherConcept.getClass()) {
- return getClass().getName().compareTo(otherConcept.getClass().getName());
- }
- final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
- int result = super.compareTo(other);
+ int result = super.compareTo(otherConcept);
if (result != 0) {
return result;
}
- result = type.compareTo(other.type);
- if (result != 0) {
- return result;
- }
-
- result = PfUtils.compareMaps(properties, other.properties);
- if (result != 0) {
- return result;
- }
+ final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
result = ObjectUtils.compare(requirements, other.requirements);
if (result != 0) {