2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.validation.impl;
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.types.LoggerConstants;
39 import org.openecomp.sdc.logging.types.LoggerErrorCode;
40 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
41 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
42 import org.openecomp.sdc.validation.UploadValidationManager;
43 import org.openecomp.sdc.validation.types.ValidationFileResponse;
44 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
47 import java.io.ByteArrayInputStream;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.util.ArrayList;
52 import java.util.List;
54 import java.util.zip.ZipEntry;
55 import java.util.zip.ZipInputStream;
59 * Created by TALIO on 4/20/2016.
61 public class UploadValidationManagerImpl implements UploadValidationManager {
62 private static FileContentHandler getFileContentMapFromZip(byte[] uploadFileData)
63 throws IOException, CoreException {
66 List<String> folderList = new ArrayList<>();
67 FileContentHandler mapFileContent = new FileContentHandler();
68 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData))) {
70 byte[] fileByteContent;
71 String currentEntryName;
73 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
74 currentEntryName = zipEntry.getName();
75 // else, get the file content (as byte array) and save it in a map.
76 fileByteContent = FileUtils.toByteArray(inputZipStream);
78 int index = lastIndexFileSeparatorIndex(currentEntryName);
79 String currSubstringWithoutSeparator =
80 currentEntryName.substring(index + 1, currentEntryName.length());
82 if (currSubstringWithoutSeparator.length() > 0) {
83 mapFileContent.addFile(currentEntryName.substring(index + 1, currentEntryName.length()),
86 folderList.add(currentEntryName);
89 mapFileContent.addFile(currentEntryName, fileByteContent);
92 } catch (RuntimeException exception) {
93 throw new IOException(exception);
96 if (CollectionUtils.isNotEmpty(folderList)) {
97 MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
98 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
99 .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
100 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
101 .withCategory(ErrorCategory.APPLICATION).build());
105 return mapFileContent;
108 private static int lastIndexFileSeparatorIndex(String filePath) {
109 int length = filePath.length() - 1;
111 for (int i = length; i >= 0; i--) {
112 char currChar = filePath.charAt(i);
113 if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
117 // if we've reached to the start of the string and didn't find file separator - return -1
122 public ValidationFileResponse validateFile(String type, InputStream fileToValidate)
124 ValidationFileResponse validationFileResponse = new ValidationFileResponse();
126 HeatTreeManager tree;
127 ValidationStructureList validationStructureList = new ValidationStructureList();
128 if (type.toLowerCase().equals("heat")) {
129 FileContentHandler content = getFileContent(fileToValidate);
130 if (!content.containsFile(SdcCommon.MANIFEST_NAME)) {
131 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
132 .withMessage(Messages.MANIFEST_NOT_EXIST.getErrorMessage())
133 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
134 .withCategory(ErrorCategory.APPLICATION).build());
136 Map<String, List<ErrorMessage>> errors = validateHeatUploadData(content);
137 tree = HeatTreeManagerUtil.initHeatTreeManager(content);
140 if (MapUtils.isNotEmpty(errors)) {
141 tree.addErrors(errors);
142 validationStructureList.setImportStructure(tree.getTree());
146 throw new RuntimeException("invalid type:" + type);
148 validationFileResponse.setValidationData(validationStructureList);
149 return validationFileResponse;
152 private Map<String, List<ErrorMessage>> validateHeatUploadData(FileContentHandler fileContentMap) {
153 ValidationManager validationManager =
154 ValidationManagerUtil.initValidationManager(fileContentMap);
155 return validationManager.validate();
158 private FileContentHandler getFileContent(InputStream is) throws IOException {
159 return getFileContentMapFromZip(FileUtils.toByteArray(is));