Validate non-mano software information artifact
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / csar / pnf / SoftwareInformationArtifactYamlParser.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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 package org.openecomp.sdc.be.csar.pnf;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.onap.sdc.tosca.services.YamlUtil;
29 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareInformation.PnfSoftwareInformationField;
30 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareVersion.PnfSoftwareVersionField;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.yaml.snakeyaml.error.YAMLException;
34
35 /**
36  * Handles the parsing of the non-mano software information file.
37  */
38 public class SoftwareInformationArtifactYamlParser {
39     private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationArtifactYamlParser.class);
40
41     private SoftwareInformationArtifactYamlParser() {
42
43     }
44
45     /**
46      * Parses the non-mano software information yaml file.
47      *
48      * @param softwareInformationYamlFileBytes the file byte array
49      * @return an {@code Optional<PnfSoftwareInformation>} if the file was successful parsed, otherwise {@code
50      * Optional.empty()}
51      */
52     @SuppressWarnings("unchecked")
53     public static Optional<PnfSoftwareInformation> parse(final byte[] softwareInformationYamlFileBytes) {
54         final Map<String, Object> softwareVersionYamlObject;
55         try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(softwareInformationYamlFileBytes)) {
56             final Object yaml = YamlUtil.read(byteArrayInputStream);
57             if (!(yaml instanceof Map)) {
58                 return Optional.empty();
59             }
60
61             softwareVersionYamlObject = (Map<String, Object>) yaml; // unchecked warning suppressed
62         } catch (final IOException | YAMLException e) {
63             LOGGER.warn("Could not parse the software information yaml file", e);
64             return Optional.empty();
65         }
66
67         final PnfSoftwareInformation pnfSoftwareInformation = new PnfSoftwareInformation();
68         pnfSoftwareInformation.setDescription(
69             (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.DESCRIPTION.getFieldName()));
70         pnfSoftwareInformation.setProvider(
71             (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.PROVIDER.getFieldName()));
72         pnfSoftwareInformation.setVersion(
73             (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.VERSION.getFieldName()));
74         final List<Map<String, String>> pnfSoftwareInformationYaml = (List<Map<String, String>>) softwareVersionYamlObject
75             .get(PnfSoftwareInformationField.PNF_SOFTWARE_INFORMATION.getFieldName()); // unchecked warning suppressed
76
77         if (CollectionUtils.isNotEmpty(pnfSoftwareInformationYaml)) {
78             pnfSoftwareInformationYaml.forEach(stringStringMap -> {
79                 final String description = stringStringMap.get(PnfSoftwareVersionField.DESCRIPTION.getFieldName());
80                 final String version = stringStringMap.get(PnfSoftwareVersionField.PNF_SOFTWARE_VERSION.getFieldName());
81                 pnfSoftwareInformation.addToSoftwareVersionSet(new PnfSoftwareVersion(version, description));
82             });
83         }
84
85         return Optional.of(pnfSoftwareInformation);
86     }
87
88
89 }