serializing OAM async task
[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 import java.util.concurrent.TimeUnit;
44 import java.util.concurrent.TimeoutException;
45
46 /**
47  * Base processor for OAM APIs, such as maintenance mode, restart, start and stop API.
48  *
49  * <p>This class holds the general API request sync handling methods for all OAM APIs.
50  * <p>Specific API processor will overwrite the general methods to add specific behaviors.
51  */
52 public abstract class BaseProcessor extends BaseCommon {
53     /** lock to serialize incoming OAM operations.  */
54     private static final Object LOCK = new Object();
55
56     final AsyncTaskHelper asyncTaskHelper;
57     final BundleHelper bundleHelper;
58
59     /** the requestTimeoutSeconds to use for this OAM operation */
60     private Integer requestTimeoutSeconds;
61     Msg auditMsg;
62     BaseActionRunnable runnable;
63     private Future<?> scheduledRunnable = null;
64
65     /**
66      * Constructor
67      *
68      * @param eelfLogger for logging
69      * @param configurationHelperIn for property reading
70      * @param stateHelperIn for APP-C OAM state checking
71      * @param asyncTaskHelperIn for scheduling async task
72      * @param operationHelperIn for operational helper
73      */
74     BaseProcessor(EELFLogger eelfLogger,
75                   ConfigurationHelper configurationHelperIn,
76                   StateHelper stateHelperIn,
77                   AsyncTaskHelper asyncTaskHelperIn,
78                   OperationHelper operationHelperIn) {
79         super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
80
81         asyncTaskHelper = asyncTaskHelperIn;
82         bundleHelper = new BundleHelper(eelfLogger, configurationHelper, stateHelper);
83     }
84
85     /**
86      * Process synch handling and schedule asynch task
87      *
88      * @param requestInput of REST API request
89      * @return Status of new APP-C OAM state
90      */
91     public Status processRequest(final Object requestInput) {
92         startTime = new Date();
93         commonHeader = operationHelper.getCommonHeader(requestInput);
94         setStatus(OAMCommandStatus.ACCEPTED);
95
96         try {
97             preProcess(requestInput);
98             //The OAM request may specify timeout value
99             requestTimeoutSeconds = operationHelper.getParamRequestTimeout(requestInput);
100             scheduleAsyncTask();
101         } catch (Exception e) {
102             setErrorStatus(e);
103         } finally {
104             postProcess();
105         }
106
107         return status;
108     }
109
110     /**
111      * Preprocess before actual handling of the REST API call. Does:
112      * <p> - commonHeader validation
113      * <p> - get NextState as well as validate if next state is valid
114      * <p> - set logging properties
115      * <p> - set appcCurrentState to next state
116      *
117      * @throws InvalidInputException when commonHeader validation failed
118      * @throws APPCException         when state validation failed
119      */
120     protected void preProcess(final Object requestInput)
121         throws InvalidInputException, APPCException, InvalidStateException,InterruptedException,TimeoutException {
122         setInitialLogProperties();
123         operationHelper.isInputValid(requestInput);
124
125         //All OAM operation pass through here first to validate if an OAM state change is allowed.
126         //If a state change is allowed cancel the occurring OAM (if any) before starting this one.
127         //we will synchronized so that only one can do this at any given time.
128         synchronized(LOCK) {
129             AppcOamStates currentOamState = stateHelper.getCurrentOamState();
130
131             //make sure this OAM operation can transition to the desired OAM operation
132             AppcOamStates nextState = operationHelper.getNextState(
133                     rpc.getAppcOperation(), currentOamState);
134
135             stateHelper.setState(nextState);
136
137             //cancel the  BaseActionRunnable currently executing
138             //it got to be completely terminated before proceeding
139             asyncTaskHelper.cancelBaseActionRunnable(
140                     rpc,
141                     currentOamState,
142                     getTimeoutMilliseconds(),
143                     TimeUnit.MILLISECONDS
144             );
145         }
146     }
147
148     /**
149      * Post process includes audit logging as well as clear MDC properties.
150      */
151     private void postProcess() {
152         auditInfoLog(auditMsg);
153         clearRequestLogProperties();
154     }
155
156     /**
157      * Schedule async task through AsyncTaskHelper.
158      */
159     protected void scheduleAsyncTask() {
160         if (runnable == null) {
161             logger.error(String.format(
162                 "Skipped schedule async task for rpc(%s) due to runnable is null", rpc.name()));
163             return;
164         }
165
166         scheduledRunnable = asyncTaskHelper.scheduleBaseRunnable(
167             runnable, runnable::abortRunnable, getInitialDelayMillis(), getDelayMillis());
168     }
169
170
171     /**
172      * The timeout for this OAM operation. The timeout source is chosen in the following order:
173      * request, config file, default value
174      * @return  - the timeout for this OAM operation.
175      */
176     long getTimeoutMilliseconds() {
177         return configurationHelper.getOAMOperationTimeoutValue(this.requestTimeoutSeconds);
178     }
179
180
181     /**
182      * @return initialDelayMillis - the time to delay first execution of {@link BaseActionRunnable}
183      */
184     protected long getInitialDelayMillis(){
185         return 0L;
186     }
187
188     /**
189      * @return delayMillis the delay between the consecutive executions of  {@link BaseActionRunnable}
190      */
191     private long getDelayMillis(){
192         return 1000L;
193     }
194
195     /**
196      * Cancel the scheduled {@link BaseActionRunnable}  through AsyncTaskHelper
197      */
198     void cancelAsyncTask() {
199         if (scheduledRunnable == null) {
200             logger.error(String.format(
201                 "Skipped cancel schedule async task for rpc(%s) due to scheduledRunnable is null", rpc.name()));
202             return;
203         }
204         scheduledRunnable.cancel(true);
205     }
206
207 }