summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/SoftwareInformationArtifactYamlParser.java
blob: ec34e2194575ad326bbc54c98d1dd4fb9509c04d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.csar;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.collections.CollectionUtils;
import org.onap.sdc.tosca.services.YamlUtil;
import org.openecomp.sdc.be.components.csar.PnfSoftwareInformation.PnfSoftwareInformationField;
import org.openecomp.sdc.be.components.csar.PnfSoftwareVersion.PnfSoftwareVersionField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.error.YAMLException;

/**
 * Handles the parsing of the non-mano software information file.
 */
public class SoftwareInformationArtifactYamlParser {
    private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationArtifactYamlParser.class);

    private SoftwareInformationArtifactYamlParser() {

    }

    /**
     * Parses the non-mano software information yaml file.
     *
     * @param softwareInformationYamlFileBytes the file byte array
     * @return an {@code Optional<PnfSoftwareInformation>} if the file was successful parsed, otherwise {@code
     * Optional.empty()}
     */
    @SuppressWarnings("unchecked")
    public static Optional<PnfSoftwareInformation> parse(final byte[] softwareInformationYamlFileBytes) {
        final Map<String, Object> softwareVersionYamlObject;
        try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(softwareInformationYamlFileBytes)) {
            final Object yaml = YamlUtil.read(byteArrayInputStream);
            if (!(yaml instanceof Map)) {
                return Optional.empty();
            }

            softwareVersionYamlObject = (Map<String, Object>) yaml; // unchecked warning suppressed
        } catch (final IOException | YAMLException e) {
            LOGGER.warn("Could not parse the software information yaml file", e);
            return Optional.empty();
        }

        final PnfSoftwareInformation pnfSoftwareInformation = new PnfSoftwareInformation();
        pnfSoftwareInformation.setDescription(
            (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.DESCRIPTION.getFieldName()));
        pnfSoftwareInformation.setProvider(
            (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.PROVIDER.getFieldName()));
        pnfSoftwareInformation.setVersion(
            (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.VERSION.getFieldName()));
        final List<Map<String, String>> pnfSoftwareInformationYaml = (List<Map<String, String>>) softwareVersionYamlObject
            .get(PnfSoftwareInformationField.PNF_SOFTWARE_INFORMATION.getFieldName()); // unchecked warning suppressed

        if (CollectionUtils.isNotEmpty(pnfSoftwareInformationYaml)) {
            pnfSoftwareInformationYaml.forEach(stringStringMap -> {
                final String description = stringStringMap.get(PnfSoftwareVersionField.DESCRIPTION.getFieldName());
                final String version = stringStringMap.get(PnfSoftwareVersionField.PNF_SOFTWARE_VERSION.getFieldName());
                pnfSoftwareInformation.addToSoftwareVersionSet(new PnfSoftwareVersion(version, description));
            });
        }

        return Optional.of(pnfSoftwareInformation);
    }


}