Refactor policy mgt components and fixed add operations for invalid devices from task service

revert-70aa11f8
harshanl 8 years ago
parent c967fc34d0
commit 108ea98677

@ -18,12 +18,8 @@
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
@ -79,8 +75,8 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO {
try {
conn = NotificationManagementDAOFactory.getConnection();
String sql =
"SELECT NOTIFICATION_ID, OPERATION_ID, DESCRIPTION, STATUS FROM DM_NOTIFICATION WHERE " +
"TENANT_ID = ? AND NOTIFICATION_ID = ?";
"SELECT NOTIFICATION_ID, OPERATION_ID, DESCRIPTION, STATUS, DEVICE_IDENTIFICATION, DEVICE_NAME, " +
"DEVICE TYPE FROM DM_NOTIFICATION WHERE TENANT_ID = ? AND NOTIFICATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, notificationId);

@ -33,8 +33,23 @@ import java.util.List;
*/
public interface DeviceManagementProviderService {
/**
* Method to retrieve all the devices of a given device type.
*
* @param deviceType Device-type of the required devices
* @return List of devices of given device-type.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices.
*/
List<Device> getAllDevices(String deviceType) throws DeviceManagementException;
/**
* Method to retrieve all the devices registered in the system.
*
* @return List of registered devices.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices.
*/
List<Device> getAllDevices() throws DeviceManagementException;
/**

@ -84,29 +84,28 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
@Override
public void addOperations() throws DeviceMgtTaskException {
DeviceManagementProviderService deviceManagementProviderService =
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider();
try {
List<Device> devices = deviceManagementProviderService.getAllDevices();
List<String> deviceTypes = deviceManagementProviderService.getAvailableDeviceTypes();
List<Device> devices;
List<String> operations = this.getValidOperationNames();
if (!devices.isEmpty()) {
for (String str : operations) {
CommandOperation operation = new CommandOperation();
operation.setEnabled(true);
operation.setType(Operation.Type.COMMAND);
operation.setCode(str);
//TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()"
String type = null;
if (devices.size() > 0) {
type = devices.get(0).getType();
for (String deviceType : deviceTypes) {
devices = deviceManagementProviderService.getAllDevices(deviceType);
if (!devices.isEmpty()) {
for (String str : operations) {
CommandOperation operation = new CommandOperation();
operation.setEnabled(true);
operation.setType(Operation.Type.COMMAND);
operation.setCode(str);
deviceManagementProviderService.addOperation(deviceType, operation,
DeviceManagerUtil.getValidDeviceIdentifiers(devices));
}
} else {
if (log.isDebugEnabled()) {
log.debug("No devices are available to perform the operations.");
}
deviceManagementProviderService.addOperation(type, operation,
DeviceManagerUtil.convertDevices(devices));
}
} else {
if (log.isDebugEnabled()) {
log.debug("No devices are available to perform the operations.");
}
}
} catch (InvalidDeviceException e) {

@ -184,6 +184,27 @@ public final class DeviceManagerUtil {
return deviceIdentifiers;
}
public static List<DeviceIdentifier> getValidDeviceIdentifiers(List<Device> devices) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (Device device : devices) {
if (device.getEnrolmentInfo() != null) {
switch (device.getEnrolmentInfo().getStatus()) {
case BLOCKED:
case REMOVED:
case SUSPENDED:
break;
default:
DeviceIdentifier identifier = new DeviceIdentifier();
identifier.setId(device.getDeviceIdentifier());
identifier.setType(device.getType());
deviceIdentifiers.add(identifier);
}
}
}
return deviceIdentifiers;
}
public static String getServerBaseHttpsUrl() {
String hostName = "localhost";
try {

@ -54,6 +54,8 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
policyAdministratorPoint = new PolicyAdministratorPointImpl();
monitoringManager = new MonitoringManagerImpl();
policyManager = new PolicyManagerImpl();
PolicyManagementDataHolder.getInstance().setMonitoringManager(monitoringManager);
PolicyManagementDataHolder.getInstance().setPolicyManager(policyManager);
}
@Override

@ -23,6 +23,8 @@ import org.wso2.carbon.ntask.core.service.TaskService;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager;
@ -36,6 +38,8 @@ public class PolicyManagementDataHolder {
private PolicyEvaluationPoint policyEvaluationPoint;
private PolicyInformationPoint policyInformationPoint;
private DeviceManagementProviderService deviceManagementService;
private MonitoringManager monitoringManager;
private PolicyManager policyManager;
private Map<String, PolicyMonitoringService> policyMonitoringServiceMap = new HashMap<>();
private TaskService taskService;
@ -47,6 +51,22 @@ public class PolicyManagementDataHolder {
return thisInstance;
}
public PolicyManager getPolicyManager() {
return policyManager;
}
public void setPolicyManager(PolicyManager policyManager) {
this.policyManager = policyManager;
}
public MonitoringManager getMonitoringManager() {
return monitoringManager;
}
public void setMonitoringManager(MonitoringManager monitoringManager) {
this.monitoringManager = monitoringManager;
}
public RealmService getRealmService() {
return realmService;
}

@ -21,7 +21,6 @@ package org.wso2.carbon.policy.mgt.core.mgt;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
@ -40,6 +39,6 @@ public interface MonitoringManager {
void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException;
List<DeviceType> getDeviceTypes() throws PolicyComplianceException;
List<String> getDeviceTypes() throws PolicyComplianceException;
}

@ -21,7 +21,6 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -30,12 +29,8 @@ 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.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
@ -56,8 +51,6 @@ import java.util.Map;
public class MonitoringManagerImpl implements MonitoringManager {
private PolicyDAO policyDAO;
// private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
private MonitoringDAO monitoringDAO;
private ComplianceDecisionPoint complianceDecisionPoint;
private PolicyConfiguration policyConfiguration;
@ -69,8 +62,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
public MonitoringManagerImpl() {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
// this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO();
this.complianceDecisionPoint = new ComplianceDecisionPointImpl();
this.policyConfiguration =
@ -84,7 +75,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<ComplianceFeature> complianceFeatures = new ArrayList<>();
try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();
PolicyManager manager = new PolicyManagerImpl();
Device device = service.getDevice(deviceIdentifier);
Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier);
@ -179,7 +170,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
@Override
public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();
Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection();
ComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo()
@ -209,7 +200,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
ComplianceData complianceData;
try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();
Device device = service.getDevice(deviceIdentifier);
complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId());
List<ComplianceFeature> complianceFeatures =
@ -374,17 +365,13 @@ public class MonitoringManagerImpl implements MonitoringManager {
}
@Override
public List<DeviceType> getDeviceTypes() throws PolicyComplianceException {
public List<String> getDeviceTypes() throws PolicyComplianceException {
List<DeviceType> deviceTypes = new ArrayList<>();
List<String> deviceTypes = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceTypes = deviceTypeDAO.getDeviceTypes(tenantId);
} catch (Exception e) {
log.error("Error occurred while getting the device types.", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
deviceTypes = PolicyManagementDataHolder.getInstance().getDeviceManagementService().getAvailableDeviceTypes();
} catch (DeviceManagementException e) {
throw new PolicyComplianceException("Error occurred while getting the device types.", e);
}
return deviceTypes;
}

@ -26,9 +26,6 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
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.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
@ -50,14 +47,12 @@ public class PolicyManagerImpl implements PolicyManager {
private ProfileDAO profileDAO;
private FeatureDAO featureDAO;
private ProfileManager profileManager;
private DeviceDAO deviceDAO;
private static Log log = LogFactory.getLog(PolicyManagerImpl.class);
public PolicyManagerImpl() {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
this.profileDAO = PolicyManagementDAOFactory.getProfileDAO();
this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.profileManager = new ProfileManagerImpl();
}

@ -40,7 +40,6 @@ import java.util.Map;
public class MonitoringTask implements Task {
private DeviceTypeDAO deviceTypeDAO;
private static Log log = LogFactory.getLog(MonitoringTask.class);
Map<String, String> properties;
@ -53,7 +52,6 @@ public class MonitoringTask implements Task {
@Override
public void init() {
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
}
@Override
@ -63,9 +61,9 @@ public class MonitoringTask implements Task {
log.debug("Monitoring task started to run.");
}
MonitoringManager monitoringManager = new MonitoringManagerImpl();
MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager();
List<DeviceType> deviceTypes = new ArrayList<>();
List<String> deviceTypes = new ArrayList<>();
try {
deviceTypes = monitoringManager.getDeviceTypes();
} catch (PolicyComplianceException e) {
@ -79,15 +77,15 @@ public class MonitoringTask implements Task {
DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
for (DeviceType deviceType : deviceTypes) {
for (String deviceType : deviceTypes) {
if (log.isDebugEnabled()) {
log.debug("Running task for device type : " + deviceType.getName());
log.debug("Running task for device type : " + deviceType);
}
PolicyMonitoringService monitoringService =
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName());
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName());
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType);
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType);
if (monitoringService != null && !devices.isEmpty()) {
@ -95,7 +93,7 @@ public class MonitoringTask implements Task {
if (log.isDebugEnabled()) {
log.debug("Removing inactive and blocked devices from the list for the device type : " +
deviceType.getName());
deviceType);
}
for (Device device : devices) {
EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus();
@ -111,8 +109,7 @@ public class MonitoringTask implements Task {
}
}
if (log.isDebugEnabled()) {
log.debug("Following devices selected to send the notification for " +
deviceType.getName());
log.debug("Following devices selected to send the notification for " + deviceType);
for (Device device : notifiableDevices) {
log.debug(device.getDeviceIdentifier());
}

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
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;

Loading…
Cancel
Save