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 --- common-be/pom.xml | 14 +++ .../sdc/be/config/NonManoArtifactType.java | 17 +-- .../sdc/be/csar/pnf/PnfSoftwareInformation.java | 78 ++++++++++++++ .../sdc/be/csar/pnf/PnfSoftwareVersion.java | 50 +++++++++ .../pnf/SoftwareInformationArtifactYamlParser.java | 89 ++++++++++++++++ .../SoftwareInformationArtifactYamlParserTest.java | 59 +++++++++++ .../sdc/be/test/util/TestResourcesHandler.java | 115 +++++++++++++++++++++ .../pnfSoftwareInformation/pnf-sw-information.yaml | 8 ++ 8 files changed, 424 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 create mode 100644 common-be/src/test/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParserTest.java create mode 100644 common-be/src/test/java/org/openecomp/sdc/be/test/util/TestResourcesHandler.java create mode 100644 common-be/src/test/resources/artifacts/pnfSoftwareInformation/pnf-sw-information.yaml (limited to 'common-be') diff --git a/common-be/pom.xml b/common-be/pom.xml index 564f200bcc..97dcc6a11e 100644 --- a/common-be/pom.xml +++ b/common-be/pom.xml @@ -103,6 +103,20 @@ + + maven-jar-plugin + ${maven-jar-plugin.version} + + + default-jar + package + + jar + test-jar + + + + com.github.sylvainlaurent.maven yaml-json-validator-maven-plugin 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); + } + + +} diff --git a/common-be/src/test/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParserTest.java b/common-be/src/test/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParserTest.java new file mode 100644 index 0000000000..b866fd55db --- /dev/null +++ b/common-be/src/test/java/org/openecomp/sdc/be/csar/pnf/SoftwareInformationArtifactYamlParserTest.java @@ -0,0 +1,59 @@ +/* + * ============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 static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.Optional; +import org.junit.Test; +import org.openecomp.sdc.be.test.util.TestResourcesHandler; + +public class SoftwareInformationArtifactYamlParserTest { + + @Test + public void parse() throws IOException { + //given + final byte[] resourceAsByteArray = TestResourcesHandler + .getResourceAsByteArray("artifacts/pnfSoftwareInformation/pnf-sw-information.yaml"); + //when + final Optional pnfSoftwareInformation = SoftwareInformationArtifactYamlParser + .parse(resourceAsByteArray); + //then + final PnfSoftwareVersion expectedPnfSoftwareVersion1 = new PnfSoftwareVersion("version1", "first software version of PNF"); + final PnfSoftwareVersion expectedPnfSoftwareVersion2 = new PnfSoftwareVersion("version2", "second software version of PNF"); + assertThat("The software information should be parsed", pnfSoftwareInformation.isPresent(), is(true)); + pnfSoftwareInformation.ifPresent(softwareInformation -> { + assertThat("The software information provider should be as expected", + softwareInformation.getProvider(), is(equalTo("Ericsson"))); + assertThat("The software information description should be as expected", + softwareInformation.getDescription(), is(equalTo("pnf software information"))); + assertThat("The software information version should be as expected", + softwareInformation.getVersion(), is(equalTo("1.0"))); + assertThat("The software versions should contain expected versions", + softwareInformation.getSoftwareVersionSet(), + hasItems(expectedPnfSoftwareVersion1, expectedPnfSoftwareVersion2)); + }); + } + +} \ No newline at end of file diff --git a/common-be/src/test/java/org/openecomp/sdc/be/test/util/TestResourcesHandler.java b/common-be/src/test/java/org/openecomp/sdc/be/test/util/TestResourcesHandler.java new file mode 100644 index 0000000000..3aba23692f --- /dev/null +++ b/common-be/src/test/java/org/openecomp/sdc/be/test/util/TestResourcesHandler.java @@ -0,0 +1,115 @@ +/* + * ============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.test.util; + +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Path; +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Util class for handling test resources. + */ +public class TestResourcesHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestResourcesHandler.class); + + private TestResourcesHandler() { + + } + + /** + * Gets the input stream of a resource file + * + * @param resourcePath The resource file path + * @return + * The resource input stream + */ + public static InputStream getResourceAsStream(final String resourcePath) { + return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath); + } + + public static InputStream getResourceAsStream(final Path resourcePath) { + return getResourceAsStream(resourcePath.toString()); + } + + /** + * Reads a file and coverts it to a byte array. + * + * @param resourcePath The resource file path + * @return + * The resource file byte array + * @throws IOException + * When the file was not found or the input stream could not be opened + */ + public static byte[] getResourceAsByteArray(final String resourcePath) throws IOException { + try(final InputStream inputStream = getResourceAsStream(resourcePath)) { + if (inputStream == null) { + throw new IOException(String.format("Could not find the resource on path \"%s\"", resourcePath)); + } + return IOUtils.toByteArray(inputStream); + } catch (final IOException ex) { + throw new IOException(String.format("Could not open the input stream for resource on path \"%s\"", resourcePath), ex); + } + } + + public static byte[] getResourceAsByteArray(final Path resourcePath) throws IOException { + return getResourceAsByteArray(resourcePath.toString()); + } + + /** + * Reads a file in the given path. + * The method forces an assertion fail if the resource could not be loaded. + * @param resourcePath The resource file path + * @return + * The resource file byte array + */ + public static byte[] getResourceBytesOrFail(final String resourcePath) { + try { + return getResourceAsByteArray(resourcePath); + } catch (final IOException e) { + final String errorMsg = String.format("Could not load resource '%s'", resourcePath); + LOGGER.error(errorMsg, e); + fail(errorMsg); + } + + return null; + } + + public static byte[] getResourceBytesOrFail(final Path resourcePath) { + return getResourceBytesOrFail(resourcePath.toString()); + } + + /** + * Gets the input stream of a resource file + * + * @param resourcePath The resource file path + * @return + * The resource input stream + */ + public static URL getFileUrl(final String resourcePath) { + return Thread.currentThread().getContextClassLoader().getResource(resourcePath); + } +} diff --git a/common-be/src/test/resources/artifacts/pnfSoftwareInformation/pnf-sw-information.yaml b/common-be/src/test/resources/artifacts/pnfSoftwareInformation/pnf-sw-information.yaml new file mode 100644 index 0000000000..31f4bc6173 --- /dev/null +++ b/common-be/src/test/resources/artifacts/pnfSoftwareInformation/pnf-sw-information.yaml @@ -0,0 +1,8 @@ +description: "pnf software information" +provider: "Ericsson" +version: "1.0" +pnf_software_information: + - description: "first software version of PNF" + pnf_software_version: "version1" + - description: "second software version of PNF" + pnf_software_version: "version2" -- cgit 1.2.3-korg