[APPC-144] OAM operation abort messages
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / BaseProcessor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.oam.processor;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.appc.exceptions.InvalidInputException;
31 import org.openecomp.appc.exceptions.InvalidStateException;
32 import org.openecomp.appc.i18n.Msg;
33 import org.openecomp.appc.oam.OAMCommandStatus;
34 import org.openecomp.appc.oam.util.AsyncTaskHelper;
35 import org.openecomp.appc.oam.util.BundleHelper;
36 import org.openecomp.appc.oam.util.ConfigurationHelper;
37 import org.openecomp.appc.oam.util.OperationHelper;
38 import org.openecomp.appc.oam.util.StateHelper;
39 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
40
41 import java.util.Date;
42 import java.util.concurrent.Future;
43
44 /**
45  * Base processor for OAM APIs, such as maintenance mode, restart, start and stop API.
46  *
47  * <p>This class holds the general API request sync handling methods for all OAM APIs.
48  * <p>Specific API processor will overwrite the general methods to add specific behaviors.
49  */
50 public abstract class BaseProcessor extends BaseCommon {
51     final AsyncTaskHelper asyncTaskHelper;
52     final BundleHelper bundleHelper;
53
54
55     Integer timeoutSeconds;
56     Msg auditMsg;
57     BaseActionRunnable runnable;
58     private Future<?> scheduledRunnable = null;
59
60     /**
61      * Constructor
62      *
63      * @param eelfLogger for logging
64      * @param configurationHelperIn for property reading
65      * @param stateHelperIn for APP-C OAM state checking
66      * @param asyncTaskHelperIn for scheduling async task
67      * @param operationHelperIn for operational helper
68      */
69     BaseProcessor(EELFLogger eelfLogger,
70                   ConfigurationHelper configurationHelperIn,
71                   StateHelper stateHelperIn,
72                   AsyncTaskHelper asyncTaskHelperIn,
73                   OperationHelper operationHelperIn) {
74         super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
75
76         asyncTaskHelper = asyncTaskHelperIn;
77         bundleHelper = new BundleHelper(eelfLogger, configurationHelper, stateHelper);
78     }
79
80     /**
81      * Process synch handling and schedule asynch task
82      *
83      * @param requestInput of REST API request
84      * @return Status of new APP-C OAM state
85      */
86     public Status processRequest(final Object requestInput) {
87         startTime = new Date();
88         commonHeader = operationHelper.getCommonHeader(requestInput);
89         setStatus(OAMCommandStatus.ACCEPTED);
90
91         try {
92             preProcess(requestInput);
93             timeoutSeconds = operationHelper.getParamRequestTimeout(requestInput);
94             scheduleAsyncTask();
95         } catch (Throwable t) {
96             setErrorStatus(t);
97         } finally {
98             postProcess();
99         }
100
101         return status;
102     }
103
104     /**
105      * Preprocess before actual handling of the REST API call. Does:
106      * <p> - commonHeader validation
107      * <p> - get NextState as well as validate if next state is valid
108      * <p> - set logging properties
109      * <p> - set appcCurrentState to next state
110      *
111      * @throws InvalidInputException when commonHeader validation failed
112      * @throws APPCException         when state validation failed
113      */
114     protected void preProcess(final Object requestInput)
115             throws InvalidInputException, APPCException, InvalidStateException {
116         operationHelper.isInputValid(requestInput);
117
118         AppcOamStates nextState = operationHelper.getNextState(
119                 rpc.getAppcOperation(), stateHelper.getCurrentOamState());
120         setInitialLogProperties();
121         stateHelper.setState(nextState);
122     }
123
124     /**
125      * Post process includes audit logging as well as clear MDC properties.
126      */
127     private void postProcess() {
128         auditInfoLog(auditMsg);
129         clearRequestLogProperties();
130     }
131
132     /**
133      * Schedule async task through AsyncTaskHelper.
134      */
135     protected void scheduleAsyncTask() {
136         if (runnable == null) {
137             logger.error(String.format(
138                     "Skipped schedule async task for rpc(%s) due to runnable is null", rpc.name()));
139             return;
140         }
141
142         scheduledRunnable = asyncTaskHelper.scheduleAsyncTask(rpc, runnable);
143     }
144
145     /**
146      * Check if current running task is the same as schedule task
147      * @return true if they are the same, otherwise false.
148      */
149     boolean isSameAsyncTask() {
150         return asyncTaskHelper.getCurrentAsyncTask() == scheduledRunnable;
151     }
152
153     /**
154      * Cancel schedueled async task through AsyncTaskHelper
155      */
156     void cancelAsyncTask() {
157         if (scheduledRunnable == null) {
158             logger.error(String.format(
159                     "Skipped cancel schedule async task for rpc(%s) due to scheduledRunnable is null", rpc.name()));
160             return;
161         }
162
163         asyncTaskHelper.cancelAsyncTask(scheduledRunnable);
164         scheduledRunnable = null;
165     }
166 }