1 package org.onap.sdc.dcae.composition.controller;
3 import org.apache.commons.lang.StringUtils;
4 import org.onap.sdc.common.onaplog.Enums.LogLevel;
5 import org.onap.sdc.dcae.composition.restmodels.MessageResponse;
6 import org.onap.sdc.dcae.catalog.asdc.ASDC;
7 import org.onap.sdc.dcae.catalog.asdc.ASDCUtils;
8 import org.onap.sdc.dcae.catalog.asdc.Blueprinter;
9 import org.onap.sdc.dcae.composition.restmodels.sdc.*;
10 import org.onap.sdc.dcae.utils.Normalizers;
11 import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
12 import org.onap.sdc.dcae.enums.ArtifactType;
13 import org.onap.sdc.dcae.enums.AssetType;
14 import org.onap.sdc.dcae.enums.LifecycleOperationType;
15 import org.onap.sdc.dcae.errormng.ActionStatus;
16 import org.onap.sdc.dcae.errormng.ErrConfMgr;
17 import org.onap.sdc.dcae.errormng.ErrConfMgr.ApiType;
18 import org.onap.sdc.dcae.utils.SdcRestClientUtils;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21 import org.springframework.http.HttpStatus;
22 import org.springframework.http.ResponseEntity;
23 import org.springframework.util.Base64Utils;
24 import org.springframework.util.CollectionUtils;
25 import org.springframework.web.bind.annotation.*;
27 import javax.annotation.PostConstruct;
28 import java.io.StringReader;
32 @EnableAutoConfiguration
34 public class BlueprintController extends BaseController{
37 private Blueprinter blueprinter;
42 private static final String CREATE_DESC = "creating new artifact blueprint on the service vfi";
43 private static final String UPDATE_DESC = "updating artifact blueprint on the service vfi";
49 URI sdcUri = URI.create(systemProperties.getProperties().getProperty(DcaeBeConstants.Config.URI));
51 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "SDC uri: {}", sdcUri);
55 * VFCMT - Resource, blueprint - as an artifact as an service.
59 * @param serviceInstanceName
60 * @param monitoringFlowType
61 * @return ResponseEntity
63 @RequestMapping(value = "/createBluePrint/{VFCMTUuid}/{serviceUuid}/{instanceName}/{monitoringFlowType}", method = RequestMethod.POST)
64 public ResponseEntity createBluePrint(@RequestHeader("USER_ID") String userId,
65 @PathVariable("VFCMTUuid") String vfcmtUuid,
66 @PathVariable("serviceUuid") String serviceUuid,
67 @PathVariable("instanceName") String serviceInstanceName,
68 @PathVariable("monitoringFlowType") String monitoringFlowType,
69 @ModelAttribute("requestId") String requestId) {
72 ResourceDetailed vfcmt = baseBusinessLogic.getSdcRestClient().getResource(vfcmtUuid, requestId);
73 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), vfcmt.toString());
74 checkVfcmtType(vfcmt);
75 Artifact cdumpArtifactData = findCdumpArtifactData(vfcmt);
76 if (null != cdumpArtifactData) {
77 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Found the cdump (composition.yml) on top of VFCMT {}", vfcmtUuid);
78 String cdump = baseBusinessLogic.getSdcRestClient().getResourceArtifact(vfcmtUuid, cdumpArtifactData.getArtifactUUID(), requestId);
79 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "---------------------------------------------------------------CDUMP: -----------------------------------------------------------------------------");
80 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), cdump);
81 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "---------------------------------------------------------------------------------------------------------------------------------------------------");
82 ASDCUtils utils = new ASDCUtils(asdc, blueprinter);
84 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Going to use python procedure to create a blueprint....");
85 String resultBlueprintCreation;
87 resultBlueprintCreation = utils.buildBlueprintViaToscaLab(new StringReader(cdump)).waitForResult().waitForResult();
89 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.GENERATE_BLUEPRINT_ERROR, e.getMessage(), vfcmt.getName());
91 if (StringUtils.isEmpty(resultBlueprintCreation)) {
92 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.GENERATE_BLUEPRINT_ERROR, "", vfcmt.getName());
95 // 1806 US374595 flowType in cdump
96 String flowTypeFromCdump = StringUtils.substringBetween(cdump,"\"flowType\":\"","\"");
97 if(StringUtils.isNotBlank(flowTypeFromCdump)) {
98 monitoringFlowType = flowTypeFromCdump;
100 // saving to serviceVfInstance
101 Artifact savedBluePrint = saveBluePrint(userId, serviceUuid, serviceInstanceName, resultBlueprintCreation, monitoringFlowType, vfcmt.getName(), requestId);
102 if(savedBluePrint!=null){
103 MessageResponse response = new MessageResponse();
104 response.setSuccessResponse("Blueprint build complete \n. Blueprint="+savedBluePrint.getArtifactName());
105 //1806 US374593 - certify VFCMT after BP generation
106 certifyVfcmt(vfcmt, requestId);
107 return new ResponseEntity<>(response, HttpStatus.OK);
110 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.SUBMIT_BLUEPRINT_ERROR);
114 return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.MISSING_TOSCA_FILE, "", vfcmt.getName());
116 } catch (Exception e) {
117 return handleException(e, ApiType.SUBMIT_BLUEPRINT);
122 /********************* private function ********************/
127 * @param resourceInstanceName
129 * @param monitoringFlowType
135 private Artifact saveBluePrint(String userId, String serviceUuid, String resourceInstanceName, String bluePrint, String monitoringFlowType, String vfcmtName, String requestId) throws Exception {
137 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "BLUEPRINT:\n{}", bluePrint);
139 ServiceDetailed service = baseBusinessLogic.getSdcRestClient().getService(serviceUuid, requestId);
141 checkUserIfResourceCheckedOut(userId, service);
142 ResourceInstance vfi = findVfiOnService(service, resourceInstanceName);
144 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "VF instance {} not found on service {}", resourceInstanceName, serviceUuid);
148 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), service.toString());
150 String normalizedArtifactLabel = Normalizers.normalizeArtifactLabel("blueprint-" + monitoringFlowType);
151 Artifact blueprintArtifact = CollectionUtils.isEmpty(vfi.getArtifacts()) ? null : vfi.getArtifacts().stream()
152 .filter(p -> normalizedArtifactLabel.equals(Normalizers.normalizeArtifactLabel(p.getArtifactLabel())))
156 boolean isNeed2Checkout = isNeedToCheckOut(service.getLifecycleState());
157 if (isNeed2Checkout) {
158 Asset result = checkout(userId, serviceUuid, AssetType.SERVICE, requestId);
159 if (result != null) {
160 serviceUuid = result.getUuid();
161 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "New service after checkout is: {}", serviceUuid);
165 if (null != blueprintArtifact) {
166 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Found that service {} already consist of {} ----> updateMode", serviceUuid, normalizedArtifactLabel);
167 blueprintArtifact.setDescription(UPDATE_DESC);
168 blueprintArtifact.setPayloadData(Base64Utils.encodeToString(bluePrint.getBytes()));
169 blueprintArtifact = baseBusinessLogic.getSdcRestClient().updateVfInstanceArtifact(userId, serviceUuid, Normalizers.normalizeComponentInstanceName(resourceInstanceName), blueprintArtifact, requestId);
172 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Service {} does not consist {} ----> createMode", serviceUuid, normalizedArtifactLabel);
173 blueprintArtifact = SdcRestClientUtils.generateDeploymentArtifact(CREATE_DESC, generateBlueprintFileName(monitoringFlowType, vfcmtName), ArtifactType.DCAE_INVENTORY_BLUEPRINT.name(), normalizedArtifactLabel, bluePrint.getBytes());
174 blueprintArtifact = baseBusinessLogic.getSdcRestClient().createVfInstanceArtifact(userId, serviceUuid, Normalizers.normalizeComponentInstanceName(resourceInstanceName), blueprintArtifact, requestId);
177 //No need to check the service in in 1806
178 // Asset blueprintAsJson = checkin(user_id, serviceUuid, AssetType.SERVICE);
179 // debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "service result after check-in: {}", blueprintAsJson.toString());
181 return blueprintArtifact;
183 } catch (Exception e) {
184 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error occurred while trying to save blueprint {}", e.toString());
191 * @param monitoringFlowType
195 private String generateBlueprintFileName(String monitoringFlowType, String vfcmtName) {
196 StringBuffer sb = new StringBuffer();
197 sb.append(monitoringFlowType);
199 sb.append(Normalizers.normalizeComponentName(vfcmtName));
201 sb.append(DcaeBeConstants.Composition.fileNames.EVENT_PROC_BP_YAML);
202 return sb.toString();
205 private ResourceInstance findVfiOnService(ServiceDetailed service, String vfiName) {
206 return null == service ? null : CollectionUtils.isEmpty(service.getResources()) ? null : service.getResources().stream().filter(p -> vfiName.equals(p.getResourceInstanceName())).findAny().orElse(null);
209 private Artifact findCdumpArtifactData(ResourceDetailed vfcmt) {
210 return null == vfcmt ? null : CollectionUtils.isEmpty(vfcmt.getArtifacts()) ? null : vfcmt.getArtifacts().stream()
211 .filter(p -> DcaeBeConstants.Composition.fileNames.COMPOSITION_YML.equals(p.getArtifactName())).findAny().orElse(null);
214 private void certifyVfcmt(ResourceDetailed vfcmt, String requestId){
215 String state = vfcmt.getLifecycleState();
217 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "Couldn't read Vfcmt lifecycle state");
220 DcaeBeConstants.LifecycleStateEnum lifeCycleState = DcaeBeConstants.LifecycleStateEnum.findState(state);
221 if(null == lifeCycleState) {
222 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "Undefined lifecycle state: {}", state);
226 switch (lifeCycleState){
227 case NOT_CERTIFIED_CHECKOUT:
228 baseBusinessLogic.getSdcRestClient().changeResourceLifecycleState(vfcmt.getLastUpdaterUserId(), vfcmt.getUuid(), LifecycleOperationType.CHECKIN.name(), "check in VFCMT after blueprint successful submission", requestId);
229 case NOT_CERTIFIED_CHECKIN:
230 baseBusinessLogic.getSdcRestClient().changeResourceLifecycleState(vfcmt.getLastUpdaterUserId(), vfcmt.getUuid(), LifecycleOperationType.CERTIFY.name(), "certify VFCMT after blueprint successful submission", requestId);
234 //informative only. no message to user (TBA)
235 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error occurred during vfcmt lifecycle operation: {}", e.toString());