49459dfae6cfb2a737fc18ec0305a568f9c836e3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.core.engine.executor.context;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.TreeMap;
32 import lombok.Getter;
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 extends AbstractExecutionContext {
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 facade to the full task definition for the task logic being executed. */
61     public final AxTaskFacade subject;
62
63     /**
64      * The incoming fields from the trigger event for the task. The task logic can access these fields when executing
65      * its logic.
66      */
67     public final Map<String, Object> inFields;
68
69     /**
70      * The outgoing fields from the task. The task logic can access and set these fields with its logic. A task outputs
71      * its result using these fields.
72      */
73     public final Map<String, Object> outFields;
74
75     /**
76      * Logger for task execution, task logic can use this field to access and log to Apex logging.
77      */
78     public final XLogger logger = EXECUTION_LOGGER;
79
80     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
81
82     // All available context albums
83     private final Map<String, ContextAlbum> context;
84
85     // The artifact stack of users of this context
86     private final List<AxConcept> usedArtifactStack;
87
88     // Parameters associated to a task
89     @Getter
90     private Map<String, String> parameters = new HashMap<>();
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 executionProperties the execution properties for task execution
98      * @param axTask the task definition that is the subject of execution
99      * @param inFields the in fields
100      * @param outFields the out fields
101      * @param internalContext the execution context of the Apex engine in which the task is being executed
102      */
103     public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
104             final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
105             final Map<String, Object> outFields, final ApexInternalContext internalContext) {
106         super(executionId, executionProperties);
107
108         // The subject is the task definition
109         subject = new AxTaskFacade(axTask);
110
111         // Populate parameters to be accessed in the task logic from the task parameters.
112         populateParameters(axTask.getTaskParameters());
113
114         // The input and output fields
115         this.inFields = Collections.unmodifiableMap(inFields);
116         this.outFields = outFields;
117
118         // Set up the context albums for this task
119         context = new TreeMap<>();
120         for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
121             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
122         }
123
124         // Get the artifact stack of the users of the policy
125         usedArtifactStack = new ArrayList<>();
126         for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
127             // Add each parent to the top of the stack
128             usedArtifactStack.add(0, parent.getKey());
129         }
130
131         // Change the stack to an array
132         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
133
134         // Set the user of the context
135         for (final ContextAlbum contextAlbum : context.values()) {
136             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
137         }
138     }
139
140     /**
141      * Populate parameters to be accessed in the task logic.
142      *
143      * @param taskParameters The task parameters
144      */
145     private void populateParameters(Map<String, AxTaskParameter> taskParameters) {
146         taskParameters.entrySet().forEach(taskParamEntry -> parameters.put(taskParamEntry.getKey(),
147                 taskParamEntry.getValue().getTaskParameterValue()));
148     }
149
150     /**
151      * Return a context album if it exists in the context definition of this task.
152      *
153      * @param contextAlbumName The context album name
154      * @return The context album
155      * @throws ContextRuntimeException if the context album does not exist on the task for this executor
156      */
157     public ContextAlbum getContextAlbum(final String contextAlbumName) {
158         // Find the context album
159         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
160
161         // Check if the context album exists
162         if (foundContextAlbum != null) {
163             return foundContextAlbum;
164         } else {
165             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
166                     + "\" on task \"" + subject.getId() + "\"");
167         }
168     }
169 }