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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.executor.context;
23 import java.util.ArrayList;
24 import java.util.List;
27 import java.util.TreeMap;
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;
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.
45 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
47 public class StateFinalizerExecutionContext {
49 * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
51 private static final XLogger EXCEUTION_LOGGER =
52 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.StateFinalizerExecutionLogging");
54 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
56 /** A facade to the full state definition for the state finalizer logic being executed. */
57 public final AxStateFacade subject;
59 /** the execution ID for the current APEX policy execution instance. */
60 public final Long executionID;
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.
66 public final Set<String> stateOutputNames;
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.
73 public final Map<String, Object> fields;
75 // A message specified in the logic
76 private String message;
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.
82 private String selectedStateOutputName;
85 * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
87 public final XLogger logger = EXCEUTION_LOGGER;
89 // CHECKSTYLE:ON: checkstyle:visibilityModifier
91 // All available context albums
92 private final Map<String, ContextAlbum> context;
95 * Instantiates a new state finalizer execution context.
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
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);
109 // Execution ID is the current policy execution instance
110 this.executionID = executionID;
112 this.fields = fields;
113 this.stateOutputNames = stateOutputNames;
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));
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());
129 // Change the stack to an array
130 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
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);
140 * Return a context album if it exists in the context definition of this state.
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
146 public ContextAlbum getContextAlbum(final String contextAlbumName) throws ContextRuntimeException {
147 // Find the context album
148 final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
150 // Check if the context album exists
151 if (foundContextAlbum != null) {
152 return foundContextAlbum;
154 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
155 + "\" on state \"" + subject.getId() + "\"");
160 * Return the state output name selected by the state finalizer logic.
162 * @return the state output name
164 public String getSelectedStateOutputName() {
165 return selectedStateOutputName;
169 * Set the state output name selected by the state finalizer logic.
171 * @param selectedStateOutputName the state output name
173 public void setSelectedStateOutputName(final String selectedStateOutputName) {
174 this.selectedStateOutputName = selectedStateOutputName;
178 * Gets the user message.
180 * @return the user message
182 public String getMessage() {
187 * Sets the user message.
189 * @param message the message
191 public void setMessage(final String message) {
192 this.message = message;