aboutsummaryrefslogtreecommitdiffstats
path: root/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/StateFinalizerExecutionContext.java
blob: 12e3503901e2b7148c7a46822016079da5abf636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.core.engine.executor.context;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import lombok.Getter;
import lombok.Setter;
import org.onap.policy.apex.context.ContextAlbum;
import org.onap.policy.apex.context.ContextRuntimeException;
import org.onap.policy.apex.core.engine.context.ApexInternalContext;
import org.onap.policy.apex.core.engine.executor.Executor;
import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.policymodel.concepts.AxState;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * Container class for the execution context for state finalizer logic executions in a state being executed in an Apex
 * engine. The state finalizer must have easy access to the state definition, the fields, as well as the policy, global,
 * and external context.
 *
 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
 */
@Getter
public class StateFinalizerExecutionContext extends AbstractExecutionContext {
    /**
     * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
     */
    private static final XLogger EXCEUTION_LOGGER =
            XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.StateFinalizerExecutionLogging");

    // CHECKSTYLE:OFF: checkstyle:VisibilityModifier Logic has access to these field

    /** A facade to the full state definition for the state finalizer logic being executed. */
    public final AxStateFacade subject;

    /**
     * The list of state outputs for this state finalizer. The purpose of a state finalizer is to select a state output
     * for a state from this list of state output names.
     */
    public final Set<String> stateOutputNames;

    /**
     * The fields of this state finalizer. A state finalizer receives this list of fields from a task and may use these
     * fields to determine what state output to select. Once a state finalizer has selected a state output, it must
     * marshal these fields so that they match the fields required for the event defined in the state output.
     */
    public final Map<String, Object> fields;

    /**
     * The state output that the state finalizer logic has selected for a state. The state finalizer logic sets this
     * field in its logic after executing and the Apex engine uses this state output for this state.
     */
    @Getter
    @Setter
    private String selectedStateOutputName;

    /**
     * Logger for state finalizer execution, state finalizer logic can use this field to access and log to Apex logging.
     */
    public final XLogger logger = EXCEUTION_LOGGER;

    // CHECKSTYLE:ON: checkstyle:visibilityModifier

    // All available context albums
    private final Map<String, ContextAlbum> context;

    // Execution properties for a policy execution
    @Getter
    private Properties executionProperties;

    /**
     * Instantiates a new state finalizer execution context.
     *
     * @param stateFinalizerExecutor the state finalizer executor that requires context
     * @param executionId the execution ID for the current APEX policy execution instance
     * @param executionProperties the execution properties for task execution
     * @param axState the state definition that is the subject of execution
     * @param fields the fields to be manipulated by the state finalizer
     * @param stateOutputNames the state output names, one of which will be selected by the state finalizer
     * @param internalContext the execution context of the Apex engine in which the task is being executed
     */
    public StateFinalizerExecutionContext(final StateFinalizerExecutor stateFinalizerExecutor, final long executionId,
            final Properties executionProperties, final AxState axState, final Map<String, Object> fields,
            final Set<String> stateOutputNames, final ApexInternalContext internalContext) {
        super(executionId, executionProperties);

        subject = new AxStateFacade(axState);

        this.fields = fields;
        this.stateOutputNames = stateOutputNames;

        // Set up the context albums for this task
        context = new TreeMap<>();
        for (final AxArtifactKey mapKey : subject.state.getContextAlbumReferences()) {
            context.put(mapKey.getName(), internalContext.getContextAlbums().get(mapKey));
        }

        // Get the artifact stack of the users of the policy
        final List<AxConcept> usedArtifactStack = new ArrayList<>();
        for (Executor<?, ?, ?, ?> parent = stateFinalizerExecutor.getParent(); parent != null; parent =
                parent.getParent()) {
            // Add each parent to the top of the stack
            usedArtifactStack.add(0, parent.getKey());
        }

        // Change the stack to an array
        final AxConcept[] usedArtifactStackArray = usedArtifactStack.toArray(new AxConcept[usedArtifactStack.size()]);

        // Set the user of the context
        // Set the user of the context
        for (final ContextAlbum contextAlbum : context.values()) {
            contextAlbum.setUserArtifactStack(usedArtifactStackArray);
        }
    }

    /**
     * Return a context album if it exists in the context definition of this state.
     *
     * @param contextAlbumName The context album name
     * @return The context album
     * @throws ContextRuntimeException if the context album does not exist on the state for this executor
     */
    public ContextAlbum getContextAlbum(final String contextAlbumName) {
        // Find the context album
        final var foundContextAlbum = context.get(contextAlbumName);

        // Check if the context album exists
        if (foundContextAlbum != null) {
            return foundContextAlbum;
        } else {
            throw new ContextRuntimeException("cannot find definition of context album \"" + contextAlbumName
                    + "\" on state \"" + subject.getId() + "\"");
        }
    }
}