package org.opendaylight.controller.sal.restconf.impl.test; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.xml.stream.XMLStreamException; 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.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.api.SimpleNode; import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; final class TestUtils { private static final Logger logger = LoggerFactory.getLogger(TestUtils.class); private final static YangModelParser parser = new YangParserImpl(); public static Set loadModules(String resourceDirectory) throws FileNotFoundException { final File testDir = new File(resourceDirectory); final String[] fileList = testDir.list(); final List testFiles = new ArrayList(); if (fileList == null) { throw new FileNotFoundException(resourceDirectory); } for (int i = 0; i < fileList.length; i++) { String fileName = fileList[i]; if (new File(testDir, fileName).isDirectory() == false) { testFiles.add(new File(testDir, fileName)); } } return parser.parseYangModels(testFiles); } public static SchemaContext loadSchemaContext(Set modules) { return parser.resolveSchemaContext(modules); } public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException { return parser.resolveSchemaContext(loadModules(resourceDirectory)); } public static Module findModule(Set modules, String moduleName) { Module result = null; for (Module module : modules) { if (module.getName().equals(moduleName)) { result = module; break; } } return result; } public static CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException { if (xmlInputStream == null) { throw new IllegalArgumentException(); } Node dataTree; try { dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream); } catch (XMLStreamException e) { logger.error("Error during building data tree from XML", e); return null; } if (dataTree == null) { logger.error("data tree is null"); return null; } if (dataTree instanceof SimpleNode) { logger.error("RPC XML was resolved as SimpleNode"); return null; } return (CompositeNode) dataTree; } public static String getDocumentInPrintableForm(Document doc) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); byte[] charData = out.toByteArray(); return new String(charData, "UTF-8"); } catch (IOException | TransformerException e) { String msg = "Error during transformation of Document into String"; logger.error(msg, e); return msg; } } }