2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.client.sdnc;
25 import java.util.LinkedHashMap;
27 import javax.ws.rs.core.UriBuilder;
29 import org.onap.so.bpmn.common.baseclient.BaseClient;
30 import org.onap.so.client.exception.BadResponseException;
31 import org.onap.so.client.exception.MapperException;
32 import org.onap.so.client.sdnc.beans.SDNCProperties;
33 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.core.ParameterizedTypeReference;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.stereotype.Component;
42 public class SDNCClient {
44 private static final Logger logger = LoggerFactory.getLogger(SDNCClient.class);
47 private SDNCProperties properties;
49 private SdnCommonTasks sdnCommonTasks;
54 * - takes in a generated object from sdnc client
55 * - creates a json request string and sends it to sdnc
56 * - receives and validates the linkedhashmap sent back from sdnc
57 * @throws MapperException
58 * @throws BadResponseException
60 public String post(Object request, SDNCTopology topology) throws MapperException, BadResponseException {
61 String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
62 String targetUrl = properties.getHost() + properties.getPath() + ":" + topology.toString() + "/";
63 BaseClient<String, LinkedHashMap<String, Object>> STOClient = new BaseClient<>();
65 STOClient.setTargetUrl(targetUrl);
66 HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
67 STOClient.setHttpHeader(httpHeader);
68 LinkedHashMap<String, Object> output = STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
69 return sdnCommonTasks.validateSDNResponse(output);
73 public String post(Object request, String url) throws MapperException, BadResponseException {
74 String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
75 BaseClient<String, LinkedHashMap<String, Object>> STOClient = new BaseClient<>();
76 STOClient.setTargetUrl(url);
77 HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
78 STOClient.setHttpHeader(httpHeader);
79 LinkedHashMap<String, Object> output = STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
80 return sdnCommonTasks.validateSDNResponse(output);
86 * - takes in a link to topology that needs to be queried
87 * - creates a json request string and sends it to sdnc
88 * - receives and validates the linkedhashmap sent back from sdnc
90 * @throws MapperException
91 * @throws BadResponseException
93 public String get(String queryLink) throws MapperException, BadResponseException {
95 String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
96 String targetUrl = UriBuilder.fromUri(properties.getHost()).path(queryLink).build().toString();
97 BaseClient<String, LinkedHashMap<String, Object>> STOClient = new BaseClient<>();
98 STOClient.setTargetUrl(targetUrl);
99 HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
100 STOClient.setHttpHeader(httpHeader);
101 LinkedHashMap<String, Object> output = STOClient.get(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
102 return sdnCommonTasks.validateSDNGetResponse(output);