789c9d1bd0c9873e5efe6f166cd720f67e469f51
[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) 2019-2020 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.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel;
21 import org.onap.dcae.runtime.web.models.DashboardConfig;
22 import org.json.JSONObject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import org.springframework.stereotype.Service;
30 import org.springframework.web.client.RestTemplate;
31
32 import java.util.List;
33
34 @Service
35 public class BlueprintInventory {
36
37     @Autowired
38     DashboardConfig dashboardConfig;
39
40     Logger logger = LoggerFactory.getLogger(BlueprintInventory.class);
41
42     private RestTemplate restTemplate = new RestTemplate();
43
44     public void distributeToInventory(List<BlueprintVessel> blueprints) {
45         for (BlueprintVessel bpv : blueprints) {
46             JSONObject body = prepareBlueprintJsonObject(bpv.name, bpv.version, bpv.blueprint);
47             postToDashboard(body);
48             logger.info(String.format("Distributed: %s", bpv.toString()));
49             //System.out.println(bpv.blueprint);
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         blueprintJsonObject.put("owner","dcae_mod");
78         blueprintJsonObject.put("typeName",blueprintName);
79         blueprintJsonObject.put("typeVersion",version);
80         blueprintJsonObject.put("blueprintTemplate",blueprintContent);
81         blueprintJsonObject.put("application","DCAE");
82         blueprintJsonObject.put("component","dcae");
83         return blueprintJsonObject;
84     }
85
86
87 }