899f96715ffc34317c53f177394dbf04d9bc966a
[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.sdc.validation.impl;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.validation.api.ValidationManager;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.ErrorCategory;
30 import org.openecomp.sdc.common.errors.ErrorCode;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.common.utils.SdcCommon;
33 import org.openecomp.sdc.datatypes.error.ErrorLevel;
34 import org.openecomp.sdc.datatypes.error.ErrorMessage;
35 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
36 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
37 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
38 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
39 import org.openecomp.sdc.logging.types.LoggerConstants;
40 import org.openecomp.sdc.logging.types.LoggerErrorCode;
41 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
42 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
43 import org.openecomp.sdc.validation.UploadValidationManager;
44 import org.openecomp.sdc.validation.types.ValidationFileResponse;
45 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
46 import org.slf4j.MDC;
47
48 import java.io.ByteArrayInputStream;
49 import java.io.File;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.util.ArrayList;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.zip.ZipEntry;
56 import java.util.zip.ZipInputStream;
57
58
59 /**
60  * Created by TALIO on 4/20/2016.
61  */
62 public class UploadValidationManagerImpl implements UploadValidationManager {
63   private static FileContentHandler getFileContentMapFromZip(byte[] uploadFileData)
64       throws IOException, CoreException {
65
66     ZipEntry zipEntry;
67     List<String> folderList = new ArrayList<>();
68     FileContentHandler mapFileContent = new FileContentHandler();
69     try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData))) {
70
71       byte[] fileByteContent;
72       String currentEntryName;
73
74       while ((zipEntry = inputZipStream.getNextEntry()) != null) {
75         currentEntryName = zipEntry.getName();
76         // else, get the file content (as byte array) and save it in a map.
77         fileByteContent = FileUtils.toByteArray(inputZipStream);
78
79         int index = lastIndexFileSeparatorIndex(currentEntryName);
80         String currSubstringWithoutSeparator =
81             currentEntryName.substring(index + 1, currentEntryName.length());
82         if (index != -1) {
83           if (currSubstringWithoutSeparator.length() > 0) {
84             mapFileContent.addFile(currentEntryName.substring(index + 1, currentEntryName.length()),
85                 fileByteContent);
86           } else {
87             folderList.add(currentEntryName);
88           }
89         } else {
90           mapFileContent.addFile(currentEntryName, fileByteContent);
91         }
92       }
93     } catch (RuntimeException exception) {
94       throw new IOException(exception);
95     }
96
97     if (CollectionUtils.isNotEmpty(folderList)) {
98       MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
99       throw new CoreException((new ErrorCode.ErrorCodeBuilder())
100           .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
101           .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
102           .withCategory(ErrorCategory.APPLICATION).build());
103
104     }
105
106     return mapFileContent;
107   }
108
109   private static int lastIndexFileSeparatorIndex(String filePath) {
110     int length = filePath.length() - 1;
111
112     for (int i = length; i >= 0; i--) {
113       char currChar = filePath.charAt(i);
114       if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
115         return i;
116       }
117     }
118     // if we've reached to the start of the string and didn't find file separator - return -1
119     return -1;
120   }
121
122   @Override
123   public ValidationFileResponse validateFile(String type, InputStream fileToValidate)
124       throws IOException {
125     ValidationFileResponse validationFileResponse = new ValidationFileResponse();
126
127     HeatTreeManager tree;
128     ValidationStructureList validationStructureList = new ValidationStructureList();
129     if (type.toLowerCase().equals("heat")) {
130       FileContentHandler content = getFileContent(fileToValidate);
131       if (!content.containsFile(SdcCommon.MANIFEST_NAME)) {
132         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
133             LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
134             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ZIP);
135         throw new CoreException((new ErrorCode.ErrorCodeBuilder())
136             .withMessage(Messages.MANIFEST_NOT_EXIST.getErrorMessage())
137             .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
138             .withCategory(ErrorCategory.APPLICATION).build());
139       }
140       Map<String, List<ErrorMessage>> errors = validateHeatUploadData(content);
141       tree = HeatTreeManagerUtil.initHeatTreeManager(content);
142       tree.createTree();
143
144       if (MapUtils.isNotEmpty(errors)) {
145         tree.addErrors(errors);
146         validationStructureList.setImportStructure(tree.getTree());
147       }
148
149     } else {
150       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
151           LoggerTragetServiceName.VALIDATE_FILE_TYPE, ErrorLevel.ERROR.name(),
152           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_FILE_TYPE);
153       throw new RuntimeException("invalid type:" + type);
154     }
155     validationFileResponse.setValidationData(validationStructureList);
156     return validationFileResponse;
157   }
158
159   private Map<String, List<ErrorMessage>> validateHeatUploadData(FileContentHandler fileContentMap) {
160     ValidationManager validationManager =
161         ValidationManagerUtil.initValidationManager(fileContentMap);
162     return validationManager.validate();
163   }
164
165   private FileContentHandler getFileContent(InputStream is) throws IOException {
166     return getFileContentMapFromZip(FileUtils.toByteArray(is));
167
168
169   }
170
171 }