1 package org.onap.sdc.dcae.composition.impl;
3 import org.apache.commons.lang.StringUtils;
4 import org.onap.sdc.common.onaplog.Enums.LogLevel;
5 import org.onap.sdc.dcae.catalog.asdc.ASDC;
6 import org.onap.sdc.dcae.catalog.asdc.ASDCUtils;
7 import org.onap.sdc.dcae.catalog.asdc.Blueprinter;
8 import org.onap.sdc.dcae.composition.restmodels.MessageResponse;
9 import org.onap.sdc.dcae.composition.restmodels.VfcmtData;
10 import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
11 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
12 import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
13 import org.onap.sdc.dcae.composition.util.SystemProperties;
14 import org.onap.sdc.dcae.errormng.ActionStatus;
15 import org.onap.sdc.dcae.errormng.ErrConfMgr;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.http.HttpStatus;
18 import org.springframework.http.ResponseEntity;
19 import org.springframework.stereotype.Component;
21 import javax.annotation.PostConstruct;
22 import java.io.StringReader;
26 public class BlueprintBusinessLogic extends BaseBusinessLogic {
29 private Blueprinter blueprinter;
33 private SystemProperties systemProperties;
34 @Autowired private CompositionBusinessLogic compositionBusinessLogic;
39 URI sdcUri = URI.create(systemProperties.getProperties().getProperty(DcaeBeConstants.Config.URI));
41 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "SDC uri: {}", sdcUri);
44 public ResponseEntity generateAndSaveBlueprint(String userId, String context, String vfcmtUuid, String serviceUuid, String vfiName, String flowType, String requestId) {
46 // prepare - fetch vfcmt and cdump
47 ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
48 Artifact cdumpArtifactData = fetchCdump(vfcmt, requestId);
49 if (null == cdumpArtifactData) {
50 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Composition not found on vfcmt {}", vfcmtUuid);
51 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.MISSING_TOSCA_FILE, "", vfcmt.getName());
53 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Found the cdump (composition.yml) on top of VFCMT {}", vfcmtUuid);
54 String cdump = cdumpArtifactData.getPayloadData();
55 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to use python procedure to create a blueprint....");
56 String resultBlueprintCreation = generateBlueprintViaToscaLab(cdump);
57 if (StringUtils.isEmpty(resultBlueprintCreation)) {
58 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error occurred during blueprint generation");
59 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.GENERATE_BLUEPRINT_ERROR, "", vfcmt.getName());
61 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "BLUEPRINT:\n{}", resultBlueprintCreation);
63 // 1806 US374595 flowType in cdump
64 String flowTypeFromCdump = extractFlowTypeFromCdump(cdump);
66 // support backward compatibility
67 if(StringUtils.isBlank(flowTypeFromCdump)) {
68 flowTypeFromCdump = flowType;
71 VfcmtData vfcmtData = new VfcmtData(vfcmt, vfiName, flowTypeFromCdump, serviceUuid);
72 Artifact blueprintArtifactResult = compositionBusinessLogic.submitComposition(userId, context, vfcmtData, resultBlueprintCreation, requestId);
73 if (null == blueprintArtifactResult) {
74 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.SUBMIT_BLUEPRINT_ERROR);
77 MessageResponse response = new MessageResponse();
78 response.setSuccessResponse("Blueprint build complete \n. Blueprint=" + blueprintArtifactResult.getArtifactName());
79 return new ResponseEntity<>(response, HttpStatus.OK);
80 } catch (Exception e) {
81 return handleException(e, ErrConfMgr.ApiType.SUBMIT_BLUEPRINT);
85 private String generateBlueprintViaToscaLab(String cdump) {
86 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "---------------------------------------------------------------CDUMP: -----------------------------------------------------------------------------");
87 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), cdump);
88 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "---------------------------------------------------------------------------------------------------------------------------------------------------");
89 ASDCUtils utils = new ASDCUtils(asdc, blueprinter);
90 String resultBlueprintCreation = null;
92 resultBlueprintCreation = utils.buildBlueprintViaToscaLab(new StringReader(cdump)).waitForResult().waitForResult();
94 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Generate blueprint via tosca lab error: {}", e);
96 return resultBlueprintCreation;