push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / ManifestValidator.java
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.core.validation.errors.ErrorMessagesFormatBuilder;
25 import org.openecomp.core.validation.errors.Messages;
26 import org.openecomp.core.validation.interfaces.Validator;
27 import org.openecomp.core.validation.types.GlobalValidationContext;
28 import org.openecomp.sdc.common.utils.AsdcCommon;
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.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.io.InputStream;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 public class ManifestValidator implements Validator {
40
41   private static Logger logger = LoggerFactory.getLogger(YamlValidator.class);
42
43
44   @Override
45   public void validate(GlobalValidationContext globalContext) {
46
47
48     InputStream content = globalContext.getFileContent(AsdcCommon.MANIFEST_NAME);
49     ManifestContent manifestContent;
50
51     try {
52       manifestContent = JsonUtil.json2Object(content, ManifestContent.class);
53     } catch (RuntimeException re) {
54       globalContext.addMessage(AsdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
55           Messages.INVALID_MANIFEST_FILE.getErrorMessage());
56       return;
57     }
58
59     List<String> manifestFiles = getManifestFileList(manifestContent, globalContext);
60     manifestFiles.stream().filter(name ->
61         !globalContext.getFileContextMap().containsKey(name)
62     ).forEach(name -> globalContext
63         .addMessage(name, ErrorLevel.ERROR, Messages.MISSING_FILE_IN_ZIP.getErrorMessage()));
64
65     globalContext.getFileContextMap().keySet().stream().filter(name ->
66         !manifestFiles.contains(name) && !AsdcCommon.MANIFEST_NAME.equals(name)
67     ).forEach(name ->
68         globalContext.addMessage(name, ErrorLevel.WARNING,
69             Messages.MISSING_FILE_IN_MANIFEST.getErrorMessage())
70     );
71
72   }
73
74   private List<String> getManifestFileList(ManifestContent manifestContent,
75                                            GlobalValidationContext context) {
76     ManifestScanner manifestScanner = new ManifestScanner();
77     manifestScanner.init(context);
78     manifestScanner.scan(null, manifestContent.getData(), context);
79     return manifestScanner.getFileList();
80   }
81
82
83   private class ManifestScanner {
84     private GlobalValidationContext globalValidationContext;
85     private List<String> fileList;
86
87     public void init(GlobalValidationContext globalValidationContext) {
88       this.globalValidationContext = globalValidationContext;
89       this.fileList = new ArrayList<>();
90     }
91
92
93     public void scan(FileData fileData, List<FileData> data,
94                      GlobalValidationContext globalContext) {
95       if (fileData == null) {
96         for (FileData childFileData : data) {
97           if (childFileData.getType() != null
98               && childFileData.getType().equals(FileData.Type.HEAT_ENV)) {
99             globalContext.addMessage(childFileData.getFile(), ErrorLevel.ERROR,
100                 ErrorMessagesFormatBuilder
101                     .getErrorWithParameters(Messages.ENV_NOT_ASSOCIATED_TO_HEAT.getErrorMessage()));
102           }
103         }
104       }
105       if (fileData != null) {
106         fileList.add(fileData.getFile());
107         validateFileTypeVsFileName(fileData);
108       }
109       if (data == null) {
110         return;
111       }
112       data.stream().forEach(chileFileData -> {
113         scan(chileFileData, chileFileData.getData(), globalContext);
114       });
115     }
116
117
118     public List<String> getFileList() {
119       return this.fileList;
120     }
121
122     private void validateFileTypeVsFileName(FileData fileData) {
123       String fileName = fileData.getFile();
124       if (fileName == null) {
125         this.globalValidationContext.addMessage(AsdcCommon.MANIFEST_NAME, ErrorLevel.ERROR,
126             Messages.MISSING_FILE_NAME_IN_MANIFEST.getErrorMessage());
127
128       }
129       FileData.Type type = fileData.getType();
130       if (type == null) {
131         this.globalValidationContext
132             .addMessage(fileName, ErrorLevel.ERROR, Messages.INVALID_FILE_TYPE.getErrorMessage());
133       } else if (type.equals(FileData.Type.HEAT_NET) || type.equals(FileData.Type.HEAT_VOL)
134           || type.equals(FileData.Type.HEAT)) {
135         if (fileName != null && !fileName.endsWith(".yml") && !fileName.endsWith(".yaml")) {
136           this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
137               ErrorMessagesFormatBuilder
138                   .getErrorWithParameters(Messages.WRONG_HEAT_FILE_EXTENSION.getErrorMessage(),
139                       fileName));
140         }
141       } else if (type.equals(FileData.Type.HEAT_ENV)) {
142         if (fileName != null && !fileName.endsWith(".env")) {
143           this.globalValidationContext.addMessage(fileName, ErrorLevel.ERROR,
144               ErrorMessagesFormatBuilder
145                   .getErrorWithParameters(Messages.WRONG_ENV_FILE_EXTENSION.getErrorMessage(),
146                       fileName));
147         }
148       }
149     }
150   }
151
152
153 }