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