2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.Inheritance;
26 import javax.persistence.InheritanceType;
27 import javax.persistence.Table;
28 import javax.ws.rs.core.Response;
30 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
34 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
35 import org.onap.policy.models.base.PfConcept;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.base.PfValidationMessage;
39 import org.onap.policy.models.base.PfValidationResult;
40 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
43 * This class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner of an
44 * instance of the class to the given string.
47 @Table(name = "ToscaConstraintLogicalString")
48 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
49 @EqualsAndHashCode(callSuper = false)
50 public class JpaToscaConstraintLogicalString extends JpaToscaConstraintLogical {
51 private static final long serialVersionUID = 8167550632122339195L;
56 private final String compareToString;
59 * The Default Constructor creates a {@link JpaToscaConstraintLogicalString} object with a null key.
61 public JpaToscaConstraintLogicalString() {
62 this(new PfReferenceKey());
66 * The Key Constructor creates a {@link JpaToscaConstraintLogicalString} object with the given concept
69 * @param key the key of the constraint
71 public JpaToscaConstraintLogicalString(final PfReferenceKey key) {
72 this(key, Operation.EQ, "");
76 * The Key Constructor creates a {@link JpaToscaConstraintLogicalString} object with the given concept
77 * key, operation, and compare string.
79 * @param key the key of the constraint
80 * @param operation the logical operation of the constraint
81 * @param compareToString the key of the object to which the object that owns this constraint will
84 public JpaToscaConstraintLogicalString(final PfReferenceKey key, @NonNull final Operation operation,
85 @NonNull final String compareToString) {
86 super(key, operation);
87 this.compareToString = compareToString.trim();
93 * @param copyConcept the concept to copy from
95 public JpaToscaConstraintLogicalString(@NonNull final JpaToscaConstraintLogical copyConcept) {
96 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
100 public PfValidationResult validate(final PfValidationResult resultIn) {
101 PfValidationResult result = super.validate(resultIn);
103 if (!ParameterValidationUtils.validateStringParameter(compareToString)) {
104 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
105 "comparison string is null or blank"));
112 public int compareTo(final PfConcept otherConcept) {
113 if (otherConcept == null) {
116 if (this == otherConcept) {
119 if (getClass() != otherConcept.getClass()) {
120 return this.hashCode() - otherConcept.hashCode();
123 final JpaToscaConstraintLogicalString other = (JpaToscaConstraintLogicalString) otherConcept;
125 int result = super.compareTo(other);
130 return compareToString.compareTo(other.compareToString);
134 public PfConcept copyTo(@NonNull final PfConcept target) {
135 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");