9e60196426a8ffb8eb8d770e7ca26583e55aa731
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.sdnc;
22
23 import java.util.LinkedHashMap;
24
25 import javax.ws.rs.core.UriBuilder;
26
27 import org.onap.so.client.exception.BadResponseException;
28 import org.onap.so.client.exception.MapperException;
29 import org.onap.so.client.sdnc.beans.SDNCProperties;
30 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
31 import org.onap.so.logger.MsoLogger;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.core.ParameterizedTypeReference;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class SDNCClient {
39
40         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCClient.class);
41         private BaseClient<String, LinkedHashMap<?, ?>> STOClient = new BaseClient<>();
42
43         @Autowired
44         private SDNCProperties properties;
45         @Autowired
46         private SdnCommonTasks sdnCommonTasks;
47         /**
48          * 
49          * @param request
50          *            - takes in a generated object from sdnc client
51          *            - creates a json request string and sends it to sdnc
52          *            - 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                         STOClient.setTargetUrl(targetUrl);
60                         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
61                         STOClient.setHttpHeader(httpHeader);
62                         msoLogger.info("Running SDNC CLIENT for TargetUrl: " + targetUrl);
63                         LinkedHashMap<?, ?> output = STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
64                         msoLogger.info("Validating output...");
65                         return sdnCommonTasks.validateSDNResponse(output);
66         }
67
68
69         /**
70          * 
71          * @param queryLink
72          *            - takes in a link to topology that needs to be queried
73          *            - creates a json request string and sends it to sdnc
74          *            - receives and validates the linkedhashmap sent back from sdnc
75          *               * 
76          * @throws MapperException 
77          * @throws BadResponseException 
78          */
79         public String get(String queryLink) throws MapperException, BadResponseException {
80                         
81                         String request = "";
82                         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
83                         String targetUrl = UriBuilder.fromUri(properties.getHost()).path(queryLink).build().toString();                 
84                         STOClient.setTargetUrl(targetUrl);
85                         msoLogger.info("TargetUrl: " + targetUrl);
86                         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
87                         STOClient.setHttpHeader(httpHeader);
88                         msoLogger.info("Running SDNC CLIENT...");
89                         LinkedHashMap<?, ?> output = STOClient.get(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
90                         msoLogger.info("Validating output...");
91                         return sdnCommonTasks.validateSDNGetResponse(output);
92         }
93
94 }