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