Remove CommandExecutorInput
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / main / java / org / openecomp / appc / executor / impl / CommandExecutorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 /**
23  *
24  */
25 package org.openecomp.appc.executor.impl;
26
27
28 import java.util.Date;
29 import java.util.concurrent.TimeUnit;
30
31 import org.apache.commons.lang.ObjectUtils;
32 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
33 import org.openecomp.appc.exceptions.APPCException;
34 import org.openecomp.appc.executionqueue.ExecutionQueueService;
35 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
36 import org.openecomp.appc.executor.CommandExecutor;
37
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40
41
42 public class CommandExecutorImpl implements CommandExecutor {
43
44     private CommandTaskFactory executionTaskFactory ;
45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
46
47     private ExecutionQueueService executionQueueService;
48     private ExpiredMessageHandler expiredMessageHandler;
49
50     public CommandExecutorImpl(){
51
52     }
53
54     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
55         this.executionQueueService = executionQueueService;
56     }
57
58     public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
59         this.expiredMessageHandler = expiredMessageHandler;
60     }
61
62     public void initialize() {
63         logger.info("initialization started of CommandExecutorImpl");
64         executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
65         executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
66     }
67
68     public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
69         this.executionTaskFactory = executionTaskFactory;
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 (RuntimeContext commandExecutorInput) throws APPCException{
80         if (logger.isTraceEnabled()) {
81             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
82         }
83         enqueRequest(commandExecutorInput);
84         if (logger.isTraceEnabled()) {
85             logger.trace("Exiting from executeCommand");
86         }
87     }
88
89     @SuppressWarnings("unchecked")
90     private void enqueRequest(RuntimeContext request) throws APPCException{
91         if (logger.isTraceEnabled()) {
92             logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
93         }
94         try {
95             CommandTask commandTask = getMessageExecutor(request.getRequestContext().getAction().name());
96             commandTask.setCommandRequest(request);
97             long remainingTTL = getRemainingTTL(request);
98             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
99         } catch (Exception e) {
100             logger.error("Exception: "+e.getMessage());
101             throw new APPCException(e);
102         }
103
104         if (logger.isTraceEnabled()) {
105             logger.trace("Exiting from enqueRequest");
106         }
107     }
108
109     private long getRemainingTTL(RuntimeContext request) {
110         Date requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
111         int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
112         return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
113     }
114
115     private CommandTask getMessageExecutor(String action){
116         if (logger.isTraceEnabled()) {
117             logger.trace("Entering to getMessageExecutor with command = "+ action);
118         }
119         CommandTask executionTask = executionTaskFactory.getExecutionTask(action);
120         if (logger.isTraceEnabled()) {
121             logger.trace("Exiting from getMessageExecutor");
122         }
123         return executionTask;
124     }
125
126
127 }