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