Updating licenses in all files
[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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 /**
24  *
25  */
26 package org.openecomp.appc.executor.impl;
27
28
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.commons.lang.ObjectUtils;
34 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
35 import org.openecomp.appc.exceptions.APPCException;
36 import org.openecomp.appc.executionqueue.ExecutionQueueService;
37 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
38 import org.openecomp.appc.executor.CommandExecutor;
39
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 (RuntimeContext commandExecutorInput) throws APPCException{
82         if (logger.isTraceEnabled()) {
83             logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
84         }
85         enqueRequest(commandExecutorInput);
86         if (logger.isTraceEnabled()) {
87             logger.trace("Exiting from executeCommand");
88         }
89     }
90
91     private RuntimeContext getCommandRequest(RuntimeContext commandExecutorInput){
92         if (logger.isTraceEnabled()) {
93             logger.trace("Entering to getCommandRequest with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
94         }
95         RuntimeContext commandRequest;
96         commandRequest = commandExecutorInput;
97         /*
98         CommandRequest commandRequest;
99
100         switch(commandExecutorInput.getRequestContext().getAction()){
101             case Sync:
102             case Audit:
103             case ConfigBackup:
104             case ConfigBackupDelete:
105             case ConfigExport:
106                 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
107                 break;
108             default:
109                 commandRequest = new LCMCommandRequest(commandExecutorInput);
110                 break;
111         }
112         */
113         if (logger.isTraceEnabled()) {
114             logger.trace("Exiting from getCommandRequest with (CommandRequest = "+ ObjectUtils.toString(commandRequest)+")");
115         }
116         return commandRequest;
117     }
118
119     @SuppressWarnings("unchecked")
120     private void enqueRequest(RuntimeContext request) throws APPCException{
121         if (logger.isTraceEnabled()) {
122             logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
123         }
124         try {
125             String action = request.getRequestContext().getAction().name();
126             CommandTask commandTask = executionTaskFactory.getExecutionTask(action, request);
127             long remainingTTL = getRemainingTTL(request);
128             executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
129         } catch (Exception e) {
130             logger.error("Exception: "+e.getMessage());
131             throw new APPCException(e);
132         }
133
134         if (logger.isTraceEnabled()) {
135             logger.trace("Exiting from enqueRequest");
136         }
137     }
138
139     private long getRemainingTTL(RuntimeContext request) {
140         Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
141         int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
142         return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));
143     }
144
145
146 }