package org.opendaylight.yangtools.yang.data.impl.codec.xml;
import static com.google.common.base.Preconditions.checkState;
+import static org.opendaylight.yangtools.yang.data.impl.codec.InstanceIdentifierForXmlCodec.INSTANCE_IDENTIFIER_FOR_XML_CODEC;
import java.net.URI;
-import java.util.Collection;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.Map.Entry;
-import java.util.Random;
import java.util.Set;
import javax.activation.UnsupportedDataTypeException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import com.google.common.collect.Maps;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.*;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+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.impl.ImmutableCompositeNode;
import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+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.api.type.InstanceIdentifierTypeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.w3c.dom.*;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
import com.google.common.base.Function;
import com.google.common.base.Objects;
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;
+ }
+ }
+
private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
@Override
- public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
+ public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
return TypeDefinitionAwareCodec.from(baseType);
}
};
* @return new instance of XML Document
* @throws UnsupportedDataTypeException
*/
- public static Document toDocument(CompositeNode data, DataNodeContainer schema, XmlCodecProvider codecProvider)
+ public static Document toDocument(final CompositeNode data, final DataNodeContainer schema, final XmlCodecProvider codecProvider)
throws UnsupportedDataTypeException {
Preconditions.checkNotNull(data);
Preconditions.checkNotNull(schema);
* @return new instance of XML Document
* @throws UnsupportedDataTypeException
*/
- public static Document toDocument(CompositeNode data, XmlCodecProvider codecProvider)
+ public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider)
throws UnsupportedDataTypeException {
Preconditions.checkNotNull(data);
return doc;
}
- private static Element createXmlRootElement(Document doc, Node<?> data, SchemaNode schema,
- XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
+ private static Element createXmlRootElement(final Document doc, final Node<?> data, final SchemaNode schema,
+ final XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
Element itemEl = createElementFor(doc, data);
if (data instanceof SimpleNode<?>) {
if (schema instanceof LeafListSchemaNode) {
}
}
} else { // CompositeNode
- for (Node<?> child : ((CompositeNode) data).getChildren()) {
+ for (Node<?> child : ((CompositeNode) data).getValue()) {
DataSchemaNode childSchema = null;
- if (schema != null) {
+ if (schema instanceof DataNodeContainer) {
childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
if (logger.isDebugEnabled()) {
if (childSchema == null) {
return itemEl;
}
- public static Element createElementFor(Document doc, Node<?> data) {
+ public static Element createElementFor(final Document doc, final Node<?> data) {
QName dataType = data.getNodeType();
Element ret;
if (dataType.getNamespace() != null) {
return ret;
}
- public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
- DataSchemaNode schema, XmlCodecProvider codecProvider) {
+ public static void writeValueByType(final Element element, final SimpleNode<?> node, final TypeDefinition<?> type,
+ final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
Object nodeValue = node.getValue();
writeValueByType(element, type, codecProvider, nodeValue);
}
- public static void writeValueByType(Element element, TypeDefinition<?> type, XmlCodecProvider codecProvider, Object nodeValue) {
+ public static void writeValueByType(final Element element, final TypeDefinition<?> type, final XmlCodecProvider codecProvider, final Object nodeValue) {
TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
if (baseType instanceof IdentityrefTypeDefinition) {
if (nodeValue instanceof QName) {
}
} else if (baseType instanceof InstanceIdentifierTypeDefinition) {
if (nodeValue instanceof InstanceIdentifier) {
- // Map< key = namespace, value = prefix>
- Map<String, String> prefixes = new HashMap<>();
- InstanceIdentifier instanceIdentifier = (InstanceIdentifier) nodeValue;
- StringBuilder textContent = new StringBuilder();
- for (PathArgument pathArgument : instanceIdentifier.getPath()) {
- textContent.append("/");
- writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
- if (pathArgument instanceof NodeIdentifierWithPredicates) {
- Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
-
- for (QName keyValue : predicates.keySet()) {
- String predicateValue = String.valueOf(predicates.get(keyValue));
- textContent.append("[");
- writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
- textContent.append("='");
- textContent.append(predicateValue);
- textContent.append("'");
- textContent.append("]");
- }
- } else if (pathArgument instanceof NodeWithValue) {
- textContent.append("[.='");
- textContent.append(((NodeWithValue)pathArgument).getValue());
- textContent.append("'");
- textContent.append("]");
- }
- }
- element.setTextContent(textContent.toString());
-
+ INSTANCE_IDENTIFIER_FOR_XML_CODEC.serialize((InstanceIdentifier)nodeValue,element);
} else {
Object value = nodeValue;
logger.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
}
}
- private static void writeIdentifierWithNamespacePrefix(Element element, StringBuilder textContent, QName qName,
- Map<String, String> prefixes) {
- String namespace = qName.getNamespace().toString();
- String prefix = prefixes.get(namespace);
- if (prefix == null) {
- prefix = qName.getPrefix();
- if (prefix == null || prefix.isEmpty() || prefixes.containsValue(prefix)) {
- prefix = generateNewPrefix(prefixes.values());
- }
- }
-
- element.setAttribute("xmlns:" + prefix, namespace.toString());
- textContent.append(prefix);
- prefixes.put(namespace, prefix);
-
- textContent.append(":");
- textContent.append(qName.getLocalName());
- }
- private static String generateNewPrefix(Collection<String> prefixes) {
- StringBuilder result = null;
- Random random = new Random();
- do {
- result = new StringBuilder();
- for (int i = 0; i < 4; i++) {
- int randomNumber = 0x61 + (Math.abs(random.nextInt()) % 26);
- result.append(Character.toChars(randomNumber));
- }
- } while (prefixes.contains(result.toString()));
-
- return result.toString();
- }
-
- public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
+ public final static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
TypeDefinition<?> superType = type;
while (superType.getBaseType() != null) {
superType = superType.getBaseType();
return superType;
}
- private static final DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
+ private static final DataSchemaNode findFirstSchemaForNode(final Node<?> node, final Set<DataSchemaNode> dataSchemaNode) {
if (dataSchemaNode != null && node != null) {
for (DataSchemaNode dsn : dataSchemaNode) {
if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
return null;
}
- public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
- Optional<XmlCodecProvider> codecProvider) {
+ public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
+ final Optional<XmlCodecProvider> codecProvider) {
if (schema.isPresent()) {
return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
}
return toDomNode(xmlElement);
}
- public static CompositeNode fromElement(Element xmlElement) {
+ public static CompositeNode fromElement(final Element xmlElement) {
CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
node.setQName(qNameFromElement(xmlElement));
return node.toInstance();
}
- public static QName qNameFromElement(Element xmlElement) {
+ public static QName qNameFromElement(final Element xmlElement) {
String namespace = xmlElement.getNamespaceURI();
String localName = xmlElement.getLocalName();
return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
}
- private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
+ 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, codecProvider);
+ return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider,schemaCtx);
} else if (schema instanceof LeafSchemaNode) {
- return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider);
+ return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider,schemaCtx);
} else if (schema instanceof LeafListSchemaNode) {
- return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider);
-
+ return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider,schemaCtx);
}
return null;
}
- private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafSchemaNode schema,
- XmlCodecProvider codecProvider) {
- TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
+ private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
+ return toNodeWithSchema(xmlElement, schema, codecProvider, null);
+ }
+
+ protected 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;
+ Object value = null;
if (codec != null) {
value = codec.deserialize(text);
+ }
- } else {
+ if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
+ value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.deserialize(xmlElement,schemaCtx);
+ } else if(schema.getType() instanceof IdentityrefTypeDefinition){
+ value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
+ }
+
+ if (value == null) {
value = xmlElement.getTextContent();
}
return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
}
- private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafListSchemaNode schema,
- XmlCodecProvider codecProvider) {
- TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
+ 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;
+ Object value = null;
if (codec != null) {
value = codec.deserialize(text);
-
- } else {
+ }
+ if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
+ value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.deserialize(xmlElement,schemaCtx);
+ }
+ if (value == null) {
value = xmlElement.getTextContent();
}
return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
}
- private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
- XmlCodecProvider codecProvider) {
- List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
+ private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
+ final XmlCodecProvider codecProvider,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());
}
public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
- public static Optional<ModifyAction> getModifyOperationFromAttributes(Element xmlElement) {
+ public 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();
+ 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(Element xmlElement, QName qName) {
+ 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(QName qname, Set<DataSchemaNode> dataSchemaNode) {
+ public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Set<DataSchemaNode> dataSchemaNode) {
if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
for (DataSchemaNode dsn : dataSchemaNode) {
if (qname.isEqualWithoutRevision(dsn.getQName())) {
return Optional.absent();
}
- public static Node<?> toDomNode(Document doc) {
+ public static Node<?> toDomNode(final Document doc) {
return toDomNode(doc.getDocumentElement());
}
- private static Node<?> toDomNode(Element element) {
+ private static Node<?> toDomNode(final Element element) {
QName qname = qNameFromElement(element);
ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
return ImmutableCompositeNode.create(qname, values.build());
}
- public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
- return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
+ public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context,final SchemaContext schemaCtx) {
+ return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
@Override
- public Optional<Node<?>> apply(Element input) {
+ public Optional<Node<?>> apply(final ElementWithSchemaContext input) {
if (context.isPresent()) {
- QName partialQName = qNameFromElement(input);
+ QName partialQName = qNameFromElement(input.getElement());
Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
if (schemaNode.isPresent()) {
return Optional.<Node<?>> fromNullable(//
- toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
+ toNodeWithSchema(input.getElement(), schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER,input.getSchemaContext()));
}
}
- return Optional.<Node<?>> fromNullable(toDomNode(input));
+ return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
}
});
}
+ public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
+ return toDomNodes(element,context,null);
+ }
+
/**
* Converts XML Document containing notification data from Netconf device to
* Data DOM Nodes. <br>
* Element with equal notification QName defined in XML Document.
*/
public static CompositeNode notificationToDomNodes(final Document document,
- final Optional<Set<NotificationDefinition>> notifications) {
+ final Optional<Set<NotificationDefinition>> notifications, final SchemaContext schemaCtx) {
if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
-
for (int i = 0; i < originChildNodes.getLength(); i++) {
org.w3c.dom.Node child = originChildNodes.item(i);
if (child instanceof Element) {
if (notificationDef.isPresent()) {
final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
final List<Node<?>> domNodes = toDomNodes(childElement,
- Optional.<Set<DataSchemaNode>> fromNullable(dataNodes));
+ Optional.<Set<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
}
}
return null;
}
+ public static CompositeNode notificationToDomNodes(final Document document,
+ final Optional<Set<NotificationDefinition>> notifications) {
+ return notificationToDomNodes(document, notifications,null);
+ }
+
private static Optional<NotificationDefinition> findNotification(final QName notifName,
final Set<NotificationDefinition> notifications) {
if ((notifName != null) && (notifications != null)) {
return Optional.<NotificationDefinition>absent();
}
- private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
+ private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node child = nodes.item(i);
if (child instanceof Element) {
- Optional<T> result = forBody.apply((Element) child);
+ Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
if (result.isPresent()) {
ret.add(result.get());
}
public static final XmlCodecProvider defaultValueCodecProvider() {
return DEFAULT_XML_VALUE_CODEC_PROVIDER;
}
-
}