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