From c62f765c1ed65829e126807e1e7a91388bb1c322 Mon Sep 17 00:00:00 2001 From: Mateusz Gołuchowski Date: Thu, 16 Jul 2020 11:00:00 +0200 Subject: Adjust pm-mapper to accept new schema. - Replaced 28.550 schema and template with 28.532 standard - Adjusted class for new schema - Adjusted xml files for tests - validate method will now throw exception when given fileFormatType is not supported Issue-ID: DCAEGEN2-2308 Change-Id: I375db2004887f1f634adac31d4d1af3675f5911c Signed-off-by: Mateusz Goluchowski --- .../NotSupportedFormatTypeException.java | 29 ++++++++++++++++++++ .../model/measurement/nr/MeasDataFile.java | 31 +++++++++++----------- .../services/pmmapper/utils/MeasConverter.java | 4 +-- .../services/pmmapper/utils/XMLValidator.java | 20 ++++++++++++-- 4 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/NotSupportedFormatTypeException.java (limited to 'src/main/java') diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/NotSupportedFormatTypeException.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/NotSupportedFormatTypeException.java new file mode 100644 index 0000000..3e3214d --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/NotSupportedFormatTypeException.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nokia. + * ================================================================================ + * 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.onap.dcaegen2.services.pmmapper.exceptions; + +public class NotSupportedFormatTypeException extends Exception { + + public NotSupportedFormatTypeException(String message) { + super(message); + } + +} diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/measurement/nr/MeasDataFile.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/measurement/nr/MeasDataFile.java index 1ef1a8b..0f87465 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/measurement/nr/MeasDataFile.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/measurement/nr/MeasDataFile.java @@ -20,10 +20,11 @@ package org.onap.dcaegen2.services.pmmapper.model.measurement.nr; -import java.util.ArrayList; -import java.util.List; +import lombok.Data; +import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementData; +import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementFile; +import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementInfo; -import java.util.Optional; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -32,10 +33,9 @@ import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSchemaType; import javax.xml.bind.annotation.XmlType; import javax.xml.datatype.XMLGregorianCalendar; -import lombok.Data; -import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementData; -import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementFile; -import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementInfo; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { @@ -43,7 +43,7 @@ import org.onap.dcaegen2.services.pmmapper.model.measurement.common.MeasurementI "measData", "fileFooter" }) -@XmlRootElement(name = "MeasDataFile") +@XmlRootElement(name = "measDataFile") @Data public class MeasDataFile implements MeasurementFile { @@ -68,7 +68,7 @@ public class MeasDataFile implements MeasurementFile { measData.clear(); measurementData.forEach(measurementDatum -> { MeasData measDatum = new MeasData(); - measDatum.setMeasuredEntity((MeasData.MeasuredEntity) measurementDatum.getManagedEntity()); + measDatum.setMeasEntity((MeasData.MeasEntity) measurementDatum.getManagedEntity()); measDatum.setMeasInfo(measurementDatum.getMeasurementInfo()); this.measData.add(measDatum); }); @@ -81,7 +81,7 @@ public class MeasDataFile implements MeasurementFile { @Data public static class FileFooter { - @XmlElement(name = "MeasData", required = true) + @XmlElement(name = "measData", required = true) protected MeasDataFile.FileFooter.MeasData measData; @XmlAccessorType(XmlAccessType.FIELD) @@ -104,7 +104,7 @@ public class MeasDataFile implements MeasurementFile { public static class FileHeader { @XmlElement(required = true) protected MeasDataFile.FileHeader.FileSender fileSender; - @XmlElement(name = "MeasData", required = true) + @XmlElement(name = "measData", required = true) protected MeasDataFile.FileHeader.MeasData measData; @XmlAttribute(name = "fileFormatVersion", required = true) protected String fileFormatVersion; @@ -137,14 +137,13 @@ public class MeasDataFile implements MeasurementFile { @XmlAccessorType(XmlAccessType.FIELD) - @XmlType(name = "", propOrder = { - "measuredEntity", + @XmlType(name = "", propOrder = {"measEntity", "measInfo" }) @Data public static class MeasData implements MeasurementData { @XmlElement(required = true) - protected MeasDataFile.MeasData.MeasuredEntity measuredEntity; + protected MeasEntity measEntity; @XmlElement() protected List measInfo; @@ -160,13 +159,13 @@ public class MeasDataFile implements MeasurementFile { @Override public Object getManagedEntity() { - return this.measuredEntity; + return this.measEntity; } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "") @Data - public static class MeasuredEntity { + public static class MeasEntity { @XmlAttribute(name = "localDn") protected String localDn; @XmlAttribute(name = "userLabel") diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasConverter.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasConverter.java index 70ad8ab..dbe58ab 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasConverter.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasConverter.java @@ -49,8 +49,8 @@ public class MeasConverter { private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(MeasConverter.class)); public static final String LTE_FILE_TYPE = "org.3GPP.32.435#measCollec"; - public static final String NR_FILE_TYPE = "org.3GPP.28.550#measData"; - + public static final String NR_FILE_TYPE = "org.3GPP.28.532#measData"; + public static final String NOT_SUPPORTED_TYPE = "notSupportedType"; /** * Converts 3GPP Measurement xml string to MeasCollecFile. diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/XMLValidator.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/XMLValidator.java index c6fa3fa..0d14e1c 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/XMLValidator.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/XMLValidator.java @@ -23,11 +23,14 @@ package org.onap.dcaegen2.services.pmmapper.utils; import java.nio.file.Files; import java.util.HashMap; import java.util.stream.Stream; + import lombok.NonNull; +import org.onap.dcaegen2.services.pmmapper.exceptions.NotSupportedFormatTypeException; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.logging.ref.slf4j.ONAPLogAdapter; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; + import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; @@ -41,6 +44,7 @@ public class XMLValidator { private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(XMLValidator.class)); private HashMap schemas; private SchemaFactory schemaFactory; + public XMLValidator(Path schemaDirectory) { logger.unwrap().trace("Constructing schema from {}", schemaDirectory); schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); @@ -57,14 +61,15 @@ public class XMLValidator { logger.unwrap().debug("Loading schema from {}", schema.toString()); try { schemas.put(schema.getFileName().toString(), schemaFactory.newSchema(schema.toFile())); - } catch(SAXException exception) { + } catch (SAXException exception) { logger.unwrap().error("Failed to discover a valid schema at {}", schema, exception); throw new IllegalArgumentException("Failed to discover a valid schema from given path", exception); } } + public boolean validate(@NonNull Event event) { try { - Validator validator = schemas.get(event.getMetadata().getFileFormatType()).newValidator(); + Validator validator = getValidatorForAccordingFileFormat(event.getMetadata().getFileFormatType()); validator.validate(new StreamSource(new StringReader(event.getBody()))); logger.unwrap().info("XML validation successful"); logger.unwrap().debug(String.valueOf(event)); @@ -72,6 +77,17 @@ public class XMLValidator { } catch (SAXException | IOException exception) { logger.unwrap().error("XML validation failed {}", event, exception); return false; + } catch (NotSupportedFormatTypeException exception) { + logger.unwrap().error("XML validation failed - given file format type is not supported. {}", event, exception); + return false; + } + } + + private Validator getValidatorForAccordingFileFormat(String fileFormatType) throws NotSupportedFormatTypeException { + Schema schema = schemas.get(fileFormatType); + if (schema == null) { + throw new NotSupportedFormatTypeException(fileFormatType); } + return schema.newValidator(); } } \ No newline at end of file -- cgit 1.2.3-korg