610738348597b57e0b57aeca2ed5484fd4695817
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  *  * Modifications copyright (c) 2020 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
24
25
26 import static org.openecomp.sdc.be.config.NonManoArtifactType.ONAP_PM_DICTIONARY;
27 import static org.openecomp.sdc.be.config.NonManoArtifactType.ONAP_SW_INFORMATION;
28 import static org.openecomp.sdc.be.config.NonManoArtifactType.ONAP_VES_EVENTS;
29 import static org.openecomp.sdc.tosca.csar.CSARConstants.CSAR_VERSION_1_0;
30 import static org.openecomp.sdc.tosca.csar.CSARConstants.CSAR_VERSION_1_1;
31 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_METADATA_LIMIT;
32 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA;
33 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_VNF_METADATA;
34 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_MANIFEST_FILE_EXT;
35 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_TYPE_PNF;
36 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_TYPE_VNF;
37 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CREATED_BY_ENTRY;
38 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CSAR_VERSION_ENTRY;
39 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
40 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ETSI_ENTRY_CERTIFICATE;
41 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ETSI_ENTRY_MANIFEST;
42 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.TOSCA_META_FILE_VERSION_ENTRY;
43 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_FILE_VERSION_1_0;
44 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
45
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Optional;
55 import java.util.Set;
56 import java.util.concurrent.CopyOnWriteArrayList;
57 import java.util.stream.Collectors;
58
59 import java.util.stream.Stream;
60 import org.apache.commons.collections.CollectionUtils;
61 import org.apache.commons.io.FilenameUtils;
62 import org.openecomp.core.impl.ToscaDefinitionImportHandler;
63 import org.openecomp.core.utilities.file.FileContentHandler;
64 import org.openecomp.sdc.be.config.NonManoArtifactType;
65 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareInformation;
66 import org.openecomp.sdc.be.csar.pnf.SoftwareInformationArtifactYamlParser;
67 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
68 import org.openecomp.sdc.common.errors.Messages;
69 import org.openecomp.sdc.common.utils.SdcCommon;
70 import org.openecomp.sdc.datatypes.error.ErrorLevel;
71 import org.openecomp.sdc.datatypes.error.ErrorMessage;
72 import org.openecomp.sdc.logging.api.Logger;
73 import org.openecomp.sdc.logging.api.LoggerFactory;
74 import org.openecomp.sdc.tosca.csar.Manifest;
75 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
76 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
77 import org.openecomp.sdc.tosca.csar.ToscaMetaEntry;
78 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
79 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageContentHandler;
80 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.exception.MissingCertificateException;
81 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.utils.FileExtractor;
82 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.utils.InternalFilesFilter;
83 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.exceptions.InvalidManifestMetadataException;
84 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
85 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
86 import org.yaml.snakeyaml.Yaml;
87
88 /**
89  * Validates the contents of the package to ensure it complies with the "CSAR with TOSCA-Metadata directory" structure
90  * as defined in ETSI GS NFV-SOL 004 v2.6.1.
91  */
92 class SOL004MetaDirectoryValidator implements Validator {
93
94     private static final Logger LOGGER = LoggerFactory.getLogger(SOL004MetaDirectoryValidator.class);
95
96     private static final String MANIFEST_SOURCE = "Source";
97     private static final String MANIFEST_NON_MANO_SOURCE = "Non-MANO Source";
98     private final List<ErrorMessage> errorsByFile = new CopyOnWriteArrayList<>();
99     private final SecurityManager securityManager;
100     private OnboardingPackageContentHandler contentHandler;
101     private Set<String> folderList;
102     private ToscaMetadata toscaMetadata;
103     private final InternalFilesFilter internalFilesFilter = new InternalFilesFilter();
104
105     public SOL004MetaDirectoryValidator() {
106         securityManager = SecurityManager.getInstance();
107     }
108
109     //for tests purpose
110     SOL004MetaDirectoryValidator(final SecurityManager securityManager) {
111         this.securityManager = securityManager;
112     }
113
114     @Override
115     public Map<String, List<ErrorMessage>> validateContent(final FileContentHandler fileContentHandler) {
116         this.contentHandler = (OnboardingPackageContentHandler) fileContentHandler;
117         this.folderList = contentHandler.getFolderList();
118         parseToscaMetadata();
119         verifyMetadataFile();
120
121         if (packageHasCertificate()) {
122             verifySignedFiles();
123         }
124         validatePMDictionaryContentsAgainstSchema();
125         return Collections.unmodifiableMap(getAnyValidationErrors());
126     }
127
128     private boolean packageHasCertificate() {
129         final String certificatePath = getCertificatePath().orElse(null);
130         return contentHandler.containsFile(certificatePath);
131     }
132
133     private Optional<String> getCertificatePath() {
134         return toscaMetadata.getEntry(ETSI_ENTRY_CERTIFICATE);
135     }
136
137     /**
138      * Parses the {@link org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo#TOSCA_META_PATH_FILE_NAME} file
139      */
140     private void parseToscaMetadata() {
141         try {
142             toscaMetadata =
143                     OnboardingToscaMetadata
144                             .parseToscaMetadataFile(contentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
145         } catch (final IOException e) {
146             reportError(ErrorLevel.ERROR, Messages.METADATA_PARSER_INTERNAL.getErrorMessage());
147             LOGGER.error(Messages.METADATA_PARSER_INTERNAL.getErrorMessage(), e.getMessage(), e);
148         }
149     }
150
151     private void verifyMetadataFile() {
152         if (toscaMetadata.isValid() && hasETSIMetadata()) {
153             verifyManifestNameAndExtension();
154             handleMetadataEntries();
155         } else {
156             errorsByFile.addAll(toscaMetadata.getErrors());
157         }
158     }
159
160     private void verifySignedFiles() {
161         final Map<String, String> signedFileMap = contentHandler.getFileAndSignaturePathMap(SecurityManager.ALLOWED_SIGNATURE_EXTENSIONS);
162         final String packageCertificatePath = getCertificatePath().orElse(null);
163         final byte[] packageCert = contentHandler.getFileContent(packageCertificatePath);
164         if (packageCert == null) {
165             throw new MissingCertificateException("Expected package certificate");
166         }
167         signedFileMap.entrySet().stream().filter(entry -> entry.getValue() != null).forEach(entry -> {
168             final String filePath = entry.getKey();
169             final String fileSignaturePath = entry.getValue();
170             final byte[] fileBytes = contentHandler.getFileContent(filePath);
171             final byte[] fileSignatureBytes = contentHandler.getFileContent(fileSignaturePath);
172             try {
173                 if (!securityManager.verifySignedData(fileSignatureBytes, packageCert, fileBytes)) {
174                     reportError(ErrorLevel.ERROR,
175                             Messages.ARTIFACT_INVALID_SIGNATURE.formatMessage(fileSignaturePath, filePath));
176                 }
177             } catch (final SecurityManagerException e) {
178                 final String errorMessage = Messages.ARTIFACT_SIGNATURE_VALIDATION_ERROR
179                         .formatMessage(fileSignaturePath, filePath, packageCertificatePath, e.getMessage());
180                 reportError(ErrorLevel.ERROR, errorMessage);
181                 LOGGER.error(errorMessage, e);
182             }
183         });
184     }
185
186     private void verifyManifestNameAndExtension() {
187         final Map<String, String> entries = toscaMetadata.getMetaEntries();
188         final String manifestFileName = getFileName(entries.get(ETSI_ENTRY_MANIFEST.getName()));
189         final String manifestExtension = getFileExtension(entries.get(ETSI_ENTRY_MANIFEST.getName()));
190         final String mainDefinitionFileName = getFileName(entries.get(ENTRY_DEFINITIONS.getName()));
191         if (!(TOSCA_MANIFEST_FILE_EXT).equals(manifestExtension)) {
192             reportError(ErrorLevel.ERROR, Messages.MANIFEST_INVALID_EXT.getErrorMessage());
193         }
194         if (!mainDefinitionFileName.equals(manifestFileName)) {
195             reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_INVALID_NAME.getErrorMessage(),
196                     manifestFileName, mainDefinitionFileName));
197         }
198     }
199
200     private String getFileExtension(final String filePath) {
201         return FilenameUtils.getExtension(filePath);
202     }
203
204     private String getFileName(final String filePath) {
205         return FilenameUtils.getBaseName(filePath);
206     }
207
208     private boolean hasETSIMetadata() {
209         final Map<String, String> entries = toscaMetadata.getMetaEntries();
210         return hasEntry(entries, TOSCA_META_FILE_VERSION_ENTRY.getName())
211                 && hasEntry(entries, CSAR_VERSION_ENTRY.getName())
212                 && hasEntry(entries, CREATED_BY_ENTRY.getName());
213     }
214
215     private boolean hasEntry(final Map<String, String> entries, final String mandatoryEntry) {
216         if (!entries.containsKey(mandatoryEntry)) {
217             reportError(ErrorLevel.ERROR,
218                     String.format(Messages.METADATA_MISSING_ENTRY.getErrorMessage(), mandatoryEntry));
219             return false;
220         }
221         return true;
222     }
223
224     private void handleMetadataEntries() {
225         toscaMetadata.getMetaEntries().entrySet().parallelStream().forEach(this::handleEntry);
226     }
227
228     private void handleEntry(final Map.Entry<String, String> entry) {
229         final String key = entry.getKey();
230         final ToscaMetaEntry toscaMetaEntry = ToscaMetaEntry.parse(entry.getKey()).orElse(null);
231         // allows any other unknown entry
232         if (toscaMetaEntry == null) {
233             return;
234         }
235         final String value = entry.getValue();
236
237         switch (toscaMetaEntry) {
238             case TOSCA_META_FILE_VERSION_ENTRY:
239             case CSAR_VERSION_ENTRY:
240             case CREATED_BY_ENTRY:
241                 verifyMetadataEntryVersions(key, value);
242                 break;
243             case ENTRY_DEFINITIONS:
244                 validateDefinitionFile(value);
245                 break;
246             case ETSI_ENTRY_MANIFEST:
247                 validateManifestFile(value);
248                 break;
249             case ETSI_ENTRY_CHANGE_LOG:
250                 validateChangeLog(value);
251                 break;
252             case ETSI_ENTRY_TESTS:
253             case ETSI_ENTRY_LICENSES:
254                 validateOtherEntries(entry);
255                 break;
256             case ETSI_ENTRY_CERTIFICATE:
257                 validateCertificate(value);
258                 break;
259             default:
260                 reportError(ErrorLevel.ERROR, Messages.METADATA_UNSUPPORTED_ENTRY.formatMessage(key));
261                 LOGGER.warn(Messages.METADATA_UNSUPPORTED_ENTRY.getErrorMessage(), key);
262                 break;
263         }
264     }
265
266     private void validateOtherEntries(final Map.Entry entry) {
267         final String manifestFile = toscaMetadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName());
268         if (verifyFileExists(contentHandler.getFileList(), manifestFile)) {
269             final Manifest onboardingManifest = new SOL004ManifestOnboarding();
270             onboardingManifest.parse(contentHandler.getFileContentAsStream(manifestFile));
271             final Optional<ResourceTypeEnum> resourceType = onboardingManifest.getType();
272             if (resourceType.isPresent() && resourceType.get() == ResourceTypeEnum.VF) {
273                 final String value = (String) entry.getValue();
274                 validateOtherEntries(value);
275             } else {
276                 final String key = (String) entry.getKey();
277                 reportError(ErrorLevel.ERROR,
278                         String.format(Messages.MANIFEST_INVALID_PNF_METADATA.getErrorMessage(), key));
279             }
280
281         }
282     }
283
284     private void verifyMetadataEntryVersions(final String key, final String version) {
285         if (!(isValidTOSCAVersion(key, version) || isValidCSARVersion(key, version)
286                 || CREATED_BY_ENTRY.getName().equals(key))) {
287             errorsByFile.add(new ErrorMessage(ErrorLevel.ERROR,
288                     String.format(Messages.METADATA_INVALID_VERSION.getErrorMessage(), key, version)));
289             LOGGER.error("{}: key {} - value {} ", Messages.METADATA_INVALID_VERSION.getErrorMessage(), key, version);
290         }
291     }
292
293     private boolean isValidTOSCAVersion(final String key, final String version) {
294         return TOSCA_META_FILE_VERSION_ENTRY.getName().equals(key) && TOSCA_META_FILE_VERSION_1_0.equals(version);
295     }
296
297     private boolean isValidCSARVersion(final String value, final String version) {
298         return CSAR_VERSION_ENTRY.getName().equals(value) && (CSAR_VERSION_1_1.equals(version)
299                 || CSAR_VERSION_1_0.equals(version));
300     }
301
302     private void validateDefinitionFile(final String filePath) {
303         final Set<String> existingFiles = contentHandler.getFileList();
304
305         if (verifyFileExists(existingFiles, filePath)) {
306             final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
307                     new ToscaDefinitionImportHandler(contentHandler.getFiles(), filePath);
308             final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
309             if (CollectionUtils.isNotEmpty(validationErrorList)) {
310                 errorsByFile.addAll(validationErrorList);
311             }
312         } else {
313             reportError(ErrorLevel.ERROR, String.format(Messages.MISSING_DEFINITION_FILE.getErrorMessage(), filePath));
314         }
315     }
316
317     private boolean verifyFileExists(final Set<String> existingFiles, final String filePath) {
318         return existingFiles.contains(filePath);
319     }
320
321     private void validateManifestFile(final String filePath) {
322         final Set<String> existingFiles = contentHandler.getFileList();
323         if (verifyFileExists(existingFiles, filePath)) {
324             final Manifest onboardingManifest = new SOL004ManifestOnboarding();
325             onboardingManifest.parse(contentHandler.getFileContentAsStream(filePath));
326             if (onboardingManifest.isValid()) {
327                 try {
328                     verifyManifestMetadata(onboardingManifest.getMetadata());
329                 } catch (final InvalidManifestMetadataException e) {
330                     reportError(ErrorLevel.ERROR, e.getMessage());
331                     LOGGER.error(e.getMessage(), e);
332                 }
333                 verifyManifestSources(onboardingManifest);
334             } else {
335                 final List<String> manifestErrors = onboardingManifest.getErrors();
336                 manifestErrors.forEach(error -> reportError(ErrorLevel.ERROR, error));
337             }
338         } else {
339             reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_NOT_FOUND.getErrorMessage(), filePath));
340         }
341     }
342
343     private void verifyManifestMetadata(final Map<String, String> metadata) {
344         if (metadata.size() != MANIFEST_METADATA_LIMIT) {
345             reportError(ErrorLevel.ERROR,
346                     String.format(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT.getErrorMessage(),
347                             MANIFEST_METADATA_LIMIT));
348         }
349         if (isPnfMetadata(metadata)) {
350             handleMetadataEntries(metadata, MANIFEST_PNF_METADATA);
351         } else {
352             handleMetadataEntries(metadata, MANIFEST_VNF_METADATA);
353         }
354     }
355
356     private boolean isPnfMetadata(final Map<String, String> metadata) {
357         final String firstMetadataDefinition = metadata.keySet().iterator().next();
358         final String expectedMetadataType =
359                 firstMetadataDefinition.contains(TOSCA_TYPE_PNF) ? TOSCA_TYPE_PNF : TOSCA_TYPE_VNF;
360         if (metadata.keySet().stream()
361                 .anyMatch((final String metadataEntry) -> !metadataEntry.contains(expectedMetadataType))) {
362             throw new InvalidManifestMetadataException(Messages.MANIFEST_METADATA_INVALID_ENTRY.getErrorMessage());
363         }
364
365         return TOSCA_TYPE_PNF.equals(expectedMetadataType);
366     }
367
368     private void handleMetadataEntries(final Map<String, String> metadata, final Set<String> manifestMetadata) {
369         manifestMetadata.stream()
370                 .filter(requiredEntry -> !metadata.containsKey(requiredEntry))
371                 .forEach(requiredEntry ->
372                         reportError(ErrorLevel.ERROR,
373                                 String.format(Messages.MANIFEST_METADATA_MISSING_ENTRY.getErrorMessage(), requiredEntry)));
374     }
375
376     /**
377      * Checks if all manifest sources exists within the package and if all package files are being referred.
378      *
379      * @param onboardingManifest The manifest
380      */
381     private void verifyManifestSources(final Manifest onboardingManifest) {
382         final Set<String> packageFiles = contentHandler.getFileList();
383         final List<String> sources = internalFilesFilter.filter(onboardingManifest.getSources());
384         verifyFilesExist(packageFiles, sources, MANIFEST_SOURCE);
385
386         final Map<String, List<String>> nonManoArtifacts = onboardingManifest.getNonManoSources();
387
388         final List<String> nonManoValidFilePaths = new ArrayList<>();
389         nonManoArtifacts.forEach((nonManoType, files) -> {
390             final List<String> internalNonManoFileList = internalFilesFilter.filter(files);
391             nonManoValidFilePaths.addAll(internalNonManoFileList);
392             final NonManoArtifactType nonManoArtifactType = NonManoArtifactType.parse(nonManoType).orElse(null);
393             if (nonManoArtifactType == ONAP_PM_DICTIONARY || nonManoArtifactType == ONAP_VES_EVENTS) {
394                 internalNonManoFileList.forEach(this::validateYaml);
395             } else if (nonManoArtifactType == ONAP_SW_INFORMATION) {
396                 validateSoftwareInformationNonManoArtifact(files);
397             }
398         });
399
400         verifyFilesExist(packageFiles, nonManoValidFilePaths, MANIFEST_NON_MANO_SOURCE);
401
402         final Set<String> allReferredFiles = new HashSet<>();
403         allReferredFiles.addAll(sources);
404         allReferredFiles.addAll(nonManoValidFilePaths);
405         verifyFilesBeingReferred(allReferredFiles, packageFiles);
406     }
407
408     private void validateSoftwareInformationNonManoArtifact(final List<String> files) {
409         if (CollectionUtils.isEmpty(files)) {
410             reportError(ErrorLevel.ERROR, Messages.EMPTY_SW_INFORMATION_NON_MANO_ERROR.getErrorMessage());
411             return;
412         }
413         if (files.size() != 1) {
414             final String formattedFileList = files.stream()
415                     .map(filePath -> String.format("'%s'", filePath))
416                     .collect(Collectors.joining(", "));
417             reportError(ErrorLevel.ERROR,
418                     Messages.UNIQUE_SW_INFORMATION_NON_MANO_ERROR.formatMessage(formattedFileList));
419             return;
420         }
421         final String swInformationFilePath = files.get(0);
422         final byte[] swInformationYaml = contentHandler.getFileContent(swInformationFilePath);
423         final Optional<PnfSoftwareInformation> parsedYaml = SoftwareInformationArtifactYamlParser
424                 .parse(swInformationYaml);
425         if (!parsedYaml.isPresent()) {
426             reportError(ErrorLevel.ERROR,
427                     Messages.INVALID_SW_INFORMATION_NON_MANO_ERROR.formatMessage(swInformationFilePath));
428         } else {
429             final PnfSoftwareInformation pnfSoftwareInformation = parsedYaml.get();
430             if (!pnfSoftwareInformation.isValid()) {
431                 reportError(ErrorLevel.ERROR,
432                         Messages.INCORRECT_SW_INFORMATION_NON_MANO_ERROR.formatMessage(swInformationFilePath));
433             }
434         }
435     }
436
437     /**
438      * Validates if a YAML file has the correct extension, is not empty and the content is a valid YAML. Reports each
439      * error found.
440      *
441      * @param filePath the file path inside the package
442      */
443     private void validateYaml(final String filePath) {
444         if (!contentHandler.containsFile(filePath)) {
445             return;
446         }
447         final String fileExtension = getFileExtension(filePath);
448         if (!"yaml".equalsIgnoreCase(fileExtension) && !"yml".equalsIgnoreCase(fileExtension)) {
449             reportError(ErrorLevel.ERROR, Messages.INVALID_YAML_EXTENSION.formatMessage(filePath));
450             return;
451         }
452
453         try (final InputStream fileContent = contentHandler.getFileContentAsStream(filePath)) {
454             if (fileContent == null) {
455                 reportError(ErrorLevel.ERROR, Messages.EMPTY_YAML_FILE_1.formatMessage(filePath));
456                 return;
457             }
458             new Yaml().loadAll(fileContent).iterator().next();
459         } catch (final IOException e) {
460             final String errorMsg = Messages.FILE_LOAD_CONTENT_ERROR.formatMessage(filePath);
461             reportError(ErrorLevel.ERROR, errorMsg);
462             LOGGER.debug(errorMsg, e);
463         } catch (final Exception e) {
464             final String message = Messages.INVALID_YAML_FORMAT_1.formatMessage(filePath, e.getMessage());
465             LOGGER.debug(message, e);
466             reportError(ErrorLevel.ERROR, message);
467         }
468     }
469
470     /**
471      * Checks if all package files are referred in manifest. Reports missing references.
472      *
473      * @param referredFileSet the list of referred files path
474      * @param packageFileSet  the list of package file path
475      */
476     private void verifyFilesBeingReferred(final Set<String> referredFileSet, final Set<String> packageFileSet) {
477         packageFileSet.forEach(filePath -> {
478             if (!isManifestFile(filePath) && !referredFileSet.contains(filePath)) {
479                 reportError(ErrorLevel.ERROR,
480                         String.format(Messages.MISSING_MANIFEST_REFERENCE.getErrorMessage(), filePath));
481             }
482         });
483     }
484
485     private boolean isManifestFile(final String filePath) {
486         return filePath.equals(toscaMetadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName()));
487     }
488
489     private void validateOtherEntries(final String folderPath) {
490         if (!verifyFoldersExist(folderList, folderPath)) {
491             reportError(ErrorLevel.ERROR, String.format(Messages.METADATA_MISSING_OPTIONAL_FOLDERS.getErrorMessage(),
492                     folderPath));
493         }
494     }
495
496     private void validateCertificate(final String file) {
497         final Set<String> packageFiles = contentHandler.getFileList();
498         if (!verifyFileExist(packageFiles, file)) {
499             reportError(ErrorLevel.ERROR,
500                     String.format(Messages.MISSING_METADATA_FILES.getErrorMessage(), file, file));
501         }
502     }
503
504     private boolean verifyFoldersExist(final Set<String> folderList, final String folderPath) {
505         return folderList.contains(folderPath + "/");
506     }
507
508     private void verifyFilesExist(final Set<String> existingFiles, final List<String> sources, final String type) {
509         sources.forEach(file -> {
510             if (!existingFiles.contains(file)) {
511                 reportError(ErrorLevel.ERROR,
512                         String.format(Messages.MISSING_MANIFEST_SOURCE.getErrorMessage(), type, file));
513             }
514         });
515     }
516
517     private boolean verifyFileExist(final Set<String> existingFiles, final String file) {
518         return existingFiles.contains(file);
519     }
520
521     private void validateChangeLog(final String filePath) {
522         if (!verifyFileExists(contentHandler.getFileList(), filePath)) {
523             reportError(ErrorLevel.ERROR, String.format(Messages.MISSING_METADATA_FILES.getErrorMessage(), filePath));
524         }
525     }
526
527     private void reportError(final ErrorLevel errorLevel, final String errorMessage) {
528         errorsByFile.add(new ErrorMessage(errorLevel, errorMessage));
529     }
530
531     private Map<String, List<ErrorMessage>> getAnyValidationErrors() {
532         if (errorsByFile.isEmpty()) {
533             return Collections.emptyMap();
534         }
535         final Map<String, List<ErrorMessage>> errors = new HashMap<>();
536         errors.put(SdcCommon.UPLOAD_FILE, errorsByFile);
537         return errors;
538     }
539
540     private void validatePMDictionaryContentsAgainstSchema() {
541         final Stream<byte[]> pmDictionaryFiles = new FileExtractor(getEtsiEntryManifestPath(), contentHandler)
542             .findFiles(ONAP_PM_DICTIONARY);
543         new PMDictionaryValidator()
544             .validate(pmDictionaryFiles, (String message) -> reportError(ErrorLevel.ERROR, message));
545     }
546
547     private String getEtsiEntryManifestPath() {
548         return toscaMetadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName());
549     }
550 }