From 4c901bd07e6dae969b152f885ff2d6211c4b3e84 Mon Sep 17 00:00:00 2001
From: apoorvnaik <apoorvnaik@apache.org>
Date: Tue, 31 Jul 2018 07:06:17 -0700
Subject: [PATCH] ATLAS-2815: Restoring the deleted audit call

Change-Id: Ia40cab9a258eb7e679975b414c7d6caf9eb6cbb5
---
 webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 49 insertions(+), 5 deletions(-)

diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
index d488b1d..5747f92 100644
--- a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
+++ b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
@@ -18,8 +18,10 @@
 package org.apache.atlas.web.rest;
 
 import org.apache.atlas.AtlasErrorCode;
+import org.apache.atlas.EntityAuditEvent;
 import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.model.TypeCategory;
+import org.apache.atlas.model.audit.EntityAuditEventV2;
 import org.apache.atlas.model.instance.AtlasClassification;
 import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo;
 import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
@@ -27,6 +29,8 @@ import org.apache.atlas.model.instance.AtlasEntityHeader;
 import org.apache.atlas.model.instance.ClassificationAssociateRequest;
 import org.apache.atlas.model.instance.EntityMutationResponse;
 import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
+import org.apache.atlas.repository.audit.EntityAuditRepository;
+import org.apache.atlas.repository.converters.AtlasInstanceConverter;
 import org.apache.atlas.repository.store.graph.AtlasEntityStore;
 import org.apache.atlas.repository.store.graph.v2.AtlasEntityStream;
 import org.apache.atlas.repository.store.graph.v2.EntityStream;
@@ -39,6 +43,7 @@ import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.inject.Inject;
@@ -61,17 +66,24 @@ import java.util.Map;
 @Singleton
 @Service
 public class EntityREST {
+    private static final Logger LOG      = LoggerFactory.getLogger(EntityREST.class);
     private static final Logger PERF_LOG = AtlasPerfTracer.getPerfLogger("rest.EntityREST");
 
     public static final String PREFIX_ATTR = "attr:";
 
-    private final AtlasTypeRegistry         typeRegistry;
-    private final AtlasEntityStore          entitiesStore;
+    private final AtlasTypeRegistry      typeRegistry;
+    private final AtlasEntityStore       entitiesStore;
+    private final EntityAuditRepository  auditRepository;
+    private final AtlasInstanceConverter instanceConverter;
+
 
     @Inject
-    public EntityREST(AtlasTypeRegistry typeRegistry, AtlasEntityStore entitiesStore) {
-        this.typeRegistry    = typeRegistry;
-        this.entitiesStore   = entitiesStore;
+    public EntityREST(AtlasTypeRegistry typeRegistry, AtlasEntityStore entitiesStore,
+                      EntityAuditRepository auditRepository, AtlasInstanceConverter instanceConverter) {
+        this.typeRegistry      = typeRegistry;
+        this.entitiesStore     = entitiesStore;
+        this.auditRepository   = auditRepository;
+        this.instanceConverter = instanceConverter;
     }
 
     /**
@@ -701,6 +713,38 @@ public class EntityREST {
         }
     }
 
+    @GET
+    @Path("{guid}/audit")
+    @Consumes(Servlets.JSON_MEDIA_TYPE)
+    @Produces(Servlets.JSON_MEDIA_TYPE)
+    public List<EntityAuditEventV2> getAuditEvents(@PathParam("guid") String guid, @QueryParam("startKey") String startKey,
+                                                   @QueryParam("count") @DefaultValue("100") short count) throws AtlasBaseException {
+        AtlasPerfTracer perf = null;
+
+        try {
+            if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
+                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityREST.getAuditEvents(" + guid + ", " + startKey + ", " + count + ")");
+            }
+
+            List                     events = auditRepository.listEvents(guid, startKey, count);
+            List<EntityAuditEventV2> ret    = new ArrayList<>();
+
+            for (Object event : events) {
+                if (event instanceof EntityAuditEventV2) {
+                    ret.add((EntityAuditEventV2) event);
+                } else if (event instanceof EntityAuditEvent) {
+                    ret.add(instanceConverter.toV2AuditEvent((EntityAuditEvent) event));
+                } else {
+                    LOG.warn("unknown entity-audit event type {}. Ignored", event != null ? event.getClass().getCanonicalName() : "null");
+                }
+            }
+
+            return ret;
+        } finally {
+            AtlasPerfTracer.log(perf);
+        }
+    }
+
     private AtlasEntityType ensureEntityType(String typeName) throws AtlasBaseException {
         AtlasEntityType ret = typeRegistry.getEntityTypeByName(typeName);
 
--
libgit2 0.27.1