diff options
Diffstat (limited to 'sdc-tosca-parser')
12 files changed, 233 insertions, 38 deletions
diff --git a/sdc-tosca-parser/pom.xml b/sdc-tosca-parser/pom.xml index 2c8eff0..cd6e49e 100644 --- a/sdc-tosca-parser/pom.xml +++ b/sdc-tosca-parser/pom.xml @@ -71,7 +71,7 @@ <dependency>
<groupId>org.openecomp.sdc.sdc-distribution-client</groupId>
<artifactId>jython-tosca-parser</artifactId>
- <version>0.3.1-SNAPSHOT</version>
+ <version>0.4.1-SNAPSHOT</version>
</dependency>
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 #.<br> @@ -150,7 +151,7 @@ public interface ISdcCsarHelper { * Get the CSAR service metadata * @return - the service metadata object. */ - //public Metadata getServiceMetadata(); + public Map<String, String> 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..d5caa5d 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,27 +22,48 @@ package org.openecomp.sdc.tosca.parser.impl; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collector; +import java.util.stream.Collectors; 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; +import org.yaml.snakeyaml.Yaml; public class SdcCsarHelperImpl implements ISdcCsarHelper { private ToscaTemplate toscaTemplate; - + private static Yaml defaultYaml = new Yaml(); + + public SdcCsarHelperImpl(ToscaTemplate toscaTemplate) { this.toscaTemplate = toscaTemplate; } @Override + //Sunny flow - covered with UT, flat and nested public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String leafValuePath) { - //TODO - return null;/*getLeafPropertyValue(nodeTemplate, leafValuePath);*/ + String[] split = leafValuePath.split("#"); + List<Property> properties = nodeTemplate.getProperties(); + Optional<Property> findFirst = properties.stream().filter(x -> x.getName().equals(split[0])).findFirst(); + if (findFirst.isPresent()){ + Property property = findFirst.get(); + Object current = property.getValue(); + if (split.length > 1){ + current = defaultYaml.load((String)current); + for (int i = 1; i < split.length; i++) { + current = ((Map<String, Object>)current).get(split[i]); + } + } + return String.valueOf(current); + } + return null; } @Override @@ -52,16 +73,18 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { } @Override + //Sunny flow - covered with UT public List<NodeTemplate> getServiceVfList() { return getNodeTemplateBySdcType(toscaTemplate.getTopologyTemplate(), Types.TYPE_VF); } - + @Override + //Sunny flow - covered with UT public List<NodeTemplate> getServiceNodeTemplatesByType(String nodeType) { List<NodeTemplate> res = new ArrayList<>(); List<NodeTemplate> nodeTemplates = toscaTemplate.getNodeTemplates(); for (NodeTemplate nodeTemplate : nodeTemplates){ - if (nodeTemplate.getTypeDefinition().getType().equals(nodeType)){ + if (nodeType.equals(nodeTemplate.getTypeDefinition().getType())){ res.add(nodeTemplate); } } @@ -77,12 +100,11 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { //Assumed to be unique property for the list private NodeTemplate getNodeTemplateByCustomizationUuid(List<NodeTemplate> 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 +124,9 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return res; } - //Metadata question - /*@Override - public String getMetadataPropertyValue(Metadata metadata, String metadataPropertyName) { - return metadata.getMetadataPropertyValue(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,21 +135,22 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return toscaTemplate.getTopologyTemplate().getSubstitutionMappings().getNodeDefinition().getType(); } - //Metadata question - /*@Override - public Metadata getServiceMetadata() { - return toscaTemplate.getMetadata(); - }*/ + @Override + public Map<String, String> getServiceMetadata() { + TopologyTemplate topologyTemplate = toscaTemplate.getTopologyTemplate(); + System.out.println(topologyTemplate.toString()); + return topologyTemplate.getMetadata(); + } //Get property from group @Override public String getGroupPropertyLeafValue(Group group, String propertyName) { return null;//getLeafPropertyValue(group, propertyName); } - + private List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate nodeTemplate, String sdcType){ //Need metadata to fetch by type - + /*List<NodeTemplate> nodeTemplates = nodeTemplate.getNestedNodeTemplates(); List<NodeTemplate> res = new ArrayList<>(); for (NodeTemplate nodeTemplateEntry : nodeTemplates){ @@ -143,18 +160,20 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { }*/ return null; } - + private List<NodeTemplate> getNodeTemplateBySdcType(TopologyTemplate topologyTemplate, String sdcType){ //Need metadata to fetch by type - -/* List<NodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates(); + + List<NodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates(); List<NodeTemplate> 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 @@ -178,16 +197,23 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { } @Override + //TODO constant strings public List<NodeTemplate> getAllottedResources() { - //TODO - Need metadata - return new ArrayList<>(); + List<NodeTemplate> nodeTemplates = toscaTemplate.getTopologyTemplate().getNodeTemplates(); + return nodeTemplates.stream().filter(x -> x.getMetadata() != null && x.getMetadata().get("category").equals("allotted_resources")).collect(Collectors.toList()); } @Override + //Sunny flow - covered with UT public String getTypeOfNodeTemplate(NodeTemplate nodeTemplate) { //Can be done return nodeTemplate.getTypeDefinition().getType(); } - - + + /*//Not part of API, for inner/test use + public NodeTemplate getNodeTemplateByName(TopologyTemplate topologyTemplate, String topologyName){ + List<NodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates(); + Optional<NodeTemplate> findFirst = nodeTemplates.stream().filter(x -> x.getName().equals(topologyName)).findFirst(); + return findFirst.isPresent() ? findFirst.get() : null; + }*/ } 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 diff --git a/sdc-tosca-parser/src/test/java/org/openecomp/sdc/impl/ToscaParserStubsTest.java b/sdc-tosca-parser/src/test/java/org/openecomp/sdc/impl/ToscaParserStubsTest.java new file mode 100644 index 0000000..aac552c --- /dev/null +++ b/sdc-tosca-parser/src/test/java/org/openecomp/sdc/impl/ToscaParserStubsTest.java @@ -0,0 +1,96 @@ +package org.openecomp.sdc.impl;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.AfterClass;
+import static org.junit.Assert.*;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
+import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
+import org.openecomp.sdc.toscaparser.api.NodeTemplate;
+
+public class ToscaParserStubsTest {
+
+ private static ISdcCsarHelper csarHelper;
+ private static SdcToscaParserFactory factory;
+
+
+ @BeforeClass
+ public static void init() throws SdcToscaParserException{
+ factory = SdcToscaParserFactory.getInstance();
+ //csarHelper = factory.getSdcCsarHelper("C:\\Users\\pa0916\\Desktop\\Work\\ASDC\\CSARs\\service-ServiceFdnt-csar-allotted-resources-4.csar");
+ String fileStr = ToscaParserStubsTest.class.getClassLoader().getResource("csars/service-ServiceFdnt-csar-allotted-resources-4.csar").getFile();
+ File file = new File(fileStr);
+ csarHelper = factory.getSdcCsarHelper(file.getAbsolutePath());
+ }
+
+
+ @Test
+ //TODO add rainy flows
+ public void testNumberOfVfSunnyFlow() throws SdcToscaParserException {
+ List<NodeTemplate> serviceVfList = csarHelper.getServiceVfList();
+ assertNotNull(serviceVfList);
+ assertEquals(1, serviceVfList.size());
+ }
+
+ @Test
+ //TODO add rainy flows
+ public void testNodeTemplateFlatProperty() throws SdcToscaParserException {
+ List<NodeTemplate> serviceVfList = csarHelper.getServiceVfList();
+ String nodeTemplatePropertyLeafValue = csarHelper.getNodeTemplatePropertyLeafValue(serviceVfList.get(0), "availability_zone_max_count");
+ assertEquals("2", nodeTemplatePropertyLeafValue);
+ }
+
+ @Test
+ //TODO add rainy flows
+ public void testNodeTemplateNestedProperty() throws SdcToscaParserException {
+ List<NodeTemplate> serviceVfList = csarHelper.getServiceVfList();
+ String nodeTemplatePropertyLeafValue = csarHelper.getNodeTemplatePropertyLeafValue(serviceVfList.get(0), "nf_role#nf_naming#instance_name");
+ assertEquals("FDNT_instance_VF", nodeTemplatePropertyLeafValue);
+ }
+
+ @Test
+ //TODO add rainy flows
+ public void testServiceNodeTemplatesByType() throws SdcToscaParserException {
+ List<NodeTemplate> serviceVfList = csarHelper.getServiceNodeTemplatesByType("org.openecomp.resource.vf.Fdnt");
+ assertNotNull(serviceVfList);
+ assertEquals(1, serviceVfList.size());
+ }
+
+ @Test
+ //TODO add rainy flows
+ public void testGetTypeOfNodeTemplate() {
+ List<NodeTemplate> serviceVfList = csarHelper.getServiceVfList();
+ String typeOfNodeTemplate = csarHelper.getTypeOfNodeTemplate(serviceVfList.get(0));
+ assertEquals("org.openecomp.resource.vf.Fdnt", typeOfNodeTemplate);
+ }
+
+
+ //@Test
+ //TODO add rainy flows
+ public void testGetServiceMetadata() {
+ //FAILS!! Metadata is null
+ Map<String, String> serviceMetadata = csarHelper.getServiceMetadata();
+ assertNotNull(serviceMetadata);
+ assertEquals("78c72999-1003-4a35-8534-bbd7d96fcae3", serviceMetadata.get("invariantUUID"));
+ assertEquals("Service FDNT", serviceMetadata.get("name"));
+ assertEquals("true", serviceMetadata.get("serviceEcompNaming"));
+ }
+
+ @Test
+ //TODO add rainy flows
+ public void testGetAllottedResources() {
+ List<NodeTemplate> allottedResources = csarHelper.getAllottedResources();
+ assertEquals(1, allottedResources.size());
+ }
+
+
+ @AfterClass
+ public static void close(){
+ factory.close();
+ }
+ }
diff --git a/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-allotted-resources-4.csar b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-allotted-resources-4.csar Binary files differnew file mode 100644 index 0000000..7b4cff2 --- /dev/null +++ b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-allotted-resources-4.csar diff --git a/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-groups-status-supported-case-3.csar b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-groups-status-supported-case-3.csar Binary files differnew file mode 100644 index 0000000..7286663 --- /dev/null +++ b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-groups-status-supported-case-3.csar diff --git a/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-nt-props-1.csar b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-nt-props-1.csar Binary files differnew file mode 100644 index 0000000..ceac90a --- /dev/null +++ b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-nt-props-1.csar diff --git a/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-nt-tt-metadata-2.csar b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-nt-tt-metadata-2.csar Binary files differnew file mode 100644 index 0000000..3cda214 --- /dev/null +++ b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar-nt-tt-metadata-2.csar diff --git a/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar.csar b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar.csar Binary files differnew file mode 100644 index 0000000..6179316 --- /dev/null +++ b/sdc-tosca-parser/src/test/resources/csars/service-ServiceFdnt-csar.csar diff --git a/sdc-tosca-parser/src/test/resources/csars/service-vmme-csar.csar b/sdc-tosca-parser/src/test/resources/csars/service-vmme-csar.csar Binary files differnew file mode 100644 index 0000000..416707c --- /dev/null +++ b/sdc-tosca-parser/src/test/resources/csars/service-vmme-csar.csar |