5efd47e532175db58c5fd40413bb624bf95a3947
[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.Set;
28 import java.util.TreeMap;
29
30 import org.onap.policy.apex.context.ContextAlbum;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
33 import org.onap.policy.apex.core.engine.executor.Executor;
34 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
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 state finalizer logic executions in a state being executed in an Apex
43  * engine. The state finalizer must have easy access to the state definition, the fields, as well as the policy, global,
44  * and external context.
45  *
46  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
47  */
48 public class StateFinalizerExecutionContext {
49     /**
50      * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
51      */
52     private static final XLogger EXCEUTION_LOGGER =
53             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.StateFinalizerExecutionLogging");
54
55     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
56
57     /** A facade to the full state definition for the state finalizer logic being executed. */
58     public final AxStateFacade subject;
59
60     /** the execution ID for the current APEX policy execution instance. */
61     public final Long executionId;
62
63     /** the execution properties the current APEX policy execution instance. */
64     public final Properties executionProperties;
65
66     /**
67      * The list of state outputs for this state finalizer. The purpose of a state finalizer is to select a state output
68      * for a state from this list of state output names.
69      */
70     public final Set<String> stateOutputNames;
71
72     /**
73      * The fields of this state finalizer. A state finalizer receives this list of fields from a task and may use these
74      * fields to determine what state output to select. Once a state finalizer has selected a state output, it must
75      * marshal these fields so that they match the fields required for the event defined in the state output.
76      */
77     public final Map<String, Object> fields;
78
79     // A message specified in the logic
80     private String message;
81
82     /**
83      * The state output that the state finalizer logic has selected for a state. The state finalizer logic sets this
84      * field in its logic after executing and the Apex engine uses this state output for this state.
85      */
86     private String selectedStateOutputName;
87
88     /**
89      * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
90      */
91     public final XLogger logger = EXCEUTION_LOGGER;
92
93     // CHECKSTYLE:ON: checkstyle:visibilityModifier
94
95     // All available context albums
96     private final Map<String, ContextAlbum> context;
97
98     /**
99      * Instantiates a new state finalizer execution context.
100      *
101      * @param stateFinalizerExecutor the state finalizer executor that requires context
102      * @param executionId the execution ID for the current APEX policy execution instance
103      * @param executionProperties the execution properties for task execution
104      * @param axState the state definition that is the subject of execution
105      * @param fields the fields to be manipulated by the state finalizer
106      * @param stateOutputNames the state output names, one of which will be selected by the state finalizer
107      * @param internalContext the execution context of the Apex engine in which the task is being executed
108      */
109     public StateFinalizerExecutionContext(final StateFinalizerExecutor stateFinalizerExecutor, final long executionId,
110             final Properties executionProperties, final AxState axState, final Map<String, Object> fields,
111             final Set<String> stateOutputNames, final ApexInternalContext internalContext) {
112         subject = new AxStateFacade(axState);
113
114         // Execution ID is the current policy execution instance
115         this.executionId = executionId;
116         this.executionProperties = executionProperties;
117
118         this.fields = fields;
119         this.stateOutputNames = stateOutputNames;
120
121         // Set up the context albums for this task
122         context = new TreeMap<>();
123         for (final AxArtifactKey mapKey : subject.state.getContextAlbumReferences()) {
124             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
125         }
126
127         // Get the artifact stack of the users of the policy
128         final List<AxConcept> usedArtifactStack = new ArrayList<>();
129         for (Executor<?, ?, ?, ?> parent = stateFinalizerExecutor.getParent(); parent != null; parent =
130                 parent.getParent()) {
131             // Add each parent to the top of the stack
132             usedArtifactStack.add(0, parent.getKey());
133         }
134
135         // Change the stack to an array
136         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
137
138         // Set the user of the context
139         // Set the user of the context
140         for (final ContextAlbum contextAlbum : context.values()) {
141             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
142         }
143     }
144
145     /**
146      * Return a context album if it exists in the context definition of this state.
147      *
148      * @param contextAlbumName The context album name
149      * @return The context album
150      * @throws ContextRuntimeException if the context album does not exist on the state for this executor
151      */
152     public ContextAlbum getContextAlbum(final String contextAlbumName) {
153         // Find the context album
154         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
155
156         // Check if the context album exists
157         if (foundContextAlbum != null) {
158             return foundContextAlbum;
159         } else {
160             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
161                     + "\" on state \"" + subject.getId() + "\"");
162         }
163     }
164
165     /**
166      * Return the state output name selected by the state finalizer logic.
167      *
168      * @return the state output name
169      */
170     public String getSelectedStateOutputName() {
171         return selectedStateOutputName;
172     }
173
174     /**
175      * Set the state output name selected by the state finalizer logic.
176      *
177      * @param selectedStateOutputName the state output name
178      */
179     public void setSelectedStateOutputName(final String selectedStateOutputName) {
180         this.selectedStateOutputName = selectedStateOutputName;
181     }
182
183     /**
184      * Gets the user message.
185      *
186      * @return the user message
187      */
188     public String getMessage() {
189         return message;
190     }
191
192     /**
193      * Sets the user message.
194      *
195      * @param message the message
196      */
197     public void setMessage(final String message) {
198         this.message = message;
199     }
200 }