a120f896a5dd7ba0ceb400eda7d64deac05f9206
[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
25 import org.apache.camel.Exchange;
26 import org.onap.aai.rest.RestClientEndpoint;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29
30 public class ChampRouter extends QueryRouter {
31   Logger logger = LoggerFactory.getInstance().getLogger(ChampRouter.class.getName());
32
33   private String champBaseURL;
34   
35
36   public ChampRouter(String champBaseURL) {
37     String baseURL = champBaseURL.endsWith("/") ? champBaseURL.substring(0, champBaseURL.length() - 1)
38         : champBaseURL;
39     if(checkRecursion(baseURL)){
40       logger.info(QueryMsgs.QUERY_INFO, "Invalid champBaseURL : Can't re-route back to DataRouter "+ champBaseURL);
41       throw new InvalidParameterException("Invalid champBaseURL : Can't re-route back to DataRouter "+ champBaseURL);
42     }
43     this.champBaseURL = baseURL;    
44   }
45
46   public void setQueryRequest(Exchange exchange) {
47     setMDC(exchange);
48     String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
49     String queryParams = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
50     String ecompUrl;
51     if (queryParams != null && !queryParams.isEmpty()) {
52       ecompUrl = champBaseURL + path + "?" + queryParams;
53     } else {
54       ecompUrl = champBaseURL + path;
55     }
56
57     logger.info(QueryMsgs.QUERY_INFO, "Routing request to Champ service URL: " + ecompUrl);
58     exchange.getIn().setHeader(RestClientEndpoint.IN_HEADER_URL, ecompUrl);
59     exchange.getIn().setHeader("X-FromAppId", SERVICE_NAME);
60     exchange.getIn().setHeader("X-TransactionId", getTxId(exchange));
61
62   }
63
64   public void setQueryResponse(Exchange exchange) {
65     Integer httpResponseCode = exchange.getIn().getHeader(RestClientEndpoint.OUT_HEADER_RESPONSE_CODE, Integer.class);
66     // Object httpBody = exchange.getIn().getBody();
67     exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, httpResponseCode);
68
69   }
70
71 }