414b946056ce78e2d15622d42c819dc74b05488d
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / onap / 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.onap.appc.oam.processor;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
29 import org.onap.appc.exceptions.APPCException;
30 import org.onap.appc.exceptions.InvalidInputException;
31 import org.onap.appc.exceptions.InvalidStateException;
32 import org.onap.appc.i18n.Msg;
33 import org.onap.appc.oam.OAMCommandStatus;
34 import org.onap.appc.oam.util.AsyncTaskHelper;
35 import org.onap.appc.oam.util.BundleHelper;
36 import org.onap.appc.oam.util.ConfigurationHelper;
37 import org.onap.appc.oam.util.OperationHelper;
38 import org.onap.appc.oam.util.StateHelper;
39 import org.onap.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             scheduleAsyncTask();
99         } catch (Exception e) {
100             setErrorStatus(e);
101         } finally {
102             postProcess();
103         }
104
105         return status;
106     }
107
108     /**
109      * Preprocess before actual handling of the REST API call. Does:
110      * <p> - commonHeader validation
111      * <p> - get NextState as well as validate if next state is valid
112      * <p> - set logging properties
113      * <p> - set appcCurrentState to next state
114      *
115      * @throws InvalidInputException when commonHeader validation failed
116      * @throws APPCException         when state validation failed
117      */
118     protected void preProcess(final Object requestInput)
119         throws InvalidInputException, APPCException, InvalidStateException,InterruptedException,TimeoutException {
120         setInitialLogProperties();
121         operationHelper.isInputValid(requestInput);
122
123         //The OAM request may specify timeout value
124         requestTimeoutSeconds = operationHelper.getParamRequestTimeout(requestInput);
125
126         //All OAM operation pass through here first to validate if an OAM state change is allowed.
127         //If a state change is allowed cancel the occurring OAM (if any) before starting this one.
128         //we will synchronized so that only one can do this at any given time.
129         synchronized(LOCK) {
130             AppcOamStates currentOamState = stateHelper.getCurrentOamState();
131
132             //make sure this OAM operation can transition to the desired OAM operation
133             AppcOamStates nextState = operationHelper.getNextState(
134                     rpc.getAppcOperation(), currentOamState);
135
136             stateHelper.setState(nextState);
137
138
139             try {
140                 //cancel the  BaseActionRunnable currently executing
141                 //it got to be completely terminated before proceeding
142                 asyncTaskHelper.cancelBaseActionRunnable(
143                         rpc,
144                         currentOamState,
145                         getTimeoutMilliseconds(),
146                         TimeUnit.MILLISECONDS
147                 );
148             } catch (TimeoutException e) {
149                 stateHelper.setState(AppcOamStates.Error);
150                 throw e;
151             }
152
153
154         }
155     }
156
157     /**
158      * Post process includes audit logging as well as clear MDC properties.
159      */
160     private void postProcess() {
161         auditInfoLog(auditMsg);
162         clearRequestLogProperties();
163     }
164
165     /**
166      * Schedule async task through AsyncTaskHelper.
167      */
168     protected void scheduleAsyncTask() {
169         if (runnable == null) {
170             logger.error(String.format(
171                 "Skipped schedule async task for rpc(%s) due to runnable is null", rpc.name()));
172             return;
173         }
174
175         scheduledRunnable = asyncTaskHelper.scheduleBaseRunnable(
176             runnable, runnable::abortRunnable, getInitialDelayMillis(), getDelayMillis());
177     }
178
179
180     /**
181      * The timeout for this OAM operation. The timeout source is chosen in the following order:
182      * request, config file, default value
183      * @return  - the timeout for this OAM operation.
184      */
185     long getTimeoutMilliseconds() {
186         return configurationHelper.getOAMOperationTimeoutValue(this.requestTimeoutSeconds);
187     }
188
189
190     /**
191      * @return initialDelayMillis - the time to delay first execution of {@link BaseActionRunnable}
192      */
193     protected long getInitialDelayMillis(){
194         return 0L;
195     }
196
197     /**
198      * @return delayMillis the delay between the consecutive executions of  {@link BaseActionRunnable}
199      */
200     private long getDelayMillis(){
201         return 1000L;
202     }
203
204     /**
205      * Cancel the scheduled {@link BaseActionRunnable}  through AsyncTaskHelper
206      */
207     void cancelAsyncTask() {
208         if (scheduledRunnable == null) {
209             logger.error(String.format(
210                 "Skipped cancel schedule async task for rpc(%s) due to scheduledRunnable is null", rpc.name()));
211             return;
212         }
213         scheduledRunnable.cancel(true);
214     }
215
216 }