Merge "Fix - Blockstorage and Compute in VF linking"
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / CsarValidationUtils.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.be.components.impl;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import java.util.stream.Collectors;
32
33 import org.apache.commons.lang3.tuple.ImmutablePair;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager;
35 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.tosca.CsarUtils;
39 import org.openecomp.sdc.common.util.GeneralUtility;
40 import org.openecomp.sdc.exception.ResponseFormat;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import fj.data.Either;
45
46 public class CsarValidationUtils {
47
48         private static Logger log = LoggerFactory.getLogger(CsarValidationUtils.class.getName());
49
50         private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
51
52         private static final String CSAR_VERSION = "CSAR-Version";
53
54         private static final String CREATED_BY = "Created-By";
55
56         private static final String NEW_LINE_DELM = "\n";
57
58         public final static String TOSCA_METADATA_FILE = "TOSCA-Metadata/TOSCA.meta";
59
60         public static final String TOSCA_META_ENTRY_DEFINITIONS = "Entry-Definitions";
61
62         private static final String[] TOSCA_METADATA_FIELDS = { TOSCA_META_FILE_VERSION, CSAR_VERSION, CREATED_BY, TOSCA_META_ENTRY_DEFINITIONS };
63
64         public final static String ARTIFACTS_METADATA_FILE = "HEAT.meta";
65
66         public static final String TOSCA_CSAR_EXTENSION = ".csar";
67 /**
68  * Validates Csar
69  * @param csar
70  * @param csarUUID
71  * @param componentsUtils
72  * @return
73  */
74         public static Either<Boolean, ResponseFormat> validateCsar(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
75                 Either<Boolean, ResponseFormat> validateStatus = validateIsTOSCAMetadataExist(csar, csarUUID, componentsUtils);
76                 if (validateStatus.isRight()) {
77                         return Either.right(validateStatus.right().value());
78                 }
79                 
80                 removeNonUniqueArtifactsFromCsar(csar);
81                 
82                 log.trace("TOSCA-Metadata/TOSCA.meta file found, CSAR id {}", csarUUID);
83                 validateStatus = validateTOSCAMetadataFile(csar, csarUUID, componentsUtils);
84                 if (validateStatus.isRight()) {
85                         return Either.right(validateStatus.right().value());
86                 }
87                 return Either.left(true);
88         }
89
90         private static void removeNonUniqueArtifactsFromCsar(Map<String, byte[]> csar) {
91                 
92                 List<String> nonUniqueArtifactsToRemove = new ArrayList<>();
93                 String[] paths = csar.keySet().toArray(new String[csar.keySet().size()]);
94                 int numberOfArtifacts = paths.length;
95                 for(int i = 0; i < numberOfArtifacts; ++i ){
96                         collectNonUniqueArtifact(paths, i, numberOfArtifacts, nonUniqueArtifactsToRemove);
97                 }
98                 nonUniqueArtifactsToRemove.stream().forEach(path->csar.remove(path));
99         }
100         
101         private static void collectNonUniqueArtifact( String[] paths, int currInd, int numberOfArtifacts, List<String> nonUniqueArtifactsToRemove) {
102
103                 String[] parsedPath = paths[currInd].split("/");
104                 String[] otherParsedPath;
105                 int artifactNameInd = parsedPath.length - 1;
106                 for(int j = currInd + 1; j < numberOfArtifacts; ++j ){
107                         otherParsedPath = paths[j].split("/");
108                         if(parsedPath.length == otherParsedPath.length && parsedPath.length > 3 && isEqualArtifactNames(parsedPath, otherParsedPath)){
109                                 log.error("Can't upload two artifact with the same name {}. The artifact with path {} will be handled, and the artifact with path {} will be ignored. ",
110                                                 parsedPath[artifactNameInd], paths[currInd], paths[j]);
111                                 nonUniqueArtifactsToRemove.add(paths[j]);
112                         }
113                 }
114         }
115
116         private static boolean isEqualArtifactNames(String[] parsedPath, String[] otherParsedPath) {
117                 boolean isEqualArtifactNames = false;
118                 int artifactNameInd = parsedPath.length - 1;
119                 int artifactGroupTypeInd = parsedPath.length - 3;
120                 String groupType = parsedPath[artifactGroupTypeInd];
121                 String artifactName = parsedPath[artifactNameInd];
122                 String otherGroupType = otherParsedPath[artifactGroupTypeInd];
123                 String otherArtifactName = otherParsedPath[artifactNameInd];
124                 String vfcToscaName = parsedPath.length == 5 ? parsedPath[1] : null;
125                 
126                 if(artifactName.equalsIgnoreCase(otherArtifactName) && groupType.equalsIgnoreCase(otherGroupType)){
127                         isEqualArtifactNames = vfcToscaName == null ? true : vfcToscaName.equalsIgnoreCase(otherParsedPath[1]);
128                 }
129                 return isEqualArtifactNames;
130         }
131
132         public static Either<ImmutablePair<String, String>, ResponseFormat> getToscaYaml(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
133                 Either<Boolean, ResponseFormat> validateStatus = validateIsTOSCAMetadataExist(csar, csarUUID, componentsUtils);
134                 if (validateStatus.isRight()) {
135                         return Either.right(validateStatus.right().value());
136                 }
137                 byte[] toscaMetaBytes = csar.get(TOSCA_METADATA_FILE);
138                 Properties props = new Properties();
139                 try {
140                         props.load(new ByteArrayInputStream(toscaMetaBytes));
141                 } catch (IOException e) {
142                         log.debug("TOSCA-Metadata/TOSCA.meta file is not in expected key-value form in csar, csar ID {}", csarUUID, e);
143                         BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
144                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID));
145                 }
146
147                 String yamlFileName = props.getProperty(TOSCA_META_ENTRY_DEFINITIONS);
148
149                 if (!csar.containsKey(yamlFileName)) {
150                         log.debug("Entry-Definitions entry not found in TOSCA-Metadata/TOSCA.meta file, csar ID {}", csarUUID);
151                         BeEcompErrorManager.getInstance().logInternalDataError("Entry-Definitions entry not found in TOSCA-Metadata/TOSCA.meta file in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
152                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, yamlFileName));
153                 }
154
155                 log.trace("Found Entry-Definitions property in TOSCA-Metadata/TOSCA.meta, Entry-Definitions: {}, CSAR id: {}", yamlFileName, csarUUID);
156                 byte[] yamlFileBytes = csar.get(yamlFileName);
157                 if (yamlFileBytes == null) {
158                         log.debug("Entry-Definitions {} file not found in csar, csar ID {}", yamlFileName, csarUUID);
159                         BeEcompErrorManager.getInstance().logInternalDataError("Entry-Definitions " + yamlFileName + " file not found in CSAR with id " + csarUUID, "CSAR structure is invalid", ErrorSeverity.ERROR);
160                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, yamlFileName));
161                 }
162
163                 String yamlFileContents = new String(yamlFileBytes);
164
165                 return Either.left(new ImmutablePair<String, String>(yamlFileName, yamlFileContents));
166         }
167
168         public static Either<ImmutablePair<String, String>, ResponseFormat> getArtifactsMeta(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
169                 
170                 if( !csar.containsKey(CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE) ) {
171                         log.debug("Entry-Definitions entry not found in TOSCA-Metadata/TOSCA.meta file, csar ID {}", csarUUID);
172                         BeEcompErrorManager.getInstance().logInternalDataError("Entry-Definitions entry not found in TOSCA-Metadata/TOSCA.meta file in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
173                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, ARTIFACTS_METADATA_FILE));
174                 }
175
176                 log.trace("Found Entry-Definitions property in TOSCA-Metadata/TOSCA.meta, Entry-Definitions: {}, CSAR id: {}", ARTIFACTS_METADATA_FILE, csarUUID);
177                 byte[] artifactsMetaBytes = csar.get(CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE);
178                 if (artifactsMetaBytes == null) {
179                         log.debug("Entry-Definitions {}{} file not found in csar, csar ID {}", CsarUtils.ARTIFACTS_PATH, ARTIFACTS_METADATA_FILE, csarUUID);
180                         BeEcompErrorManager.getInstance().logInternalDataError("Entry-Definitions " + CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE + " file not found in CSAR with id " + csarUUID, "CSAR structure is invalid", ErrorSeverity.ERROR);
181                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.YAML_NOT_FOUND_IN_CSAR, csarUUID, CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE));
182                 }
183
184                 String artifactsFileContents = new String(artifactsMetaBytes);
185
186                 return Either.left(new ImmutablePair<String, String>(CsarUtils.ARTIFACTS_PATH + ARTIFACTS_METADATA_FILE, artifactsFileContents));
187         }
188
189         public static Either<ImmutablePair<String, byte[]>, ResponseFormat> getArtifactsContent(String csarUUID, Map<String, byte[]> csar, String artifactPath, String artifactName, ComponentsUtils componentsUtils) {
190                 if (!csar.containsKey(artifactPath)) {
191                         log.debug("Entry-Definitions entry not found in Artifacts/HEAT.meta file, csar ID {}", csarUUID);
192                         BeEcompErrorManager.getInstance().logInternalDataError("Entry-Definitions entry not found in TOSCA-Metadata/TOSCA.meta file in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
193                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, CsarUtils.ARTIFACTS_PATH + artifactName, csarUUID));
194                 }
195
196                 log.trace("Found Entry-Definitions property in Artifacts/HEAT.meta, Entry-Definitions: {}, CSAR id: {}", artifactPath, csarUUID);
197                 byte[] artifactFileBytes = csar.get(artifactPath);
198                 if (artifactFileBytes == null) {
199                         log.debug("Entry-Definitions {}{} file not found in csar, csar ID {}", CsarUtils.ARTIFACTS_PATH, artifactName, csarUUID);
200                         BeEcompErrorManager.getInstance().logInternalDataError("Entry-Definitions " + artifactPath + " file not found in CSAR with id " + csarUUID, "CSAR structure is invalid", ErrorSeverity.ERROR);
201                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, artifactPath, csarUUID));
202                 }
203
204                 return Either.left(new ImmutablePair<String, byte[]>(artifactName, artifactFileBytes));
205         }
206
207         private static Either<Boolean, ResponseFormat> validateTOSCAMetadataFile(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
208
209                 byte[] toscaMetaBytes = csar.get(TOSCA_METADATA_FILE);
210                 String toscaMetadata = new String(toscaMetaBytes);
211                 String[] splited = toscaMetadata.split(NEW_LINE_DELM);
212                 if (splited == null || splited.length < TOSCA_METADATA_FIELDS.length) {
213                         log.debug("TOSCA-Metadata/TOSCA.meta file is not in expected key-value form in csar, csar ID {}", csarUUID);
214                         BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
215                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID));
216                 }
217                 /*
218                  * if(splited.length == TOSCA_METADATA_FIELDS.length){ if(!toscaMetadata.endsWith(NEW_LINE_DELM)){ log. debug("TOSCA-Metadata/TOSCA.meta file is not in expected key-value form in csar, csar ID {}" , csarUUID);
219                  * BeEcompErrorManager.getInstance(). logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " +csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR); return
220                  * Either.right(componentsUtils.getResponseFormat(ActionStatus. CSAR_INVALID_FORMAT, csarUUID)); } }
221                  */
222
223                 Either<Boolean, ResponseFormat> block_0Status = validateBlock_0(csarUUID, splited, componentsUtils);
224                 if (block_0Status.isRight()) {
225                         return Either.right(block_0Status.right().value());
226                 }
227
228                 return Either.left(true);
229
230         }
231
232         private static Either<Boolean, ResponseFormat> validateBlock_0(String csarUUID, String[] splited, ComponentsUtils componentsUtils) {
233                 int index = 0;
234                 for (String toscaField : TOSCA_METADATA_FIELDS) {
235
236                         Properties props = new Properties();
237
238                         try {
239                                 props.load(new ByteArrayInputStream(splited[index].getBytes()));
240                         } catch (IOException e) {
241                                 log.debug("TOSCA-Metadata/TOSCA.meta file is not in expected key-value form in csar, csar ID {}", csarUUID, e);
242                                 BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
243                                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID));
244                         }
245                         if (!props.containsKey(toscaField)) {
246                                 log.debug("TOSCA.meta file format is invalid: No new line after block_0 as expected in csar, csar ID {}", csarUUID);
247                                 BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
248                                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID));
249                         }
250                         String value = props.getProperty(toscaField);
251                         if (value == null || value.isEmpty()) {
252                                 log.debug("TOSCA-Metadata/TOSCA.meta file is not in expected key-value form in csar, csar ID {}", csarUUID);
253                                 BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
254                                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID));
255                         }
256
257                         // TOSCA-Meta-File-Version & CSAR-Version : digit.digit - format
258                         // validation
259                         if (toscaField.equals(TOSCA_META_FILE_VERSION) || toscaField.equals(CSAR_VERSION)) {
260                                 if (!validateTOSCAMetaProperty(value)) {
261                                         log.debug("TOSCA-Metadata/TOSCA.meta file contains {} in wrong format (digit.digit), csar ID {}", toscaField, csarUUID);
262                                         BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not in expected key-value form in CSAR with id " + csarUUID, "CSAR internals are invalid", ErrorSeverity.ERROR);
263                                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, csarUUID));
264                                 }
265                         }
266                         index++;
267                 }
268                 return Either.left(true);
269         }
270
271         private static boolean validateTOSCAMetaProperty(String toscaProperty) {
272                 final String FLOAT_STRING = "^\\d{1}[.]\\d{1}$";
273                 final Pattern FLOAT_PATTERN = Pattern.compile(FLOAT_STRING);
274
275                 Matcher floatMatcher = FLOAT_PATTERN.matcher(toscaProperty);
276                 return floatMatcher.matches();
277         }
278
279         private static Either<Boolean, ResponseFormat> validateIsTOSCAMetadataExist(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
280                 if (csar == null || csar.isEmpty()) {
281                         log.debug("Error when fetching csar with ID {}", csarUUID);
282                         BeEcompErrorManager.getInstance().logBeDaoSystemError("Creating resource from CSAR: fetching CSAR with id " + csarUUID + " failed");
283                         ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID, csarUUID);
284                         return Either.right(responseFormat);
285                 }
286                 if (!csar.containsKey(TOSCA_METADATA_FILE)) {
287                         log.debug("TOSCA-Metadata/TOSCA.meta file not found in csar, csar ID {}", csarUUID);
288                         BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not found in CSAR with id " + csarUUID, "CSAR structure is invalid", ErrorSeverity.ERROR);
289                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID, csarUUID));
290                 }
291                 byte[] toscaMetaBytes = csar.get(TOSCA_METADATA_FILE);
292                 // && exchanged for ||
293                 if (toscaMetaBytes == null || toscaMetaBytes.length == 0) {
294                         log.debug("TOSCA-Metadata/TOSCA.meta file not found in csar, csar ID {}", csarUUID);
295                         BeEcompErrorManager.getInstance().logInternalDataError("TOSCA-Metadata/TOSCA.meta file not found in CSAR with id " + csarUUID, "CSAR structure is invalid", ErrorSeverity.ERROR);
296                         return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID, csarUUID));
297                 }
298
299                 return Either.left(Boolean.TRUE);
300         }
301
302         public static Either<String, ResponseFormat> getToscaYamlChecksum(Map<String, byte[]> csar, String csarUUID, ComponentsUtils componentsUtils) {
303
304                 Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlRes = getToscaYaml(csar, csarUUID, componentsUtils);
305                 if (toscaYamlRes.isRight() || toscaYamlRes.left().value() == null || toscaYamlRes.left().value().getRight() == null) {
306                         log.debug("Faild to create toscaYamlChecksum for csar, csar ID {}", csarUUID);
307                         return Either.right(toscaYamlRes.right().value());
308                 }
309
310                 String newCheckSum = GeneralUtility.calculateMD5Base64EncodedByByteArray(toscaYamlRes.left().value().getRight().getBytes());
311                 return Either.left(newCheckSum);
312
313         }
314
315         public static boolean isCsarPayloadName(String payloadName) {
316                 if (payloadName == null)
317                         return false;
318                 return payloadName.toLowerCase().endsWith(TOSCA_CSAR_EXTENSION);
319         }
320
321 }