Merge topic 'stable/helium'
authorTony Tkacik <ttkacik@cisco.com>
Mon, 15 Dec 2014 07:57:53 +0000 (07:57 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 15 Dec 2014 07:57:54 +0000 (07:57 +0000)
* changes:
  Shorten YangInstanceIdentifier references
  BUG-2510: handle RPC route removal

opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/dom/impl/DomBrokerImplModule.java
opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/InMemoryDOMDataStore.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestconfDocumentedExceptionMapper.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfMessageToEXIEncoder.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfMessageToXMLEncoder.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ThreadLocalTransformers.java [new file with mode: 0644]
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XMLNetconfUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java

index f1f16cd..d299afa 100644 (file)
@@ -84,6 +84,7 @@ public final class DomBrokerImplModule extends org.opendaylight.controller.confi
         return new BrokerImpl(router, services);
     }
 
+    @Deprecated
     private DataProviderService createLegacyDataService(final DataStore legacyStore, final SchemaService schemaService) {
         YangInstanceIdentifier rootPath = YangInstanceIdentifier.builder().toInstance();
         DataBrokerImpl dataService = new DataBrokerImpl();
index 85134f1..deddd69 100644 (file)
@@ -244,7 +244,7 @@ public class InMemoryDOMDataStore extends TransactionReadyPrototype implements D
 
                 // For debugging purposes, allow dumping of the modification. Coupled with the above
                 // precondition log, it should allow us to understand what went on.
-                LOG.trace("Store Tx: {} modifications: {}", modification);
+                LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, dataTree);
 
                 return Futures.immediateFailedFuture(new TransactionCommitFailedException("Data did not pass validation.", e));
             } catch (Exception e) {
index 10201ab..d564d4c 100644 (file)
@@ -16,7 +16,6 @@ import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ER
 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_TAG_QNAME;
 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.ERROR_TYPE_QNAME;
 import static org.opendaylight.controller.sal.rest.api.Draft02.RestConfModule.NAMESPACE;
-
 import com.google.common.base.Charsets;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
@@ -69,6 +68,7 @@ import org.xml.sax.InputSource;
 public class RestconfDocumentedExceptionMapper implements ExceptionMapper<RestconfDocumentedException> {
 
     private final static Logger LOG = LoggerFactory.getLogger(RestconfDocumentedExceptionMapper.class);
+    private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
 
     @Context
     private HttpHeaders headers;
@@ -178,8 +178,7 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
 
     private Transformer createTransformer() throws TransformerFactoryConfigurationError,
             TransformerConfigurationException {
-        TransformerFactory tf = TransformerFactory.newInstance();
-        Transformer transformer = tf.newTransformer();
+        Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
index 55dcd9d..f1e72ed 100644 (file)
@@ -14,11 +14,9 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 import java.io.IOException;
 import java.io.OutputStream;
-import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.sax.SAXTransformerFactory;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.openexi.proc.common.EXIOptionsException;
 import org.openexi.sax.Transmogrifier;
@@ -26,10 +24,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<NetconfMessage> {
-
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToEXIEncoder.class);
-
-    private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
     private final NetconfEXICodec codec;
 
     public NetconfMessageToEXIEncoder(final NetconfEXICodec codec) {
@@ -44,8 +39,7 @@ public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<Netco
             final Transmogrifier transmogrifier = codec.getTransmogrifier();
             transmogrifier.setOutputStream(os);
 
-            final Transformer transformer = saxTransformerFactory.newTransformer();
-            transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));
+            ThreadLocalTransformers.getDefaultTransformer().transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));
         }
     }
 }
index d109e2d..99251b0 100644 (file)
@@ -17,10 +17,7 @@ import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
@@ -30,7 +27,6 @@ import org.w3c.dom.Comment;
 
 public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
-    private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
 
     private final Optional<String> clientId;
 
@@ -38,13 +34,13 @@ public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMess
         this(Optional.<String>absent());
     }
 
-    public NetconfMessageToXMLEncoder(Optional<String> clientId) {
+    public NetconfMessageToXMLEncoder(final Optional<String> clientId) {
         this.clientId = clientId;
     }
 
     @Override
     @VisibleForTesting
-    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
+    public void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws IOException, TransformerException {
         LOG.trace("Sent to encode : {}", msg);
 
         if (clientId.isPresent()) {
@@ -53,14 +49,10 @@ public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMess
         }
 
         try (OutputStream os = new ByteBufOutputStream(out)) {
-            Transformer transformer = FACTORY.newTransformer();
-            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-
             // Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter
             StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(os)));
             DOMSource source = new DOMSource(msg.getDocument());
-            transformer.transform(source, result);
+            ThreadLocalTransformers.getPrettyTransformer().transform(source, result);
         }
     }
 }
diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ThreadLocalTransformers.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ThreadLocalTransformers.java
new file mode 100644 (file)
index 0000000..a2039d6
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2014 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.nettyutil.handler;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+
+/**
+ * Utility class for cached thread-local transformers. This class exists mostly for use by handlers.
+ */
+final class ThreadLocalTransformers {
+    private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
+
+    private static final ThreadLocal<Transformer> DEFAULT_TRANSFORMER = new ThreadLocal<Transformer>() {
+        @Override
+        protected Transformer initialValue() {
+            try {
+                return FACTORY.newTransformer();
+            } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
+                throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
+            }
+        };
+
+        @Override
+        public void set(final Transformer value) {
+            throw new UnsupportedOperationException();
+        };
+    };
+
+    private static final ThreadLocal<Transformer> PRETTY_TRANSFORMER = new ThreadLocal<Transformer>() {
+        @Override
+        protected Transformer initialValue() {
+            final Transformer ret;
+
+            try {
+                ret = FACTORY.newTransformer();
+            } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
+                throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
+            }
+
+            ret.setOutputProperty(OutputKeys.INDENT, "yes");
+            ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+            return ret;
+        };
+
+        @Override
+        public void set(final Transformer value) {
+            throw new UnsupportedOperationException();
+        };
+    };
+
+    private ThreadLocalTransformers() {
+        throw new UnsupportedOperationException("Utility class");
+    }
+
+    /**
+     * Get the transformer with default configuration.
+     *
+     * @return A transformer with default configuration based on the default implementation.
+     */
+    public static Transformer getDefaultTransformer() {
+        return DEFAULT_TRANSFORMER.get();
+    }
+
+    /**
+     * Get the transformer with default configuration, but with automatic indentation
+     * and the XML declaration removed.
+     *
+     * @return A transformer with human-friendly configuration.
+     */
+    public static Transformer getPrettyTransformer() {
+        return PRETTY_TRANSFORMER.get();
+    }
+}
index eaaf320..6569dba 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.controller.netconf.util.xml;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
@@ -15,14 +16,17 @@ import javax.xml.xpath.XPathFactory;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 
 public final class XMLNetconfUtil {
+    private static final XPathFactory FACTORY = XPathFactory.newInstance();
+    private static final NamespaceContext NS_CONTEXT = new HardcodedNamespaceResolver("netconf",
+        XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
 
-    private XMLNetconfUtil() {}
+    private XMLNetconfUtil() {
+        throw new UnsupportedOperationException("Utility class");
+    }
 
-    public static XPathExpression compileXPath(String xPath) {
-        final XPathFactory xPathfactory = XPathFactory.newInstance();
-        final XPath xpath = xPathfactory.newXPath();
-        xpath.setNamespaceContext(new HardcodedNamespaceResolver("netconf",
-                XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
+    public static XPathExpression compileXPath(final String xPath) {
+        final XPath xpath = FACTORY.newXPath();
+        xpath.setNamespaceContext(NS_CONTEXT);
         try {
             return xpath.compile(xPath);
         } catch (final XPathExpressionException e) {
index 68c4d9f..ee5b27b 100644 (file)
@@ -43,7 +43,9 @@ public final class XmlUtil {
 
     public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
     public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
-    private static final DocumentBuilderFactory BUILDERFACTORY;
+    private static final DocumentBuilderFactory BUILDER_FACTORY;
+    private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
+    private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
     static {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -51,32 +53,34 @@ public final class XmlUtil {
         factory.setCoalescing(true);
         factory.setIgnoringElementContentWhitespace(true);
         factory.setIgnoringComments(true);
-        BUILDERFACTORY = factory;
+        BUILDER_FACTORY = factory;
     }
 
-    private XmlUtil() {}
+    private XmlUtil() {
+        throw new UnsupportedOperationException("Utility class");
+    }
 
-    public static Element readXmlToElement(String xmlContent) throws SAXException, IOException {
+    public static Element readXmlToElement(final String xmlContent) throws SAXException, IOException {
         Document doc = readXmlToDocument(xmlContent);
         return doc.getDocumentElement();
     }
 
-    public static Element readXmlToElement(InputStream xmlContent) throws SAXException, IOException {
+    public static Element readXmlToElement(final InputStream xmlContent) throws SAXException, IOException {
         Document doc = readXmlToDocument(xmlContent);
         return doc.getDocumentElement();
     }
 
-    public static Document readXmlToDocument(String xmlContent) throws SAXException, IOException {
+    public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
     }
 
     // TODO improve exceptions throwing
     // along with XmlElement
 
-    public static Document readXmlToDocument(InputStream xmlContent) throws SAXException, IOException {
+    public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
         DocumentBuilder dBuilder;
         try {
-            dBuilder = BUILDERFACTORY.newDocumentBuilder();
+            dBuilder = BUILDER_FACTORY.newDocumentBuilder();
         } catch (ParserConfigurationException e) {
             throw new IllegalStateException("Failed to parse XML document", e);
         }
@@ -86,20 +90,20 @@ public final class XmlUtil {
         return doc;
     }
 
-    public static Element readXmlToElement(File xmlFile) throws SAXException, IOException {
+    public static Element readXmlToElement(final File xmlFile) throws SAXException, IOException {
         return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement();
     }
 
     public static Document newDocument() {
         try {
-            DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder();
+            DocumentBuilder builder = BUILDER_FACTORY.newDocumentBuilder();
             return builder.newDocument();
         } catch (ParserConfigurationException e) {
             throw new IllegalStateException("Failed to create document", e);
         }
     }
 
-    public static Element createElement(final Document document, String qName, Optional<String> namespaceURI) {
+    public static Element createElement(final Document document, final String qName, final Optional<String> namespaceURI) {
         if(namespaceURI.isPresent()) {
             final Element element = document.createElementNS(namespaceURI.get(), qName);
             String name = XMLNS_ATTRIBUTE_KEY;
@@ -112,20 +116,20 @@ public final class XmlUtil {
         return document.createElement(qName);
     }
 
-    public static Element createTextElement(Document document, String qName, String content, Optional<String> namespaceURI) {
+    public static Element createTextElement(final Document document, final String qName, final String content, final Optional<String> namespaceURI) {
         Element typeElement = createElement(document, qName, namespaceURI);
         typeElement.appendChild(document.createTextNode(content));
         return typeElement;
     }
 
-    public static Element createTextElementWithNamespacedContent(Document document, String qName, String prefix,
-                                                                 String namespace, String contentWithoutPrefix) {
+    public static Element createTextElementWithNamespacedContent(final Document document, final String qName, final String prefix,
+                                                                 final String namespace, final String contentWithoutPrefix) {
 
         return createTextElementWithNamespacedContent(document, qName, prefix, namespace, contentWithoutPrefix, Optional.<String>absent());
     }
 
-    public static Element createTextElementWithNamespacedContent(Document document, String qName, String prefix,
-                                                                 String namespace, String contentWithoutPrefix, Optional<String> namespaceURI) {
+    public static Element createTextElementWithNamespacedContent(final Document document, final String qName, final String prefix,
+                                                                 final String namespace, final String contentWithoutPrefix, final Optional<String> namespaceURI) {
 
         String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
         Element element = createTextElement(document, qName, content, namespaceURI);
@@ -134,25 +138,25 @@ public final class XmlUtil {
         return element;
     }
 
-    public static String createPrefixedValue(String prefix, String value) {
+    public static String createPrefixedValue(final String prefix, final String value) {
         return prefix + ":" + value;
     }
 
-    public static String toString(Document document) {
+    public static String toString(final Document document) {
         return toString(document.getDocumentElement());
     }
 
-    public static String toString(Element xml) {
+    public static String toString(final Element xml) {
         return toString(xml, false);
     }
 
-    public static String toString(XmlElement xmlElement) {
+    public static String toString(final XmlElement xmlElement) {
         return toString(xmlElement.getDomElement(), false);
     }
 
-    public static String toString(Element xml, boolean addXmlDeclaration) {
+    public static String toString(final Element xml, final boolean addXmlDeclaration) {
         try {
-            Transformer transformer = TransformerFactory.newInstance().newTransformer();
+            Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes");
 
@@ -166,26 +170,25 @@ public final class XmlUtil {
         }
     }
 
-    public static String toString(Document doc, boolean addXmlDeclaration) {
+    public static String toString(final Document doc, final boolean addXmlDeclaration) {
         return toString(doc.getDocumentElement(), addXmlDeclaration);
     }
 
-    public static Schema loadSchema(InputStream... fromStreams) {
+    public static Schema loadSchema(final InputStream... fromStreams) {
         Source[] sources = new Source[fromStreams.length];
         int i = 0;
         for (InputStream stream : fromStreams) {
             sources[i++] = new StreamSource(stream);
         }
 
-        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         try {
-            return schemaFactory.newSchema(sources);
+            return SCHEMA_FACTORY.newSchema(sources);
         } catch (SAXException e) {
             throw new IllegalStateException("Failed to instantiate XML schema", e);
         }
     }
 
-    public static Object evaluateXPath(XPathExpression expr, Object rootNode, QName returnType) {
+    public static Object evaluateXPath(final XPathExpression expr, final Object rootNode, final QName returnType) {
         try {
             return expr.evaluate(rootNode, returnType);
         } catch (XPathExpressionException e) {
@@ -193,7 +196,7 @@ public final class XmlUtil {
         }
     }
 
-    public static Document createDocumentCopy(Document original) {
+    public static Document createDocumentCopy(final Document original) {
         final Document copiedDocument = newDocument();
         final Node copiedRoot = copiedDocument.importNode(original.getDocumentElement(), true);
         copiedDocument.appendChild(copiedRoot);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.