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.time.Instant;
29 import java.time.temporal.ChronoUnit;
30 import java.util.concurrent.TimeUnit;
32 import org.apache.commons.lang.ObjectUtils;
33 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.appc.executionqueue.ExecutionQueueService;
36 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
37 import org.openecomp.appc.executor.CommandExecutor;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
43 public class CommandExecutorImpl implements CommandExecutor {
45 private CommandTaskFactory executionTaskFactory ;
46 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
48 private ExecutionQueueService executionQueueService;
49 private ExpiredMessageHandler expiredMessageHandler;
51 public CommandExecutorImpl(){
55 public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
56 this.executionQueueService = executionQueueService;
59 public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
60 this.expiredMessageHandler = expiredMessageHandler;
63 public void initialize() {
64 logger.info("initialization started of CommandExecutorImpl");
65 executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
66 executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
69 public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
70 this.executionTaskFactory = executionTaskFactory;
74 * Execute given command
75 * Create command request and enqueue it for execution.
76 * @param commandExecutorInput Contains CommandHeader, command , target Id , payload and conf ID (optional)
77 * @throws APPCException in case of error.
80 public void executeCommand (RuntimeContext commandExecutorInput) throws APPCException{
81 if (logger.isTraceEnabled()) {
82 logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
84 enqueRequest(commandExecutorInput);
85 if (logger.isTraceEnabled()) {
86 logger.trace("Exiting from executeCommand");
90 @SuppressWarnings("unchecked")
91 private void enqueRequest(RuntimeContext request) throws APPCException{
92 if (logger.isTraceEnabled()) {
93 logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
96 String action = request.getRequestContext().getAction().name();
97 CommandTask commandTask = executionTaskFactory.getExecutionTask(action, request);
98 long remainingTTL = getRemainingTTL(request);
99 executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
100 } catch (Exception e) {
101 logger.error("Exception: "+e.getMessage());
102 throw new APPCException(e);
105 if (logger.isTraceEnabled()) {
106 logger.trace("Exiting from enqueRequest");
110 private long getRemainingTTL(RuntimeContext request) {
111 Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
112 int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
113 return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));