da58210653d8170c0c0c187cc4f1755b98db2c6a
[sdc.git] /
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.MdcDataErrorMessage;
26 import org.openecomp.sdc.logging.types.LoggerConstants;
27 import org.openecomp.sdc.logging.types.LoggerErrorCode;
28 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerServiceName;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.validation.UploadValidationManager;
32 import org.openecomp.sdc.validation.types.ValidationFileResponse;
33 import org.openecomp.sdcrests.validation.rest.Validation;
34 import org.openecomp.sdcrests.validation.rest.mapping.MapValidationFileResponseToValidationFileResponseDto;
35 import org.openecomp.sdcrests.validation.types.ValidationFileResponseDto;
36 import org.slf4j.MDC;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Service;
40
41 import javax.inject.Named;
42 import javax.ws.rs.core.Response;
43 import java.io.IOException;
44 import java.io.InputStream;
45
46 @Named
47 @Service("validation")
48 @Scope(value = "prototype")
49 public class ValidationImpl implements Validation {
50   @Autowired
51   private UploadValidationManager uploadValidationManager;
52
53   @Override
54   public Response validateFile(String type, InputStream fileToValidate) {
55     MDC.put(LoggerConstants.SERVICE_NAME,
56         LoggerServiceName.Validate.toString());
57     ValidationFileResponse validationFileResponse = null;
58     try {
59       validationFileResponse = uploadValidationManager.validateFile(type, fileToValidate);
60     } catch (IOException exception) {
61       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
62           LoggerTragetServiceName.VALIDATE_FILE, ErrorLevel.ERROR.name(),
63           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.VALIDATE_FILE);
64       throw new RuntimeException(exception);
65     }
66
67     ValidationFileResponseDto validationFileResponseDto =
68         new MapValidationFileResponseToValidationFileResponseDto()
69             .applyMapping(validationFileResponse, ValidationFileResponseDto.class);
70     return Response.ok(validationFileResponseDto).build();
71   }
72
73
74 }