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.domainmodel.lcm.RuntimeContext;
33 import org.openecomp.appc.exceptions.APPCException;
34 import org.openecomp.appc.executionqueue.ExecutionQueueService;
35 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
36 import org.openecomp.appc.executor.CommandExecutor;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
42 public class CommandExecutorImpl implements CommandExecutor {
44 private CommandTaskFactory executionTaskFactory ;
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
47 private ExecutionQueueService executionQueueService;
48 private ExpiredMessageHandler expiredMessageHandler;
50 public CommandExecutorImpl(){
54 public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
55 this.executionQueueService = executionQueueService;
58 public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
59 this.expiredMessageHandler = expiredMessageHandler;
62 public void initialize() {
63 logger.info("initialization started of CommandExecutorImpl");
64 executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
65 executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
68 public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
69 this.executionTaskFactory = executionTaskFactory;
73 * Execute given command
74 * Create command request and enqueue it for execution.
75 * @param commandExecutorInput Contains CommandHeader, command , target Id , payload and conf ID (optional)
76 * @throws APPCException in case of error.
79 public void executeCommand (RuntimeContext commandExecutorInput) throws APPCException{
80 if (logger.isTraceEnabled()) {
81 logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
83 enqueRequest(commandExecutorInput);
84 if (logger.isTraceEnabled()) {
85 logger.trace("Exiting from executeCommand");
89 @SuppressWarnings("unchecked")
90 private void enqueRequest(RuntimeContext request) throws APPCException{
91 if (logger.isTraceEnabled()) {
92 logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
95 CommandTask commandTask = getMessageExecutor(request.getRequestContext().getAction().name());
96 commandTask.setCommandRequest(request);
97 long remainingTTL = getRemainingTTL(request);
98 executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
99 } catch (Exception e) {
100 logger.error("Exception: "+e.getMessage());
101 throw new APPCException(e);
104 if (logger.isTraceEnabled()) {
105 logger.trace("Exiting from enqueRequest");
109 private long getRemainingTTL(RuntimeContext request) {
110 Date requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
111 int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
112 return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
115 private CommandTask getMessageExecutor(String action){
116 if (logger.isTraceEnabled()) {
117 logger.trace("Entering to getMessageExecutor with command = "+ action);
119 CommandTask executionTask = executionTaskFactory.getExecutionTask(action);
120 if (logger.isTraceEnabled()) {
121 logger.trace("Exiting from getMessageExecutor");
123 return executionTask;