Remove CommandRequest and subclasses
[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.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.objects.CommandExecutorInput;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40
41 public class CommandExecutorImpl implements CommandExecutor {
42
43     private CommandTaskFactory executionTaskFactory ;
44     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
45
46     private ExecutionQueueService executionQueueService;
47     private ExpiredMessageHandler expiredMessageHandler;
48
49     public CommandExecutorImpl(){
50
51     }
52
53     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
54         this.executionQueueService = executionQueueService;
55     }
56
57     public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
58         this.expiredMessageHandler = expiredMessageHandler;
59     }
60
61     public void initialize() {
62         logger.info("initialization started of CommandExecutorImpl");
63         executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
64         executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
65     }
66
67     public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
68         this.executionTaskFactory = executionTaskFactory;
69     }
70
71     /**
72      * Execute given command
73      * Create command request and enqueue it for execution.
74      * @param commandExecutorInput Contains CommandHeader,  command , target Id , payload and conf ID (optional)
75      * @throws APPCException in case of error.
76      */
77     @Override
78     public void executeCommand (CommandExecutorInput commandExecutorInput) throws APPCException{
79         if (logger.isTraceEnabled()) {
80             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
81         }
82         enqueRequest(commandExecutorInput);
83         if (logger.isTraceEnabled()) {
84             logger.trace("Exiting from executeCommand");
85         }
86     }
87
88     @SuppressWarnings("unchecked")
89     private void enqueRequest(CommandExecutorInput request) throws APPCException{
90         if (logger.isTraceEnabled()) {
91             logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
92         }
93         try {
94             CommandTask commandTask = getMessageExecutor(request.getRuntimeContext().getRequestContext().getAction().name());
95             commandTask.setCommandRequest(request);
96             long remainingTTL = getRemainingTTL(request);
97             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
98         } catch (Exception e) {
99             logger.error("Exception: "+e.getMessage());
100             throw new APPCException(e);
101         }
102
103         if (logger.isTraceEnabled()) {
104             logger.trace("Exiting from enqueRequest");
105         }
106     }
107
108     private long getRemainingTTL(CommandExecutorInput request) {
109         Date requestTimestamp = request.getRuntimeContext().getRequestContext().getCommonHeader().getTimeStamp();
110         int ttl = request.getRuntimeContext().getRequestContext().getCommonHeader().getFlags().getTtl();
111         return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis();
112     }
113
114     private CommandTask getMessageExecutor(String action){
115         if (logger.isTraceEnabled()) {
116             logger.trace("Entering to getMessageExecutor with command = "+ action);
117         }
118         CommandTask executionTask = executionTaskFactory.getExecutionTask(action);
119         if (logger.isTraceEnabled()) {
120             logger.trace("Exiting from getMessageExecutor");
121         }
122         return executionTask;
123     }
124
125
126 }