e8d16a7051eb89aa01b9a3af508d470c07a87538
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogical.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import javax.persistence.Column;
25 import lombok.EqualsAndHashCode;
26 import lombok.Getter;
27 import lombok.NonNull;
28 import lombok.ToString;
29 import org.apache.commons.lang3.ObjectUtils;
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
31
32 /**
33  * This class represents a logical TOSCA constraint: =,>,>=,<,<=.
34  */
35 @EqualsAndHashCode(callSuper = false)
36 @ToString
37 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
38     private static final long serialVersionUID = -2730203215911880756L;
39
40     @Column
41     @Getter
42     private JpaToscaConstraintOperation operation;
43
44     @Column
45     @Getter
46     private String compareTo;
47
48     /**
49      * Constructor to set operation.
50      *
51      * @param operation the operation to set
52      * @param compareTo the string to compare to
53      */
54     public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
55             @NonNull final String compareTo) {
56         this.operation = operation;
57         this.compareTo = compareTo;
58     }
59
60     /**
61      * Authorative constructor.
62      *
63      * @param authorativeConcept the authorative concept to copy from
64      */
65     public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
66         /*
67          * The following will call invoke fromAuthorative() which will populate the class fields.
68          */
69         super(authorativeConcept);
70     }
71
72     @Override
73     public ToscaConstraint toAuthorative() {
74         var toscaConstraint = new ToscaConstraint();
75
76         if (operation == null) {
77             return null;
78         }
79
80         switch (operation) {
81             case EQ:
82                 toscaConstraint.setEqual(compareTo);
83                 break;
84
85             case GT:
86                 toscaConstraint.setGreaterThan(compareTo);
87                 break;
88
89             case GE:
90                 toscaConstraint.setGreaterOrEqual(compareTo);
91                 break;
92
93             case LT:
94                 toscaConstraint.setLessThan(compareTo);
95                 break;
96
97             case LE:
98                 toscaConstraint.setLessOrEqual(compareTo);
99                 break;
100
101             default:
102                 // Can't happen
103         }
104
105         return toscaConstraint;
106     }
107
108     @Override
109     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
110         // @formatter:off
111         if (toscaConstraint.getEqual() != null) {
112             operation = JpaToscaConstraintOperation.EQ;
113             compareTo = toscaConstraint.getEqual();
114         } else  if (toscaConstraint.getGreaterThan() != null) {
115             operation = JpaToscaConstraintOperation.GT;
116             compareTo = toscaConstraint.getGreaterThan();
117         } else  if (toscaConstraint.getGreaterOrEqual() != null) {
118             operation = JpaToscaConstraintOperation.GE;
119             compareTo = toscaConstraint.getGreaterOrEqual();
120         } else  if (toscaConstraint.getLessThan() != null) {
121             operation = JpaToscaConstraintOperation.LT;
122             compareTo = toscaConstraint.getLessThan();
123         } else  if (toscaConstraint.getLessOrEqual() != null) {
124             operation = JpaToscaConstraintOperation.LE;
125             compareTo = toscaConstraint.getLessOrEqual();
126         }
127         // @formatter:on
128     }
129
130     @Override
131     public int compareTo(JpaToscaConstraint otherConstraint) {
132         if (otherConstraint == null) {
133             return -1;
134         }
135         if (this == otherConstraint) {
136             return 0;
137         }
138         if (getClass() != otherConstraint.getClass()) {
139             return getClass().getName().compareTo(otherConstraint.getClass().getName());
140         }
141
142         final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
143
144         int result = ObjectUtils.compare(operation, other.operation);
145         if (result != 0) {
146             return result;
147         }
148
149         return ObjectUtils.compare(compareTo, other.compareTo);
150     }
151 }