Change nexus values to properties
[appc.git] / app-c / appc / 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.impl.objects.CommandRequest;
37 import org.openecomp.appc.executor.objects.CommandResponse;
38 import org.openecomp.appc.executor.objects.LCMCommandStatus;
39 import org.openecomp.appc.executor.objects.Params;
40 import org.openecomp.appc.logging.LoggingConstants;
41 import org.openecomp.appc.requesthandler.RequestHandler;
42 import org.openecomp.appc.workflow.WorkFlowManager;
43 import org.openecomp.appc.workflow.objects.WorkflowRequest;
44 import org.openecomp.appc.workflow.objects.WorkflowResponse;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import org.slf4j.MDC;
48
49 /**
50  * This abstract class is base class for all Command tasks. All command task must inherit this class.
51  */
52
53 public abstract class CommandTask<M> implements Runnable {
54
55     protected RequestHandler requestHandler;
56     protected WorkFlowManager workflowManager;
57
58     private CommandRequest commandRequest;
59
60     public CommandRequest getCommandRequest() {
61         return commandRequest;
62     }
63
64     public void setCommandRequest(CommandRequest commandRequest) {
65         this.commandRequest = commandRequest;
66     }
67
68     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandTask.class);
69
70     public void setWorkflowManager(WorkFlowManager workflowManager) {
71         this.workflowManager = workflowManager;
72     }
73
74     public void setRequestHandler(RequestHandler requestHandler) {
75         this.requestHandler = requestHandler;
76     }
77
78     CommandTask(){
79     }
80
81     public void onRequestCompletion(CommandRequest request, CommandResponse response , boolean isAAIUpdated) {
82         logger.debug("Entry: onRequestCompletion()");
83         requestHandler.onRequestExecutionEnd(request.getCommandExecutorInput().getRuntimeContext(), isAAIUpdated);
84     }
85
86     public abstract void onRequestCompletion(CommandRequest request, CommandResponse response);
87
88     protected CommandResponse buildCommandResponse(CommandRequest request, WorkflowResponse response) {
89
90         CommandResponse commandResponse = new CommandResponse();
91         commandResponse.setRuntimeContext(request.getCommandExecutorInput().getRuntimeContext());
92         return commandResponse;
93     }
94
95
96     public void execute() {
97         final RuntimeContext runtimeContext = commandRequest.getCommandExecutorInput().getRuntimeContext();
98         MDC.put(MDC_KEY_REQUEST_ID, runtimeContext.getRequestContext().getCommonHeader().getRequestId());
99         if (runtimeContext.getRequestContext().getActionIdentifiers().getServiceInstanceId() != null)
100             MDC.put(MDC_SERVICE_INSTANCE_ID, runtimeContext.getRequestContext().getActionIdentifiers().getServiceInstanceId());
101         MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, runtimeContext.getRequestContext().getCommonHeader().getOriginatorId());
102         MDC.put(MDC_SERVICE_NAME, runtimeContext.getRequestContext().getAction().name());
103         try {
104             MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); //Don't change it to a .getLocalHostName() again please. It's wrong!
105             MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
106         }catch(Exception e){
107             logger.debug(e.getMessage());
108         }
109         MDC.put(MDC_INSTANCE_UUID, ""); //TODO make instanse_UUID generation once during APPC-instanse deploying
110
111         WorkflowRequest workflowRequest = new WorkflowRequest();
112         workflowRequest.setRequestContext(runtimeContext.getRequestContext());
113         workflowRequest.setResponseContext(runtimeContext.getResponseContext());
114         workflowRequest.setVnfContext(runtimeContext.getVnfContext());
115
116         WorkflowResponse response = workflowManager.executeWorkflow(workflowRequest);
117
118         CommandResponse commandResponse =  buildCommandResponse(commandRequest, response);
119         this.onRequestCompletion(commandRequest,commandResponse);
120     }
121
122     public static void fillStatus(Status status, LCMCommandStatus lcmCommandStatus, Params params) {
123         status.setCode(lcmCommandStatus.getResponseCode());
124         status.setMessage(lcmCommandStatus.getFormattedMessage(params));
125     }
126 }