summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/impl/BlueprintBusinessLogic.java
blob: 7a6f9a7759b7e5dbce9cee60a2da8f95a3eb8cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.onap.sdc.dcae.composition.impl;

import org.apache.commons.lang.StringUtils;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.catalog.asdc.ASDC;
import org.onap.sdc.dcae.catalog.asdc.ASDCUtils;
import org.onap.sdc.dcae.catalog.asdc.Blueprinter;
import org.onap.sdc.dcae.composition.restmodels.MessageResponse;
import org.onap.sdc.dcae.composition.restmodels.VfcmtData;
import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.errormng.ActionStatus;
import org.onap.sdc.dcae.errormng.ErrConfMgr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.StringReader;
import java.net.URI;

@Component
public class BlueprintBusinessLogic extends CompositionBusinessLogic {

    @Autowired
    private Blueprinter blueprinter;
    @Autowired
    private ASDC asdc;


    @PostConstruct
    public void init() {
        URI sdcUri = URI.create(systemProperties.getProperties().getProperty(DcaeBeConstants.Config.URI));
        asdc.setUri(sdcUri);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "SDC uri: {}", sdcUri);
    }


    public ResponseEntity generateAndSaveBlueprint(String userId, String context, String vfcmtUuid, String serviceUuid, String vfiName, String flowType, String requestId) {
        try {
            // prepare - fetch vfcmt and cdump
            ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
            Artifact cdumpArtifactData = fetchCdump(vfcmt, requestId);
            if (null == cdumpArtifactData) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Composition not found on vfcmt {}", vfcmtUuid);
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.MISSING_TOSCA_FILE, "", vfcmt.getName());
            }
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Found the cdump (composition.yml) on top of VFCMT {}", vfcmtUuid);
            String cdump = cdumpArtifactData.getPayloadData();
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to use python procedure to create a blueprint....");
            String resultBlueprintCreation = generateBlueprintViaToscaLab(cdump);
            if (StringUtils.isEmpty(resultBlueprintCreation)) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error occurred during blueprint generation");
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.GENERATE_BLUEPRINT_ERROR, "", vfcmt.getName());
            }
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "BLUEPRINT:\n{}", resultBlueprintCreation);

            // 1806 US374595 flowType in cdump
            String flowTypeFromCdump = extractFlowTypeFromCdump(cdump);

            // support backward compatibility
            if(StringUtils.isBlank(flowTypeFromCdump)) {
                flowTypeFromCdump = flowType;
            }

            VfcmtData vfcmtData = new VfcmtData(vfcmt, vfiName, flowTypeFromCdump, serviceUuid);
            Artifact blueprintArtifactResult = submitComposition(userId, context, vfcmtData, resultBlueprintCreation, requestId);
            if (null == blueprintArtifactResult) {
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.SUBMIT_BLUEPRINT_ERROR);
            }

            MessageResponse response = new MessageResponse();
            response.setSuccessResponse("Blueprint build complete \n. Blueprint=" + blueprintArtifactResult.getArtifactName());
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (Exception e) {
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), e.getMessage());
			return ErrConfMgr.INSTANCE.handleException(e, ErrConfMgr.ApiType.SUBMIT_BLUEPRINT);
        }
    }

    private String generateBlueprintViaToscaLab(String cdump) {
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "---------------------------------------------------------------CDUMP: -----------------------------------------------------------------------------");
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), cdump);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "---------------------------------------------------------------------------------------------------------------------------------------------------");
        ASDCUtils utils = new ASDCUtils(asdc, blueprinter);
        String resultBlueprintCreation = null;
        try{
            resultBlueprintCreation	= utils.buildBlueprintViaToscaLab(new StringReader(cdump)).waitForResult().waitForResult();
        }catch (Exception e){
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Generate blueprint via tosca lab error: {}", e);
        }
        return resultBlueprintCreation;
    }
}