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.impl.objects.CommandRequest;
 
  37 import org.openecomp.appc.executor.impl.objects.LCMCommandRequest;
 
  38 import org.openecomp.appc.executor.impl.objects.LCMReadOnlyCommandRequest;
 
  39 import org.openecomp.appc.executor.objects.CommandExecutorInput;
 
  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 (CommandExecutorInput commandExecutorInput) throws APPCException{
 
  82         if (logger.isTraceEnabled()) {
 
  83             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
 
  85         CommandRequest request = getCommandRequest(commandExecutorInput);
 
  86         enqueRequest(request);
 
  87         if (logger.isTraceEnabled()) {
 
  88             logger.trace("Exiting from executeCommand");
 
  92     private CommandRequest getCommandRequest(CommandExecutorInput commandExecutorInput){
 
  93         if (logger.isTraceEnabled()) {
 
  94             logger.trace("Entering to getCommandRequest with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
 
  96         CommandRequest commandRequest;
 
  98         switch(commandExecutorInput.getRuntimeContext().getRequestContext().getAction()){
 
 100                 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
 
 103                 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
 
 106                 commandRequest = new LCMCommandRequest(commandExecutorInput);
 
 109         if (logger.isTraceEnabled()) {
 
 110             logger.trace("Exiting from getCommandRequest with (CommandRequest = "+ ObjectUtils.toString(commandRequest)+")");
 
 112         return commandRequest;
 
 115     @SuppressWarnings("unchecked")
 
 116     private void enqueRequest(CommandRequest request) throws APPCException{
 
 117         if (logger.isTraceEnabled()) {
 
 118             logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
 
 121             CommandTask commandTask = getMessageExecutor(request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getAction().name());
 
 122             commandTask.setCommandRequest(request);
 
 123             long remainingTTL = getRemainingTTL(request);
 
 124             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
 
 125         } catch (Exception e) {
 
 126             logger.error("Exception: "+e.getMessage());
 
 127             throw new APPCException(e);
 
 130         if (logger.isTraceEnabled()) {
 
 131             logger.trace("Exiting from enqueRequest");
 
 135     private long getRemainingTTL(CommandRequest request) {
 
 136         Date requestTimestamp = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getTimeStamp();
 
 137         int ttl = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getFlags().getTtl();
 
 138         return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
 
 141     private CommandTask getMessageExecutor(String action){
 
 142         if (logger.isTraceEnabled()) {
 
 143             logger.trace("Entering to getMessageExecutor with command = "+ action);
 
 145         CommandTask executionTask = executionTaskFactory.getExecutionTask(action);
 
 146         if (logger.isTraceEnabled()) {
 
 147             logger.trace("Exiting from getMessageExecutor");
 
 149         return executionTask;