From f8a3c48b5ca94ffb04903b252c032073d17c83a9 Mon Sep 17 00:00:00 2001 From: nancylizi Date: Mon, 26 Sep 2016 10:51:22 +0800 Subject: Modify the bug about uploadPackage and queryPackageById Issue-id:TOSCA-77 Change-Id: I2697f150bca4404bd2c806c9669961c2ddba6de2 Signed-off-by: nancylizi --- .../catalog/wrapper/PackageWrapper.java | 121 ++++++++++----------- .../catalog/wrapper/PackageWrapperUtil.java | 25 +++-- .../catalog/wrapper/PackageWrapperTest.java | 19 ++-- 3 files changed, 86 insertions(+), 79 deletions(-) diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapper.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapper.java index 0f54f03f..dbe570c8 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapper.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapper.java @@ -70,13 +70,8 @@ public class PackageWrapper { ArrayList dbResult = new ArrayList(); ArrayList result = new ArrayList(); dbResult = PackageWrapperUtil.getPackageInfoById(csarId); - if (dbResult.size() != 0) { result = PackageWrapperUtil.packageDataList2PackageMetaList(dbResult); return Response.ok(result).build(); - } else { - String errorMsg = "get package info by Id error !"; - return RestUtil.getRestException(errorMsg); - } } /** @@ -89,12 +84,7 @@ public class PackageWrapper { */ public Response uploadPackage(InputStream uploadedInputStream, FormDataContentDisposition fileDetail, HttpHeaders head) throws Exception { - PackageBasicInfo basicInfo = new PackageBasicInfo(); - String tempDirName = null; int fileSize = 0; - String fileName = ""; - UploadPackageResponse result = new UploadPackageResponse(); - PackageMeta packageMeta = new PackageMeta(); if (uploadedInputStream == null) { LOG.info("the uploadStream is null"); return Response.serverError().build(); @@ -103,65 +93,68 @@ public class PackageWrapper { LOG.info("the fileDetail is null"); return Response.serverError().build(); } + LOG.info("the fileDetail = " + ToolUtil.objectToString(fileDetail)); + String contentRange = null; + String fileName = ""; + fileName = ToolUtil.processFileName(fileDetail.getFileName()); + String tempDirName = null; + tempDirName = ToolUtil.getTempDir(CommonConstant.CATALOG_CSAR_DIR_NAME, fileName); + if (head != null) { + contentRange = head.getHeaderString(CommonConstant.HTTP_HEADER_CONTENT_RANGE); + } + LOG.info("store package chunk file, fileName:" + fileName + ",contentRange:" + contentRange); + if (ToolUtil.isEmptyString(contentRange)) { + fileSize = uploadedInputStream.available(); + contentRange = "0-" + fileSize + "/" + fileSize; + } + String fileLocation = + ToolUtil.storeChunkFileInLocal(tempDirName, fileName, uploadedInputStream); + LOG.info("the fileLocation when upload package is :" + fileLocation); + uploadedInputStream.close(); - try { - String contentRange = null; - fileName = ToolUtil.processFileName(fileDetail.getFileName()); - tempDirName = ToolUtil.getTempDir(CommonConstant.CATALOG_CSAR_DIR_NAME, fileName); - if (head != null) { - contentRange = head.getHeaderString(CommonConstant.HTTP_HEADER_CONTENT_RANGE); - } - LOG.debug("store package chunk file, fileName:" + fileName + ",contentRange:" + contentRange); - if (ToolUtil.isEmptyString(contentRange)) { - fileSize = uploadedInputStream.available(); - contentRange = "0-" + fileSize + "/" + fileSize; - } - String fileLocation = - ToolUtil.storeChunkFileInLocal(tempDirName, fileName, uploadedInputStream); - LOG.info("the fileLocation when upload package is :" + fileLocation); - uploadedInputStream.close(); + PackageBasicInfo basicInfo = new PackageBasicInfo(); + basicInfo = PackageWrapperUtil.getPacageBasicInfo(fileLocation); + String path = basicInfo.getType().toString() + File.separator + basicInfo.getProvider() + + File.separator + fileName.replace(".csar", "") + File.separator + basicInfo.getVersion(); + LOG.info("dest path is : " + path); + PackageMeta packageMeta = new PackageMeta(); + packageMeta = PackageWrapperUtil.getPackageMeta(fileName, fileLocation, basicInfo); + String dowloadUri = File.separator + path + File.separator; + String destPath = File.separator + path; + packageMeta.setDownloadUri(dowloadUri); + LOG.info("packageMeta = " + ToolUtil.objectToString(packageMeta)); + Boolean isEnd = PackageWrapperUtil.isUploadEnd(contentRange, fileName); + if (isEnd) { + String serviceTemplateId = null; + boolean uploadResult = FileManagerFactory.createFileManager().upload(tempDirName, destPath); + if (uploadResult == true) { + PackageData packageData = PackageWrapperUtil.getPackageData(packageMeta); + PackageData packateDbData = PackageManager.getInstance().addPackage(packageData); + LOG.info("Store package data to database succed ! packateDbData = " + + ToolUtil.objectToString(packateDbData)); + try { + String tempCsarPath = tempDirName + File.separator + fileName; + serviceTemplateId = ModelParserFactory.getInstance().parse(packageMeta.getCsarId(), + tempCsarPath, PackageWrapperUtil.getPackageFormat(packageMeta.getFormat())); + LOG.info("Package parse success ! serviceTemplateId = " + serviceTemplateId); + } catch (Exception e1) { + LOG.error("Parse package error ! "); + PackageManager.getInstance().deletePackage(packateDbData.getCsarId()); + throw new Exception(e1); + } - basicInfo = PackageWrapperUtil.getPacageBasicInfo(fileLocation); - String path = - basicInfo.getType().toString() + File.separator + basicInfo.getProvider() - + File.separator + fileName.replace(".csar", "") + File.separator - + basicInfo.getVersion(); - LOG.info("dest path is : " + path); - packageMeta = PackageWrapperUtil.getPackageMeta(fileName, fileLocation, basicInfo); - String dowloadUri = File.separator + path + File.separator; - String destPath = File.separator + path; - packageMeta.setDownloadUri(dowloadUri); - LOG.info("packageMeta = " + ToolUtil.objectToString(packageMeta)); - Boolean isEnd = PackageWrapperUtil.isUploadEnd(contentRange, fileName); - if (isEnd) { - boolean uploadResult = FileManagerFactory.createFileManager().upload(tempDirName, destPath); - if (uploadResult == true) { - try { - String tempCsarPath = tempDirName + File.separator + fileName; - ModelParserFactory.getInstance().parse(packageMeta.getCsarId(), - tempCsarPath , PackageWrapperUtil.getPackageFormat(packageMeta.getFormat())); - } catch (CatalogResourceException e1) { - LOG.error("parse package error ! " + e1.getMessage()); - } - PackageData packageData = PackageWrapperUtil.getPackageData(packageMeta); - PackageManager.getInstance().addPackage(packageData); + if (null != packateDbData || null == serviceTemplateId) { + PackageManager.getInstance().deletePackage(packateDbData.getCsarId()); } - LOG.info("upload package file end, fileName:" + fileName); - } - result.setCsarId(packageMeta.getCsarId()); - return Response.ok(result).build(); - } catch (Exception e1) { - LOG.error("upload package fail.", e1); - String csarId = packageMeta.getCsarId(); - if (csarId != null) { - PackageManager.getInstance().deletePackage(csarId); - } - return RestUtil.getRestException(e1.getMessage()); - } finally { - if (tempDirName != null) { - ToolUtil.deleteDir(new File(tempDirName)); } + LOG.info("upload package file end, fileName:" + fileName); + } + UploadPackageResponse result = new UploadPackageResponse(); + result.setCsarId(packageMeta.getCsarId()); + if (tempDirName != null) { + ToolUtil.deleteDir(new File(tempDirName)); } + return Response.ok(result).build(); } /** diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapperUtil.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapperUtil.java index 6354e6a1..75d02af6 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapperUtil.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapperUtil.java @@ -21,6 +21,7 @@ import com.google.gson.reflect.TypeToken; import org.openo.commontosca.catalog.common.CommonConstant; import org.openo.commontosca.catalog.common.FileUtil; +import org.openo.commontosca.catalog.common.HttpServerAddrConfig; import org.openo.commontosca.catalog.common.MsbAddrConfig; import org.openo.commontosca.catalog.common.ToolUtil; import org.openo.commontosca.catalog.db.entity.PackageData; @@ -277,10 +278,12 @@ public class PackageWrapperUtil { ArrayList dbResult) { ArrayList metas = new ArrayList(); PackageMeta meta = new PackageMeta(); - for (int i = 0; i < dbResult.size(); i++) { - PackageData data = dbResult.get(i); - meta = packageData2PackageMeta(data); - metas.add(meta); + if (dbResult.size() > 0) { + for (int i = 0; i < dbResult.size(); i++) { + PackageData data = dbResult.get(i); + meta = packageData2PackageMeta(data); + metas.add(meta); + } } return metas; } @@ -317,7 +320,11 @@ public class PackageWrapperUtil { meta.setUsageState(EnumUsageState.valueOf(packageData.getUsageState())); meta.setVersion(packageData.getVersion()); meta.setOnBoardState(packageData.getOnBoardState()); - meta.setProcessState(EnumProcessState.valueOf(packageData.getProcessState())); + String processState = packageData.getProcessState(); + if (processState.equals("deletefail")) { + processState = "deleteFailed"; + } + meta.setProcessState(EnumProcessState.valueOf(processState)); return meta; } @@ -328,8 +335,12 @@ public class PackageWrapperUtil { */ public static String getUrl(String uri) { String url = null; - if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) { - url = MsbAddrConfig.getMsbAddress() + uri.substring(1); +// if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) { +// url = MsbAddrConfig.getMsbAddress() + uri.substring(1); +// } +// url = MsbAddrConfig.getMsbAddress() + uri; + if ((HttpServerAddrConfig.getHttpServerAddress().endsWith("/")) && uri.startsWith("/")) { + url = HttpServerAddrConfig.getHttpServerAddress() + uri.substring(1); } url = MsbAddrConfig.getMsbAddress() + uri; String urlresult = url.replace("\\", "/"); diff --git a/catalog-core/catalog-mgr/src/test/java/org/openo/commontosca/catalog/wrapper/PackageWrapperTest.java b/catalog-core/catalog-mgr/src/test/java/org/openo/commontosca/catalog/wrapper/PackageWrapperTest.java index 4622542b..d6dc15b9 100644 --- a/catalog-core/catalog-mgr/src/test/java/org/openo/commontosca/catalog/wrapper/PackageWrapperTest.java +++ b/catalog-core/catalog-mgr/src/test/java/org/openo/commontosca/catalog/wrapper/PackageWrapperTest.java @@ -43,6 +43,9 @@ import org.openo.commontosca.catalog.entity.EnumProcessState; import org.openo.commontosca.catalog.entity.EnumUsageState; import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse; import org.openo.commontosca.catalog.entity.response.PackageMeta; +import org.openo.commontosca.catalog.model.parser.EnumPackageFormat; +import org.openo.commontosca.catalog.model.parser.ModelParserFactory; +import org.openo.commontosca.catalog.model.parser.yaml.zte.ToscaYamlModelParser; import java.io.File; import java.io.FileInputStream; @@ -55,19 +58,18 @@ import javax.ws.rs.core.Response; public class PackageWrapperTest { private static String resourcePath; - - static { - MsbAddrConfig.setMsbAddress("http://127.0.0.1:80"); - } static { + MsbAddrConfig.setMsbAddress("http://10.74.44.28:80"); HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080"); - } - - static { HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/"); + + CatalogAppConfiguration configuration = new CatalogAppConfiguration(); + Config.setConfigration(configuration); + Config.getConfigration().setParserType("zte"); + ModelParserFactory.getInstance().put(EnumPackageFormat.TOSCA_YAML, new TestYamlModelParser()); } - + private static PackageManager manager; @@ -135,6 +137,7 @@ public class PackageWrapperTest { e3.printStackTrace(); } } + assertNotNull(result); assertEquals(200, result.getStatus()); assertNotNull(result.getEntity()); -- cgit 1.2.3-korg