cae4a19f209f3439b74641027eaaf1fec5e7b0d6
[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
27 import javax.ws.rs.core.UriBuilder;
28
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;
40
41 @Component
42 public class SDNCClient {
43
44         private static final Logger logger = LoggerFactory.getLogger(SDNCClient.class);
45
46         @Autowired
47         private SDNCProperties properties;
48         @Autowired
49         private SdnCommonTasks sdnCommonTasks;
50         
51         /**
52          * 
53          * @param request
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 
59          */
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<>();
64
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);
70         }
71         
72         
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);
81         }
82         
83         /**
84          * 
85          * @param queryLink
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
89          *               * 
90          * @throws MapperException 
91          * @throws BadResponseException 
92          */
93         public String get(String queryLink) throws MapperException, BadResponseException {
94                 String request = "";
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);
103         }
104
105 }