4a9e83063abe34ce5c496ab4ba66e242319113cd
[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.TreeMap;
28
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;
39
40 /**
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.
44  *
45  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
46  */
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");
51
52     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
53
54     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
55     public final Boolean isTrue = true;
56
57     /**
58      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
59      */
60     public final Boolean isFalse = false;
61
62     /** A facade to the full task definition for the task logic being executed. */
63     public final AxTaskFacade subject;
64
65     /** the execution ID for the current APEX policy execution instance. */
66     public final Long executionId;
67
68     /**
69      * The incoming fields from the trigger event for the task. The task logic can access these
70      * fields when executing its logic.
71      */
72     public final Map<String, Object> inFields;
73
74     /**
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.
77      */
78     public final Map<String, Object> outFields;
79
80     /**
81      * Logger for task execution, task logic can use this field to access and log to Apex logging.
82      */
83     public final XLogger logger = EXECUTION_LOGGER;
84
85     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
86
87     // All available context albums
88     private final Map<String, ContextAlbum> context;
89
90     // The artifact stack of users of this context
91     private final List<AxConcept> usedArtifactStack;
92
93     // A message specified in the logic
94     private String message;
95
96     /**
97      * Instantiates a new task execution context.
98      *
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
105      *        executed
106      */
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);
112
113         // Execution ID is the current policy execution instance
114         this.executionId = executionId;
115
116         // The input and output fields
117         this.inFields = Collections.unmodifiableMap(inFields);
118         this.outFields = outFields;
119
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));
124         }
125
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());
131         }
132
133         // Change the stack to an array
134         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
135
136         // Set the user of the context
137         for (final ContextAlbum contextAlbum : context.values()) {
138             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
139         }
140     }
141
142     /**
143      * Return a context album if it exists in the context definition of this task.
144      *
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
148      *         executor
149      */
150     public ContextAlbum getContextAlbum(final String contextAlbumName) {
151         // Find the context album
152         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
153
154         // Check if the context album exists
155         if (foundContextAlbum != null) {
156             return foundContextAlbum;
157         } else {
158             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
159                     + "\" on task \"" + subject.getId() + "\"");
160         }
161     }
162
163     /**
164      * Get the user message.
165      *
166      * @return the user message
167      */
168     public String getMessage() {
169         return message;
170     }
171
172     /**
173      * Sets the user message.
174      *
175      * @param message the message
176      */
177     public void setMessage(final String message) {
178         this.message = message;
179     }
180 }