e9a2830c7ce9e518e9ecad07ffcb3206774892e2
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / ChampRESTAPI.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.champ;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import java.util.Timer;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.POST;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.QueryParam;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.HttpHeaders;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import javax.ws.rs.core.Response.Status;
47 import javax.ws.rs.core.UriInfo;
48
49 import org.json.JSONException;
50 import org.json.JSONObject;
51 import org.onap.aai.champcore.ChampTransaction;
52 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
53 import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
54 import org.onap.aai.champcore.exceptions.ChampTransactionException;
55 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
56 import org.onap.aai.champcore.model.ChampObject;
57 import org.onap.aai.champcore.model.ChampRelationship;
58 import org.onap.aai.cl.api.Logger;
59 import org.onap.aai.cl.eelf.LoggerFactory;
60 import org.onap.champ.async.ChampAsyncRequestProcessor;
61 import org.onap.champ.entity.ChampObjectDeserializer;
62 import org.onap.champ.entity.ChampObjectSerializer;
63 import org.onap.champ.entity.ChampRelationshipDeserializer;
64 import org.onap.champ.entity.ChampRelationshipSerializer;
65 import org.onap.champ.exception.ChampServiceException;
66 import org.onap.champ.service.ChampDataService;
67 import org.onap.champ.service.logging.ChampMsgs;
68 import org.onap.champ.service.logging.LoggingUtil;
69 import org.onap.champ.util.ChampProperties;
70 import org.onap.champ.util.ChampServiceConstants;
71
72 import com.fasterxml.jackson.core.JsonProcessingException;
73 import com.fasterxml.jackson.databind.ObjectMapper;
74 import com.fasterxml.jackson.databind.module.SimpleModule;
75
76 @Path(value = "/")
77 public class ChampRESTAPI {
78
79   private ObjectMapper mapper;
80
81   private ChampDataService champDataService;
82   private String TRANSACTION_METHOD = "method";
83   private Timer timer;
84
85   private Logger logger = LoggerFactory.getInstance().getLogger(ChampRESTAPI.class);
86   Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(ChampRESTAPI.class.getName());
87   private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(ChampRESTAPI.class.getName());
88
89   public ChampRESTAPI(ChampDataService champDataService, ChampAsyncRequestProcessor champAsyncRequestProcessor) {
90     this.champDataService = champDataService;
91
92     // Async request handling is optional.  
93     if (champAsyncRequestProcessor != null) {
94       timer = new Timer("ChampAsyncRequestProcessor-1");
95       timer.schedule(champAsyncRequestProcessor, champAsyncRequestProcessor.getRequestPollingTimeSeconds(),
96           champAsyncRequestProcessor.getRequestPollingTimeSeconds());
97     }
98
99     mapper = new ObjectMapper();
100     SimpleModule module = new SimpleModule();
101     module.addSerializer(ChampObject.class, new ChampObjectSerializer());
102     module.addDeserializer(ChampObject.class, new ChampObjectDeserializer());
103     module.addSerializer(ChampRelationship.class, new ChampRelationshipSerializer());
104     module.addDeserializer(ChampRelationship.class, new ChampRelationshipDeserializer());
105     mapper.registerModule(module);
106   }
107
108   @GET
109   @Path("echo")
110   @Produces(MediaType.TEXT_PLAIN)
111   public Response echo() {
112     return Response.ok().entity("alive").build();
113   }
114
115   @GET
116   @Path("objects/{objectId}")
117   @Produces(MediaType.APPLICATION_JSON)
118   public Response getObject(@PathParam("objectId") String objectId, @QueryParam("transactionId") String tId,
119       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
120     LoggingUtil.initMdcContext(req, headers);
121     long startTimeInMs = System.currentTimeMillis();
122     logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
123
124     Response response = null;
125     ChampObject retrieved;
126
127     try {
128       ChampTransaction transaction = champDataService.getTransaction(tId);
129
130       if (tId != null && transaction == null) {
131         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
132       }
133       retrieved = champDataService.getObject(objectId, Optional.ofNullable(transaction));
134       if (retrieved == null) {
135         response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
136       } else {
137         response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
138       }
139
140     } catch (JsonProcessingException e) {
141       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
142     } catch (ChampServiceException ce) {
143       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
144     } finally {
145       logger.debug(response.getEntity().toString());
146       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
147       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
148     }
149
150     return response;
151   }
152
153   @DELETE
154   @Path("objects/{objectId}")
155   public Response deleteObject(@PathParam("objectId") String objectId, @QueryParam("transactionId") String tId,
156       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
157     LoggingUtil.initMdcContext(req, headers);
158     long startTimeInMs = System.currentTimeMillis();
159     logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId);
160     ChampObject retrieved;
161     Response response = null;
162     try {
163       ChampTransaction transaction = champDataService.getTransaction(tId);
164
165       if (tId != null && transaction == null) {
166         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
167       }
168       champDataService.deleteObject(objectId, Optional.ofNullable(transaction));
169
170       response = Response.status(Status.OK).build();
171     } catch (ChampObjectNotExistsException e) {
172       response = Response.status(Status.NOT_FOUND).entity(objectId + " not found").build();
173     } catch (ChampServiceException ce) {
174       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
175     } catch (ChampTransactionException | ChampUnmarshallingException e) {
176       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
177     } finally {
178       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
179       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "DELETE",
180           Long.toString(System.currentTimeMillis() - startTimeInMs));
181     }
182     return response;
183   }
184
185   @POST
186   @Path("objects")
187   @Consumes(MediaType.APPLICATION_JSON)
188   @Produces(MediaType.APPLICATION_JSON)
189   public Response postObject(String champObj, @QueryParam("transactionId") String tId, @Context HttpHeaders headers,
190       @Context UriInfo uriInfo, @Context HttpServletRequest req) {
191     LoggingUtil.initMdcContext(req, headers);
192     long startTimeInMs = System.currentTimeMillis();
193     logger.info(ChampMsgs.INCOMING_REQUEST, tId, champObj);
194     Response response = null;
195     try {
196       ChampTransaction transaction = champDataService.getTransaction(tId);
197       if (tId != null && transaction == null) {
198         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
199       }
200       ChampObject champObject = mapper.readValue(champObj, ChampObject.class);
201
202       ChampObject created = champDataService.storeObject(champObject, Optional.ofNullable(transaction));
203       response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
204     } catch (IOException e) {
205       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
206     } catch (ChampServiceException ce) {
207       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
208     } catch (Exception e) {
209       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
210       LoggingUtil.logInternalError(logger, e);
211     } finally {
212       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
213       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST",
214           Long.toString(System.currentTimeMillis() - startTimeInMs));
215     }
216     return response;
217   }
218
219   @PUT
220   @Path("objects/{objectId}")
221   @Consumes(MediaType.APPLICATION_JSON)
222   @Produces(MediaType.APPLICATION_JSON)
223   public Response putObject(@PathParam("objectId") String objectId, String champObj,
224       @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
225       @Context HttpServletRequest req) {
226     LoggingUtil.initMdcContext(req, headers);
227     long startTimeInMs = System.currentTimeMillis();
228     logger.info(ChampMsgs.INCOMING_REQUEST, tId, objectId + " " + champObj);
229
230     Response response = null;
231     try {
232       ChampTransaction transaction = champDataService.getTransaction(tId);
233       if (tId != null && transaction == null) {
234         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
235       }
236
237       ChampObject co = mapper.readValue(champObj, ChampObject.class);
238       // check if key is present or if it equals the key that is in the URI
239       ChampObject updated = champDataService.replaceObject(co, objectId, Optional.ofNullable(transaction));
240
241       response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
242     } catch (IOException e) {
243       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
244     } catch (ChampServiceException ce) {
245       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
246     } catch (Exception e) {
247       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
248       LoggingUtil.logInternalError(logger, e);
249     } finally {
250       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
251       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
252     }
253     return response;
254   }
255
256   @GET
257   @Path("objects/relationships/{oId}")
258   @Produces(MediaType.APPLICATION_JSON)
259   public Response getEdges(@PathParam("oId") String oId, @QueryParam("transactionId") String tId,
260       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
261     LoggingUtil.initMdcContext(req, headers);
262     long startTimeInMs = System.currentTimeMillis();
263     List<ChampRelationship> retrieved;
264     Optional<ChampObject> rObject;
265     Response response = null;
266     ChampTransaction transaction = null;
267     try {
268
269       retrieved = champDataService.getRelationshipsByObject(oId, Optional.ofNullable(transaction));
270       response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
271     } catch (JsonProcessingException e) {
272       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
273     } catch (ChampServiceException ce) {
274       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
275     } catch (Exception e) {
276       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
277       LoggingUtil.logInternalError(logger, e);
278     } finally {
279       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
280       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
281     }
282     return response;
283   }
284
285   @GET
286   @Path("objects/filter/")
287   @Produces(MediaType.APPLICATION_JSON)
288   public Response filterObject(@Context HttpHeaders headers, @Context UriInfo uriInfo,
289       @Context HttpServletRequest req) {
290     LoggingUtil.initMdcContext(req, headers);
291     long startTimeInMs = System.currentTimeMillis();
292     String propertiesKey = ChampProperties.get(ChampServiceConstants.CHAMP_COLLECTION_PROPERTIES_KEY);
293     List<ChampObject> objects;
294     Map<String, Object> filter = new HashMap<>();
295
296     for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
297       if (!e.getKey().equals(propertiesKey)) {
298         filter.put(e.getKey(), e.getValue().get(0));
299       }
300     }
301
302     HashSet<String> properties;
303     if (uriInfo.getQueryParameters().containsKey(propertiesKey)) {
304       properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey));
305     } else {
306       properties = new HashSet<>();
307     }
308
309     Response response = null;
310     try {
311       objects = champDataService.queryObjects(filter, properties);
312       response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(objects))
313           .build();
314     } catch (JsonProcessingException e) {
315       e.printStackTrace();
316       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
317     } catch (ChampServiceException e1) {
318       response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
319     } catch (Exception e) {
320       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
321       LoggingUtil.logInternalError(logger, e);
322     } finally {
323       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
324       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
325     }
326     return response;
327   }
328
329   @GET
330   @Path("relationships/{rId}")
331   @Produces(MediaType.APPLICATION_JSON)
332   public Response getRelationship(@PathParam("rId") String rId, @QueryParam("transactionId") String tId,
333       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
334     LoggingUtil.initMdcContext(req, headers);
335     long startTimeInMs = System.currentTimeMillis();
336     logger.info(ChampMsgs.INCOMING_REQUEST, tId, rId);
337     ChampRelationship retrieved;
338     Response response = null;
339     try {
340       ChampTransaction transaction = champDataService.getTransaction(tId);
341
342       if (tId != null && transaction == null) {
343         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
344       }
345       retrieved = champDataService.getRelationship(rId, Optional.ofNullable(transaction));
346       if (retrieved == null) {
347         response = Response.status(Status.NOT_FOUND).entity(rId + " not found").build();
348         return response;
349       }
350       response = Response.status(Status.OK).entity(mapper.writeValueAsString(retrieved)).build();
351
352     } catch (IOException e) {
353       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
354     } catch (ChampServiceException ce) {
355       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
356     } catch (Exception e) {
357       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
358       LoggingUtil.logInternalError(logger, e);
359     } finally {
360       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
361       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
362     }
363     return response;
364   }
365
366   @POST
367   @Path("relationships")
368   @Consumes(MediaType.APPLICATION_JSON)
369   @Produces(MediaType.APPLICATION_JSON)
370   public Response postRelationships(String relationship, @QueryParam("transactionId") String tId,
371       @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
372     LoggingUtil.initMdcContext(req, headers);
373     long startTimeInMs = System.currentTimeMillis();
374     logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
375     Response response = null;
376     try {
377       ChampTransaction transaction = champDataService.getTransaction(tId);
378       if (tId != null && transaction == null) {
379         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
380       }
381       ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
382
383       ChampRelationship created = champDataService.storeRelationship(r, Optional.ofNullable(transaction));
384
385       response = Response.status(Status.CREATED).entity(mapper.writeValueAsString(created)).build();
386     } catch (IOException e) {
387       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
388     } catch (ChampServiceException ce) {
389       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
390     } catch (Exception e) {
391       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
392       LoggingUtil.logInternalError(logger, e);
393     } finally {
394       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
395       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST",
396           Long.toString(System.currentTimeMillis() - startTimeInMs));
397     }
398     return response;
399   }
400
401   @PUT
402   @Path("relationships/{rId}")
403   @Consumes(MediaType.APPLICATION_JSON)
404   @Produces(MediaType.APPLICATION_JSON)
405   public Response updateRelationship(@PathParam("rId") String rId, String relationship,
406       @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
407       @Context HttpServletRequest req) {
408     LoggingUtil.initMdcContext(req, headers);
409     long startTimeInMs = System.currentTimeMillis();
410     logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationship);
411
412     Response response = null;
413     try {
414       ChampTransaction transaction = champDataService.getTransaction(tId);
415       if (tId != null && transaction == null) {
416         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
417       }
418       ChampRelationship r = mapper.readValue(relationship, ChampRelationship.class);
419       ChampRelationship updated = champDataService.updateRelationship(r, rId, Optional.ofNullable(transaction));
420
421       response = Response.status(Status.OK).entity(mapper.writeValueAsString(updated)).build();
422     } catch (IOException e) {
423       response = Response.status(Status.BAD_REQUEST).entity("Unable to parse the payload").build();
424     } catch (ChampServiceException ce) {
425       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
426     } catch (Exception e) {
427       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
428       LoggingUtil.logInternalError(logger, e);
429     } finally {
430       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
431       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
432     }
433     return response;
434   }
435
436   @DELETE
437   @Path("relationships/{relationshipId}")
438   public Response deleteRelationship(@PathParam("relationshipId") String relationshipId,
439       @QueryParam("transactionId") String tId, @Context HttpHeaders headers, @Context UriInfo uriInfo,
440       @Context HttpServletRequest req) {
441     LoggingUtil.initMdcContext(req, headers);
442     long startTimeInMs = System.currentTimeMillis();
443     logger.info(ChampMsgs.INCOMING_REQUEST, tId, relationshipId);
444
445     Response response = null;
446     try {
447       ChampTransaction transaction = champDataService.getTransaction(tId);
448       if (tId != null && transaction == null) {
449         throw new ChampServiceException("transactionId not found", Status.BAD_REQUEST);
450       }
451       champDataService.deleteRelationship(relationshipId, Optional.ofNullable(transaction));
452       response = Response.status(Status.OK).build();
453
454     } catch (ChampRelationshipNotExistsException e) {
455       response = Response.status(Status.NOT_FOUND).entity(relationshipId + " not found").build();
456     } catch (ChampServiceException ce) {
457       response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
458     } catch (ChampTransactionException | ChampUnmarshallingException e) {
459       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
460     } finally {
461       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
462       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "DELETE",
463           Long.toString(System.currentTimeMillis() - startTimeInMs));
464     }
465     return response;
466   }
467
468   @GET
469   @Path("relationships/filter/")
470   @Produces(MediaType.APPLICATION_JSON)
471   public Response filterMethod(@Context HttpHeaders headers, @Context UriInfo uriInfo,
472       @Context HttpServletRequest req) {
473     LoggingUtil.initMdcContext(req, headers);
474     long startTimeInMs = System.currentTimeMillis();
475     List<ChampRelationship> list;
476     Map<String, Object> filter = new HashMap<>();
477     for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
478       filter.put(e.getKey(), e.getValue().get(0));
479     }
480     Response response = null;
481     try {
482       list = champDataService.queryRelationships(filter);
483       response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(mapper.writeValueAsString(list))
484           .build();
485     } catch (JsonProcessingException e) {
486       e.printStackTrace();
487       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
488     } catch (ChampServiceException e1) {
489       response = Response.status(e1.getHttpStatus()).entity(e1.getMessage()).build();
490     } catch (Exception e) {
491       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
492       LoggingUtil.logInternalError(logger, e);
493     } finally {
494       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
495       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
496     }
497     return response;
498   }
499
500   @POST
501   @Path("transaction")
502   @Produces(MediaType.TEXT_PLAIN)
503   public Response openTransaction(@Context HttpHeaders headers, @Context UriInfo uriInfo,
504       @Context HttpServletRequest req) {
505     LoggingUtil.initMdcContext(req, headers);
506     long startTimeInMs = System.currentTimeMillis();
507     Status s;
508     String transaction = champDataService.openTransaction();
509
510     s = Status.OK;
511     Response response = Response.status(s).entity(transaction).build();
512     logger.info(ChampMsgs.PROCESS_EVENT, "Opened Transaction with ID: " + transaction, s.toString());
513     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
514     metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "POST", Long.toString(System.currentTimeMillis() - startTimeInMs));
515     return response;
516   }
517
518   @GET
519   @Path("transaction/{tId}")
520   public Response getSpecificTransaction(@PathParam("tId") String tId, @Context HttpHeaders headers,
521       @Context UriInfo uriInfo, @Context HttpServletRequest req) {
522     LoggingUtil.initMdcContext(req, headers);
523     long startTimeInMs = System.currentTimeMillis();
524
525     Response response = null;
526     ChampTransaction transaction = champDataService.getTransaction(tId);
527     if (transaction == null) {
528       response = Response.status(Status.NOT_FOUND).entity("transaction " + tId + " not found").build();
529       return response;
530     }
531
532     try {
533       response = Response.status(Status.OK).entity(mapper.writeValueAsString(tId + " is OPEN")).build();
534     } catch (JsonProcessingException e) {
535       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
536     } catch (Exception e) {
537       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
538       LoggingUtil.logInternalError(logger, e);
539     } finally {
540       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
541       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "GET", Long.toString(System.currentTimeMillis() - startTimeInMs));
542     }
543     return response;
544   }
545
546   @PUT
547   @Path("transaction/{tId}")
548   @Produces(MediaType.TEXT_PLAIN)
549   @Consumes(MediaType.APPLICATION_JSON)
550   public Response updateTransaction(String t, @PathParam("tId") String tId, @Context HttpHeaders headers,
551       @Context UriInfo uriInfo, @Context HttpServletRequest req) {
552     LoggingUtil.initMdcContext(req, headers);
553     long startTimeInMs = System.currentTimeMillis();
554     logger.info(ChampMsgs.INCOMING_REQUEST, tId, "COMMIT/ROLLBACK");
555
556     Response response = null;
557     try {
558       JSONObject jsonObj = new JSONObject(t);
559       String method = jsonObj.getString(this.TRANSACTION_METHOD);
560
561       if (method.equals("commit")) {
562         champDataService.commitTransaction(tId);
563         response = Response.status(Status.OK).entity("COMMITTED").build();
564
565       } else if (method.equals("rollback")) {
566         champDataService.rollbackTransaction(tId);
567         response = Response.status(Status.OK).entity("ROLLED BACK").build();
568       } else {
569         response = Response.status(Status.BAD_REQUEST).entity("Invalid Method: " + method).build();
570         return response;
571       }
572
573     } catch (ChampTransactionException e) {
574       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
575     } catch (JSONException e) {
576       response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
577     } catch (ChampServiceException e) {
578       response = Response.status(e.getHttpStatus()).entity(e.getMessage()).build();
579     } catch (Exception e) {
580       response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
581       LoggingUtil.logInternalError(logger, e);
582     } finally {
583       LoggingUtil.logRestRequest(logger, auditLogger, req, response);
584       metricsLogger.info(ChampMsgs.PROCESSED_REQUEST, "PUT", Long.toString(System.currentTimeMillis() - startTimeInMs));
585     }
586     return response;
587   }
588
589 }