2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
26 package org.openecomp.appc.executor.impl;
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31 import java.util.concurrent.TimeUnit;
33 import org.apache.commons.lang.ObjectUtils;
34 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
35 import org.openecomp.appc.exceptions.APPCException;
36 import org.openecomp.appc.executionqueue.ExecutionQueueService;
37 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
38 import org.openecomp.appc.executor.CommandExecutor;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
44 public class CommandExecutorImpl implements CommandExecutor {
46 private CommandTaskFactory executionTaskFactory ;
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
49 private ExecutionQueueService executionQueueService;
50 private ExpiredMessageHandler expiredMessageHandler;
52 public CommandExecutorImpl(){
56 public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
57 this.executionQueueService = executionQueueService;
60 public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
61 this.expiredMessageHandler = expiredMessageHandler;
64 public void initialize() {
65 logger.info("initialization started of CommandExecutorImpl");
66 executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
67 executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
70 public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
71 this.executionTaskFactory = executionTaskFactory;
75 * Execute given command
76 * Create command request and enqueue it for execution.
77 * @param commandExecutorInput Contains CommandHeader, command , target Id , payload and conf ID (optional)
78 * @throws APPCException in case of error.
81 public void executeCommand (RuntimeContext commandExecutorInput) throws APPCException{
82 if (logger.isTraceEnabled()) {
83 logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
85 enqueRequest(commandExecutorInput);
86 if (logger.isTraceEnabled()) {
87 logger.trace("Exiting from executeCommand");
91 private RuntimeContext getCommandRequest(RuntimeContext commandExecutorInput){
92 if (logger.isTraceEnabled()) {
93 logger.trace("Entering to getCommandRequest with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
95 RuntimeContext commandRequest;
96 commandRequest = commandExecutorInput;
98 CommandRequest commandRequest;
100 switch(commandExecutorInput.getRequestContext().getAction()){
104 case ConfigBackupDelete:
106 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
109 commandRequest = new LCMCommandRequest(commandExecutorInput);
113 if (logger.isTraceEnabled()) {
114 logger.trace("Exiting from getCommandRequest with (CommandRequest = "+ ObjectUtils.toString(commandRequest)+")");
116 return commandRequest;
119 @SuppressWarnings("unchecked")
120 private void enqueRequest(RuntimeContext request) throws APPCException{
121 if (logger.isTraceEnabled()) {
122 logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
125 String action = request.getRequestContext().getAction().name();
126 CommandTask commandTask = executionTaskFactory.getExecutionTask(action, request);
127 long remainingTTL = getRemainingTTL(request);
128 executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
129 } catch (Exception e) {
130 logger.error("Exception: "+e.getMessage());
131 throw new APPCException(e);
134 if (logger.isTraceEnabled()) {
135 logger.trace("Exiting from enqueRequest");
139 private long getRemainingTTL(RuntimeContext request) {
140 Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
141 int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
142 return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));