Upgrade dt-be-main
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / impl / BlueprintBusinessLogic.java
1 package org.onap.sdc.dcae.composition.impl;
2
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;
20
21 import javax.annotation.PostConstruct;
22 import java.io.StringReader;
23 import java.net.URI;
24
25 @Component
26 public class BlueprintBusinessLogic extends BaseBusinessLogic {
27
28     @Autowired
29     private Blueprinter blueprinter;
30     @Autowired
31     private ASDC asdc;
32     @Autowired
33     private SystemProperties systemProperties;
34     @Autowired private CompositionBusinessLogic compositionBusinessLogic;
35
36
37     @PostConstruct
38     public void init() {
39         URI sdcUri = URI.create(systemProperties.getProperties().getProperty(DcaeBeConstants.Config.URI));
40         asdc.setUri(sdcUri);
41         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "SDC uri: {}", sdcUri);
42     }
43
44     public ResponseEntity generateAndSaveBlueprint(String userId, String context, String vfcmtUuid, String serviceUuid, String vfiName, String flowType, String requestId) {
45         try {
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());
52             }
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());
60             }
61             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "BLUEPRINT:\n{}", resultBlueprintCreation);
62
63             // 1806 US374595 flowType in cdump
64             String flowTypeFromCdump = extractFlowTypeFromCdump(cdump);
65
66             // support backward compatibility
67             if(StringUtils.isBlank(flowTypeFromCdump)) {
68                 flowTypeFromCdump = flowType;
69             }
70
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);
75             }
76
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);
82         }
83     }
84
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;
91         try{
92             resultBlueprintCreation     = utils.buildBlueprintViaToscaLab(new StringReader(cdump)).waitForResult().waitForResult();
93         }catch (Exception e){
94             errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Generate blueprint via tosca lab error: {}", e);
95         }
96         return resultBlueprintCreation;
97     }
98 }