21a00861edce89c6782bda9fe261d922c28737ee
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / main / java / org / openecomp / appc / executor / impl / CommandTask.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 package org.openecomp.appc.executor.impl;
23
24 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
25 import static com.att.eelf.configuration.Configuration.MDC_INSTANCE_UUID;
26 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
27 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
28 import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
29 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID;
30 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
31
32 import java.net.InetAddress;
33
34 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
35 import org.openecomp.appc.domainmodel.lcm.Status;
36 import org.openecomp.appc.executor.objects.CommandResponse;
37 import org.openecomp.appc.executor.objects.LCMCommandStatus;
38 import org.openecomp.appc.executor.objects.Params;
39 import org.openecomp.appc.logging.LoggingConstants;
40 import org.openecomp.appc.requesthandler.RequestHandler;
41 import org.openecomp.appc.workflow.WorkFlowManager;
42 import org.openecomp.appc.workflow.objects.WorkflowRequest;
43 import org.openecomp.appc.workflow.objects.WorkflowResponse;
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
46 import org.slf4j.MDC;
47
48 /**
49  * This abstract class is base class for all Command tasks. All command task must inherit this class.
50  */
51
52 public abstract class CommandTask implements Runnable {
53
54     protected final RequestHandler requestHandler;
55     protected final WorkFlowManager workflowManager;
56     protected final RuntimeContext commandRequest;
57
58     protected CommandTask(RuntimeContext commandRequest, RequestHandler requestHandler,
59             WorkFlowManager workflowManager) {
60         super();
61         this.commandRequest = commandRequest;
62         this.requestHandler = requestHandler;
63         this.workflowManager = workflowManager;
64     }
65
66     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandTask.class);
67
68     public void onRequestCompletion(CommandResponse response, boolean isAAIUpdated) {
69         logger.debug("Entry: onRequestCompletion()");
70         requestHandler.onRequestExecutionEnd(commandRequest, isAAIUpdated);
71     }
72
73     public abstract void onRequestCompletion(CommandResponse response);
74
75     protected CommandResponse buildCommandResponse(WorkflowResponse response) {
76
77         return new CommandResponse(commandRequest);
78     }
79
80
81     public void execute() {
82         final RuntimeContext runtimeContext = commandRequest;
83         MDC.put(MDC_KEY_REQUEST_ID, runtimeContext.getRequestContext().getCommonHeader().getRequestId());
84         if (runtimeContext.getRequestContext().getActionIdentifiers().getServiceInstanceId() != null)
85             MDC.put(MDC_SERVICE_INSTANCE_ID, runtimeContext.getRequestContext().getActionIdentifiers().getServiceInstanceId());
86         MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, runtimeContext.getRequestContext().getCommonHeader().getOriginatorId());
87         MDC.put(MDC_SERVICE_NAME, runtimeContext.getRequestContext().getAction().name());
88         try {
89             MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); //Don't change it to a .getLocalHostName() again please. It's wrong!
90             MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
91         }catch(Exception e){
92             logger.debug(e.getMessage());
93         }
94         MDC.put(MDC_INSTANCE_UUID, ""); //TODO make instanse_UUID generation once during APPC-instanse deploying
95
96         WorkflowRequest workflowRequest = new WorkflowRequest();
97         workflowRequest.setRequestContext(runtimeContext.getRequestContext());
98         workflowRequest.setResponseContext(runtimeContext.getResponseContext());
99         workflowRequest.setVnfContext(runtimeContext.getVnfContext());
100
101         WorkflowResponse response = workflowManager.executeWorkflow(workflowRequest);
102
103         CommandResponse commandResponse = buildCommandResponse(response);
104         this.onRequestCompletion(commandResponse);
105     }
106
107 }