Merge of new rebased code
[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     private RuntimeContext getCommandRequest(RuntimeContext commandExecutorInput){
91         if (logger.isTraceEnabled()) {
92             logger.trace("Entering to getCommandRequest with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
93         }
94         RuntimeContext commandRequest;
95         commandRequest = commandExecutorInput;
96         /*
97         CommandRequest commandRequest;
98
99         switch(commandExecutorInput.getRequestContext().getAction()){
100             case Sync:
101             case Audit:
102             case ConfigBackup:
103             case ConfigBackupDelete:
104             case ConfigExport:
105                 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
106                 break;
107             default:
108                 commandRequest = new LCMCommandRequest(commandExecutorInput);
109                 break;
110         }
111         */
112         if (logger.isTraceEnabled()) {
113             logger.trace("Exiting from getCommandRequest with (CommandRequest = "+ ObjectUtils.toString(commandRequest)+")");
114         }
115         return commandRequest;
116     }
117
118     @SuppressWarnings("unchecked")
119     private void enqueRequest(RuntimeContext request) throws APPCException{
120         if (logger.isTraceEnabled()) {
121             logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
122         }
123         try {
124             String action = request.getRequestContext().getAction().name();
125             CommandTask commandTask = executionTaskFactory.getExecutionTask(action, request);
126             long remainingTTL = getRemainingTTL(request);
127             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
128         } catch (Exception e) {
129             logger.error("Exception: "+e.getMessage());
130             throw new APPCException(e);
131         }
132
133         if (logger.isTraceEnabled()) {
134             logger.trace("Exiting from enqueRequest");
135         }
136     }
137
138     private long getRemainingTTL(RuntimeContext request) {
139         Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
140         int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
141         return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));
142     }
143
144
145 }