b322cf402d6b5397851b8054678bb859fcdb9d31
[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 lombok.Getter;
31 import lombok.Setter;
32
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.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
43
44 /**
45  * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
46  * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
47  * global, and external context.
48  *
49  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
50  */
51 public class TaskExecutionContext {
52     // Logger for task execution
53     private static final XLogger EXECUTION_LOGGER =
54             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
55
56     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
57
58     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
59     public final Boolean isTrue = true;
60
61     /**
62      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
63      */
64     public final Boolean isFalse = false;
65
66     /** A facade to the full task definition for the task logic being executed. */
67     public final AxTaskFacade subject;
68
69     /** the execution ID for the current APEX policy execution instance. */
70     public final Long executionId;
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     @Getter
99     @Setter
100     private String message;
101
102     // Execution properties for a policy execution
103     @Getter
104     private Properties executionProperties;
105
106     /**
107      * Instantiates a new task execution context.
108      *
109      * @param taskExecutor the task executor that requires context
110      * @param executionId the execution ID for the current APEX policy execution instance
111      * @param executionProperties the execution properties for task execution
112      * @param axTask the task definition that is the subject of execution
113      * @param inFields the in fields
114      * @param outFields the out fields
115      * @param internalContext the execution context of the Apex engine in which the task is being executed
116      */
117     public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
118             final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
119             final Map<String, Object> outFields, final ApexInternalContext internalContext) {
120         // The subject is the task definition
121         subject = new AxTaskFacade(axTask);
122
123         // Execution ID is the current policy execution instance
124         this.executionId = executionId;
125         this.executionProperties = executionProperties;
126
127         // The input and output fields
128         this.inFields = Collections.unmodifiableMap(inFields);
129         this.outFields = outFields;
130
131         // Set up the context albums for this task
132         context = new TreeMap<>();
133         for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
134             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
135         }
136
137         // Get the artifact stack of the users of the policy
138         usedArtifactStack = new ArrayList<>();
139         for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
140             // Add each parent to the top of the stack
141             usedArtifactStack.add(0, parent.getKey());
142         }
143
144         // Change the stack to an array
145         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
146
147         // Set the user of the context
148         for (final ContextAlbum contextAlbum : context.values()) {
149             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
150         }
151     }
152
153     /**
154      * Return a context album if it exists in the context definition of this task.
155      *
156      * @param contextAlbumName The context album name
157      * @return The context album
158      * @throws ContextRuntimeException if the context album does not exist on the task for this executor
159      */
160     public ContextAlbum getContextAlbum(final String contextAlbumName) {
161         // Find the context album
162         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
163
164         // Check if the context album exists
165         if (foundContextAlbum != null) {
166             return foundContextAlbum;
167         } else {
168             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
169                     + "\" on task \"" + subject.getId() + "\"");
170         }
171     }
172 }