diff options
author | Lathish <lathishbabu.ganesan@est.tech> | 2019-06-19 15:48:09 +0000 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-07-06 02:16:41 +0000 |
commit | d368feb33cf3dcf1008b060503b7a41edef67e9e (patch) | |
tree | 86489f5e766fe73aee9051b2ef539829c83fda6c /appc-config/appc-config-adaptor/provider | |
parent | 066fc4828dd8e0dd9e6c0e9cc7e7dd705f02f7c1 (diff) |
Fixed netconf-xml parsing issue
Issue-ID: APPC-1624
Change-Id: I12aa5f1a42d5e09b5eb8886132ea9e5a2c84ef5e
Signed-off-by: Lathish <lathishbabu.ganesan@est.tech>
Diffstat (limited to 'appc-config/appc-config-adaptor/provider')
3 files changed, 76 insertions, 11 deletions
diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java index 00df23bf8..f57f188d9 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/ConfigComponentAdaptor.java @@ -25,18 +25,46 @@ package org.onap.appc.ccadaptor; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.HttpURLConnection; +import java.util.HashMap; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Properties; +import java.util.StringTokenizer; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +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 javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import org.onap.appc.exceptions.APPCException; +import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.core.util.Base64; -import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; - -import java.io.*; -import java.net.HttpURLConnection; -import java.util.*; public class ConfigComponentAdaptor implements SvcLogicAdaptor { @@ -982,7 +1010,45 @@ public class ConfigComponentAdaptor implements SvcLogicAdaptor { return (strBuff.toString()); } - private String trimResponse(String response) { + /** + * A supporting method to extract the data and configuration element from the NETCONF-XML response + * from the node and create well formatted xml. + * + * @param requestData The unformatted NETCONF-XML + * @return responseBuilder the extracted data + * @throws APPCException Exception during parsing xml data + */ + private String extractFromNetconfXml(String requestData) throws APPCException { + log.debug("extractFromNetconfXml -- Start"); + StringBuilder responseBuilder = new StringBuilder(); + StringWriter stringBuffer = new StringWriter(); + try { + requestData = requestData.replaceAll("]]>]]>", ""); + DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document document = documentBuilder.parse(new InputSource(new StringReader(requestData))); + document.getDocumentElement().normalize(); + XPath xPath = XPathFactory.newInstance().newXPath(); + Node dataNode = (Node) xPath.evaluate("//data", document, XPathConstants.NODE); + Transformer xform = TransformerFactory.newInstance().newTransformer(); + xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + xform.setOutputProperty(OutputKeys.INDENT, "yes"); + xform.transform(new DOMSource(dataNode), new StreamResult(stringBuffer)); + responseBuilder.append(stringBuffer.toString()); + if (log.isTraceEnabled()) { + log.trace("ConfigComponentAdaptor:extractFromNetconfXml after Extract: " + + responseBuilder.toString()); + } + } catch (SAXException | IOException | ParserConfigurationException | TransformerException + | XPathExpressionException exception) { + throw new APPCException("Error Occured during parsing Netconf-XML", exception); + + } + log.debug("extractFromNetconfXml -- End"); + return responseBuilder.toString(); + } + + private String trimResponse(String response) throws APPCException { + response = extractFromNetconfXml(response); log.debug("runningConfig before trimResponse : " + response); StringTokenizer line = new StringTokenizer(response, "\n"); StringBuffer sb = new StringBuffer(); @@ -992,7 +1058,7 @@ public class ConfigComponentAdaptor implements SvcLogicAdaptor { String token = line.nextToken(); if (token.indexOf("<configuration xmlns=") != -1) { captureText = true; - }else if(token.indexOf("<data>") != -1) { + }else if(token.indexOf("<data") != -1) { log.debug("token-line:with in Data: "+token); captureText = true; continue; diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java index 62773a06c..570f11eed 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java @@ -654,7 +654,6 @@ public class SshJcraftWrapper { int numTokens = rr.countTokens(); if (numTokens > 1) { - rr.nextToken(); //Skip the first line. while (rr.hasMoreTokens()) { sb.append(rr.nextToken() + '\n'); } diff --git a/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java b/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java index e798c65f6..c987bb2c9 100644 --- a/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java +++ b/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/ConfigComponentAdaptorTest.java @@ -245,7 +245,7 @@ public class ConfigComponentAdaptorTest { public void testXmlGetrunningconfig() throws TimedOutException, IOException { Properties props = null; ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); - Mockito.doReturn("<configuration xmlns=\"\n<data>\n</data>\n</configuration>") + Mockito.doReturn("<configuration xmlns=\"\">\"\n<data>\n</data>\n</configuration>") .when(mockWrapper) .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()); Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper(); @@ -260,7 +260,7 @@ public class ConfigComponentAdaptorTest { public void testXmlGetrunningconfigLongResponse() throws TimedOutException, IOException { Properties props = null; ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props)); - Mockito.doReturn("<configuration xmlns=\"\n<data>\nData line 1\nData line 2\nData line 3\nData line 4\n" + Mockito.doReturn("<configuration xmlns=\"\">\"\n<data>\nData line 1\nData line 2\nData line 3\nData line 4\n" + "Data line 5\nData line 6\nData line 7\nData line 8\nData line 9\n Data line 10\n" + "Data line 11\nData line 12\nData line 13\nData line 14\nData line 15\n" + "Data line 16\nData line 17\nData line 18\nData line 19\nData line 20</data>\n</configuration>") |