f7ffdadce251d27df4e8eb66d4ced69062dee96e
[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 com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
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.executor.CommandExecutor;
38
39 import java.time.Instant;
40 import java.time.temporal.ChronoUnit;
41 import java.util.concurrent.TimeUnit;
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     /**
57      * Injected by blueprint
58      *
59      * @param executionQueueService
60      */
61     public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
62         this.executionQueueService = executionQueueService;
63     }
64
65     /**
66      * Injected by blueprint
67      * @param expiredMessageHandler
68      */
69     public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
70         this.expiredMessageHandler = expiredMessageHandler;
71     }
72
73     public void initialize() {
74         logger.info("initialization started of CommandExecutorImpl");
75         executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
76     }
77
78     public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
79         this.executionTaskFactory = executionTaskFactory;
80     }
81
82     /**
83      * Execute given command
84      * Create command request and enqueue it for execution.
85      *
86      * @param commandExecutorInput Contains CommandHeader,  command , target Id , payload and conf ID (optional)
87      * @throws APPCException in case of error.
88      */
89     @Override
90     public void executeCommand(RuntimeContext commandExecutorInput) throws APPCException {
91         if (logger.isTraceEnabled()) {
92             logger.trace("Entering to executeCommand with CommandExecutorInput = " + ObjectUtils.toString(commandExecutorInput));
93         }
94         enqueRequest(commandExecutorInput);
95         if (logger.isTraceEnabled()) {
96             logger.trace("Exiting from executeCommand");
97         }
98     }
99
100     private RuntimeContext getCommandRequest(RuntimeContext commandExecutorInput) {
101         if (logger.isTraceEnabled()) {
102             logger.trace("Entering to getCommandRequest with CommandExecutorInput = " + ObjectUtils.toString(commandExecutorInput));
103         }
104         RuntimeContext commandRequest;
105         commandRequest = commandExecutorInput;
106         if (logger.isTraceEnabled()) {
107             logger.trace("Exiting from getCommandRequest with (CommandRequest = " + ObjectUtils.toString(commandRequest) + ")");
108         }
109         return commandRequest;
110     }
111
112     @SuppressWarnings("unchecked")
113     private void enqueRequest(RuntimeContext request) throws APPCException {
114         if (logger.isTraceEnabled()) {
115             logger.trace("Entering to enqueRequest with CommandRequest = " + ObjectUtils.toString(request));
116         }
117         try {
118             CommandTask commandTask = executionTaskFactory.getExecutionTask(request);
119
120             long remainingTTL = getRemainingTTL(request);
121
122             executionQueueService.putMessage(commandTask, remainingTTL, TimeUnit.MILLISECONDS);
123         } catch (Exception e) {
124             logger.error("Exception: " + e.getMessage());
125             throw new APPCException(e);
126         }
127
128         if (logger.isTraceEnabled()) {
129             logger.trace("Exiting from enqueRequest");
130         }
131     }
132
133     private long getRemainingTTL(RuntimeContext request) {
134         Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
135         int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
136         return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));
137     }
138
139     private CommandTask getMessageExecutor(RuntimeContext request) {
140         if (logger.isTraceEnabled()) {
141             logger.trace("Entering to getMessageExecutor with command = " + request);
142         }
143         CommandTask executionTask = executionTaskFactory.getExecutionTask(request);
144         if (logger.isTraceEnabled()) {
145             logger.trace("Exiting from getMessageExecutor");
146         }
147         return executionTask;
148     }
149 }