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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.concepts;
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;
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
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.
62 @Table(name = "AxLogic")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
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" })
70 public class AxLogic extends AxConcept {
71 private static final long serialVersionUID = -4260562004005697328L;
73 private static final String WHITESPACE_REGEXP = "\\s+$";
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";
80 /** Regular expression that specifies the allowed characters in logic flavour tokens. */
81 public static final String LOGIC_FLAVOUR_REGEXP = "[A-Za-z0-9\\-_]+";
83 /** When logic flavour is undefined, it has this value. */
84 public static final String LOGIC_FLAVOUR_UNDEFINED = "UNDEFINED";
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
90 @XmlElement(name = "key", required = true)
91 @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
92 private AxReferenceKey key;
94 @Column(name = LOGIC_FLAVOUR_TOKEN)
95 @XmlElement(required = true)
96 private String logicFlavour;
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;
105 * The Default Constructor creates a logic instance with a null key, undefined logic flavour and a null logic
109 this(new AxReferenceKey());
110 logicFlavour = LOGIC_FLAVOUR_UNDEFINED;
116 * @param copyConcept the concept to copy from
118 public AxLogic(final AxLogic copyConcept) {
123 * The Key Constructor creates a logic instance with the given reference key, undefined logic flavour and a null
126 * @param key the reference key of the logic
128 public AxLogic(final AxReferenceKey key) {
129 this(key, LOGIC_FLAVOUR_UNDEFINED, "");
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.
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
141 public AxLogic(final AxReferenceKey parentKey, final String logicName, final String logicFlavour,
142 final String logic) {
143 this(new AxReferenceKey(parentKey, logicName), logicFlavour, logic);
147 * This Constructor creates a logic instance with the given reference key and all of its fields defined.
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
153 public AxLogic(final AxReferenceKey key, final String logicFlavour, final String logic) {
155 Assertions.argumentNotNull(key, KEY_NULL_MESSAGE);
156 Assertions.argumentNotNull(logicFlavour, LOGIC_FLAVOUR_NULL_MESSAGE);
157 Assertions.argumentNotNull(logic, LOGIC_NULL_MESSAGE);
160 this.logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
161 this.logic = logic.replaceAll(WHITESPACE_REGEXP, "");
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.
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
172 public AxLogic(final AxReferenceKey key, final String logicFlavour, final AxLogicReader logicReader) {
174 Assertions.argumentNotNull(key, KEY_NULL_MESSAGE);
175 Assertions.argumentNotNull(logicFlavour, LOGIC_FLAVOUR_NULL_MESSAGE);
176 Assertions.argumentNotNull(logicReader, "logicReader may not be null");
179 this.logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
180 logic = logicReader.readLogic(this);
187 public AxReferenceKey getKey() {
195 public List<AxKey> getKeys() {
196 return key.getKeys();
204 public void setKey(final AxReferenceKey key) {
205 Assertions.argumentNotNull(key, KEY_NULL_MESSAGE);
210 * Gets the logic flavour.
212 * @return the logic flavour
214 public String getLogicFlavour() {
219 * Sets the logic flavour.
221 * @param logicFlavour the logic flavour
223 public void setLogicFlavour(final String logicFlavour) {
224 this.logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
232 public String getLogic() {
239 * @param logic the logic
241 public void setLogic(final String logic) {
242 Assertions.argumentNotNull(logic, LOGIC_NULL_MESSAGE);
243 this.logic = logic.replaceAll(WHITESPACE_REGEXP, "");
250 public AxValidationResult validate(final AxValidationResult resultIn) {
251 AxValidationResult result = resultIn;
253 if (key.equals(AxReferenceKey.getNullKey())) {
254 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
255 "key is a null key"));
258 result = key.validate(result);
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"));
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));
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"));
285 public void clean() {
289 logicFlavour = Assertions.validateStringParameter(LOGIC_FLAVOUR_TOKEN, logicFlavour, LOGIC_FLAVOUR_REGEXP);
290 logic = logic.replaceAll(WHITESPACE_REGEXP, "");
297 public String toString() {
298 final StringBuilder builder = new StringBuilder();
299 builder.append(this.getClass().getSimpleName());
300 builder.append(":(");
301 builder.append("key=");
303 builder.append(",logicFlavour=");
304 builder.append(logicFlavour);
305 builder.append(",logic=");
306 builder.append(logic);
308 return builder.toString();
315 public AxConcept copyTo(final AxConcept targetObject) {
316 Assertions.argumentNotNull(targetObject, "target may not be null");
318 final Object copyObject = targetObject;
319 Assertions.instanceOf(copyObject, AxLogic.class);
321 final AxLogic copy = ((AxLogic) copyObject);
322 copy.setKey(new AxReferenceKey(key));
323 copy.setLogicFlavour(logicFlavour);
324 copy.setLogic(logic);
333 public int hashCode() {
334 final int prime = 31;
336 result = prime * result + key.hashCode();
337 result = prime * result + logicFlavour.hashCode();
338 result = prime * result + logic.hashCode();
346 public boolean equals(final Object obj) {
353 if (getClass() != obj.getClass()) {
357 final AxLogic other = (AxLogic) obj;
358 if (!key.equals(other.key)) {
361 if (!logicFlavour.equals(other.logicFlavour)) {
364 final String thislogic = CDataConditioner.clean(logic).replaceAll("\n", "");
365 final String otherlogic = CDataConditioner.clean(other.logic).replaceAll("\n", "");
366 return thislogic.equals(otherlogic);
373 public int compareTo(final AxConcept otherObj) {
374 if (otherObj == null) {
377 if (this == otherObj) {
380 if (getClass() != otherObj.getClass()) {
381 return this.hashCode() - otherObj.hashCode();
384 final AxLogic other = (AxLogic) otherObj;
385 if (!key.equals(other.key)) {
386 return key.compareTo(other.key);
388 if (!logicFlavour.equals(other.logicFlavour)) {
389 return logicFlavour.compareTo(other.logicFlavour);
391 return logic.compareTo(other.logic);