From 788e651b9234fbfeb2bb4cc052fecb06a10ef70f Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Tue, 19 Jun 2018 16:01:12 +0900 Subject: CCSDK PropNode can read XML and put to ctx Issue-ID: CCSDK-303 Change-Id: I7104e7f8735d0c1496e93cdb8112e49333b016e9 Signed-off-by: Ganesh Chandrasekaran --- .../onap/ccsdk/sli/plugins/prop/Parameters.java | 4 + .../ccsdk/sli/plugins/prop/PropertiesNode.java | 52 ++- .../org/onap/ccsdk/sli/plugins/prop/XmlParser.java | 171 ++++++++++ .../ccsdk/sli/plugins/prop/TestPropertiesNode.java | 352 +++++++++++++++++++++ .../onap/ccsdk/sli/plugins/prop/TestXmlParser.java | 121 +++++++ .../provider/src/test/resources/invalidlength.xml | 49 +++ properties-node/provider/src/test/resources/test | 30 ++ .../provider/src/test/resources/test-invalid.xml | 170 ++++++++++ .../provider/src/test/resources/test.xml | 182 +++++++++++ .../provider/src/test/resources/test3.xml | 82 +++++ 10 files changed, 1201 insertions(+), 12 deletions(-) create mode 100644 properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/XmlParser.java create mode 100644 properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestXmlParser.java create mode 100644 properties-node/provider/src/test/resources/invalidlength.xml create mode 100644 properties-node/provider/src/test/resources/test create mode 100644 properties-node/provider/src/test/resources/test-invalid.xml create mode 100644 properties-node/provider/src/test/resources/test.xml create mode 100644 properties-node/provider/src/test/resources/test3.xml (limited to 'properties-node/provider/src') diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java index e15f0822d..99e4647d7 100644 --- a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java @@ -21,7 +21,11 @@ package org.onap.ccsdk.sli.plugins.prop; +import java.util.Set; + public class Parameters { public String fileName; public String contextPrefix; + public Set listNameList; + public boolean fileBasedParsing; } diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java index 63fdeded8..cefc9c23e 100644 --- a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java @@ -25,8 +25,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.HashSet; import java.util.Map; import java.util.Properties; +import java.util.Set; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; @@ -46,12 +48,33 @@ public class PropertiesNode implements SvcLogicJavaPlugin { InputStream in = new FileInputStream(file); Map mm = null; String pfx = param.contextPrefix != null ? param.contextPrefix + '.' : ""; - if ("json".equalsIgnoreCase(getFileExtension(param.fileName))){ + if(param.fileBasedParsing){ byte[] data = new byte[(int) file.length()]; - in.read(data); - in.close(); - String str = new String(data, "UTF-8"); - mm = JsonParser.convertToProperties(str); + if ("json".equalsIgnoreCase(getFileExtension(param.fileName))) { + in.read(data); + String str = new String(data, "UTF-8"); + mm = JsonParser.convertToProperties(str); + } else if ("xml".equalsIgnoreCase(getFileExtension(param.fileName))) { + in.read(data); + String str = new String(data, "UTF-8"); + mm = XmlParser.convertToProperties(str, param.listNameList); + } else { + prop.load(in); + for (Object key : prop.keySet()) { + String name = (String) key; + String value = prop.getProperty(name); + if (value != null && value.trim().length() > 0) { + ctx.setAttribute(pfx + name, value.trim()); + log.info("+++ " + pfx + name + ": [" + value + "]"); + } + } + } + if (mm != null){ + for (Map.Entry entry : mm.entrySet()){ + ctx.setAttribute(pfx + entry.getKey(), entry.getValue()); + log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]"); + } + } } else { prop.load(in); for (Object key : prop.keySet()) { @@ -63,13 +86,7 @@ public class PropertiesNode implements SvcLogicJavaPlugin { } } } - if (mm != null){ - for (Map.Entry entry : mm.entrySet()){ - ctx.setAttribute(pfx + entry.getKey(), entry.getValue()); - log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]"); - } - } - + in.close(); } catch (IOException e) { throw new SvcLogicException("Cannot read property file: " + param.fileName + ": " + e.getMessage(), e); } @@ -92,9 +109,20 @@ public class PropertiesNode implements SvcLogicJavaPlugin { Parameters p = new Parameters(); p.fileName = parseParam(paramMap, "fileName", true, null); p.contextPrefix = parseParam(paramMap, "contextPrefix", false, null); + p.listNameList = getListNameList(paramMap); + String fileBasedParsingStr = paramMap.get("fileBasedParsing"); + p.fileBasedParsing = "true".equalsIgnoreCase(fileBasedParsingStr); return p; } + protected Set getListNameList(Map paramMap) { + Set ll = new HashSet<>(); + for (Map.Entry entry : paramMap.entrySet()) + if (entry.getKey().startsWith("listName")) + ll.add(entry.getValue()); + return ll; + } + private String parseParam(Map paramMap, String name, boolean required, String def) throws SvcLogicException { String s = paramMap.get(name); diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/XmlParser.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/XmlParser.java new file mode 100644 index 000000000..c9581af55 --- /dev/null +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/XmlParser.java @@ -0,0 +1,171 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.plugins.prop; + +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static com.google.common.base.Preconditions.checkNotNull; + +public final class XmlParser { + + private static final Logger log = LoggerFactory.getLogger(XmlParser.class); + + private XmlParser() { + // Preventing instantiation of the same. + } + + public static Map convertToProperties(String s, Set listNameList) + throws SvcLogicException { + + checkNotNull(s, "Input should not be null."); + + Handler handler = new Handler(listNameList); + try { + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser saxParser = factory.newSAXParser(); + InputStream in = new ByteArrayInputStream(s.getBytes()); + saxParser.parse(in, handler); + } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) { + throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e); + } + return handler.getProperties(); + } + + private static class Handler extends DefaultHandler { + + private Set listNameList; + + private Map properties = new HashMap<>(); + + public Map getProperties() { + return properties; + } + + public Handler(Set listNameList) { + super(); + this.listNameList = listNameList; + if (this.listNameList == null) + this.listNameList = new HashSet<>(); + } + + StringBuilder currentName = new StringBuilder(); + StringBuilder currentValue = new StringBuilder(); + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + super.startElement(uri, localName, qName, attributes); + + String name = localName; + if (name == null || name.trim().length() == 0) + name = qName; + int i2 = name.indexOf(':'); + if (i2 >= 0) + name = name.substring(i2 + 1); + + if (currentName.length() > 0) + currentName.append(Character.toString('.')); + currentName.append(name); + + String listName = removeIndexes(currentName.toString()); + + if (listNameList.contains(listName)) { + String n = currentName.toString() + "_length"; + int len = getInt(properties, n); + properties.put(n, String.valueOf(len + 1)); + currentName.append("[").append(len).append("]"); + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + super.endElement(uri, localName, qName); + + String name = localName; + if (name == null || name.trim().length() == 0) + name = qName; + int i2 = name.indexOf(':'); + if (i2 >= 0) + name = name.substring(i2 + 1); + + String s = currentValue.toString().trim(); + if (s.length() > 0) { + properties.put(currentName.toString(), s); + + log.info("Added property: {} : {}", currentName, s); + currentValue = new StringBuilder(); + } + + int i1 = currentName.lastIndexOf("." + name); + if (i1 <= 0) + currentName = new StringBuilder(); + else + currentName = new StringBuilder(currentName.substring(0, i1)); + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + super.characters(ch, start, length); + + String value = new String(ch, start, length); + currentValue.append(value); + } + + private static int getInt(Map mm, String name) { + String s = mm.get(name); + if (s == null) + return 0; + return Integer.parseInt(s); + } + + private String removeIndexes(String currentName) { + StringBuilder b = new StringBuilder(); + boolean add = true; + for (int i = 0; i < currentName.length(); i++) { + char c = currentName.charAt(i); + if (c == '[') + add = false; + else if (c == ']') + add = true; + else if (add) + b.append(Character.toString(c)); + } + return b.toString(); + } + } +} diff --git a/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java b/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java index 894eee997..aa3f89d12 100644 --- a/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java +++ b/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java @@ -1,7 +1,9 @@ package jtest.org.onap.ccsdk.sli.plugins.prop; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -22,6 +24,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.json"); p.put("contextPrefix", "test-json"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -37,6 +40,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.json"); p.put("contextPrefix", "test-json"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -54,6 +58,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.json"); p.put("contextPrefix", ""); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -72,6 +77,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.json"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -91,6 +97,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.json"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -112,6 +119,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test-invalid.json"); p.put("contextPrefix", "invalid"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -126,6 +134,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.txt"); p.put("contextPrefix", "test-txt"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -147,6 +156,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.txt"); p.put("contextPrefix", ""); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -167,6 +177,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.txt"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -188,6 +199,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/test/resources/test.txt"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -211,6 +223,7 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("responsePrefix", "response"); p.put("skipSending", "true"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -224,6 +237,7 @@ public class TestPropertiesNode { ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1"); Map p = new HashMap(); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); @@ -238,10 +252,348 @@ public class TestPropertiesNode { Map p = new HashMap(); p.put("fileName", "src/tests/resources/test.txt"); + p.put("fileBasedParsing","true"); PropertiesNode rcn = new PropertiesNode(); rcn.readProperties(p, ctx); assertEquals(ctx.getAttribute("tmp.sdn-circuit-req-row_length"),"1"); } + + @Test + public void testXMLFileParsing() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("contextPrefix", "test-xml"); + p.put("listName", "project.build"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("test-xml.project.modelVersion"),"4.0.0"); + } + + @Test + public void testXMLFileInnerParsing() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("contextPrefix", "test-xml"); + p.put("listName", "project.modelVersion"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("test-xml.project.properties.project.build.sourceEncoding"),"UTF-8"); + assertEquals(ctx.getAttribute("test-xml.project.dependencies.dependency.scope"),"provided"); + assertEquals(ctx.getAttribute("test-xml.project.build.pluginManagement.plugins.plugin.configuration" + + ".lifecycleMappingMetadata.pluginExecutions.pluginExecution." + + "pluginExecutionFilter.versionRange"),"[1.2.0.100-SNAPSHOT,)"); + assertEquals(ctx.getAttribute("test-xml.project.build.plugins.plugin.configuration." + + "instructions.Import-Package"),"*"); + } + + @Test + public void testXMLFileParsingPrefixCheck() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("contextPrefix", ""); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("project.properties.project.build.sourceEncoding"),"UTF-8"); + assertEquals(ctx.getAttribute("project.dependencies.dependency.scope"),"provided"); + assertEquals(ctx.getAttribute("project.build.pluginManagement.plugins.plugin.configuration" + + ".lifecycleMappingMetadata.pluginExecutions.pluginExecution." + + "pluginExecutionFilter.versionRange"),"[1.2.0.100-SNAPSHOT,)"); + assertEquals(ctx.getAttribute("project.build.plugins.plugin.configuration." + + "instructions.Import-Package"),"*"); + } + + @Test + public void testXMLFileParsingNoPrefix() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("project.properties.project.build.sourceEncoding"),"UTF-8"); + assertEquals(ctx.getAttribute("project.dependencies.dependency.scope"),"provided"); + assertEquals(ctx.getAttribute("project.build.pluginManagement.plugins.plugin.configuration" + + ".lifecycleMappingMetadata.pluginExecutions.pluginExecution." + + "pluginExecutionFilter.versionRange"),"[1.2.0.100-SNAPSHOT,)"); + assertEquals(ctx.getAttribute("project.build.plugins.plugin.configuration." + + "instructions.Import-Package"),"*"); + } + + @Test + public void testXMLFileParsingCtxCheck() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1"); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("project.properties.project.build.sourceEncoding"),"UTF-8"); + assertEquals(ctx.getAttribute("project.dependencies.dependency.scope"),"provided"); + assertEquals(ctx.getAttribute("project.build.pluginManagement.plugins.plugin.configuration" + + ".lifecycleMappingMetadata.pluginExecutions.pluginExecution." + + "pluginExecutionFilter.versionRange"),"[1.2.0.100-SNAPSHOT,)"); + assertEquals(ctx.getAttribute("project.build.plugins.plugin.configuration." + + "instructions.Import-Package"),"*"); + assertEquals(ctx.getAttribute("tmp.sdn-circuit-req-row_length"),"1"); + } + + @Test(expected = SvcLogicException.class) + public void testToPropertiesInvalidXML() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1"); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test-invalid.xml"); + p.put("contextPrefix", "invalid"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("tmp.sdn-circuit-req-row_length"),"1"); + } + + @Test + public void testXMLFileParsingListName() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("contextPrefix", "test-xml-listName"); + p.put("fileBasedParsing","true"); + p.put("listName", "project.build.pluginManagement"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("test-xml-listName.project.build." + + "pluginManagement.plugins.plugin.version"),null); + assertEquals(ctx.getAttribute("test-xml-listName.project.build." + + "plugins.plugin.groupId"),"org.apache.felix"); + } + + @Test + public void testXMLFileParsingListNameAnother() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("contextPrefix", "test-xml-listName"); + p.put("fileBasedParsing","true"); + p.put("listName", "project.modelVersion"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("test-xml-listName.project.modelVersion"),null); + assertEquals(ctx.getAttribute("test-xml-listName.project.build." + + "plugins.plugin.groupId"),"org.apache.felix"); + } + + @Test + public void testTXTFileParsingNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.txt"); + p.put("contextPrefix", "test-txt"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("test-txt.service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("test-txt.service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("test-txt.operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("test-txt.service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("test-txt.service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + } + + @Test + public void testTXTFileParsingPrefixCheckNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.txt"); + p.put("contextPrefix", ""); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + } + + @Test + public void testTXTFileParsingNoPrefixNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.txt"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + } + + @Test + public void testTXTFileParsingCtxCheckNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1"); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.txt"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + assertEquals(ctx.getAttribute("tmp.sdn-circuit-req-row_length"),"1"); + } + + @Test + public void testJSONFileArrayParsingNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.json"); + p.put("contextPrefix", "NotFileBased"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("NotFileBased.\"limit-value\""),"\"1920000\""); + assertEquals(ctx.getAttribute("NotFileBased.\"hard-limit-expression\""),"\"max-server-speed * number-primary-servers\","); + assertEquals(ctx.getAttribute("NotFileBased.\"test-inner-node\""),"\"Test-Value\""); + } + + @Test + public void testXMLFileInnerParsingNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test.xml"); + p.put("contextPrefix", "NotFileBased"); + p.put("listName", "project.modelVersion"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("NotFileBased.RESTAPI"),"Call Node - Provider"); + assertEquals(ctx.getAttribute("NotFileBased."); + assertEquals(ctx.getAttribute("NotFileBased.openECOMP"),"SDN-C"); + assertEquals(ctx.getAttribute("NotFileBased."); + } + + @Test + public void testNoFileTypeNoPrefixNotFileBased() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + } + + @Test(expected = SvcLogicException.class) + public void testNoFileTypeParseReqError() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("file Name", "src/test/resources/test"); + p.put("fileBasedParsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + } + + @Test + public void testNoFileTypeParseError() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("fileName", "src/test/resources/test"); + p.put("file Based % Parsing","true"); + + PropertiesNode rcn = new PropertiesNode(); + rcn.readProperties(p, ctx); + + assertEquals(ctx.getAttribute("service-data.service-information.service-type"),"AVPN"); + assertEquals(ctx.getAttribute("service-configuration-notification-input.response-code"),"0"); + assertEquals(ctx.getAttribute("operational-data.avpn-ip-port-information.port-" + + "level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing"),"WRED"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.l1-customer-handoff"),"_1000BASELX"); + assertEquals(ctx.getAttribute("service-data.avpn-ip-port-information.avpn-" + + "access-information.vlan-tag-control"),"_1Q"); + } } diff --git a/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestXmlParser.java b/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestXmlParser.java new file mode 100644 index 000000000..7cd072ed8 --- /dev/null +++ b/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestXmlParser.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package jtest.org.onap.ccsdk.sli.plugins.prop; + +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.prop.XmlParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class TestXmlParser { + + private static final Logger log = LoggerFactory.getLogger(TestXmlParser.class); + + @Test + public void test() throws Exception { + BufferedReader in = new BufferedReader( + new InputStreamReader(ClassLoader.getSystemResourceAsStream("test3.xml")) + ); + StringBuilder b = new StringBuilder(); + String line; + while ((line = in.readLine()) != null) + b.append(line).append('\n'); + + Set listNameList = new HashSet(); + listNameList.add("project.dependencies.dependency"); + listNameList.add("project.build.plugins.plugin"); + listNameList.add("project.build.plugins.plugin.executions.execution"); + listNameList.add("project.build.pluginManagement.plugins.plugin"); + listNameList.add("project.build.pluginManagement." + + "plugins.plugin.configuration.lifecycleMappingMetadata.pluginExecutions.pluginExecution"); + + Map mm = XmlParser.convertToProperties(b.toString(), listNameList); + logProperties(mm); + in.close(); + } + + @Test + public void testValidLength() throws Exception { + BufferedReader in = new BufferedReader( + new InputStreamReader(ClassLoader.getSystemResourceAsStream("test3.xml")) + ); + StringBuilder b = new StringBuilder(); + String line; + while ((line = in.readLine()) != null) + b.append(line).append('\n'); + + Set listNameList = new HashSet(); + listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfImport"); + listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfExport"); + + Map mm = XmlParser.convertToProperties(b.toString(), listNameList); + + assertThat(mm.get("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfExport[5]"), is("SET_RESET_LP")); + assertThat(mm.get("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfImport[0]"), is("SET_BVOIP_IN")); + + logProperties(mm); + in.close(); + } + + @Test(expected = SvcLogicException.class) + public void testInvalidLength() throws Exception { + BufferedReader in = new BufferedReader( + new InputStreamReader(ClassLoader.getSystemResourceAsStream("invalidlength.xml")) + ); + StringBuilder b = new StringBuilder(); + String line; + while ((line = in.readLine()) != null) + b.append(line).append('\n'); + + Set listNameList = new HashSet(); + listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfImport"); + listNameList.add("ApplyGroupResponse.ApplyGroupResponseData.VrfDetails.VrfExport"); + + Map mm = XmlParser.convertToProperties(b.toString(), listNameList); + logProperties(mm); + in.close(); + } + + private void logProperties(Map mm) { + List ll = new ArrayList<>(); + for (Object o : mm.keySet()) + ll.add((String) o); + Collections.sort(ll); + + log.info("Properties:"); + for (String name : ll) + log.info("--- " + name + ": " + mm.get(name)); + } +} diff --git a/properties-node/provider/src/test/resources/invalidlength.xml b/properties-node/provider/src/test/resources/invalidlength.xml new file mode 100644 index 000000000..c086d564e --- /dev/null +++ b/properties-node/provider/src/test/resources/invalidlength.xml @@ -0,0 +1,49 @@ + + + + + + ICOREPVC-81114561 + + VPNL811182 + 811182 + 21302:811182 + SET_BVOIP_IN + SET6_BVOIP_IN + a + SET6_DSU + SET_DSU + SET6_MANAGED + SET_MANAGED + SET_LOVRF_COMMUNITY + SET_RESET_LP + + AG_MAX_MCASTROUTES + + + + 200 + Success + Y + + diff --git a/properties-node/provider/src/test/resources/test b/properties-node/provider/src/test/resources/test new file mode 100644 index 000000000..79e8acff4 --- /dev/null +++ b/properties-node/provider/src/test/resources/test @@ -0,0 +1,30 @@ +operational-data.avpn-ip-port-information.port-level-cos.queueing.pe-egress-class-queueing-policing-codes.cos2v-queueing-code = P +operational-data.avpn-ip-port-information.port-level-cos.queueing.pe-egress-class-queueing-policing-codes.cos3-queueing-code = W +operational-data.avpn-ip-port-information.port-level-cos.queueing.pe-per-class-queueing-behaviors.cos2-queueing = WRED +operational-data.avpn-ip-port-information.port-level-cos.queueing.pe-per-class-queueing-behaviors.cos2v-queueing = QueueLimit +operational-data.avpn-ip-port-information.port-level-cos.queueing.pe-per-class-queueing-behaviors.cos3-queueing = WRED +operational-data.avpn-ip-port-information.port-level-cos.shaping.pe-egress-per-class-shaping-behaviors.cos2-shaping = Disable +operational-data.avpn-ip-port-information.port-level-cos.shaping.pe-egress-per-class-shaping-behaviors.cos2v-shaping = Enable +operational-data.avpn-ip-port-information.port-level-cos.shaping.pe-egress-per-class-shaping-behaviors.cos3-shaping = Disable +operational-data.avpn-ip-port-information.port-level-cos.shaping.pe-egress-per-class-shaping-codes.cos2-shaping-code = W +operational-data.avpn-ip-port-information.port-level-cos.shaping.pe-egress-per-class-shaping-codes.cos2v-shaping-code = P +service-configuration-notification-input.ack-final-indicator = Y +service-configuration-notification-input.response-code = 0 +service-configuration-notification-input.response-message = Plc Activation Failed: Device gblond2005me6 Sync-from Failed. Please check device IP address and NCS setup. +service-configuration-notification-input.service-information.service-instance-id = TEST7 +service-configuration-notification-input.service-information.service-type = AVPN +service-configuration-notification-input.svc-request-id = TEST7 +service-data.avpn-ip-port-information.avpn-access-information.access-circuit-id = DHEC.54831.170.ATI +service-data.avpn-ip-port-information.avpn-access-information.access-interface = _1G +service-data.avpn-ip-port-information.avpn-access-information.access-speed = 10000 +service-data.avpn-ip-port-information.avpn-access-information.access-speed-units = Kbps +service-data.avpn-ip-port-information.avpn-access-information.l1-customer-handoff = _1000BASELX +service-data.avpn-ip-port-information.avpn-access-information.managed-ce = N +service-data.avpn-ip-port-information.avpn-access-information.vlan-tag-control = _1Q +service-data.avpn-ip-port-information.clli = LONDENEH +service-data.avpn-ip-port-information.contracted-port-speed = 10000 +service-data.avpn-ip-port-information.contracted-port-speed-units = Kbps +service-data.avpn-ip-port-information.endpoint-information.bundle-id = 33 +service-data.avpn-ip-port-information.endpoint-information.interface-string = ae0 +service-data.service-information.service-instance-id = ICORESITE-2751508 +service-data.service-information.service-type = AVPN \ No newline at end of file diff --git a/properties-node/provider/src/test/resources/test-invalid.xml b/properties-node/provider/src/test/resources/test-invalid.xml new file mode 100644 index 000000000..50bd0fff2 --- /dev/null +++ b/properties-node/provider/src/test/resources/test-invalid.xml @@ -0,0 +1,170 @@ + + + + + 4.0.0 + + org.onap.ccsdk.sli.plugins + restapi-call-node + 6.0.0-SNAPSHOT + + restapi-call-node-provider + bundle + RESTAPI Call Node - Provider + http://maven.apache.org + + UTF-8 + + + + junit + junit + test + + + org.springframework + spring-test + 3.1.4.RELEASE + test + + + org.onap.ccsdk.sli + sli-common + compile + + + org.onap.ccsdk.sli + sli-provider + compile + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + + org.springframework + spring-beans + 3.1.4.RELEASE + + + org.springframework + spring-context + 3.1.4.RELEASE + + + xerces + xerces + 2.4.0 + provided + + + com.sun.jersey + jersey-client + 1.17 + + + com.s + + + + + + com.brocade.developer + providermodule-plugin + + org.onap.ccsdk.sli.plugins + restapi-call-node + + + + process-sources + + process + + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + org.onap.ccsdk.sli.plugins.restapicall + org.onap.ccsdk.sli.plugins.restapicall + * + + + ${project.basedir}/src/main/resources/META-INF + + + + + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + com.brocade.developer + + + providermodule-plugin + + + [1.2.0.100-SNAPSHOT,) + + + process + + + + + + + + + + + + + + diff --git a/properties-node/provider/src/test/resources/test.xml b/properties-node/provider/src/test/resources/test.xml new file mode 100644 index 000000000..1974f688e --- /dev/null +++ b/properties-node/provider/src/test/resources/test.xml @@ -0,0 +1,182 @@ + + + + + 4.0.0 + + org.onap.ccsdk.sli.plugins + restapi-call-node + 6.0.0-SNAPSHOT + + restapi-call-node-provider + bundle + RESTAPI Call Node - Provider + http://maven.apache.org + + UTF-8 + + + + junit + junit + test + + + org.springframework + spring-test + 3.1.4.RELEASE + test + + + org.onap.ccsdk.sli + sli-common + compile + + + org.onap.ccsdk.sli + sli-provider + compile + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + + org.springframework + spring-beans + 3.1.4.RELEASE + + + org.springframework + spring-context + 3.1.4.RELEASE + + + xerces + xerces + 2.4.0 + provided + + + com.sun.jersey + jersey-client + 1.17 + + + com.sun.jersey.contribs.jersey-oauth + oauth-signature + 1.17 + + + com.sun.jersey.contribs.jersey-oauth + oauth-client + 1.17 + + + commons-codec + commons-codec + + + + + + + com.brocade.developer + providermodule-plugin + + org.onap.ccsdk.sli.plugins + restapi-call-node + + + + process-sources + + process + + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + org.onap.ccsdk.sli.plugins.restapicall + org.onap.ccsdk.sli.plugins.restapicall + * + + + ${project.basedir}/src/main/resources/META-INF + + + + + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + com.brocade.developer + + + providermodule-plugin + + + [1.2.0.100-SNAPSHOT,) + + + process + + + + + + + + + + + + + + diff --git a/properties-node/provider/src/test/resources/test3.xml b/properties-node/provider/src/test/resources/test3.xml new file mode 100644 index 000000000..ade412681 --- /dev/null +++ b/properties-node/provider/src/test/resources/test3.xml @@ -0,0 +1,82 @@ + + + + + + ICOREPVC-81114561 + + VPNL811182 + 811182 + 21302:811182 + SET_BVOIP_IN + SET6_BVOIP_IN + SET6_DSU + SET_DSU + SET6_MANAGED + SET_MANAGED + SET_LOVRF_COMMUNITY + SET_RESET_LP + + AG_MAX_MCASTROUTES + + + + BGP4_PROTOCOL + v4 + gp_21302:811182 + + AG_L3VPN_EBGP + + + AG_MAX_PREFIX + + + AG_BGP_UNMANAGED + + + AG_BFD_BGP_3000 + + + + BGP4_PROTOCOL + v6 + gp6_21302:811182 + + AG6_L3VPN_EBGP + + + AG6_MAX_PREFIX + + + AG6_BGP_UNMANAGED + + + AG6_BFD_BGP_3000 + + + + 200 + Success + Y + + -- cgit 1.2.3-korg