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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.csar.pnf;
22 import org.apache.commons.collections.CollectionUtils;
23 import org.onap.sdc.tosca.services.YamlUtil;
24 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareInformation.PnfSoftwareInformationField;
25 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareVersion.PnfSoftwareVersionField;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.yaml.snakeyaml.error.YAMLException;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.util.List;
34 import java.util.Optional;
37 * Handles the parsing of the non-mano software information file.
39 public class SoftwareInformationArtifactYamlParser {
40 private static final Logger LOGGER = LoggerFactory.getLogger(SoftwareInformationArtifactYamlParser.class);
42 private SoftwareInformationArtifactYamlParser() {
47 * Parses the non-mano software information yaml file.
49 * @param softwareInformationYamlFileBytes the file byte array
50 * @return an {@code Optional<PnfSoftwareInformation>} if the file was successful parsed, otherwise {@code
53 @SuppressWarnings("unchecked")
54 public static Optional<PnfSoftwareInformation> parse(final byte[] softwareInformationYamlFileBytes) {
55 final Map<String, Object> softwareVersionYamlObject;
56 try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(softwareInformationYamlFileBytes)) {
57 final Object yaml = YamlUtil.read(byteArrayInputStream);
58 if (!(yaml instanceof Map)) {
59 return Optional.empty();
62 softwareVersionYamlObject = (Map<String, Object>) yaml; // unchecked warning suppressed
63 } catch (final IOException | YAMLException e) {
64 LOGGER.warn("Could not parse the software information yaml file", e);
65 return Optional.empty();
68 final PnfSoftwareInformation pnfSoftwareInformation = new PnfSoftwareInformation();
69 pnfSoftwareInformation.setDescription(
70 (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.DESCRIPTION.getFieldName()));
71 pnfSoftwareInformation.setProvider(
72 (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.PROVIDER.getFieldName()));
73 pnfSoftwareInformation.setVersion(
74 (String) softwareVersionYamlObject.get(PnfSoftwareInformationField.VERSION.getFieldName()));
75 final List<Map<String, String>> pnfSoftwareInformationYaml = (List<Map<String, String>>) softwareVersionYamlObject
76 .get(PnfSoftwareInformationField.PNF_SOFTWARE_INFORMATION.getFieldName()); // unchecked warning suppressed
78 if (CollectionUtils.isNotEmpty(pnfSoftwareInformationYaml)) {
79 pnfSoftwareInformationYaml.forEach(stringStringMap -> {
80 final String description = stringStringMap.get(PnfSoftwareVersionField.DESCRIPTION.getFieldName());
81 final String version = stringStringMap.get(PnfSoftwareVersionField.PNF_SOFTWARE_VERSION.getFieldName());
82 pnfSoftwareInformation.addToSoftwareVersionSet(new PnfSoftwareVersion(version, description));
86 return Optional.of(pnfSoftwareInformation);