2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.executor.context;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.Properties;
28 import java.util.TreeMap;
30 import org.onap.policy.apex.context.ContextAlbum;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
33 import org.onap.policy.apex.core.engine.executor.Executor;
34 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
37 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
38 import org.slf4j.ext.XLogger;
39 import org.slf4j.ext.XLoggerFactory;
42 * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
43 * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
44 * global, and external context.
46 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
48 public class TaskExecutionContext {
49 // Logger for task execution
50 private static final XLogger EXECUTION_LOGGER =
51 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
53 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
55 /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
56 public final Boolean isTrue = true;
59 * A constant <code>boolean false</code> value available for reuse e.g., for the return value
61 public final Boolean isFalse = false;
63 /** A facade to the full task definition for the task logic being executed. */
64 public final AxTaskFacade subject;
66 /** the execution ID for the current APEX policy execution instance. */
67 public final Long executionId;
69 /** the execution properties the current APEX policy execution instance. */
70 public final Properties executionProperties;
73 * The incoming fields from the trigger event for the task. The task logic can access these fields when executing
76 public final Map<String, Object> inFields;
79 * The outgoing fields from the task. The task logic can access and set these fields with its logic. A task outputs
80 * its result using these fields.
82 public final Map<String, Object> outFields;
85 * Logger for task execution, task logic can use this field to access and log to Apex logging.
87 public final XLogger logger = EXECUTION_LOGGER;
89 // CHECKSTYLE:ON: checkstyle:VisibilityModifier
91 // All available context albums
92 private final Map<String, ContextAlbum> context;
94 // The artifact stack of users of this context
95 private final List<AxConcept> usedArtifactStack;
97 // A message specified in the logic
98 private String message;
101 * Instantiates a new task execution context.
103 * @param taskExecutor the task executor that requires context
104 * @param executionId the execution ID for the current APEX policy execution instance
105 * @param executionProperties the execution properties for task execution
106 * @param axTask the task definition that is the subject of execution
107 * @param inFields the in fields
108 * @param outFields the out fields
109 * @param internalContext the execution context of the Apex engine in which the task is being executed
111 public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
112 final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
113 final Map<String, Object> outFields, final ApexInternalContext internalContext) {
114 // The subject is the task definition
115 subject = new AxTaskFacade(axTask);
117 // Execution ID is the current policy execution instance
118 this.executionId = executionId;
119 this.executionProperties = executionProperties;
121 // The input and output fields
122 this.inFields = Collections.unmodifiableMap(inFields);
123 this.outFields = outFields;
125 // Set up the context albums for this task
126 context = new TreeMap<>();
127 for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
128 context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
131 // Get the artifact stack of the users of the policy
132 usedArtifactStack = new ArrayList<>();
133 for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
134 // Add each parent to the top of the stack
135 usedArtifactStack.add(0, parent.getKey());
138 // Change the stack to an array
139 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
141 // Set the user of the context
142 for (final ContextAlbum contextAlbum : context.values()) {
143 contextAlbum.setUserArtifactStack(usedArtifactStackArray);
148 * Return a context album if it exists in the context definition of this task.
150 * @param contextAlbumName The context album name
151 * @return The context album
152 * @throws ContextRuntimeException if the context album does not exist on the task for this executor
154 public ContextAlbum getContextAlbum(final String contextAlbumName) {
155 // Find the context album
156 final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
158 // Check if the context album exists
159 if (foundContextAlbum != null) {
160 return foundContextAlbum;
162 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
163 + "\" on task \"" + subject.getId() + "\"");
168 * Get the user message.
170 * @return the user message
172 public String getMessage() {
177 * Sets the user message.
179 * @param message the message
181 public void setMessage(final String message) {
182 this.message = message;