1a19d18b8dd3eb94f899a498d81c976609ec14aa
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor.context;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.TreeMap;
31 import lombok.Getter;
32 import lombok.Setter;
33 import org.onap.policy.apex.context.ContextAlbum;
34 import org.onap.policy.apex.context.ContextRuntimeException;
35 import org.onap.policy.apex.context.SchemaHelper;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.core.engine.executor.Executor;
38 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
41 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
42 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
47
48 /**
49  * Container class for the execution context for Task logic executions in a task being executed in an Apex engine. The
50  * task must have easy access to the task definition, the incoming and outgoing field contexts, as well as the policy,
51  * global, and external context.
52  *
53  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
54  */
55 @Getter
56 public class TaskExecutionContext extends AbstractExecutionContext {
57     // Logger for task execution
58     private static final XLogger EXECUTION_LOGGER =
59             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskExecutionLogging");
60
61     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
62
63     /** A facade to the full task definition for the task logic being executed. */
64     public final AxTaskFacade subject;
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     /**
79      * Logger for task execution, task logic can use this field to access and log to Apex logging.
80      */
81     public final XLogger logger = EXECUTION_LOGGER;
82
83     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
84
85     // All available context albums
86     private final Map<String, ContextAlbum> context;
87
88     // The artifact stack of users of this context
89     private final List<AxConcept> usedArtifactStack;
90
91     // Parameters associated to a task
92     @Getter
93     private Map<String, String> parameters = new HashMap<>();
94
95     /**
96      * Instantiates a new task execution context.
97      *
98      * @param taskExecutor the task executor that requires context
99      * @param executionId the execution ID for the current APEX policy execution instance
100      * @param executionProperties the execution properties for task execution
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 executed
105      */
106     public TaskExecutionContext(final TaskExecutor taskExecutor, final long executionId,
107             final Properties executionProperties, final AxTask axTask, final Map<String, Object> inFields,
108             final Map<String, Object> outFields, final ApexInternalContext internalContext) {
109         super(executionId, executionProperties);
110
111         // The subject is the task definition
112         subject = new AxTaskFacade(axTask);
113
114         // Populate parameters to be accessed in the task logic from the task parameters.
115         populateParameters(axTask.getTaskParameters());
116
117         // The input and output fields
118         this.inFields = Collections.unmodifiableMap(inFields);
119         this.outFields = outFields;
120
121         // Set up the context albums for this task
122         context = new TreeMap<>();
123         for (final AxArtifactKey mapKey : subject.task.getContextAlbumReferences()) {
124             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
125         }
126
127         // Get the artifact stack of the users of the policy
128         usedArtifactStack = new ArrayList<>();
129         for (Executor<?, ?, ?, ?> parent = taskExecutor.getParent(); parent != null; parent = parent.getParent()) {
130             // Add each parent to the top of the stack
131             usedArtifactStack.add(0, parent.getKey());
132         }
133
134         // Change the stack to an array
135         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
136
137         // Set the user of the context
138         for (final ContextAlbum contextAlbum : context.values()) {
139             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
140         }
141     }
142
143     /**
144      * Populate parameters to be accessed in the task logic.
145      *
146      * @param taskParameters The task parameters
147      */
148     private void populateParameters(Map<String, AxTaskParameter> taskParameters) {
149         taskParameters.entrySet().forEach(taskParamEntry -> parameters.put(taskParamEntry.getKey(),
150                 taskParamEntry.getValue().getTaskParameterValue()));
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 }