2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.platform.mod.web.service.blueprintdistributionservice;
24 import lombok.extern.slf4j.Slf4j;
25 import org.json.JSONObject;
26 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
27 import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo;
28 import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelStatus;
29 import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse;
30 import org.onap.dcaegen2.platform.mod.model.restapi.SuccessResponse;
31 import org.onap.dcaegen2.platform.mod.util.BlueprintDistributionUtils;
32 import org.onap.dcaegen2.platform.mod.util.SSLUtils;
33 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactGateway;
34 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Service;
43 import org.springframework.web.client.RestTemplate;
48 public class BlueprintDistributionServiceImpl implements BlueprintDistributionService{
51 private DeploymentArtifactService deploymentArtifactService;
54 private BlueprintDistributionUtils blueprintDistributionUtils;
57 private DeploymentArtifactGateway deploymentArtifactGateway;
60 private RestTemplate restTemplate;
62 public ResponseEntity distributeBlueprint(String deploymentArtifactId, String env){
63 DeploymentArtifact deploymentArtifact = deploymentArtifactService.findDeploymentArtifactById(deploymentArtifactId);
64 return postBlueprintToDcae(deploymentArtifact,env);
67 private ResponseEntity postBlueprintToDcae(DeploymentArtifact deploymentArtifact, String env){
69 HttpHeaders headers = new HttpHeaders();
70 headers.setContentType(MediaType.APPLICATION_JSON);
71 headers.setBasicAuth(blueprintDistributionUtils.getBlueprintDashboardUserName(env),blueprintDistributionUtils.getBlueprintDashboardPassword(env));
73 DistributionInfo distributionInfo = DistributionInfo.builder().url(blueprintDistributionUtils.getBlueprintDashboardURL(env)).build();
74 deploymentArtifact.setDistributionInfo(distributionInfo);
75 JSONObject blueprintJsonObject = prepareBlueprintJsonObject(deploymentArtifact);
76 HttpEntity<String> request = new HttpEntity<>(blueprintJsonObject.toString(), headers);
79 SSLUtils.turnOffSslChecking();
80 String response = restTemplate.postForObject(blueprintDistributionUtils.getBlueprintDashboardURL(env),request,String.class);
82 log.info(String.format("Distributed Blueprint to DCAE: %s", blueprintJsonObject.toString()));
83 distributionInfo.setStatus(PolicyModelStatus.SUCCESS);
84 deploymentArtifactGateway.save(deploymentArtifact);
85 blueprintJsonObject.put("distributionInfoStatus", PolicyModelStatus.SUCCESS.name());
86 return new ResponseEntity<>(new SuccessResponse(blueprintJsonObject.toString()), HttpStatus.OK);
87 }catch (Exception e) {
88 log.error("Failed to push Blueprint to DCAE");
89 log.error(e.getMessage());
90 distributionInfo.setStatus(PolicyModelStatus.FAILED);
91 deploymentArtifactGateway.save(deploymentArtifact);
92 return new ResponseEntity<>(new ErrorResponse(e.getMessage()), HttpStatus.BAD_REQUEST);
96 private JSONObject prepareBlueprintJsonObject(DeploymentArtifact deploymentArtifact) {
97 JSONObject blueprintJsonObject = new JSONObject();
98 blueprintJsonObject.put("owner","dcae_mod");
99 blueprintJsonObject.put("typeName",deploymentArtifact.getFileName());
100 blueprintJsonObject.put("typeVersion",deploymentArtifact.getVersion());
101 blueprintJsonObject.put("blueprintTemplate",deploymentArtifact.getContent());
102 blueprintJsonObject.put("application","DCAE");
103 blueprintJsonObject.put("component","dcae");
104 blueprintJsonObject.put("distributionInfoUrl", deploymentArtifact.getDistributionInfo().getUrl());
105 return blueprintJsonObject;
109 private RestTemplate getRestTemplate(){
110 return new RestTemplate();