/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019, 2023 Nordix Foundation.
+ * Modifications Copyright (C) 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 javax.persistence.Column;
-import javax.ws.rs.core.Response;
-
+import jakarta.persistence.Column;
+import java.io.Serial;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
-
-import org.onap.policy.models.base.PfConcept;
-import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.base.PfReferenceKey;
+import org.apache.commons.lang3.ObjectUtils;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
/**
* This class represents a logical TOSCA constraint: =,>,>=,<,<=.
@EqualsAndHashCode(callSuper = false)
@ToString
public class JpaToscaConstraintLogical extends JpaToscaConstraint {
- private static final long serialVersionUID = 2562306457768745444L;
-
- public enum Operation {
- EQ,
- GT,
- GE,
- LT,
- LE
- }
+ @Serial
+ private static final long serialVersionUID = -2730203215911880756L;
@Column
- @NonNull
@Getter
- private final Operation operation;
+ private JpaToscaConstraintOperation operation;
- /**
- * The Default Constructor creates a {@link JpaToscaConstraintLogical} object with a null key.
- */
- public JpaToscaConstraintLogical() {
- this(new PfReferenceKey());
- }
+ @Column
+ @Getter
+ private String compareTo;
/**
- * The Key Constructor creates a {@link JpaToscaConstraintLogical} object with the given concept key.
+ * Constructor to set operation.
*
- * @param key the key of the constraint
+ * @param operation the operation to set
+ * @param compareTo the string to compare to
*/
- public JpaToscaConstraintLogical(final PfReferenceKey key) {
- this(key, Operation.EQ);
+ public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
+ @NonNull final String compareTo) {
+ this.operation = operation;
+ this.compareTo = compareTo;
}
/**
- * The Key Constructor creates a {@link JpaToscaConstraintLogical} object with the given concept key and operation.
- *
- * @param key the key of the constraint
- * @param operation the logical operation of the constraint
+ * Authorative constructor.
*
+ * @param authorativeConcept the authorative concept to copy from
*/
- public JpaToscaConstraintLogical(final PfReferenceKey key, @NonNull final Operation operation) {
- super(key);
- this.operation = operation;
+ public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
+ /*
+ * The following will call invoke fromAuthorative() which will populate the class fields.
+ */
+ super(authorativeConcept);
}
- /**
- * Copy constructor.
- *
- * @param copyConcept the concept to copy from
- */
- public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintLogical copyConcept) {
- throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
+ @Override
+ public ToscaConstraint toAuthorative() {
+ var toscaConstraint = new ToscaConstraint();
+
+ if (operation == null) {
+ return null;
+ }
+
+ switch (operation) {
+ case EQ -> toscaConstraint.setEqual(compareTo);
+ case GT -> toscaConstraint.setGreaterThan(compareTo);
+ case GE -> toscaConstraint.setGreaterOrEqual(compareTo);
+ case LT -> toscaConstraint.setLessThan(compareTo);
+ case LE -> toscaConstraint.setLessOrEqual(compareTo);
+ default -> {
+ return null;
+ }
+ }
+
+ return toscaConstraint;
}
@Override
- public int compareTo(final PfConcept otherConcept) {
- if (otherConcept == null) {
- return -1;
+ public void fromAuthorative(final ToscaConstraint toscaConstraint) {
+ // @formatter:off
+ if (toscaConstraint.getEqual() != null) {
+ operation = JpaToscaConstraintOperation.EQ;
+ compareTo = toscaConstraint.getEqual();
+ } else if (toscaConstraint.getGreaterThan() != null) {
+ operation = JpaToscaConstraintOperation.GT;
+ compareTo = toscaConstraint.getGreaterThan();
+ } else if (toscaConstraint.getGreaterOrEqual() != null) {
+ operation = JpaToscaConstraintOperation.GE;
+ compareTo = toscaConstraint.getGreaterOrEqual();
+ } else if (toscaConstraint.getLessThan() != null) {
+ operation = JpaToscaConstraintOperation.LT;
+ compareTo = toscaConstraint.getLessThan();
+ } else if (toscaConstraint.getLessOrEqual() != null) {
+ operation = JpaToscaConstraintOperation.LE;
+ compareTo = toscaConstraint.getLessOrEqual();
}
- if (this == otherConcept) {
+ // @formatter:on
+ }
+
+ @Override
+ public int compareTo(@NonNull JpaToscaConstraint otherConstraint) {
+ if (this == otherConstraint) {
return 0;
}
- if (getClass() != otherConcept.getClass()) {
- return this.hashCode() - otherConcept.hashCode();
+ if (getClass() != otherConstraint.getClass()) {
+ return getClass().getName().compareTo(otherConstraint.getClass().getName());
}
- final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConcept;
+ final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
- int result = super.compareTo(other);
+ int result = ObjectUtils.compare(operation, other.operation);
if (result != 0) {
return result;
}
- return operation.compareTo(other.operation);
- }
-
- @Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
+ return ObjectUtils.compare(compareTo, other.compareTo);
}
}