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 java.io.IOException;
24 import java.io.InputStream;
25 import java.util.List;
27 import java.util.Objects;
28 import org.apache.commons.collections4.MapUtils;
29 import org.openecomp.core.utilities.file.FileContentHandler;
30 import org.openecomp.core.utilities.file.FileUtils;
31 import org.openecomp.core.validation.api.ValidationManager;
32 import org.openecomp.sdc.common.errors.CoreException;
33 import org.openecomp.sdc.common.errors.ErrorCategory;
34 import org.openecomp.sdc.common.errors.ErrorCode;
35 import org.openecomp.sdc.common.errors.Messages;
36 import org.openecomp.sdc.common.utils.SdcCommon;
37 import org.openecomp.sdc.common.zip.ZipUtils;
38 import org.openecomp.sdc.common.zip.exception.ZipException;
39 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
41 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
42 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
43 import org.openecomp.sdc.validation.UploadValidationManager;
44 import org.openecomp.sdc.validation.types.ValidationFileResponse;
45 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
49 * Created by TALIO on 4/20/2016.
51 public class UploadValidationManagerImpl implements UploadValidationManager {
53 private static FileContentHandler getFileContentMapFromZip(byte[] uploadFileData) throws IOException {
54 final Map<String, byte[]> zipFileAndByteMap;
56 zipFileAndByteMap = ZipUtils.readZip(uploadFileData, true);
57 } catch (final ZipException e) {
58 throw new IOException(e);
61 final boolean zipHasFolders = zipFileAndByteMap.values().stream().anyMatch(Objects::isNull);
63 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
64 .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
65 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
66 .withCategory(ErrorCategory.APPLICATION).build());
68 final FileContentHandler mapFileContent = new FileContentHandler();
69 zipFileAndByteMap.entrySet().stream()
70 .filter(entry -> entry.getValue() != null)
71 .forEach(zipEntry -> mapFileContent.addFile(zipEntry.getKey(), zipEntry.getValue()));
73 return mapFileContent;
77 public ValidationFileResponse validateFile(String type, InputStream fileToValidate)
79 ValidationFileResponse validationFileResponse = new ValidationFileResponse();
82 ValidationStructureList validationStructureList = new ValidationStructureList();
83 if (type.equalsIgnoreCase("heat")) {
84 FileContentHandler content = getFileContent(fileToValidate);
85 if (!content.containsFile(SdcCommon.MANIFEST_NAME)) {
86 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
87 .withMessage(Messages.MANIFEST_NOT_EXIST.getErrorMessage())
88 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
89 .withCategory(ErrorCategory.APPLICATION).build());
91 Map<String, List<ErrorMessage>> errors = validateHeatUploadData(content);
92 tree = HeatTreeManagerUtil.initHeatTreeManager(content);
95 if (MapUtils.isNotEmpty(errors)) {
96 tree.addErrors(errors);
97 validationStructureList.setImportStructure(tree.getTree());
101 throw new RuntimeException("invalid type:" + type);
103 validationFileResponse.setValidationData(validationStructureList);
104 return validationFileResponse;
107 private Map<String, List<ErrorMessage>> validateHeatUploadData(FileContentHandler fileContentMap) {
108 ValidationManager validationManager =
109 ValidationManagerUtil.initValidationManager(fileContentMap);
110 return validationManager.validate();
113 private FileContentHandler getFileContent(InputStream is) throws IOException {
114 return getFileContentMapFromZip(FileUtils.toByteArray(is));