2 * Copyright 2017 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.wrapper;
19 import java.io.BufferedInputStream;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
31 import javax.ws.rs.core.HttpHeaders;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.core.Response.Status;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.commons.lang3.StringUtils;
38 import org.eclipse.jetty.http.HttpStatus;
39 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
40 import org.onap.vnfsdk.marketplace.common.CommonConstant;
41 import org.onap.vnfsdk.marketplace.common.CommonErrorResponse;
42 import org.onap.vnfsdk.marketplace.common.FileUtil;
43 import org.onap.vnfsdk.marketplace.common.RestUtil;
44 import org.onap.vnfsdk.marketplace.common.ToolUtil;
45 import org.onap.vnfsdk.marketplace.db.entity.PackageData;
46 import org.onap.vnfsdk.marketplace.db.exception.ErrorCodeException;
47 import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException;
48 import org.onap.vnfsdk.marketplace.db.resource.PackageManager;
49 import org.onap.vnfsdk.marketplace.db.util.MarketplaceDbUtil;
50 import org.onap.vnfsdk.marketplace.entity.request.PackageBasicInfo;
51 import org.onap.vnfsdk.marketplace.entity.response.PackageMeta;
52 import org.onap.vnfsdk.marketplace.entity.response.UploadPackageResponse;
53 import org.onap.vnfsdk.marketplace.filemanage.FileManagerFactory;
54 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingOperResult;
55 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult;
56 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingSteps;
57 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
58 import org.onap.vnfsdk.marketplace.onboarding.hooks.functiontest.FunctionTestExceutor;
59 import org.onap.vnfsdk.marketplace.onboarding.hooks.functiontest.FunctionTestHook;
60 import org.onap.vnfsdk.marketplace.onboarding.hooks.validatelifecycle.ValidateLifecycleTestResponse;
61 import org.onap.vnfsdk.marketplace.onboarding.onboardmanager.OnBoardingHandler;
62 import org.open.infc.grpc.Result;
63 import org.open.infc.grpc.client.OpenRemoteCli;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 public class PackageWrapper {
69 private static PackageWrapper packageWrapper;
71 private static final Logger LOG = LoggerFactory.getLogger(PackageWrapper.class);
73 private static final boolean DISABLE_VALIDATION = true;
75 private static final String FILE_FORMAT = ".csar";
78 * get PackageWrapper instance.
80 * @return package wrapper instance
82 public static PackageWrapper getInstance() {
83 if(packageWrapper == null) {
84 packageWrapper = new PackageWrapper();
86 return packageWrapper;
89 public Response updateValidateStatus(InputStream inputStream) throws IOException {
90 String reqParam = IOUtils.toString(inputStream);
91 LOG.info("updateValidateStatus request param:" + reqParam);
92 if(StringUtils.isBlank(reqParam)) {
93 LOG.error("The updateValidateStatus request params can't be null");
94 return Response.status(Status.EXPECTATION_FAILED).build();
97 ValidateLifecycleTestResponse lyfValidateResp = null;
98 // TBD - Use Gson - jackson has security issue/
100 if(!checkOperationSucess(lyfValidateResp)) {
101 return Response.status(Status.EXPECTATION_FAILED).build();
104 String funcTestResponse = FunctionTestExceutor.executeFunctionTest(reqParam);
105 if(null == funcTestResponse) {
106 return Response.status(Status.EXPECTATION_FAILED).build();
109 if(!funcTestResponse.contains(CommonConstant.SUCCESS_STR)) {
110 return Response.status(Status.EXPECTATION_FAILED).build();
113 return Response.ok().build();
116 private boolean checkOperationSucess(ValidateLifecycleTestResponse lyfValidateResp) {
117 boolean bOperStatus = false;
118 if(null == lyfValidateResp) {
119 LOG.error("ValidateLifecycleTestResponse is NUll !!!");
122 if(lyfValidateResp.getLifecycleStatus().equalsIgnoreCase(CommonConstant.SUCCESS_STR)
123 && lyfValidateResp.getValidateStatus().equalsIgnoreCase(CommonConstant.SUCCESS_STR)) {
124 LOG.error("Lifecycle/Validation Response failed :" + lyfValidateResp.getLifecycleStatus() + File.separator
125 + lyfValidateResp.getValidateStatus());
132 * query package list by condition.
134 * @param name package name
135 * @param provider package provider
136 * @param version package version
137 * @param deletionPending package deletionPending
138 * @param type package type
141 public Response queryPackageListByCond(String name, String provider, String version, String deletionPending,
143 List<PackageData> dbresult = new ArrayList<>();
144 List<PackageMeta> result = new ArrayList<>();
145 LOG.info("query package info.name:" + name + " provider:" + provider + " version" + version + " deletionPending"
146 + deletionPending + " type:" + type);
148 dbresult = PackageManager.getInstance().queryPackage(name, provider, version, deletionPending, type);
149 result = PackageWrapperUtil.packageDataList2PackageMetaList(dbresult);
150 return Response.ok(ToolUtil.objectToString(result)).build();
151 } catch(MarketplaceResourceException e1) {
152 LOG.error("query package by csarId from db error ! ", e1);
153 return RestUtil.getRestException(e1.getMessage());
158 * query package by id.
160 * @param csarId package id
163 public Response queryPackageById(String csarId) {
164 PackageData dbResult = PackageWrapperUtil.getPackageInfoById(csarId);
165 PackageMeta result = PackageWrapperUtil.packageData2PackageMeta(dbResult);
166 return Response.ok(ToolUtil.objectToString(result)).build();
172 * @param uploadedInputStream inputStream
173 * @param fileDetail package detail
174 * @param head http header
176 * @throws Exception e
178 public Response uploadPackage(InputStream uploadedInputStream, FormDataContentDisposition fileDetail,
179 String details, HttpHeaders head) {
180 LOG.info("Upload/Reupload request Received !!!!");
182 String packageId = MarketplaceDbUtil.generateId();
183 return handlePackageUpload(packageId, uploadedInputStream, fileDetail, details, head);
184 } catch(IOException e) {
185 LOG.error("can't get package id", e);
187 return Response.status(Status.INTERNAL_SERVER_ERROR).build();
190 private UploadPackageResponse manageUpload(String packageId, String fileName, String fileLocation, String details,
191 String contentRange) throws ErrorCodeException {
192 String localDirName = ToolUtil.getTempDir(CommonConstant.CATALOG_CSAR_DIR_NAME, fileName);
193 PackageBasicInfo basicInfo = PackageWrapperUtil.getPacageBasicInfo(fileLocation);
194 UploadPackageResponse result = new UploadPackageResponse();
195 Boolean isEnd = PackageWrapperUtil.isUploadEnd(contentRange);
197 PackageMeta packageMeta =
198 PackageWrapperUtil.getPackageMeta(packageId, fileName, fileLocation, basicInfo, details);
200 String path = basicInfo.getType().toString() + File.separator + basicInfo.getProvider() + File.separator
201 + packageMeta.getCsarId() + File.separator + fileName.replace(FILE_FORMAT, "") + File.separator
202 + basicInfo.getVersion();
204 String dowloadUri = File.separator + path + File.separator;
205 packageMeta.setDownloadUri(dowloadUri);
207 LOG.info("dest path is : " + path);
208 LOG.info("packageMeta = " + ToolUtil.objectToString(packageMeta));
210 PackageData packageData = PackageWrapperUtil.getPackageData(packageMeta);
212 List<PackageData> lstPkgData =
213 PackageManager.getInstance().queryPackage(packageMeta.getName(), "", "", "", "");
214 if(!lstPkgData.isEmpty()) {
215 LOG.error("Package name is not unique");
216 throw new ErrorCodeException(HttpStatus.INTERNAL_SERVER_ERROR_500, "Package name already exists");
219 String destPath = File.separator + path + File.separator + File.separator;
220 boolean uploadResult = FileManagerFactory.createFileManager().upload(localDirName, destPath);
222 OnBoradingRequest oOnboradingRequest = new OnBoradingRequest();
223 oOnboradingRequest.setCsarId(packageId);
224 oOnboradingRequest.setPackageName(fileName);
225 oOnboradingRequest.setPackagePath(localDirName);
227 packageData.setCsarId(packageId);
228 packageData.setDownloadCount(-1);
229 PackageData packateDbData = PackageManager.getInstance().addPackage(packageData);
231 LOG.info("Store package data to database succed ! packateDbData = "
232 + ToolUtil.objectToString(packateDbData));
233 LOG.info("upload package file end, fileName:" + fileName);
235 result.setCsarId(packateDbData.getCsarId());
237 addOnBoardingRequest(oOnboradingRequest);
239 LOG.info("OnboradingRequest Data : " + ToolUtil.objectToString(oOnboradingRequest));
241 } catch(NullPointerException e) {
242 LOG.error("Package basicInfo is incorrect ! basicIonfo = " + ToolUtil.objectToString(basicInfo), e);
250 * Interface for Uploading package
253 * @param uploadedInputStream
258 * @throws IOException
259 * @throws MarketplaceResourceException
261 private Response handlePackageUpload(String packageId, InputStream uploadedInputStream,
262 FormDataContentDisposition fileDetail, String details, HttpHeaders head) throws IOException {
263 boolean bResult = handleDataValidate(packageId, uploadedInputStream, fileDetail);
265 LOG.error("Validation of Input received for Package Upload failed !!!");
266 return Response.status(Status.EXPECTATION_FAILED)
267 .entity(new CommonErrorResponse("Input package is empty or exception happened during validation"))
271 String fileName = "temp_" + packageId + FILE_FORMAT;
272 if(null != fileDetail) {
273 LOG.info("the fileDetail = " + ToolUtil.objectToString(fileDetail));
275 fileName = ToolUtil.processFileName(fileDetail.getFileName());
278 String localDirName = ToolUtil.getTempDir(CommonConstant.CATALOG_CSAR_DIR_NAME, fileName);
280 String contentRange = null;
282 contentRange = head.getHeaderString(CommonConstant.HTTP_HEADER_CONTENT_RANGE);
284 LOG.info("store package chunk file, fileName:" + fileName + ",contentRange:" + contentRange);
285 if(ToolUtil.isEmptyString(contentRange)) {
286 int fileSize = uploadedInputStream.available();
287 contentRange = "0-" + fileSize + "/" + fileSize;
290 String fileLocation = ToolUtil.storeChunkFileInLocal(localDirName, fileName, uploadedInputStream);
291 LOG.info("the fileLocation when upload package is :" + fileLocation);
293 uploadedInputStream.close();
295 if (!DISABLE_VALIDATION) {
297 Result result = OpenRemoteCli.run("localhost", 50051, null, Arrays.asList(new String[] { "--product", "onap-vtp", "csar-validate", "--csar", fileLocation, "--format", "json" }));
298 LOG.info("CSAR validation is successful" + result.getOutput());
300 int exitCode = result.getExitCode();
301 String output = result.getOutput();
303 if((exitCode != 0) || !output.contains("\"error\":\"SUCCESS\"")) {
304 LOG.error("Could not validate failed");
305 return Response.status(Status.EXPECTATION_FAILED).entity(new CommonErrorResponse(output))
308 } catch (Exception e) {
309 LOG.error("CSAR validation panicked", e);
310 return Response.serverError().entity(
311 new CommonErrorResponse("Exception occurred while validating csar package:" + e.getMessage()))
316 UploadPackageResponse result = null;
318 result = manageUpload(packageId, fileName, fileLocation, details, contentRange);
319 } catch(ErrorCodeException e) {
320 LOG.error("ErrorCodeException occurs ",e);
321 return Response.status(Status.EXPECTATION_FAILED)
322 .entity(new CommonErrorResponse("Package Name already exists")).build();
325 return Response.ok(ToolUtil.objectToString(result), MediaType.APPLICATION_JSON).build();
327 return Response.serverError().build();
332 * Execute OnBarding request
334 * @param oOnboradingRequest
336 private void addOnBoardingRequest(final OnBoradingRequest oOnboradingRequest) {
337 ExecutorService es = Executors.newFixedThreadPool(CommonConstant.ONBOARDING_THREAD_COUNT);
338 Callable<Integer> callableInteger = () -> {
339 new OnBoardingHandler().handleOnBoardingReq(oOnboradingRequest);
340 return CommonConstant.SUCESS;
342 es.submit(callableInteger);
346 * delete package by package id.
348 * @param csarId package id
351 public Response delPackage(String csarId) {
352 LOG.info("delete package info.csarId:" + csarId);
353 if(ToolUtil.isEmptyString(csarId)) {
354 LOG.error("delete package fail, csarid is null");
355 return Response.serverError().build();
357 deletePackageDataById(csarId);
358 return Response.ok().build();
362 * Delete Package by CSAR ID
366 private void deletePackageDataById(String csarId) {
367 String packagePath = PackageWrapperUtil.getPackagePath(csarId);
368 if(packagePath == null) {
369 LOG.error("package path is null! ");
373 FileManagerFactory.createFileManager().delete(packagePath);
374 // Delete Results Data
375 FileManagerFactory.createFileManager().delete(File.separator + csarId);
377 // delete package data from database
379 PackageManager.getInstance().deletePackage(csarId);
380 } catch(MarketplaceResourceException e1) {
381 LOG.error("delete package by csarId from db error ! " + e1.getMessage(), e1);
386 * download package by package id.
388 * @param csarId package id
391 public Response downloadCsarPackagesById(String csarId) {
392 PackageData packageData = PackageWrapperUtil.getPackageInfoById(csarId);
394 String packageName = packageData.getName();
395 String path = org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath()
396 + File.separatorChar + packageData.getType() + File.separatorChar + packageData.getProvider()
397 + File.separatorChar + packageData.getCsarId() + File.separator + packageName + File.separatorChar
398 + packageData.getVersion() + File.separator + packageName + FILE_FORMAT;
400 LOG.info("downloadCsarPackagesById path is : " + path);
402 File csarFile = new File(path);
403 if(!csarFile.exists()) {
404 return Response.status(Status.INTERNAL_SERVER_ERROR).build();
407 LOG.info("downloadCsarPackagesById ABS path is : " + csarFile.getAbsolutePath());
410 InputStream fis = new BufferedInputStream(new FileInputStream(csarFile.getAbsolutePath()));
411 return Response.ok(fis).header("Content-Disposition", "attachment; filename=\"" + csarFile.getName() + "\"")
413 } catch(Exception e1) {
414 LOG.error("download vnf package fail.", e1);
415 return RestUtil.getRestException(e1.getMessage());
420 * get package file uri.
422 * @param csarId package id
423 * @param relativePath file relative path
426 public Response getCsarFileUri(String csarId) {
427 return downloadCsarPackagesById(csarId);
431 * Interface to Update Download count for CSAR ID
436 public Response updateDwonloadCount(String csarId) {
437 return handleDownladCountUpdate(csarId) ? Response.ok().build()
438 : Response.status(Status.EXPECTATION_FAILED).build();
442 * Handle downlowa count update
447 private boolean handleDownladCountUpdate(String csarId) {
448 boolean bupdateSucess = false;
450 PackageManager.getInstance().updateDownloadCount(csarId);
451 bupdateSucess = true;
452 } catch(Exception exp) {
453 LOG.error("Updating Donwload count failed for Package with ID !!! : " + exp.getMessage(), exp);
455 return bupdateSucess;
459 * Interface to Re upload Package
462 * @param uploadedInputStream
469 public Response reUploadPackage(String csarId, InputStream uploadedInputStream,
470 FormDataContentDisposition fileDetail, String details, HttpHeaders head)
472 LOG.info("Reupload request Received !!!!");
474 // STEP 1: Validate Input Data
475 // ----------------------------
476 boolean bResult = handleDataValidate(csarId, uploadedInputStream, fileDetail);
478 LOG.error("Validation of Input received for Package Upload failed during Reload!!!");
479 return Response.status(Status.EXPECTATION_FAILED).build();
483 // STEP 2: Delete All Package Data based on package id
484 // ----------------------------------------------------
485 deletePackageDataById(csarId);
487 // STEP 3: upload package with same package id
488 // -------------------------------------------
489 return handlePackageUpload(csarId, uploadedInputStream, fileDetail, details, head);
490 } catch(IOException e) {
491 LOG.error("delete package failed", e);
493 return Response.status(Status.INTERNAL_SERVER_ERROR).build();
497 * Interface to get OnBoarding Result by Operation Type
504 public Response getOnBoardingResult(String csarId, String operTypeId, String operId) {
505 LOG.info("getOnBoardingResult request : csarId:" + csarId + " operTypeId:" + operTypeId + " operId:" + operId);
507 PackageData packageData = PackageWrapperUtil.getPackageInfoById(csarId);
508 if(null == packageData) {
509 return Response.status(Response.Status.PRECONDITION_FAILED).build();
512 handleDelayExec(operId);
514 OnBoardingResult oOnBoardingResult = FunctionTestHook.getOnBoardingResult(packageData);
515 if(null == oOnBoardingResult) {
516 return Response.status(Response.Status.PRECONDITION_FAILED).build();
518 filterOnBoardingResultByOperId(oOnBoardingResult, operId);
520 String strResult = ToolUtil.objectToString(oOnBoardingResult);
521 LOG.info("getOnBoardingResult response : " + strResult);
522 return Response.ok(strResult, "application/json").build();
523 } catch(NullPointerException e) {
524 LOG.error("Null param in getOnBoardingResult", e);
525 return Response.status(Response.Status.BAD_REQUEST).build();
529 private void filterOnBoardingResultByOperId(OnBoardingResult oOnBoardingResult, String operId) {
530 if(0 == operId.compareToIgnoreCase("all")) {
533 if(0 == operId.compareToIgnoreCase("download")) {
534 List<OnBoardingOperResult> operResultListTemp = new ArrayList<>();
535 OnBoardingOperResult operResultListTmp = new OnBoardingOperResult();
536 operResultListTmp.setOperId("download");
537 operResultListTmp.setStatus(0);
538 operResultListTemp.add(operResultListTmp);
539 oOnBoardingResult.setOperResult(operResultListTemp);
542 List<OnBoardingOperResult> operResultListOut = new ArrayList<>();
543 List<OnBoardingOperResult> operResultList = oOnBoardingResult.getOperResult();
544 for(OnBoardingOperResult operResult : operResultList) {
545 if(0 == operResult.getOperId().compareToIgnoreCase(operId)) {
546 operResultListOut.add(operResult);
549 oOnBoardingResult.setOperResult(operResultListOut);
553 * Interface to get OnBoarding Status by Operation ID
559 public Response getOperResultByOperTypeId(String csarId, String operTypeId) {
560 LOG.error("getOnBoardingResult request : csarId:" + csarId + " operTypeId:" + operTypeId);
561 if(null == csarId || null == operTypeId || csarId.isEmpty() || operTypeId.isEmpty()) {
562 return Response.status(Status.BAD_REQUEST).build();
565 PackageData packageData = PackageWrapperUtil.getPackageInfoById(csarId);
566 if(null == packageData) {
567 LOG.error("Failed to find package for PackageID:" + csarId);
568 return Response.status(Status.PRECONDITION_FAILED).build();
571 // Get result key to fetch Function Test Results
572 // ---------------------------------------------
573 String strResult = FunctionTestHook.getFuncTestResults(packageData);
574 if(null == strResult) {
575 LOG.error("NULL reponse for getOperResultByOperTypeId response :" + strResult);
576 return Response.status(Status.INTERNAL_SERVER_ERROR).build();
578 LOG.info("getOperResultByOperTypeId response :" + strResult);
579 return Response.ok(strResult, MediaType.APPLICATION_JSON).build();
582 private boolean handleDataValidate(String packageId, InputStream uploadedInputStream,
583 FormDataContentDisposition fileDetail) {
584 boolean bvalidateOk = false;
585 if((null != uploadedInputStream) && (fileDetail != null) && !ToolUtil.isEmptyString(packageId)) {
592 * Interface to get OnBoarding Steps
596 public Response getOnBoardingSteps() {
597 LOG.info("Get OnBoarding Steps request Received !!!");
599 String filePath = org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getAppDeployPath() + File.separator
600 + "generalconfig/OnBoardingSteps.json";
601 LOG.info("Onboarding Steps Json file Path :" + filePath);
603 OnBoardingSteps oOnBoardingSteps =
604 (OnBoardingSteps)FileUtil.readJsonDatafFromFile(filePath, OnBoardingSteps.class);
605 if(null == oOnBoardingSteps) {
606 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
608 String strResult = ToolUtil.objectToString(oOnBoardingSteps);
609 LOG.info("getOnBoardingSteps response :" + strResult);
610 return Response.ok(strResult, MediaType.APPLICATION_JSON).build();
613 private void handleDelayExec(String operId) {
614 if(0 == operId.compareToIgnoreCase(CommonConstant.FunctionTest.FUNCTEST_EXEC)) {
617 } catch(InterruptedException e) {
618 LOG.info("handleDelayExex response : ", e);
619 Thread.currentThread().interrupt();