From c74cd7e7d7edfc1b5333c3bb53d080f5c3a0e052 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Mon, 25 May 2020 19:09:50 +0530 Subject: [PATCH] Add report syncing functionality --- .../mgt/core/DeviceManagementConstants.java | 1 + .../DeviceManagementProviderServiceImpl.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 7f7610126b..f063c665ee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -168,5 +168,6 @@ public final class DeviceManagementConstants { public static final String REPORTING_CONTEXT = "/reporting/api/analyticsadmin/v1.0/event"; public static final String DEVICE_INFO_PARAM = "device-info"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; + public static final String ENROLLMENT_INFO_ENDPOINT = REPORTING_CONTEXT + "/enrolment-info"; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index f89dbce3ad..79f5ae11f8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -57,6 +57,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo; import org.wso2.carbon.device.mgt.common.configuration.mgt.EnrollmentConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.device.details.DeviceData; +import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; @@ -67,6 +68,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; +import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.exceptions.UnauthorizedDeviceAccessException; @@ -114,6 +116,7 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; import org.wso2.carbon.email.sender.core.ContentProviderInfo; import org.wso2.carbon.email.sender.core.EmailContext; import org.wso2.carbon.email.sender.core.EmailSendingFailedException; @@ -395,6 +398,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } addInitialOperations(deviceIdentifier, device.getType()); sendNotification(device); + Runnable task = () -> syncReportingApp(device, tenantId); + new Thread(task).start(); } extractDeviceLocationToUpdate(device); try { @@ -4186,4 +4191,26 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } + + /** + * Sync with reporting App + * + * @param device Enrolling Device + * @param tenantId Tenant Id + */ + private void syncReportingApp (Device device, int tenantId) { + + String reportingHost = HttpReportingUtil.getReportingHost(); + if (StringUtils.isNotBlank(reportingHost) && HttpReportingUtil.isPublishingEnabledForTenant()) { + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setTenantId(tenantId); + deviceDetailsWrapper.setDevice(device); + try { + HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), + reportingHost + DeviceManagementConstants.Report.ENROLLMENT_INFO_ENDPOINT); + } catch (EventPublishingException e) { + log.warn("Error occurred while syncing with reporting app"); + } + } + } }