Moving Historical Queries to spring boot
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / service / HistoricalQueryService.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.service;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.Encoded;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.Response.Status;
39 import javax.ws.rs.core.UriInfo;
40
41 import org.onap.aai.cl.api.Logger;
42 import org.onap.aai.cl.eelf.LoggerFactory;
43 import org.onap.aai.datarouter.exception.DataRouterException;
44 import org.onap.aai.datarouter.logging.DataRouterMsgs;
45 import org.onap.aai.datarouter.logging.LoggingUtil;
46 import org.onap.aai.datarouter.query.QueryRouter;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.beans.factory.annotation.Qualifier;
49 import org.springframework.stereotype.Component;
50
51 @Component
52 @Path("/services/champ-service/v1")
53 public class HistoricalQueryService {
54
55   private Logger logger = LoggerFactory.getInstance().getLogger(HistoricalQueryService.class);
56   Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(HistoricalQueryService.class.getName());
57   private static Logger metricsLogger = LoggerFactory.getInstance()
58       .getMetricsLogger(HistoricalQueryService.class.getName());
59
60   @Autowired
61   @Qualifier("champ")
62   private QueryRouter champRouter;
63
64   @Autowired
65   @Qualifier("chameleon")
66   private QueryRouter chameleonRouter;
67
68   @GET  
69   @Path("{uri: .+}")
70   @Produces({ MediaType.APPLICATION_JSON })
71   public Response process(String content, @PathParam("version") String versionParam,
72       @PathParam("uri") @Encoded String uri, @Context HttpHeaders httpHeaders, @Context UriInfo uriInfo,
73       @Context HttpServletRequest req) {
74     LoggingUtil.initMdcContext(req, httpHeaders);
75     long startTimeInMs = System.currentTimeMillis();
76     Response response = null;
77     String urlContext = "/"+ uri;
78     String queryParams = uriInfo.getRequestUri().getQuery();
79
80     try {
81
82       Map<String, List<String>> parameters = new HashMap<String, List<String>>();
83       for (Map.Entry<String, List<String>> e : httpHeaders.getRequestHeaders().entrySet()) {
84         parameters.put(e.getKey(), e.getValue());
85       }
86       if (uriInfo.getQueryParameters().containsKey("t-k")) {
87         response = Response.status(Status.OK).entity(chameleonRouter.process(urlContext, queryParams, parameters))
88             .build();
89       } else {
90         response = Response.status(Status.OK).entity(champRouter.process(urlContext, queryParams, parameters)).build();
91       }
92     } catch (DataRouterException ex) {
93       response = Response.status(ex.getHttpStatus()).entity(ex.getMessage()).build();
94
95     } catch (Exception e) {
96       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
97
98     } finally {      
99       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
100       metricsLogger.info(DataRouterMsgs.PROCESSED_REQUEST, "GET",
101           Long.toString(System.currentTimeMillis() - startTimeInMs));
102     }
103     return response;
104
105   }
106
107 }