a4b40393a349e16af1f8a570ef646948558c0cfa
[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 import java.util.Optional;
25
26 import javax.ws.rs.core.UriBuilder;
27
28 import org.onap.so.bpmn.common.baseclient.BaseClient;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.onap.so.client.exception.MapperException;
31 import org.onap.so.client.sdnc.beans.SDNCProperties;
32 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
33 import org.onap.so.logger.MsoLogger;
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 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 @Component
43 public class SDNCClient {
44
45         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCClient.class);
46         private BaseClient<String, LinkedHashMap<?, ?>> STOClient = new BaseClient<>();
47
48         @Autowired
49         private SDNCProperties properties;
50         @Autowired
51         private SdnCommonTasks sdnCommonTasks;
52         /**
53          * 
54          * @param request
55          *            - takes in a generated object from sdnc client
56          *            - creates a json request string and sends it to sdnc
57          *            - receives and validates the linkedhashmap sent back from sdnc
58          * @throws MapperException 
59          * @throws BadResponseException 
60          */
61         public String post(Object request, SDNCTopology topology) throws MapperException, BadResponseException {
62                         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
63                         String targetUrl = properties.getHost() + properties.getPath() + ":" + topology.toString() + "/";
64                         STOClient.setTargetUrl(targetUrl);
65                         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
66                         STOClient.setHttpHeader(httpHeader);
67                         msoLogger.info("Running SDNC CLIENT for TargetUrl: " + targetUrl);
68                         LinkedHashMap<?, ?> output = STOClient.post(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
69                         Optional<String> sdncResponse = logSDNCResponse(output);
70                         if(sdncResponse.isPresent()){
71                                 msoLogger.info(sdncResponse.get());
72                         }
73                         msoLogger.info("Validating output...");
74                         return sdnCommonTasks.validateSDNResponse(output);
75         }
76
77         protected Optional<String> logSDNCResponse(LinkedHashMap<?, ?> output) {
78                 ObjectMapper mapper = new ObjectMapper();
79                 String sdncOutput = "";
80                 try {
81                         sdncOutput = mapper.writeValueAsString(output);
82                         return Optional.of(sdncOutput);
83                 } catch (JsonProcessingException e) {
84                         msoLogger.debug("Failed to map response from sdnc to json string for logging purposes.");
85                 }
86                 return Optional.empty();
87         }
88
89         /**
90          * 
91          * @param queryLink
92          *            - takes in a link to topology that needs to be queried
93          *            - creates a json request string and sends it to sdnc
94          *            - receives and validates the linkedhashmap sent back from sdnc
95          *               * 
96          * @throws MapperException 
97          * @throws BadResponseException 
98          */
99         public String get(String queryLink) throws MapperException, BadResponseException {
100                         
101                         String request = "";
102                         String jsonRequest = sdnCommonTasks.buildJsonRequest(request);
103                         String targetUrl = UriBuilder.fromUri(properties.getHost()).path(queryLink).build().toString();                 
104                         STOClient.setTargetUrl(targetUrl);
105                         msoLogger.info("TargetUrl: " + targetUrl);
106                         HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth());
107                         STOClient.setHttpHeader(httpHeader);
108                         msoLogger.info("Running SDNC CLIENT...");
109                         LinkedHashMap<?, ?> output = STOClient.get(jsonRequest, new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
110                         msoLogger.info("Validating output...");
111                         return sdnCommonTasks.validateSDNGetResponse(output);
112         }
113
114 }