Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / concepts / AxLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
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.apex.model.policymodel.concepts;
23
24 import java.util.List;
25 import javax.persistence.Column;
26 import javax.persistence.Convert;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.Table;
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlRootElement;
36 import javax.xml.bind.annotation.XmlType;
37 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
44 import org.onap.policy.apex.model.basicmodel.dao.converters.CDataConditioner;
45 import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter;
46 import org.onap.policy.common.utils.validation.Assertions;
47
48 /**
49  * This class holds Logic for executing a task or task selection on an Apex policy state. The flavour of the logic
50  * describes the type of logic being used and it may be a language identifier such as "Javascript" or "Jython". The
51  * logic itself is held as a string. The {@link AxLogic} instance is used by the Apex engine to start an executor with
52  * the required flavour. Once the executor is started, the Apex engine passes the logic to the executor and the executor
53  * executes it. In the Apex engine, executors are deployed as plugins. Apex also provides the executor with run-time
54  * context, which makes context such as input fields, output fields, and context albums available to the task at
55  * runtime.
56  *
57  * <p>Validation checks that the logic key is valid, that the logic flavour is defined and is valid when checked against
58  * the {@code LOGIC_FLAVOUR_REGEXP} regular expression, and that the specified logic string is not null or blank.
59  */
60
61 @Entity
62 @Table(name = "AxLogic")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlRootElement(name = "apexLogic", namespace = "http://www.onap.org/policy/apex-pdp")
67 @XmlType(name = "AxLogic", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
68     { "key", "logicFlavour", "logic" })
69
70 public class AxLogic extends AxConcept {
71     private static final long serialVersionUID = -4260562004005697328L;
72
73     private static final String WHITESPACE_REGEXP = "\\s+$";
74
75     private static final String LOGIC_FLAVOUR_TOKEN = "logicFlavour";
76     private static final String KEY_NULL_MESSAGE = "key may not be null";
77     private static final String LOGIC_FLAVOUR_NULL_MESSAGE = "logicFlavour may not be null";
78     private static final String LOGIC_NULL_MESSAGE = "logic may not be null";
79
80     /** Regular expression that specifies the allowed characters in logic flavour tokens. */
81     public static final String LOGIC_FLAVOUR_REGEXP = "[A-Za-z0-9\\-_]+";
82
83     /** When logic flavour is undefined, it has this value. */
84     public static final String LOGIC_FLAVOUR_UNDEFINED = "UNDEFINED";
85
86     /** The maximum permissible size of a logic definition. */
87     public static final int MAX_LOGIC_SIZE = 32672; // The maximum size supported by Apache Derby
88
89     @EmbeddedId()
90     @XmlElement(name = "key", required = true)
91     @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
92     private AxReferenceKey key;
93
94     @Column(name = LOGIC_FLAVOUR_TOKEN)
95     @XmlElement(required = true)
96     private String logicFlavour;
97
98     @Column(name = "logic", length = MAX_LOGIC_SIZE)
99     @Convert(converter = CDataConditioner.class)
100     @XmlJavaTypeAdapter(value = CDataConditioner.class)
101     @XmlElement(required = true)
102     private String logic;
103
104     /**
105      * The Default Constructor creates a logic instance with a null key, undefined logic flavour and a null logic
106      * string.
107      */
108     public AxLogic() {
109         this(new AxReferenceKey());
110         logicFlavour = LOGIC_FLAVOUR_UNDEFINED;
111     }
112
113     /**
114      * Copy constructor.
115      *
116      * @param copyConcept the concept to copy from
117      */
118     public AxLogic(final AxLogic copyConcept) {
119         super(copyConcept);
120     }
121
122     /**
123      * The Key Constructor creates a logic instance with the given reference key, undefined logic flavour and a null
124      * logic string.
125      *
126      * @param key the reference key of the logic
127      */
128     public AxLogic(final AxReferenceKey key) {
129         this(key, LOGIC_FLAVOUR_UNDEFINED, "");
130     }
131
132     /**
133      * This Constructor creates a logic instance with a reference key constructed from the parents key and the logic
134      * local name and all of its fields defined.
135      *
136      * @param parentKey the reference key of the parent of this logic
137      * @param logicName the logic name, held as the local name of the reference key of this logic
138      * @param logicFlavour the flavour of this logic
139      * @param logic the actual logic as a string
140      */
141     public AxLogic(final AxReferenceKey parentKey, final String logicName, final String logicFlavour,
142                     final String logic) {
143         this(new AxReferenceKey(parentKey, logicName), logicFlavour, logic);
144     }
145
146     /**
147      * This Constructor creates a logic instance with the given reference key and all of its fields defined.
148      *
149      * @param key the reference key of this logic
150      * @param logicFlavour the flavour of this logic
151      * @param logic the actual logic as a string
152      */
153     public AxLogic(final AxReferenceKey key, final String logicFlavour, final String logic) {
154         super();
155         Assertions.argumentNotNull(key, KEY_NULL_MESSAGE);
156         Assertions.argumentNotNull(logicFlavour, LOGIC_FLAVOUR_NULL_MESSAGE);
157         Assertions.argumentNotNull(logic, LOGIC_NULL_MESSAGE);
158
159         this.key = key;
160         this.logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
161         this.logic = logic.replaceAll(WHITESPACE_REGEXP, "");
162     }
163
164     /**
165      * This Constructor creates a logic instance with the given reference key and logic flavour, the logic is provided
166      * by the given logic reader instance.
167      *
168      * @param key the reference key of this logic
169      * @param logicFlavour the flavour of this logic
170      * @param logicReader the logic reader to use to read the logic for this logic instance
171      */
172     public AxLogic(final AxReferenceKey key, final String logicFlavour, final AxLogicReader logicReader) {
173         super();
174         Assertions.argumentNotNull(key, KEY_NULL_MESSAGE);
175         Assertions.argumentNotNull(logicFlavour, LOGIC_FLAVOUR_NULL_MESSAGE);
176         Assertions.argumentNotNull(logicReader, "logicReader may not be null");
177
178         this.key = key;
179         this.logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
180         logic = logicReader.readLogic(this);
181     }
182
183     /**
184      * {@inheritDoc}.
185      */
186     @Override
187     public AxReferenceKey getKey() {
188         return key;
189     }
190
191     /**
192      * {@inheritDoc}.
193      */
194     @Override
195     public List<AxKey> getKeys() {
196         return key.getKeys();
197     }
198
199     /**
200      * Sets the key.
201      *
202      * @param key the key
203      */
204     public void setKey(final AxReferenceKey key) {
205         Assertions.argumentNotNull(key, KEY_NULL_MESSAGE);
206         this.key = key;
207     }
208
209     /**
210      * Gets the logic flavour.
211      *
212      * @return the logic flavour
213      */
214     public String getLogicFlavour() {
215         return logicFlavour;
216     }
217
218     /**
219      * Sets the logic flavour.
220      *
221      * @param logicFlavour the logic flavour
222      */
223     public void setLogicFlavour(final String logicFlavour) {
224         this.logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
225     }
226
227     /**
228      * Gets the logic.
229      *
230      * @return the logic
231      */
232     public String getLogic() {
233         return logic;
234     }
235
236     /**
237      * Sets the logic.
238      *
239      * @param logic the logic
240      */
241     public void setLogic(final String logic) {
242         Assertions.argumentNotNull(logic, LOGIC_NULL_MESSAGE);
243         this.logic = logic.replaceAll(WHITESPACE_REGEXP, "");
244     }
245
246     /**
247      * {@inheritDoc}.
248      */
249     @Override
250     public AxValidationResult validate(final AxValidationResult resultIn) {
251         AxValidationResult result = resultIn;
252
253         if (key.equals(AxReferenceKey.getNullKey())) {
254             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
255                             "key is a null key"));
256         }
257
258         result = key.validate(result);
259
260         if (logicFlavour.replaceAll(WHITESPACE_REGEXP, "").length() == 0
261                         || logicFlavour.equals(LOGIC_FLAVOUR_UNDEFINED)) {
262             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
263                             "logic flavour is not defined"));
264         }
265
266         String flavourValidationString = Assertions.getStringParameterValidationMessage(LOGIC_FLAVOUR_TOKEN,
267                         logicFlavour, LOGIC_FLAVOUR_REGEXP);
268         if (flavourValidationString != null) {
269             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
270                             "logic flavour invalid-" + flavourValidationString));
271         }
272
273         if (logic.replaceAll(WHITESPACE_REGEXP, "").length() == 0) {
274             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
275                             "no logic specified, logic may not be blank"));
276         }
277
278         return result;
279     }
280
281     /**
282      * {@inheritDoc}.
283      */
284     @Override
285     public void clean() {
286         if (key != null) {
287             key.clean();
288         }
289         logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
290         logic = logic.replaceAll(WHITESPACE_REGEXP, "");
291     }
292
293     /**
294      * {@inheritDoc}.
295      */
296     @Override
297     public String toString() {
298         final StringBuilder builder = new StringBuilder();
299         builder.append(this.getClass().getSimpleName());
300         builder.append(":(");
301         builder.append("key=");
302         builder.append(key);
303         builder.append(",logicFlavour=");
304         builder.append(logicFlavour);
305         builder.append(",logic=");
306         builder.append(logic);
307         builder.append(")");
308         return builder.toString();
309     }
310
311     /**
312      * {@inheritDoc}.
313      */
314     @Override
315     public AxConcept copyTo(final AxConcept targetObject) {
316         Assertions.argumentNotNull(targetObject, "target may not be null");
317
318         final Object copyObject = targetObject;
319         Assertions.instanceOf(copyObject, AxLogic.class);
320
321         final AxLogic copy = ((AxLogic) copyObject);
322         copy.setKey(new AxReferenceKey(key));
323         copy.setLogicFlavour(logicFlavour);
324         copy.setLogic(logic);
325
326         return copy;
327     }
328
329     /**
330      * {@inheritDoc}.
331      */
332     @Override
333     public int hashCode() {
334         final int prime = 31;
335         int result = 1;
336         result = prime * result + key.hashCode();
337         result = prime * result + logicFlavour.hashCode();
338         result = prime * result + logic.hashCode();
339         return result;
340     }
341
342     /**
343      * {@inheritDoc}.
344      */
345     @Override
346     public boolean equals(final Object obj) {
347         if (obj == null) {
348             return false;
349         }
350         if (this == obj) {
351             return true;
352         }
353         if (getClass() != obj.getClass()) {
354             return false;
355         }
356
357         final AxLogic other = (AxLogic) obj;
358         if (!key.equals(other.key)) {
359             return false;
360         }
361         if (!logicFlavour.equals(other.logicFlavour)) {
362             return false;
363         }
364         final String thislogic = CDataConditioner.clean(logic).replaceAll("\n", "");
365         final String otherlogic = CDataConditioner.clean(other.logic).replaceAll("\n", "");
366         return thislogic.equals(otherlogic);
367     }
368
369     /**
370      * {@inheritDoc}.
371      */
372     @Override
373     public int compareTo(final AxConcept otherObj) {
374         if (otherObj == null) {
375             return -1;
376         }
377         if (this == otherObj) {
378             return 0;
379         }
380         if (getClass() != otherObj.getClass()) {
381             return this.hashCode() - otherObj.hashCode();
382         }
383
384         final AxLogic other = (AxLogic) otherObj;
385         if (!key.equals(other.key)) {
386             return key.compareTo(other.key);
387         }
388         if (!logicFlavour.equals(other.logicFlavour)) {
389             return logicFlavour.compareTo(other.logicFlavour);
390         }
391         return logic.compareTo(other.logic);
392     }
393 }