2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.core.engine.executor.context;
26 import java.util.ArrayList;
27 import java.util.List;
29 import java.util.Properties;
31 import java.util.TreeMap;
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;
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.
50 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
53 public class StateFinalizerExecutionContext extends AbstractExecutionContext {
55 * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
57 private static final XLogger EXCEUTION_LOGGER =
58 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.StateFinalizerExecutionLogging");
60 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
62 /** A facade to the full state definition for the state finalizer logic being executed. */
63 public final AxStateFacade subject;
66 * The list of state outputs for this state finalizer. The purpose of a state finalizer is to select a state output
67 * for a state from this list of state output names.
69 public final Set<String> stateOutputNames;
72 * The fields of this state finalizer. A state finalizer receives this list of fields from a task and may use these
73 * fields to determine what state output to select. Once a state finalizer has selected a state output, it must
74 * marshal these fields so that they match the fields required for the event defined in the state output.
76 public final Map<String, Object> fields;
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.
84 private String selectedStateOutputName;
87 * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
89 public final XLogger logger = EXCEUTION_LOGGER;
91 // CHECKSTYLE:ON: checkstyle:visibilityModifier
93 // All available context albums
94 private final Map<String, ContextAlbum> context;
96 // Execution properties for a policy execution
98 private Properties executionProperties;
101 * Instantiates a new state finalizer execution context.
103 * @param stateFinalizerExecutor the state finalizer executor that requires context
104 * @param executionId the execution ID for the current APEX policy execution instance
105 * @param executionProperties the execution properties for task execution
106 * @param axState the state definition that is the subject of execution
107 * @param fields the fields to be manipulated by the state finalizer
108 * @param stateOutputNames the state output names, one of which will be selected by the state finalizer
109 * @param internalContext the execution context of the Apex engine in which the task is being executed
111 public StateFinalizerExecutionContext(final StateFinalizerExecutor stateFinalizerExecutor, final long executionId,
112 final Properties executionProperties, final AxState axState, final Map<String, Object> fields,
113 final Set<String> stateOutputNames, final ApexInternalContext internalContext) {
114 super(executionId, executionProperties);
116 subject = new AxStateFacade(axState);
118 this.fields = fields;
119 this.stateOutputNames = stateOutputNames;
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));
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());
135 // Change the stack to an array
136 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
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);
146 * Return a context album if it exists in the context definition of this state.
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
152 public ContextAlbum getContextAlbum(final String contextAlbumName) {
153 // Find the context album
154 final var foundContextAlbum = context.get(contextAlbumName);
156 // Check if the context album exists
157 if (foundContextAlbum != null) {
158 return foundContextAlbum;
160 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
161 + "\" on state \"" + subject.getId() + "\"");