X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Fsimple%2Fconcepts%2FJpaToscaConstraintLogical.java;h=e8d16a7051eb89aa01b9a3af508d470c07a87538;hb=d08d7fea4cf9400b9ba258768a3e2d082d849df2;hp=231e26188fe3739433db0cebbba20e9b49994013;hpb=cc5b96bfd33cd7d91fe6994d348e8d6a0ebb54fa;p=policy%2Fmodels.git diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java index 231e26188..e8d16a705 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 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. @@ -21,16 +22,12 @@ package org.onap.policy.models.tosca.simple.concepts; import javax.persistence.Column; -import javax.ws.rs.core.Response; - 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: =,>,>=,<,<=. @@ -38,82 +35,117 @@ import org.onap.policy.models.base.PfReferenceKey; @EqualsAndHashCode(callSuper = false) @ToString public class JpaToscaConstraintLogical extends JpaToscaConstraint { - private static final long serialVersionUID = 2562306457768745444L; - - public enum Operation { - EQ, - GT, - GE, - LT, - LE - } + 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); + break; + + case GT: + toscaConstraint.setGreaterThan(compareTo); + break; + + case GE: + toscaConstraint.setGreaterOrEqual(compareTo); + break; + + case LT: + toscaConstraint.setLessThan(compareTo); + break; + + case LE: + toscaConstraint.setLessOrEqual(compareTo); + break; + + default: + // Can't happen + } + + return toscaConstraint; + } + + @Override + 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(); + } + // @formatter:on } @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { + public int compareTo(JpaToscaConstraint otherConstraint) { + if (otherConstraint == null) { return -1; } - if (this == otherConcept) { + 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); } }