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
42 * an Apex engine. The task must have easy access to the task definition, the incoming and outgoing
43 * field contexts, as well as the policy, 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 isTrue = true;
58 * A constant <code>boolean false</code> value available for reuse e.g., for the return value
60 public final Boolean isFalse = false;
62 /** A facade to the full task definition for the task logic being executed. */
63 public final AxTaskFacade subject;
65 /** the execution ID for the current APEX policy execution instance. */
66 public final Long executionId;
69 * The incoming fields from the trigger event for the task. The task logic can access these
70 * fields when executing its logic.
72 public final Map<String, Object> inFields;
75 * The outgoing fields from the task. The task logic can access and set these fields with its
76 * logic. A task outputs its result using these fields.
78 public final Map<String, Object> outFields;
81 * Logger for task execution, task logic can use this field to access and log to Apex logging.
83 public final XLogger logger = EXECUTION_LOGGER;
85 // CHECKSTYLE:ON: checkstyle:VisibilityModifier
87 // All available context albums
88 private final Map<String, ContextAlbum> context;
90 // The artifact stack of users of this context
91 private final List<AxConcept> usedArtifactStack;
93 // A message specified in the logic
94 private String message;
97 * Instantiates a new task execution context.
99 * @param taskExecutor the task executor that requires context
100 * @param executionId the execution ID for the current APEX policy execution instance
101 * @param axTask the task definition that is the subject of execution
102 * @param inFields the in fields
103 * @param outFields the out fields
104 * @param internalContext the execution context of the Apex engine in which the task is being
107 public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId, final AxTask axTask,
108 final Map<String, Object> inFields, final Map<String, Object> outFields,
109 final ApexInternalContext internalContext) {
110 // The subject is the task definition
111 subject = new AxTaskFacade(axTask);
113 // Execution ID is the current policy execution instance
114 this.executionId = executionId;
116 // The input and output fields
117 this.inFields = Collections.unmodifiableMap(inFields);
118 this.outFields = outFields;
120 // Set up the context albums for this task
121 context = new TreeMap<>();
122 for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
123 context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
126 // Get the artifact stack of the users of the policy
127 usedArtifactStack = new ArrayList<>();
128 for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
129 // Add each parent to the top of the stack
130 usedArtifactStack.add(0, parent.getKey());
133 // Change the stack to an array
134 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
136 // Set the user of the context
137 for (final ContextAlbum contextAlbum : context.values()) {
138 contextAlbum.setUserArtifactStack(usedArtifactStackArray);
143 * Return a context album if it exists in the context definition of this task.
145 * @param contextAlbumName The context album name
146 * @return The context album
147 * @throws ContextRuntimeException if the context album does not exist on the task for this
150 public ContextAlbum getContextAlbum(final String contextAlbumName) {
151 // Find the context album
152 final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
154 // Check if the context album exists
155 if (foundContextAlbum != null) {
156 return foundContextAlbum;
158 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
159 + "\" on task \"" + subject.getId() + "\"");
164 * Get the user message.
166 * @return the user message
168 public String getMessage() {
173 * Sets the user message.
175 * @param message the message
177 public void setMessage(final String message) {
178 this.message = message;