Migrate Spike code to ONAP
[aai/spike.git] / src / main / java / org / onap / aai / spike / service / EchoService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Spike
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.spike.service;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.Response.Status;
29 import org.onap.aai.cl.api.LogFields;
30 import org.onap.aai.cl.api.LogLine;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.cl.mdc.MdcContext;
34 import org.onap.aai.spike.logging.SpikeMsgs;
35 import org.onap.aai.spike.util.SpikeConstants;
36 import org.slf4j.MDC;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.RequestHeader;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RestController;
44
45 @RestController
46 @RequestMapping(value = "/services/spike/v1/echo-service")
47 public class EchoService {
48
49     private static Logger logger = LoggerFactory.getInstance().getLogger(EchoService.class.getName());
50     private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EchoService.class.getName());
51
52     @GetMapping("/echo")
53     public ResponseEntity<String> ping(@RequestHeader HttpHeaders headers, HttpServletRequest req) {
54
55         String fromIp = req.getRemoteAddr();
56         String fromAppId = "";
57         String transId = null;
58
59         if (headers.getFirst("X-FromAppId") != null) {
60             fromAppId = headers.getFirst("X-FromAppId");
61         }
62
63         if ((headers.getFirst("X-TransactionId") == null) || headers.getFirst("X-TransactionId").isEmpty()) {
64             transId = java.util.UUID.randomUUID().toString();
65         } else {
66             transId = headers.getFirst("X-TransactionId");
67         }
68
69         MdcContext.initialize(transId, SpikeConstants.SPIKE_SERVICE_NAME, "", fromAppId, fromIp);
70
71         // Generate error log
72         logger.info(SpikeMsgs.PROCESS_REST_REQUEST, req.getMethod(), req.getRequestURL().toString(),
73                 req.getRemoteHost(), Status.OK.toString());
74
75         // Generate audit log.
76         auditLogger.info(SpikeMsgs.PROCESS_REST_REQUEST,
77                 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, Status.OK.toString())
78                         .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, Status.OK.toString()),
79                 (req != null) ? req.getMethod() : "Unknown", (req != null) ? req.getRequestURL().toString() : "Unknown",
80                 (req != null) ? req.getRemoteHost() : "Unknown", Status.OK.toString());
81         MDC.clear();
82
83         return new ResponseEntity<>("Alive", HttpStatus.OK);
84     }
85 }