2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.datarouter.query;
23 import java.security.InvalidParameterException;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
29 import javax.annotation.PostConstruct;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response.Status;
33 import org.eclipse.jetty.util.security.Password;
34 import org.onap.aai.cl.api.Logger;
35 import org.onap.aai.cl.eelf.LoggerFactory;
36 import org.onap.aai.datarouter.exception.DataRouterException;
37 import org.onap.aai.datarouter.util.DataRouterConstants;
38 import org.onap.aai.restclient.client.OperationResult;
39 import org.onap.aai.restclient.client.RestClient;
40 import org.onap.aai.restclient.rest.HttpUtil;
41 import org.springframework.beans.factory.annotation.Qualifier;
42 import org.springframework.stereotype.Component;
47 public class ChampRouter implements QueryRouter {
48 Logger logger = LoggerFactory.getInstance().getLogger(ChampRouter.class.getName());
50 private String champBaseURL;
52 private RestClient restClient ;
54 public ChampRouter(){}
56 public ChampRouter(String champBaseURL, RestClientConfig config) {
57 this.champBaseURL = champBaseURL;
58 this.restClient = new RestClient().validateServerHostname(false).validateServerCertChain(true)
59 .clientCertFile(config.getCertPath())
60 .clientCertPassword(Password.deobfuscate(config.getCertPassword()))
61 .trustStore(config.getTrustStorePath())
62 .connectTimeoutMs(config.getConnectionTimeout())
63 .readTimeoutMs(config.getReadTimeout());
68 public void validate(){
69 String baseURL = champBaseURL.endsWith("/") ? champBaseURL.substring(0, champBaseURL.length() - 1) : champBaseURL;
70 if (baseURL.contains(DATA_ROUTER_PORT)) {
71 logger.info(QueryMsgs.QUERY_INFO, "Invalid champBaseURL : Can't re-route back to DataRouter " + champBaseURL);
72 throw new InvalidParameterException("Invalid champBaseURL : Can't re-route back to DataRouter " + champBaseURL);
74 this.champBaseURL = baseURL;
78 public String process(String urlContext, String queryParams, Map<String, List<String>> headers)
79 throws DataRouterException {
82 if (queryParams != null && !queryParams.isEmpty()) {
83 champURL = champBaseURL + urlContext + "?" + queryParams;
85 champURL = champBaseURL + urlContext;
88 logger.info(QueryMsgs.QUERY_INFO, "Routing request to Champ service URL: " + champURL);
90 headers = headers == null ? new HashMap<String, List<String>>() : headers;
91 headers.put("X-FromAppId", Arrays.asList(DataRouterConstants.DATA_ROUTER_SERVICE_NAME));
92 OperationResult result = restClient.get(champURL, headers, MediaType.APPLICATION_JSON_TYPE);
94 if (HttpUtil.isHttpResponseClassSuccess(result.getResultCode())) {
95 response = result.getResult();
97 logger.info(QueryMsgs.QUERY_ERROR,
98 "Error while calling Champ service URL: " + champURL + " failure cause: " + result.getFailureCause());
99 throw new DataRouterException(
100 "Error while calling Champ service URL: " + champURL + " failure cause: " + result.getFailureCause(),
101 Status.fromStatusCode(result.getResultCode()));