7cfb6348e76eefbd6af0a14f1894d368885db7b3
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / executor / context / TaskSelectionExecutionContext.java
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.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.event.EnEvent;
37 import org.onap.policy.apex.core.engine.executor.Executor;
38 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
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.AxState;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
44
45 /**
46  * Container class for the execution context for Task Selection logic executions in a task being executed in an Apex
47  * engine. The task must have easy access to the state definition, the incoming and outgoing event contexts, as well as
48  * the policy, global, and external context.
49  *
50  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
51  */
52 @Getter
53 public class TaskSelectionExecutionContext {
54     // Logger for task execution
55     private static final XLogger EXECUTION_LOGGER =
56             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskSelectionExecutionLogging");
57
58     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
59
60     /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
61     public final Boolean isTrue = true;
62
63     /**
64      * A constant <code>boolean false</code> value available for reuse e.g., for the return value
65      */
66     public final Boolean isFalse = false;
67
68     /** A facade to the full state definition for the task selection logic being executed. */
69     public final AxStateFacade subject;
70
71     /** the execution ID for the current APEX policy execution instance. */
72     public final Long executionId;
73
74     /**
75      * The incoming fields from the trigger event for the state. The task selection logic can access these fields to
76      * decide what task to select for the state.
77      */
78     public final Map<String, Object> inFields;
79
80     /**
81      * The task that the task selection logic has selected for a state. The task selection logic sets this field in its
82      * logic prior to executing and the Apex engine executes this task as the task for this state.
83      */
84     public final AxArtifactKey selectedTask;
85
86     /**
87      * Logger for task selection execution, task selection logic can use this field to access and log to Apex logging.
88      */
89     public final XLogger logger = EXECUTION_LOGGER;
90
91     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
92
93     // All available context albums
94     private final Map<String, ContextAlbum> context;
95
96     // A message specified in the logic
97     @Getter
98     @Setter
99     private String message;
100
101     // Execution properties for a policy execution
102     @Getter
103     private Properties executionProperties;
104
105     /**
106      * Instantiates a new task selection execution context.
107      *
108      * @param taskSelectExecutor the task selection executor that requires context
109      * @param executionId the execution identifier
110      * @param axState the state definition that is the subject of execution
111      * @param incomingEvent the incoming event for the state
112      * @param outgoingKey the outgoing key for the task to execute in this state
113      * @param internalContext the execution context of the Apex engine in which the task is being executed
114      */
115     public TaskSelectionExecutionContext(final TaskSelectExecutor taskSelectExecutor, final long executionId,
116             final AxState axState, final EnEvent incomingEvent, final AxArtifactKey outgoingKey,
117             final ApexInternalContext internalContext) {
118         // The subject is the state definition
119         subject = new AxStateFacade(axState);
120
121         // Execution ID is the current policy execution instance
122         this.executionId = executionId;
123         this.executionProperties = incomingEvent.getExecutionProperties();
124
125         // The events
126         inFields = incomingEvent;
127         selectedTask = outgoingKey;
128
129         // Set up the context albums for this task
130         // Set up the context albums for this task
131         context = new TreeMap<>();
132         for (final AxArtifactKey mapKey : subject.state.getContextAlbumReferences()) {
133             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
134         }
135
136         // Get the artifact stack of the users of the policy
137         final List<AxConcept> usedArtifactStack = new ArrayList<>();
138         for (Executor<?, ?, ?, ?> parent = taskSelectExecutor.getParent(); parent != null; parent =
139                 parent.getParent()) {
140             // Add each parent to the top of the stack
141             usedArtifactStack.add(0, parent.getKey());
142         }
143
144         // Add the events to the artifact stack
145         usedArtifactStack.add(incomingEvent.getKey());
146
147         // Change the stack to an array
148         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
149
150         // Set the user of the context
151         // Set the user of the context
152         for (final ContextAlbum contextAlbum : context.values()) {
153             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
154         }
155         incomingEvent.setUserArtifactStack(usedArtifactStackArray);
156     }
157
158     /**
159      * Return a context album if it exists in the context definition of this state.
160      *
161      * @param contextAlbumName The context album name
162      * @return The context albumxxxxxx
163      * @throws ContextRuntimeException if the context album does not exist on the state for this executor
164      */
165     public ContextAlbum getContextAlbum(final String contextAlbumName) {
166         // Find the context album
167         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
168
169         // Check if the context album exists
170         if (foundContextAlbum != null) {
171             return foundContextAlbum;
172         } else {
173             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
174                     + "\" on state \"" + subject.getId() + "\"");
175         }
176     }
177 }