summaryrefslogtreecommitdiffstats
path: root/core/core-engine
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-11-11 11:13:34 +0000
committerliamfallon <liam.fallon@est.tech>2020-11-11 14:43:13 +0000
commit90bc246ffec30f7d97474d772d8bad73715cdd45 (patch)
treed2bac99b05d55dcb9a55c01bb40cb32d1a07a458 /core/core-engine
parentf76d02a321664e458ebe5bb8a825492e6fd0d986 (diff)
Add method to allow JSON conversion to execution context for logic
The Rhino JSON.Stringify() method does not work well. This patch allows the user to call schema specific or generic JSON conversion using the schema handling in Apex or not as the developer chooses. Added a single argument stringify2Json() method. Issue-ID: POLICY-2463 Change-Id: I9ec1e0dea2f6cd153b73b2eace63d8806674884d Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'core/core-engine')
-rw-r--r--core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/AbstractExecutionContext.java97
-rw-r--r--core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java16
-rw-r--r--core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java31
-rw-r--r--core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java27
4 files changed, 108 insertions, 63 deletions
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/AbstractExecutionContext.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/AbstractExecutionContext.java
new file mode 100644
index 000000000..a47ccaa48
--- /dev/null
+++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/AbstractExecutionContext.java
@@ -0,0 +1,97 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.core.engine.executor.context;
+
+import java.util.Properties;
+import lombok.Getter;
+import lombok.Setter;
+import org.onap.policy.apex.context.SchemaHelper;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+
+/**
+ * Abstract class for the execution context for logic executions in logic being executed in an Apex engine. The
+ * logic must have easy access to its subject definition, the incoming and outgoing field contexts, as well as the
+ * policy, global, and external context.
+ */
+@Getter
+public class AbstractExecutionContext {
+ /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
+ public final Boolean isTrue = true;
+
+ /**
+ * A constant <code>boolean false</code> value available for reuse e.g., for the return value
+ */
+ public final Boolean isFalse = false;
+
+ /** the execution ID for the current APEX policy execution instance. */
+ public final Long executionId;
+
+ // Standard coder for JSON converts
+ private static final StandardCoder STANDARD_CODER = new StandardCoder();
+
+ // A message specified in the logic
+ @Setter
+ private String message;
+
+ // Execution properties for a policy execution
+ private final Properties executionProperties;
+
+ /**
+ * Instantiates a new task execution context.
+ *
+ * @param executionId the execution ID for the current APEX policy execution instance
+ * @param executionProperties the execution properties for task execution
+ */
+ public AbstractExecutionContext(final long executionId, final Properties executionProperties) {
+
+ // Execution ID is the current policy execution instance
+ this.executionId = executionId;
+ this.executionProperties = executionProperties;
+ }
+
+ /**
+ * Get a JSON representation of an object.
+ *
+ * @param theObject the object to get a JSON representation of
+ * @return the JSON version of the object
+ * @throws CoderException on JSON coding errors
+ */
+ public String stringify2Json(final Object theObject) throws CoderException {
+ return stringify2Json(theObject, null);
+ }
+
+ /**
+ * Get a JSON representation of an object.
+ *
+ * @param theObject the object to get a JSON representation of
+ * @param schemaHelper a schema helper to use for the JSON conversion, if null, a standard conversion is done
+ * @return the JSON version of the object
+ * @throws CoderException on JSON coding errors
+ */
+ public String stringify2Json(final Object theObject, final SchemaHelper schemaHelper) throws CoderException {
+ if (schemaHelper == null) {
+ return STANDARD_CODER.encode(theObject);
+ } else {
+ return schemaHelper.marshal2String(theObject);
+ }
+ }
+}
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java
index 55d629d84..3bc83b970 100644
--- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java
+++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java
@@ -48,7 +48,7 @@ import org.slf4j.ext.XLoggerFactory;
* @author Sven van der Meer (sven.van.der.meer@ericsson.com)
*/
@Getter
-public class StateFinalizerExecutionContext {
+public class StateFinalizerExecutionContext extends AbstractExecutionContext {
/**
* Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
*/
@@ -60,9 +60,6 @@ public class StateFinalizerExecutionContext {
/** A facade to the full state definition for the state finalizer logic being executed. */
public final AxStateFacade subject;
- /** the execution ID for the current APEX policy execution instance. */
- public final Long executionId;
-
/**
* The list of state outputs for this state finalizer. The purpose of a state finalizer is to select a state output
* for a state from this list of state output names.
@@ -92,11 +89,6 @@ public class StateFinalizerExecutionContext {
// All available context albums
private final Map<String, ContextAlbum> context;
- // A message specified in the logic
- @Getter
- @Setter
- private String message;
-
// Execution properties for a policy execution
@Getter
private Properties executionProperties;
@@ -115,11 +107,9 @@ public class StateFinalizerExecutionContext {
public StateFinalizerExecutionContext(final StateFinalizerExecutor stateFinalizerExecutor, final long executionId,
final Properties executionProperties, final AxState axState, final Map<String, Object> fields,
final Set<String> stateOutputNames, final ApexInternalContext internalContext) {
- subject = new AxStateFacade(axState);
+ super(executionId, executionProperties);
- // Execution ID is the current policy execution instance
- this.executionId = executionId;
- this.executionProperties = executionProperties;
+ subject = new AxStateFacade(axState);
this.fields = fields;
this.stateOutputNames = stateOutputNames;
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java
index 9d5087e83..1a19d18b8 100644
--- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java
+++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java
@@ -32,6 +32,7 @@ import lombok.Getter;
import lombok.Setter;
import org.onap.policy.apex.context.ContextAlbum;
import org.onap.policy.apex.context.ContextRuntimeException;
+import org.onap.policy.apex.context.SchemaHelper;
import org.onap.policy.apex.core.engine.context.ApexInternalContext;
import org.onap.policy.apex.core.engine.executor.Executor;
import org.onap.policy.apex.core.engine.executor.TaskExecutor;
@@ -39,6 +40,8 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.policymodel.concepts.AxTask;
import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
@@ -50,27 +53,16 @@ import org.slf4j.ext.XLoggerFactory;
* @author Sven van der Meer (sven.van.der.meer@ericsson.com)
*/
@Getter
-public class TaskExecutionContext {
+public class TaskExecutionContext extends AbstractExecutionContext {
// Logger for task execution
private static final XLogger EXECUTION_LOGGER =
XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
// CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
- /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
- public final Boolean isTrue = true;
-
- /**
- * A constant <code>boolean false</code> value available for reuse e.g., for the return value
- */
- public final Boolean isFalse = false;
-
/** A facade to the full task definition for the task logic being executed. */
public final AxTaskFacade subject;
- /** the execution ID for the current APEX policy execution instance. */
- public final Long executionId;
-
/**
* The incoming fields from the trigger event for the task. The task logic can access these fields when executing
* its logic.
@@ -96,15 +88,6 @@ public class TaskExecutionContext {
// The artifact stack of users of this context
private final List<AxConcept> usedArtifactStack;
- // A message specified in the logic
- @Getter
- @Setter
- private String message;
-
- // Execution properties for a policy execution
- @Getter
- private Properties executionProperties;
-
// Parameters associated to a task
@Getter
private Map<String, String> parameters = new HashMap<>();
@@ -123,16 +106,14 @@ public class TaskExecutionContext {
public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
final Map<String, Object> outFields, final ApexInternalContext internalContext) {
+ super(executionId, executionProperties);
+
// The subject is the task definition
subject = new AxTaskFacade(axTask);
// Populate parameters to be accessed in the task logic from the task parameters.
populateParameters(axTask.getTaskParameters());
- // Execution ID is the current policy execution instance
- this.executionId = executionId;
- this.executionProperties = executionProperties;
-
// The input and output fields
this.inFields = Collections.unmodifiableMap(inFields);
this.outFields = outFields;
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java
index 2006d653c..9c3c2be0f 100644
--- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java
+++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java
@@ -48,27 +48,16 @@ import org.slf4j.ext.XLoggerFactory;
* @author Sven van der Meer (sven.van.der.meer@ericsson.com)
*/
@Getter
-public class TaskSelectionExecutionContext {
+public class TaskSelectionExecutionContext extends AbstractExecutionContext {
// Logger for task execution
private static final XLogger EXECUTION_LOGGER =
XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskSelectionExecutionLogging");
// CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
- /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
- public final Boolean isTrue = true;
-
- /**
- * A constant <code>boolean false</code> value available for reuse e.g., for the return value
- */
- public final Boolean isFalse = false;
-
/** A facade to the full state definition for the task selection logic being executed. */
public final AxStateFacade subject;
- /** the execution ID for the current APEX policy execution instance. */
- public final Long executionId;
-
/**
* The incoming fields from the trigger event for the state. The task selection logic can access these fields to
* decide what task to select for the state.
@@ -91,15 +80,6 @@ public class TaskSelectionExecutionContext {
// All available context albums
private final Map<String, ContextAlbum> context;
- // A message specified in the logic
- @Getter
- @Setter
- private String message;
-
- // Execution properties for a policy execution
- @Getter
- private Properties executionProperties;
-
/**
* Instantiates a new task selection execution context.
*
@@ -113,13 +93,10 @@ public class TaskSelectionExecutionContext {
public TaskSelectionExecutionContext(final TaskSelectExecutor taskSelectExecutor, final long executionId,
final AxState axState, final EnEvent incomingEvent, final AxArtifactKey outgoingKey,
final ApexInternalContext internalContext) {
+ super(executionId, incomingEvent.getExecutionProperties());
// The subject is the state definition
subject = new AxStateFacade(axState);
- // Execution ID is the current policy execution instance
- this.executionId = executionId;
- this.executionProperties = incomingEvent.getExecutionProperties();
-
// The events
inFields = incomingEvent;
selectedTask = outgoingKey;