3ee89f11cf5f7bd8587b26557473b1d494dff120
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / main / java / org / onap / appc / executor / impl / CommandExecutorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.executor.impl;
26
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.commons.lang.ObjectUtils;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.appc.executionqueue.ExecutionQueueService;
33 import org.onap.appc.executor.CommandExecutor;
34 import org.onap.appc.executor.impl.objects.CommandRequest;
35 import org.onap.appc.executor.objects.CommandExecutorInput;
36 import org.onap.appc.requesthandler.RequestHandler;
37 import org.onap.appc.workflow.WorkFlowManager;
38
39 import java.util.Date;
40 import java.util.concurrent.TimeUnit;
41
42
43 public class CommandExecutorImpl implements CommandExecutor {
44
45     private final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
46
47     private ExecutionQueueService executionQueueService;
48     private RequestHandler requestHandler;
49     private WorkFlowManager workflowManager;
50
51     /**
52      * Initialization.
53      * <p>Used through blueprint.
54      */
55     public void initialize() {
56         logger.info("initialization started of CommandExecutorImpl");
57     }
58
59     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
60         this.executionQueueService = executionQueueService;
61     }
62
63     public void setWorkflowManager(WorkFlowManager workflowManager) {
64         this.workflowManager = workflowManager;
65     }
66
67     public void setRequestHandler(RequestHandler requestHandler) {
68         this.requestHandler = requestHandler;
69     }
70
71
72     /**
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.
77      */
78     @Override
79     public void executeCommand (CommandExecutorInput commandExecutorInput) throws APPCException{
80         if (logger.isTraceEnabled()) {
81             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
82         }
83         CommandTask commandTask;
84         try {
85             commandTask= new CommandTask(requestHandler,workflowManager);
86             commandTask.setCommandRequest(new CommandRequest(commandExecutorInput));
87             long remainingTTL = getRemainingTTL(commandTask.getCommandRequest());
88             if (logger.isTraceEnabled()) {
89                 logger.trace("Queuing request with CommandRequest = "+ ObjectUtils.toString(commandTask.getCommandRequest()));
90             }
91             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
92         } catch (Exception e) {
93             logger.error("Exception: "+e.getMessage());
94             throw new APPCException(e);
95         }
96
97         if (logger.isTraceEnabled()) {
98             logger.trace("Exiting from executeCommand");
99         }
100     }
101
102     private long getRemainingTTL(CommandRequest request) {
103         Date requestTimestamp = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getTimeStamp();
104         int ttl = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getFlags().getTtl();
105         return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
106     }
107
108 }