Merge pull request #422 from madawas/grouping-policy

Generalize Policy Implementation - Creating system groups based on ownership
merge-requests/7/head
Madawa Soysa 8 years ago committed by GitHub
commit 55bc5042bb

@ -48,6 +48,12 @@ public class DeviceGroup implements Serializable {
private Long dateOfLastUpdate;
private String owner;
public DeviceGroup() {}
public DeviceGroup(String name) {
this.name = name;
}
public int getGroupId() {
return id;
}

@ -19,12 +19,17 @@ package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
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.DeviceGroupConstants;
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.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
@ -32,6 +37,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations;
import org.wso2.carbon.device.mgt.core.dao.*;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
@ -130,7 +136,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return false;
}
deviceManager.enrollDevice(device);
if (deviceManager.isClaimable(deviceIdentifier)) {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE);
} else {
@ -221,6 +226,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
status = true;
}
if (status) {
addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership());
}
return status;
}
@ -1834,4 +1842,58 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return deviceManagementService.getDeviceManager();
}
/**
* Adds the enrolled devices to the default groups based on ownership
*
* @param deviceIdentifier of the device.
* @param ownership of the device.
* @throws DeviceManagementException If error occurred in adding the device to the group.
*/
private void addDeviceToGroups(DeviceIdentifier deviceIdentifier, EnrolmentInfo.OwnerShip ownership)
throws DeviceManagementException {
GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl();
try {
DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString());
if (defaultGroup != null) {
groupManagementProviderService.addDevice(defaultGroup.getGroupId(), deviceIdentifier);
}
} catch (DeviceNotFoundException e) {
throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(),
e);
} catch (GroupManagementException e) {
throw new DeviceManagementException("An error occurred when adding the device to the group.", e);
}
}
/**
* Checks for the default group existence and create group based on device ownership
*
* @param service {@link GroupManagementProviderService} instance.
* @param groupName of the group to create.
* @return Group with details.
* @throws GroupManagementException
*/
private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName)
throws GroupManagementException {
DeviceGroup defaultGroup = service.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.setDateOfCreation(new Date().getTime());
defaultGroup.setDateOfLastUpdate(new Date().getTime());
try {
service.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 service.getGroup(groupName);
} else {
return defaultGroup;
}
}
}

@ -29,6 +29,7 @@ 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.GroupUser;
import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException;
import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException;
import java.util.List;
@ -76,6 +77,15 @@ public interface GroupManagementProviderService {
*/
DeviceGroup getGroup(int groupId) throws GroupManagementException;
/**
* Get the device group provided the device group name.
*
* @param groupName of the group.
* @return group with details.
* @throws GroupManagementException
*/
DeviceGroup getGroup(String groupName) throws GroupManagementException;
/**
* Get all device groups in tenant.
*
@ -211,15 +221,24 @@ public interface GroupManagementProviderService {
*/
int getDeviceCount(int groupId) throws GroupManagementException;
/**
* @param groupId of the group.
* @param deviceIdentifier of the device to add.
* @throws DeviceNotFoundException If device does not exist.
* @throws GroupManagementException If unable to add device to the group.
*/
void addDevice(int groupId, DeviceIdentifier deviceIdentifier)
throws DeviceNotFoundException, GroupManagementException;
/**
* Add device to device group.
*
* @param groupId of the group.
* @param groupId of the group.
* @param deviceIdentifiers of devices.
* @throws GroupManagementException
*/
void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers) throws GroupManagementException,
DeviceNotFoundException;
void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers)
throws GroupManagementException, DeviceNotFoundException;
/**
* Remove device from device group.

@ -188,6 +188,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return deviceGroup;
}
/**
* {@inheritDoc}
*/
@Override
public DeviceGroup getGroup(String groupName) throws GroupManagementException {
DeviceGroup deviceGroup;
try {
GroupManagementDAOFactory.openConnection();
deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId());
} catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while obtaining group with name: '" + groupName + "'", e);
} catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
return deviceGroup;
}
@Override
public List<DeviceGroup> getGroups() throws GroupManagementException {
List<DeviceGroup> deviceGroups = new ArrayList<>();
@ -587,6 +606,36 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
}
/**
* {@inheritDoc}
*/
@Override
public void addDevice(int groupId, DeviceIdentifier deviceIdentifier)
throws DeviceNotFoundException, GroupManagementException {
Device device;
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.beginTransaction();
device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier);
if (device == null) {
throw new DeviceNotFoundException("Device not found for id '" + deviceIdentifier.getId() + "'");
}
if (!this.groupDAO.isDeviceMappedToGroup(groupId, device.getId(), tenantId)) {
this.groupDAO.addDevice(groupId, device.getId(), tenantId);
}
GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", e);
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while adding device to group.", e);
} catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
/**
* {@inheritDoc}
*/

Loading…
Cancel
Save