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 / 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
27 import com.google.common.collect.ImmutableSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.logging.api.Logger;
34 import org.openecomp.sdc.logging.api.LoggerFactory;
35 import org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261;
36 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
37
38 /**
39  * 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
40  * 004 v3.3.1.
41  */
42 class SOL004Version3MetaDirectoryValidator extends SOL004MetaDirectoryValidator {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(SOL004Version3MetaDirectoryValidator.class);
45
46     public SOL004Version3MetaDirectoryValidator() {
47         super();
48     }
49
50     SOL004Version3MetaDirectoryValidator(final SecurityManager securityManager) {
51         super(securityManager);
52     }
53
54     @Override
55     protected void handleOtherEntry(final Map.Entry<String, String> entry) {
56         if (!ToscaMetaEntryVersion261.OTHER_DEFINITIONS.getName().equals(entry.getKey())) {
57             reportError(ErrorLevel.ERROR, Messages.METADATA_UNSUPPORTED_ENTRY.formatMessage(entry.getKey()));
58             LOGGER.warn(Messages.METADATA_UNSUPPORTED_ENTRY.getErrorMessage(), entry.getKey());
59         } else {
60             validateDefinitionFile(entry.getValue());
61         }
62     }
63
64     @Override
65     protected boolean validMetaLimit(Map<String, String> metadata) {
66         int maxAllowedEntries = isPnfMetadata(metadata) ? MANIFEST_PNF_METADATA_LIMIT_VERSION_3 : MANIFEST_VNF_METADATA_LIMIT_VERSION_3;
67         return metadata.size() == maxAllowedEntries;
68     }
69
70     @Override
71     protected ImmutableSet<String> getManifestMetadata(final Map<String, String> metadata) {
72         return isPnfMetadata(metadata) ? MANIFEST_PNF_METADATA_VERSION_3 : MANIFEST_VNF_METADATA_VERSION_3;
73     }
74
75     @Override
76     protected boolean isPnfMetadata(final Map<String, String> metadata) {
77         List<String> keys = metadata.keySet().stream().collect(Collectors.toList());
78         //Both VNF and PNF share this attribute
79         return validatorUtils.isPnfMetadata(keys);
80     }
81 }