Restructure for authorative models
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogicalKey.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.List;
24
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;
31
32 import lombok.EqualsAndHashCode;
33 import lombok.Getter;
34 import lombok.NonNull;
35 import lombok.ToString;
36
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;
45
46 /**
47  * This immutable class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner
48  * of an instance of the class to the referenced key.
49  */
50 @Entity
51 @Table(name = "ToscaConstraintLogicalKey")
52 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
53 @EqualsAndHashCode(callSuper = false)
54 @ToString
55 public final class JpaToscaConstraintLogicalKey extends JpaToscaConstraintLogical {
56     private static final long serialVersionUID = -2420828090326264341L;
57
58     @Column
59     @NonNull
60     @Getter
61     private final PfKey compareToKey;
62
63     /**
64      * The Default Constructor creates a {@link JpaToscaConstraintLogicalKey} object with a null key.
65      */
66     public JpaToscaConstraintLogicalKey() {
67         this(new PfReferenceKey());
68     }
69
70     /**
71      * The Key Constructor creates a {@link JpaToscaConstraintLogicalKey} object with the given concept
72      * key.
73      *
74      * @param key the key of the constraint
75      */
76     public JpaToscaConstraintLogicalKey(final PfReferenceKey key) {
77         this(key, Operation.EQ, PfConceptKey.getNullKey());
78     }
79
80     /**
81      * The Key Constructor creates a {@link JpaToscaConstraintLogicalKey} object with the given concept
82      * key, operation, and compare key.
83      *
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
87      *        be compared
88      */
89     public JpaToscaConstraintLogicalKey(final PfReferenceKey key, @NonNull final Operation operation,
90             @NonNull final PfKey compareToKey) {
91         super(key, operation);
92         this.compareToKey = compareToKey;
93     }
94
95     /**
96      * Copy constructor.
97      *
98      * @param copyConcept the concept to copy from
99      */
100     public JpaToscaConstraintLogicalKey(@NonNull final JpaToscaConstraintLogical copyConcept) {
101         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
102     }
103
104     @Override
105     public List<PfKey> getKeys() {
106         final List<PfKey> keyList = super.getKeys();
107         keyList.addAll(compareToKey.getKeys());
108         return keyList;
109     }
110
111     @Override
112     public void clean() {
113         super.clean();
114         compareToKey.clean();
115     }
116
117     @Override
118     public PfValidationResult validate(final PfValidationResult resultIn) {
119         PfValidationResult result = super.validate(resultIn);
120
121         if (compareToKey.isNullKey()) {
122             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
123                     "comparison key is a null key"));
124         }
125
126         return result;
127     }
128
129     @Override
130     public int compareTo(final PfConcept otherConcept) {
131         if (otherConcept == null) {
132             return -1;
133         }
134         if (this == otherConcept) {
135             return 0;
136         }
137         if (getClass() != otherConcept.getClass()) {
138             return this.hashCode() - otherConcept.hashCode();
139         }
140
141         final JpaToscaConstraintLogicalKey other = (JpaToscaConstraintLogicalKey) otherConcept;
142
143         int result = super.compareTo(other);
144         if (result != 0) {
145             return result;
146         }
147
148         return compareToKey.compareTo(other.compareToKey);
149     }
150
151     @Override
152     public PfConcept copyTo(@NonNull final PfConcept target) {
153         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
154     }
155 }