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;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.Random;
21 import javax.activation.UnsupportedDataTypeException;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
26 import com.google.common.collect.Maps;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.*;
29 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
30 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.Node;
33 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
34 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
35 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
36 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 import com.google.common.base.Function;
55 import com.google.common.base.Objects;
56 import com.google.common.base.Optional;
57 import com.google.common.base.Preconditions;
58 import com.google.common.base.Strings;
59 import com.google.common.collect.ImmutableList;
61 public class XmlDocumentUtils {
63 private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
66 public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
67 return TypeDefinitionAwareCodec.from(baseType);
71 private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
74 * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
75 * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
76 * be transformed to root in XML Document. Each element of Data DOM tree is compared against specified Data
77 * Node Container Schema and transformed accordingly.
79 * @param data Data DOM root element
80 * @param schema Data Node Container Schema
81 * @param codecProvider XML Codec Provider
82 * @return new instance of XML Document
83 * @throws UnsupportedDataTypeException
85 public static Document toDocument(CompositeNode data, DataNodeContainer schema, XmlCodecProvider codecProvider)
86 throws UnsupportedDataTypeException {
87 Preconditions.checkNotNull(data);
88 Preconditions.checkNotNull(schema);
90 Document doc = getDocument();
92 if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
93 doc.appendChild(createXmlRootElement(doc, data, (SchemaNode) schema, codecProvider));
96 throw new UnsupportedDataTypeException(
97 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
101 public static Document getDocument() {
102 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
105 DocumentBuilder bob = dbf.newDocumentBuilder();
106 doc = bob.newDocument();
107 } catch (ParserConfigurationException e) {
108 throw new RuntimeException(e);
114 * Converts Data DOM structure to XML Document for specified XML Codec Provider. The CompositeNode
115 * data parameter enters as root of Data DOM tree and will be transformed to root in XML Document. The child
116 * nodes of Data Tree are transformed accordingly.
118 * @param data Data DOM root element
119 * @param codecProvider XML Codec Provider
120 * @return new instance of XML Document
121 * @throws UnsupportedDataTypeException
123 public static Document toDocument(CompositeNode data, XmlCodecProvider codecProvider)
124 throws UnsupportedDataTypeException {
125 Preconditions.checkNotNull(data);
127 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
128 dbf.setNamespaceAware(true);
131 DocumentBuilder bob = dbf.newDocumentBuilder();
132 doc = bob.newDocument();
133 } catch (ParserConfigurationException e) {
137 doc.appendChild(createXmlRootElement(doc, data, null, codecProvider));
141 private static Element createXmlRootElement(Document doc, Node<?> data, SchemaNode schema,
142 XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
143 Element itemEl = createElementFor(doc, data);
144 if (data instanceof SimpleNode<?>) {
145 if (schema instanceof LeafListSchemaNode) {
146 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
147 (DataSchemaNode) schema, codecProvider);
148 } else if (schema instanceof LeafSchemaNode) {
149 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
150 (DataSchemaNode) schema, codecProvider);
152 Object value = data.getValue();
154 itemEl.setTextContent(String.valueOf(value));
157 } else { // CompositeNode
158 for (Node<?> child : ((CompositeNode) data).getChildren()) {
159 DataSchemaNode childSchema = null;
160 if (schema != null) {
161 childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
162 if (logger.isDebugEnabled()) {
163 if (childSchema == null) {
164 logger.debug("Probably the data node \""
165 + ((child == null) ? "" : child.getNodeType().getLocalName())
166 + "\" is not conform to schema");
170 itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
176 public static Element createElementFor(Document doc, Node<?> data) {
177 QName dataType = data.getNodeType();
179 if (dataType.getNamespace() != null) {
180 ret = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
182 ret = doc.createElementNS(null, dataType.getLocalName());
184 if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
185 for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
186 ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
187 attribute.getValue());
194 public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
195 DataSchemaNode schema, XmlCodecProvider codecProvider) {
197 Object nodeValue = node.getValue();
199 writeValueByType(element, type, codecProvider, nodeValue);
202 public static void writeValueByType(Element element, TypeDefinition<?> type, XmlCodecProvider codecProvider, Object nodeValue) {
203 TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
204 if (baseType instanceof IdentityrefTypeDefinition) {
205 if (nodeValue instanceof QName) {
206 QName value = (QName) nodeValue;
208 if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
209 prefix = value.getPrefix();
211 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
212 element.setTextContent(prefix + ":" + value.getLocalName());
214 Object value = nodeValue;
215 logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(),
216 baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
218 element.setTextContent(String.valueOf(value));
221 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
222 if (nodeValue instanceof InstanceIdentifier) {
223 // Map< key = namespace, value = prefix>
224 Map<String, String> prefixes = new HashMap<>();
225 InstanceIdentifier instanceIdentifier = (InstanceIdentifier) nodeValue;
226 StringBuilder textContent = new StringBuilder();
227 for (PathArgument pathArgument : instanceIdentifier.getPath()) {
228 textContent.append("/");
229 writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
230 if (pathArgument instanceof NodeIdentifierWithPredicates) {
231 Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
233 for (QName keyValue : predicates.keySet()) {
234 String predicateValue = String.valueOf(predicates.get(keyValue));
235 textContent.append("[");
236 writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
237 textContent.append("='");
238 textContent.append(predicateValue);
239 textContent.append("'");
240 textContent.append("]");
242 } else if (pathArgument instanceof NodeWithValue) {
243 textContent.append("[.='");
244 textContent.append(((NodeWithValue)pathArgument).getValue());
245 textContent.append("'");
246 textContent.append("]");
249 element.setTextContent(textContent.toString());
252 Object value = nodeValue;
253 logger.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
255 baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
257 element.setTextContent(String.valueOf(value));
261 if (nodeValue != null) {
262 final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
265 final String text = codec.serialize(nodeValue);
266 element.setTextContent(text);
267 } catch (ClassCastException e) {
268 logger.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", nodeValue, baseType, e);
269 element.setTextContent(String.valueOf(nodeValue));
272 logger.error("Failed to find codec for {}, falling back to using stream", baseType);
273 element.setTextContent(String.valueOf(nodeValue));
279 private static void writeIdentifierWithNamespacePrefix(Element element, StringBuilder textContent, QName qName,
280 Map<String, String> prefixes) {
281 String namespace = qName.getNamespace().toString();
282 String prefix = prefixes.get(namespace);
283 if (prefix == null) {
284 prefix = qName.getPrefix();
285 if (prefix == null || prefix.isEmpty() || prefixes.containsValue(prefix)) {
286 prefix = generateNewPrefix(prefixes.values());
290 element.setAttribute("xmlns:" + prefix, namespace.toString());
291 textContent.append(prefix);
292 prefixes.put(namespace, prefix);
294 textContent.append(":");
295 textContent.append(qName.getLocalName());
298 private static String generateNewPrefix(Collection<String> prefixes) {
299 StringBuilder result = null;
300 Random random = new Random();
302 result = new StringBuilder();
303 for (int i = 0; i < 4; i++) {
304 int randomNumber = 0x61 + (Math.abs(random.nextInt()) % 26);
305 result.append(Character.toChars(randomNumber));
307 } while (prefixes.contains(result.toString()));
309 return result.toString();
312 public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
313 TypeDefinition<?> superType = type;
314 while (superType.getBaseType() != null) {
315 superType = superType.getBaseType();
320 private static final DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
321 if (dataSchemaNode != null && node != null) {
322 for (DataSchemaNode dsn : dataSchemaNode) {
323 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
325 } else if (dsn instanceof ChoiceNode) {
326 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
327 DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
328 if (foundDsn != null) {
338 public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
339 Optional<XmlCodecProvider> codecProvider) {
340 if (schema.isPresent()) {
341 return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
343 return toDomNode(xmlElement);
346 public static CompositeNode fromElement(Element xmlElement) {
347 CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
348 node.setQName(qNameFromElement(xmlElement));
350 return node.toInstance();
353 public static QName qNameFromElement(Element xmlElement) {
354 String namespace = xmlElement.getNamespaceURI();
355 String localName = xmlElement.getLocalName();
356 return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
359 private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
360 checkQName(xmlElement, schema.getQName());
361 if (schema instanceof DataNodeContainer) {
362 return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider);
363 } else if (schema instanceof LeafSchemaNode) {
364 return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider);
365 } else if (schema instanceof LeafListSchemaNode) {
366 return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider);
372 private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafSchemaNode schema,
373 XmlCodecProvider codecProvider) {
374 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
375 String text = xmlElement.getTextContent();
378 value = codec.deserialize(text);
381 value = xmlElement.getTextContent();
384 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
385 return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
388 private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafListSchemaNode schema,
389 XmlCodecProvider codecProvider) {
390 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
391 String text = xmlElement.getTextContent();
394 value = codec.deserialize(text);
397 value = xmlElement.getTextContent();
400 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
401 return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
404 private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
405 XmlCodecProvider codecProvider) {
406 List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
407 Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
408 return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
411 public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
413 public static Optional<ModifyAction> getModifyOperationFromAttributes(Element xmlElement) {
414 Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
415 if(attributeNodeNS == null)
416 return Optional.absent();
418 ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
419 Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
421 return Optional.of(action);
424 private static void checkQName(Element xmlElement, QName qName) {
425 checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
426 checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
429 public static final Optional<DataSchemaNode> findFirstSchema(QName qname, Set<DataSchemaNode> dataSchemaNode) {
430 if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
431 for (DataSchemaNode dsn : dataSchemaNode) {
432 if (qname.isEqualWithoutRevision(dsn.getQName())) {
433 return Optional.<DataSchemaNode> of(dsn);
434 } else if (dsn instanceof ChoiceNode) {
435 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
436 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
437 if (foundDsn != null && foundDsn.isPresent()) {
444 return Optional.absent();
447 public static Node<?> toDomNode(Document doc) {
448 return toDomNode(doc.getDocumentElement());
451 private static Node<?> toDomNode(Element element) {
452 QName qname = qNameFromElement(element);
454 ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
455 NodeList nodes = element.getChildNodes();
456 boolean isSimpleObject = true;
458 for (int i = 0; i < nodes.getLength(); i++) {
459 org.w3c.dom.Node child = nodes.item(i);
460 if (child instanceof Element) {
461 isSimpleObject = false;
462 values.add(toDomNode((Element) child));
464 if (isSimpleObject && child instanceof org.w3c.dom.Text) {
465 value = element.getTextContent();
466 if (!Strings.isNullOrEmpty(value)) {
467 isSimpleObject = true;
471 if (isSimpleObject) {
472 return new SimpleNodeTOImpl<>(qname, null, value);
474 return ImmutableCompositeNode.create(qname, values.build());
477 public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
478 return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
481 public Optional<Node<?>> apply(Element input) {
482 if (context.isPresent()) {
483 QName partialQName = qNameFromElement(input);
484 Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
485 if (schemaNode.isPresent()) {
486 return Optional.<Node<?>> fromNullable(//
487 toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
490 return Optional.<Node<?>> fromNullable(toDomNode(input));
498 * Converts XML Document containing notification data from Netconf device to
499 * Data DOM Nodes. <br>
500 * By specification defined in <a
501 * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
502 * there are xml elements containing notifications metadata, like eventTime
503 * or root notification element which specifies namespace for which is
504 * notification defined in yang model. Those elements MUST be stripped off
505 * notifications body. This method returns pure notification body which
506 * begins in element which is equal to notifications name defined in
507 * corresponding yang model. Rest of notification metadata are obfuscated,
508 * thus Data DOM contains only pure notification body.
511 * XML Document containing notification body
512 * @param notifications
513 * Notifications Definition Schema
514 * @return Data DOM Nodes containing xml notification body definition or
515 * <code>null</code> if there is no NotificationDefinition with
516 * Element with equal notification QName defined in XML Document.
518 public static CompositeNode notificationToDomNodes(final Document document,
519 final Optional<Set<NotificationDefinition>> notifications) {
520 if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
521 final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
523 for (int i = 0; i < originChildNodes.getLength(); i++) {
524 org.w3c.dom.Node child = originChildNodes.item(i);
525 if (child instanceof Element) {
526 final Element childElement = (Element) child;
527 final QName partialQName = qNameFromElement(childElement);
528 final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
529 notifications.get());
530 if (notificationDef.isPresent()) {
531 final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
532 final List<Node<?>> domNodes = toDomNodes(childElement,
533 Optional.<Set<DataSchemaNode>> fromNullable(dataNodes));
534 return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
542 private static Optional<NotificationDefinition> findNotification(final QName notifName,
543 final Set<NotificationDefinition> notifications) {
544 if ((notifName != null) && (notifications != null)) {
545 for (final NotificationDefinition notification : notifications) {
546 if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
547 return Optional.<NotificationDefinition>fromNullable(notification);
551 return Optional.<NotificationDefinition>absent();
554 private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
555 ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
556 for (int i = 0; i < nodes.getLength(); i++) {
557 org.w3c.dom.Node child = nodes.item(i);
558 if (child instanceof Element) {
559 Optional<T> result = forBody.apply((Element) child);
560 if (result.isPresent()) {
561 ret.add(result.get());
568 public static final XmlCodecProvider defaultValueCodecProvider() {
569 return DEFAULT_XML_VALUE_CODEC_PROVIDER;