From ae2ec5a37b79fa834c55ece2a732f04dcfc79f57 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Thu, 15 Jun 2023 00:16:48 +0530 Subject: [PATCH] Add logs for device operations --- .../operation/mgt/OperationManagerImpl.java | 19 +++-- .../DeviceManagementProviderServiceImpl.java | 11 ++- .../logger/DeviceConnectivityLogContext.java | 80 +++++++++++++++++++ .../logger/DeviceEnrolmentLogContext.java | 31 +++++++ .../logger/util/MDCContextUtil.java | 21 +++++ 5 files changed, 153 insertions(+), 9 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java index b037eeebb7..51b93d5fdd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -177,6 +177,8 @@ public class OperationManagerImpl implements OperationManager { } } + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); List validDeviceIds = deviceValidationResult.getValidDeviceIDList(); if (!validDeviceIds.isEmpty()) { @@ -253,6 +255,10 @@ public class OperationManagerImpl implements OperationManager { activity.setActivityStatus( this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult)); } + for (DeviceIdentifier deviceId : authorizedDeviceIds) { + device = getDevice(deviceId); + log.info("Operation added", deviceConnectivityLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(deviceType).setActionTag("ADD_OPERATION").setTenantDomain(tenantDomain).setTenantId(tenantId).setUserName(initiatedBy).setOperationCode(operationCode).build()); + } return activity; } catch (OperationManagementDAOException e) { OperationManagementDAOFactory.rollbackTransaction(); @@ -699,7 +705,9 @@ public class OperationManagerImpl implements OperationManager { if (log.isDebugEnabled()) { log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() + "]"); } - + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + @@ -712,16 +720,18 @@ public class OperationManagerImpl implements OperationManager { case INACTIVE: case UNREACHABLE: this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceId, tenantId); break; } - log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(deviceId.getId()).setDeviceType(deviceId.getType()).build()); + log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(deviceId.getId()).setDeviceType(deviceId.getType()).setActionTag("PENDING_OPERATION").setTenantDomain(tenantDomain).setTenantId(String.valueOf(tenantId)).setUserName(userName).build()); return getOperations(deviceId, Operation.Status.PENDING, enrolmentId); } @Override public List getPendingOperations(Device device) throws OperationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + @@ -739,11 +749,10 @@ public class OperationManagerImpl implements OperationManager { this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE); enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); device.setEnrolmentInfo(enrolmentInfo); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier, device, tenantId); break; } - log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(device.getDeviceIdentifier()).setDeviceType(device.getType()).build()); + log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(device.getDeviceIdentifier()).setDeviceType(device.getType()).setActionTag("PENDING_OPERATION").setTenantDomain(tenantDomain).setTenantId(String.valueOf(tenantId)).setUserName(userName).build()); return getOperations(deviceIdentifier, Operation.Status.PENDING, enrolmentId); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index bb28ec8c7e..8dcaf1ebca 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -263,6 +263,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return false; } + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); EnrollmentConfiguration enrollmentConfiguration = DeviceManagerUtil.getEnrollmentConfigurationEntry(); String deviceSerialNumber = null; if (enrollmentConfiguration != null) { @@ -349,7 +351,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() + "'"); } - log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(existingDevice.getId())).setDeviceType(String.valueOf(existingDevice.getType())).setOwner(newEnrolmentInfo.getOwner()).setOwnership(String.valueOf(newEnrolmentInfo.getOwnership())).setTenantID(String.valueOf(tenantId)).build()); + log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(existingDevice.getId())).setDeviceType(String.valueOf(existingDevice.getType())).setOwner(newEnrolmentInfo.getOwner()).setOwnership(String.valueOf(newEnrolmentInfo.getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); status = true; } else { log.warn("Unable to update device enrollment for device : " + device.getDeviceIdentifier() + @@ -386,7 +388,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } device.setEnrolmentInfo(enrollment); DeviceManagementDAOFactory.commitTransaction(); - log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(enrollment.getOwner()).setOwnership(String.valueOf(enrollment.getOwnership())).setTenantID(String.valueOf(tenantId)).build()); + log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(enrollment.getOwner()).setOwnership(String.valueOf(enrollment.getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); } else { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("No device type registered with name - " + device.getType() @@ -575,7 +577,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } int tenantId = this.getTenantId(); - + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Device device = this.getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { @@ -609,7 +612,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } //procees to dis-enroll a device from traccar ends - log.info("Device disenrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(device.getEnrolmentInfo().getOwner()).setOwnership(String.valueOf(device.getEnrolmentInfo().getOwnership())).setTenantID(String.valueOf(tenantId)).build()); + log.info("Device disenrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(device.getEnrolmentInfo().getOwner()).setOwnership(String.valueOf(device.getEnrolmentInfo().getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while dis-enrolling '" + deviceId.getType() + diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java index 813814165a..d110e84a43 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java @@ -24,10 +24,20 @@ public class DeviceConnectivityLogContext extends LogContext { private final String deviceId; private final String deviceType; + private final String actionTag; + private final String operationCode; + private final String tenantId; + private final String tenantDomain; + private final String userName; private DeviceConnectivityLogContext(Builder builder) { this.deviceId = builder.deviceId; this.deviceType = builder.deviceType; + this.actionTag = builder.actionTag; + this.operationCode = builder.operationCode; + this.tenantId = builder.tenantId; + this.tenantDomain = builder.tenantDomain; + this.userName = builder.userName; } public String getDeviceId() { @@ -38,9 +48,34 @@ public class DeviceConnectivityLogContext extends LogContext { return deviceType; } + public String getActionTag() { + return actionTag; + } + + public String getOperationCode() { + return operationCode; + } + + public String getTenantId() { + return tenantId; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public String getUserName() { + return userName; + } + public static class Builder { private String deviceId; private String deviceType; + private String operationCode; + private String actionTag; + private String tenantId; + private String tenantDomain; + private String userName; public Builder() { } @@ -63,6 +98,51 @@ public class DeviceConnectivityLogContext extends LogContext { return this; } + public String getOperationCode() { + return operationCode; + } + + public Builder setOperationCode(String operationCode) { + this.operationCode = operationCode; + return this; + } + + public String getTenantId() { + return tenantId; + } + + public Builder setTenantId(String tenantId) { + this.tenantId = tenantId; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getActionTag() { + return actionTag; + } + + public Builder setActionTag(String actionTag) { + this.actionTag = actionTag; + return this; + } + public DeviceConnectivityLogContext build() { return new DeviceConnectivityLogContext(this); } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceEnrolmentLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceEnrolmentLogContext.java index c16d93e6c8..b355b29671 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceEnrolmentLogContext.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceEnrolmentLogContext.java @@ -26,6 +26,8 @@ public class DeviceEnrolmentLogContext extends LogContext { private final String owner; private final String ownership; private final String tenantID; + private final String tenantDomain; + private final String userName; private DeviceEnrolmentLogContext(Builder builder) { this.deviceId = builder.deviceId; @@ -33,6 +35,8 @@ public class DeviceEnrolmentLogContext extends LogContext { this.owner = builder.owner; this.ownership = builder.ownership; this.tenantID = builder.tenantID; + this.tenantDomain = builder.tenantDomain; + this.userName = builder.userName; } public String getTenantID() { @@ -55,12 +59,21 @@ public class DeviceEnrolmentLogContext extends LogContext { return ownership; } + public String getTenantDomain() { + return tenantDomain; + } + + public String getUserName() { + return userName; + } public static class Builder { private String deviceId; private String deviceType; private String owner; private String ownership; private String tenantID; + private String tenantDomain; + private String userName; public Builder() { } @@ -83,6 +96,24 @@ public class DeviceEnrolmentLogContext extends LogContext { return this; } + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + public String getDeviceId() { return deviceId; } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java index bbb7406fb0..dc046e8eeb 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java @@ -120,6 +120,21 @@ public final class MDCContextUtil { if (mdcContext.getDeviceType() != null) { MDC.put("DeviceType", mdcContext.getDeviceType()); } + if (mdcContext.getOperationCode() != null) { + MDC.put("OperationCode", mdcContext.getOperationCode()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getTenantId() != null) { + MDC.put("TenantId", mdcContext.getTenantId()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } + if (mdcContext.getActionTag() != null) { + MDC.put("ActionTag", mdcContext.getActionTag()); + } } public static void populateDeviceEnrolmentMDCContext(final DeviceEnrolmentLogContext mdcContext) { @@ -138,6 +153,12 @@ public final class MDCContextUtil { if (mdcContext.getTenantID() != null) { MDC.put("TenantId", mdcContext.getTenantID()); } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } } }