aboutsummaryrefslogtreecommitdiffstats
path: root/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskSelectionExecutionContext.java
blob: 69d51c45a6fb283ddaab0202c0c158024a150fb7 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.TreeMap;
import lombok.Getter;
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.event.EnEvent;
import org.onap.policy.apex.core.engine.executor.Executor;
import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
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 Task Selection logic executions in a task being executed in an Apex
 * engine. The task must have easy access to the state definition, the incoming and outgoing event contexts, as well as
 * the policy, global, and external context.
 *
 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
 */
@Getter
public class TaskSelectionExecutionContext extends AbstractExecutionContext {
    // Logger for task execution
    private static final XLogger EXECUTION_LOGGER =
            XLoggerFactory.getXLogger("org.onap.policy.apex.executionlogging.TaskSelectionExecutionLogging");

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

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

    /**
     * The incoming fields from the trigger event for the state. The task selection logic can access these fields to
     * decide what task to select for the state.
     */
    public final Map<String, Object> inFields;

    /**
     * The task that the task selection logic has selected for a state. The task selection logic sets this field in its
     * logic prior to executing and the Apex engine executes this task as the task for this state.
     */
    public final AxArtifactKey selectedTask;

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

    // CHECKSTYLE:ON: checkstyle:VisibilityModifier

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

    /**
     * Instantiates a new task selection execution context.
     *
     * @param taskSelectExecutor the task selection executor that requires context
     * @param executionId the execution identifier
     * @param axState the state definition that is the subject of execution
     * @param incomingEvent the incoming event for the state
     * @param outgoingKey the outgoing key for the task to execute in this state
     * @param internalContext the execution context of the Apex engine in which the task is being executed
     */
    public TaskSelectionExecutionContext(final TaskSelectExecutor taskSelectExecutor, final long executionId,
            final AxState axState, final EnEvent incomingEvent, final AxArtifactKey outgoingKey,
            final ApexInternalContext internalContext) {
        super(executionId, incomingEvent.getExecutionProperties());
        // The subject is the state definition
        subject = new AxStateFacade(axState);

        // The events
        inFields = incomingEvent;
        selectedTask = outgoingKey;

        // Set up the context albums for this task
        // 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 = taskSelectExecutor.getParent(); parent != null; parent =
                parent.getParent()) {
            // Add each parent to the top of the stack
            usedArtifactStack.add(0, parent.getKey());
        }

        // Add the events to the artifact stack
        usedArtifactStack.add(incomingEvent.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);
        }
        incomingEvent.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 albumxxxxxx
     * @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 ContextAlbum 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() + "\"");
        }
    }
}