69d51c45a6fb283ddaab0202c0c158024a150fb7
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.core.engine.executor.context;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.TreeMap;
29 import lombok.Getter;
30 import org.onap.policy.apex.context.ContextAlbum;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
33 import org.onap.policy.apex.core.engine.event.EnEvent;
34 import org.onap.policy.apex.core.engine.executor.Executor;
35 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
38 import org.onap.policy.apex.model.policymodel.concepts.AxState;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
41
42 /**
43  * Container class for the execution context for Task Selection logic executions in a task being executed in an Apex
44  * engine. The task must have easy access to the state definition, the incoming and outgoing event contexts, as well as
45  * the policy, global, and external context.
46  *
47  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
48  */
49 @Getter
50 public class TaskSelectionExecutionContext extends AbstractExecutionContext {
51     // Logger for task execution
52     private static final XLogger EXECUTION_LOGGER =
53             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskSelectionExecutionLogging");
54
55     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
56
57     /** A facade to the full state definition for the task selection logic being executed. */
58     public final AxStateFacade subject;
59
60     /**
61      * The incoming fields from the trigger event for the state. The task selection logic can access these fields to
62      * decide what task to select for the state.
63      */
64     public final Map<String, Object> inFields;
65
66     /**
67      * The task that the task selection logic has selected for a state. The task selection logic sets this field in its
68      * logic prior to executing and the Apex engine executes this task as the task for this state.
69      */
70     public final AxArtifactKey selectedTask;
71
72     /**
73      * Logger for task selection execution, task selection logic can use this field to access and log to Apex logging.
74      */
75     public final XLogger logger = EXECUTION_LOGGER;
76
77     // CHECKSTYLE:ON: checkstyle:VisibilityModifier
78
79     // All available context albums
80     private final Map<String, ContextAlbum> context;
81
82     /**
83      * Instantiates a new task selection execution context.
84      *
85      * @param taskSelectExecutor the task selection executor that requires context
86      * @param executionId the execution identifier
87      * @param axState the state definition that is the subject of execution
88      * @param incomingEvent the incoming event for the state
89      * @param outgoingKey the outgoing key for the task to execute in this state
90      * @param internalContext the execution context of the Apex engine in which the task is being executed
91      */
92     public TaskSelectionExecutionContext(final TaskSelectExecutor taskSelectExecutor, final long executionId,
93             final AxState axState, final EnEvent incomingEvent, final AxArtifactKey outgoingKey,
94             final ApexInternalContext internalContext) {
95         super(executionId, incomingEvent.getExecutionProperties());
96         // The subject is the state definition
97         subject = new AxStateFacade(axState);
98
99         // The events
100         inFields = incomingEvent;
101         selectedTask = outgoingKey;
102
103         // Set up the context albums for this task
104         // Set up the context albums for this task
105         context = new TreeMap<>();
106         for (final AxArtifactKey mapKey : subject.state.getContextAlbumReferences()) {
107             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
108         }
109
110         // Get the artifact stack of the users of the policy
111         final List<AxConcept> usedArtifactStack = new ArrayList<>();
112         for (Executor<?, ?, ?, ?> parent = taskSelectExecutor.getParent(); parent != null; parent =
113                 parent.getParent()) {
114             // Add each parent to the top of the stack
115             usedArtifactStack.add(0, parent.getKey());
116         }
117
118         // Add the events to the artifact stack
119         usedArtifactStack.add(incomingEvent.getKey());
120
121         // Change the stack to an array
122         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
123
124         // Set the user of the context
125         // Set the user of the context
126         for (final ContextAlbum contextAlbum : context.values()) {
127             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
128         }
129         incomingEvent.setUserArtifactStack(usedArtifactStackArray);
130     }
131
132     /**
133      * Return a context album if it exists in the context definition of this state.
134      *
135      * @param contextAlbumName The context album name
136      * @return The context albumxxxxxx
137      * @throws ContextRuntimeException if the context album does not exist on the state for this executor
138      */
139     public ContextAlbum getContextAlbum(final String contextAlbumName) {
140         // Find the context album
141         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
142
143         // Check if the context album exists
144         if (foundContextAlbum != null) {
145             return foundContextAlbum;
146         } else {
147             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
148                     + "\" on state \"" + subject.getId() + "\"");
149         }
150     }
151 }