From a80a2e20ab5414c35a948fd58613df33d3bee3d3 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Thu, 10 Oct 2019 17:39:03 +0100 Subject: Validate non-mano software information artifact Change-Id: Ie9fabd6af821b830e211ff1068fb287357fd38ee Issue-ID: SDC-2619 Signed-off-by: andre.schmid --- .../sdc/be/config/NonManoArtifactType.java | 17 +++-- .../sdc/be/csar/pnf/PnfSoftwareInformation.java | 78 +++++++++++++++++++ .../sdc/be/csar/pnf/PnfSoftwareVersion.java | 50 ++++++++++++ .../pnf/SoftwareInformationArtifactYamlParser.java | 89 ++++++++++++++++++++++ 4 files changed, 228 insertions(+), 6 deletions(-) create mode 100644 common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareInformation.java create mode 100644 common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareVersion.java create mode 100644 common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParser.java (limited to 'common-be/src/main') diff --git a/common-be/src/main/java/org/openecomp/sdc/be/config/NonManoArtifactType.java b/common-be/src/main/java/org/openecomp/sdc/be/config/NonManoArtifactType.java index 35917f88a7..62e11bba00 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/config/NonManoArtifactType.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/config/NonManoArtifactType.java @@ -19,9 +19,16 @@ package org.openecomp.sdc.be.config; +import java.util.Arrays; +import java.util.Optional; +import lombok.AllArgsConstructor; +import lombok.Getter; + /** * Stores non mano artifact types. */ +@Getter +@AllArgsConstructor public enum NonManoArtifactType { ONAP_VES_EVENTS("onap_ves_events"), ONAP_PM_DICTIONARY("onap_pm_dictionary"), @@ -33,11 +40,9 @@ public enum NonManoArtifactType { private final String type; - NonManoArtifactType(final String type) { - this.type = type; - } - - public String getType() { - return type; + public static Optional parse(final String type) { + return Arrays.stream(values()) + .filter(nonManoArtifactType -> nonManoArtifactType.getType().equals(type)) + .findFirst(); } } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareInformation.java b/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareInformation.java new file mode 100644 index 0000000000..d714faaa23 --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareInformation.java @@ -0,0 +1,78 @@ +/* + * ============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.csar.pnf; + +import java.util.LinkedHashSet; +import java.util.Set; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.collections.CollectionUtils; + +/** + * Represents the Pnf software information non-mano yaml + */ +@Getter +@Setter +public class PnfSoftwareInformation { + + private String description; + private String provider; + private String version; + @Setter(AccessLevel.NONE) + private Set softwareVersionSet = new LinkedHashSet<>(); + + /** + * Adds a {@link PnfSoftwareVersion} instance to the software version set + * @param softwareVersion the pnf software version to add + */ + public void addToSoftwareVersionSet(final PnfSoftwareVersion softwareVersion) { + softwareVersionSet.add(softwareVersion); + } + + public Set getSoftwareVersionSet() { + return new LinkedHashSet<>(softwareVersionSet); + } + + /** + * Stores the software information yaml field names. + */ + @AllArgsConstructor + @Getter + public enum PnfSoftwareInformationField { + DESCRIPTION("description"), + PROVIDER("provider"), + VERSION("version"), + PNF_SOFTWARE_INFORMATION("pnf_software_information"); + + private final String fieldName; + + } + + public boolean isValid() { + if(CollectionUtils.isEmpty(softwareVersionSet)) { + return false; + } + + return softwareVersionSet.stream().allMatch(PnfSoftwareVersion::isValid); + } + +} diff --git a/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareVersion.java b/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareVersion.java new file mode 100644 index 0000000000..8dbb43ae00 --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/PnfSoftwareVersion.java @@ -0,0 +1,50 @@ +/* + * ============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.csar.pnf; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; + +@AllArgsConstructor +@EqualsAndHashCode +@Getter +public class PnfSoftwareVersion { + + private final String version; + private final String description; + + /** + * Stores the pnf software version yaml fields. + */ + @Getter + @AllArgsConstructor + public enum PnfSoftwareVersionField { + DESCRIPTION("description"), + PNF_SOFTWARE_VERSION("pnf_software_version"); + + private final String fieldName; + } + + public boolean isValid() { + return StringUtils.isNotEmpty(version); + } +} \ No newline at end of file diff --git a/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParser.java b/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParser.java new file mode 100644 index 0000000000..e5696afaed --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParser.java @@ -0,0 +1,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.csar.pnf; + +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.csar.pnf.PnfSoftwareInformation.PnfSoftwareInformationField; +import org.openecomp.sdc.be.csar.pnf.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} if the file was successful parsed, otherwise {@code + * Optional.empty()} + */ + @SuppressWarnings("unchecked") + public static Optional parse(final byte[] softwareInformationYamlFileBytes) { + final Map softwareVersionYamlObject; + try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(softwareInformationYamlFileBytes)) { + final Object yaml = YamlUtil.read(byteArrayInputStream); + if (!(yaml instanceof Map)) { + return Optional.empty(); + } + + softwareVersionYamlObject = (Map) 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> pnfSoftwareInformationYaml = (List>) 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); + } + + +} -- cgit 1.2.3-korg