add tosca parser verification step

add tosca parser verification step to onboarding test

Change-Id: Ie74e0351ff16487279ca9739d7c494ac5340e4d8
Issue-Id: SDC-563
Signed-off-by: Yuli Shlosberg <ys9693@att.com>
diff --git a/pom.xml b/pom.xml
index 52f876d..003e133 100644
--- a/pom.xml
+++ b/pom.xml
@@ -408,7 +408,7 @@
 				<artifact-generator-api.version>1710.0.0</artifact-generator-api.version>
 				<artifact-generator-core.version>1710.0.0</artifact-generator-core.version>
 				<dox-common-lib.version>1710.0.0</dox-common-lib.version>
-				<sdc-tosca-parser.version>1.1.14</sdc-tosca-parser.version>
+				<sdc-tosca-parser.version>1.1.34</sdc-tosca-parser.version>
 			</properties>
 		</profile>
 
diff --git a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/api/ComponentBaseTest.java b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/api/ComponentBaseTest.java
index 6b0dc56..01fad09 100644
--- a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/api/ComponentBaseTest.java
+++ b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/api/ComponentBaseTest.java
@@ -28,6 +28,7 @@
 import com.thinkaurelius.titan.core.TitanGraph;
 import com.thinkaurelius.titan.core.TitanVertex;
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.ImmutableTriple;
 import org.apache.log4j.Logger;
@@ -48,6 +49,7 @@
 import org.openecomp.sdc.ci.tests.utils.cassandra.CassandraUtils;
 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
+import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
 import org.openecomp.sdc.ci.tests.utils.rest.*;
 import org.slf4j.LoggerFactory;
 import org.testng.ITestContext;
@@ -186,6 +188,10 @@
 		
 		performClean();
 		shutdownTitanLogic();
+		String basePath = FileHandling.getBasePath();
+		String csarDir = FileHandling.getCreateDirByName("outputCsar");
+		FileUtils.cleanDirectory(new File(csarDir));
+
 
 	}
 
diff --git a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/sanity/Onboard.java b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/sanity/Onboard.java
index 44e386a..4db35a2 100644
--- a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/sanity/Onboard.java
+++ b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/sanity/Onboard.java
@@ -24,8 +24,10 @@
 
 
 
+import org.apache.http.HttpResponse;
 import org.junit.Rule;
 import org.junit.rules.TestName;
+import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
 import org.openecomp.sdc.be.model.ComponentInstance;
 import org.openecomp.sdc.be.model.Resource;
 import org.openecomp.sdc.be.model.Service;
@@ -39,9 +41,14 @@
 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
+import org.openecomp.sdc.ci.tests.utils.CsarToscaTester;
 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
 import org.openecomp.sdc.ci.tests.utils.general.OnboardingUtillViaApis;
+import org.openecomp.sdc.ci.tests.utils.rest.AssetRestUtils;
+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.testng.annotations.BeforeMethod;
 import org.testng.annotations.Optional;
 import org.testng.annotations.Parameters;
@@ -52,9 +59,10 @@
 
 import fj.data.Either;
 
-
-
-
+import java.io.File;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
 
 
 public class Onboard extends ComponentBaseTest {
@@ -68,6 +76,8 @@
 	}
 
 	protected String makeDistributionValue;
+	protected ISdcCsarHelper fdntCsarHelper;
+	protected SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
 
 
 
@@ -115,12 +125,30 @@
 		ExtentTestActions.log(Status.INFO, String.format("Certify Service"));
 		service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
 
+
 //		ExtentTestActions.log(Status.INFO, String.format("Distribute Service"));
 //		AtomicOperationUtils.distributeService(service, true);
-		
-		ExtentTestActions.log(Status.INFO, String.format("The onboarding %s test is passed ! ", vnfFile));
+		try{
+//			HttpResponse assetResponse = AssetRestUtils.getComponentToscaModel(AssetTypeEnum.SERVICES, service.getUUID());
+//			InputStream inputStream = assetResponse.getEntity().getContent();
+			File csarFile = AssetRestUtils.getToscaModelCsarFile(AssetTypeEnum.SERVICES, service.getUUID());
+
+			ExtentTestActions.log(Status.INFO, "Tosca parser is going to convert service csar file to ISdcCsarHelper object...");
+			fdntCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath());
+            CsarToscaTester.processCsar(fdntCsarHelper);
+
+			ExtentTestActions.log(Status.INFO, String.format("Tosca parser successfully parsed service CSAR"));
+
+			ExtentTestActions.log(Status.INFO, String.format("The onboarding %s test is passed ! ", vnfFile));
+
+		}catch(Exception e){
+			ExtentTestActions.log(Status.ERROR, "Tosca parser FAILED to convert service csar file to ISdcCsarHelper object...");
+			ExtentTestActions.log(Status.FAIL, e);
+
+		}
+
+
+
 	}
 
-
-
 }
diff --git a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/CsarToscaTester.java b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/CsarToscaTester.java
new file mode 100644
index 0000000..afdfa43
--- /dev/null
+++ b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/CsarToscaTester.java
@@ -0,0 +1,137 @@
+package org.openecomp.sdc.ci.tests.utils;
+
+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.tosca.parser.impl.SdcTypes;
+import org.openecomp.sdc.toscaparser.api.Group;
+import org.openecomp.sdc.toscaparser.api.NodeTemplate;
+import org.openecomp.sdc.toscaparser.api.elements.Metadata;
+import org.openecomp.sdc.toscaparser.api.parameters.Input;
+import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+public class CsarToscaTester {
+	public static void main(String[] args) throws Exception {
+		System.out.println("CsarToscaParser - path to CSAR's Directory is " + Arrays.toString(args));
+		SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
+
+		File folder = new File(args[0]);
+		File[] listOfFiles = folder.listFiles();
+		Date now = new Date();
+		SimpleDateFormat dateFormat = new SimpleDateFormat("d-MM-y-HH_mm_ss");
+		String time = dateFormat.format(now);
+		String csarsDir = args[1] + "/csar-reports-" + time;
+		File dir = new File(csarsDir);
+		dir.mkdir();
+
+
+		for (File file : listOfFiles) {
+			if (file.isFile()) {  
+				System.out.println("File  " + file.getAbsolutePath());
+				String name = file.getName();
+				String currentCsarDir = csarsDir+"/"+name+"-"+time;
+				dir = new File(currentCsarDir);
+				dir.mkdir();
+				try {
+					processCsar(factory, file);
+				} catch (SdcToscaParserException e){
+					System.out.println("SdcToscaParserException caught. Code: "+e.getCode()+", message: "+ e.getMessage());
+				}
+				List<String> notAnalyzedReport = ThreadLocalsHolder.getCollector().getNotAnalyzedExceptionsReport();
+				System.out.println("NOT ANALYZED during CSAR parsing are: " + (notAnalyzedReport != null ? notAnalyzedReport.toString() : "none"));
+				List<String> warningsReport = ThreadLocalsHolder.getCollector().getWarningsReport();
+				//System.out.println("WARNINGS during CSAR parsing are: " + (warningsReport != null ? warningsReport.toString() : "none"));
+				List<String> criticalsReport = ThreadLocalsHolder.getCollector().getCriticalsReport();
+				System.out.println("CRITICALS during CSAR parsing are: " + (criticalsReport != null ? criticalsReport.toString() : "none"));
+
+				try {
+					generateReport(time, name, currentCsarDir, criticalsReport, "critical");
+					generateReport(time, name, currentCsarDir, warningsReport, "warning");
+					generateReport(time, name, currentCsarDir, notAnalyzedReport, "notAnalyzed");
+
+				} catch (IOException ex) {
+					ex.printStackTrace();
+				}
+			}
+
+		}		
+	}
+
+	public static void processCsar(SdcToscaParserFactory factory, File file) throws SdcToscaParserException {
+		ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(file.getAbsolutePath());
+		processCsarImpl(sdcCsarHelper);
+		
+	}
+
+	public static void processCsar(ISdcCsarHelper sdcCsarHelper) throws SdcToscaParserException {
+		processCsarImpl(sdcCsarHelper);
+
+	}
+
+	private static void processCsarImpl(ISdcCsarHelper sdcCsarHelper) {
+
+		//Service level
+		System.out.println("Invoking sdc-tosca methods on this CSAR....");
+		String conformanceLevel = sdcCsarHelper.getConformanceLevel();
+		System.out.println("getConformanceLevel() - conformance level is "+conformanceLevel);
+		String serviceSubstitutionMappingsTypeName = sdcCsarHelper.getServiceSubstitutionMappingsTypeName();
+		System.out.println("serviceSubstitutionMappingsTypeName() - subst mappings type of service is "+serviceSubstitutionMappingsTypeName);
+		List<Input> serviceInputs = sdcCsarHelper.getServiceInputs();
+		System.out.println("getServiceInputs() - service inputs are "+serviceInputs);
+		Metadata serviceMetadata = sdcCsarHelper.getServiceMetadata();
+		System.out.println("getServiceMetadata() - service metadata is "+serviceMetadata);
+		Map<String, Object> serviceMetadataProperties = sdcCsarHelper.getServiceMetadataProperties();
+		System.out.println("getServiceMetadataProperties() - service metadata properties is "+serviceMetadataProperties);
+		List<NodeTemplate> allottedResources = sdcCsarHelper.getAllottedResources();
+		System.out.println("getAllottedResources() - service allotted resources are "+allottedResources);
+		List<NodeTemplate> serviceVfList = sdcCsarHelper.getServiceVfList();
+		System.out.println("getServiceVfList() - VF list is "+serviceVfList);
+		List<NodeTemplate> serviceNodeTemplateBySdcType = sdcCsarHelper.getServiceNodeTemplateBySdcType(SdcTypes.VF);
+		System.out.println("getServiceNodeTemplateBySdcType() - VF list is "+serviceNodeTemplateBySdcType);
+		List<NodeTemplate> serviceNodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
+		System.out.println("getServiceNodeTemplates() - all node templates list of service is "+serviceNodeTemplates);
+
+		serviceVfList.forEach(x -> {
+			String nodeTemplateCustomizationUuid = sdcCsarHelper.getNodeTemplateCustomizationUuid(x);
+			System.out.println("getNodeTemplateCustomizationUuid() - VF ID is "+nodeTemplateCustomizationUuid);
+			String typeOfNodeTemplate = sdcCsarHelper.getTypeOfNodeTemplate(x);
+			System.out.println("getTypeOfNodeTemplate() - VF tosca type is "+typeOfNodeTemplate);
+			List<Group> vfModulesByVf = sdcCsarHelper.getVfModulesByVf(nodeTemplateCustomizationUuid);
+			System.out.println("getVfModulesByVf() - VF modules list is "+vfModulesByVf);
+			vfModulesByVf.forEach(y -> {
+				List<NodeTemplate> membersOfVfModule = sdcCsarHelper.getMembersOfVfModule(x, y);
+				System.out.println("getMembersOfVfModule() - members of VfModule are "+membersOfVfModule);
+			});
+			List<NodeTemplate> vfcListByVf = sdcCsarHelper.getVfcListByVf(nodeTemplateCustomizationUuid);
+			System.out.println("getVfcListByVf() - VFC list is "+vfcListByVf);
+			vfcListByVf.forEach(z -> {
+				List<NodeTemplate> nodeTemplateBySdcType = sdcCsarHelper.getNodeTemplateBySdcType(z, SdcTypes.CP);
+				System.out.println("getNodeTemplateBySdcType() - CP children node templates of this VFC are "+nodeTemplateBySdcType);
+				Map<String, Map<String, Object>> cpPropertiesFromVfcAsObject = sdcCsarHelper.getCpPropertiesFromVfcAsObject(z);
+				System.out.println("getCpPropertiesFromVfcAsObject() - consolidated CP properties for this VFC are "+cpPropertiesFromVfcAsObject);
+				boolean hasTopology = sdcCsarHelper.hasTopology(z);
+				System.out.println("hasTopology() - this VFC is "+(hasTopology ? "nested" : "not nested"));
+			});
+		});
+	}
+
+	private static void generateReport(String time, String name, String currentCsarDir, List<String> criticalsReport, String type)
+			throws IOException {
+		FileWriter fw;
+		fw = new FileWriter(new File(currentCsarDir + "/" + criticalsReport.size() + "-"+type+"-" + name +"-"+time + ".txt"));
+		for (String exception : criticalsReport) {
+			fw.write(exception);
+			fw.write("\r\n");
+		}
+		fw.close();
+	}
+}
diff --git a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/general/FileHandling.java b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/general/FileHandling.java
index c8f9ee4..2c5d985 100644
--- a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/general/FileHandling.java
+++ b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/general/FileHandling.java
@@ -390,6 +390,15 @@
 //
 //		}
 //	}
+
+	public static String getCreateDirByName(String dirName) {
+		File dir = new File(dirName);
+		dir.mkdir();
+		if(!dir.exists()) {
+		}
+
+		return dir.getPath();
+	}
 	
 	public static boolean isFileDownloaded(String downloadPath, String fileName) {
 		boolean flag = false;
diff --git a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/rest/AssetRestUtils.java b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/rest/AssetRestUtils.java
index 13a5641..7bcf338 100644
--- a/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/rest/AssetRestUtils.java
+++ b/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/rest/AssetRestUtils.java
@@ -26,10 +26,7 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.Map.Entry;
 
 import org.apache.http.HttpEntity;
@@ -62,6 +59,7 @@
 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
 import org.openecomp.sdc.ci.tests.utils.Utils;
 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
+import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -105,7 +103,9 @@
 		String url = String.format(Urls.GET_TOSCA_MODEL, config.getCatalogBeHost(), config.getCatalogBePort(),
 				assetType.getValue(), uuid);
 		HttpGet httpGet = new HttpGet(url);
-		File myFile = new File("tmpCSAR");
+
+		String csarDir = FileHandling.getCreateDirByName("outputCsar");
+		File myFile = new File(csarDir+ File.separator + "tmpCSAR" + getShortUUID()+".csar");
 
 		
 		httpGet.addHeader(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), "ci");
@@ -123,7 +123,9 @@
 	    }
 		return myFile;
 	}
-	
+
+
+
 	public static RestResponse getRestResponseComponentToscaModel(AssetTypeEnum assetType, String uuid) throws IOException {
 		Config config = Utils.getConfig();
 		
@@ -622,5 +624,8 @@
 		}
 		return null;
 	}
+	private static String getShortUUID() {
+		return UUID.randomUUID().toString().split("-")[0];
+	}
 	
 }