2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
28 package org.openecomp.appc.executor.impl;
31 import java.time.Instant;
32 import java.time.temporal.ChronoUnit;
33 import java.util.concurrent.TimeUnit;
35 import org.apache.commons.lang.ObjectUtils;
36 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
37 import org.openecomp.appc.exceptions.APPCException;
38 import org.openecomp.appc.executionqueue.ExecutionQueueService;
39 import org.openecomp.appc.executionqueue.impl.ExecutionQueueServiceFactory;
40 import org.openecomp.appc.executor.CommandExecutor;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
46 public class CommandExecutorImpl implements CommandExecutor {
48 private CommandTaskFactory executionTaskFactory ;
49 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class);
51 private ExecutionQueueService executionQueueService;
52 private ExpiredMessageHandler expiredMessageHandler;
54 public CommandExecutorImpl(){
58 public void setExecutionQueueService(ExecutionQueueService executionQueueService) {
59 this.executionQueueService = executionQueueService;
62 public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) {
63 this.expiredMessageHandler = expiredMessageHandler;
66 public void initialize() {
67 logger.info("initialization started of CommandExecutorImpl");
68 executionQueueService = ExecutionQueueServiceFactory.getExecutionQueueService();
69 executionQueueService.registerMessageExpirationListener(expiredMessageHandler);
72 public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) {
73 this.executionTaskFactory = executionTaskFactory;
77 * Execute given command
78 * Create command request and enqueue it for execution.
79 * @param commandExecutorInput Contains CommandHeader, command , target Id , payload and conf ID (optional)
80 * @throws APPCException in case of error.
83 public void executeCommand (RuntimeContext commandExecutorInput) throws APPCException{
84 if (logger.isTraceEnabled()) {
85 logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
87 enqueRequest(commandExecutorInput);
88 if (logger.isTraceEnabled()) {
89 logger.trace("Exiting from executeCommand");
93 private RuntimeContext getCommandRequest(RuntimeContext commandExecutorInput){
94 if (logger.isTraceEnabled()) {
95 logger.trace("Entering to getCommandRequest with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput));
97 RuntimeContext commandRequest;
98 commandRequest = commandExecutorInput;
100 CommandRequest commandRequest;
102 switch(commandExecutorInput.getRequestContext().getAction()){
106 case ConfigBackupDelete:
108 commandRequest = new LCMReadOnlyCommandRequest(commandExecutorInput);
111 commandRequest = new LCMCommandRequest(commandExecutorInput);
115 if (logger.isTraceEnabled()) {
116 logger.trace("Exiting from getCommandRequest with (CommandRequest = "+ ObjectUtils.toString(commandRequest)+")");
118 return commandRequest;
121 @SuppressWarnings("unchecked")
122 private void enqueRequest(RuntimeContext request) throws APPCException{
123 if (logger.isTraceEnabled()) {
124 logger.trace("Entering to enqueRequest with CommandRequest = "+ ObjectUtils.toString(request));
127 String action = request.getRequestContext().getAction().name();
128 CommandTask commandTask = executionTaskFactory.getExecutionTask(action, request);
129 long remainingTTL = getRemainingTTL(request);
130 executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS);
131 } catch (Exception e) {
132 logger.error("Exception: "+e.getMessage());
133 throw new APPCException(e);
136 if (logger.isTraceEnabled()) {
137 logger.trace("Exiting from enqueRequest");
141 private long getRemainingTTL(RuntimeContext request) {
142 Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp();
143 int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl();
144 return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl));