Moving Historical Queries to spring boot
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / query / ChampRouter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.datarouter.query;
22
23 import java.security.InvalidParameterException;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.annotation.PostConstruct;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response.Status;
32
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;
43
44
45 @Component
46 @Qualifier("champ")
47 public class ChampRouter implements QueryRouter {
48   Logger logger = LoggerFactory.getInstance().getLogger(ChampRouter.class.getName());
49
50   private String champBaseURL;
51
52   private RestClient restClient ;
53   
54   public ChampRouter(){}
55
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());
64     validate();
65   }
66
67  
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);
73     }
74     this.champBaseURL = baseURL;
75   }
76
77   @Override
78   public String process(String urlContext, String queryParams, Map<String, List<String>> headers)
79       throws DataRouterException {
80     String champURL;
81     String response;
82     if (queryParams != null && !queryParams.isEmpty()) {
83       champURL = champBaseURL + urlContext + "?" + queryParams;
84     } else {
85       champURL = champBaseURL + urlContext;
86     }
87
88     logger.info(QueryMsgs.QUERY_INFO, "Routing request to Champ service URL: " + champURL);
89
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);
93
94     if (HttpUtil.isHttpResponseClassSuccess(result.getResultCode())) {
95       response = result.getResult();
96     } else {
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()));
102     }
103
104     return response;
105
106   }
107
108 }