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