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.TreeMap;
29 import org.onap.policy.apex.context.ContextAlbum;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
32 import org.onap.policy.apex.core.engine.executor.Executor;
33 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
36 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
42 * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
43 * global, and external context.
45 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
47 public class TaskExecutionContext {
48 // Logger for task execution
49 private static final XLogger EXECUTION_LOGGER =
50 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
52 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
54 /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
55 public final Boolean TRUE = true;
57 /** A constant <code>boolean false</code> value available for reuse e.g., for the return value */
58 public final Boolean FALSE = false;
60 /** A facade to the full task definition for the task logic being executed. */
61 public final AxTaskFacade subject;
63 /** the execution ID for the current APEX policy execution instance. */
64 public final Long executionID;
67 * The incoming fields from the trigger event for the task. The task logic can access these fields when executing
70 public final Map<String, Object> inFields;
73 * The outgoing fields from the task. The task logic can access and set these fields with its logic. A task outputs
74 * its result using these fields.
76 public final Map<String, Object> outFields;
78 /** Logger for task execution, task logic can use this field to access and log to Apex logging. */
79 public final XLogger logger = EXECUTION_LOGGER;
81 // CHECKSTYLE:ON: checkstyle:VisibilityModifier
83 // All available context albums
84 private final Map<String, ContextAlbum> context;
86 // The artifact stack of users of this context
87 private final List<AxConcept> usedArtifactStack;
89 // A message specified in the logic
90 private String message;
93 * Instantiates a new task execution context.
95 * @param taskExecutor the task executor that requires context
96 * @param executionID the execution ID for the current APEX policy execution instance
97 * @param axTask the task definition that is the subject of execution
98 * @param inFields the in fields
99 * @param outFields the out fields
100 * @param internalContext the execution context of the Apex engine in which the task is being executed
102 public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionID, final AxTask axTask,
103 final Map<String, Object> inFields, final Map<String, Object> outFields,
104 final ApexInternalContext internalContext) {
105 // The subject is the task definition
106 subject = new AxTaskFacade(axTask);
108 // Execution ID is the current policy execution instance
109 this.executionID = executionID;
111 // The input and output fields
112 this.inFields = Collections.unmodifiableMap(inFields);
113 this.outFields = outFields;
115 // Set up the context albums for this task
116 context = new TreeMap<>();
117 for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
118 context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
121 // Get the artifact stack of the users of the policy
122 usedArtifactStack = new ArrayList<>();
123 for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
124 // Add each parent to the top of the stack
125 usedArtifactStack.add(0, parent.getKey());
128 // Change the stack to an array
129 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
131 // Set the user of the context
132 for (final ContextAlbum contextAlbum : context.values()) {
133 contextAlbum.setUserArtifactStack(usedArtifactStackArray);
138 * Return a context album if it exists in the context definition of this task.
140 * @param contextAlbumName The context album name
141 * @return The context album
142 * @throws ContextRuntimeException if the context album does not exist on the task for this executor
144 public ContextAlbum getContextAlbum(final String contextAlbumName) throws ContextRuntimeException {
145 // Find the context album
146 final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
148 // Check if the context album exists
149 if (foundContextAlbum != null) {
150 return foundContextAlbum;
152 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
153 + "\" on task \"" + subject.getId() + "\"");
158 * Get the user message.
160 * @return the user message
162 public String getMessage() {
167 * Sets the user message.
169 * @param message the message
171 public void setMessage(final String message) {
172 this.message = message;