2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
18 package org.onap.dcae.runtime.web.service;
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;
32 import java.util.List;
35 public class BlueprintInventory {
38 DashboardConfig dashboardConfig;
40 Logger logger = LoggerFactory.getLogger(BlueprintInventory.class);
42 public void distributeToInventory(List<BlueprintVessel> blueprints) {
43 for (BlueprintVessel bpv : blueprints) {
44 JSONObject body = prepareBlueprintJsonObject(bpv.name, bpv.version, bpv.blueprint);
45 postToDashboard(body);
46 logger.info(String.format("Distributed: %s", bpv.toString()));
47 //System.out.println(bpv.blueprint);
51 // Should work with inventory too!
52 private boolean postToDashboard(JSONObject blueprintJsonObject){
54 RestTemplate restTemplate = new RestTemplate();
55 HttpHeaders headers = new HttpHeaders();
56 headers.setContentType(MediaType.APPLICATION_JSON);
57 // NOTE: This commented out line is to be used for dcae dashboard api and not inventory
58 //headers.setBasicAuth(dashboardConfig.getUsername(),dashboardConfig.getPassword());
61 HttpEntity<String> request = new HttpEntity<String>(blueprintJsonObject.toString(), headers);
63 SSLUtils.turnOffSslChecking();
64 String response = restTemplate.postForObject(dashboardConfig.getUrl(),request,String.class);
65 logger.info(response);
67 }catch (Exception e) {
68 logger.error("failed to push on inventory");
69 logger.error(e.getMessage());
74 private JSONObject prepareBlueprintJsonObject(String blueprintName, int version, String blueprintContent) {
75 JSONObject blueprintJsonObject = new JSONObject();
76 blueprintJsonObject.put("owner","dcae_mod");
77 blueprintJsonObject.put("typeName",blueprintName);
78 blueprintJsonObject.put("typeVersion",version);
79 blueprintJsonObject.put("blueprintTemplate",blueprintContent);
80 blueprintJsonObject.put("application","DCAE");
81 blueprintJsonObject.put("component","dcae");
82 return blueprintJsonObject;