added generic fabric support to SO
[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.bpmn.common.baseclient.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.onap.so.logger.MsoLogger;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.core.ParameterizedTypeReference;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class SDNCClient {
40
41         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCClient.class);
42         private BaseClient<String, LinkedHashMap<?, ?>> STOClient = new BaseClient<>();
43
44         @Autowired
45         private SDNCProperties properties;
46         @Autowired
47         private SdnCommonTasks sdnCommonTasks;
48         /**
49          * 
50          * @param request
51          *            - takes in a generated object from sdnc client
52          *            - creates a json request string and sends it to sdnc
53          *            - receives and validates the linkedhashmap sent back from sdnc
54          * @throws MapperException 
55          * @throws BadResponseException 
56          */
57         public String post(Object request, SDNCTopology topology) throws MapperException, BadResponseException {
58                         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
59                         String targetUrl = properties.getHost() + properties.getPath() + ":" + topology.toString() + "/";
60                         STOClient.setTargetUrl(targetUrl);
61                         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
62                         STOClient.setHttpHeader(httpHeader);
63                         msoLogger.info("Running SDNC CLIENT for TargetUrl: " + targetUrl);
64                         LinkedHashMap<?, ?> output = STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
65                         msoLogger.info("Validating output...");
66                         return sdnCommonTasks.validateSDNResponse(output);
67         }
68
69
70         /**
71          * 
72          * @param queryLink
73          *            - takes in a link to topology that needs to be queried
74          *            - creates a json request string and sends it to sdnc
75          *            - receives and validates the linkedhashmap sent back from sdnc
76          *               * 
77          * @throws MapperException 
78          * @throws BadResponseException 
79          */
80         public String get(String queryLink) throws MapperException, BadResponseException {
81                         
82                         String request = "";
83                         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
84                         String targetUrl = UriBuilder.fromUri(properties.getHost()).path(queryLink).build().toString();                 
85                         STOClient.setTargetUrl(targetUrl);
86                         msoLogger.info("TargetUrl: " + targetUrl);
87                         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
88                         STOClient.setHttpHeader(httpHeader);
89                         msoLogger.info("Running SDNC CLIENT...");
90                         LinkedHashMap<?, ?> output = STOClient.get(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
91                         msoLogger.info("Validating output...");
92                         return sdnCommonTasks.validateSDNGetResponse(output);
93         }
94
95 }