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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
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;
30 import java.util.List;
32 import java.util.stream.Collectors;
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;
42 import com.google.common.collect.ImmutableSet;
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.
48 class SOL004Version3MetaDirectoryValidator extends SOL004MetaDirectoryValidator {
50 private static final Logger LOGGER = LoggerFactory.getLogger(SOL004Version3MetaDirectoryValidator.class);
52 public SOL004Version3MetaDirectoryValidator() {
56 SOL004Version3MetaDirectoryValidator(final SecurityManager securityManager) {
57 super(securityManager);
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());
66 validateDefinitionFile(entry.getValue());
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;
76 protected ImmutableSet<String> getManifestMetadata(final Map<String, String> metadata) {
77 return isPnfMetadata(metadata) ? MANIFEST_PNF_METADATA_VERSION_3 : MANIFEST_VNF_METADATA_VERSION_3;
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;
88 .anyMatch(k -> !k.contains(expectedMetadataType))) {
89 throw new InvalidManifestMetadataException(Messages.MANIFEST_METADATA_INVALID_ENTRY.getErrorMessage());
91 return expectedMetadataType.equals(TOSCA_TYPE_PNF);