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 java.util.List;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.Inheritance;
28 import javax.persistence.InheritanceType;
29 import javax.persistence.Table;
30 import javax.ws.rs.core.Response;
32 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import lombok.ToString;
37 import org.onap.policy.models.base.PfConcept;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfKey;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.base.PfReferenceKey;
42 import org.onap.policy.models.base.PfValidationMessage;
43 import org.onap.policy.models.base.PfValidationResult;
44 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
47 * This immutable class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner
48 * of an instance of the class to the referenced key.
51 @Table(name = "ToscaConstraintLogicalKey")
52 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
53 @EqualsAndHashCode(callSuper = false)
55 public final class JpaToscaConstraintLogicalKey extends JpaToscaConstraintLogical {
56 private static final long serialVersionUID = -2420828090326264341L;
61 private final PfKey compareToKey;
64 * The Default Constructor creates a {@link JpaToscaConstraintLogicalKey} object with a null key.
66 public JpaToscaConstraintLogicalKey() {
67 this(new PfReferenceKey());
71 * The Key Constructor creates a {@link JpaToscaConstraintLogicalKey} object with the given concept
74 * @param key the key of the constraint
76 public JpaToscaConstraintLogicalKey(final PfReferenceKey key) {
77 this(key, Operation.EQ, PfConceptKey.getNullKey());
81 * The Key Constructor creates a {@link JpaToscaConstraintLogicalKey} object with the given concept
82 * key, operation, and compare key.
84 * @param key the key of the constraint
85 * @param operation the logical operation of the constraint
86 * @param compareToKey the key of the object to which the object that owns this constraint will
89 public JpaToscaConstraintLogicalKey(final PfReferenceKey key, @NonNull final Operation operation,
90 @NonNull final PfKey compareToKey) {
91 super(key, operation);
92 this.compareToKey = compareToKey;
98 * @param copyConcept the concept to copy from
100 public JpaToscaConstraintLogicalKey(@NonNull final JpaToscaConstraintLogical copyConcept) {
101 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
105 public List<PfKey> getKeys() {
106 final List<PfKey> keyList = super.getKeys();
107 keyList.addAll(compareToKey.getKeys());
112 public void clean() {
114 compareToKey.clean();
118 public PfValidationResult validate(final PfValidationResult resultIn) {
119 PfValidationResult result = super.validate(resultIn);
121 if (compareToKey.isNullKey()) {
122 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
123 "comparison key is a null key"));
130 public int compareTo(final PfConcept otherConcept) {
131 if (otherConcept == null) {
134 if (this == otherConcept) {
137 if (getClass() != otherConcept.getClass()) {
138 return this.hashCode() - otherConcept.hashCode();
141 final JpaToscaConstraintLogicalKey other = (JpaToscaConstraintLogicalKey) otherConcept;
143 int result = super.compareTo(other);
148 return compareToKey.compareTo(other.compareToKey);
152 public PfConcept copyTo(@NonNull final PfConcept target) {
153 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");