diff options
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace/src')
3 files changed, 549 insertions, 538 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/CommonConstant.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/CommonConstant.java index e167ae10..c9ea1945 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/CommonConstant.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/CommonConstant.java @@ -13,125 +13,143 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.onap.vnfsdk.marketplace.common; public class CommonConstant { - // Package Status - public static final String PACKAGE_STATUS_DELETING = "deleting"; + // Package Status + public static final String PACKAGE_STATUS_DELETING = "deleting"; - public static final String PACKAGE_STATUS_DELETE_FAIL = "deleteFailed"; + public static final String PACKAGE_STATUS_DELETE_FAIL = "deleteFailed"; - public static final String PACKAGE_XML_FORMAT = "xml"; + public static final String PACKAGE_XML_FORMAT = "xml"; + public static final String PACKAGE_YAML_FORMAT = "yaml"; - public static final String PACKAGE_YAML_FORMAT = "yaml"; + // host image progress - // host image progress + public static final String TOSCA_METADATA = "TOSCA-Metadata"; - public static final String TOSCA_METADATA = "TOSCA-Metadata"; + public static final String CSAR_VERSION_META = "Version"; - public static final String CSAR_VERSION_META = "Version"; + public static final String CSAR_TYPE_META = "Type"; - public static final String CSAR_TYPE_META = "Type"; + public static final String CSAR_PROVIDER_META = "Provider"; - public static final String CSAR_PROVIDER_META = "Provider"; + public static final String DEFINITIONS = "Definitions"; - public static final String DEFINITIONS = "Definitions"; + public static final String CSAR_META = "csar.meta"; - public static final String CSAR_META = "csar.meta"; + public static final String MANIFEST = ".mf"; - public static final String MANIFEST = ".mf"; + public static final String MF_VERSION_META = "vnf_package_version"; - public static final String MF_VERSION_META = "vnf_package_version"; + public static final String MF_PRODUCT_NAME = "vnf_product_name"; - public static final String MF_PRODUCT_NAME = "vnf_product_name"; + public static final String MF_PROVIDER_META = "vnf_provider_id"; - public static final String MF_PROVIDER_META = "vnf_provider_id"; + public static final String MF_RELEASE_DATETIME = "vnf_release_date_time"; - public static final String MF_RELEASE_DATETIME = "vnf_release_date_time"; + public static final String CSAR_SUFFIX = ".csar"; - public static final String CSAR_SUFFIX = ".csar"; + public static final String HTTP_HEADER_CONTENT_RANGE = "Content-Range"; - public static final String HTTP_HEADER_CONTENT_RANGE = "Content-Range"; + public static final String CATALOG_CSAR_DIR_NAME = "/csar"; - public static final String CATALOG_CSAR_DIR_NAME = "/csar"; + public static final String REPORT_CSAR_DIR_NAME = "/reports"; - public static final String REPORT_CSAR_DIR_NAME = "/reports"; + public static final String COMETD_CHANNEL_PACKAGE_DELETE = "/package/delete"; - public static final String COMETD_CHANNEL_PACKAGE_DELETE = "/package/delete"; + public static final String SUCCESS_STR = "SUCCESS"; - public static final String SUCCESS_STR = "SUCCESS"; + public static final int ONBOARDING_THREAD_COUNT = 1; - public static final int ONBOARDING_THREAD_COUNT = 1; - public static final int SUCESS = 0; - public static final int FAILED = -1; + public static final int SUCESS = 0; - public static final String CATALOUGE_UPLOAD_URL = "/onapapi/catalog/v1/csars"; + public static final int FAILED = -1; - private CommonConstant (){ - // Cannot create instance of the class - } + public static final int HTTP_PORT = 8702; - public static class functionTest - { - public static final String FUNCTEST_URL = "/onapapi/vnfsdk/v1/functest/"; - public static final String FUNCTEST_RESULT_URL = "/onapapi/vnfsdk/v1/functest/download/"; - public static final String FUNCTEST_OPERTYPE_ID = "functiontest"; - public static final String FUNCTEST_PACKAGE_EXISTS = "packageExists"; - public static final String FUNCTEST_EXEC = "functestexec"; + public static final int HTTPS_PORT = 8702; - private functionTest() { - } - } + public static final String BASE_URL = "/onapui/vnfmarket"; - public static class HttpContext { + public static final String CATALOUGE_UPLOAD_URL = "/onapapi/catalog/v1/csars"; - public static final String CONTENT_TYPE = "Content-Type"; + private CommonConstant() { + // Cannot create instance of the class + } - public static final String MEDIA_TYPE_JSON = "application/json;charset=UTF-8"; + public static class functionTest { - public static final String URL = "url"; + public static final String FUNCTEST_URL = "/onapapi/vnfsdk/v1/functest/"; - public static final String METHOD_TYPE = "methodType"; + public static final String FUNCTEST_RESULT_URL = "/onapapi/vnfsdk/v1/functest/download/"; - private HttpContext() { - } - } + public static final String FUNCTEST_OPERTYPE_ID = "functiontest"; - public static class LifeCycleTest - { - public static final String LIFECYCLE_TEST_URL = "/onapapi/nslcm/v1/vnfpackage"; - public static final String LIFECYCLE_TEST_OPERTYPE_ID = "lifecycletest"; - public static final String LIFECYCLE_TEST_EXEC = "lifecycleTestexec"; - private LifeCycleTest() { - } - } + public static final String FUNCTEST_PACKAGE_EXISTS = "packageExists"; - public static class MethodType { + public static final String FUNCTEST_EXEC = "functestexec"; - public static final String POST = "post"; + private functionTest() { + } + } - public static final String DELETE = "delete"; + public static class HttpContext { - public static final String PUT = "put"; + public static final String CONTENT_TYPE = "Content-Type"; - public static final String GET = "get"; + public static final String MEDIA_TYPE_JSON = "application/json;charset=UTF-8"; - private MethodType() { - } - } + public static final String URL = "url"; - public static class MsbRegisterCode { + public static final String METHOD_TYPE = "methodType"; - public static final int MSDB_REGISTER_RETRIES = 12; - public static final int MSDB_REGISTER_RETRY_SLEEP = 10000; + private HttpContext() { + } + } - public static final int MSDB_REGISTER_FILE_NOT_EXISTS = 2; - public static final int MSDB_REGISTER_SUCESS = 0; - public static final int MSDB_REGISTER_FAILED = -1; - private MsbRegisterCode() { - } - } -} + public static class LifeCycleTest { + + public static final String LIFECYCLE_TEST_URL = "/onapapi/nslcm/v1/vnfpackage"; + + public static final String LIFECYCLE_TEST_OPERTYPE_ID = "lifecycletest"; + + public static final String LIFECYCLE_TEST_EXEC = "lifecycleTestexec"; + + private LifeCycleTest() { + } + } + + public static class MethodType { + + public static final String POST = "post"; + + public static final String DELETE = "delete"; + + public static final String PUT = "put"; + public static final String GET = "get"; + + private MethodType() { + } + } + + public static class MsbRegisterCode { + + public static final int MSDB_REGISTER_RETRIES = 12; + + public static final int MSDB_REGISTER_RETRY_SLEEP = 10000; + + public static final int MSDB_REGISTER_FILE_NOT_EXISTS = 2; + + public static final int MSDB_REGISTER_SUCESS = 0; + + public static final int MSDB_REGISTER_FAILED = -1; + + private MsbRegisterCode() { + } + } +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java index 7e50eab8..f8f4f543 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.onap.vnfsdk.marketplace.resource; import java.io.IOException; @@ -35,11 +36,16 @@ import javax.ws.rs.core.Response; import org.eclipse.jetty.http.HttpStatus; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataParam; +import org.onap.vnfsdk.marketplace.common.CommonConstant; import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException; +import org.onap.vnfsdk.marketplace.db.resource.PackageManager; import org.onap.vnfsdk.marketplace.entity.response.CsarFileUriResponse; import org.onap.vnfsdk.marketplace.entity.response.PackageMeta; import org.onap.vnfsdk.marketplace.entity.response.UploadPackageResponse; import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult; +import org.onap.vnfsdk.marketplace.rest.RestConstant; +import org.onap.vnfsdk.marketplace.rest.RestResponse; +import org.onap.vnfsdk.marketplace.rest.RestfulClient; import org.onap.vnfsdk.marketplace.wrapper.PackageWrapper; import io.swagger.annotations.Api; @@ -52,7 +58,6 @@ import io.swagger.annotations.ApiResponses; * csar package service. * * @author 10189609 - * */ @Path("/PackageResource") @Api(tags = {"Package Resource"}) @@ -64,78 +69,59 @@ public class PackageResource { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "update error", - response = String.class)}) - public Response updateValidateStatus( - @ApiParam(value = "http request body") @Context HttpServletRequest request, - @ApiParam(value = "http header") @Context HttpHeaders head - ) throws IOException { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "update error", response = String.class)}) + public Response updateValidateStatus(@ApiParam(value = "http request body") @Context HttpServletRequest request, + @ApiParam(value = "http header") @Context HttpHeaders head) throws IOException { InputStream input = request.getInputStream(); return PackageWrapper.getInstance().updateValidateStatus(input); } - @Path("/csars") @GET - @ApiOperation(value = "get csar package list by condition", response = PackageMeta.class, - responseContainer = "List") + @ApiOperation(value = "get csar package list by condition", response = PackageMeta.class, responseContainer = "List") @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", - response = String.class)}) - public Response queryPackageListByCond( - @ApiParam(value = "csar name") @QueryParam("name") String name, @ApiParam( - value = "csar provider") @QueryParam("provider") String provider, @ApiParam( - value = "csar version") @QueryParam("version") String version, @ApiParam( - value = "delay to delete") @QueryParam("deletionPending") String deletionPending, + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class)}) + public Response queryPackageListByCond(@ApiParam(value = "csar name") @QueryParam("name") String name, + @ApiParam(value = "csar provider") @QueryParam("provider") String provider, + @ApiParam(value = "csar version") @QueryParam("version") String version, + @ApiParam(value = "delay to delete") @QueryParam("deletionPending") String deletionPending, @ApiParam(value = "csar type") @QueryParam("type") String type) { - return PackageWrapper.getInstance().queryPackageListByCond(name, provider, version, - deletionPending, type); + return PackageWrapper.getInstance().queryPackageListByCond(name, provider, version, deletionPending, type); } @Path("/csars/{csarId}") @GET - @ApiOperation(value = "get csar package list", response = PackageMeta.class, - responseContainer = "List") + @ApiOperation(value = "get csar package list", response = PackageMeta.class, responseContainer = "List") @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", - response = String.class)}) - public Response queryPackageById( - @ApiParam(value = "csar id") @PathParam("csarId") String csarId) { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class)}) + public Response queryPackageById(@ApiParam(value = "csar id") @PathParam("csarId") String csarId) { return PackageWrapper.getInstance().queryPackageById(csarId); } + @Path("/csars") @POST @ApiOperation(value = "upload csar package", response = UploadPackageResponse.class) @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", - response = String.class)}) + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class)}) public Response uploadPackage( - @ApiParam(value = "file inputstream", - required = true) @FormDataParam("file") InputStream uploadedInputStream,@FormDataParam("params") String details, - @ApiParam(value = "file detail", - required = false) @FormDataParam("file") FormDataContentDisposition fileDetail, - @ApiParam(value = "http header") @Context HttpHeaders head) throws IOException, MarketplaceResourceException { + @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream, + @FormDataParam("params") String details, + @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail, + @ApiParam(value = "http header") @Context HttpHeaders head) + throws IOException, MarketplaceResourceException { return PackageWrapper.getInstance().uploadPackage(uploadedInputStream, fileDetail, details, head); } @@ -143,12 +129,9 @@ public class PackageResource { @DELETE @ApiOperation(value = "delete a package") @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", - response = String.class)}) + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class)}) public Response delPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) { return PackageWrapper.getInstance().delPackage(csarId); } @@ -158,20 +141,16 @@ public class PackageResource { @ApiOperation(value = "get csar file uri by csarId", response = CsarFileUriResponse.class) @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", - response = String.class)}) - public Response getCsarFileUri( - @ApiParam(value = "csar Id", required = true) @PathParam("csarId") String csarId) { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class)}) + public Response getCsarFileUri(@ApiParam(value = "csar Id", required = true) @PathParam("csarId") String csarId) { return PackageWrapper.getInstance().getCsarFileUri(csarId); } @Path("/csars/{csarId}/downloaded") @GET - @ApiOperation(value = "update download count for a package",response = Response.class) + @ApiOperation(value = "update download count for a package", response = Response.class) public Response updateDwonloadCount(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) { return PackageWrapper.getInstance().updateDwonloadCount(csarId); } @@ -182,30 +161,25 @@ public class PackageResource { @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", - response = String.class), - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, - message = "Unprocessable MicroServiceInfo Entity ", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", - response = String.class)}) - public Response reUploadPackage( - @ApiParam(value = "csar Id") @PathParam("csarId") String csarId, - @ApiParam(value = "file inputstream", - required = true) @FormDataParam("file") InputStream uploadedInputStream,@FormDataParam("params") String details, - @ApiParam(value = "file detail", - required = false) @FormDataParam("file") FormDataContentDisposition fileDetail, - @ApiParam(value = "http header") @Context HttpHeaders head) throws IOException, MarketplaceResourceException { - return PackageWrapper.getInstance().reUploadPackage(csarId,uploadedInputStream, fileDetail, details, head); + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class)}) + public Response reUploadPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId, + @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream, + @FormDataParam("params") String details, + @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail, + @ApiParam(value = "http header") @Context HttpHeaders head) + throws IOException, MarketplaceResourceException { + return PackageWrapper.getInstance().reUploadPackage(csarId, uploadedInputStream, fileDetail, details, head); } @Path("/csars/{csarId}/onboardstatus") @GET - @ApiOperation(value="Get VNF OnBoarding Result", response=OnBoardingResult.class) + @ApiOperation(value = "Get VNF OnBoarding Result", response = OnBoardingResult.class) @Produces(MediaType.APPLICATION_JSON) public Response getOnBoardingResult(@ApiParam("csar Id") @PathParam("csarId") String csarId, @ApiParam("operation type") @QueryParam("operTypeId") String operTypeId, - @ApiParam("operation id") @QueryParam("operId") String operId) - { + @ApiParam("operation id") @QueryParam("operId") String operId) { return PackageWrapper.getInstance().getOnBoardingResult(csarId, operTypeId, operId); } @@ -215,16 +189,36 @@ public class PackageResource { @Produces(MediaType.APPLICATION_JSON) public Response getOperStatus(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId, @ApiParam(value = "operation type") @QueryParam("operTypeId") String operTypeId) { - return PackageWrapper.getInstance().getOperResultByOperTypeId(csarId,operTypeId); + return PackageWrapper.getInstance().getOperResultByOperTypeId(csarId, operTypeId); } @Path("/csars/onboardsteps") @GET - @ApiOperation(value="Get VNF OnBoarded Steps", response=Response.class) + @ApiOperation(value = "Get VNF OnBoarded Steps", response = Response.class) @Produces(MediaType.APPLICATION_JSON) - public Response getOnBoardingSteps() - { + public Response getOnBoardingSteps() { return PackageWrapper.getInstance().getOnBoardingSteps(); } -} + @Path("/healthcheck") + @GET + @ApiOperation(value = "Health for VNF Repository", response = Response.class) + @Produces(MediaType.APPLICATION_JSON) + public Response healthCheck() { + + // Step 1: Check whether tomcat server is up + RestResponse resp = RestfulClient.get("127.0.0.1", CommonConstant.HTTP_PORT, CommonConstant.BASE_URL); + if(RestConstant.RESPONSE_CODE_200 != resp.getStatusCode()) { + return Response.serverError().build(); + } + + // Step 2: Check whether postgres database is up + try { + PackageManager.getInstance().queryPackageByCsarId("01"); + } catch(Exception e) { + return Response.serverError().build(); + } + + return Response.ok().build(); + } +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java index ab94e983..3fbf584d 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapperUtil.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.onap.vnfsdk.marketplace.wrapper; import java.io.BufferedReader; @@ -40,400 +41,398 @@ import org.slf4j.LoggerFactory; import com.google.gson.internal.LinkedTreeMap; - public class PackageWrapperUtil { - private static final Logger LOG = LoggerFactory.getLogger(PackageWrapperUtil.class); - - private PackageWrapperUtil() { - } - - public static long getPacakgeSize(String fileLocation) { - File file = new File(fileLocation); - return file.length(); - } - - /** - * change package metadata to fix database. - * @param meta package metadata - * @param details - * @return package data in database - */ - public static PackageData getPackageData(PackageMeta meta) { - PackageData packageData = new PackageData(); - packageData.setCreateTime(meta.getCreateTime()); - packageData.setDeletionPending(String.valueOf(meta.isDeletionPending())); - packageData.setDownloadUri(meta.getDownloadUri()); - packageData.setFormat(meta.getFormat()); - packageData.setModifyTime(meta.getModifyTime()); - packageData.setName(meta.getName()); - packageData.setCsarId(meta.getCsarId()); - packageData.setProvider(meta.getProvider()); - String fileSize = meta.getSize(); - packageData.setSize(fileSize); - packageData.setType(meta.getType()); - packageData.setVersion(meta.getVersion()); - packageData.setDetails(meta.getDetails()); - packageData.setShortDesc(meta.getShortDesc()); - packageData.setRemarks(meta.getRemarks()); - return packageData; - } - - /** - * judge wether is the end of upload package. - * @param contentRange package sise range - * @param csarName package name - * @return boolean - */ - public static boolean isUploadEnd(String contentRange) { - String range = contentRange; - range = range.replace("bytes", "").trim(); - range = range.substring(0, range.indexOf("/")); - String size = - contentRange.substring(contentRange.indexOf("/") + 1, contentRange.length()).trim(); - int fileSize = Integer.parseInt(size); - String[] ranges = range.split("-"); - int endPosition = Integer.parseInt(ranges[1]) + 1; - if (endPosition >= fileSize) { - return true; + + private static final Logger LOG = LoggerFactory.getLogger(PackageWrapperUtil.class); + + private PackageWrapperUtil() { } - return false; - } - - /** - * get package detail by package id. - * @param csarId package id - * @return package detail - */ - public static PackageData getPackageInfoById(String csarId) { - PackageData result = new PackageData(); - List<PackageData> packageDataList = new ArrayList<>(); - try { - packageDataList = PackageManager.getInstance().queryPackageByCsarId(csarId); - if (packageDataList != null && ! packageDataList.isEmpty()) { - result = PackageManager.getInstance().queryPackageByCsarId(csarId).get(0); - } - } catch (MarketplaceResourceException e1) { - LOG.error("query package by csarId from db error ! " + e1.getMessage(), e1); + + public static long getPacakgeSize(String fileLocation) { + File file = new File(fileLocation); + return file.length(); } - return result; - } - - /** - * get package metadata from basic info. - * @param fileName package name - * @param fileLocation the location of package - * @param basic basic infomation of package. include version, type and provider - * @return package metadata - */ - public static PackageMeta getPackageMeta(String packageId, String fileName, String fileLocation, - PackageBasicInfo basic, String details) { - PackageMeta packageMeta = new PackageMeta(); - long size = getPacakgeSize(fileLocation); - packageMeta.setFormat(basic.getFormat()); - String usedPackageId = packageId; - if(null == packageId) - { - usedPackageId = ToolUtil.generateId(); + + /** + * change package metadata to fix database. + * + * @param meta package metadata + * @param details + * @return package data in database + */ + public static PackageData getPackageData(PackageMeta meta) { + PackageData packageData = new PackageData(); + packageData.setCreateTime(meta.getCreateTime()); + packageData.setDeletionPending(String.valueOf(meta.isDeletionPending())); + packageData.setDownloadUri(meta.getDownloadUri()); + packageData.setFormat(meta.getFormat()); + packageData.setModifyTime(meta.getModifyTime()); + packageData.setName(meta.getName()); + packageData.setCsarId(meta.getCsarId()); + packageData.setProvider(meta.getProvider()); + String fileSize = meta.getSize(); + packageData.setSize(fileSize); + packageData.setType(meta.getType()); + packageData.setVersion(meta.getVersion()); + packageData.setDetails(meta.getDetails()); + packageData.setShortDesc(meta.getShortDesc()); + packageData.setRemarks(meta.getRemarks()); + return packageData; } - packageMeta.setCsarId(usedPackageId); - - packageMeta.setName(fileName.replace(CommonConstant.CSAR_SUFFIX, "")); - packageMeta.setType(basic.getType().toString()); - packageMeta.setVersion(basic.getVersion()); - packageMeta.setProvider(basic.getProvider()); - packageMeta.setDeletionPending(false); - String sizeStr = ToolUtil.getFormatFileSize(size); - packageMeta.setSize(sizeStr); - SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String currentTime = sdf1.format(new Date()); - packageMeta.setCreateTime(currentTime); - packageMeta.setModifyTime(currentTime); - if(null != details) - { - LinkedTreeMap<String,String> csarDetails = ToolUtil.fromJson(details, LinkedTreeMap.class); - packageMeta.setDetails(csarDetails.get("details")); - packageMeta.setShortDesc(csarDetails.get("shortDesc")); - packageMeta.setRemarks(csarDetails.get("remarks")); + /** + * judge wether is the end of upload package. + * + * @param contentRange package sise range + * @param csarName package name + * @return boolean + */ + public static boolean isUploadEnd(String contentRange) { + String range = contentRange; + range = range.replace("bytes", "").trim(); + range = range.substring(0, range.indexOf("/")); + String size = contentRange.substring(contentRange.indexOf("/") + 1, contentRange.length()).trim(); + int fileSize = Integer.parseInt(size); + String[] ranges = range.split("-"); + int endPosition = Integer.parseInt(ranges[1]) + 1; + if(endPosition >= fileSize) { + return true; + } + return false; } - return packageMeta; - } - - /** - * get downloadUri from package metadata. - * @param csarId package id - * @return download uri - */ - public static String getPackagePath(String csarId) { - List<PackageData> packageList = new ArrayList<>(); - String downloadUri = null; - try { - packageList = PackageManager.getInstance().queryPackageByCsarId(csarId); - downloadUri = packageList.get(0).getDownloadUri(); - } catch (MarketplaceResourceException e1) { - LOG.error("Query CSAR package by ID failed ! csarId = " + csarId, e1); + + /** + * get package detail by package id. + * + * @param csarId package id + * @return package detail + */ + public static PackageData getPackageInfoById(String csarId) { + PackageData result = new PackageData(); + List<PackageData> packageDataList = new ArrayList<>(); + try { + packageDataList = PackageManager.getInstance().queryPackageByCsarId(csarId); + if(packageDataList != null && !packageDataList.isEmpty()) { + result = packageDataList.get(0); + } + } catch(MarketplaceResourceException e1) { + LOG.error("query package by csarId from db error ! " + e1.getMessage(), e1); + } + return result; } - return downloadUri; - } - - - /** - * get package name from ftpUrl. - * @param ftpUrl ftp url - * @return package name - */ - public static String getPackageName(String ftpUrl) { - int index = ftpUrl.lastIndexOf("/"); - - return ftpUrl.substring(index); - } - - /** - * translate package data from database to package metadata. - * @param dbResult data from database - * @return package metadata list - */ - public static List<PackageMeta> packageDataList2PackageMetaList( - List<PackageData> dbResult) { - ArrayList<PackageMeta> metas = new ArrayList<>(); - if (! dbResult.isEmpty()) { - for (int i = 0; i < dbResult.size(); i++) { - PackageData data = dbResult.get(i); - PackageMeta meta = packageData2PackageMeta(data); - metas.add(meta); - } + + /** + * get package metadata from basic info. + * + * @param fileName package name + * @param fileLocation the location of package + * @param basic basic infomation of package. include version, type and provider + * @return package metadata + */ + public static PackageMeta getPackageMeta(String packageId, String fileName, String fileLocation, + PackageBasicInfo basic, String details) { + PackageMeta packageMeta = new PackageMeta(); + long size = getPacakgeSize(fileLocation); + packageMeta.setFormat(basic.getFormat()); + String usedPackageId = packageId; + if(null == packageId) { + usedPackageId = ToolUtil.generateId(); + } + + packageMeta.setCsarId(usedPackageId); + + packageMeta.setName(fileName.replace(CommonConstant.CSAR_SUFFIX, "")); + packageMeta.setType(basic.getType().toString()); + packageMeta.setVersion(basic.getVersion()); + packageMeta.setProvider(basic.getProvider()); + packageMeta.setDeletionPending(false); + String sizeStr = ToolUtil.getFormatFileSize(size); + packageMeta.setSize(sizeStr); + SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String currentTime = sdf1.format(new Date()); + packageMeta.setCreateTime(currentTime); + packageMeta.setModifyTime(currentTime); + if(null != details) { + LinkedTreeMap<String, String> csarDetails = ToolUtil.fromJson(details, LinkedTreeMap.class); + packageMeta.setDetails(csarDetails.get("details")); + packageMeta.setShortDesc(csarDetails.get("shortDesc")); + packageMeta.setRemarks(csarDetails.get("remarks")); + } + return packageMeta; } - return metas; - } - - public static PackageMeta packageData2PackageMeta(PackageData packageData) { - PackageMeta meta = new PackageMeta(); - meta.setCsarId(packageData.getCsarId()); - meta.setCreateTime(packageData.getCreateTime()); - meta.setDeletionPending(Boolean.getBoolean(packageData.getDeletionPending())); - String packageUri = - packageData.getDownloadUri() + packageData.getName() + CommonConstant.CSAR_SUFFIX; - String packageUrl = getUrl(packageUri); - meta.setDownloadUri(packageUrl); - meta.setReport(packageData.getReport()); - meta.setFormat(packageData.getFormat()); - meta.setModifyTime(packageData.getModifyTime()); - meta.setName(packageData.getName()); - meta.setDetails(packageData.getDetails()); - meta.setProvider(packageData.getProvider()); - meta.setSize(packageData.getSize()); - meta.setType(packageData.getType()); - meta.setShortDesc(packageData.getShortDesc()); - meta.setVersion(packageData.getVersion()); - meta.setRemarks(packageData.getRemarks()); - meta.setDownloadCount(packageData.getDownloadCount()); - return meta; - } - - /** - * add msb address as prefix to uri. - * @param uri uri - * @return url - */ - public static String getUrl(String uri) { - String url = getDownloadUriHead(); - if (url.endsWith("/") && uri.startsWith("/")) { - url += uri.substring(1); - } else { - url += uri; + + /** + * get downloadUri from package metadata. + * + * @param csarId package id + * @return download uri + */ + public static String getPackagePath(String csarId) { + List<PackageData> packageList = new ArrayList<>(); + String downloadUri = null; + try { + packageList = PackageManager.getInstance().queryPackageByCsarId(csarId); + downloadUri = packageList.get(0).getDownloadUri(); + } catch(MarketplaceResourceException e1) { + LOG.error("Query CSAR package by ID failed ! csarId = " + csarId, e1); + } + return downloadUri; } - return url.replace("\\", "/"); - } - - public static String getDownloadUriHead() { - return MsbAddrConfig.getMsbAddress() + "/files/catalog-http"; - } - - /** - * get local path. - * @param uri uri - * @return local path - */ - public static String getLocalPath(String uri) { - File srcDir = new File(uri); - String localPath = srcDir.getAbsolutePath(); - return localPath.replace("\\", "/"); - } - - /** - * get package basic information. - * @param fileLocation package location - * @return package basic information - */ - public static PackageBasicInfo getPacageBasicInfo(String fileLocation) { - PackageBasicInfo basicInfo = new PackageBasicInfo(); - String unzipDir = ToolUtil.getUnzipDir(fileLocation); - boolean isXmlCsar = false; - try { - String tempfolder = unzipDir; - List<String> unzipFiles = FileUtil.unzip(fileLocation, tempfolder); - if (unzipFiles.isEmpty()) { - isXmlCsar = true; - } - for (String unzipFile : unzipFiles) { - if (unzipFile.endsWith(CommonConstant.MANIFEST)) { - basicInfo = readManifest(unzipFile); + + /** + * get package name from ftpUrl. + * + * @param ftpUrl ftp url + * @return package name + */ + public static String getPackageName(String ftpUrl) { + int index = ftpUrl.lastIndexOf("/"); + + return ftpUrl.substring(index); + } + + /** + * translate package data from database to package metadata. + * + * @param dbResult data from database + * @return package metadata list + */ + public static List<PackageMeta> packageDataList2PackageMetaList(List<PackageData> dbResult) { + ArrayList<PackageMeta> metas = new ArrayList<>(); + if(!dbResult.isEmpty()) { + for(int i = 0; i < dbResult.size(); i++) { + PackageData data = dbResult.get(i); + PackageMeta meta = packageData2PackageMeta(data); + metas.add(meta); + } } - - if (unzipFile.endsWith(CommonConstant.CSAR_META)) { - basicInfo = readMetaData(unzipFile); + return metas; + } + + public static PackageMeta packageData2PackageMeta(PackageData packageData) { + PackageMeta meta = new PackageMeta(); + meta.setCsarId(packageData.getCsarId()); + meta.setCreateTime(packageData.getCreateTime()); + meta.setDeletionPending(Boolean.getBoolean(packageData.getDeletionPending())); + String packageUri = packageData.getDownloadUri() + packageData.getName() + CommonConstant.CSAR_SUFFIX; + String packageUrl = getUrl(packageUri); + meta.setDownloadUri(packageUrl); + meta.setReport(packageData.getReport()); + meta.setFormat(packageData.getFormat()); + meta.setModifyTime(packageData.getModifyTime()); + meta.setName(packageData.getName()); + meta.setDetails(packageData.getDetails()); + meta.setProvider(packageData.getProvider()); + meta.setSize(packageData.getSize()); + meta.setType(packageData.getType()); + meta.setShortDesc(packageData.getShortDesc()); + meta.setVersion(packageData.getVersion()); + meta.setRemarks(packageData.getRemarks()); + meta.setDownloadCount(packageData.getDownloadCount()); + return meta; + } + + /** + * add msb address as prefix to uri. + * + * @param uri uri + * @return url + */ + public static String getUrl(String uri) { + String url = getDownloadUriHead(); + if(url.endsWith("/") && uri.startsWith("/")) { + url += uri.substring(1); + } else { + url += uri; } - - if (ToolUtil.isYamlFile(new File(unzipFile))) { - isXmlCsar = false; + return url.replace("\\", "/"); + } + + public static String getDownloadUriHead() { + return MsbAddrConfig.getMsbAddress() + "/files/catalog-http"; + } + + /** + * get local path. + * + * @param uri uri + * @return local path + */ + public static String getLocalPath(String uri) { + File srcDir = new File(uri); + String localPath = srcDir.getAbsolutePath(); + return localPath.replace("\\", "/"); + } + + /** + * get package basic information. + * + * @param fileLocation package location + * @return package basic information + */ + public static PackageBasicInfo getPacageBasicInfo(String fileLocation) { + PackageBasicInfo basicInfo = new PackageBasicInfo(); + String unzipDir = ToolUtil.getUnzipDir(fileLocation); + boolean isXmlCsar = false; + try { + String tempfolder = unzipDir; + List<String> unzipFiles = FileUtil.unzip(fileLocation, tempfolder); + if(unzipFiles.isEmpty()) { + isXmlCsar = true; + } + for(String unzipFile : unzipFiles) { + if(unzipFile.endsWith(CommonConstant.MANIFEST)) { + basicInfo = readManifest(unzipFile); + } + + if(unzipFile.endsWith(CommonConstant.CSAR_META)) { + basicInfo = readMetaData(unzipFile); + } + + if(ToolUtil.isYamlFile(new File(unzipFile))) { + isXmlCsar = false; + } + } + } catch(IOException e1) { + LOG.error("judge package type error ! " + e1.getMessage(), e1); } - } - } catch (IOException e1) { - LOG.error("judge package type error ! " + e1.getMessage(), e1); + if(isXmlCsar) { + basicInfo.setFormat(CommonConstant.PACKAGE_XML_FORMAT); + } else { + basicInfo.setFormat(CommonConstant.PACKAGE_YAML_FORMAT); + } + return basicInfo; } - if (isXmlCsar) { - basicInfo.setFormat(CommonConstant.PACKAGE_XML_FORMAT); - } else { - basicInfo.setFormat(CommonConstant.PACKAGE_YAML_FORMAT); + + /** + * Reads the manifest file in the package and fills the basic infor about package + * + * @param unzipFile + * @return basic infor about package + */ + private static PackageBasicInfo readMetaData(String unzipFile) { + + // Fix the package type to CSAR, temporary + PackageBasicInfo basicInfo = new PackageBasicInfo(); + basicInfo.setType(EnumType.CSAR); + + File file = new File(unzipFile); + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + + for(String tempString; (tempString = reader.readLine()) != null;) { + // If line is empty, ignore + if("".equals(tempString)) { + continue; + } + + int count1 = tempString.indexOf(":"); + String meta = tempString.substring(0, count1).trim(); + + // Check for the package provider name + if(meta.equalsIgnoreCase(CommonConstant.CSAR_PROVIDER_META)) { + int count = tempString.indexOf(":") + 1; + basicInfo.setProvider(tempString.substring(count).trim()); + } + + // Check for package version + if(meta.equalsIgnoreCase(CommonConstant.CSAR_VERSION_META)) { + int count = tempString.indexOf(":") + 1; + basicInfo.setVersion(tempString.substring(count).trim()); + } + + // Check for package type + if(meta.equalsIgnoreCase(CommonConstant.CSAR_TYPE_META)) { + int count = tempString.indexOf(":") + 1; + + basicInfo.setType(getEnumType(tempString.substring(count).trim())); + } + } + + reader.close(); + } catch(IOException e) { + LOG.error("Exception while parsing manifest file" + e, e); + } + + return basicInfo; + } + + private static EnumType getEnumType(String type) { + EnumType vnfType = EnumType.CSAR; + if(type == "CSAR") { + vnfType = EnumType.CSAR; + } + + if(type == "GSAR") { + vnfType = EnumType.GSAR; + } + + if(type == "NSAR") { + vnfType = EnumType.NSAR; + } + + if(type == "SSAR") { + vnfType = EnumType.SSAR; + } + + if(type == "NFAR") { + vnfType = EnumType.NFAR; + } + + return vnfType; } - return basicInfo; - } - - /** - * Reads the manifest file in the package and fills the basic infor about package - * @param unzipFile - * @return basic infor about package - */ - private static PackageBasicInfo readMetaData(String unzipFile) { - - // Fix the package type to CSAR, temporary - PackageBasicInfo basicInfo = new PackageBasicInfo(); - basicInfo.setType(EnumType.CSAR); - - File file = new File(unzipFile); - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - - for(String tempString; (tempString = reader.readLine()) != null;) - { - // If line is empty, ignore - if ("".equals(tempString)) { - continue; - } - - int count1 = tempString.indexOf(":"); - String meta = tempString.substring(0, count1).trim(); - - // Check for the package provider name - if (meta.equalsIgnoreCase(CommonConstant.CSAR_PROVIDER_META)) { - int count = tempString.indexOf(":") + 1; - basicInfo.setProvider(tempString.substring(count).trim()); - } - - // Check for package version - if (meta.equalsIgnoreCase(CommonConstant.CSAR_VERSION_META)) { - int count = tempString.indexOf(":") + 1; - basicInfo.setVersion(tempString.substring(count).trim()); - } - - // Check for package type - if (meta.equalsIgnoreCase(CommonConstant.CSAR_TYPE_META)) { - int count = tempString.indexOf(":") + 1; - - basicInfo.setType(getEnumType(tempString.substring(count).trim())); - } - } - - reader.close(); - } catch (IOException e) { - LOG.error("Exception while parsing manifest file" + e, e); + + private static PackageBasicInfo readManifest(String unzipFile) { + + // Fix the package type to CSAR, temporary + PackageBasicInfo basicInfo = new PackageBasicInfo(); + basicInfo.setType(EnumType.CSAR); + + File file = new File(unzipFile); + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + + for(String tempString; (tempString = reader.readLine()) != null;) { + // If line is empty, ignore + if("".equals(tempString)) { + continue; + } + + int count1 = tempString.indexOf(":"); + String meta = tempString.substring(0, count1).trim(); + + // Check for the package provider name + if(meta.equalsIgnoreCase(CommonConstant.MF_PROVIDER_META)) { + int count = tempString.indexOf(":") + 1; + basicInfo.setProvider(tempString.substring(count).trim()); + } + + // Check for package version + if(meta.equalsIgnoreCase(CommonConstant.MF_VERSION_META)) { + int count = tempString.indexOf(":") + 1; + basicInfo.setVersion(tempString.substring(count).trim()); + } + } + + reader.close(); + } catch(IOException e) { + LOG.error("Exception while parsing manifest file" + e, e); + } + + return basicInfo; } - return basicInfo; - } - - private static EnumType getEnumType (String type) - { - EnumType vnfType = EnumType.CSAR; - if (type == "CSAR") - { - vnfType = EnumType.CSAR; - } - - if (type == "GSAR") - { - vnfType = EnumType.GSAR; - } - - if (type == "NSAR") - { - vnfType = EnumType.NSAR; - } - - if (type == "SSAR") - { - vnfType = EnumType.SSAR; - } - - if (type == "NFAR") - { - vnfType = EnumType.NFAR; - } - - return vnfType; - } - - private static PackageBasicInfo readManifest(String unzipFile) { - - // Fix the package type to CSAR, temporary - PackageBasicInfo basicInfo = new PackageBasicInfo(); - basicInfo.setType(EnumType.CSAR); - - File file = new File(unzipFile); - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - - for(String tempString; (tempString = reader.readLine()) != null;) - { - // If line is empty, ignore - if ("".equals(tempString)) { - continue; - } - - int count1 = tempString.indexOf(":"); - String meta = tempString.substring(0, count1).trim(); - - // Check for the package provider name - if (meta.equalsIgnoreCase(CommonConstant.MF_PROVIDER_META)) { - int count = tempString.indexOf(":") + 1; - basicInfo.setProvider(tempString.substring(count).trim()); - } - - // Check for package version - if (meta.equalsIgnoreCase(CommonConstant.MF_VERSION_META)) { - int count = tempString.indexOf(":") + 1; - basicInfo.setVersion(tempString.substring(count).trim()); - } - } - - reader.close(); - } catch (IOException e) { - LOG.error("Exception while parsing manifest file" + e, e); - } - - return basicInfo; - } - /** - * get package format enum. - * @param format package format - * @return package format enum - */ - public static EnumPackageFormat getPackageFormat(String format) { - if ("xml".equals(format)) { - return EnumPackageFormat.TOSCA_XML; - } else if ("yml".equals(format) || "yaml".equals(format)) { - return EnumPackageFormat.TOSCA_YAML; - } else { - return null; + /** + * get package format enum. + * + * @param format package format + * @return package format enum + */ + public static EnumPackageFormat getPackageFormat(String format) { + if("xml".equals(format)) { + return EnumPackageFormat.TOSCA_XML; + } else if("yml".equals(format) || "yaml".equals(format)) { + return EnumPackageFormat.TOSCA_YAML; + } else { + return null; + } } - } } - |