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