Add support for legacy guard policies
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.List;
27
28 import javax.persistence.EmbeddedId;
29 import javax.ws.rs.core.Response;
30
31 import lombok.Data;
32 import lombok.EqualsAndHashCode;
33 import lombok.NonNull;
34
35 import org.onap.policy.models.base.PfConcept;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.PfValidationMessage;
40 import org.onap.policy.models.base.PfValidationResult;
41 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
42
43 /**
44  * Immutable class to represent the Constraint of property in TOSCA definition.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 @Data
50 @EqualsAndHashCode(callSuper = false)
51 public abstract class JpaToscaConstraint extends PfConcept {
52     private static final long serialVersionUID = 6426438089914347734L;
53
54     @EmbeddedId
55     private final PfReferenceKey key;
56
57     /**
58      * The Default Constructor creates a {@link JpaToscaConstraint} object with a null key.
59      */
60     public JpaToscaConstraint() {
61         this(new PfReferenceKey());
62     }
63
64     /**
65      * The Key Constructor creates a {@link JpaToscaConstraint} object with the given concept key.
66      *
67      * @param key the key
68      */
69     public JpaToscaConstraint(@NonNull final PfReferenceKey key) {
70         this.key = key;
71     }
72
73     /**
74      * Copy constructor.
75      *
76      * @param copyConcept the concept to copy from
77      */
78     public JpaToscaConstraint(@NonNull final JpaToscaConstraint copyConcept) {
79         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
80     }
81
82     @Override
83     public List<PfKey> getKeys() {
84         return getKey().getKeys();
85     }
86
87     @Override
88     public void clean() {
89         key.clean();
90     }
91
92     @Override
93     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
94         PfValidationResult result = resultIn;
95
96         if (key.isNullKey()) {
97             result.addValidationMessage(
98                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
99         }
100
101         return key.validate(result);
102     }
103
104     @Override
105     public int compareTo(final PfConcept otherConcept) {
106         if (otherConcept == null) {
107             return -1;
108         }
109         if (this == otherConcept) {
110             return 0;
111         }
112         if (getClass() != otherConcept.getClass()) {
113             return this.hashCode() - otherConcept.hashCode();
114         }
115
116         final JpaToscaConstraint other = (JpaToscaConstraint) otherConcept;
117
118         return key.compareTo(other.key);
119     }
120
121     @Override
122     public PfConcept copyTo(@NonNull final PfConcept target) {
123         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
124     }
125 }