Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / executor / context / TaskExecutionContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor.context;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.TreeMap;
31 import lombok.Getter;
32 import lombok.Setter;
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.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
44
45 /**
46  * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
47  * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
48  * global, and external context.
49  *
50  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
51  */
52 @Getter
53 public class TaskExecutionContext {
54     // Logger for task execution
55     private static final XLogger EXECUTION_LOGGER =
56             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
57
58     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
59
60     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
61     public final Boolean isTrue = true;
62
63     /**
64      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
65      */
66     public final Boolean isFalse = false;
67
68     /** A facade to the full task definition for the task logic being executed. */
69     public final AxTaskFacade subject;
70
71     /** the execution ID for the current APEX policy execution instance. */
72     public final Long executionId;
73
74     /**
75      * The incoming fields from the trigger event for the task. The task logic can access these fields when executing
76      * its logic.
77      */
78     public final Map<String, Object> inFields;
79
80     /**
81      * The outgoing fields from the task. The task logic can access and set these fields with its logic. A task outputs
82      * its result using these fields.
83      */
84     public final Map<String, Object> outFields;
85
86     /**
87      * Logger for task execution, task logic can use this field to access and log to Apex logging.
88      */
89     public final XLogger logger = EXECUTION_LOGGER;
90
91     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
92
93     // All available context albums
94     private final Map<String, ContextAlbum> context;
95
96     // The artifact stack of users of this context
97     private final List<AxConcept> usedArtifactStack;
98
99     // A message specified in the logic
100     @Getter
101     @Setter
102     private String message;
103
104     // Execution properties for a policy execution
105     @Getter
106     private Properties executionProperties;
107
108     // Parameters associated to a task
109     @Getter
110     private Map<String, String> parameters = new HashMap<>();
111
112     /**
113      * Instantiates a new task execution context.
114      *
115      * @param taskExecutor the task executor that requires context
116      * @param executionId the execution ID for the current APEX policy execution instance
117      * @param executionProperties the execution properties for task execution
118      * @param axTask the task definition that is the subject of execution
119      * @param inFields the in fields
120      * @param outFields the out fields
121      * @param internalContext the execution context of the Apex engine in which the task is being executed
122      */
123     public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
124             final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
125             final Map<String, Object> outFields, final ApexInternalContext internalContext) {
126         // The subject is the task definition
127         subject = new AxTaskFacade(axTask);
128
129         // Populate parameters to be accessed in the task logic from the task parameters.
130         populateParameters(axTask.getTaskParameters());
131
132         // Execution ID is the current policy execution instance
133         this.executionId = executionId;
134         this.executionProperties = executionProperties;
135
136         // The input and output fields
137         this.inFields = Collections.unmodifiableMap(inFields);
138         this.outFields = outFields;
139
140         // Set up the context albums for this task
141         context = new TreeMap<>();
142         for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
143             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
144         }
145
146         // Get the artifact stack of the users of the policy
147         usedArtifactStack = new ArrayList<>();
148         for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
149             // Add each parent to the top of the stack
150             usedArtifactStack.add(0, parent.getKey());
151         }
152
153         // Change the stack to an array
154         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
155
156         // Set the user of the context
157         for (final ContextAlbum contextAlbum : context.values()) {
158             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
159         }
160     }
161
162     /**
163      * Populate parameters to be accessed in the task logic.
164      *
165      * @param taskParameters The task parameters
166      */
167     private void populateParameters(Map<String, AxTaskParameter> taskParameters) {
168         taskParameters.entrySet().forEach(taskParamEntry -> parameters.put(taskParamEntry.getKey(),
169                 taskParamEntry.getValue().getTaskParameterValue()));
170     }
171
172     /**
173      * Return a context album if it exists in the context definition of this task.
174      *
175      * @param contextAlbumName The context album name
176      * @return The context album
177      * @throws ContextRuntimeException if the context album does not exist on the task for this executor
178      */
179     public ContextAlbum getContextAlbum(final String contextAlbumName) {
180         // Find the context album
181         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
182
183         // Check if the context album exists
184         if (foundContextAlbum != null) {
185             return foundContextAlbum;
186         } else {
187             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
188                     + "\" on task \"" + subject.getId() + "\"");
189         }
190     }
191 }