Add logs for device operations

pull/136/head
prathabanKavin 2 years ago
parent 681bcd6c5a
commit ae2ec5a37b

@ -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<DeviceIdentifier> 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<? extends Operation> 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);
}

@ -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() +

@ -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);
}

@ -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;
}

@ -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());
}
}
}

Loading…
Cancel
Save