79c4dc2d9639b27448773db7c1e21de10790f584
[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.validators;
22
23 import org.openecomp.core.utilities.json.JsonUtil;
24 import org.openecomp.sdc.validation.Validator;
25 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
26 import org.openecomp.core.validation.types.GlobalValidationContext;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
35 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
36 import org.openecomp.sdc.logging.types.LoggerConstants;
37 import org.openecomp.sdc.logging.types.LoggerErrorCode;
38 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
39 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
40
41 import java.io.InputStream;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Optional;
45
46
47 public class ManifestValidator implements Validator {
48   public static final MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
49   private static Logger logger = (Logger) LoggerFactory.getLogger(YamlValidator.class);
50
51   @Override
52   public void validate(GlobalValidationContext globalContext) {
53     mdcDataDebugMessage.debugEntryMessage(null, null);
54
55     Optional<InputStream> content = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
56     ManifestContent manifestContent;
57
58     try {
59       if (content.isPresent()) {
60         manifestContent = JsonUtil.json2Object(content.get(), ManifestContent.class);
61       } else {
62         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
63             LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
64             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE);
65         throw new Exception("The manifest file '" + SdcCommon.MANIFEST_NAME + "' has no content");
66       }
67     } catch (Exception re) {
68       logger.debug("",re);
69       globalContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
70           Messages.INVALID_MANIFEST_FILE.getErrorMessage(),
71           LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT,
72           LoggerErrorDescription.INVALID_MANIFEST);
73       return;
74     }
75
76     List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
77     manifestFiles.stream().filter(name ->
78         !globalContext.getFileContextMap().containsKey(name)
79     ).forEach(name -> globalContext
80         .addMessage(name, ErrorLevel.ERROR, Messages.MISSING_FILE_IN_ZIP.getErrorMessage(),
81             LoggerTragetServiceName.VALIDATE_FILE_IN_ZIP, LoggerErrorDescription.MISSING_FILE));
82
83     globalContext.getFileContextMap().keySet().stream().filter(name ->
84         isNotManifestFiles(manifestFiles, name) && isNotManifestName(name)
85     ).forEach(name ->
86         globalContext.addMessage(name, ErrorLevel.WARNING,
87             Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage(),
88             LoggerTragetServiceName.VALIDATE_FILE_IN_MANIFEST, LoggerErrorDescription.MISSING_FILE)
89     );
90
91     mdcDataDebugMessage.debugExitMessage(null, null);
92   }
93
94   private boolean isNotManifestFiles(List<String> manifestFiles, String name) {
95     return !manifestFiles.contains(name);
96   }
97
98   private boolean isNotManifestName(String name) {
99     return !SdcCommon.MANIFEST_NAME.equals(name);
100   }
101
102
103   private List<String> getManifestFileList(ManifestContent manifestContent,
104                                            GlobalValidationContext context) {
105
106
107     mdcDataDebugMessage.debugEntryMessage(null, null);
108
109     ManifestScanner manifestScanner = new ManifestScanner();
110     manifestScanner.init(context);
111     manifestScanner.scan(null, manifestContent.getData(), context);
112
113     mdcDataDebugMessage.debugExitMessage(null, null);
114     return manifestScanner.getFileList();
115   }
116
117
118   private class ManifestScanner {
119     private GlobalValidationContext globalValidationContext;
120     private List<String> fileList;
121
122     public void init(GlobalValidationContext globalValidationContext) {
123       this.globalValidationContext = globalValidationContext;
124       this.fileList = new ArrayList<>();
125     }
126
127
128     public void scan(FileData fileData, List<FileData> data,
129                      GlobalValidationContext globalContext) {
130       if (fileData == null) {
131         for (FileData childFileData : data) {
132           if (childFileData.getType() != null
133               && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
134             globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
135                 ErrorMessagesFormatBuilder
136                     .getErrorWithParameters(Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()),
137                 LoggerTragetServiceName.SCAN_MANIFEST_STRUCTURE,
138                 "env file is not associated to HEAT file");
139           }
140         }
141       }
142       if (fileData != null) {
143         fileList.add(fileData.getFile());
144         validateFileTypeVsFileName(fileData);
145       }
146       if (data == null) {
147         return;
148       }
149       data.forEach(chileFileData -> scan(chileFileData, chileFileData.getData(), globalContext));
150     }
151
152
153     public List<String> getFileList() {
154       return this.fileList;
155     }
156
157     private void validateFileTypeVsFileName(FileData fileData) {
158       String fileName = fileData.getFile();
159       if (fileName == null) {
160         this.globalValidationContext.addMessage(SdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
161             Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage(),
162             LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Missing file name in manifest");
163
164       }
165       FileData.Type type = fileData.getType();
166       if (type == null) {
167         this.globalValidationContext
168             .addMessage(fileName, ErrorLevel.ERROR, Messages.INVALID_FILE_TYPE.getErrorMessage(),
169                 LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME, "Invalid file type");
170       } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
171           || type.equals(FileData.Type.HEAT)) {
172         if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
173           this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
174               ErrorMessagesFormatBuilder
175                   .getErrorWithParameters(Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
176                       fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
177               "Wrong HEAT file extention");
178         }
179       } else if (type.equals(FileData.Type.HEAT_ENV)) {
180         if (fileName != null && !fileName.endsWith(".env")) {
181           this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
182               ErrorMessagesFormatBuilder
183                   .getErrorWithParameters(Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
184                       fileName), LoggerTragetServiceName.VALIDATE_FILE_TYPE_AND_NAME,
185               "Wrong env file extention");
186         }
187       }
188     }
189   }
190
191
192 }