2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.datarouter.util.client;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import org.apache.camel.Exchange;
27 import org.apache.camel.Message;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.datarouter.logging.DataRouterMsgs;
32 import org.onap.aai.datarouter.util.AaiUiSvcPolicyUtil;
33 import org.onap.aai.datarouter.util.NodeUtils;
34 import org.onap.aai.datarouter.util.RouterServiceUtil;
35 import org.onap.aai.restclient.client.Headers;
36 import org.onap.aai.restclient.client.OperationResult;
37 import org.onap.aai.restclient.client.RestClient;
38 import org.onap.aai.restclient.enums.RestAuthenticationMode;
39 import org.onap.aai.restclient.rest.HttpUtil;
41 import org.springframework.http.HttpStatus;
43 import javax.ws.rs.core.MediaType;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
49 public class NoAuthRestClient implements SvcRoutingRestClient {
51 private RestClient restClient;
55 private String originUrl;
56 private String targetUri;
57 private JsonNode targetPayload;
58 private Logger logger;
59 private Logger auditLogger;
61 public NoAuthRestClient(int connectTimeOut, int readTimeOut) {
62 LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance();
63 logger = loggerFactoryInstance.getLogger(NoAuthRestClient.class.getName());
64 auditLogger = loggerFactoryInstance.getAuditLogger(NoAuthRestClient.class.getName());
65 restClient = new RestClient().authenticationMode(RestAuthenticationMode.HTTP_NOAUTH)
66 .connectTimeoutMs(connectTimeOut).readTimeoutMs(readTimeOut);
70 private OperationResult getResults(String url, JsonNode payload) {
71 Map<String, List<String>> headers = new HashMap<>();
72 headers.put(Headers.FROM_APP_ID, Arrays.asList("Synapse"));
73 headers.put(Headers.TRANSACTION_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
74 return this.getRestClient().post(url, payload.asText(),
75 headers, MediaType.APPLICATION_JSON_TYPE,
76 MediaType.APPLICATION_JSON_TYPE);
79 public final void handleRequest(String host, String port, Exchange exchange) throws Exception {
80 RouterServiceUtil.setMdcContext(exchange);
81 Message message = exchange.getIn();
82 String body = message.getBody(String.class);
83 OperationResult result = new OperationResult();
88 this.setOriginUrl(message.getHeader(Exchange.HTTP_URL).toString());
89 if (body != null && body.length() != 0) {
90 JsonNode node = NodeUtils.convertJsonStrToJsonNode(body);
91 this.setTargetPayload(AaiUiSvcPolicyUtil.getOriginPayload(node));
92 this.setTargetUri(AaiUiSvcPolicyUtil.getTargetUri(node));
95 if (this.getTargetPayload() == null || this.getTargetUri() == null) {
96 logger.error(DataRouterMsgs.INVALID_ORIGIN_PAYLOAD, body);
97 result.setResultCode(HttpStatus.BAD_REQUEST.value());
98 result.setFailureCause("Invalid payload");
101 String targetUrl = "http://" + host + ":" + port + "/" + this.targetUri;
102 auditLogger.info(DataRouterMsgs.ROUTING_FROM_TO, this.getOriginUrl(), targetUrl);
103 long startTimeInMs = System.currentTimeMillis();
105 result = this.getResults(targetUrl, targetPayload);
107 long targetMsOpTime = System.currentTimeMillis() - startTimeInMs;
108 auditLogger.info(DataRouterMsgs.OP_TIME, "Target service at " + targetUrl, String.valueOf(targetMsOpTime));
110 int rc = result.getResultCode();
112 if (HttpUtil.isHttpResponseClassSuccess(rc)) {
113 resultStr = result.getResult();
115 resultStr = result.getFailureCause();
118 logger.debug(DataRouterMsgs.ROUTING_RESPONSE, targetUrl, result.toString());
119 exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, rc);
120 exchange.getOut().setBody(resultStr);
125 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#getHost()
128 public String getHost() {
133 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#setHost(java.lang.String)
136 public void setHost(String host) {
142 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#getPort()
145 public String getPort() {
151 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#setPort(java.lang.String)
154 public void setPort(String port) {
160 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#getTargetUri()
163 public String getTargetUri() {
169 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#setTargetUri(java.lang.String)
172 public void setTargetUri(String targetUri) {
173 this.targetUri = targetUri;
178 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#getTargetPayload()
181 public JsonNode getTargetPayload() {
182 return targetPayload;
187 * @see org.onap.aai.datarouter.util.client
188 * .SvcRoutingRestClient#setTargetPayload(com.fasterxml.jackson.databind.JsonNode)
191 public void setTargetPayload(JsonNode targetPayload) {
192 this.targetPayload = targetPayload;
197 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#getRestClient()
200 public RestClient getRestClient() {
205 * @see org.onap.aai.datarouter.util.client.SvcRoutingRestClient#setRestClient()
208 public void setRestClient(RestClient client) {
209 this.restClient = client;
213 public String getOriginUrl() {
218 public void setOriginUrl(String originUrl) {
219 this.originUrl = originUrl;