X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fxml%2Fcodec%2FXmlUtils.java;h=85d2c31ad3bff437ffcd77d922ab0269c652857a;hb=412db94945c5db5d2da918f5e23bd3abcecc4d10;hp=ea8f4a3ef19810a6d95ebc4211b0f4569b6e2716;hpb=b10d77375b5a290143106180f1583ea4e18f8478;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/xml/codec/XmlUtils.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/xml/codec/XmlUtils.java index ea8f4a3ef1..85d2c31ad3 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/xml/codec/XmlUtils.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/xml/codec/XmlUtils.java @@ -7,46 +7,16 @@ */ package org.opendaylight.controller.xml.codec; -import com.google.common.base.Optional; -import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import java.util.Map; +import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.api.SimpleNode; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; -import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; -import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider; -import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder; -import org.opendaylight.yangtools.yang.model.api.RpcDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import javax.activation.UnsupportedDataTypeException; -import javax.annotation.Nonnull; -import javax.xml.stream.XMLStreamException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.StringWriter; -import java.util.List; -import java.util.Map; -import java.util.Set; /** * Common XML-related utility methods, which are not specific to a particular @@ -64,187 +34,6 @@ public class XmlUtils { private XmlUtils() { } - private static final String BLANK = ""; - private static final Logger LOG = LoggerFactory.getLogger(XmlUtils.class); - - /** - * Converts the composite node to xml using rpc input schema node - * @param cNode - * @param schemaContext - * @return xml String - */ - public static String inputCompositeNodeToXml(CompositeNode cNode, SchemaContext schemaContext){ - LOG.debug("Converting input composite node to xml {}", cNode); - if (cNode == null) { - return BLANK; - } - - if(schemaContext == null) { - return BLANK; - } - - Document domTree = null; - try { - Set rpcs = schemaContext.getOperations(); - for(RpcDefinition rpc : rpcs) { - if(rpc.getQName().equals(cNode.getNodeType())){ - LOG.debug("Found the rpc definition from schema context matching with input composite node {}", rpc.getQName()); - - CompositeNode inputContainer = cNode.getFirstCompositeByName(QName.create(cNode.getNodeType(), "input")); - domTree = XmlDocumentUtils.toDocument(inputContainer, rpc.getInput(), XmlDocumentUtils.defaultValueCodecProvider()); - - LOG.debug("input composite node to document conversion complete, document is {}", domTree); - break; - } - } - - } catch (UnsupportedDataTypeException e) { - LOG.error("Error during translation of CompositeNode to Document", e); - } - return domTransformer(domTree); - } - - /** - * Converts the composite node to xml String using rpc output schema node - * @param cNode - * @param schemaContext - * @return xml string - */ - public static String outputCompositeNodeToXml(CompositeNode cNode, SchemaContext schemaContext){ - LOG.debug("Converting output composite node to xml {}", cNode); - if (cNode == null) { - return BLANK; - } - - if(schemaContext == null) { - return BLANK; - } - - Document domTree = null; - try { - Set rpcs = schemaContext.getOperations(); - for(RpcDefinition rpc : rpcs) { - if(rpc.getQName().equals(cNode.getNodeType())){ - LOG.debug("Found the rpc definition from schema context matching with output composite node {}", rpc.getQName()); - - CompositeNode outputContainer = cNode.getFirstCompositeByName(QName.create(cNode.getNodeType(), "output")); - domTree = XmlDocumentUtils.toDocument(outputContainer, rpc.getOutput(), XmlDocumentUtils.defaultValueCodecProvider()); - - LOG.debug("output composite node to document conversion complete, document is {}", domTree); - break; - } - } - - } catch (UnsupportedDataTypeException e) { - LOG.error("Error during translation of CompositeNode to Document", e); - } - return domTransformer(domTree); - } - - private static String domTransformer(Document domTree) { - StringWriter writer = new StringWriter(); - try { - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - transformer.transform(new DOMSource(domTree), new StreamResult(writer)); - } catch (TransformerException e) { - - LOG.error("Error during translation of Document to OutputStream", e); - } - LOG.debug("Document to string conversion complete, xml string is {} ", writer.toString()); - - return writer.toString(); - } - - public static CompositeNode xmlToCompositeNode(String xml){ - if (xml==null || xml.length()==0) { - return null; - } - - Node dataTree; - try { - dataTree = XmlTreeBuilder.buildDataTree(new ByteArrayInputStream(xml.getBytes())); - } catch (XMLStreamException e) { - LOG.error("Error during building data tree from XML", e); - return null; - } - if (dataTree == null) { - LOG.error("data tree is null"); - return null; - } - if (dataTree instanceof SimpleNode) { - LOG.error("RPC XML was resolved as SimpleNode"); - return null; - } - return (CompositeNode) dataTree; - } - - /** - * Converts the xml to composite node using rpc input schema node - * @param rpc - * @param xml - * @param schemaContext - * @return CompositeNode object based on the input, if any of the input parameter is null, a null object is returned - */ - public static CompositeNode inputXmlToCompositeNode(QName rpc, String xml, SchemaContext schemaContext){ - LOG.debug("Converting input xml to composite node {}", xml); - if (xml==null || xml.length()==0) { - return null; - } - - if(rpc == null) { - return null; - } - - if(schemaContext == null) { - return null; - } - - CompositeNode compositeNode = null; - try { - - Document doc = XmlUtil.readXmlToDocument(xml); - Set rpcs = schemaContext.getOperations(); - for(RpcDefinition rpcDef : rpcs) { - if(rpcDef.getQName().equals(rpc)){ - LOG.debug("found the rpc definition from schema context matching rpc {}", rpc); - - if(rpcDef.getInput() == null) { - LOG.warn("found rpc definition's input is null"); - return null; - } - - QName input = rpcDef.getInput().getQName(); - NodeList nodeList = doc.getElementsByTagNameNS(input.getNamespace().toString(), "input"); - if(nodeList == null || nodeList.getLength() < 1) { - LOG.warn("xml does not have input entry. {}", xml); - return null; - } - Element xmlData = (Element)nodeList.item(0); - - List> dataNodes = XmlDocumentUtils.toDomNodes(xmlData, - Optional.of(rpcDef.getInput().getChildNodes()), schemaContext); - - LOG.debug("Converted xml input to list of nodes {}", dataNodes); - - final CompositeNodeBuilder it = ImmutableCompositeNode.builder(); - it.setQName(rpc); - it.add(ImmutableCompositeNode.create(input, dataNodes)); - compositeNode = it.toInstance(); - break; - } - } - } catch (SAXException e) { - LOG.error("Error during building data tree from XML", e); - } catch (IOException e) { - LOG.error("Error during building data tree from XML", e); - } - - LOG.debug("Xml to composite node conversion complete {} ", compositeNode); - return compositeNode; - } - public static TypeDefinition resolveBaseTypeFrom(final @Nonnull TypeDefinition type) { TypeDefinition superType = type; while (superType.getBaseType() != null) { @@ -266,17 +55,17 @@ public class XmlUtils { */ public static String encodeIdentifier(final RandomPrefix prefixes, final YangInstanceIdentifier id) { - StringBuilder textContent = new StringBuilder(); - for (PathArgument pathArgument : id.getPathArguments()) { + final StringBuilder textContent = new StringBuilder(); + for (final PathArgument pathArgument : id.getPathArguments()) { textContent.append('/'); textContent.append(prefixes.encodeQName(pathArgument.getNodeType())); if (pathArgument instanceof NodeIdentifierWithPredicates) { - Map predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues(); + final Map predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues(); - for (QName keyValue : predicates.keySet()) { - Object value = predicates.get(keyValue); - String type = value.getClass().getName(); - String predicateValue = String.valueOf(value); + for (final QName keyValue : predicates.keySet()) { + final Object value = predicates.get(keyValue); + final String type = value.getClass().getName(); + final String predicateValue = String.valueOf(value); textContent.append('['); textContent.append(prefixes.encodeQName(keyValue)); textContent.append("='");