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.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
35 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
36 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
37 import org.openecomp.sdc.validation.UploadValidationManager;
38 import org.openecomp.sdc.validation.types.ValidationFileResponse;
39 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
41 import java.io.ByteArrayInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.util.ArrayList;
46 import java.util.List;
48 import java.util.zip.ZipEntry;
49 import java.util.zip.ZipInputStream;
53 * Created by TALIO on 4/20/2016.
55 public class UploadValidationManagerImpl implements UploadValidationManager {
56 private static FileContentHandler getFileContentMapFromZip(byte[] uploadFileData)
57 throws IOException, CoreException {
60 List<String> folderList = new ArrayList<>();
61 FileContentHandler mapFileContent = new FileContentHandler();
62 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData))) {
64 byte[] fileByteContent;
65 String currentEntryName;
67 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
68 currentEntryName = zipEntry.getName();
69 // else, get the file content (as byte array) and save it in a map.
70 fileByteContent = FileUtils.toByteArray(inputZipStream);
72 int index = lastIndexFileSeparatorIndex(currentEntryName);
73 String currSubstringWithoutSeparator =
74 currentEntryName.substring(index + 1, currentEntryName.length());
76 if (currSubstringWithoutSeparator.length() > 0) {
77 mapFileContent.addFile(currentEntryName.substring(index + 1, currentEntryName.length()),
80 folderList.add(currentEntryName);
83 mapFileContent.addFile(currentEntryName, fileByteContent);
86 } catch (RuntimeException exception) {
87 throw new IOException(exception);
90 if (CollectionUtils.isNotEmpty(folderList)) {
91 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
92 .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
93 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
94 .withCategory(ErrorCategory.APPLICATION).build());
98 return mapFileContent;
101 private static int lastIndexFileSeparatorIndex(String filePath) {
102 int length = filePath.length() - 1;
104 for (int i = length; i >= 0; i--) {
105 char currChar = filePath.charAt(i);
106 if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
110 // if we've reached to the start of the string and didn't find file separator - return -1
115 public ValidationFileResponse validateFile(String type, InputStream fileToValidate)
117 ValidationFileResponse validationFileResponse = new ValidationFileResponse();
119 HeatTreeManager tree;
120 ValidationStructureList validationStructureList = new ValidationStructureList();
121 if (type.equalsIgnoreCase("heat")) {
122 FileContentHandler content = getFileContent(fileToValidate);
123 if (!content.containsFile(SdcCommon.MANIFEST_NAME)) {
124 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
125 .withMessage(Messages.MANIFEST_NOT_EXIST.getErrorMessage())
126 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
127 .withCategory(ErrorCategory.APPLICATION).build());
129 Map<String, List<ErrorMessage>> errors = validateHeatUploadData(content);
130 tree = HeatTreeManagerUtil.initHeatTreeManager(content);
133 if (MapUtils.isNotEmpty(errors)) {
134 tree.addErrors(errors);
135 validationStructureList.setImportStructure(tree.getTree());
139 throw new RuntimeException("invalid type:" + type);
141 validationFileResponse.setValidationData(validationStructureList);
142 return validationFileResponse;
145 private Map<String, List<ErrorMessage>> validateHeatUploadData(FileContentHandler fileContentMap) {
146 ValidationManager validationManager =
147 ValidationManagerUtil.initValidationManager(fileContentMap);
148 return validationManager.validate();
151 private FileContentHandler getFileContent(InputStream is) throws IOException {
152 return getFileContentMapFromZip(FileUtils.toByteArray(is));