2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * ================================================================================
 
   9  * Modifications Copyright (C) 2019 Ericsson
 
  10  * =============================================================================
 
  11  * Licensed under the Apache License, Version 2.0 (the "License");
 
  12  * you may not use this file except in compliance with the License.
 
  13  * You may obtain a copy of the License at
 
  15  *      http://www.apache.org/licenses/LICENSE-2.0
 
  17  * Unless required by applicable law or agreed to in writing, software
 
  18  * distributed under the License is distributed on an "AS IS" BASIS,
 
  19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  20  * See the License for the specific language governing permissions and
 
  21  * limitations under the License.
 
  23  * ============LICENSE_END=========================================================
 
  26 package org.onap.appc.executor.impl;
 
  29 import com.att.eelf.configuration.EELFLogger;
 
  30 import com.att.eelf.configuration.EELFManager;
 
  31 import org.apache.commons.lang.ObjectUtils;
 
  32 import org.onap.appc.exceptions.APPCException;
 
  33 import org.onap.appc.executionqueue.ExecutionQueueService;
 
  34 import org.onap.appc.executor.CommandExecutor;
 
  35 import org.onap.appc.executor.impl.objects.CommandRequest;
 
  36 import org.onap.appc.executor.objects.CommandExecutorInput;
 
  37 import org.onap.appc.requesthandler.RequestHandler;
 
  38 import org.onap.appc.workflow.WorkFlowManager;
 
  40 import java.util.Date;
 
  41 import java.util.concurrent.TimeUnit;
 
  44 public class CommandExecutorImpl implements CommandExecutor {
 
  46     private final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
 
  48     private ExecutionQueueService executionQueueService;
 
  49     private RequestHandler requestHandler;
 
  50     private WorkFlowManager workflowManager;
 
  54      * <p>Used through blueprint.
 
  56     public void initialize() {
 
  57         logger.info("initialization started of CommandExecutorImpl");
 
  60     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
 
  61         this.executionQueueService = executionQueueService;
 
  64     public void setWorkflowManager(WorkFlowManager workflowManager) {
 
  65         this.workflowManager = workflowManager;
 
  68     public void setRequestHandler(RequestHandler requestHandler) {
 
  69         this.requestHandler = requestHandler;
 
  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 (CommandExecutorInput commandExecutorInput) throws APPCException{
 
  81         logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
 
  82         CommandTask commandTask;
 
  84             commandTask= getCommandTask(requestHandler, workflowManager);
 
  85             commandTask.setCommandRequest(new CommandRequest(commandExecutorInput));
 
  86             long remainingTTL = getRemainingTTL(commandTask.getCommandRequest());
 
  87             logger.trace("Queuing request with CommandRequest = "+ ObjectUtils.toString(commandTask.getCommandRequest()));
 
  88             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
 
  89         } catch (Exception e) {
 
  90             logger.error("Exception: "+e.getMessage());
 
  91             throw new APPCException(e);
 
  93         logger.trace("Exiting from executeCommand");
 
  96     private long getRemainingTTL(CommandRequest request) {
 
  97         Date requestTimestamp = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getTimeStamp();
 
  98         int ttl = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getFlags().getTtl();
 
  99         return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
 
 102     protected CommandTask getCommandTask(RequestHandler requestHandler, WorkFlowManager workflowManager) {
 
 103         return new CommandTask(requestHandler, workflowManager);