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