Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / executor / TaskSelectExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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;
23
24 import static org.onap.policy.common.utils.validation.Assertions.argumentNotNull;
25
26 import java.util.Properties;
27 import lombok.NonNull;
28 import org.onap.policy.apex.context.ContextException;
29 import org.onap.policy.apex.core.engine.ExecutorParameters;
30 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
31 import org.onap.policy.apex.core.engine.event.EnEvent;
32 import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.policymodel.concepts.AxState;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
39
40 /**
41  * This abstract class executes a the task selection logic of a state of an Apex policy and is specialized by classes
42  * that implement execution of task selection logic.
43  *
44  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 public abstract class TaskSelectExecutor implements Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> {
48     // Logger for this class
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TaskSelectExecutor.class);
50
51     // Hold the state and context definitions for this task selector
52     private Executor<?, ?, ?, ?> parent = null;
53     private AxState axState = null;
54     private ApexInternalContext context = null;
55
56     // Holds the incoming event and outgoing task keys
57     private EnEvent incomingEvent = null;
58     private AxArtifactKey outgoingTaskKey = null;
59
60     // The next task selection executor
61     private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutor = null;
62
63     // The task selection execution context; contains the facades for events and context to be used
64     // by tasks executed by
65     // this task selection executor
66     private TaskSelectionExecutionContext executionContext;
67
68     /**
69      * Gets the execution context.
70      *
71      * @return the execution context
72      */
73     protected TaskSelectionExecutionContext getExecutionContext() {
74         return executionContext;
75     }
76
77     /**
78      * {@inheritDoc}.
79      */
80     @Override
81     public void setContext(final Executor<?, ?, ?, ?> newParent, final AxState newAxState,
82             final ApexInternalContext newContext) {
83         this.parent = newParent;
84         this.axState = newAxState;
85         this.context = newContext;
86     }
87
88     /**
89      * {@inheritDoc}.
90      */
91     @Override
92     public void prepare() throws StateMachineException {
93         LOGGER.debug("prepare:" + axState.getKey().getId() + "," + axState.getTaskSelectionLogic().getLogicFlavour()
94                 + "," + axState.getTaskSelectionLogic().getLogic());
95         argumentNotNull(axState.getTaskSelectionLogic().getLogic(), "task selection logic cannot be null.");
96     }
97
98     /**
99      * {@inheritDoc}.
100      */
101     @Override
102     public AxArtifactKey execute(final long executionId, final Properties executionProperties,
103             final EnEvent newIncomingEvent) throws StateMachineException, ContextException {
104         throw new StateMachineException("execute() not implemented on class");
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public final void executePre(final long executionId, @NonNull final Properties executionProperties,
112             final EnEvent newIncomingEvent) throws StateMachineException {
113         LOGGER.debug("execute-pre:" + axState.getKey().getId() + "," + axState.getTaskSelectionLogic().getLogicFlavour()
114                 + "," + axState.getTaskSelectionLogic().getLogic());
115
116         this.incomingEvent = newIncomingEvent;
117
118         // Initialize the returned task object so it can be set
119         outgoingTaskKey = new AxArtifactKey();
120
121         // Get task selection context object
122         executionContext = new TaskSelectionExecutionContext(this, executionId, getSubject(), getIncoming(),
123                 getOutgoing(), getContext());
124     }
125
126     /**
127      * {@inheritDoc}.
128      */
129     @Override
130     public final void executePost(final boolean returnValue) throws StateMachineException {
131         if (!returnValue) {
132             String errorMessage = "execute-post: task selection logic failed on state \"" + axState.getKey().getId()
133                     + "\"";
134             if (executionContext.getMessage() != null) {
135                 errorMessage += ", user message: " + executionContext.getMessage();
136             }
137             LOGGER.warn(errorMessage);
138             throw new StateMachineException(errorMessage);
139         }
140
141         if (outgoingTaskKey == null || AxArtifactKey.getNullKey().getName().equals(outgoingTaskKey.getName())) {
142             outgoingTaskKey = axState.getDefaultTask();
143             LOGGER.debug("execute-post:" + axState.getKey().getId() + ", returning default task");
144             return;
145         }
146
147         if (!axState.getTaskReferences().containsKey(outgoingTaskKey)) {
148             LOGGER.error("execute-post: task \"" + outgoingTaskKey.getId()
149                     + "\" returned by task selection logic not defined on state \"" + axState.getKey().getId() + "\"");
150             throw new StateMachineException("task \"" + outgoingTaskKey.getId()
151                     + "\" returned by task selection logic not defined on state \"" + axState.getKey().getId() + "\"");
152         }
153
154         LOGGER.debug("execute-post:" + axState.getKey().getId() + "," + ", returning task " + outgoingTaskKey.getId());
155     }
156
157     /**
158      * {@inheritDoc}.
159      */
160     @Override
161     public void cleanUp() throws StateMachineException {
162         throw new StateMachineException("cleanUp() not implemented on class");
163     }
164
165     /**
166      * {@inheritDoc}.
167      */
168     @Override
169     public AxReferenceKey getKey() {
170         return axState.getKey();
171     }
172
173     /**
174      * {@inheritDoc}.
175      */
176     @Override
177     public Executor<?, ?, ?, ?> getParent() {
178         return parent;
179     }
180
181     /**
182      * {@inheritDoc}.
183      */
184     @Override
185     public AxState getSubject() {
186         return axState;
187     }
188
189     /**
190      * {@inheritDoc}.
191      */
192     @Override
193     public ApexInternalContext getContext() {
194         return context;
195     }
196
197     /**
198      * {@inheritDoc}.
199      */
200     @Override
201     public void setNext(final Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> newNextExecutor) {
202         this.nextExecutor = newNextExecutor;
203     }
204
205     /**
206      * {@inheritDoc}.
207      */
208     @Override
209     public Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> getNext() {
210         return nextExecutor;
211     }
212
213     /**
214      * {@inheritDoc}.
215      */
216     @Override
217     public EnEvent getIncoming() {
218         return incomingEvent;
219     }
220
221     /**
222      * {@inheritDoc}.
223      */
224     @Override
225     public AxArtifactKey getOutgoing() {
226         return outgoingTaskKey;
227     }
228
229     /**
230      * {@inheritDoc}.
231      */
232     @Override
233     public void setParameters(final ExecutorParameters parameters) {
234         // Not used
235     }
236 }