aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/openecomp/mso/apihandlerinfra/E2EServiceInstances.java
blob: 46a7d43acc1f9ad1d697e5e2846c93335d4b88cf (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.apihandlerinfra;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.apihandlerinfra.e2eserviceinstancebeans.E2EServiceInstanceRequest;
import org.openecomp.mso.apihandlerinfra.e2eserviceinstancebeans.E2EUserParam;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.ServiceInstancesRequest;
import org.openecomp.mso.db.catalog.CatalogDatabase;
import org.openecomp.mso.db.catalog.beans.Service;
import org.openecomp.mso.db.catalog.beans.ServiceRecipe;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoAlarmLogger;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.requestsdb.InfraActiveRequests;
import org.openecomp.mso.requestsdb.RequestsDatabase;
import org.openecomp.mso.utils.UUIDChecker;

@Path("/")
public class E2EServiceInstances {

	private HashMap<String, String> instanceIdMap = new HashMap<String,String>();
	private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
	private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
	public final static String MSO_PROP_APIHANDLER_INFRA = "MSO_PROP_APIHANDLER_INFRA";

	public E2EServiceInstances() {
	}

	@POST
	@Path("e2eServiceInstances/{version:[vV][2-3]}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response createE2EServiceInstance(String request, @PathParam("version") String version) {

		Response response = E2EserviceInstances(request, Action.createInstance,	null, version);

		return response;
	}

	@DELETE
	@Path("e2eServiceInstances/{version:[vV][2-3]}/{serviceId}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteE2EServiceInstance(String request, @PathParam("version") String version) {

		Response response = E2EserviceInstances(request, Action.deleteInstance,	null, version);

		return response;
	}

	private Response E2EserviceInstances(String requestJSON, Action action,
			HashMap<String, String> instanceIdMap, String version) {

		String requestId = UUIDChecker.generateUUID(msoLogger);
		long startTime = System.currentTimeMillis();
		msoLogger.debug("requestId is: " + requestId);
		E2EServiceInstanceRequest sir = null;

		MsoRequest msoRequest = new MsoRequest(requestId);
		ObjectMapper mapper = new ObjectMapper();
		try {
			sir = mapper
					.readValue(requestJSON, E2EServiceInstanceRequest.class);

		} catch (Exception e) {

			msoLogger.debug("Mapping of request to JSON object failed : ", e);
			Response response = msoRequest.buildServiceErrorResponse(
					HttpStatus.SC_BAD_REQUEST,
					MsoException.ServiceException,
					"Mapping of request to JSON object failed.  "
							+ e.getMessage(), ErrorNumbers.SVC_BAD_PARAMETER,
							null);
			msoLogger.error(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,
					MSO_PROP_APIHANDLER_INFRA, "", "",
					MsoLogger.ErrorCode.SchemaError, requestJSON, e);
			msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR,
					MsoLogger.ResponseCode.SchemaError,
					"Mapping of request to JSON object failed");
			msoLogger.debug("End of the transaction, the final response is: "
					+ (String) response.getEntity());
			return response;
		}

		InfraActiveRequests dup = null;
		String instanceName = sir.getService().getName();
		String requestScope = sir.getService().getParameters().getNodeType();

		try {
			if(!(instanceName==null && requestScope.equals("service") && (action == Action.createInstance || action == Action.activateInstance))){
				dup = (RequestsDatabase.getInstance()).checkInstanceNameDuplicate (instanceIdMap, instanceName, requestScope);
			}
		} catch (Exception e) {
			msoLogger.error (MessageEnum.APIH_DUPLICATE_CHECK_EXC, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.DataError, "Error during duplicate check ", e);

			Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, MsoException.ServiceException,
					e.getMessage(),
					ErrorNumbers.SVC_DETAILED_SERVICE_ERROR,
					null) ;


			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "Error during duplicate check");
			msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
			return response;
		}

		if (dup != null) {
			// Found the duplicate record. Return the appropriate error.
			String instance = null;
			if(instanceName != null){
				instance = instanceName;
			}else{
				instance = instanceIdMap.get(requestScope + "InstanceId");
			}
			String dupMessage = "Error: Locked instance - This " + requestScope + " (" + instance + ") " + "already has a request being worked with a status of " + dup.getRequestStatus() + " (RequestId - " + dup.getRequestId() + "). The existing request must finish or be cleaned up before proceeding.";

			Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_CONFLICT, MsoException.ServiceException,
					dupMessage,
					ErrorNumbers.SVC_DETAILED_SERVICE_ERROR,
					null) ;


			msoLogger.warn (MessageEnum.APIH_DUPLICATE_FOUND, dupMessage, "", "", MsoLogger.ErrorCode.SchemaError, "Duplicate request - Subscriber already has a request for this service");
			msoRequest.createRequestRecord (Status.FAILED, action);
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.Conflict, dupMessage);
			msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
			return response;
		}

		CatalogDatabase db = null;
		try {
			db = CatalogDatabase.getInstance();
		} catch (Exception e) {
			msoLogger.error (MessageEnum.APIH_DB_ACCESS_EXC, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception while communciate with Catalog DB", e);
			msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
			Response response = msoRequest.buildServiceErrorResponse (HttpStatus.SC_NOT_FOUND,
					MsoException.ServiceException,
					"No communication to catalog DB " + e.getMessage (),
					ErrorNumbers.SVC_NO_SERVER_RESOURCES,
					null);
			alarmLogger.sendAlarm ("MsoDatabaseAccessError",
					MsoAlarmLogger.CRITICAL,
					Messages.errors.get (ErrorNumbers.NO_COMMUNICATION_TO_CATALOG_DB));
			msoRequest.createRequestRecord (Status.FAILED,action);
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "Exception while communciate with DB");
			msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
			return response;
		}


		RecipeLookupResult recipeLookupResult = null;

		try {
			recipeLookupResult = getServiceInstanceOrchestrationURI(db, sir, action);
		} catch (Exception e) {
			msoLogger.error (MessageEnum.APIH_DB_ACCESS_EXC, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.DataError, "Exception while querying Catalog DB", e);
			msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
			Response response = msoRequest.buildServiceErrorResponse (HttpStatus.SC_NOT_FOUND,
					MsoException.ServiceException,
					"Recipe could not be retrieved from catalog DB " + e.getMessage (),
					ErrorNumbers.SVC_GENERAL_SERVICE_ERROR,
					null);
			alarmLogger.sendAlarm ("MsoDatabaseAccessError",
					MsoAlarmLogger.CRITICAL,
					Messages.errors.get (ErrorNumbers.ERROR_FROM_CATALOG_DB));
			msoRequest.createRequestRecord (Status.FAILED,action);
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, "Exception while querying Catalog DB");
			msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
			db.close();
			return response;
		}
		finally{
			if(null != db)
			{
				db.close();
			}

		}

		if (recipeLookupResult == null) {
			msoLogger.error (MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.DataError, "No recipe found in DB");
			msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
			Response response = msoRequest.buildServiceErrorResponse (HttpStatus.SC_NOT_FOUND,
					MsoException.ServiceException,
					"Recipe does not exist in catalog DB",
					ErrorNumbers.SVC_GENERAL_SERVICE_ERROR,
					null);
			msoRequest.createRequestRecord (Status.FAILED, action);
			msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DataNotFound, "No recipe found in DB");
			msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());

			return response;
		}


		String modelInfo = sir.getService().getParameters().getNodeTemplateName();
		String[] arrayOfInfo = modelInfo.split(":");
		String serviceInstanceType = arrayOfInfo[0];



		String serviceInstanceId = "";

		RequestClient requestClient = null;
		HttpResponse response = null;

		long subStartTime = System.currentTimeMillis();
		String sirRequestJson = mappingObtainedRequestJSONToServiceInstanceRequest(sir);

		try {
			requestClient = RequestClientFactory.getRequestClient (recipeLookupResult.getOrchestrationURI (), MsoPropertiesUtils.loadMsoProperties ());

			// Capture audit event
			msoLogger.debug ("MSO API Handler Posting call to BPEL engine for url: " + requestClient.getUrl ());

			response = requestClient.post(requestId, false,
					recipeLookupResult.getRecipeTimeout(),
					action.name(), serviceInstanceId, null, null, null, null, serviceInstanceType,
					null, null, null, sirRequestJson);

			msoLogger.recordMetricEvent(subStartTime,
					MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
					"Successfully received response from BPMN engine", "BPMN",
					recipeLookupResult.getOrchestrationURI(), null);
		} catch (Exception e) {
			msoLogger.recordMetricEvent(subStartTime,
					MsoLogger.StatusCode.ERROR,
					MsoLogger.ResponseCode.CommunicationError,
					"Exception while communicate with BPMN engine", "BPMN",
					recipeLookupResult.getOrchestrationURI(), null);
			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());
			return resp;
		}

		if (response == null) {
			Response resp = msoRequest.buildServiceErrorResponse(
					HttpStatus.SC_BAD_GATEWAY, MsoException.ServiceException,
					"bpelResponse is null",
					ErrorNumbers.SVC_NO_SERVER_RESOURCES, null);
			msoLogger.error(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR,
					MSO_PROP_APIHANDLER_INFRA, "", "",
					MsoLogger.ErrorCode.BusinessProcesssError,
					"Null response from BPEL");
			msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR,
					MsoLogger.ResponseCode.InternalError,
					"Null response from BPMN");
			msoLogger.debug("End of the transaction, the final response is: "
					+ (String) resp.getEntity());
			return resp;
		}

		ResponseHandler respHandler = new ResponseHandler(response,
				requestClient.getType());
		int bpelStatus = respHandler.getStatus();

		// BPEL accepted the request, the request is in progress
		if (bpelStatus == HttpStatus.SC_ACCEPTED) {
			String camundaJSONResponseBody = respHandler.getResponseBody();
			msoLogger
			.debug("Received from Camunda: " + camundaJSONResponseBody);
			(RequestsDatabase.getInstance()).updateInfraStatus(requestId,
					Status.IN_PROGRESS.toString(),
					Constants.PROGRESS_REQUEST_IN_PROGRESS,
					Constants.MODIFIED_BY_APIHANDLER);

			msoLogger.recordAuditEvent(startTime,
					MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
					"BPMN accepted the request, the request is in progress");
			msoLogger.debug("End of the transaction, the final response is: "
					+ (String) camundaJSONResponseBody);
			return Response.status(HttpStatus.SC_ACCEPTED)
					.entity(camundaJSONResponseBody).build();
		} else {
			List<String> variables = new ArrayList<String>();
			variables.add(bpelStatus + "");
			String camundaJSONResponseBody = respHandler.getResponseBody();
			if (camundaJSONResponseBody != null
					&& !camundaJSONResponseBody.isEmpty()) {
				Response resp = msoRequest.buildServiceErrorResponse(
						bpelStatus, MsoException.ServiceException,
						"Request Failed due to BPEL error with HTTP Status= %1 "
								+ '\n' + camundaJSONResponseBody,
								ErrorNumbers.SVC_DETAILED_SERVICE_ERROR, variables);
				msoLogger.error(MessageEnum.APIH_BPEL_RESPONSE_ERROR,
						requestClient.getUrl(), "", "",
						MsoLogger.ErrorCode.BusinessProcesssError,
						"Response from BPEL engine is failed with HTTP Status="
								+ bpelStatus);
				msoLogger.recordAuditEvent(startTime,
						MsoLogger.StatusCode.ERROR,
						MsoLogger.ResponseCode.InternalError,
						"Response from BPMN engine is failed");
				msoLogger
				.debug("End of the transaction, the final response is: "
						+ (String) resp.getEntity());
				return resp;
			} else {
				Response resp = msoRequest
						.buildServiceErrorResponse(
								bpelStatus,
								MsoException.ServiceException,
								"Request Failed due to BPEL error with HTTP Status= %1",
								ErrorNumbers.SVC_DETAILED_SERVICE_ERROR,
								variables);
				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;
			}
		}
	}

	private RecipeLookupResult getServiceInstanceOrchestrationURI(
			CatalogDatabase db, E2EServiceInstanceRequest sir, Action action) {

		RecipeLookupResult recipeLookupResult = null;

		recipeLookupResult = getServiceURI(db, sir, action);

		if (recipeLookupResult != null) {
			msoLogger.debug ("Orchestration URI is: " + recipeLookupResult.getOrchestrationURI() + ", recipe Timeout is: " + Integer.toString(recipeLookupResult.getRecipeTimeout ()));
		}
		else {
			msoLogger.debug("No matching recipe record found");
		}
		return recipeLookupResult;
	}

	private RecipeLookupResult getServiceURI(CatalogDatabase db,
			E2EServiceInstanceRequest sir, Action action) {

		String defaultServiceModelName = "UUI_DEFAULT";

		Service serviceRecord = null;
		ServiceRecipe recipe = null;

		serviceRecord = db.getServiceByModelName(defaultServiceModelName);
		recipe = db.getServiceRecipeByModelUUID(serviceRecord.getModelUUID(), action.name());

		if (recipe == null) {
			return null;
		}
		return new RecipeLookupResult(recipe.getOrchestrationUri(),
				recipe.getRecipeTimeout());

	}

	private String mappingObtainedRequestJSONToServiceInstanceRequest(E2EServiceInstanceRequest e2eSir){

		ServiceInstancesRequest sir = new ServiceInstancesRequest();

		String returnString = null;

		//ModelInvariantId
		sir.getRequestDetails().getModelInfo().setModelInvariantId(e2eSir.getService().getServiceDefId());

		//modelNameVersionId
		sir.getRequestDetails().getModelInfo().setModelNameVersionId(e2eSir.getService().getTemplateId());

		String modelInfo = e2eSir.getService().getParameters().getNodeTemplateName();
		String[] arrayOfInfo = modelInfo.split(":");
		String modelName = arrayOfInfo[0];
		String modelVersion = arrayOfInfo[0];

		//modelName
		sir.getRequestDetails().getModelInfo().setModelName(modelName);

		//modelVersion
		sir.getRequestDetails().getModelInfo().setModelVersion(modelVersion);

		//modelType
		if(ModelType.service.equals(e2eSir.getService().getParameters().getNodeType())){
			sir.getRequestDetails().getModelInfo().setModelType(ModelType.service);
		}

		sir.getRequestDetails().getModelInfo().getModelType();

		//globalsubscriberId
		sir.getRequestDetails().getSubscriberInfo().setGlobalSubscriberId(e2eSir.getService().getParameters().getGlobalSubscriberId());

		//subscriberName
		sir.getRequestDetails().getSubscriberInfo().setSubscriberName(e2eSir.getService().getParameters().getSubscriberName());

		//instanceName
		sir.getRequestDetails().getRequestInfo().setInstanceName(e2eSir.getService().getName());

		//source
		sir.getRequestDetails().getRequestInfo().setSource("UUI");

		//suppressRollback
		sir.getRequestDetails().getRequestInfo().setSuppressRollback(true);

		//subscriptionServiceType
		sir.getRequestDetails().getRequestParameters().setSubscriptionServiceType("MOG");

		//Userparams
		List<E2EUserParam> userParams = new ArrayList<>(); 
		userParams = e2eSir.getService().getParameters().getRequestParameters().getUserParams();
		List<Map<String, String>> userParamList = new ArrayList<Map<String,String>>();
		Map<String,String> userParamMap= new HashMap<String, String>();
		for(E2EUserParam userp: userParams){
			userParamMap.put(userp.getName(), userp.getValue());
			userParamList.add(userParamMap);
		}

		sir.getRequestDetails().getRequestParameters().setUserParams(userParamList);

		//converting to string
		ObjectMapper mapper = new ObjectMapper();
		try {
			returnString = mapper.writeValueAsString(sir);
		} catch (IOException e) {
			msoLogger.debug("Exception while converting ServiceInstancesRequest object to string", e);
		}

		return returnString;
	}
}