2 * Copyright (c) 2014 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.controller.sal.connector.remoterpc;
10 import org.junit.Test;
11 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
12 import org.opendaylight.yangtools.yang.data.api.Node;
13 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
14 import org.opendaylight.yangtools.yang.data.impl.NodeUtils;
15 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.w3c.dom.Document;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.transform.OutputKeys;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamResult;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.io.StringWriter;
31 public class SerilizationTest {
33 private static final Logger _logger = LoggerFactory.getLogger(SerilizationTest.class);
35 public void fromXml() {
39 public void toXml() throws FileNotFoundException {
41 //InputStream xmlStream = SerilizationTest.class.getResourceAsStream("/FourSimpleChildren.xml");
42 InputStream xmlStream = SerilizationTest.class.getResourceAsStream("/AddFlow.xml");
43 StringWriter writer = new StringWriter();
45 CompositeNode data = loadCompositeNode(xmlStream);
46 Document domTree = NodeUtils.buildShadowDomTree(data);
48 TransformerFactory tf = TransformerFactory.newInstance();
49 Transformer transformer = tf.newTransformer();
50 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
51 //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
52 //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
53 //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
54 //transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
55 transformer.transform(new DOMSource(domTree), new StreamResult(writer));
56 } catch (TransformerException e) {
57 _logger.error("Error during translation of Document to OutputStream", e);
60 _logger.info("Parsed xml [{}]", writer.toString());
63 // Figure out how to include TestUtils through pom ...was getting errors
64 private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
65 if (xmlInputStream == null) {
66 throw new IllegalArgumentException();
70 dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
71 } catch (XMLStreamException e) {
72 _logger.error("Error during building data tree from XML", e);
75 if (dataTree == null) {
76 _logger.error("data tree is null");
79 if (dataTree instanceof SimpleNode) {
80 _logger.error("RPC XML was resolved as SimpleNode");
83 return (CompositeNode) dataTree;