From 60b4a2493cc0141e81bdd74a0d0e071eeee5ffe4 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 29 Mar 2020 12:59:28 +0530 Subject: [PATCH] merge conflics resoved --- .../device/details/DeviceDetailsWrapper.java | 20 +++++ .../impl/DeviceInformationManagerImpl.java | 85 +++++++++++++++---- .../GroupManagementProviderService.java | 10 +++ .../GroupManagementProviderServiceImpl.java | 36 ++++++++ .../mgt/core/util/DeviceManagerUtil.java | 33 ++++++- .../configuration.hbs | 15 ++++ .../public/js/platform-configuration.js | 13 ++- 7 files changed, 194 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java index 80f637c880..975d27b50f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.common.device.details; import com.google.gson.Gson; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import java.util.List; @@ -31,6 +32,25 @@ public class DeviceDetailsWrapper { DeviceLocation location; int tenantId; + List groups; + String [] role; + + public List getGroups() { + return groups; + } + + public void setGroups(List groups) { + this.groups = groups; + } + + public String [] getRole() { + return role; + } + + public void setRole(String [] role) { + this.role = role; + } + public int getTenantId() { return tenantId; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 5261b56246..d4ad97c043 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -31,6 +31,8 @@ import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; 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.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -41,8 +43,11 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.report.mgt.Constants; +import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; import java.sql.SQLException; import java.util.ArrayList; @@ -58,6 +63,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream"; + private static final String IS_EVENT_PUBLISHING_ENABED = "isEventPublishingEnabled"; public DeviceInformationManagerImpl() { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); @@ -79,6 +85,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { @Override public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { try { + + publishEvents(device, deviceInfo); + DeviceManagementDAOFactory.beginTransaction(); DeviceInfo newDeviceInfo; DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), @@ -117,18 +126,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); DeviceManagementDAOFactory.commitTransaction(); - String reportingHost = System.getProperty(DeviceManagementConstants.Report - .REPORTING_EVENT_HOST); - if (reportingHost != null && !reportingHost.isEmpty()) { - DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); - deviceDetailsWrapper.setDevice(device); - deviceDetailsWrapper.setDeviceInfo(deviceInfo); - deviceDetailsWrapper.getJSONString(); - - HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), - reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); - } - //TODO :: This has to be fixed by adding the enrollment ID. if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; @@ -174,14 +171,54 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (DataPublisherConfigurationException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); - } catch (EventPublishingException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while sending events", e); - } finally { + }finally { DeviceManagementDAOFactory.closeConnection(); } } + private void publishEvents(Device device, DeviceInfo deviceInfo) { + String reportingHost = System.getProperty(DeviceManagementConstants.Report + .REPORTING_EVENT_HOST); + if (reportingHost != null && !reportingHost.isEmpty() && isPublishingEnabledForTenant()) { + try { + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setDevice(device); + deviceDetailsWrapper.setDeviceInfo(deviceInfo); + deviceDetailsWrapper.getJSONString(); + GroupManagementProviderService groupManagementService = DeviceManagementDataHolder + .getInstance().getGroupManagementProviderService(); + + List groups = groupManagementService.getGroups(device, false); + if (groups != null && groups.size() > 0) { + deviceDetailsWrapper.setGroups(groups); + } + + String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext() + .getUsername()); + if (rolesOfUser != null && rolesOfUser.length > 0) { + deviceDetailsWrapper.setRole(rolesOfUser); + } + + HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), + reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); + } catch (EventPublishingException e) { + log.error("Error occurred while sending events", e); + } catch (GroupManagementException e) { + log.error("Error occurred while getting group list", e); + } catch (UserStoreException e) { + log.error("Error occurred while getting role list", e); + } + } + } + + private boolean isPublishingEnabledForTenant() { + Object configuration = DeviceManagerUtil.getConfiguration(IS_EVENT_PUBLISHING_ENABED); + if (configuration != null) { + return Boolean.valueOf(configuration.toString()); + } + return false; + } + @Override public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { Device device = getDevice(deviceId); @@ -426,6 +463,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { return newDeviceInfo; } + /** * Generate and add a value depending on the device's OS version included in device info * @@ -460,5 +498,20 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } } + + private String[] getRolesOfUser(String userName) throws UserStoreException { + UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String[] roleList; + if (userRealm != null) { + userRealm.getUserStoreManager().getRoleNames(); + roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); + } else { + String msg = "User realm is not initiated. Logged in user: " + userName; + log.error(msg); + throw new UserStoreException(msg); + } + return roleList; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 4300ce232b..cca1768bd1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -243,6 +243,16 @@ public interface GroupManagementProviderService { */ List getGroups(DeviceIdentifier deviceIdentifier, boolean requireGroupProps) throws GroupManagementException; + /** + * Get groups which contains particular device. + * + * @param device interested devoce. + * @return groups contain the device. + * @throws GroupManagementException + */ + public List getGroups(Device device, boolean requireGroupProps) + throws GroupManagementException; + /** * Checks for the default group existence and create group based on device ownership. * @param groupName of the group diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index efe71421db..0fc3e978c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -938,6 +938,42 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } + @Override + public List getGroups(Device device, boolean requireGroupProps) + throws GroupManagementException { + if (device.getDeviceIdentifier() == null) { + String msg = "Received empty device identifier for getGroups"; + log.error(msg); + throw new GroupManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Get groups of device " + device.getDeviceIdentifier()); + } + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + GroupManagementDAOFactory.openConnection(); + List deviceGroups = groupDAO.getGroups(device.getId(), tenantId); + if (requireGroupProps) { + if (deviceGroups != null && !deviceGroups.isEmpty()) { + for (DeviceGroup group : deviceGroups) { + populateGroupProperties(group, tenantId); + } + } + } + return deviceGroups; + } catch (GroupManagementDAOException | SQLException e) { + String msg = "Error occurred while retrieving device groups."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getGroups"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + /** * {@inheritDoc} */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 0c0308d6b1..14d0aeba68 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -38,6 +38,10 @@ import org.wso2.carbon.device.mgt.common.ApplicationRegistration; import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; @@ -53,6 +57,8 @@ import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; +import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; @@ -97,9 +103,10 @@ import java.util.regex.Pattern; import java.util.stream.IntStream; -public final class DeviceManagerUtil { + public final class DeviceManagerUtil { private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); + public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; private static boolean isDeviceCacheInitialized = false; @@ -804,4 +811,28 @@ public final class DeviceManagerUtil { } return joiner.toString(); } + + public static Object getConfiguration(String key) { + + PlatformConfigurationManagementService configMgtService = + new PlatformConfigurationManagementServiceImpl(); + + try { + PlatformConfiguration tenantConfiguration = configMgtService.getConfiguration + (GENERAL_CONFIG_RESOURCE_PATH); + List configuration = tenantConfiguration.getConfiguration(); + + if (configuration != null && !configuration.isEmpty()) { + for (ConfigurationEntry cEntry : configuration) { + if (key.equalsIgnoreCase(cEntry.getName())) { + return cEntry.getValue(); + } + } + } + } catch (ConfigurationManagementException e) { + log.error("Error while getting the configurations from registry.", e); + return null; + } + return null; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs index 753a3c8851..d938d1050d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs @@ -95,6 +95,21 @@ class="form-control" placeholder="[ Required Field ]"> +
+ + +
diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js index 9e4176066b..a9bf2bb255 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js @@ -22,7 +22,8 @@ $(document).ready(function () { var configParams = { "NOTIFIER_TYPE": "notifierType", - "NOTIFIER_FREQUENCY": "notifierFrequency" + "NOTIFIER_FREQUENCY": "notifierFrequency", + "IS_EVENT_PUBLISHING_ENABLED": "isEventPublishingEnabled" }; var responseCodes = { @@ -51,6 +52,8 @@ $(document).ready(function () { var config = data.configuration[i]; if (config.name == configParams["NOTIFIER_FREQUENCY"]) { $("input#monitoring-config-frequency").val(config.value / 1000); + } else if (config.name == configParams["IS_EVENT_PUBLISHING_ENABLED"]) { + $("select#publish-for-analytics").val(config.value); } } } @@ -65,6 +68,7 @@ $(document).ready(function () { */ $("button#save-general-btn").click(function () { var notifierFrequency = $("input#monitoring-config-frequency").val(); + var publishEvents = $("select#publish-for-analytics").val(); var errorMsgWrapper = "#email-config-error-msg"; var errorMsg = "#email-config-error-msg span"; @@ -84,6 +88,13 @@ $(document).ready(function () { "contentType": "text" }; + var publishEventsDetails = { + "name": configParams["IS_EVENT_PUBLISHING_ENABLED"], + "value": publishEvents, + "contentType": "text" + }; + + configList.push(publishEventsDetails); configList.push(monitorFrequency); addConfigFormData.configuration = configList;