X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fxml%2FExiUtil.java;fp=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fxml%2FExiUtil.java;h=8baa68e494272ebda3658527525ff09c95df9398;hb=a474fa0e090124ba715dc4251944dd8532c90491;hp=0000000000000000000000000000000000000000;hpb=20bd54c0c63f16541a0ab907165a7ea2d6165442;p=controller.git diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/ExiUtil.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/ExiUtil.java new file mode 100644 index 0000000000..8baa68e494 --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/ExiUtil.java @@ -0,0 +1,91 @@ +package org.opendaylight.controller.netconf.util.xml; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.EmptyByteBuf; +import io.netty.buffer.Unpooled; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.siemens.ct.exi.EXIFactory; +import com.siemens.ct.exi.api.dom.DOMBuilder; +import com.siemens.ct.exi.api.dom.DOMWriter; +import com.siemens.ct.exi.exceptions.EXIException; +import com.siemens.ct.exi.helpers.DefaultEXIFactory; + +public class ExiUtil { + + private final static Logger logger = LoggerFactory.getLogger(ExiUtil.class); + + + + public static void encode(final Object msg, final ByteBuf out, + ExiParameters parameters) + throws EXIException, IOException, SAXException { + final byte[] bytes = toExi(msg, parameters); + out.writeBytes(bytes); + } + + public static void decode(ByteBuf in, List out, + ExiParameters parameters) throws ParserConfigurationException, EXIException, IOException + { + if (in instanceof EmptyByteBuf){ + return; + } + + EXIFactory exiFactory = DefaultEXIFactory.newInstance(); + if (parameters.getGrammars() != null) { + exiFactory.setGrammars(parameters.getGrammars()); + } + + if (parameters.getFidelityOptions() != null) { + exiFactory.setFidelityOptions(parameters.getFidelityOptions()); + } + + exiFactory.setCodingMode(parameters.getCodingMode()); + try (ByteArrayInputStream exiIS = new ByteArrayInputStream(((ByteBuf)in).readBytes(((ByteBuf)in).readableBytes()).array())){ + DOMBuilder domBuilder = new DOMBuilder(exiFactory); + ByteBuf result = Unpooled.copiedBuffer(XmlUtil.toString(domBuilder.parse(exiIS)).getBytes()); + exiIS.close(); + out.add(result); + } + } + + private static byte[] toExi(Object msg, ExiParameters parameters) throws EXIException, IOException, + SAXException { + + if (!(msg instanceof ByteBuf)){ + return Unpooled.EMPTY_BUFFER.array(); + } + + EXIFactory exiFactory = DefaultEXIFactory.newInstance(); + if (parameters.getGrammars() != null) { + exiFactory.setGrammars(parameters.getGrammars()); + } + + if (parameters.getFidelityOptions() != null) { + exiFactory.setFidelityOptions(parameters.getFidelityOptions()); + } + + Document doc = XmlUtil.readXmlToDocument(new String( ((ByteBuf)msg).readBytes(((ByteBuf)msg).readableBytes()).array(),"UTF-8")); + exiFactory.setCodingMode(parameters.getCodingMode()); + + try (ByteArrayOutputStream exiOS = new ByteArrayOutputStream()){ + DOMWriter domWriter = new DOMWriter(exiFactory); + domWriter.setOutput(exiOS); + domWriter.encode(doc) ; + exiOS.close(); + return exiOS.toByteArray(); + } + } + +}