2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.oam.processor;
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;
41 import java.util.Date;
42 import java.util.concurrent.Future;
43 import java.util.concurrent.TimeUnit;
44 import java.util.concurrent.TimeoutException;
47 * Base processor for OAM APIs, such as maintenance mode, restart, start and stop API.
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.
52 public abstract class BaseProcessor extends BaseCommon {
53 /** lock to serialize incoming OAM operations. */
54 private static final Object LOCK = new Object();
56 final AsyncTaskHelper asyncTaskHelper;
57 final BundleHelper bundleHelper;
59 /** the requestTimeoutSeconds to use for this OAM operation */
60 private Integer requestTimeoutSeconds;
62 BaseActionRunnable runnable;
63 private Future<?> scheduledRunnable = null;
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
74 BaseProcessor(EELFLogger eelfLogger,
75 ConfigurationHelper configurationHelperIn,
76 StateHelper stateHelperIn,
77 AsyncTaskHelper asyncTaskHelperIn,
78 OperationHelper operationHelperIn) {
79 super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
81 asyncTaskHelper = asyncTaskHelperIn;
82 bundleHelper = new BundleHelper(eelfLogger, configurationHelper, stateHelper);
86 * Process synch handling and schedule asynch task
88 * @param requestInput of REST API request
89 * @return Status of new APP-C OAM state
91 public Status processRequest(final Object requestInput) {
92 startTime = new Date();
93 commonHeader = operationHelper.getCommonHeader(requestInput);
94 setStatus(OAMCommandStatus.ACCEPTED);
97 preProcess(requestInput);
99 } catch (Exception e) {
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
115 * @throws InvalidInputException when commonHeader validation failed
116 * @throws APPCException when state validation failed
118 protected void preProcess(final Object requestInput)
119 throws InvalidInputException, APPCException, InvalidStateException,InterruptedException,TimeoutException {
120 setInitialLogProperties();
121 operationHelper.isInputValid(requestInput);
123 //The OAM request may specify timeout value
124 requestTimeoutSeconds = operationHelper.getParamRequestTimeout(requestInput);
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.
130 AppcOamStates currentOamState = stateHelper.getCurrentOamState();
132 //make sure this OAM operation can transition to the desired OAM operation
133 AppcOamStates nextState = operationHelper.getNextState(
134 rpc.getAppcOperation(), currentOamState);
136 stateHelper.setState(nextState);
140 //cancel the BaseActionRunnable currently executing
141 //it got to be completely terminated before proceeding
142 asyncTaskHelper.cancelBaseActionRunnable(
145 getTimeoutMilliseconds(),
146 TimeUnit.MILLISECONDS
148 } catch (TimeoutException e) {
149 stateHelper.setState(AppcOamStates.Error);
158 * Post process includes audit logging as well as clear MDC properties.
160 private void postProcess() {
161 auditInfoLog(auditMsg);
162 clearRequestLogProperties();
166 * Schedule async task through AsyncTaskHelper.
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()));
175 scheduledRunnable = asyncTaskHelper.scheduleBaseRunnable(
176 runnable, runnable::abortRunnable, getInitialDelayMillis(), getDelayMillis());
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.
185 long getTimeoutMilliseconds() {
186 return configurationHelper.getOAMOperationTimeoutValue(this.requestTimeoutSeconds);
191 * @return initialDelayMillis - the time to delay first execution of {@link BaseActionRunnable}
193 protected long getInitialDelayMillis(){
198 * @return delayMillis the delay between the consecutive executions of {@link BaseActionRunnable}
200 private long getDelayMillis(){
205 * Cancel the scheduled {@link BaseActionRunnable} through AsyncTaskHelper
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()));
213 scheduledRunnable.cancel(true);