6b2576d90f16aae8165b927a0578e9e1698bf2f3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 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.Set;
29 import java.util.TreeMap;
30 import lombok.Getter;
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.executor.Executor;
35 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
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 state finalizer logic executions in a state being executed in an Apex
44  * engine. The state finalizer must have easy access to the state definition, the fields, as well as the policy, global,
45  * and external context.
46  *
47  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
48  */
49 @Getter
50 public class StateFinalizerExecutionContext extends AbstractExecutionContext {
51     /**
52      * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
53      */
54     private static final XLogger EXCEUTION_LOGGER =
55             XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.StateFinalizerExecutionLogging");
56
57     // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field
58
59     /** A facade to the full state definition for the state finalizer logic being executed. */
60     public final AxStateFacade subject;
61
62     /**
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.
65      */
66     public final Set<String> stateOutputNames;
67
68     /**
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.
72      */
73     public final Map<String, Object> fields;
74
75     /**
76      * The state output that the state finalizer logic has selected for a state. The state finalizer logic sets this
77      * field in its logic after executing and the Apex engine uses this state output for this state.
78      */
79     private String selectedStateOutputName;
80
81     /**
82      * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
83      */
84     public final XLogger logger = EXCEUTION_LOGGER;
85
86     // CHECKSTYLE:ON: checkstyle:visibilityModifier
87
88     // All available context albums
89     private final Map<String, ContextAlbum> context;
90
91     // Execution properties for a policy execution
92     @Getter
93     private Properties executionProperties;
94
95     /**
96      * Instantiates a new state finalizer execution context.
97      *
98      * @param stateFinalizerExecutor the state finalizer executor that requires context
99      * @param executionId the execution ID for the current APEX policy execution instance
100      * @param executionProperties the execution properties for task execution
101      * @param axState the state definition that is the subject of execution
102      * @param fields the fields to be manipulated by the state finalizer
103      * @param stateOutputNames the state output names, one of which will be selected by the state finalizer
104      * @param internalContext the execution context of the Apex engine in which the task is being executed
105      */
106     public StateFinalizerExecutionContext(final StateFinalizerExecutor stateFinalizerExecutor, final long executionId,
107             final Properties executionProperties, final AxState axState, final Map<String, Object> fields,
108             final Set<String> stateOutputNames, final ApexInternalContext internalContext) {
109         super(executionId, executionProperties);
110
111         subject = new AxStateFacade(axState);
112
113         this.fields = fields;
114         this.stateOutputNames = stateOutputNames;
115
116         // Set up the context albums for this task
117         context = new TreeMap<>();
118         for (final AxArtifactKey mapKey : subject.state.getContextAlbumReferences()) {
119             context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
120         }
121
122         // Get the artifact stack of the users of the policy
123         final List<AxConcept> usedArtifactStack = new ArrayList<>();
124         for (Executor<?, ?, ?, ?> parent = stateFinalizerExecutor.getParent(); parent != null; parent =
125                 parent.getParent()) {
126             // Add each parent to the top of the stack
127             usedArtifactStack.add(0, parent.getKey());
128         }
129
130         // Change the stack to an array
131         final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);
132
133         // Set the user of the context
134         // Set the user of the context
135         for (final ContextAlbum contextAlbum : context.values()) {
136             contextAlbum.setUserArtifactStack(usedArtifactStackArray);
137         }
138     }
139
140     /**
141      * Return a context album if it exists in the context definition of this state.
142      *
143      * @param contextAlbumName The context album name
144      * @return The context album
145      * @throws ContextRuntimeException if the context album does not exist on the state for this executor
146      */
147     public ContextAlbum getContextAlbum(final String contextAlbumName) {
148         // Find the context album
149         final ContextAlbum foundContextAlbum = context.get(contextAlbumName);
150
151         // Check if the context album exists
152         if (foundContextAlbum != null) {
153             return foundContextAlbum;
154         } else {
155             throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
156                     + "\" on state \"" + subject.getId() + "\"");
157         }
158     }
159
160     /**
161      * Return the state output name selected by the state finalizer logic.
162      *
163      * @return the state output name
164      */
165     public String getSelectedStateOutputName() {
166         return selectedStateOutputName;
167     }
168
169     /**
170      * Set the state output name selected by the state finalizer logic.
171      *
172      * @param selectedStateOutputName the state output name
173      */
174     public void setSelectedStateOutputName(final String selectedStateOutputName) {
175         this.selectedStateOutputName = selectedStateOutputName;
176     }
177 }