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;
26 import java.util.Properties;
27 import java.util.TreeMap;
32 import org.onap.policy.apex.context.ContextAlbum;
33 import org.onap.policy.apex.context.ContextRuntimeException;
34 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
35 import org.onap.policy.apex.core.engine.event.EnEvent;
36 import org.onap.policy.apex.core.engine.executor.Executor;
37 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
40 import org.onap.policy.apex.model.policymodel.concepts.AxState;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
45 * Container class for the execution context for Task Selection logic executions in a task being
46 * executed in an Apex engine. The task must have easy access to the state definition, the incoming
47 * and outgoing event contexts, as well as the policy, global, and external context.
49 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
51 public class TaskSelectionExecutionContext {
52 // Logger for task execution
53 private static final XLogger EXECUTION_LOGGER =
54 XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskSelectionExecutionLogging");
56 // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
58 /** A constant <code>boolean true</code> value available for reuse e.g., for the return value */
59 public final Boolean isTrue = true;
62 * A constant <code>boolean false</code> value available for reuse e.g., for the return value
64 public final Boolean isFalse = false;
66 /** A facade to the full state definition for the task selection logic being executed. */
67 public final AxStateFacade subject;
69 /** the execution ID for the current APEX policy execution instance. */
70 public final Long executionId;
73 * The incoming fields from the trigger event for the state. The task selection logic can access
74 * these fields to decide what task to select for the state.
76 public final Map<String, Object> inFields;
79 * The task that the task selection logic has selected for a state. The task selection logic
80 * sets this field in its logic prior to executing and the Apex engine executes this task as the
81 * task for this state.
83 public final AxArtifactKey selectedTask;
86 * Logger for task selection execution, task selection logic can use this field to access and
87 * log to Apex logging.
89 public final XLogger logger = EXECUTION_LOGGER;
91 // CHECKSTYLE:ON: checkstyle:VisibilityModifier
93 // All available context albums
94 private final Map<String, ContextAlbum> context;
96 // A message specified in the logic
99 private String message;
101 // Execution properties for a policy execution
103 private Properties executionProperties;
106 * Instantiates a new task selection execution context.
108 * @param taskSelectExecutor the task selection executor that requires context
109 * @param executionId the execution identifier
110 * @param axState the state definition that is the subject of execution
111 * @param incomingEvent the incoming event for the state
112 * @param outgoingKey the outgoing key for the task to execute in this state
113 * @param internalContext the execution context of the Apex engine in which the task is being
116 public TaskSelectionExecutionContext(final TaskSelectExecutor taskSelectExecutor, final long executionId,
117 final AxState axState, final EnEvent incomingEvent, final AxArtifactKey outgoingKey,
118 final ApexInternalContext internalContext) {
119 // The subject is the state definition
120 subject = new AxStateFacade(axState);
122 // Execution ID is the current policy execution instance
123 this.executionId = executionId;
124 this.executionProperties = incomingEvent.getExecutionProperties();
127 inFields = incomingEvent;
128 selectedTask = outgoingKey;
130 // Set up the context albums for this task
131 // Set up the context albums for this task
132 context = new TreeMap<>();
133 for (final AxArtifactKey mapKey : subject.state.getContextAlbumReferences()) {
134 context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
137 // Get the artifact stack of the users of the policy
138 final List<AxConcept> usedArtifactStack = new ArrayList<>();
139 for (Executor<?, ?, ?, ?> parent = taskSelectExecutor.getParent(); parent != null; parent =
140 parent.getParent()) {
141 // Add each parent to the top of the stack
142 usedArtifactStack.add(0, parent.getKey());
145 // Add the events to the artifact stack
146 usedArtifactStack.add(incomingEvent.getKey());
148 // Change the stack to an array
149 final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
151 // Set the user of the context
152 // Set the user of the context
153 for (final ContextAlbum contextAlbum : context.values()) {
154 contextAlbum.setUserArtifactStack(usedArtifactStackArray);
156 incomingEvent.setUserArtifactStack(usedArtifactStackArray);
160 * Return a context album if it exists in the context definition of this state.
162 * @param contextAlbumName The context album name
163 * @return The context albumxxxxxx
164 * @throws ContextRuntimeException if the context album does not exist on the state for this
167 public ContextAlbum getContextAlbum(final String contextAlbumName) {
168 // Find the context album
169 final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
171 // Check if the context album exists
172 if (foundContextAlbum != null) {
173 return foundContextAlbum;
175 throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
176 + "\" on state \"" + subject.getId() + "\"");