Merge "Fix modules Restconf call for mounted devices"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / xml / codec / XmlDocumentUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.xml.codec;
9
10 import com.google.common.base.Optional;
11 import java.net.URI;
12 import java.util.Collection;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 public class XmlDocumentUtils {
25   public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
26
27   public static Document getDocument() {
28     final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
29     Document doc = null;
30     try {
31       final DocumentBuilder bob = dbf.newDocumentBuilder();
32       doc = bob.newDocument();
33     } catch (final ParserConfigurationException e) {
34       throw new RuntimeException(e);
35     }
36     return doc;
37   }
38
39
40   public static QName qNameFromElement(final Element xmlElement) {
41     final String namespace = xmlElement.getNamespaceURI();
42     final String localName = xmlElement.getLocalName();
43     return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
44   }
45
46   public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Collection<DataSchemaNode> dataSchemaNode) {
47     if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
48       for (final DataSchemaNode dsn : dataSchemaNode) {
49         if (qname.isEqualWithoutRevision(dsn.getQName())) {
50           return Optional.<DataSchemaNode> of(dsn);
51         } else if (dsn instanceof ChoiceSchemaNode) {
52           for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
53             final Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
54             if (foundDsn != null && foundDsn.isPresent()) {
55               return foundDsn;
56             }
57           }
58         }
59       }
60     }
61     return Optional.absent();
62   }
63
64   public static final XmlCodecProvider defaultValueCodecProvider() {
65     return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
66   }
67 }