aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/openecomp/mso/apihandlerinfra/TasksHandler.java
blob: 92e3e5b0782ce92129d34e705b464737a5209e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*-
 * #%L
 * MSO
 * %%
 * Copyright (C) 2016 OPENECOMP - MSO
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package org.openecomp.mso.apihandlerinfra;

import org.openecomp.mso.apihandlerinfra.tasksbeans.*;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONObject;
import org.codehaus.jackson.map.ObjectMapper;

import org.openecomp.mso.apihandler.common.ErrorNumbers;
import org.openecomp.mso.apihandler.common.RequestClient;
import org.openecomp.mso.apihandler.common.RequestClientFactory;
import org.openecomp.mso.apihandler.common.ResponseHandler;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoAlarmLogger;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.utils.UUIDChecker;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Path("/tasks")
@Api(value="/tasks/{version: [vV]1}",description="Queries of Manual Tasks")
public class TasksHandler {

    private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
    private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
    public final static String MSO_PROP_APIHANDLER_INFRA = "MSO_PROP_APIHANDLER_INFRA";
    public final static String requestUrl = "mso/task/";	

    @Path("/{version:[vV]1}")
    @GET
    @ApiOperation(value="Finds Manual Tasks",response=Response.class)
    public Response queryFilters (@QueryParam("taskId") String taskId,
                                  @QueryParam("originalRequestId") String originalRequestId,
                                  @QueryParam("subscriptionServiceType") String subscriptionServiceType,
                                  @QueryParam("nfRole") String nfRole,
                                  @QueryParam("buildingBlockName") String buildingBlockName,
                                  @QueryParam("originalRequestDate") String originalRequestDate,
                                  @QueryParam("originalRequestorId") String originalRequestorId,
                                  @PathParam("version") String version) {
    	Response responseBack = null;
        long startTime = System.currentTimeMillis ();
        String requestId = UUIDChecker.generateUUID(msoLogger);
        MsoLogger.setServiceName ("ManualTasksQuery");
        // Generate a Request Id
        UUIDChecker.generateUUID(msoLogger);
        msoLogger.debug ("Incoming request received for queryFilter with taskId:" + taskId
        							+ " originalRequestId:" + originalRequestId
        							+ " subscriptionServiceType:" + subscriptionServiceType
        							+ " nfRole:" + nfRole
        							+ " buildingBlockName:" + buildingBlockName
        							+ " originalRequestDate:" + originalRequestDate
        							+ " originalRequestorId: " + originalRequestorId);
        
        // Prepare the query string to /task interface
        TaskVariables tv = new TaskVariables();
        
        List<TaskVariableValue> tvvList = new ArrayList<>();
        
        if (originalRequestId != null) {
        	TaskVariableValue tvv = new TaskVariableValue();
        	tvv.setName("originalRequestId");
        	tvv.setValue(originalRequestId);
        	tvv.setOperator("eq");
        	tvvList.add(tvv);        
        }
        if (subscriptionServiceType != null) {
        	TaskVariableValue tvv = new TaskVariableValue();
        	tvv.setName("subscriptionServiceType");
        	tvv.setValue(subscriptionServiceType);
        	tvv.setOperator("eq");
        	tvvList.add(tvv);        
        }
        if (nfRole != null) {
        	TaskVariableValue tvv = new TaskVariableValue();
        	tvv.setName("nfRole");
        	tvv.setValue(nfRole);
        	tvv.setOperator("eq");
        	tvvList.add(tvv);        
        }
        if (buildingBlockName != null) {
        	TaskVariableValue tvv = new TaskVariableValue();
        	tvv.setName("buildingBlockName");
        	tvv.setValue(buildingBlockName);
        	tvv.setOperator("eq");
        	tvvList.add(tvv);        
        }
        if (originalRequestDate != null) {
        	TaskVariableValue tvv = new TaskVariableValue();
        	tvv.setName("originalRequestDate");
        	tvv.setValue(originalRequestDate);
        	tvv.setOperator("eq");
        	tvvList.add(tvv);        
        }
        if (originalRequestorId != null) {
        	TaskVariableValue tvv = new TaskVariableValue();
        	tvv.setName("originalRequestorId");
        	tvv.setValue(originalRequestorId);
        	tvv.setOperator("eq");
        	tvvList.add(tvv);        
        }       
      
        tv.setTaskVariables(tvvList);
       
        RequestClient requestClient = null;
        MsoRequest msoRequest = new MsoRequest(requestId);
		HttpResponse response = null;
		long subStartTime = System.currentTimeMillis();		
				
		try {
			requestClient = RequestClientFactory.getRequestClient (requestUrl, MsoPropertiesUtils.loadMsoProperties ());
			// Capture audit event
			msoLogger.debug ("MSO API Handler Post call to Camunda engine for url: " + requestClient.getUrl ());

			System.out.println("URL : " + requestClient.getUrl ());
			ObjectMapper mapper = new ObjectMapper();			
			String camundaJsonReq = mapper.writeValueAsString(tv);
			msoLogger.debug("Camunda Json Request: " + camundaJsonReq);
			response = requestClient.post(camundaJsonReq);

			msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from BPMN engine", "BPMN", requestUrl, null);
		} catch (Exception e) {
			msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with BPMN engine", "BPMN", requestUrl, null);
			msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
			Response resp = msoRequest.buildServiceErrorResponse (HttpStatus.SC_BAD_GATEWAY,
					MsoException.ServiceException,
					"Failed calling bpmn " + e.getMessage (),
					ErrorNumbers.SVC_NO_SERVER_RESOURCES,
					null);
				alarmLogger.sendAlarm ("MsoConfigurationError",
					MsoAlarmLogger.CRITICAL,
					Messages.errors.get (ErrorNumbers.NO_COMMUNICATION_TO_BPEL));
			msoRequest.updateFinalStatus (Status.FAILED);
			msoLogger.error (MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception while communicate with BPMN engine");
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with BPMN engine");
			msoLogger.debug ("End of the transaction, the final response is: " + (String) resp.getEntity (),e);
			return resp;
		}
		TasksGetResponse trr = new TasksGetResponse();
		List<TaskList> taskList = new ArrayList<>();
		
		ResponseHandler respHandler = new ResponseHandler (response, requestClient.getType ());
		int bpelStatus = respHandler.getStatus ();
		if (bpelStatus == HttpStatus.SC_NO_CONTENT || bpelStatus == HttpStatus.SC_ACCEPTED) {			
			msoLogger.debug ("Received good response from Camunda");
						
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "BPMN completed the request");
			String respBody = respHandler.getResponseBody();		
			if (respBody != null) {				
				JSONArray data = new JSONArray(respBody);
				
				for (int i=0; i<data.length();i++) {
					JSONObject taskEntry = data.getJSONObject(i);
					String id = taskEntry.getString("id");
					msoLogger.debug("taskId is: " + id);
					if (taskId != null && !taskId.equals(id)) {
						continue;						
					}
					// Get variables info for each task ID
					TaskList taskListEntry = null;
					try {
						taskListEntry = getTaskInfo(id);
						msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from BPMN engine", "BPMN", requestUrl, null);
					} catch (Exception e) {
						msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with BPMN engine", "BPMN", requestUrl, null);
						msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
						Response resp = msoRequest.buildServiceErrorResponse (HttpStatus.SC_BAD_GATEWAY,
							MsoException.ServiceException,
							"Failed calling bpmn " + e.getMessage (),
							ErrorNumbers.SVC_NO_SERVER_RESOURCES,
							null);
						alarmLogger.sendAlarm ("MsoConfigurationError",
							MsoAlarmLogger.CRITICAL,
							Messages.errors.get (ErrorNumbers.NO_COMMUNICATION_TO_BPEL));
						
						msoLogger.error (MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception while communicate with BPMN engine");
						msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with BPMN engine");
						msoLogger.debug ("End of the transaction, the final response is: " + (String) resp.getEntity (),e);
						return resp;
					}
					taskList.add(taskListEntry);				
					
				}
				trr.setTaskList(taskList);				
			}
		
		} else {			
				msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
				Response resp = msoRequest.buildServiceErrorResponse(bpelStatus,
						MsoException.ServiceException,
						"Request Failed due to BPEL error with HTTP Status= %1" ,
						ErrorNumbers.SVC_DETAILED_SERVICE_ERROR,
						null);				
				msoLogger.error (MessageEnum.APIH_BPEL_RESPONSE_ERROR, requestClient.getUrl (), "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Response from BPEL engine is empty");
				msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, "Response from BPEL engine is empty");
				msoLogger.debug ("End of the transaction, the final response is: " + (String) resp.getEntity ());
				return resp;
		}	
			
		
		String jsonResponse = null;
		try {
			ObjectMapper mapper = new ObjectMapper();			
			jsonResponse = mapper.writeValueAsString(trr);
		}
		catch (Exception e) {
			msoLogger.debug("Unable to format response",e);
			Response resp = msoRequest.buildServiceErrorResponse(500,
					MsoException.ServiceException,
					"Request Failed due to bad response format" ,
					ErrorNumbers.SVC_DETAILED_SERVICE_ERROR,
					null);				
			msoLogger.error (MessageEnum.APIH_BPEL_RESPONSE_ERROR, requestClient.getUrl (), "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Bad response format");
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, "Bad response format");
			msoLogger.debug ("End of the transaction, the final response is: " + (String) resp.getEntity ());
			return resp;
		}
		
        
        msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
        responseBack = Response.status (HttpStatus.SC_ACCEPTED).entity (jsonResponse).build ();
        return responseBack;
		
    }    

    protected MsoLogger getMsoLogger () {
        return msoLogger;
    }
    
    // Makes a GET call to Camunda to get variables for this task
    private TaskList getTaskInfo(String taskId) throws Exception {
    	TaskList taskList;
    	String getRequestUrl = requestUrl + taskId + "/variables";
		HttpResponse getResponse;
		long subStartTime = System.currentTimeMillis();
		
		RequestClient requestClient = RequestClientFactory.getRequestClient (getRequestUrl, MsoPropertiesUtils.loadMsoProperties ());						
		// Capture audit event						
		msoLogger.debug ("MSO API Handler Get call to Camunda engine for url: " + requestClient.getUrl ());
		getResponse = requestClient.get();
		
		ResponseHandler respHandler = new ResponseHandler (getResponse, requestClient.getType ());
		int bpelStatus = respHandler.getStatus ();
		if (bpelStatus == HttpStatus.SC_ACCEPTED) {			
			msoLogger.debug ("Received good response from Camunda");
						
			msoLogger.recordAuditEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "BPMN completed the request");
			String respBody = respHandler.getResponseBody();		
			if (respBody != null) {
				taskList = buildTaskList(taskId, respBody);				
			}
			else {
				throw new Exception("Null task info from Camunda");
			}
			
		}
		else {
			throw new Exception ("Bad GET response from Camunda. Status is " + bpelStatus);
		}		
		
    	return taskList;
    	
    }
    
    private TaskList buildTaskList(String taskId, String respBody) {
    	TaskList taskList = new TaskList();
    	JSONObject variables = new JSONObject(respBody);
    	
    	taskList.setTaskId(taskId);
    	taskList.setType(getOptVariableValue(variables, "type"));
    	taskList.setNfRole(getOptVariableValue(variables, "nfRole"));
    	taskList.setSubscriptionServiceType(getOptVariableValue(variables, "subscriptionServiceType"));
    	taskList.setOriginalRequestId(getOptVariableValue(variables, "originalRequestId"));
    	taskList.setOriginalRequestorId(getOptVariableValue(variables, "originalRequestorId"));
    	taskList.setErrorSource(getOptVariableValue(variables, "errorSource"));
    	taskList.setErrorCode(getOptVariableValue(variables, "errorCode"));
    	taskList.setErrorMessage(getOptVariableValue(variables, "errorMessage"));
    	taskList.setBuildingBlockName(getOptVariableValue(variables, "buildingBlockName"));
    	taskList.setBuildingBlockStep(getOptVariableValue(variables, "buildingBlockStep"));  
    	taskList.setValidResponses(new JSONArray("[" + getOptVariableValue(variables, "validResponses").toLowerCase() + "]"));
    	
    	return taskList;       	
    }
    
    private String getOptVariableValue(JSONObject variables, String name) {
    	String variableEntry = variables.optString(name);
    	String value = "";
    	if (!variableEntry.isEmpty()) {
    		JSONObject variableEntryJson = new JSONObject(variableEntry);
    		value = variableEntryJson.optString("value");    		
    	}
    	return value;
    }
   
   
}