2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.executor.impl;
28 import java.util.Date;
29 import java.util.concurrent.TimeUnit;
31 import org.apache.commons.lang.ObjectUtils;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.appc.executionqueue.ExecutionQueueService;
34 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
35 import org.openecomp.appc.executor.CommandExecutor;
36 import org.openecomp.appc.executor.objects.CommandExecutorInput;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
41 public class CommandExecutorImpl implements CommandExecutor {
43 private CommandTaskFactory executionTaskFactory ;
44 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
46 private ExecutionQueueService executionQueueService;
47 private ExpiredMessageHandler expiredMessageHandler;
49 public CommandExecutorImpl(){
53 public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
54 this.executionQueueService = executionQueueService;
57 public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
58 this.expiredMessageHandler = expiredMessageHandler;
61 public void initialize() {
62 logger.info("initialization started of CommandExecutorImpl");
63 executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
64 executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
67 public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
68 this.executionTaskFactory = executionTaskFactory;
72 * Execute given command
73 * Create command request and enqueue it for execution.
74 * @param commandExecutorInput Contains CommandHeader, command , target Id , payload and conf ID (optional)
75 * @throws APPCException in case of error.
78 public void executeCommand (CommandExecutorInput commandExecutorInput) throws APPCException{
79 if (logger.isTraceEnabled()) {
80 logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
82 enqueRequest(commandExecutorInput);
83 if (logger.isTraceEnabled()) {
84 logger.trace("Exiting from executeCommand");
88 @SuppressWarnings("unchecked")
89 private void enqueRequest(CommandExecutorInput request) throws APPCException{
90 if (logger.isTraceEnabled()) {
91 logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
94 CommandTask commandTask = getMessageExecutor(request.getRuntimeContext().getRequestContext().getAction().name());
95 commandTask.setCommandRequest(request);
96 long remainingTTL = getRemainingTTL(request);
97 executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
98 } catch (Exception e) {
99 logger.error("Exception: "+e.getMessage());
100 throw new APPCException(e);
103 if (logger.isTraceEnabled()) {
104 logger.trace("Exiting from enqueRequest");
108 private long getRemainingTTL(CommandExecutorInput request) {
109 Date requestTimestamp = request.getRuntimeContext().getRequestContext().getCommonHeader().getTimeStamp();
110 int ttl = request.getRuntimeContext().getRequestContext().getCommonHeader().getFlags().getTtl();
111 return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
114 private CommandTask getMessageExecutor(String action){
115 if (logger.isTraceEnabled()) {
116 logger.trace("Entering to getMessageExecutor with command = "+ action);
118 CommandTask executionTask = executionTaskFactory.getExecutionTask(action);
119 if (logger.isTraceEnabled()) {
120 logger.trace("Exiting from getMessageExecutor");
122 return executionTask;