diff options
author | 2022-12-23 12:09:19 +0000 | |
---|---|---|
committer | 2022-12-23 13:43:29 +0000 | |
commit | 216d299732eb360fecaff7b382376eaa43a07b39 (patch) | |
tree | 4afda003775db4f624c0c9fa876263a9a14a7970 /cps-service/src/main/java/org/onap/cps/utils/YangUtils.java | |
parent | 294bcb5cb9f06fc83c37648688831e353ed43b22 (diff) |
Fix SonarQube warnings
- fixed most new warnings (required some rerfactoring and renaming)
- had to change method order (becasue private/public)
which loosk liek a big change because gerrit cannot match thee old/new correctly :-(
- some reformatting to improve readability
- used autoclosable fro streams
- use of deprecated ODL methods will be addressed by separate commit (Lee-Anjella)
Issue-ID: CPS-475
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: I4b2f74492ac9e2c644659edab1e7ff05dddcbd20
Diffstat (limited to 'cps-service/src/main/java/org/onap/cps/utils/YangUtils.java')
-rw-r--r-- | cps-service/src/main/java/org/onap/cps/utils/YangUtils.java | 157 |
1 files changed, 88 insertions, 69 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java index eb0c764cb..c0dfe5205 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java +++ b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java @@ -38,9 +38,11 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; +import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.TransformerException; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -75,6 +77,9 @@ import org.xml.sax.SAXException; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class YangUtils { + public static final String DATA_ROOT_NODE_NAMESPACE = "urn:ietf:params:xml:ns:netconf:base:1.0"; + public static final String DATA_ROOT_NODE_TAG_NAME = "data"; + /** * Parses data into Collection of NormalizedNode according to given schema context. * @@ -82,13 +87,13 @@ public class YangUtils { * @param schemaContext schema context describing associated data model * @return the NormalizedNode object */ - public static ContainerNode parseData(final ContentType contentType, final String nodeData, - final SchemaContext schemaContext) { + public static ContainerNode parseData(final ContentType contentType, + final String nodeData, + final SchemaContext schemaContext) { if (contentType == ContentType.JSON) { - return parseJsonData(nodeData, schemaContext, Optional.empty()); + return parseJsonDataWithOptionalParent(nodeData, schemaContext, Optional.empty()); } - return parseXmlData(XmlFileUtils.prepareXmlContent(nodeData, schemaContext), schemaContext, - Optional.empty()); + return parseXmlDataWithOptionalParent(nodeData, schemaContext, Optional.empty()); } /** @@ -98,19 +103,14 @@ public class YangUtils { * @param schemaContext schema context describing associated data model * @return the NormalizedNode object */ - public static ContainerNode parseData(final ContentType contentType, final String nodeData, - final SchemaContext schemaContext, final String parentNodeXpath) { - final DataSchemaNode parentSchemaNode = - (DataSchemaNode) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext) - .get("dataSchemaNode"); - final Collection<QName> dataSchemaNodeIdentifiers = - (Collection<QName>) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext) - .get("dataSchemaNodeIdentifiers"); + public static ContainerNode parseData(final ContentType contentType, + final String nodeData, + final SchemaContext schemaContext, + final String parentNodeXpath) { if (contentType == ContentType.JSON) { - return parseJsonData(nodeData, schemaContext, Optional.of(dataSchemaNodeIdentifiers)); + return parseJsonDataWithOptionalParent(nodeData, schemaContext, Optional.of(parentNodeXpath)); } - return parseXmlData(XmlFileUtils.prepareXmlContent(nodeData, parentSchemaNode, parentNodeXpath), schemaContext, - Optional.of(dataSchemaNodeIdentifiers)); + return parseXmlDataWithOptionalParent(nodeData, schemaContext, Optional.of(parentNodeXpath)); } /** @@ -121,7 +121,7 @@ public class YangUtils { * @return the Collection of NormalizedNode object */ public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext) { - return parseJsonData(jsonData, schemaContext, Optional.empty()); + return parseJsonDataWithOptionalParent(jsonData, schemaContext, Optional.empty()); } /** @@ -132,40 +132,59 @@ public class YangUtils { * @param parentNodeXpath the xpath referencing the parent node current data fragment belong to * @return the NormalizedNode object */ - public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext, - final String parentNodeXpath) { - final Collection<QName> dataSchemaNodeIdentifiers = - (Collection<QName>) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext) - .get("dataSchemaNodeIdentifiers"); - return parseJsonData(jsonData, schemaContext, Optional.of(dataSchemaNodeIdentifiers)); + public static ContainerNode parseJsonData(final String jsonData, + final SchemaContext schemaContext, + final String parentNodeXpath) { + return parseJsonDataWithOptionalParent(jsonData, schemaContext, Optional.of(parentNodeXpath)); + } + + /** + * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument). + * + * @param nodeIdentifier the NodeIdentifier + * @return a xpath + */ + public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) { + final StringBuilder xpathBuilder = new StringBuilder(); + xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName()); + + if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) { + xpathBuilder.append(getKeyAttributesStatement( + (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier)); + } + return xpathBuilder.toString(); } - private static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext, - final Optional<Collection<QName>> dataSchemaNodeIdentifiers) { + private static ContainerNode parseJsonDataWithOptionalParent(final String jsonData, + final SchemaContext schemaContext, + final Optional<String> parentNodeXpath) { final JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02 .getShared((EffectiveModelContext) schemaContext); final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> dataContainerNodeBuilder = Builders.containerBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName())); + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier( + QName.create(DATA_ROOT_NODE_NAMESPACE, DATA_ROOT_NODE_TAG_NAME) + )); final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter .from(dataContainerNodeBuilder); final JsonReader jsonReader = new JsonReader(new StringReader(jsonData)); final JsonParserStream jsonParserStream; - if (dataSchemaNodeIdentifiers.isPresent()) { + if (parentNodeXpath.isPresent()) { + final Collection<QName> dataSchemaNodeIdentifiers + = getDataSchemaNodeIdentifiers(schemaContext, parentNodeXpath.get()); final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext); final EffectiveStatementInference effectiveStatementInference = SchemaInferenceStack.of(effectiveModelContext, - SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference(); + SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers)).toInference(); jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, effectiveStatementInference); } else { jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory); } - try { + try (jsonParserStream) { jsonParserStream.parse(jsonReader); - jsonParserStream.close(); } catch (final IOException | JsonSyntaxException exception) { throw new DataValidationException( "Failed to parse json data: " + jsonData, exception.getMessage(), exception); @@ -177,58 +196,58 @@ public class YangUtils { return dataContainerNodeBuilder.build(); } - private static ContainerNode parseXmlData(final String xmlData, final SchemaContext schemaContext, - final Optional<Collection<QName>> dataSchemaNodeIdentifiers) { + private static ContainerNode parseXmlDataWithOptionalParent(final String xmlData, + final SchemaContext schemaContext, + final Optional<String> parentNodeXpath) { final XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(XMLInputFactory.SUPPORT_DTD, false); final NormalizedNodeResult normalizedNodeResult = new NormalizedNodeResult(); final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter .from(normalizedNodeResult); - final XmlParserStream xmlParser; - final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext); - - if (dataSchemaNodeIdentifiers.isPresent()) { - final EffectiveStatementInference effectiveStatementInference = + final EffectiveModelContext effectiveModelContext = (EffectiveModelContext) schemaContext; + final XmlParserStream xmlParserStream; + final String preparedXmlContent; + try { + if (parentNodeXpath.isPresent()) { + final DataSchemaNode parentSchemaNode = + (DataSchemaNode) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath.get(), schemaContext) + .get("dataSchemaNode"); + final Collection<QName> dataSchemaNodeIdentifiers = + getDataSchemaNodeIdentifiers(schemaContext, parentNodeXpath.get()); + final EffectiveStatementInference effectiveStatementInference = SchemaInferenceStack.of(effectiveModelContext, - SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference(); - xmlParser = XmlParserStream.create(normalizedNodeStreamWriter, effectiveStatementInference); - } else { - xmlParser = XmlParserStream.create(normalizedNodeStreamWriter, effectiveModelContext); - } + SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers)).toInference(); + preparedXmlContent = XmlFileUtils.prepareXmlContent(xmlData, parentSchemaNode, parentNodeXpath.get()); + xmlParserStream = XmlParserStream.create(normalizedNodeStreamWriter, effectiveStatementInference); + } else { + preparedXmlContent = XmlFileUtils.prepareXmlContent(xmlData, schemaContext); + xmlParserStream = XmlParserStream.create(normalizedNodeStreamWriter, effectiveModelContext); + } - try { - final XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(xmlData)); - xmlParser.parse(reader); - xmlParser.close(); - } catch (final XMLStreamException | URISyntaxException | IOException - | SAXException | NullPointerException exception) { + try (xmlParserStream; + StringReader stringReader = new StringReader(preparedXmlContent)) { + final XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(stringReader); + xmlParserStream.parse(xmlStreamReader); + } + } catch (final XMLStreamException | URISyntaxException | IOException | SAXException | NullPointerException + | ParserConfigurationException | TransformerException exception) { throw new DataValidationException( - "Failed to parse xml data: " + xmlData, exception.getMessage(), exception); + "Failed to parse xml data: " + xmlData, exception.getMessage(), exception); } - final NormalizedNode normalizedNode = getFirstChildXmlRoot(normalizedNodeResult.getResult()); - return Builders.containerBuilder().withChild((DataContainerChild) normalizedNode) - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName())).build(); + final DataContainerChild dataContainerChild = + (DataContainerChild) getFirstChildXmlRoot(normalizedNodeResult.getResult()); + final YangInstanceIdentifier.NodeIdentifier nodeIdentifier = + new YangInstanceIdentifier.NodeIdentifier(dataContainerChild.getIdentifier().getNodeType()); + return Builders.containerBuilder().withChild(dataContainerChild).withNodeIdentifier(nodeIdentifier).build(); } - /** - * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument). - * - * @param nodeIdentifier the NodeIdentifier - * @return a xpath - */ - public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) { - final StringBuilder xpathBuilder = new StringBuilder(); - xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName()); - - if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) { - xpathBuilder.append(getKeyAttributesStatement( - (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier)); - } - return xpathBuilder.toString(); + private static Collection<QName> getDataSchemaNodeIdentifiers(final SchemaContext schemaContext, + final String parentNodeXpath) { + return (Collection<QName>) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext) + .get("dataSchemaNodeIdentifiers"); } - private static String getKeyAttributesStatement( final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) { final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map( @@ -313,4 +332,4 @@ public class YangUtils { } return getFirstChildXmlRoot(child); } -}
\ No newline at end of file +} |