Merge "Refactor CommandExecutorInput to be immutable"
[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.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;
42
43
44 public class CommandExecutorImpl implements CommandExecutor {
45
46     private CommandTaskFactory executionTaskFactory ;
47     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
48
49     private ExecutionQueueService executionQueueService;
50     private ExpiredMessageHandler expiredMessageHandler;
51
52     public CommandExecutorImpl(){
53
54     }
55
56     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
57         this.executionQueueService = executionQueueService;
58     }
59
60     public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
61         this.expiredMessageHandler = expiredMessageHandler;
62     }
63
64     public void initialize() {
65         logger.info("initialization started of CommandExecutorImpl");
66         executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
67         executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
68     }
69
70     public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
71         this.executionTaskFactory = executionTaskFactory;
72     }
73
74     /**
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.
79      */
80     @Override
81     public void executeCommand (CommandExecutorInput commandExecutorInput) throws APPCException{
82         if (logger.isTraceEnabled()) {
83             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
84         }
85         CommandRequest request = getCommandRequest(commandExecutorInput);
86         enqueRequest(request);
87         if (logger.isTraceEnabled()) {
88             logger.trace("Exiting from executeCommand");
89         }
90     }
91
92     private CommandRequest getCommandRequest(CommandExecutorInput commandExecutorInput){
93         if (logger.isTraceEnabled()) {
94             logger.trace("Entering to getCommandRequest with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
95         }
96         CommandRequest commandRequest;
97
98         switch(commandExecutorInput.getRuntimeContext().getRequestContext().getAction()){
99             case Sync:
100                 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
101                 break;
102             case Audit:
103                 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
104                 break;
105             default:
106                 commandRequest = new LCMCommandRequest(commandExecutorInput);
107                 break;
108         }
109         if (logger.isTraceEnabled()) {
110             logger.trace("Exiting from getCommandRequest with (CommandRequest = "+ ObjectUtils.toString(commandRequest)+")");
111         }
112         return commandRequest;
113     }
114
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));
119         }
120         try {
121             CommandTask<? extends CommandRequest> 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);
128         }
129
130         if (logger.isTraceEnabled()) {
131             logger.trace("Exiting from enqueRequest");
132         }
133     }
134
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();
139     }
140
141     private CommandTask<? extends CommandRequest> getMessageExecutor(String action){
142         if (logger.isTraceEnabled()) {
143             logger.trace("Entering to getMessageExecutor with command = "+ action);
144         }
145         CommandTask<? extends CommandRequest> executionTask = executionTaskFactory.getExecutionTask(action);
146         if (logger.isTraceEnabled()) {
147             logger.trace("Exiting from getMessageExecutor");
148         }
149         return executionTask;
150     }
151
152
153 }