Moving Historical Queries to spring boot
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / service / EchoService.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 javax.servlet.http.HttpServletRequest;
24 import javax.ws.rs.GET;
25 import javax.ws.rs.Path;
26 import javax.ws.rs.PathParam;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.core.Context;
29 import javax.ws.rs.core.HttpHeaders;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.UriInfo;
32
33 import org.onap.aai.cl.api.LogFields;
34 import org.onap.aai.cl.api.LogLine;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.cl.mdc.MdcContext;
38 import org.onap.aai.datarouter.logging.DataRouterMsgs;
39 import org.onap.aai.datarouter.util.DataRouterConstants;
40 import org.slf4j.MDC;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 @Path("/data-router/v1")
45 public class EchoService {
46
47   private static Logger logger = LoggerFactory.getInstance().getLogger(EchoService.class.getName());
48   private static Logger auditLogger =
49       LoggerFactory.getInstance().getAuditLogger(EchoService.class.getName());
50   private static final String XFROMAPPID = "X-FromAppId";
51   private static final String XTRANSACTIONID = "X-TransactionId";
52
53   @GET
54   @Path("/echo/{input}")
55   @Produces("text/plain")
56   public String ping(@PathParam("input") String input, @Context HttpHeaders headers,
57       @Context UriInfo info, @Context HttpServletRequest req) {
58
59         String fromIp = req.getRemoteAddr();
60     String fromAppId = "";
61     String transId;
62
63     if (headers.getRequestHeaders().getFirst(XFROMAPPID) != null) {
64       fromAppId = headers.getRequestHeaders().getFirst(XFROMAPPID);
65     }
66
67     if ((headers.getRequestHeaders().getFirst(XTRANSACTIONID) == null)
68         || headers.getRequestHeaders().getFirst(XTRANSACTIONID).isEmpty()) {
69       transId = java.util.UUID.randomUUID().toString();
70     } else {
71       transId = headers.getRequestHeaders().getFirst(XTRANSACTIONID);
72     }
73
74     MdcContext.initialize(transId, DataRouterConstants.DATA_ROUTER_SERVICE_NAME, "", fromAppId,
75         fromIp);
76     
77     int status = 200;
78     String respStatusString = "";
79     if (Response.Status.fromStatusCode(status) != null) {
80       respStatusString = Response.Status.fromStatusCode(status).toString();
81     }
82
83     // Generate error log
84     logger.info(DataRouterMsgs.PROCESS_REST_REQUEST, req.getMethod(),
85         req.getRequestURL().toString(), req.getRemoteHost(), Integer.toString(status));
86
87     // Generate audit log.
88     auditLogger.info(DataRouterMsgs.PROCESS_REST_REQUEST,
89         new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, status)
90             .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
91         req.getMethod(), req.getRequestURL().toString(), req.getRemoteHost(), Integer.toString(status));
92     MDC.clear();
93
94     return "Hello, " + input + ".";
95   }
96 }