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.openecomp.appc.oam.processor;
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;
41 import java.util.Date;
42 import java.util.concurrent.Future;
45 * Base processor for OAM APIs, such as maintenance mode, restart, start and stop API.
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.
50 public abstract class BaseProcessor extends BaseCommon {
51 final AsyncTaskHelper asyncTaskHelper;
52 final BundleHelper bundleHelper;
55 Integer timeoutSeconds;
57 BaseActionRunnable runnable;
58 private Future<?> scheduledRunnable = null;
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
69 BaseProcessor(EELFLogger eelfLogger,
70 ConfigurationHelper configurationHelperIn,
71 StateHelper stateHelperIn,
72 AsyncTaskHelper asyncTaskHelperIn,
73 OperationHelper operationHelperIn) {
74 super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
76 asyncTaskHelper = asyncTaskHelperIn;
77 bundleHelper = new BundleHelper(eelfLogger, configurationHelper, stateHelper);
81 * Process synch handling and schedule asynch task
83 * @param requestInput of REST API request
84 * @return Status of new APP-C OAM state
86 public Status processRequest(final Object requestInput) {
87 startTime = new Date();
88 commonHeader = operationHelper.getCommonHeader(requestInput);
89 setStatus(OAMCommandStatus.ACCEPTED);
92 preProcess(requestInput);
93 timeoutSeconds = operationHelper.getParamRequestTimeout(requestInput);
95 } catch (Exception e) {
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
111 * @throws InvalidInputException when commonHeader validation failed
112 * @throws APPCException when state validation failed
114 protected void preProcess(final Object requestInput)
115 throws InvalidInputException, APPCException, InvalidStateException {
116 operationHelper.isInputValid(requestInput);
118 AppcOamStates nextState = operationHelper.getNextState(
119 rpc.getAppcOperation(), stateHelper.getCurrentOamState());
120 setInitialLogProperties();
121 stateHelper.setState(nextState);
125 * Post process includes audit logging as well as clear MDC properties.
127 private void postProcess() {
128 auditInfoLog(auditMsg);
129 clearRequestLogProperties();
133 * Schedule async task through AsyncTaskHelper.
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()));
142 scheduledRunnable = asyncTaskHelper.scheduleAsyncTask(rpc, runnable);
146 * Check if current running task is the same as schedule task
147 * @return true if they are the same, otherwise false.
149 boolean isSameAsyncTask() {
150 return asyncTaskHelper.getCurrentAsyncTask() == scheduledRunnable;
154 * Cancel schedueled async task through AsyncTaskHelper
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()));
163 asyncTaskHelper.cancelAsyncTask(scheduledRunnable);
164 scheduledRunnable = null;