Add a CSAR validator for model ETSI SOL001 2.5.1
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / EtsiSol004Version251Validator.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2021 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
22
23 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_MANIFEST_FILE_EXT;
24 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_CERTIFICATE;
25 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_DEFINITIONS;
26 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST;
27
28 import java.util.Map;
29 import java.util.Optional;
30 import lombok.NoArgsConstructor;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251;
34
35 /**
36  * Validates the contents of the package to ensure it complies with the "CSAR with TOSCA-Metadata directory" structure as defined in ETSI GS NFV-SOL
37  * 004 v2.5.1.
38  */
39 @NoArgsConstructor
40 public class EtsiSol004Version251Validator extends SOL004MetaDirectoryValidator {
41
42     @Override
43     public boolean appliesTo(final String model) {
44         return "ETSI SOL001 v2.5.1".equals(model);
45     }
46
47     @Override
48     public int getOrder() {
49         return 0;
50     }
51
52     @Override
53     protected Optional<String> getCertificatePath() {
54         return getToscaMetadata().getEntry(ENTRY_CERTIFICATE);
55     }
56
57     @Override
58     protected void handleEntry(final Map.Entry<String, String> entry) {
59         final String key = entry.getKey();
60         final var toscaMetaEntry = ToscaMetaEntryVersion251.parse(entry.getKey()).orElse(null);
61         // allows any other unknown entry
62         if (toscaMetaEntry == null) {
63             return;
64         }
65         final String value = entry.getValue();
66         switch (toscaMetaEntry) {
67             case TOSCA_META_FILE_VERSION_ENTRY:
68             case CSAR_VERSION_ENTRY:
69             case CREATED_BY_ENTRY:
70                 verifyMetadataEntryVersions(key, value);
71                 break;
72             case ENTRY_DEFINITIONS:
73                 validateDefinitionFile(value);
74                 break;
75             case ENTRY_MANIFEST:
76                 validateManifestFile(value);
77                 break;
78             case ENTRY_CHANGE_LOG:
79                 validateChangeLog(value);
80                 break;
81             case ENTRY_TESTS:
82             case ENTRY_LICENSES:
83                 validateOtherEntries(entry);
84                 break;
85             case ENTRY_CERTIFICATE:
86                 validateCertificate(value);
87                 break;
88             default:
89                 handleOtherEntry(entry);
90                 break;
91         }
92     }
93
94     @Override
95     protected String getManifestFilePath() {
96         return getToscaMetadata().getMetaEntries().get(ENTRY_MANIFEST.getName());
97     }
98
99     @Override
100     protected void verifyManifestNameAndExtension() {
101         final Map<String, String> entries = getToscaMetadata().getMetaEntries();
102         final String manifestFileName = getFileName(entries.get(ENTRY_MANIFEST.getName()));
103         final String manifestExtension = getFileExtension(entries.get(ENTRY_MANIFEST.getName()));
104         final String mainDefinitionFileName = getFileName(entries.get(ENTRY_DEFINITIONS.getName()));
105         if (!(TOSCA_MANIFEST_FILE_EXT).equals(manifestExtension)) {
106             reportError(ErrorLevel.ERROR, Messages.MANIFEST_INVALID_EXT.getErrorMessage());
107         }
108         if (!mainDefinitionFileName.equals(manifestFileName)) {
109             reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_INVALID_NAME.getErrorMessage(), manifestFileName, mainDefinitionFileName));
110         }
111     }
112
113 }