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