From b51f888ee16f8bf92c7d9a0331eaa05e3bb938d2 Mon Sep 17 00:00:00 2001 From: Pavel Aharoni Date: Sun, 2 Apr 2017 09:21:38 +0300 Subject: [SDC-8] adding factory/parser init logic Change-Id: I4ed1581a2871915e12f0795de304f90fcab7d0db Signed-off-by: Pavel Aharoni --- jython-tosca-parser/pom.xml | 2 +- pom.xml | 1 - .../main/java/org/openecomp/test/ClientTest.java | 28 +++------ sdc-tosca-parser/pom.xml | 2 +- .../sdc/tosca/parser/api/ISdcCsarHelper.java | 7 ++- .../parser/exceptions/SdcToscaParserException.java | 5 +- .../sdc/tosca/parser/impl/SdcCsarHelperImpl.java | 49 +++++++++------ .../tosca/parser/impl/SdcToscaParserFactory.java | 69 ++++++++++++++++++++++ 8 files changed, 116 insertions(+), 47 deletions(-) create mode 100644 sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java diff --git a/jython-tosca-parser/pom.xml b/jython-tosca-parser/pom.xml index 2058d3a..29a8cd2 100644 --- a/jython-tosca-parser/pom.xml +++ b/jython-tosca-parser/pom.xml @@ -9,7 +9,7 @@ jython-tosca-parser - ${jython.tosca.parser.version} + 0.4.0-SNAPSHOT diff --git a/pom.xml b/pom.xml index 27ee15c..fe0f9e8 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,6 @@ 1.1.2 4.12 1.14 - 0.4.0-SNAPSHOT diff --git a/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java b/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java index 519684b..5e8dafd 100644 --- a/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java +++ b/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java @@ -20,16 +20,12 @@ package org.openecomp.test; -import java.io.IOException; - import org.openecomp.sdc.api.IDistributionClient; import org.openecomp.sdc.api.consumer.INotificationCallback; import org.openecomp.sdc.api.results.IDistributionClientResult; import org.openecomp.sdc.impl.DistributionClientFactory; -import org.openecomp.sdc.tosca.parser.impl.SdcCsarHelperImpl; -import org.openecomp.sdc.toscaparser.ToscaParser; -import org.openecomp.sdc.toscaparser.ToscaParserFactory; -import org.openecomp.sdc.toscaparser.api.ToscaTemplate; +import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper; +import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Level; @@ -37,24 +33,14 @@ import ch.qos.logback.classic.LoggerContext; public class ClientTest { public static void main(String[] args) throws Exception { - /* ToscaParserFactory toscaParserFactory = null; - try { - toscaParserFactory = new ToscaParserFactory(); - ToscaParser parser = toscaParserFactory.create(); - ToscaTemplate toscaTemplate = parser.parse("test.csar"); - SdcCsarHelperImpl csarHelper = new SdcCsarHelperImpl(toscaTemplate); + try (SdcToscaParserFactory toscaParserFactory = SdcToscaParserFactory.getInstance()){ + //ISdcCsarHelper csarHelper = toscaParserFactory.getSdcCsarHelper("C:\\Users\\pa0916\\Desktop\\Work\\ASDC\\CSARs\\service-ServiceFdnt-csar-nt-metadata.csar"); + ISdcCsarHelper csarHelper = toscaParserFactory.getSdcCsarHelper("C:\\Users\\pa0916\\Desktop\\Work\\ASDC\\CSARs\\service-ServiceFdnt-csar.csar"); String serviceSubstitutionMappingsTypeName = csarHelper.getServiceSubstitutionMappingsTypeName(); System.out.println("serviceSubstitutionMappingsTypeName is "+serviceSubstitutionMappingsTypeName); + String nodeTemplatePropertyLeafValue = csarHelper.getNodeTemplatePropertyLeafValue(csarHelper.getServiceVfList().get(0), "nf_role"); + System.out.println("property is "+nodeTemplatePropertyLeafValue); } - catch (Exception e){ - System.out.println(e); - } - finally{ - if (toscaParserFactory != null){ - toscaParserFactory.close(); - } - }*/ - LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.getLogger("org.apache.http").setLevel(Level.INFO); diff --git a/sdc-tosca-parser/pom.xml b/sdc-tosca-parser/pom.xml index 59ae0e4..d4ffda8 100644 --- a/sdc-tosca-parser/pom.xml +++ b/sdc-tosca-parser/pom.xml @@ -71,7 +71,7 @@ org.openecomp.sdc.sdc-distribution-client jython-tosca-parser - ${jython.tosca.parser.version} + 0.4.0-SNAPSHOT diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java index 916953c..26995c4 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.tosca.parser.api; import java.util.List; +import java.util.Map; import org.apache.commons.lang3.tuple.Pair; import org.openecomp.sdc.toscaparser.api.Group; @@ -119,9 +120,9 @@ public interface ISdcCsarHelper { * @param metadata - metadata object. * @param metadataPropertyName - the name of the metadata property. * @return metadata property value - */ + *//* //public String getMetadataPropertyValue(Metadata metadata, String metadataPropertyName); - + */ /** * Get input leaf value for the CSAR service, by full path separated by #.
@@ -150,7 +151,7 @@ public interface ISdcCsarHelper { * Get the CSAR service metadata * @return - the service metadata object. */ - //public Metadata getServiceMetadata(); + public Map getServiceMetadata(); /** * Get all VFC node templates from a specified VF. diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java index 5d57fc9..f41141f 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/exceptions/SdcToscaParserException.java @@ -5,5 +5,8 @@ public class SdcToscaParserException extends Exception{ * */ private static final long serialVersionUID = 626014844866501196L; - + + public SdcToscaParserException(String string) { + super(string); + } } diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java index 0d59a4d..51afe7e 100644 --- a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java @@ -22,14 +22,16 @@ package org.openecomp.sdc.tosca.parser.impl; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Optional; import org.apache.commons.lang3.tuple.Pair; import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper; import org.openecomp.sdc.toscaparser.api.Group; import org.openecomp.sdc.toscaparser.api.NodeTemplate; +import org.openecomp.sdc.toscaparser.api.Property; import org.openecomp.sdc.toscaparser.api.TopologyTemplate; import org.openecomp.sdc.toscaparser.api.ToscaTemplate; -import org.openecomp.sdc.tosca.parser.impl.Types; public class SdcCsarHelperImpl implements ISdcCsarHelper { @@ -41,8 +43,18 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { @Override public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String leafValuePath) { - //TODO - return null;/*getLeafPropertyValue(nodeTemplate, leafValuePath);*/ + String[] split = leafValuePath.split("#"); + List properties = nodeTemplate.getProperties(); + Optional findFirst = properties.stream().filter(x -> x.getName().equals(split[0])).findFirst(); + if (findFirst.isPresent()){ + Object current = findFirst.get().getValue(); + /*for (int i = 1; i < split.length; i++) { + //if (i ) + }*/ + //TODO add nested props + return (String)current; + } + return null; } @Override @@ -61,7 +73,7 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { List res = new ArrayList<>(); List nodeTemplates = toscaTemplate.getNodeTemplates(); for (NodeTemplate nodeTemplate : nodeTemplates){ - if (nodeTemplate.getTypeDefinition().getType().equals(nodeType)){ + if (nodeType.equals(nodeTemplate.getTypeDefinition().getType())){ res.add(nodeTemplate); } } @@ -77,12 +89,11 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { //Assumed to be unique property for the list private NodeTemplate getNodeTemplateByCustomizationUuid(List nodeTemplates, String customizationId){ - //TODO Metadata is missing - /*for (NodeTemplate nodeTemplate : nodeTemplates){ - if (nodeTemplate.getMetadata().getMetadataPropertyValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID).equals(customizationId)){ + for (NodeTemplate nodeTemplate : nodeTemplates){ + if (customizationId.equals(nodeTemplate.getMetadata().get(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID))){ return nodeTemplate; } - }*/ + } return null; } @@ -102,15 +113,14 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return res; } - //Metadata question /*@Override public String getMetadataPropertyValue(Metadata metadata, String metadataPropertyName) { - return metadata.getMetadataPropertyValue(metadataPropertyName); + return (String)metadata.get(metadataPropertyName); }*/ @Override public String getServiceInputLeafValue(String inputLeafValuePath) { - toscaTemplate.getTopologyTemplate().getNodeTemplates().get(0).getTypeDefinition().getType(); + //toscaTemplate.getTopologyTemplate().getNodeTemplates().get(0).getProperties().get(0). return null;//getLeafPropertyValue(toscaTemplate, inputLeafValuePath); } @@ -119,11 +129,10 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return toscaTemplate.getTopologyTemplate().getSubstitutionMappings().getNodeDefinition().getType(); } - //Metadata question - /*@Override - public Metadata getServiceMetadata() { - return toscaTemplate.getMetadata(); - }*/ + @Override + public Map getServiceMetadata() { + return toscaTemplate.getTopologyTemplate().getMetadata(); + } //Get property from group @Override @@ -147,14 +156,16 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { private List getNodeTemplateBySdcType(TopologyTemplate topologyTemplate, String sdcType){ //Need metadata to fetch by type -/* List nodeTemplates = topologyTemplate.getNodeTemplates(); + List nodeTemplates = topologyTemplate.getNodeTemplates(); List res = new ArrayList<>(); for (NodeTemplate nodeTemplateEntry : nodeTemplates){ - if (nodeTemplateEntry.getMetadata().getMetadataPropertyValue(SdcPropertyNames.PROPERTY_NAME_TYPE).equals(sdcType)){ + //TODO switch back to type condition + if (nodeTemplateEntry.getTypeDefinition().getType().contains("."+sdcType.toLowerCase()+".")){ + //if (sdcType.equals(nodeTemplateEntry.getMetadata().get(SdcPropertyNames.PROPERTY_NAME_TYPE))){ res.add(nodeTemplateEntry); } } -*/ return null; + return res; } @Override diff --git a/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java new file mode 100644 index 0000000..6be293e --- /dev/null +++ b/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java @@ -0,0 +1,69 @@ +package org.openecomp.sdc.tosca.parser.impl; + +import java.io.IOException; + +import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper; +import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException; +import org.openecomp.sdc.toscaparser.ToscaParserFactory; + +public class SdcToscaParserFactory implements AutoCloseable{ + + private static SdcToscaParserFactory instance; + private static ToscaParserFactory toscaParserFactory; + + private SdcToscaParserFactory(){} + + /** + * Get an SdcToscaParserFactory instance. + * After parsing work is done, it must be closed using the close() method. + */ + public static SdcToscaParserFactory getInstance() { + if (instance == null) { + synchronized (SdcToscaParserFactory.class) { + if (instance == null) { + instance = new SdcToscaParserFactory(); + toscaParserFactory = new ToscaParserFactory(); + } + } + } + return instance; + } + + /** + * Get an ISdcCsarHelper object for this CSAR file. + * @param csarPath - the path to CSAR file. + * @return ISdcCsarHelper object. + * @throws SdcToscaParserException - in case the path or CSAR are invalid. + */ + public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws SdcToscaParserException{ + //TODO add logic to check if legal file and csar + synchronized (SdcToscaParserFactory.class) { + if (toscaParserFactory == null){ + throw new SdcToscaParserException("The factory is closed. It was probably closed too soon."); + } + try { + return new SdcCsarHelperImpl(toscaParserFactory.create().parse(csarPath)); + } catch (IOException e) { + throw new SdcToscaParserException("Exception when creating the parser: "+e.getMessage()); + } + } + } + + /** + * Close the SdcToscaParserFactory. + */ + public void close() { + if (toscaParserFactory != null){ + synchronized (SdcToscaParserFactory.class) { + if (toscaParserFactory != null) { + try { + toscaParserFactory.close(); + toscaParserFactory = null; + } catch (IOException e) { + //TODO add logging + } + } + } + } + } +} \ No newline at end of file -- cgit 1.2.3-korg