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
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 private RestTemplate restTemplate = new RestTemplate();
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);
53 // Should work with inventory too!
54 private boolean postToDashboard(JSONObject blueprintJsonObject){
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());
62 HttpEntity<String> request = new HttpEntity<String>(blueprintJsonObject.toString(), headers);
64 SSLUtils.turnOffSslChecking();
65 String response = restTemplate.postForObject(dashboardConfig.getUrl(),request,String.class);
66 logger.info(response);
68 }catch (Exception e) {
69 logger.error("failed to push on inventory");
70 logger.error(e.getMessage());
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;