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;
12 import com.google.common.base.Function;
13 import com.google.common.base.Objects;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Strings;
17 import com.google.common.collect.ImmutableList;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
24 import java.util.Map.Entry;
26 import javax.activation.UnsupportedDataTypeException;
27 import javax.annotation.Nonnull;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.stream.XMLOutputFactory;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamWriter;
34 import javax.xml.transform.dom.DOMResult;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
39 import org.opendaylight.yangtools.yang.data.api.Node;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
42 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
43 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
44 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
46 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
47 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
49 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
54 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
57 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
58 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
59 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
60 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
61 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64 import org.w3c.dom.Attr;
65 import org.w3c.dom.Document;
66 import org.w3c.dom.Element;
67 import org.w3c.dom.NodeList;
69 public final class XmlDocumentUtils {
71 private XmlDocumentUtils() {
72 throw new UnsupportedOperationException("Utility class should not be instantiated");
75 private static class ElementWithSchemaContext {
77 SchemaContext schemaContext;
79 ElementWithSchemaContext(final Element element,final SchemaContext schemaContext) {
80 this.schemaContext = schemaContext;
81 this.element = element;
84 Element getElement() {
88 SchemaContext getSchemaContext() {
93 public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(SchemaContext.NAME, "operation");
94 private static final QName RPC_REPLY_QNAME = QName.create(SchemaContext.NAME, "rpc-reply");
95 private static final Logger LOG = LoggerFactory.getLogger(XmlDocumentUtils.class);
96 private static final XMLOutputFactory FACTORY = XMLOutputFactory.newFactory();
99 * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
100 * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
101 * be transformed to root in XML Document. Each element of Data DOM tree is compared against specified Data
102 * Node Container Schema and transformed accordingly.
104 * @param data Data DOM root element
105 * @param schemaNode Data Node Container Schema
106 * @param codecProvider XML Codec Provider
107 * @return new instance of XML Document
108 * @throws UnsupportedDataTypeException
110 * @deprecated Use {@link #toDocument(org.opendaylight.yangtools.yang.data.api.CompositeNode, com.google.common.base.Optional, org.opendaylight.yangtools.yang.model.api.DataNodeContainer, XmlCodecProvider)} instead.
111 * The whole schema context allows for proper serialization of leafrefs.
114 public static Document toDocument(final CompositeNode data, final DataNodeContainer schemaNode, final XmlCodecProvider codecProvider)
115 throws UnsupportedDataTypeException {
116 return toDocument(data, Optional.<SchemaContext>absent(), schemaNode, codecProvider);
120 * Serializes data DOM node into a w3c DOM document. Whole schema context is used to resolve leafref elements.
122 * @param data Data DOM root element
123 * @param schemaContext Entire schema context for correct leafref resolution
124 * @param schema Data Node Container Schema
125 * @param codecProvider XML Codec Provider
126 * @return serialized w3c DOM document
127 * @throws UnsupportedDataTypeException
129 public static Document toDocument(final CompositeNode data, final SchemaContext schemaContext, final DataNodeContainer schemaNode, final XmlCodecProvider codecProvider)
130 throws UnsupportedDataTypeException {
131 return toDocument(data, Optional.of(schemaContext), schemaNode, codecProvider);
134 private static Document toDocument(final CompositeNode data, final Optional<SchemaContext> schemaContext, final DataNodeContainer schema, final XmlCodecProvider codecProvider)
135 throws UnsupportedDataTypeException {
136 Preconditions.checkNotNull(data);
137 Preconditions.checkNotNull(schema);
139 if (!(schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode)) {
140 throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
143 final DOMResult result = new DOMResult(getDocument());
145 final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
146 XmlStreamUtils xmlStreamUtils = schemaContext.isPresent() ? XmlStreamUtils.create(codecProvider, schemaContext.get()) : XmlStreamUtils.create(codecProvider);
147 xmlStreamUtils.writeDocument(writer, data, (SchemaNode) schema);
149 return (Document)result.getNode();
150 } catch (XMLStreamException e) {
151 LOG.error("Failed to serialize data {}", data, e);
156 public static Document getDocument() {
157 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
160 DocumentBuilder bob = dbf.newDocumentBuilder();
161 doc = bob.newDocument();
162 } catch (ParserConfigurationException e) {
163 throw new RuntimeException(e);
169 * Converts Data DOM structure to XML Document for specified XML Codec Provider. The CompositeNode
170 * data parameter enters as root of Data DOM tree and will be transformed to root in XML Document. The child
171 * nodes of Data Tree are transformed accordingly.
173 * @param data Data DOM root element
174 * @param codecProvider XML Codec Provider
175 * @return new instance of XML Document
176 * @throws UnsupportedDataTypeException
178 public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider) {
179 final DOMResult result = new DOMResult(getDocument());
181 final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
182 XmlStreamUtils.create(codecProvider).writeDocument(writer, data);
184 return (Document)result.getNode();
185 } catch (XMLStreamException e) {
186 LOG.error("Failed to serialize data {}", data, e);
191 private static final Element createElementFor(final Document doc, final QName qname, final Object obj) {
193 if (qname.getNamespace() != null) {
194 ret = doc.createElementNS(qname.getNamespace().toString(), qname.getLocalName());
196 ret = doc.createElementNS(null, qname.getLocalName());
199 if (obj instanceof AttributesContainer) {
200 final Map<QName, String> attrs = ((AttributesContainer)obj).getAttributes();
203 for (Entry<QName, String> attribute : attrs.entrySet()) {
204 ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
205 attribute.getValue());
213 public static Element createElementFor(final Document doc, final Node<?> data) {
214 return createElementFor(doc, data.getNodeType(), data);
217 public static Element createElementFor(final Document doc, final NormalizedNode<?, ?> data) {
218 return createElementFor(doc, data.getNodeType(), data);
221 public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
222 final Optional<XmlCodecProvider> codecProvider) {
223 return toDomNode(xmlElement, schema, codecProvider, Optional.<SchemaContext>absent());
226 public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
227 final Optional<XmlCodecProvider> codecProvider, final Optional<SchemaContext> schemaContext) {
228 if (schema.isPresent()) {
229 if(schemaContext.isPresent()) {
230 return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(XmlUtils.DEFAULT_XML_CODEC_PROVIDER), schemaContext.get());
232 return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(XmlUtils.DEFAULT_XML_CODEC_PROVIDER));
235 return toDomNode(xmlElement);
238 public static QName qNameFromElement(final Element xmlElement) {
239 String namespace = xmlElement.getNamespaceURI();
240 String localName = xmlElement.getLocalName();
241 return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
244 private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
245 checkQName(xmlElement, schema.getQName());
246 if (schema instanceof DataNodeContainer) {
247 return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider,schemaCtx);
248 } else if (schema instanceof LeafSchemaNode) {
249 return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider,schemaCtx);
250 } else if (schema instanceof LeafListSchemaNode) {
251 return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider,schemaCtx);
252 } else if (schema instanceof AnyXmlSchemaNode) {
253 return toDomNode(xmlElement);
258 private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
259 return toNodeWithSchema(xmlElement, schema, codecProvider, null);
262 protected static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
263 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
264 final Object value = resolveValueFromSchemaType(xmlElement, schema, schema.getType(), codecProvider, schemaCtx);
265 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
266 return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
269 private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
270 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
271 final Object value = resolveValueFromSchemaType(xmlElement, schema, schema.getType(), codecProvider, schemaCtx);
272 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
273 return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
276 private static Object resolveValueFromSchemaType(final Element xmlElement, final DataSchemaNode schema, final TypeDefinition<?> type,
277 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
279 TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
280 if (baseType instanceof LeafrefTypeDefinition) {
281 final LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) baseType;
282 baseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypeDefinition, schemaCtx, schema);
285 final String text = xmlElement.getTextContent();
288 if (baseType instanceof InstanceIdentifierType) {
289 value = InstanceIdentifierForXmlCodec.deserialize(xmlElement, schemaCtx);
290 } else if (baseType instanceof IdentityrefTypeDefinition) {
291 value = InstanceIdentifierForXmlCodec.toIdentity(text, xmlElement, schemaCtx);
293 final TypeDefinitionAwareCodec<?, ?> codec = codecProvider.codecFor(baseType);
295 LOG.info("No codec for schema {}, falling back to text", schema);
298 value = codec.deserialize(text);
304 private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
305 final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
306 List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
307 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
308 return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
311 public static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
312 Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
313 if(attributeNodeNS == null) {
314 return Optional.absent();
317 ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
318 Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
320 return Optional.of(action);
323 private static void checkQName(final Element xmlElement, final QName qName) {
324 checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()), "Not equal: %s to: %s for: %s and: %s", qName.getNamespace(), xmlElement.getNamespaceURI(), qName, xmlElement);
325 checkState(qName.getLocalName().equals(xmlElement.getLocalName()), "Not equal: %s to: %s for: %s and: %s", qName.getLocalName(), xmlElement.getLocalName(), qName, xmlElement);
328 public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
329 if (dataSchemaNode != null && qname != null) {
330 for (DataSchemaNode dsn : dataSchemaNode) {
331 if (qname.isEqualWithoutRevision(dsn.getQName())) {
332 return Optional.<DataSchemaNode> of(dsn);
333 } else if (dsn instanceof ChoiceNode) {
334 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
335 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
336 if (foundDsn != null && foundDsn.isPresent()) {
343 return Optional.absent();
346 public static Node<?> toDomNode(final Document doc) {
347 return toDomNode(doc.getDocumentElement());
350 private static Node<?> toDomNode(final Element element) {
351 QName qname = qNameFromElement(element);
353 ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
354 NodeList nodes = element.getChildNodes();
355 boolean isSimpleObject = true;
357 for (int i = 0; i < nodes.getLength(); i++) {
358 org.w3c.dom.Node child = nodes.item(i);
359 if (child instanceof Element) {
360 isSimpleObject = false;
361 values.add(toDomNode((Element) child));
363 if (isSimpleObject && child instanceof org.w3c.dom.Text) {
364 value = element.getTextContent();
365 if (!Strings.isNullOrEmpty(value)) {
366 isSimpleObject = true;
370 if (isSimpleObject) {
371 return new SimpleNodeTOImpl<>(qname, null, value);
373 return ImmutableCompositeNode.create(qname, values.build());
376 public static List<Node<?>> toDomNodes(final Element element, final Optional<? extends Iterable<DataSchemaNode>> context, final SchemaContext schemaCtx) {
377 return forEachChild(element.getChildNodes(), schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
380 public Optional<Node<?>> apply(final @Nonnull ElementWithSchemaContext input) {
381 if (context.isPresent()) {
382 Preconditions.checkNotNull(input);
383 QName partialQName = qNameFromElement(input.getElement());
384 Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
385 if (schemaNode.isPresent()) {
386 return Optional.<Node<?>> fromNullable(
387 toNodeWithSchema(input.getElement(), schemaNode.get(), XmlUtils.DEFAULT_XML_CODEC_PROVIDER, input.getSchemaContext()));
390 return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
396 public static List<Node<?>> toDomNodes(final Element element, final Optional<? extends Iterable<DataSchemaNode>> context) {
397 return toDomNodes(element, context, null);
401 * Converts XML Document containing notification data from Netconf device to
402 * Data DOM Nodes. <br>
403 * By specification defined in <a
404 * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
405 * there are xml elements containing notifications metadata, like eventTime
406 * or root notification element which specifies namespace for which is
407 * notification defined in yang model. Those elements MUST be stripped off
408 * notifications body. This method returns pure notification body which
409 * begins in element which is equal to notifications name defined in
410 * corresponding yang model. Rest of notification metadata are obfuscated,
411 * thus Data DOM contains only pure notification body.
414 * XML Document containing notification body
415 * @param notifications
416 * Notifications Definition Schema
417 * @return Data DOM Nodes containing xml notification body definition or
418 * <code>null</code> if there is no NotificationDefinition with
419 * Element with equal notification QName defined in XML Document.
421 public static CompositeNode notificationToDomNodes(final Document document,
422 final Optional<Set<NotificationDefinition>> notifications, final SchemaContext schemaCtx) {
423 if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
424 final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
425 for (int i = 0; i < originChildNodes.getLength(); i++) {
426 org.w3c.dom.Node child = originChildNodes.item(i);
427 if (child instanceof Element) {
428 final Element childElement = (Element) child;
429 final QName partialQName = qNameFromElement(childElement);
430 final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
431 notifications.get());
432 if (notificationDef.isPresent()) {
433 final Iterable<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
434 final List<Node<?>> domNodes = toDomNodes(childElement,
435 Optional.<Iterable<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
436 return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
445 * Transforms XML Document representing Rpc output body into Composite Node structure based on Rpc definition
446 * within given Schema Context. The transformation is based on Rpc Definition which is defined in provided Schema Context.
447 * If Rpc Definition is missing from given Schema Context the method will return <code>null</code>
449 * @param document XML Document containing Output RPC message
450 * @param rpcName Rpc QName
451 * @param context Schema Context
452 * @return Rpc message in Composite Node data structures if Rpc definition is present within provided Schema Context, otherwise
453 * returns <code>null</code>
455 public static CompositeNode rpcReplyToDomNodes(final Document document, final QName rpcName,
456 final SchemaContext context) {
457 Preconditions.checkNotNull(document);
458 Preconditions.checkNotNull(rpcName);
459 Preconditions.checkNotNull(context);
461 Optional<RpcDefinition> rpcDefinition = findRpc(rpcName, context);
462 if (rpcDefinition.isPresent()) {
463 RpcDefinition rpc = rpcDefinition.get();
465 final Collection<DataSchemaNode> outputNode = rpc.getOutput() != null ? rpc.getOutput().getChildNodes() : null;
466 final Element rpcReplyElement = document.getDocumentElement();
467 final QName partialQName = qNameFromElement(rpcReplyElement);
469 if (RPC_REPLY_QNAME.equals(partialQName)) {
470 final List<Node<?>> domNodes = toDomNodes(rpcReplyElement, Optional.fromNullable(outputNode), context);
471 QName qName = rpc.getOutput() != null ? rpc.getOutput().getQName() : QName.cachedReference(QName.create(rpcName, "output"));
472 List<Node<?>> rpcOutNodes = Collections.<Node<?>>singletonList(ImmutableCompositeNode.create(
474 return ImmutableCompositeNode.create(rpcName, rpcOutNodes);
481 * Method searches given schema context for Rpc Definition with given QName.
482 * Returns Rpc Definition if is present within given Schema Context, otherwise returns Optional.absent().
484 * @param rpc Rpc QName
485 * @param context Schema Context
486 * @return Rpc Definition if is present within given Schema Context, otherwise returns Optional.absent().
488 private static Optional<RpcDefinition> findRpc(final QName rpc, final SchemaContext context) {
489 Preconditions.checkNotNull(rpc);
490 Preconditions.checkNotNull(context);
491 for (final RpcDefinition rpcDefinition : context.getOperations()) {
492 if ((rpcDefinition != null) && rpc.equals(rpcDefinition.getQName())) {
493 return Optional.of(rpcDefinition);
496 return Optional.absent();
499 public static CompositeNode notificationToDomNodes(final Document document,
500 final Optional<Set<NotificationDefinition>> notifications) {
501 return notificationToDomNodes(document, notifications,null);
504 private static Optional<NotificationDefinition> findNotification(final QName notifName,
505 final Set<NotificationDefinition> notifications) {
506 if ((notifName != null) && (notifications != null)) {
507 for (final NotificationDefinition notification : notifications) {
508 if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
509 return Optional.<NotificationDefinition>fromNullable(notification);
513 return Optional.<NotificationDefinition>absent();
516 private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
517 final int l = nodes.getLength();
519 return ImmutableList.of();
522 final List<T> list = new ArrayList<>(l);
523 for (int i = 0; i < l; i++) {
524 org.w3c.dom.Node child = nodes.item(i);
525 if (child instanceof Element) {
526 Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
527 if (result.isPresent()) {
528 list.add(result.get());
532 return ImmutableList.copyOf(list);
535 public static final XmlCodecProvider defaultValueCodecProvider() {
536 return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;