- Added exi capability utilities, handlers and necessary modifications
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / xml / ExiUtil.java
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 (file)
index 0000000..8baa68e
--- /dev/null
@@ -0,0 +1,91 @@
+package org.opendaylight.controller.netconf.util.xml;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.buffer.EmptyByteBuf;\r
+import io.netty.buffer.Unpooled;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.util.List;\r
+\r
+import javax.xml.parsers.ParserConfigurationException;\r
+\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.w3c.dom.Document;\r
+import org.xml.sax.SAXException;\r
+\r
+import com.siemens.ct.exi.EXIFactory;\r
+import com.siemens.ct.exi.api.dom.DOMBuilder;\r
+import com.siemens.ct.exi.api.dom.DOMWriter;\r
+import com.siemens.ct.exi.exceptions.EXIException;\r
+import com.siemens.ct.exi.helpers.DefaultEXIFactory;\r
+\r
+public class ExiUtil {\r
+\r
+    private final static Logger logger = LoggerFactory.getLogger(ExiUtil.class);\r
+\r
+\r
+\r
+    public static void encode(final Object msg, final ByteBuf out,\r
+            ExiParameters parameters)\r
+            throws EXIException, IOException, SAXException {\r
+        final byte[] bytes = toExi(msg, parameters);\r
+        out.writeBytes(bytes);\r
+    }\r
+\r
+    public static void decode(ByteBuf in, List<Object> out,\r
+            ExiParameters parameters) throws ParserConfigurationException, EXIException, IOException\r
+             {\r
+        if (in instanceof EmptyByteBuf){\r
+            return;\r
+        }\r
+\r
+        EXIFactory exiFactory = DefaultEXIFactory.newInstance();\r
+        if (parameters.getGrammars() != null) {\r
+            exiFactory.setGrammars(parameters.getGrammars());\r
+        }\r
+\r
+        if (parameters.getFidelityOptions() != null) {\r
+            exiFactory.setFidelityOptions(parameters.getFidelityOptions());\r
+        }\r
+\r
+        exiFactory.setCodingMode(parameters.getCodingMode());\r
+        try (ByteArrayInputStream exiIS = new ByteArrayInputStream(((ByteBuf)in).readBytes(((ByteBuf)in).readableBytes()).array())){\r
+            DOMBuilder domBuilder = new DOMBuilder(exiFactory);\r
+            ByteBuf result = Unpooled.copiedBuffer(XmlUtil.toString(domBuilder.parse(exiIS)).getBytes());\r
+            exiIS.close();\r
+            out.add(result);\r
+        }\r
+    }\r
+\r
+    private static byte[] toExi(Object msg, ExiParameters parameters) throws EXIException, IOException,\r
+            SAXException {\r
+\r
+        if (!(msg instanceof ByteBuf)){\r
+              return Unpooled.EMPTY_BUFFER.array();\r
+        }\r
+\r
+        EXIFactory exiFactory = DefaultEXIFactory.newInstance();\r
+        if (parameters.getGrammars() != null) {\r
+            exiFactory.setGrammars(parameters.getGrammars());\r
+        }\r
+\r
+        if (parameters.getFidelityOptions() != null) {\r
+            exiFactory.setFidelityOptions(parameters.getFidelityOptions());\r
+        }\r
+\r
+        Document doc = XmlUtil.readXmlToDocument(new String( ((ByteBuf)msg).readBytes(((ByteBuf)msg).readableBytes()).array(),"UTF-8"));\r
+        exiFactory.setCodingMode(parameters.getCodingMode());\r
+\r
+        try (ByteArrayOutputStream exiOS = new ByteArrayOutputStream()){\r
+            DOMWriter domWriter = new DOMWriter(exiFactory);\r
+            domWriter.setOutput(exiOS);\r
+            domWriter.encode(doc) ;\r
+            exiOS.close();\r
+            return exiOS.toByteArray();\r
+        }\r
+    }\r
+\r
+}\r