*/
package org.opendaylight.controller.xml.codec;
-import com.google.common.base.Function;
-import com.google.common.base.Objects;
import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-import com.google.common.collect.ImmutableList;
+import java.net.URI;
+import java.util.Collection;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.ModifyAction;
-import org.opendaylight.yangtools.yang.data.api.Node;
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
-import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
-import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-
-import javax.activation.UnsupportedDataTypeException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-import javax.xml.transform.dom.DOMResult;
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import static com.google.common.base.Preconditions.checkState;
public class XmlDocumentUtils {
- private static class ElementWithSchemaContext {
- Element element;
- SchemaContext schemaContext;
-
- ElementWithSchemaContext(final Element element,final SchemaContext schemaContext) {
- this.schemaContext = schemaContext;
- this.element = element;
- }
-
- Element getElement() {
- return element;
- }
-
- SchemaContext getSchemaContext() {
- return schemaContext;
- }
- }
-
public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
- private static final Logger LOG = LoggerFactory.getLogger(XmlDocumentUtils.class);
- private static final XMLOutputFactory FACTORY = XMLOutputFactory.newFactory();
-
- /**
- * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
- * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
- * be transformed to root in XML Document. Each element of Data DOM tree is compared against specified Data
- * Node Container Schema and transformed accordingly.
- *
- * @param data Data DOM root element
- * @param schema Data Node Container Schema
- * @param codecProvider XML Codec Provider
- * @return new instance of XML Document
- * @throws javax.activation.UnsupportedDataTypeException
- */
- public static Document toDocument(final CompositeNode data, final DataNodeContainer schema, final XmlCodecProvider codecProvider)
- throws UnsupportedDataTypeException {
- Preconditions.checkNotNull(data);
- Preconditions.checkNotNull(schema);
-
- if (!(schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode)) {
- throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
- }
-
- final DOMResult result = new DOMResult(getDocument());
- try {
- final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
- XmlStreamUtils.create(codecProvider).writeDocument(writer, data, (SchemaNode)schema);
- writer.close();
- return (Document)result.getNode();
- } catch (XMLStreamException e) {
- LOG.error("Failed to serialize data {}", data, e);
- return null;
- }
- }
public static Document getDocument() {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
- DocumentBuilder bob = dbf.newDocumentBuilder();
+ final DocumentBuilder bob = dbf.newDocumentBuilder();
doc = bob.newDocument();
- } catch (ParserConfigurationException e) {
+ } catch (final ParserConfigurationException e) {
throw new RuntimeException(e);
}
return doc;
public static QName qNameFromElement(final Element xmlElement) {
- String namespace = xmlElement.getNamespaceURI();
- String localName = xmlElement.getLocalName();
+ final String namespace = xmlElement.getNamespaceURI();
+ final String localName = xmlElement.getLocalName();
return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
}
- private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
- checkQName(xmlElement, schema.getQName());
- if (schema instanceof DataNodeContainer) {
- return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, schemaCtx);
- } else if (schema instanceof LeafSchemaNode) {
- return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider,schemaCtx);
- } else if (schema instanceof LeafListSchemaNode) {
- return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider,schemaCtx);
- }
- return null;
- }
-
-
-
- private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
- final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
- TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
- String text = xmlElement.getTextContent();
- Object value = null;
- if (codec != null) {
- LOG.debug("toSimpleNodeWithType: found codec, deserializing text {}", text);
- value = codec.deserialize(text);
- }
-
- final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
- if (baseType instanceof InstanceIdentifierType) {
- LOG.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
- value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
-
- } else if(baseType instanceof IdentityrefTypeDefinition){
- LOG.debug("toSimpleNodeWithType: base type of node is IdentityrefTypeDefinition, deserializing element", xmlElement);
- value = InstanceIdentifierForXmlCodec.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
-
- }
-
- if (value == null) {
- LOG.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
- value = xmlElement.getTextContent();
- }
-
- Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
- return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
- }
-
- private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
- final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
- TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
- String text = xmlElement.getTextContent();
- Object value = null;
- if (codec != null) {
- LOG.debug("toSimpleNodeWithType: found codec, deserializing text {}", text);
- value = codec.deserialize(text);
- }
-
- final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
- if (baseType instanceof InstanceIdentifierType) {
- LOG.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
- value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
- }
-
- if (value == null) {
- LOG.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
- value = xmlElement.getTextContent();
- }
-
- Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
- return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
- }
-
- private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
- final SchemaContext schemaCtx) {
- List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
- Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
- return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
- }
-
- private static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
- Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
- if(attributeNodeNS == null) {
- return Optional.absent();
- }
-
- ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
- Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
-
- return Optional.of(action);
- }
-
- private static void checkQName(final Element xmlElement, final QName qName) {
- checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
- checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
- }
-
public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Collection<DataSchemaNode> dataSchemaNode) {
if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
- for (DataSchemaNode dsn : dataSchemaNode) {
+ for (final DataSchemaNode dsn : dataSchemaNode) {
if (qname.isEqualWithoutRevision(dsn.getQName())) {
return Optional.<DataSchemaNode> of(dsn);
} else if (dsn instanceof ChoiceSchemaNode) {
- for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
- Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
+ for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
+ final Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
if (foundDsn != null && foundDsn.isPresent()) {
return foundDsn;
}
return Optional.absent();
}
- private static Node<?> toDomNode(Element element) {
- QName qname = qNameFromElement(element);
-
- ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
- NodeList nodes = element.getChildNodes();
- boolean isSimpleObject = true;
- String value = null;
- for (int i = 0; i < nodes.getLength(); i++) {
- org.w3c.dom.Node child = nodes.item(i);
- if (child instanceof Element) {
- isSimpleObject = false;
- values.add(toDomNode((Element) child));
- }
- if (isSimpleObject && child instanceof org.w3c.dom.Text) {
- value = element.getTextContent();
- if (!Strings.isNullOrEmpty(value)) {
- isSimpleObject = true;
- }
- }
- }
- if (isSimpleObject) {
- return new SimpleNodeTOImpl<>(qname, null, value);
- }
- return ImmutableCompositeNode.create(qname, values.build());
- }
-
- public static List<Node<?>> toDomNodes(final Element element, final Optional<Collection<DataSchemaNode>> context,SchemaContext schemaCtx) {
- return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
-
- @Override
- public Optional<Node<?>> apply(ElementWithSchemaContext input) {
- if (context.isPresent()) {
- QName partialQName = qNameFromElement(input.getElement());
- Optional<DataSchemaNode> schemaNode = XmlDocumentUtils.findFirstSchema(partialQName, context.get());
- if (schemaNode.isPresent()) {
- return Optional.<Node<?>> fromNullable(//
- toNodeWithSchema(input.getElement(), schemaNode.get(), XmlDocumentUtils.defaultValueCodecProvider(),input.getSchemaContext()));
- }
- }
- return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
- }
-
- });
-
- }
-
- private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
- final int l = nodes.getLength();
- if (l == 0) {
- return ImmutableList.of();
- }
-
- final List<T> list = new ArrayList<>(l);
- for (int i = 0; i < l; i++) {
- org.w3c.dom.Node child = nodes.item(i);
- if (child instanceof Element) {
- Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
- if (result.isPresent()) {
- list.add(result.get());
- }
- }
- }
- return ImmutableList.copyOf(list);
- }
-
public static final XmlCodecProvider defaultValueCodecProvider() {
return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
}
import java.net.URI;
import java.util.Map.Entry;
import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
-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.impl.codec.TypeDefinitionAwareCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
-import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
return new XmlStreamUtils(codecProvider);
}
- /**
- * Check if a particular data element can be emitted as an empty element, bypassing value encoding. This
- * functionality is optional, as valid XML stream is produced even if start/end element is produced unconditionally.
- *
- * @param data Data node
- * @return True if the data node will result in empty element body.
- */
- public static boolean isEmptyElement(final Node<?> data) {
- if (data == null) {
- return true;
- }
-
- if (data instanceof CompositeNode) {
- return ((CompositeNode) data).getValue().isEmpty();
- }
- if (data instanceof SimpleNode) {
- return data.getValue() == null;
- }
-
- // Safe default
- return false;
- }
-
/**
* Write an InstanceIdentifier into the output stream. Calling corresponding {@link javax.xml.stream.XMLStreamWriter#writeStartElement(String)}
* and {@link javax.xml.stream.XMLStreamWriter#writeEndElement()} is the responsibility of the caller.
final RandomPrefix prefixes = new RandomPrefix();
final String str = XmlUtils.encodeIdentifier(prefixes, id);
- for (Entry<URI, String> e: prefixes.getPrefixes()) {
+ for (final Entry<URI, String> e: prefixes.getPrefixes()) {
writer.writeNamespace(e.getValue(), e.getKey().toString());
}
if(LOG.isDebugEnabled()) {
writer.writeCharacters(str);
}
- /**
- * Write a full XML document corresponding to a CompositeNode into an XML stream writer.
- *
- * @param writer XML Stream writer
- * @param data data node
- * @param schema corresponding schema node, may be null
- * @throws javax.xml.stream.XMLStreamException if an encoding problem occurs
- */
- public void writeDocument(final @Nonnull XMLStreamWriter writer, final @Nonnull CompositeNode data, final @Nullable SchemaNode schema) throws XMLStreamException {
- // final Boolean repairing = (Boolean) writer.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
- // Preconditions.checkArgument(repairing == true, "XML Stream Writer has to be repairing namespaces");
-
- writer.writeStartDocument();
- writeElement(writer, data, schema);
- writer.writeEndDocument();
- writer.flush();
- }
-
-
- /**
- * Write an element into a XML stream writer. This includes the element start/end tags and
- * the value of the element.
- *
- * @param writer XML Stream writer
- * @param data data node
- * @param schema Schema node
- * @throws javax.xml.stream.XMLStreamException if an encoding problem occurs
- */
- public void writeElement(final XMLStreamWriter writer, final @Nonnull Node<?> data, final SchemaNode schema) throws XMLStreamException {
- final QName qname = data.getNodeType();
- final String ns = qname.getNamespace() != null ? qname.getNamespace().toString() : "";
-
- if (isEmptyElement(data)) {
- writer.writeEmptyElement("", qname.getLocalName(), ns);
- return;
- }
-
- writer.writeStartElement("", qname.getLocalName(), ns);
- if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
- for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
- writer.writeAttribute(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(), attribute.getValue());
- }
- }
-
- if (data instanceof SimpleNode<?>) {
- LOG.debug("writeElement : node is of type SimpleNode");
- // Simple node
- if (schema instanceof LeafListSchemaNode) {
- writeValue(writer, ((LeafListSchemaNode) schema).getType(), data.getValue());
- } else if (schema instanceof LeafSchemaNode) {
- writeValue(writer, ((LeafSchemaNode) schema).getType(), data.getValue());
- } else {
- Object value = data.getValue();
- if (value != null) {
- writer.writeCharacters(String.valueOf(value));
- }
- }
- } else {
- LOG.debug("writeElement : node is of type CompositeNode");
- // CompositeNode
- for (Node<?> child : ((CompositeNode) data).getValue()) {
- DataSchemaNode childSchema = null;
- if (schema instanceof DataNodeContainer) {
- childSchema = SchemaUtils.findFirstSchema(child.getNodeType(), ((DataNodeContainer) schema).getChildNodes()).orNull();
- if (childSchema == null && LOG.isDebugEnabled()) {
- LOG.debug("Probably the data node \"{}\" does not conform to schema", child == null ? "" : child.getNodeType().getLocalName());
- }
- }
-
- writeElement(writer, child, childSchema);
- }
- }
-
- writer.writeEndElement();
- }
-
/**
* Write a value into a XML stream writer. This method assumes the start and end of element is
* emitted by the caller.
if (codec != null) {
try {
text = codec.serialize(value);
- } catch (ClassCastException e) {
+ } catch (final ClassCastException e) {
LOG.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", value, baseType, e);
text = String.valueOf(value);
}
*/
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
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){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converting input composite node to xml {}", cNode);
- }
- if (cNode == null) {
- return BLANK;
- }
-
- if(schemaContext == null) {
- return BLANK;
- }
-
- Document domTree = null;
- try {
- Set<RpcDefinition> rpcs = schemaContext.getOperations();
- for(RpcDefinition rpc : rpcs) {
- if(rpc.getQName().equals(cNode.getNodeType())){
- if(LOG.isDebugEnabled()) {
- 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());
- if(LOG.isDebugEnabled()) {
- 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){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converting output composite node to xml {}", cNode);
- }
- if (cNode == null) {
- return BLANK;
- }
-
- if(schemaContext == null) {
- return BLANK;
- }
-
- Document domTree = null;
- try {
- Set<RpcDefinition> rpcs = schemaContext.getOperations();
- for(RpcDefinition rpc : rpcs) {
- if(rpc.getQName().equals(cNode.getNodeType())){
- if(LOG.isDebugEnabled()) {
- 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());
- if(LOG.isDebugEnabled()) {
- 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);
- }
- if(LOG.isDebugEnabled()) {
- 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){
- if(LOG.isDebugEnabled()) {
- 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<RpcDefinition> rpcs = schemaContext.getOperations();
- for(RpcDefinition rpcDef : rpcs) {
- if(rpcDef.getQName().equals(rpc)){
- if(LOG.isDebugEnabled()) {
- 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<Node<?>> dataNodes = XmlDocumentUtils.toDomNodes(xmlData,
- Optional.of(rpcDef.getInput().getChildNodes()), schemaContext);
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converted xml input to list of nodes {}", dataNodes);
- }
- final CompositeNodeBuilder<ImmutableCompositeNode> 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);
- }
- if(LOG.isDebugEnabled()) {
- 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) {
*/
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<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
+ final Map<QName, Object> 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("='");
package org.opendaylight.controller.xml.codec;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
-import org.junit.Assert;
import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.ModifyAction;
-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.impl.ImmutableCompositeNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+// FIXME : CompositeNode is not avaliable anymore so fix the test to use NormalizedNodeContainer ASAP
public class XmlUtilsTest {
private static final DocumentBuilderFactory BUILDERFACTORY;
testRpc = rpcTestModule.getRpcs().iterator().next();
}
- @Test
- public void testNullInputXmlToComposite() {
- CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), null, schema);
- Assert.assertNull(node);
- }
-
- @Test
- public void testNullRpcXmlToComposite() {
- CompositeNode node = XmlUtils.inputXmlToCompositeNode(null, XML_CONTENT, schema);
- Assert.assertNull(node);
- }
-
- @Test
- public void testInputXmlToCompositeNode() {
- CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
- ImmutableList<SimpleNode<?>> input = (ImmutableList<SimpleNode<?>>)node.getValue().get(0).getValue();
- SimpleNode<?> firstNode = input.get(0);
-
- Assert.assertEquals("id", firstNode.getNodeType().getLocalName());
- Assert.assertEquals("flowid", firstNode.getValue());
-
- SimpleNode<?> secondNode = input.get(1);
- Assert.assertEquals("flow", secondNode.getNodeType().getLocalName());
-
- YangInstanceIdentifier instance = (YangInstanceIdentifier) secondNode.getValue();
- Iterable<YangInstanceIdentifier.PathArgument> iterable = instance.getPathArguments();
- Iterator<YangInstanceIdentifier.PathArgument> it = iterable.iterator();
- YangInstanceIdentifier.NodeIdentifier firstPath = (YangInstanceIdentifier.NodeIdentifier) it.next();
- Assert.assertEquals("node", firstPath.getNodeType().getLocalName());
- YangInstanceIdentifier.NodeIdentifierWithPredicates secondPath = (YangInstanceIdentifier.NodeIdentifierWithPredicates)it.next();
- Short value = (Short)secondPath.getKeyValues().values().iterator().next();
- Short expected = 3;
- Assert.assertEquals(expected, value);
- }
-
- @Test
- public void testInputCompositeNodeToXML() {
- CompositeNode input = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
- List<Node<?>> childNodes = new ArrayList<>();
- childNodes.add(input);
- QName rpcQName = schema.getOperations().iterator().next().getQName();
- CompositeNode node = new ImmutableCompositeNode(rpcQName, input.getValue(), ModifyAction.REPLACE);
- String xml = XmlUtils.inputCompositeNodeToXml(node, schema);
- Assert.assertNotNull(xml);
- Assert.assertTrue(xml.contains("3@java.lang.Short"));
- }
-
- @Test
- public void testNullCompositeNodeToXml(){
- String xml = XmlUtils.inputCompositeNodeToXml(null, schema);
- Assert.assertTrue(xml.isEmpty());
- }
-
- @Test
- public void testNullSchemaCompositeNodeToXml(){
- String xml = XmlUtils.inputCompositeNodeToXml(new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE), null);
- Assert.assertTrue(xml.isEmpty());
- }
-
-
}