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;
33 import org.onap.policy.apex.context.ContextAlbum;
34 import org.onap.policy.apex.context.ContextRuntimeException;
35 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
36 import org.onap.policy.apex.core.engine.executor.Executor;
37 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
40 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
45 * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
46 * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
47 * global, and external context.
49 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
51 public class TaskExecutionContext {
52 // Logger for task execution
53 private static final XLogger EXECUTION_LOGGER =
54 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
56 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
58 /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
59 public final Boolean isTrue = true;
62 * A constant <code>boolean false</code> value available for reuse e.g., for the return value
64 public final Boolean isFalse = false;
66 /** A facade to the full task definition for the task logic being executed. */
67 public final AxTaskFacade subject;
69 /** the execution ID for the current APEX policy execution instance. */
70 public final Long executionId;
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
100 private String message;
102 // Execution properties for a policy execution
104 private Properties executionProperties;
107 * Instantiates a new task execution context.
109 * @param taskExecutor the task executor that requires context
110 * @param executionId the execution ID for the current APEX policy execution instance
111 * @param executionProperties the execution properties for task execution
112 * @param axTask the task definition that is the subject of execution
113 * @param inFields the in fields
114 * @param outFields the out fields
115 * @param internalContext the execution context of the Apex engine in which the task is being executed
117 public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
118 final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
119 final Map<String, Object> outFields, final ApexInternalContext internalContext) {
120 // The subject is the task definition
121 subject = new AxTaskFacade(axTask);
123 // Execution ID is the current policy execution instance
124 this.executionId = executionId;
125 this.executionProperties = executionProperties;
127 // The input and output fields
128 this.inFields = Collections.unmodifiableMap(inFields);
129 this.outFields = outFields;
131 // Set up the context albums for this task
132 context = new TreeMap<>();
133 for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
134 context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
137 // Get the artifact stack of the users of the policy
138 usedArtifactStack = new ArrayList<>();
139 for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
140 // Add each parent to the top of the stack
141 usedArtifactStack.add(0, parent.getKey());
144 // Change the stack to an array
145 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
147 // Set the user of the context
148 for (final ContextAlbum contextAlbum : context.values()) {
149 contextAlbum.setUserArtifactStack(usedArtifactStackArray);
154 * Return a context album if it exists in the context definition of this task.
156 * @param contextAlbumName The context album name
157 * @return The context album
158 * @throws ContextRuntimeException if the context album does not exist on the task for this executor
160 public ContextAlbum getContextAlbum(final String contextAlbumName) {
161 // Find the context album
162 final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
164 // Check if the context album exists
165 if (foundContextAlbum != null) {
166 return foundContextAlbum;
168 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
169 + "\" on task \"" + subject.getId() + "\"");