Create onboarding validator for ASD VSPs
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / csar / AsdManifestOnboarding.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  *
19  *
20  */
21 package org.openecomp.sdc.tosca.csar;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.common.errors.Messages;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30
31 import static org.openecomp.sdc.tosca.csar.CSARConstants.ASD_MANIFEST_META_ENTRIES;
32 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS;
33
34 /**
35  * Processes a ASD Manifest.
36  */
37 public class AsdManifestOnboarding extends SOL004ManifestOnboarding {
38
39     @Override
40     protected boolean isMetadataEntry(final String metadataEntry) {
41         final Optional<ManifestTokenType> manifestTokenType = ManifestTokenType.parse(metadataEntry);
42         return manifestTokenType.map(ManifestTokenType::isMetadataAsdEntry).orElse(false);
43     }
44
45     /**
46      * Processes {@link ManifestTokenType#SOURCE} entries in {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS}.
47      *
48      * @return A list of sources paths
49      */
50     protected List<String> readNonManoSourceList() {
51         final List<String> nonManoSourceList = new ArrayList<>();
52         while (getCurrentLine().isPresent()) {
53             final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
54             if (!(manifestTokenType == ManifestTokenType.SOURCE || manifestTokenType == ManifestTokenType.VENDOR_NAME || manifestTokenType == ManifestTokenType.ARTIFACT_TYPE)) {
55                 break;
56             }
57             if (manifestTokenType == ManifestTokenType.SOURCE) {
58                 final String value = readCurrentEntryValue().orElse(null);
59                 if (!StringUtils.isEmpty(value)) {
60                     nonManoSourceList.add(value);
61                 } else {
62                     reportError(Messages.MANIFEST_EMPTY_NON_MANO_SOURCE);
63                     break;
64                 }
65             }
66             readNextNonEmptyLine();
67         }
68         return nonManoSourceList;
69     }
70
71     /**
72      * Validates the manifest metadata content, reporting errors found.
73      *
74      * @return {@code true} if the metadata content is valid, {@code false} otherwise.
75      */
76     protected boolean validateMetadata() {
77         if (metadata.isEmpty()) {
78             reportError(Messages.MANIFEST_NO_METADATA);
79             return false;
80         }
81         String key = metadata.keySet().stream().filter(k -> !COMPATIBLE_SPECIFICATION_VERSIONS.getToken().equals(k)).findFirst().orElse(null);
82         final ManifestTokenType firstManifestEntryTokenType = ManifestTokenType.parse(key).orElse(null);
83         if (firstManifestEntryTokenType == null) {
84             reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, key);
85             return false;
86         }
87         for (final Map.Entry<String, String> manifestEntry : metadata.entrySet()) {
88             final ManifestTokenType manifestEntryTokenType = ManifestTokenType.parse(manifestEntry.getKey()).orElse(null);
89             if (manifestEntryTokenType == null || !manifestEntryTokenType.isMetadataAsdEntry()) {
90                 reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, manifestEntry.getKey());
91                 return false;
92             }
93         }
94         if (metadata.entrySet().size() != getMaxAllowedManifestMetaEntries()) {
95             reportError(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT, getMaxAllowedManifestMetaEntries());
96             return false;
97         }
98         return true;
99     }
100
101     protected int getMaxAllowedManifestMetaEntries() {
102         return ASD_MANIFEST_META_ENTRIES;
103     }
104 }