Moved base client to new location
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sdnc / SDNCClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.client.sdnc;
24
25 import java.util.LinkedHashMap;
26 import javax.ws.rs.core.UriBuilder;
27 import org.onap.so.client.BaseClient;
28 import org.onap.so.client.exception.BadResponseException;
29 import org.onap.so.client.exception.MapperException;
30 import org.onap.so.client.sdnc.beans.SDNCProperties;
31 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.core.ParameterizedTypeReference;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class SDNCClient {
41
42     private static final Logger logger = LoggerFactory.getLogger(SDNCClient.class);
43
44     @Autowired
45     private SDNCProperties properties;
46     @Autowired
47     private SdnCommonTasks sdnCommonTasks;
48
49     /**
50      * 
51      * @param request - takes in a generated object from sdnc client - creates a json request string and sends it to
52      *        sdnc - receives and validates the linkedhashmap sent back from sdnc
53      * @throws MapperException
54      * @throws BadResponseException
55      */
56     public String post(Object request, SDNCTopology topology) throws MapperException, BadResponseException {
57         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
58         String targetUrl = properties.getHost() + properties.getPath() + ":" + topology.toString() + "/";
59         BaseClient<String, LinkedHashMap<String, Object>> STOClient = new BaseClient<>();
60
61         STOClient.setTargetUrl(targetUrl);
62         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
63         STOClient.setHttpHeader(httpHeader);
64         LinkedHashMap<String, Object> output =
65                 STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
66         return sdnCommonTasks.validateSDNResponse(output);
67     }
68
69
70     public String post(Object request, String url) throws MapperException, BadResponseException {
71         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
72         BaseClient<String, LinkedHashMap<String, Object>> STOClient = new BaseClient<>();
73         STOClient.setTargetUrl(url);
74         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
75         STOClient.setHttpHeader(httpHeader);
76         LinkedHashMap<String, Object> output =
77                 STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
78         return sdnCommonTasks.validateSDNResponse(output);
79     }
80
81     /**
82      * 
83      * @param queryLink - takes in a link to topology that needs to be queried - creates a json request string and sends
84      *        it to sdnc - receives and validates the linkedhashmap sent back from sdnc *
85      * @throws MapperException
86      * @throws BadResponseException
87      */
88     public String get(String queryLink) throws MapperException, BadResponseException {
89         String request = "";
90         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
91         String targetUrl = UriBuilder.fromUri(properties.getHost()).path(queryLink).build().toString();
92         BaseClient<String, LinkedHashMap<String, Object>> STOClient = new BaseClient<>();
93         STOClient.setTargetUrl(targetUrl);
94         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
95         STOClient.setHttpHeader(httpHeader);
96         LinkedHashMap<String, Object> output =
97                 STOClient.get(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
98         return sdnCommonTasks.validateSDNGetResponse(output);
99     }
100
101 }