From d368feb33cf3dcf1008b060503b7a41edef67e9e Mon Sep 17 00:00:00 2001 From: Lathish Date: Wed, 19 Jun 2019 15:48:09 +0000 Subject: Fixed netconf-xml parsing issue Issue-ID: APPC-1624 Change-Id: I12aa5f1a42d5e09b5eb8886132ea9e5a2c84ef5e Signed-off-by: Lathish --- .../appc/ccadaptor/ConfigComponentAdaptor.java | 82 +++++++++++++++++++--- .../org/onap/appc/ccadaptor/SshJcraftWrapper.java | 1 - .../appc/ccadaptor/ConfigComponentAdaptorTest.java | 4 +- 3 files changed, 76 insertions(+), 11 deletions(-) (limited to 'appc-config/appc-config-adaptor') 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("") != -1) { + }else if(token.indexOf(" 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("\n\n") + Mockito.doReturn("\"\n\n\n") .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("\nData line 1\nData line 2\nData line 3\nData line 4\n" + Mockito.doReturn("\"\n\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\n") -- cgit 1.2.3-korg