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