Add collaboration feature
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / validation-rest / validation-rest-services / src / main / java / org / openecomp / sdcrests / validation / rest / services / ValidationImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.validation.rest.services;
22
23
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
26 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerServiceName;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.validation.UploadValidationManager;
33 import org.openecomp.sdc.validation.types.ValidationFileResponse;
34 import org.openecomp.sdcrests.validation.rest.Validation;
35 import org.openecomp.sdcrests.validation.rest.mapping.MapValidationFileResponseToValidationFileResponseDto;
36 import org.openecomp.sdcrests.validation.types.ValidationFileResponseDto;
37 import org.slf4j.MDC;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Service;
41
42 import javax.inject.Named;
43 import javax.ws.rs.core.Response;
44 import java.io.IOException;
45 import java.io.InputStream;
46
47 @Named
48 @Service("validation")
49 @Scope(value = "prototype")
50 public class ValidationImpl implements Validation {
51
52   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
53   @Autowired
54   private UploadValidationManager uploadValidationManager;
55
56   @Override
57   public Response validateFile(String type, InputStream fileToValidate) {
58
59     mdcDataDebugMessage.debugEntryMessage(null, null);
60
61     MDC.put(LoggerConstants.SERVICE_NAME,
62         LoggerServiceName.Validate.toString());
63     ValidationFileResponse validationFileResponse = null;
64     try {
65       validationFileResponse = uploadValidationManager.validateFile(type, fileToValidate);
66     } catch (IOException exception) {
67       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
68           LoggerTragetServiceName.VALIDATE_FILE, ErrorLevel.ERROR.name(),
69           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.VALIDATE_FILE);
70       throw new RuntimeException(exception);
71     }
72
73     ValidationFileResponseDto validationFileResponseDto =
74         new MapValidationFileResponseToValidationFileResponseDto()
75             .applyMapping(validationFileResponse, ValidationFileResponseDto.class);
76
77     mdcDataDebugMessage.debugExitMessage(null, null);
78
79     return Response.ok(validationFileResponseDto).build();
80   }
81
82
83 }