Add support for legacy guard policies
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogicalString.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 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;
29
30 import lombok.EqualsAndHashCode;
31 import lombok.Getter;
32 import lombok.NonNull;
33
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;
41
42 /**
43  * This class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner of an
44  * instance of the class to the given string.
45  */
46 @Entity
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;
52
53     @Column
54     @NonNull
55     @Getter
56     private final String compareToString;
57
58     /**
59      * The Default Constructor creates a {@link JpaToscaConstraintLogicalString} object with a null key.
60      */
61     public JpaToscaConstraintLogicalString() {
62         this(new PfReferenceKey());
63     }
64
65     /**
66      * The Key Constructor creates a {@link JpaToscaConstraintLogicalString} object with the given concept
67      * key.
68      *
69      * @param key the key of the constraint
70      */
71     public JpaToscaConstraintLogicalString(final PfReferenceKey key) {
72         this(key, Operation.EQ, "");
73     }
74
75     /**
76      * The Key Constructor creates a {@link JpaToscaConstraintLogicalString} object with the given concept
77      * key, operation, and compare string.
78      *
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
82      *        be compared
83      */
84     public JpaToscaConstraintLogicalString(final PfReferenceKey key, @NonNull final Operation operation,
85             @NonNull final String compareToString) {
86         super(key, operation);
87         this.compareToString = compareToString.trim();
88     }
89
90     /**
91      * Copy constructor.
92      *
93      * @param copyConcept the concept to copy from
94      */
95     public JpaToscaConstraintLogicalString(@NonNull final JpaToscaConstraintLogical copyConcept) {
96         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
97     }
98
99     @Override
100     public PfValidationResult validate(final PfValidationResult resultIn) {
101         PfValidationResult result = super.validate(resultIn);
102
103         if (!ParameterValidationUtils.validateStringParameter(compareToString)) {
104             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
105                     "comparison string is null or blank"));
106         }
107
108         return result;
109     }
110
111     @Override
112     public int compareTo(final PfConcept otherConcept) {
113         if (otherConcept == null) {
114             return -1;
115         }
116         if (this == otherConcept) {
117             return 0;
118         }
119         if (getClass() != otherConcept.getClass()) {
120             return this.hashCode() - otherConcept.hashCode();
121         }
122
123         final JpaToscaConstraintLogicalString other = (JpaToscaConstraintLogicalString) otherConcept;
124
125         int result = super.compareTo(other);
126         if (result != 0) {
127             return result;
128         }
129
130         return compareToString.compareTo(other.compareToString);
131     }
132
133     @Override
134     public PfConcept copyTo(@NonNull final PfConcept target) {
135         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
136     }
137 }