2  * Copyright 2017-18 Huawei Technologies Co., Ltd.
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  17 package org.onap.vnfsdk.marketplace.resource;
 
  19 import java.io.IOException;
 
  20 import java.io.InputStream;
 
  22 import javax.servlet.http.HttpServletRequest;
 
  23 import javax.ws.rs.Consumes;
 
  24 import javax.ws.rs.DELETE;
 
  25 import javax.ws.rs.GET;
 
  26 import javax.ws.rs.POST;
 
  27 import javax.ws.rs.Path;
 
  28 import javax.ws.rs.PathParam;
 
  29 import javax.ws.rs.Produces;
 
  30 import javax.ws.rs.QueryParam;
 
  31 import javax.ws.rs.core.Context;
 
  32 import javax.ws.rs.core.HttpHeaders;
 
  33 import javax.ws.rs.core.MediaType;
 
  34 import javax.ws.rs.core.Response;
 
  36 import org.eclipse.jetty.http.HttpStatus;
 
  37 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
 
  38 import org.glassfish.jersey.media.multipart.FormDataParam;
 
  39 import org.onap.vnfsdk.marketplace.db.connection.ConnectionUtil;
 
  40 import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException;
 
  41 import org.onap.vnfsdk.marketplace.entity.response.CsarFileUriResponse;
 
  42 import org.onap.vnfsdk.marketplace.entity.response.PackageMeta;
 
  43 import org.onap.vnfsdk.marketplace.entity.response.UploadPackageResponse;
 
  44 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult;
 
  45 import org.onap.vnfsdk.marketplace.wrapper.PackageWrapper;
 
  47 import io.swagger.annotations.Api;
 
  48 import io.swagger.annotations.ApiOperation;
 
  49 import io.swagger.annotations.ApiParam;
 
  50 import io.swagger.annotations.ApiResponse;
 
  51 import io.swagger.annotations.ApiResponses;
 
  54 @Path("/PackageResource")
 
  55 @Api(tags = { "Package Resource" })
 
  56 public class PackageResource {
 
  58     @Path("/updatestatus")
 
  60     @ApiOperation(value = "update validate and lifecycle test status", response = UploadPackageResponse.class)
 
  61     @Consumes(MediaType.APPLICATION_JSON)
 
  62     @Produces(MediaType.APPLICATION_JSON)
 
  63     @ApiResponses(value = {
 
  64             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
  65             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
  66             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "update  error", response = String.class) })
 
  67     public Response updateValidateStatus(@ApiParam(value = "http request body") @Context HttpServletRequest request,
 
  68             @ApiParam(value = "http header") @Context HttpHeaders head) throws IOException {
 
  69         InputStream input = request.getInputStream();
 
  70         return PackageWrapper.getInstance().updateValidateStatus(input);
 
  76     @ApiOperation(value = "get csar package list by condition", response = PackageMeta.class, responseContainer = "List")
 
  77     @Produces(MediaType.APPLICATION_JSON)
 
  78     @ApiResponses(value = {
 
  79             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
  80             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
  81             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
 
  82     public Response queryPackageListByCond(@ApiParam(value = "csar name") @QueryParam("name") String name,
 
  83             @ApiParam(value = "csar provider") @QueryParam("provider") String provider,
 
  84             @ApiParam(value = "csar version") @QueryParam("version") String version,
 
  85             @ApiParam(value = "delay to delete") @QueryParam("deletionPending") String deletionPending,
 
  86             @ApiParam(value = "csar type") @QueryParam("type") String type) {
 
  87         return PackageWrapper.getInstance().queryPackageListByCond(name, provider, version, deletionPending, type);
 
  90     @Path("/csars/{csarId}")
 
  92     @ApiOperation(value = "get csar package list", response = PackageMeta.class, responseContainer = "List")
 
  93     @Produces(MediaType.APPLICATION_JSON)
 
  94     @ApiResponses(value = {
 
  95             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
  96             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
  97             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
 
  98     public Response queryPackageById(@ApiParam(value = "csar id") @PathParam("csarId") String csarId) {
 
  99         return PackageWrapper.getInstance().queryPackageById(csarId);
 
 104     @ApiOperation(value = "upload csar package", response = UploadPackageResponse.class)
 
 105     @Consumes(MediaType.MULTIPART_FORM_DATA)
 
 106     @Produces(MediaType.APPLICATION_JSON)
 
 107     @ApiResponses(value = {
 
 108             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
 109             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
 110             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
 
 111     public Response uploadPackage(
 
 112             @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream,
 
 113             @FormDataParam("params") String details,
 
 114             @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail,
 
 115             @ApiParam(value = "http header") @Context HttpHeaders head)
 
 116             throws IOException, MarketplaceResourceException {
 
 117         return PackageWrapper.getInstance().uploadPackage(uploadedInputStream, fileDetail, details, head);
 
 120     @Path("/csars/{csarId}")
 
 122     @ApiOperation(value = "delete a package")
 
 123     @ApiResponses(value = {
 
 124             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
 125             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
 126             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
 
 127     public Response delPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) {
 
 128         return PackageWrapper.getInstance().delPackage(csarId);
 
 131     @Path("/csars/{csarId}/files")
 
 133     @ApiOperation(value = "get csar file uri by csarId", response = CsarFileUriResponse.class)
 
 134     @Produces(MediaType.APPLICATION_JSON)
 
 135     @ApiResponses(value = {
 
 136             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
 137             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
 138             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
 
 139     public Response getCsarFileUri(@ApiParam(value = "csar Id", required = true) @PathParam("csarId") String csarId) {
 
 140         return PackageWrapper.getInstance().getCsarFileUri(csarId);
 
 143     @Path("/csars/{csarId}/downloaded")
 
 145     @ApiOperation(value = "update download count for a package", response = Response.class)
 
 146     public Response updateDwonloadCount(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) {
 
 147         return PackageWrapper.getInstance().updateDwonloadCount(csarId);
 
 150     @Path("/csars/{csarId}/reupload")
 
 152     @ApiOperation(value = "re-upload csar package", response = UploadPackageResponse.class)
 
 153     @Consumes(MediaType.MULTIPART_FORM_DATA)
 
 154     @Produces(MediaType.APPLICATION_JSON)
 
 155     @ApiResponses(value = {
 
 156             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
 
 157             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
 
 158             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
 
 159     public Response reUploadPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId,
 
 160             @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream,
 
 161             @FormDataParam("params") String details,
 
 162             @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail,
 
 163             @ApiParam(value = "http header") @Context HttpHeaders head)
 
 164             throws IOException, MarketplaceResourceException {
 
 165         return PackageWrapper.getInstance().reUploadPackage(csarId, uploadedInputStream, fileDetail, details, head);
 
 168     @Path("/csars/{csarId}/onboardstatus")
 
 170     @ApiOperation(value = "Get VNF OnBoarding Result", response = OnBoardingResult.class)
 
 171     @Produces(MediaType.APPLICATION_JSON)
 
 172     public Response getOnBoardingResult(@ApiParam("csar Id") @PathParam("csarId") String csarId,
 
 173             @ApiParam("operation type") @QueryParam("operTypeId") String operTypeId,
 
 174             @ApiParam("operation id") @QueryParam("operId") String operId) {
 
 175         return PackageWrapper.getInstance().getOnBoardingResult(csarId, operTypeId, operId);
 
 178     @Path("/csars/{csarId}/operresult")
 
 180     @ApiOperation(value = "Get VNF OnBoarded Opeartion Result", response = Response.class)
 
 181     @Produces(MediaType.APPLICATION_JSON)
 
 182     public Response getOperStatus(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId,
 
 183             @ApiParam(value = "operation type") @QueryParam("operTypeId") String operTypeId) {
 
 184         return PackageWrapper.getInstance().getOperResultByOperTypeId(csarId, operTypeId);
 
 187     @Path("/csars/onboardsteps")
 
 189     @ApiOperation(value = "Get VNF OnBoarded Steps", response = Response.class)
 
 190     @Produces(MediaType.APPLICATION_JSON)
 
 191     public Response getOnBoardingSteps() {
 
 192         return PackageWrapper.getInstance().getOnBoardingSteps();
 
 195     @Path("/healthcheck")
 
 197     @ApiOperation(value = "Health for VNF Repository", response = Response.class)
 
 198     @Produces(MediaType.APPLICATION_JSON)
 
 199     public Response healthCheck() {
 
 200         if (ConnectionUtil.getSession() == null){
 
 201             return Response.serverError().build();
 
 204         return Response.ok().build();