90f8ad369bc695ab8eb9d4031086f7f0a5a918f1
[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 java.io.IOException;
24 import java.io.InputStream;
25 import java.util.List;
26 import java.util.Map;
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;
46
47
48 /**
49  * Created by TALIO on 4/20/2016.
50  */
51 public class UploadValidationManagerImpl implements UploadValidationManager {
52
53   private static FileContentHandler getFileContentMapFromZip(byte[] uploadFileData) throws IOException {
54     final Map<String, byte[]> zipFileAndByteMap;
55     try {
56       zipFileAndByteMap = ZipUtils.readZip(uploadFileData, true);
57     } catch (final ZipException e) {
58       throw new IOException(e);
59     }
60
61     final boolean zipHasFolders = zipFileAndByteMap.values().stream().anyMatch(Objects::isNull);
62     if (zipHasFolders) {
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());
67     }
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()));
72
73     return mapFileContent;
74   }
75
76   @Override
77   public ValidationFileResponse validateFile(String type, InputStream fileToValidate)
78       throws IOException {
79     ValidationFileResponse validationFileResponse = new ValidationFileResponse();
80
81     HeatTreeManager tree;
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());
90       }
91       Map<String, List<ErrorMessage>> errors = validateHeatUploadData(content);
92       tree = HeatTreeManagerUtil.initHeatTreeManager(content);
93       tree.createTree();
94
95       if (MapUtils.isNotEmpty(errors)) {
96         tree.addErrors(errors);
97         validationStructureList.setImportStructure(tree.getTree());
98       }
99
100     } else {
101       throw new RuntimeException("invalid type:" + type);
102     }
103     validationFileResponse.setValidationData(validationStructureList);
104     return validationFileResponse;
105   }
106
107   private Map<String, List<ErrorMessage>> validateHeatUploadData(FileContentHandler fileContentMap) {
108     ValidationManager validationManager =
109         ValidationManagerUtil.initValidationManager(fileContentMap);
110     return validationManager.validate();
111   }
112
113   private FileContentHandler getFileContent(InputStream is) throws IOException {
114     return getFileContentMapFromZip(FileUtils.toByteArray(is));
115
116
117   }
118
119 }