473d68e49a70db60f727166d7b2809571dd4eba8
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / SOL004Version3MetaDirectoryValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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  */
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
21
22 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA_LIMIT_VERSION_3;
23 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA_VERSION_3;
24 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_VNF_METADATA_LIMIT_VERSION_3;
25 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_VNF_METADATA_VERSION_3;
26 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_TYPE_PNF;
27 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_TYPE_VNF;
28 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS;
29
30 import java.util.List;
31 import java.util.Map;
32 import java.util.stream.Collectors;
33
34 import org.openecomp.sdc.common.errors.Messages;
35 import org.openecomp.sdc.datatypes.error.ErrorLevel;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
38 import org.openecomp.sdc.tosca.csar.ToscaMetaEntry;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.exceptions.InvalidManifestMetadataException;
40 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
41
42 import com.google.common.collect.ImmutableSet;
43
44 /**
45  * Validates the contents of the package to ensure it complies with the "CSAR with TOSCA-Metadata directory" structure
46  * as defined in ETSI GS NFV-SOL 004 v3.3.1.
47  */
48 class SOL004Version3MetaDirectoryValidator extends SOL004MetaDirectoryValidator {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(SOL004Version3MetaDirectoryValidator.class);
51
52     public SOL004Version3MetaDirectoryValidator() {
53         super();
54     }
55
56     SOL004Version3MetaDirectoryValidator(final SecurityManager securityManager) {
57         super(securityManager);
58     }
59
60     @Override
61     protected void handleOtherEntry(final Map.Entry<String, String> entry) {
62         if (!ToscaMetaEntry.OTHER_DEFINITIONS.getName().equals(entry.getKey())) {
63             reportError(ErrorLevel.ERROR, Messages.METADATA_UNSUPPORTED_ENTRY.formatMessage(entry.getKey()));
64             LOGGER.warn(Messages.METADATA_UNSUPPORTED_ENTRY.getErrorMessage(), entry.getKey());
65         } else
66             validateDefinitionFile(entry.getValue());
67     }
68
69     @Override
70     protected boolean validMetaLimit(Map<String, String> metadata) {
71         int maxAllowedEntries = isPnfMetadata(metadata) ? MANIFEST_PNF_METADATA_LIMIT_VERSION_3 : MANIFEST_VNF_METADATA_LIMIT_VERSION_3;
72         return metadata.size() == maxAllowedEntries;
73     }
74
75     @Override
76     protected ImmutableSet<String> getManifestMetadata(final Map<String, String> metadata) {
77         return isPnfMetadata(metadata) ? MANIFEST_PNF_METADATA_VERSION_3 : MANIFEST_VNF_METADATA_VERSION_3;
78     }
79
80     @Override
81     protected boolean isPnfMetadata(final Map<String, String> metadata) {
82         List<String> keys = metadata.keySet().stream().collect(Collectors.toList());
83         //Both VNF and PNF share this attribute
84         keys.remove(COMPATIBLE_SPECIFICATION_VERSIONS.getToken());
85         final String expectedMetadataType =
86                 keys.get(0).contains(TOSCA_TYPE_PNF) ? TOSCA_TYPE_PNF : TOSCA_TYPE_VNF;
87         if (keys.stream()
88             .anyMatch(k -> !k.contains(expectedMetadataType))) {
89             throw new InvalidManifestMetadataException(Messages.MANIFEST_METADATA_INVALID_ENTRY.getErrorMessage());
90         }
91         return expectedMetadataType.equals(TOSCA_TYPE_PNF);
92     }
93 }