aboutsummaryrefslogtreecommitdiffstats
path: root/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/TaskSelectExecutor.java
blob: 3f360ea9cf833fdf861728a450bf2cdd03f0b934 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * 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;

import static org.onap.policy.common.utils.validation.Assertions.argumentNotNull;

import java.util.Properties;

import lombok.NonNull;

import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.core.engine.ExecutorParameters;
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.context.TaskSelectionExecutionContext;
import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
import org.onap.policy.apex.model.policymodel.concepts.AxState;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * This abstract class executes a the task selection logic of a state of an Apex policy and is specialized by classes
 * that implement execution of task selection logic.
 *
 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public abstract class TaskSelectExecutor implements Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> {
    // Logger for this class
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(TaskSelectExecutor.class);

    // Hold the state and context definitions for this task selector
    private Executor<?, ?, ?, ?> parent = null;
    private AxState axState = null;
    private ApexInternalContext context = null;

    // Holds the incoming event and outgoing task keys
    private EnEvent incomingEvent = null;
    private AxArtifactKey outgoingTaskKey = null;

    // The next task selection executor
    private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutor = null;

    // The task selection execution context; contains the facades for events and context to be used
    // by tasks executed by
    // this task selection executor
    private TaskSelectionExecutionContext executionContext;

    /**
     * Gets the execution context.
     *
     * @return the execution context
     */
    protected TaskSelectionExecutionContext getExecutionContext() {
        return executionContext;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void setContext(final Executor<?, ?, ?, ?> newParent, final AxState newAxState,
            final ApexInternalContext newContext) {
        this.parent = newParent;
        this.axState = newAxState;
        this.context = newContext;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void prepare() throws StateMachineException {
        LOGGER.debug("prepare:" + axState.getKey().getId() + "," + axState.getTaskSelectionLogic().getLogicFlavour()
                + "," + axState.getTaskSelectionLogic().getLogic());
        argumentNotNull(axState.getTaskSelectionLogic().getLogic(), "task selection logic cannot be null.");
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxArtifactKey execute(final long executionId, final Properties executionProperties,
            final EnEvent newIncomingEvent) throws StateMachineException, ContextException {
        throw new StateMachineException("execute() not implemented on class");
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public final void executePre(final long executionId, @NonNull final Properties executionProperties,
            final EnEvent newIncomingEvent) throws StateMachineException {
        LOGGER.debug("execute-pre:" + axState.getKey().getId() + "," + axState.getTaskSelectionLogic().getLogicFlavour()
                + "," + axState.getTaskSelectionLogic().getLogic());

        this.incomingEvent = newIncomingEvent;

        // Initialize the returned task object so it can be set
        outgoingTaskKey = new AxArtifactKey();

        // Get task selection context object
        executionContext = new TaskSelectionExecutionContext(this, executionId, getSubject(), getIncoming(),
                getOutgoing(), getContext());
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public final void executePost(final boolean returnValue) throws StateMachineException {
        if (!returnValue) {
            String errorMessage = "execute-post: task selection logic failed on state \"" + axState.getKey().getId()
                    + "\"";
            if (executionContext.getMessage() != null) {
                errorMessage += ", user message: " + executionContext.getMessage();
            }
            LOGGER.warn(errorMessage);
            throw new StateMachineException(errorMessage);
        }

        if (outgoingTaskKey == null || AxArtifactKey.getNullKey().getName().equals(outgoingTaskKey.getName())) {
            outgoingTaskKey = axState.getDefaultTask();
            LOGGER.debug("execute-post:" + axState.getKey().getId() + ", returning default task");
            return;
        }

        if (!axState.getTaskReferences().containsKey(outgoingTaskKey)) {
            LOGGER.error("execute-post: task \"" + outgoingTaskKey.getId()
                    + "\" returned by task selection logic not defined on state \"" + axState.getKey().getId() + "\"");
            throw new StateMachineException("task \"" + outgoingTaskKey.getId()
                    + "\" returned by task selection logic not defined on state \"" + axState.getKey().getId() + "\"");
        }

        LOGGER.debug("execute-post:" + axState.getKey().getId() + "," + ", returning task " + outgoingTaskKey.getId());
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void cleanUp() throws StateMachineException {
        throw new StateMachineException("cleanUp() not implemented on class");
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxReferenceKey getKey() {
        return axState.getKey();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Executor<?, ?, ?, ?> getParent() {
        return parent;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxState getSubject() {
        return axState;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public ApexInternalContext getContext() {
        return context;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void setNext(final Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> newNextExecutor) {
        this.nextExecutor = newNextExecutor;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> getNext() {
        return nextExecutor;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public EnEvent getIncoming() {
        return incomingEvent;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxArtifactKey getOutgoing() {
        return outgoingTaskKey;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void setParameters(final ExecutorParameters parameters) {
        // Not used
    }
}