summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/impl/BlueprintBusinessLogic.java
blob: 2c0dda308a72ba1950276080f3f176200f3f88c0 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package org.onap.sdc.dcae.composition.impl;

import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.onap.sdc.common.onaplog.enums.LogLevel;
import org.onap.sdc.dcae.catalog.commons.Recycler;
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.errormng.ActionStatus;
import org.onap.sdc.dcae.errormng.ErrConfMgr;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import org.yaml.snakeyaml.Yaml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@Component
@ConfigurationProperties(prefix="blueprinter")
public class BlueprintBusinessLogic extends CompositionBusinessLogic {

	private String uri;

	public void setUri(String uri) {
		this.uri = uri;
	}

    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 input = prepareInput(cdump, requestId);
            if (null == input) {
				return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.SUBMIT_BLUEPRINT_ERROR);
			}
			String resultBlueprintCreation = new RestTemplate().postForObject(uri, new HttpEntity<>(input), String.class);

            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 prepareInput(String cdump, String requestId) throws IOException {

		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "fetched cdump payload: {}", cdump);
		Map	cdumpToTosca = new Recycler().recycle(new StringReader(cdump));
		Set<String> dcaeComponentsUuids = extractComponentUuids(cdumpToTosca);
		List<Map> extractedModels = Collections.synchronizedSet(dcaeComponentsUuids).parallelStream().map(id -> fetchAndExtractModel(id, requestId)).filter(Objects::nonNull).collect(Collectors.toList());
		// aggregation of parallel stream fetch results - exceptions are swallowed and we check the final size to verify no errors occurred
		if(dcaeComponentsUuids.size() != extractedModels.size()) {
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), "error: {} distinct DCAE components were mapped to {} tosca lab input models.", dcaeComponentsUuids.size(), extractedModels.size());
			return null;
		}
		return new Gson().toJson(new ToscaLabInput(Base64Utils.encodeToString(new Yaml().dump(cdumpToTosca).getBytes()), extractedModels));
	}

	private Set<String> extractComponentUuids(Map cdump) {
		//the node description contains the UUID of the resource declaring it
		//if the description is the URI the resource uuid is the 5th path element (backward compatibility)
		// TODO there has to be a better way
		Map<String, Map<String, Object>> nodes = (Map<String, Map<String, Object>>)((Map<String, Object>)cdump.get("topology_template")).get("node_templates");
		return nodes.values().stream()
				.map(n -> (String)n.get("description"))
				.filter(StringUtils::isNotBlank)
				.map(d -> StringUtils.substringBetween(d, "resources/", "/"))
						.collect(Collectors.toSet());
	}


	private class ToscaLabInput {
    	private String template;
    	private List<Map> models;

    	ToscaLabInput(String template, List<Map> models){
    		this.template = template;
    		this.models = models;
		}
	}

	private Map<String, String> fetchAndExtractModel(String uuid, String requestId) {
    	try {
			return extractModelFromCsar(sdcRestClient.getResourceToscaModel(uuid, requestId));
		} catch (Exception e) {
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), "model extraction error: {}", e);
			return null;
		}
	}

	private Map<String, String> extractModelFromCsar(byte[] csar) throws IOException {
		//we are only interested in unzipping the 3 files under Artifacts/Deployment/DCAE_TOSCA/
		String dcaeToscaDir = "Artifacts/Deployment/DCAE_TOSCA/";
		Map<String, String> extracted = new HashMap<>();
		try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(csar))) {
			ZipEntry ze = zis.getNextEntry();
			while (ze != null && 3 != extracted.size()) {
				if(ze.getName().startsWith(dcaeToscaDir)) {
					extracted.put(ze.getName().replace(dcaeToscaDir,"").split("\\.")[0], Base64Utils.encodeToString(extractFile(zis)));
				}
				ze = zis.getNextEntry();
			}
			return extracted;
		}
	}
}