From afe312152f530ae7e95ee5486ab1ebff57364f19 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 17 Jul 2017 14:43:06 +0530 Subject: [PATCH] Fixing https://github.com/wso2/product-iots/issues/1007 --- .../core/config/DeviceManagementConfig.java | 10 ++++++ .../DeviceManagementServiceComponent.java | 31 +++++++++++++++++- .../GroupManagementProviderService.java | 8 +++++ .../GroupManagementProviderServiceImpl.java | 32 ++++++++++++++++--- .../src/main/resources/conf/cdm-config.xml | 1 + 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index e95fa19e51c..5d757f4f0bf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -47,6 +47,7 @@ public final class DeviceManagementConfig { private DeviceStatusTaskConfig deviceStatusTaskConfig; private DeviceCacheConfiguration deviceCacheConfiguration; private GeoLocationConfiguration geoLocationConfiguration; + private String defaultGroupsConfiguration; @XmlElement(name = "ManagementRepository", required = true) public DeviceManagementConfigRepository getDeviceManagementConfigRepository() { @@ -138,5 +139,14 @@ public final class DeviceManagementConfig { public void setGeoLocationConfiguration(GeoLocationConfiguration geoLocationConfiguration) { this.geoLocationConfiguration = geoLocationConfiguration; } + + @XmlElement(name = "DefaultGroupsConfiguration", required = true) + public String getDefaultGroupsConfiguration() { + return defaultGroupsConfiguration; + } + + public void setDefaultGroupsConfiguration(String defaultGroupsConfiguration) { + this.defaultGroupsConfiguration = defaultGroupsConfiguration; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 3e087594054..32e9e978e10 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.geo.service.GeoService; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; @@ -253,6 +254,21 @@ public class DeviceManagementServiceComponent { /* Registering Group Management Service */ GroupManagementProviderService groupManagementProvider = new GroupManagementProviderServiceImpl(); + String defaultGroups = + DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getDefaultGroupsConfiguration(); + List groups = this.parseDefaultGroups(defaultGroups); + for(String group : groups){ + try { + groupManagementProvider.createDefaultGroup(group); + } catch (GroupManagementException e) { + // Error is ignored, because error could be group already exist exception. Therefore it does not require + // to print the error. + if(log.isDebugEnabled()){ + log.error("Error occurred while adding the group"); + } + } + } + DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(groupManagementProvider); bundleContext.registerService(GroupManagementProviderService.class.getName(), groupManagementProvider, null); @@ -311,6 +327,19 @@ public class DeviceManagementServiceComponent { } } + private List parseDefaultGroups(String defaultGroups) { + List defaultGroupsList = new ArrayList<>(); + if (defaultGroups != null && !defaultGroups.isEmpty()) { + String gps[] = defaultGroups.split(","); + if (gps.length != 0) { + for(String group : gps){ + defaultGroupsList.add(group.trim()); + } + } + } + return defaultGroupsList; + } + /** * Sets Device Manager service. * @@ -320,7 +349,7 @@ public class DeviceManagementServiceComponent { try { if (log.isDebugEnabled()) { log.debug("Setting Device Management Service Provider: '" + - deviceManagementService.getType() + "'"); + deviceManagementService.getType() + "'"); } synchronized (LOCK) { deviceManagers.add(deviceManagementService); 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 875cc859e56..054a923c437 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 @@ -212,4 +212,12 @@ public interface GroupManagementProviderService { */ List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException; + /** + * Checks for the default group existence and create group based on device ownership. + * @param groupName + * @return + * @throws GroupManagementException + */ + DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException; + } 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 626b086528d..ad8ecf77d1e 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 @@ -30,10 +30,7 @@ import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.TransactionManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.*; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -572,4 +569,31 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.closeConnection(); } } + + /** + * {@inheritDoc} + */ + @Override + public DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException { + + DeviceGroup defaultGroup = this.getGroup(groupName); + if (defaultGroup == null) { + defaultGroup = new DeviceGroup(groupName); + // Setting system level user (wso2.system.user) as the owner + defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME); + defaultGroup.setDescription("Default system group for devices with " + groupName + " ownership."); + try { + this.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, + DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); + } catch (GroupAlreadyExistException e) { + if (log.isDebugEnabled()) { + log.debug("Default group: " + defaultGroup.getName() + " already exists. Skipping group creation.", + e); + } + } + return this.getGroup(groupName); + } else { + return defaultGroup; + } + } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 0f0312f977d..fbf6103cfff 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -82,5 +82,6 @@ false false + BYOD,COPE