2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.core.engine.executor.context;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Properties;
33 import java.util.TreeMap;
35 import org.onap.policy.apex.context.ContextAlbum;
36 import org.onap.policy.apex.context.ContextRuntimeException;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.executor.Executor;
39 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
42 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
43 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
44 import org.slf4j.ext.XLogger;
45 import org.slf4j.ext.XLoggerFactory;
48 * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
49 * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
50 * global, and external context.
52 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
55 public class TaskExecutionContext extends AbstractExecutionContext {
56 // Logger for task execution
57 private static final XLogger EXECUTION_LOGGER =
58 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
60 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
62 /** A facade to the full task definition for the task logic being executed. */
63 public final AxTaskFacade subject;
66 * The incoming fields from the trigger event for the task. The task logic can access these fields when executing
69 public final Map<String, Object> inFields;
72 * The outgoing fields from the task. The task logic can access and set these fields with its logic. A task outputs
73 * its result using these fields.
75 public final Map<String, Object> outFields;
78 * The outgoing fields from the task. The task logic can access and set these fields with its logic. A task outputs
79 * its result using these fields.
81 public final Collection<Map<String, Object>> outFieldsList;
84 * Logger for task execution, task logic can use this field to access and log to Apex logging.
86 public final XLogger logger = EXECUTION_LOGGER;
88 // CHECKSTYLE:ON: checkstyle:VisibilityModifier
90 // All available context albums
91 private final Map<String, ContextAlbum> context;
93 // The artifact stack of users of this context
94 private final List<AxConcept> usedArtifactStack;
96 // Parameters associated to a task
98 private Map<String, String> parameters = new HashMap<>();
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 outFieldsList collection of 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 Collection<Map<String, Object>> outFieldsList, final ApexInternalContext internalContext) {
114 super(executionId, executionProperties);
116 // The subject is the task definition
117 subject = new AxTaskFacade(axTask);
119 // Populate parameters to be accessed in the task logic from the task parameters.
120 populateParameters(axTask.getTaskParameters());
122 // The input and output fields
123 this.inFields = Collections.unmodifiableMap(inFields);
124 this.outFieldsList = outFieldsList;
125 // if only a single output event needs to fired from a task, the outFields alone can be used too
126 if (outFieldsList.isEmpty()) {
127 this.outFields = new TreeMap<>();
129 this.outFields = outFieldsList.iterator().next();
132 // Set up the context albums for this task
133 context = new TreeMap<>();
134 for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
135 context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
138 // Get the artifact stack of the users of the policy
139 usedArtifactStack = new ArrayList<>();
140 for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
141 // Add each parent to the top of the stack
142 usedArtifactStack.add(0, parent.getKey());
145 // Change the stack to an array
146 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
148 // Set the user of the context
149 for (final ContextAlbum contextAlbum : context.values()) {
150 contextAlbum.setUserArtifactStack(usedArtifactStackArray);
155 * Populate parameters to be accessed in the task logic.
157 * @param taskParameters The task parameters
159 private void populateParameters(Map<String, AxTaskParameter> taskParameters) {
160 taskParameters.entrySet().forEach(taskParamEntry -> parameters.put(taskParamEntry.getKey(),
161 taskParamEntry.getValue().getTaskParameterValue()));
165 * Return a context album if it exists in the context definition of this task.
167 * @param contextAlbumName The context album name
168 * @return The context album
169 * @throws ContextRuntimeException if the context album does not exist on the task for this executor
171 public ContextAlbum getContextAlbum(final String contextAlbumName) {
172 // Find the context album
173 final var foundContextAlbum = context.get(contextAlbumName);
175 // Check if the context album exists
176 if (foundContextAlbum != null) {
177 return foundContextAlbum;
179 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
180 + "\" on task \"" + subject.getId() + "\"");
185 * Method to add fields to the output event list.
186 * @param fields the fields to be added
188 public void addFieldsToOutput(Map<String, Object> fields) {
189 for (Map<String, Object> outputFields : outFieldsList) {
190 if (outputFields.keySet().containsAll(fields.keySet())) {
191 outputFields.replaceAll((name, value) -> fields.get(name));