6fb55a3e78c47877cff3638038f7bc0de23cc516
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.engine.executor.context;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.TreeMap;
29
30 import org.onap.policy.apex.context.ContextAlbum;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
33 import org.onap.policy.apex.core.engine.executor.Executor;
34 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
37 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
38 import org.slf4j.ext.XLogger;
39 import org.slf4j.ext.XLoggerFactory;
40
41 /**
42  * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
43  * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
44  * global, and external context.
45  *
46  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
47  */
48 public class TaskExecutionContext {
49     // Logger for task execution
50     private static final XLogger EXECUTION_LOGGER =
51             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
52
53     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
54
55     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
56     public final Boolean isTrue = true;
57
58     /**
59      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
60      */
61     public final Boolean isFalse = false;
62
63     /** A facade to the full task definition for the task logic being executed. */
64     public final AxTaskFacade subject;
65
66     /** the execution ID for the current APEX policy execution instance. */
67     public final Long executionId;
68
69     /** the execution properties the current APEX policy execution instance. */
70     public final Properties executionProperties;
71
72     /**
73      * The incoming fields from the trigger event for the task. The task logic can access these fields when executing
74      * its logic.
75      */
76     public final Map<String, Object> inFields;
77
78     /**
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.
81      */
82     public final Map<String, Object> outFields;
83
84     /**
85      * Logger for task execution, task logic can use this field to access and log to Apex logging.
86      */
87     public final XLogger logger = EXECUTION_LOGGER;
88
89     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
90
91     // All available context albums
92     private final Map<String, ContextAlbum> context;
93
94     // The artifact stack of users of this context
95     private final List<AxConcept> usedArtifactStack;
96
97     // A message specified in the logic
98     private String message;
99
100     /**
101      * Instantiates a new task execution context.
102      *
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 outFields the out fields
109      * @param internalContext the execution context of the Apex engine in which the task is being executed
110      */
111     public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
112             final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
113             final Map<String, Object> outFields, final ApexInternalContext internalContext) {
114         // The subject is the task definition
115         subject = new AxTaskFacade(axTask);
116
117         // Execution ID is the current policy execution instance
118         this.executionId = executionId;
119         this.executionProperties = executionProperties;
120
121         // The input and output fields
122         this.inFields = Collections.unmodifiableMap(inFields);
123         this.outFields = outFields;
124
125         // Set up the context albums for this task
126         context = new TreeMap<>();
127         for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
128             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
129         }
130
131         // Get the artifact stack of the users of the policy
132         usedArtifactStack = new ArrayList<>();
133         for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
134             // Add each parent to the top of the stack
135             usedArtifactStack.add(0, parent.getKey());
136         }
137
138         // Change the stack to an array
139         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
140
141         // Set the user of the context
142         for (final ContextAlbum contextAlbum : context.values()) {
143             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
144         }
145     }
146
147     /**
148      * Return a context album if it exists in the context definition of this task.
149      *
150      * @param contextAlbumName The context album name
151      * @return The context album
152      * @throws ContextRuntimeException if the context album does not exist on the task for this executor
153      */
154     public ContextAlbum getContextAlbum(final String contextAlbumName) {
155         // Find the context album
156         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
157
158         // Check if the context album exists
159         if (foundContextAlbum != null) {
160             return foundContextAlbum;
161         } else {
162             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
163                     + "\" on task \"" + subject.getId() + "\"");
164         }
165     }
166
167     /**
168      * Get the user message.
169      *
170      * @return the user message
171      */
172     public String getMessage() {
173         return message;
174     }
175
176     /**
177      * Sets the user message.
178      *
179      * @param message the message
180      */
181     public void setMessage(final String message) {
182         this.message = message;
183     }
184 }