Decouple config and netconf subsystems.
[controller.git] / opendaylight / config / config-util / src / main / java / org / opendaylight / controller / config / util / xml / XmlElement.java
similarity index 79%
rename from opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlElement.java
rename to opendaylight/config/config-util/src/main/java/org/opendaylight/controller/config/util/xml/XmlElement.java
index 3d46bf6ab43f8cb353fa1a33009581a16af32f51..795c6b62dbd04d252220764feb8d32a24789eaad 100644 (file)
@@ -1,12 +1,12 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.controller.netconf.util.xml;
+package org.opendaylight.controller.config.util.xml;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
@@ -20,10 +20,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Attr;
@@ -54,27 +50,27 @@ public final class XmlElement {
         return new XmlElement(xml.getDocumentElement());
     }
 
-    public static XmlElement fromString(String s) throws NetconfDocumentedException {
+    public static XmlElement fromString(String s) throws DocumentedException {
         try {
             return new XmlElement(XmlUtil.readXmlToElement(s));
         } catch (IOException | SAXException e) {
-            throw NetconfDocumentedException.wrap(e);
+            throw DocumentedException.wrap(e);
         }
     }
 
-    public static XmlElement fromDomElementWithExpected(Element element, String expectedName) throws NetconfDocumentedException {
+    public static XmlElement fromDomElementWithExpected(Element element, String expectedName) throws DocumentedException {
         XmlElement xmlElement = XmlElement.fromDomElement(element);
         xmlElement.checkName(expectedName);
         return xmlElement;
     }
 
-    public static XmlElement fromDomElementWithExpected(Element element, String expectedName, String expectedNamespace) throws NetconfDocumentedException {
+    public static XmlElement fromDomElementWithExpected(Element element, String expectedName, String expectedNamespace) throws DocumentedException {
         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(element, expectedName);
         xmlElement.checkNamespace(expectedNamespace);
         return xmlElement;
     }
 
-    private Map<String, String> extractNamespaces() throws NetconfDocumentedException {
+    private Map<String, String> extractNamespaces() throws DocumentedException {
         Map<String, String> namespaces = new HashMap<>();
         NamedNodeMap attributes = element.getAttributes();
         for (int i = 0; i < attributes.getLength(); i++) {
@@ -86,10 +82,10 @@ public final class XmlElement {
                     prefix = DEFAULT_NAMESPACE_PREFIX;
                 } else {
                     if (!attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY + ":")){
-                        throw new NetconfDocumentedException("Attribute doesn't start with :",
-                                NetconfDocumentedException.ErrorType.application,
-                                NetconfDocumentedException.ErrorTag.invalid_value,
-                                NetconfDocumentedException.ErrorSeverity.error);
+                        throw new DocumentedException("Attribute doesn't start with :",
+                                DocumentedException.ErrorType.application,
+                                DocumentedException.ErrorTag.invalid_value,
+                                DocumentedException.ErrorSeverity.error);
                     }
                     prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
                 }
@@ -112,9 +108,9 @@ public final class XmlElement {
         if (!getName().equals(expectedName)){
             throw new UnexpectedElementException(String.format("Expected %s xml element but was %s", expectedName,
                     getName()),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.operation_failed,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.operation_failed,
+                    DocumentedException.ErrorSeverity.error);
         }
     }
 
@@ -124,9 +120,9 @@ public final class XmlElement {
             throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
                     getNamespaceAttribute(),
                     expectedNamespace),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.operation_failed,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.operation_failed,
+                    DocumentedException.ErrorSeverity.error);
         }
     }
 
@@ -136,9 +132,9 @@ public final class XmlElement {
             throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
                     getNamespace(),
                     expectedNamespace),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.operation_failed,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.operation_failed,
+                    DocumentedException.ErrorSeverity.error);
         }
     }
 
@@ -250,13 +246,13 @@ public final class XmlElement {
         });
     }
 
-    public XmlElement getOnlyChildElement(String childName) throws NetconfDocumentedException {
+    public XmlElement getOnlyChildElement(String childName) throws DocumentedException {
         List<XmlElement> nameElements = getChildElements(childName);
         if (nameElements.size() != 1){
-            throw new NetconfDocumentedException("One element " + childName + " expected in " + toString(),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.invalid_value,
-                    NetconfDocumentedException.ErrorSeverity.error);
+            throw new DocumentedException("One element " + childName + " expected in " + toString(),
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.invalid_value,
+                    DocumentedException.ErrorSeverity.error);
         }
         return nameElements.get(0);
     }
@@ -283,7 +279,7 @@ public final class XmlElement {
         return Optional.of(children.get(0));
     }
 
-    public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws  NetconfDocumentedException {
+    public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws  DocumentedException {
         return getOnlyChildElement(childName, getNamespace());
     }
 
@@ -305,7 +301,7 @@ public final class XmlElement {
         return Optional.absent();
     }
 
-    public XmlElement getOnlyChildElementWithSameNamespace() throws NetconfDocumentedException {
+    public XmlElement getOnlyChildElementWithSameNamespace() throws DocumentedException {
         XmlElement childElement = getOnlyChildElement();
         childElement.checkNamespace(getNamespace());
         return childElement;
@@ -322,7 +318,7 @@ public final class XmlElement {
         return Optional.absent();
     }
 
-    public XmlElement getOnlyChildElement(final String childName, String namespace) throws NetconfDocumentedException {
+    public XmlElement getOnlyChildElement(final String childName, String namespace) throws DocumentedException {
         List<XmlElement> children = getChildElementsWithinNamespace(namespace);
         children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
             @Override
@@ -331,24 +327,24 @@ public final class XmlElement {
             }
         }));
         if (children.size() != 1){
-            throw new NetconfDocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
+            throw new DocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
                     childName, toString(), children.size()),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.invalid_value,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.invalid_value,
+                    DocumentedException.ErrorSeverity.error);
         }
 
         return children.get(0);
     }
 
-    public XmlElement getOnlyChildElement() throws NetconfDocumentedException {
+    public XmlElement getOnlyChildElement() throws DocumentedException {
         List<XmlElement> children = getChildElements();
         if (children.size() != 1){
-            throw new NetconfDocumentedException(String.format( "One element expected in %s but was %s", toString(),
+            throw new DocumentedException(String.format( "One element expected in %s but was %s", toString(),
                     children.size()),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.invalid_value,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.invalid_value,
+                    DocumentedException.ErrorSeverity.error);
         }
         return children.get(0);
     }
@@ -361,7 +357,7 @@ public final class XmlElement {
         return Optional.of(children.get(0));
     }
 
-    public String getTextContent() throws NetconfDocumentedException {
+    public String getTextContent() throws DocumentedException {
         NodeList childNodes = element.getChildNodes();
         if (childNodes.getLength() == 0) {
             return DEFAULT_NAMESPACE_PREFIX;
@@ -373,10 +369,10 @@ public final class XmlElement {
                 return content.trim();
             }
         }
-        throw new NetconfDocumentedException(getName() + " should contain text.",
-                NetconfDocumentedException.ErrorType.application,
-                NetconfDocumentedException.ErrorTag.invalid_value,
-                NetconfDocumentedException.ErrorSeverity.error
+        throw new DocumentedException(getName() + " should contain text.",
+                DocumentedException.ErrorType.application,
+                DocumentedException.ErrorTag.invalid_value,
+                DocumentedException.ErrorSeverity.error
         );
     }
 
@@ -396,9 +392,9 @@ public final class XmlElement {
         if (attribute == null || attribute.equals(DEFAULT_NAMESPACE_PREFIX)){
             throw new MissingNameSpaceException(String.format("Element %s must specify namespace",
                     toString()),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.operation_failed,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.operation_failed,
+                    DocumentedException.ErrorSeverity.error);
         }
         return attribute;
     }
@@ -424,9 +420,9 @@ public final class XmlElement {
         Optional<String> namespaceURI = getNamespaceOptionally();
         if (!namespaceURI.isPresent()){
             throw new MissingNameSpaceException(String.format("No namespace defined for %s", this),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.operation_failed,
-                    NetconfDocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.operation_failed,
+                    DocumentedException.ErrorSeverity.error);
         }
         return namespaceURI.get();
     }
@@ -459,7 +455,7 @@ public final class XmlElement {
      * namespace is returned with empty string as key. If no default namespace
      * is found value will be null.
      */
-    public Map.Entry<String/* prefix */, String/* namespace */> findNamespaceOfTextContent() throws NetconfDocumentedException {
+    public Map.Entry<String/* prefix */, String/* namespace */> findNamespaceOfTextContent() throws DocumentedException {
         Map<String, String> namespaces = extractNamespaces();
         String textContent = getTextContent();
         int indexOfColon = textContent.indexOf(':');
@@ -487,21 +483,21 @@ public final class XmlElement {
     }
 
     public void checkUnrecognisedElements(List<XmlElement> recognisedElements,
-            XmlElement... additionalRecognisedElements) throws NetconfDocumentedException {
+                                          XmlElement... additionalRecognisedElements) throws DocumentedException {
         List<XmlElement> childElements = getChildElements();
         childElements.removeAll(recognisedElements);
         for (XmlElement additionalRecognisedElement : additionalRecognisedElements) {
             childElements.remove(additionalRecognisedElement);
         }
         if (!childElements.isEmpty()){
-            throw new NetconfDocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
-                    NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.invalid_value,
-                    NetconfDocumentedException.ErrorSeverity.error);
+            throw new DocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.invalid_value,
+                    DocumentedException.ErrorSeverity.error);
         }
     }
 
-    public void checkUnrecognisedElements(XmlElement... additionalRecognisedElements) throws NetconfDocumentedException {
+    public void checkUnrecognisedElements(XmlElement... additionalRecognisedElements) throws DocumentedException {
         checkUnrecognisedElements(Collections.<XmlElement>emptyList(), additionalRecognisedElements);
     }