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 --- 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 ++++++++++++++++++++++ 5 files changed, 108 insertions(+), 24 deletions(-) create mode 100644 sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java (limited to 'sdc-tosca-parser') 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