Migrate to Java 8 Time API
[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.time.Instant;
29 import java.time.temporal.ChronoUnit;
30 import java.util.concurrent.TimeUnit;
31
32 import org.apache.commons.lang.ObjectUtils;
33 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.appc.executionqueue.ExecutionQueueService;
36 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
37 import org.openecomp.appc.executor.CommandExecutor;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42
43 public class CommandExecutorImpl implements CommandExecutor {
44
45     private CommandTaskFactory executionTaskFactory ;
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
47
48     private ExecutionQueueService executionQueueService;
49     private ExpiredMessageHandler expiredMessageHandler;
50
51     public CommandExecutorImpl(){
52
53     }
54
55     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
56         this.executionQueueService = executionQueueService;
57     }
58
59     public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
60         this.expiredMessageHandler = expiredMessageHandler;
61     }
62
63     public void initialize() {
64         logger.info("initialization started of CommandExecutorImpl");
65         executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
66         executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
67     }
68
69     public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
70         this.executionTaskFactory = executionTaskFactory;
71     }
72
73     /**
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.
78      */
79     @Override
80     public void executeCommand (RuntimeContext commandExecutorInput) throws APPCException{
81         if (logger.isTraceEnabled()) {
82             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
83         }
84         enqueRequest(commandExecutorInput);
85         if (logger.isTraceEnabled()) {
86             logger.trace("Exiting from executeCommand");
87         }
88     }
89
90     @SuppressWarnings("unchecked")
91     private void enqueRequest(RuntimeContext request) throws APPCException{
92         if (logger.isTraceEnabled()) {
93             logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
94         }
95         try {
96             CommandTask commandTask = getMessageExecutor(request.getRequestContext().getAction().name());
97             commandTask.setCommandRequest(request);
98             long remainingTTL = getRemainingTTL(request);
99             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
100         } catch (Exception e) {
101             logger.error("Exception: "+e.getMessage());
102             throw new APPCException(e);
103         }
104
105         if (logger.isTraceEnabled()) {
106             logger.trace("Exiting from enqueRequest");
107         }
108     }
109
110     private long getRemainingTTL(RuntimeContext request) {
111         Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
112         int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
113         return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));
114     }
115
116     private CommandTask getMessageExecutor(String action){
117         if (logger.isTraceEnabled()) {
118             logger.trace("Entering to getMessageExecutor with command = "+ action);
119         }
120         CommandTask executionTask = executionTaskFactory.getExecutionTask(action);
121         if (logger.isTraceEnabled()) {
122             logger.trace("Exiting from getMessageExecutor");
123         }
124         return executionTask;
125     }
126
127
128 }