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