From c87c5a096920abd37b12f36f967a1524802bc395 Mon Sep 17 00:00:00 2001
From: Stephen Henderson <stephen.henderson@microsoft.com>
Date: Wed, 22 Jan 2020 15:32:04 -0600
Subject: [PATCH] cont'd moving Audit* to final locations

---
 .../osdu/indexer/api/ReindexApi.java          |   2 +-
 .../osdu/indexer/logging/AuditEvents.java     | 218 ++++++++++++++++++
 .../osdu/indexer/logging/AuditLogger.java     | 102 ++++++++
 .../indexer/service/IndexCopyServiceImpl.java |   2 +-
 .../indexer/service/IndexerServiceImpl.java   |   1 +
 .../osdu/indexer/api/ReindexApiTest.java      |   2 +-
 .../osdu/indexer/logging/AuditEventsTest.java |   1 -
 .../osdu/indexer/logging/AuditLoggerTest.java |   1 -
 .../service/IndexCopyServiceImplTest.java     |   2 +-
 .../service/IndexCopyServiceImplTest.java     |   2 +-
 10 files changed, 326 insertions(+), 7 deletions(-)
 create mode 100644 indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java
 create mode 100644 indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java

diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java
index b4a7f30a0..caeeac9c9 100644
--- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java
@@ -15,7 +15,7 @@
 package org.opengroup.osdu.indexer.api;
 
 import org.opengroup.osdu.core.common.model.coreis.SearchServiceRole;
-import org.opengroup.osdu.core.common.model.indexer.AuditLogger;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
 import org.opengroup.osdu.core.common.model.indexer.RecordReindexRequest;
 import org.opengroup.osdu.indexer.service.ReindexService;
 import org.springframework.http.ResponseEntity;
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java
new file mode 100644
index 000000000..0530c5d43
--- /dev/null
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java
@@ -0,0 +1,218 @@
+// Copyright 2017-2019, Schlumberger
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.opengroup.osdu.indexer.logging;
+
+import com.google.common.base.Strings;
+import org.opengroup.osdu.core.common.service.logging.audit.AuditAction;
+import org.opengroup.osdu.core.common.service.logging.audit.AuditPayload;
+import org.opengroup.osdu.core.common.service.logging.audit.AuditStatus;
+
+import java.util.List;
+
+public class AuditEvents {
+
+    private static final String INDEX_CREATE_RECORD_ACTION_ID = "IN001";
+    private static final String INDEX_CREATE_RECORDS_SUCCESS = "Successfully created record in index";
+    private static final String INDEX_CREATE_RECORDS_FAILURE = "Failed creating record in index";
+
+    private static final String INDEX_UPDATE_RECORD_ACTION_ID = "IN002";
+    private static final String INDEX_UPDATE_RECORDS_SUCCESS = "Successfully updated record in index";
+    private static final String INDEX_UPDATE_RECORDS_FAILURE = "Failed updating record in index";
+
+    private static final String INDEX_DELETE_RECORD_ACTION_ID = "IN003";
+    private static final String INDEX_DELETE_RECORDS_SUCCESS = "Successfully deleted record in index";
+    private static final String INDEX_DELETE_RECORDS_FAILURE = "Failed deleting record in index";
+
+    private static final String INDEX_PURGE_RECORD_ACTION_ID = "IN004";
+
+    private static final String REINDEX_KIND_ACTION_ID = "IN007";
+    private static final String REINDEX_KIND_OPERATION = "Reindex kind";
+
+    private static final String COPY_INDEX_ACTION_ID = "IN008";
+    private static final String COPY_INDEX_OPERATION = "Copy index";
+
+    private static final String GET_TASK_STATUS_ACTION_ID = "IN009";
+    private static final String GET_TASK_STATUS_OPERATION = "Get task status";
+
+    private static final String RUN_JOB_ACTION_ID = "IN010";
+    private static final String RUN_JOB_MESSAGE_SUCCESS = "Index clean-up status job run success";
+
+    private static final String INDEX_MAPPING_UPDATE_ACTION_ID = "IN0011";
+    private static final String INDEX_MAPPING_UPDATE_SUCCESS = "Successfully updated index mapping";
+    private static final String INDEX_MAPPING_UPDATE_FAILURE = "Failed updating index mapping";
+
+    private final String user;
+
+    public AuditEvents(String user) {
+        if (Strings.isNullOrEmpty(user)) {
+            throw new IllegalArgumentException("User not provided for audit events.");
+        }
+        this.user = user;
+    }
+
+    public AuditPayload getIndexCreateRecordSuccessEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.CREATE)
+                .status(AuditStatus.SUCCESS)
+                .actionId(INDEX_CREATE_RECORD_ACTION_ID)
+                .message(INDEX_CREATE_RECORDS_SUCCESS)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexCreateRecordFailEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.CREATE)
+                .status(AuditStatus.FAILURE)
+                .actionId(INDEX_CREATE_RECORD_ACTION_ID)
+                .message(INDEX_CREATE_RECORDS_FAILURE)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexUpdateRecordSuccessEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.UPDATE)
+                .status(AuditStatus.SUCCESS)
+                .actionId(INDEX_UPDATE_RECORD_ACTION_ID)
+                .message(INDEX_UPDATE_RECORDS_SUCCESS)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexUpdateRecordFailEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.UPDATE)
+                .status(AuditStatus.FAILURE)
+                .actionId(INDEX_UPDATE_RECORD_ACTION_ID)
+                .message(INDEX_UPDATE_RECORDS_FAILURE)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexDeleteRecordSuccessEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.DELETE)
+                .status(AuditStatus.SUCCESS)
+                .actionId(INDEX_DELETE_RECORD_ACTION_ID)
+                .message(INDEX_DELETE_RECORDS_SUCCESS)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexDeleteRecordFailEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.DELETE)
+                .status(AuditStatus.FAILURE)
+                .actionId(INDEX_DELETE_RECORD_ACTION_ID)
+                .message(INDEX_DELETE_RECORDS_FAILURE)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexPurgeRecordSuccessEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.DELETE)
+                .status(AuditStatus.SUCCESS)
+                .actionId(INDEX_PURGE_RECORD_ACTION_ID)
+                .message(INDEX_DELETE_RECORDS_SUCCESS)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexPurgeRecordFailEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.DELETE)
+                .status(AuditStatus.FAILURE)
+                .actionId(INDEX_PURGE_RECORD_ACTION_ID)
+                .message(INDEX_DELETE_RECORDS_FAILURE)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getReindexEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.CREATE)
+                .status(AuditStatus.SUCCESS)
+                .actionId(REINDEX_KIND_ACTION_ID)
+                .message(REINDEX_KIND_OPERATION)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getCopyIndexEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.CREATE)
+                .status(AuditStatus.SUCCESS)
+                .actionId(COPY_INDEX_ACTION_ID)
+                .message(COPY_INDEX_OPERATION)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getTaskStatusEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.READ)
+                .status(AuditStatus.SUCCESS)
+                .actionId(GET_TASK_STATUS_ACTION_ID)
+                .message(GET_TASK_STATUS_OPERATION)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
+    public AuditPayload getIndexCleanUpJobRunEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.JOB_RUN)
+                .status(AuditStatus.SUCCESS)
+                .user(this.user)
+                .actionId(RUN_JOB_ACTION_ID)
+                .message(RUN_JOB_MESSAGE_SUCCESS)
+                .resources(resources)
+                .build();
+    }
+
+    public AuditPayload getIndexMappingUpdateEvent(List<String> resources, boolean isSuccess) {
+        if (isSuccess) {
+            return AuditPayload.builder()
+                    .action(AuditAction.UPDATE)
+                    .status(AuditStatus.SUCCESS)
+                    .actionId(INDEX_MAPPING_UPDATE_ACTION_ID)
+                    .message(INDEX_MAPPING_UPDATE_SUCCESS)
+                    .resources(resources)
+                    .user(this.user)
+                    .build();
+        } else {
+            return AuditPayload.builder()
+                    .action(AuditAction.UPDATE)
+                    .status(AuditStatus.FAILURE)
+                    .actionId(INDEX_MAPPING_UPDATE_ACTION_ID)
+                    .message(INDEX_MAPPING_UPDATE_FAILURE)
+                    .resources(resources)
+                    .user(this.user)
+                    .build();
+        }
+    }
+}
\ No newline at end of file
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java
new file mode 100644
index 000000000..48b7644a1
--- /dev/null
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java
@@ -0,0 +1,102 @@
+// Copyright 2017-2019, Schlumberger
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.opengroup.osdu.indexer.logging;
+
+import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog;
+import org.opengroup.osdu.core.common.service.logging.audit.AuditPayload;
+import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.annotation.RequestScope;
+
+import javax.inject.Inject;
+import java.util.List;
+
+@Component
+@RequestScope
+public class AuditLogger {
+
+    @Inject
+    private JaxRsDpsLog logger;
+    @Inject
+    private IHeadersInfo headers;
+
+    private AuditEvents events = null;
+
+    private AuditEvents getAuditEvents() {
+        if (this.events == null) {
+            this.events = new AuditEvents(this.headers.getUser());
+        }
+        return this.events;
+    }
+
+    public void indexCreateRecordSuccess(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexCreateRecordSuccessEvent(resources));
+    }
+
+    public void indexCreateRecordFail(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexCreateRecordFailEvent(resources));
+    }
+
+    public void indexUpdateRecordSuccess(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexUpdateRecordSuccessEvent(resources));
+    }
+
+    public void indexUpdateRecordFail(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexUpdateRecordFailEvent(resources));
+    }
+
+    public void indexDeleteRecordSuccess(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexDeleteRecordSuccessEvent(resources));
+    }
+
+    public void indexDeleteRecordFail(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexDeleteRecordFailEvent(resources));
+    }
+
+    public void indexPurgeRecordSuccess(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexPurgeRecordSuccessEvent(resources));
+    }
+
+    public void indexPurgeRecordFail(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexPurgeRecordFailEvent(resources));
+    }
+
+    public void getReindex(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getReindexEvent(resources));
+    }
+
+    public void copyIndex(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getCopyIndexEvent(resources));
+    }
+
+    public void getTaskStatus(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getTaskStatusEvent(resources));
+    }
+
+    public void getIndexCleanUpJobRun(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexCleanUpJobRunEvent(resources));
+    }
+
+    public void indexMappingUpdateSuccess(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexMappingUpdateEvent(resources,true));
+    }
+    public void indexMappingUpdateFail(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getIndexMappingUpdateEvent(resources,false));
+    }
+
+    private void writeLog(AuditPayload log) {
+        this.logger.audit(log);
+    }
+}
\ No newline at end of file
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java
index 19280c31e..fb79db44f 100644
--- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java
@@ -30,7 +30,7 @@ import org.opengroup.osdu.core.common.model.core.DpsHeaders;
 import org.opengroup.osdu.core.common.model.core.TenantInfo;
 import org.opengroup.osdu.core.common.model.AppException;
 import org.opengroup.osdu.core.common.service.coreis.*;
-import org.opengroup.osdu.core.common.model.indexer.AuditLogger;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
 import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo;
 import org.opengroup.osdu.is.core.service.ElasticSettingService;
 import org.opengroup.osdu.is.core.util.ElasticClientHandler;
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java
index 78536790b..23fbea57c 100644
--- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java
@@ -37,6 +37,7 @@ import org.opengroup.osdu.core.common.model.indexer.*;
 import org.opengroup.osdu.core.common.model.storage.ConversionStatus;
 import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog;
 import org.opengroup.osdu.core.common.spi.indexer.IPublisher;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
 import org.opengroup.osdu.indexer.util.IndexerQueueTaskBuilder;
 import org.opengroup.osdu.core.common.model.coreis.RequestStatus;
 import org.opengroup.osdu.core.common.model.coreis.RecordChangedMessages;
diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java
index eda802f62..212baccf0 100644
--- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java
+++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java
@@ -20,7 +20,7 @@ import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.opengroup.osdu.core.common.model.AppException;
-import org.opengroup.osdu.core.common.model.indexer.AuditLogger;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
 import org.opengroup.osdu.core.common.model.indexer.RecordReindexRequest;
 import org.opengroup.osdu.indexer.service.ReindexService;
 
diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java
index 05a62448a..3157d0e3a 100644
--- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java
+++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java
@@ -18,7 +18,6 @@ import com.google.common.collect.Lists;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.opengroup.osdu.core.common.service.logging.audit.AuditAction;
-import org.opengroup.osdu.core.common.model.indexer.AuditEvents;
 import org.opengroup.osdu.core.common.service.logging.audit.AuditStatus;
 import org.springframework.test.context.junit4.SpringRunner;
 
diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java
index 5c6ed9873..70fdbdf2c 100644
--- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java
+++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java
@@ -21,7 +21,6 @@ import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
-import org.opengroup.osdu.core.common.model.indexer.AuditLogger;
 import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog;
 import org.opengroup.osdu.core.common.service.logging.audit.AuditPayload;
 import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo;
diff --git a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java b/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java
index 8d17050f3..3fb280b7e 100644
--- a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java
+++ b/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java
@@ -31,7 +31,7 @@ import org.mockito.ArgumentMatchers;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.opengroup.osdu.core.common.model.core.DpsHeaders;
-import org.opengroup.osdu.core.common.model.indexer.AuditLogger;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
 import org.opengroup.osdu.indexer.service.IndexCopyServiceImpl;
 import org.opengroup.osdu.indexer.service.IndexerMappingService;
 import org.opengroup.osdu.core.common.model.core.ClusterSettings;
diff --git a/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java b/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java
index b1a6cccc1..295eed078 100644
--- a/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java
+++ b/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java
@@ -34,7 +34,7 @@ import org.mockito.Mock;
 import org.opengroup.osdu.core.common.model.core.ClusterSettings;
 import org.opengroup.osdu.core.common.model.core.DpsHeaders;
 import org.opengroup.osdu.core.common.model.AppException;
-import org.opengroup.osdu.core.common.model.indexer.AuditLogger;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
 import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo;
 import org.opengroup.osdu.core.common.spi.coreis.IRequestInfo;
 import org.opengroup.osdu.is.core.service.ElasticSettingService;
-- 
GitLab