2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.opendaylight.yangtools.yang.data.impl.codec.InstanceIdentifierForXmlCodec.INSTANCE_IDENTIFIER_FOR_XML_CODEC;
14 import java.util.List;
15 import java.util.Map.Entry;
18 import javax.activation.UnsupportedDataTypeException;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
25 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
28 import org.opendaylight.yangtools.yang.data.api.Node;
29 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
30 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
31 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
32 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
33 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.w3c.dom.Attr;
51 import org.w3c.dom.Document;
52 import org.w3c.dom.Element;
53 import org.w3c.dom.NodeList;
55 import com.google.common.base.Function;
56 import com.google.common.base.Objects;
57 import com.google.common.base.Optional;
58 import com.google.common.base.Preconditions;
59 import com.google.common.base.Strings;
60 import com.google.common.collect.ImmutableList;
62 public class XmlDocumentUtils {
64 private static class ElementWithSchemaContext {
66 SchemaContext schemaContext;
68 ElementWithSchemaContext(final Element element,final SchemaContext schemaContext) {
69 this.schemaContext = schemaContext;
70 this.element = element;
73 Element getElement() {
77 SchemaContext getSchemaContext() {
82 private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
85 public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
86 return TypeDefinitionAwareCodec.from(baseType);
90 private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
93 * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
94 * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
95 * be transformed to root in XML Document. Each element of Data DOM tree is compared against specified Data
96 * Node Container Schema and transformed accordingly.
98 * @param data Data DOM root element
99 * @param schema Data Node Container Schema
100 * @param codecProvider XML Codec Provider
101 * @return new instance of XML Document
102 * @throws UnsupportedDataTypeException
104 public static Document toDocument(final CompositeNode data, final DataNodeContainer schema, final XmlCodecProvider codecProvider)
105 throws UnsupportedDataTypeException {
106 Preconditions.checkNotNull(data);
107 Preconditions.checkNotNull(schema);
109 Document doc = getDocument();
111 if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
112 doc.appendChild(createXmlRootElement(doc, data, (SchemaNode) schema, codecProvider));
115 throw new UnsupportedDataTypeException(
116 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
120 public static Document getDocument() {
121 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
124 DocumentBuilder bob = dbf.newDocumentBuilder();
125 doc = bob.newDocument();
126 } catch (ParserConfigurationException e) {
127 throw new RuntimeException(e);
133 * Converts Data DOM structure to XML Document for specified XML Codec Provider. The CompositeNode
134 * data parameter enters as root of Data DOM tree and will be transformed to root in XML Document. The child
135 * nodes of Data Tree are transformed accordingly.
137 * @param data Data DOM root element
138 * @param codecProvider XML Codec Provider
139 * @return new instance of XML Document
140 * @throws UnsupportedDataTypeException
142 public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider)
143 throws UnsupportedDataTypeException {
144 Preconditions.checkNotNull(data);
146 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
147 dbf.setNamespaceAware(true);
150 DocumentBuilder bob = dbf.newDocumentBuilder();
151 doc = bob.newDocument();
152 } catch (ParserConfigurationException e) {
156 doc.appendChild(createXmlRootElement(doc, data, null, codecProvider));
160 private static Element createXmlRootElement(final Document doc, final Node<?> data, final SchemaNode schema,
161 final XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
162 Element itemEl = createElementFor(doc, data);
163 if (data instanceof SimpleNode<?>) {
164 if (schema instanceof LeafListSchemaNode) {
165 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
166 (DataSchemaNode) schema, codecProvider);
167 } else if (schema instanceof LeafSchemaNode) {
168 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
169 (DataSchemaNode) schema, codecProvider);
171 Object value = data.getValue();
173 itemEl.setTextContent(String.valueOf(value));
176 } else { // CompositeNode
177 for (Node<?> child : ((CompositeNode) data).getValue()) {
178 DataSchemaNode childSchema = null;
179 if (schema instanceof DataNodeContainer) {
180 childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
181 if (logger.isDebugEnabled()) {
182 if (childSchema == null) {
183 logger.debug("Probably the data node \""
184 + ((child == null) ? "" : child.getNodeType().getLocalName())
185 + "\" is not conform to schema");
189 itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
195 public static Element createElementFor(final Document doc, final Node<?> data) {
196 QName dataType = data.getNodeType();
198 if (dataType.getNamespace() != null) {
199 ret = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
201 ret = doc.createElementNS(null, dataType.getLocalName());
203 if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
204 for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
205 ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
206 attribute.getValue());
213 public static void writeValueByType(final Element element, final SimpleNode<?> node, final TypeDefinition<?> type,
214 final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
216 Object nodeValue = node.getValue();
218 writeValueByType(element, type, codecProvider, nodeValue);
221 public static void writeValueByType(final Element element, final TypeDefinition<?> type, final XmlCodecProvider codecProvider, final Object nodeValue) {
222 TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
223 if (baseType instanceof IdentityrefTypeDefinition) {
224 if (nodeValue instanceof QName) {
225 QName value = (QName) nodeValue;
227 if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
228 prefix = value.getPrefix();
230 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
231 element.setTextContent(prefix + ":" + value.getLocalName());
233 Object value = nodeValue;
234 logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(),
235 baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
237 element.setTextContent(String.valueOf(value));
240 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
241 if (nodeValue instanceof InstanceIdentifier) {
242 INSTANCE_IDENTIFIER_FOR_XML_CODEC.serialize((InstanceIdentifier)nodeValue,element);
244 Object value = nodeValue;
245 logger.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
247 baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
249 element.setTextContent(String.valueOf(value));
253 if (nodeValue != null) {
254 final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
257 final String text = codec.serialize(nodeValue);
258 element.setTextContent(text);
259 } catch (ClassCastException e) {
260 logger.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", nodeValue, baseType, e);
261 element.setTextContent(String.valueOf(nodeValue));
264 logger.error("Failed to find codec for {}, falling back to using stream", baseType);
265 element.setTextContent(String.valueOf(nodeValue));
272 public final static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
273 TypeDefinition<?> superType = type;
274 while (superType.getBaseType() != null) {
275 superType = superType.getBaseType();
280 private static final DataSchemaNode findFirstSchemaForNode(final Node<?> node, final Set<DataSchemaNode> dataSchemaNode) {
281 if (dataSchemaNode != null && node != null) {
282 for (DataSchemaNode dsn : dataSchemaNode) {
283 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
285 } else if (dsn instanceof ChoiceNode) {
286 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
287 DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
288 if (foundDsn != null) {
298 public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
299 final Optional<XmlCodecProvider> codecProvider) {
300 if (schema.isPresent()) {
301 return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
303 return toDomNode(xmlElement);
306 public static CompositeNode fromElement(final Element xmlElement) {
307 CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
308 node.setQName(qNameFromElement(xmlElement));
310 return node.toInstance();
313 public static QName qNameFromElement(final Element xmlElement) {
314 String namespace = xmlElement.getNamespaceURI();
315 String localName = xmlElement.getLocalName();
316 return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
319 private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
320 checkQName(xmlElement, schema.getQName());
321 if (schema instanceof DataNodeContainer) {
322 return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider,schemaCtx);
323 } else if (schema instanceof LeafSchemaNode) {
324 return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider,schemaCtx);
325 } else if (schema instanceof LeafListSchemaNode) {
326 return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider,schemaCtx);
331 private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
332 return toNodeWithSchema(xmlElement, schema, codecProvider, null);
335 protected static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
336 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
337 TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
338 String text = xmlElement.getTextContent();
341 value = codec.deserialize(text);
344 if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
345 value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.deserialize(xmlElement,schemaCtx);
346 } else if(schema.getType() instanceof IdentityrefTypeDefinition){
347 value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
351 value = xmlElement.getTextContent();
354 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
355 return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
358 private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
359 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
360 TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
361 String text = xmlElement.getTextContent();
364 value = codec.deserialize(text);
366 if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
367 value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.deserialize(xmlElement,schemaCtx);
370 value = xmlElement.getTextContent();
373 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
374 return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
377 private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
378 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
379 List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
380 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
381 return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
384 public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
386 public static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
387 Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
388 if(attributeNodeNS == null) {
389 return Optional.absent();
392 ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
393 Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
395 return Optional.of(action);
398 private static void checkQName(final Element xmlElement, final QName qName) {
399 checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
400 checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
403 public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Set<DataSchemaNode> dataSchemaNode) {
404 if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
405 for (DataSchemaNode dsn : dataSchemaNode) {
406 if (qname.isEqualWithoutRevision(dsn.getQName())) {
407 return Optional.<DataSchemaNode> of(dsn);
408 } else if (dsn instanceof ChoiceNode) {
409 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
410 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
411 if (foundDsn != null && foundDsn.isPresent()) {
418 return Optional.absent();
421 public static Node<?> toDomNode(final Document doc) {
422 return toDomNode(doc.getDocumentElement());
425 private static Node<?> toDomNode(final Element element) {
426 QName qname = qNameFromElement(element);
428 ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
429 NodeList nodes = element.getChildNodes();
430 boolean isSimpleObject = true;
432 for (int i = 0; i < nodes.getLength(); i++) {
433 org.w3c.dom.Node child = nodes.item(i);
434 if (child instanceof Element) {
435 isSimpleObject = false;
436 values.add(toDomNode((Element) child));
438 if (isSimpleObject && child instanceof org.w3c.dom.Text) {
439 value = element.getTextContent();
440 if (!Strings.isNullOrEmpty(value)) {
441 isSimpleObject = true;
445 if (isSimpleObject) {
446 return new SimpleNodeTOImpl<>(qname, null, value);
448 return ImmutableCompositeNode.create(qname, values.build());
451 public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context,final SchemaContext schemaCtx) {
452 return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
455 public Optional<Node<?>> apply(final ElementWithSchemaContext input) {
456 if (context.isPresent()) {
457 QName partialQName = qNameFromElement(input.getElement());
458 Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
459 if (schemaNode.isPresent()) {
460 return Optional.<Node<?>> fromNullable(//
461 toNodeWithSchema(input.getElement(), schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER,input.getSchemaContext()));
464 return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
471 public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
472 return toDomNodes(element,context,null);
476 * Converts XML Document containing notification data from Netconf device to
477 * Data DOM Nodes. <br>
478 * By specification defined in <a
479 * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
480 * there are xml elements containing notifications metadata, like eventTime
481 * or root notification element which specifies namespace for which is
482 * notification defined in yang model. Those elements MUST be stripped off
483 * notifications body. This method returns pure notification body which
484 * begins in element which is equal to notifications name defined in
485 * corresponding yang model. Rest of notification metadata are obfuscated,
486 * thus Data DOM contains only pure notification body.
489 * XML Document containing notification body
490 * @param notifications
491 * Notifications Definition Schema
492 * @return Data DOM Nodes containing xml notification body definition or
493 * <code>null</code> if there is no NotificationDefinition with
494 * Element with equal notification QName defined in XML Document.
496 public static CompositeNode notificationToDomNodes(final Document document,
497 final Optional<Set<NotificationDefinition>> notifications, final SchemaContext schemaCtx) {
498 if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
499 final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
500 for (int i = 0; i < originChildNodes.getLength(); i++) {
501 org.w3c.dom.Node child = originChildNodes.item(i);
502 if (child instanceof Element) {
503 final Element childElement = (Element) child;
504 final QName partialQName = qNameFromElement(childElement);
505 final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
506 notifications.get());
507 if (notificationDef.isPresent()) {
508 final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
509 final List<Node<?>> domNodes = toDomNodes(childElement,
510 Optional.<Set<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
511 return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
519 public static CompositeNode notificationToDomNodes(final Document document,
520 final Optional<Set<NotificationDefinition>> notifications) {
521 return notificationToDomNodes(document, notifications,null);
524 private static Optional<NotificationDefinition> findNotification(final QName notifName,
525 final Set<NotificationDefinition> notifications) {
526 if ((notifName != null) && (notifications != null)) {
527 for (final NotificationDefinition notification : notifications) {
528 if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
529 return Optional.<NotificationDefinition>fromNullable(notification);
533 return Optional.<NotificationDefinition>absent();
536 private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
537 ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
538 for (int i = 0; i < nodes.getLength(); i++) {
539 org.w3c.dom.Node child = nodes.item(i);
540 if (child instanceof Element) {
541 Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
542 if (result.isPresent()) {
543 ret.add(result.get());
550 public static final XmlCodecProvider defaultValueCodecProvider() {
551 return DEFAULT_XML_VALUE_CODEC_PROVIDER;