integrate helm-chart-generator to runtime api
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / main / java / org / onap / dcae / runtime / web / service / BlueprintInventory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.dcae.runtime.web.service;
19
20 import org.json.JSONException;
21 import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel;
22 import org.onap.dcae.runtime.web.models.DashboardConfig;
23 import org.json.JSONObject;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.http.HttpEntity;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.MediaType;
30 import org.springframework.stereotype.Service;
31 import org.springframework.web.client.RestTemplate;
32
33 import java.util.List;
34
35 @Service
36 public class BlueprintInventory {
37
38     @Autowired
39     DashboardConfig dashboardConfig;
40
41     Logger logger = LoggerFactory.getLogger(BlueprintInventory.class);
42
43     private RestTemplate restTemplate = new RestTemplate();
44
45     public void distributeToInventory(List<BlueprintVessel> blueprints) {
46         for (BlueprintVessel bpv : blueprints) {
47             JSONObject body = prepareBlueprintJsonObject(bpv.name, bpv.version, bpv.blueprint);
48             postToDashboard(body);
49             logger.info(String.format("Distributed: %s", bpv.toString()));
50         }
51     }
52
53     // Should work with inventory too!
54     private boolean postToDashboard(JSONObject blueprintJsonObject){
55         //1. setup
56         HttpHeaders headers = new HttpHeaders();
57         headers.setContentType(MediaType.APPLICATION_JSON);
58         // NOTE: This commented out line is to be used for dcae dashboard api and not inventory
59         //headers.setBasicAuth(dashboardConfig.getUsername(),dashboardConfig.getPassword());
60
61         //2. request
62         HttpEntity<String> request = new HttpEntity<String>(blueprintJsonObject.toString(), headers);
63         try{
64             SSLUtils.turnOffSslChecking();
65             String response = restTemplate.postForObject(dashboardConfig.getUrl(),request,String.class);
66             logger.info(response);
67             return true;
68         }catch (Exception e) {
69             logger.error("failed to push on inventory");
70             logger.error(e.getMessage());
71             return false;
72         }
73     }
74
75     private JSONObject prepareBlueprintJsonObject(String blueprintName, int version, String blueprintContent) {
76         JSONObject blueprintJsonObject = new JSONObject();
77         try {
78             blueprintJsonObject.put("owner","dcae_mod");
79             blueprintJsonObject.put("typeName",blueprintName);
80             blueprintJsonObject.put("typeVersion",version);
81             blueprintJsonObject.put("blueprintTemplate",blueprintContent);
82             blueprintJsonObject.put("application","DCAE");
83             blueprintJsonObject.put("component","dcae");
84         } catch (JSONException e) {
85             e.printStackTrace();
86         }
87         return blueprintJsonObject;
88     }
89
90
91 }