Merge branch 'master' into 'master'

Performance Improvements

See merge request entgra/carbon-device-mgt!541
reporting
Dharmakeerthi Lasantha 5 years ago
commit 62ef8d6bb5

@ -35,7 +35,6 @@ public class NonComplianceData {
private Timestamp lastRequestedTime;
private Timestamp lastSucceededTime;
private Timestamp lastFailedTime;
private int attempts;
private String message;
/**
@ -85,14 +84,6 @@ public class NonComplianceData {
this.lastFailedTime = lastFailedTime;
}
public int getAttempts() {
return attempts;
}
public void setAttempts(int attempts) {
this.attempts = attempts;
}
public int getDeviceId() {
return deviceId;
}

@ -2650,7 +2650,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
int deviceCount = 0;
try {
conn = this.getConnection();
String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS != 'REMOVED'";
String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS = 'ACTIVE' " +
"OR STATUS = 'UNREACHABLE'";
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();

@ -88,7 +88,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
conn = this.getConnection();
stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " +
"VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ? " +
"WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?");
"WHERE ID = ?");
for (Application application : applications) {
stmt.setString(1, application.getName());
@ -100,10 +100,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(7, application.getImageUrl());
stmt.setInt(8, application.getMemoryUsage());
stmt.setBoolean(9, application.isActive());
stmt.setString(10, application.getApplicationIdentifier());
stmt.setInt(11, deviceId);
stmt.setInt(12, enrolmentId);
stmt.setInt(13, tenantId);
stmt.setInt(10, application.getId());
stmt.addBatch();
}
stmt.executeBatch();
@ -123,14 +120,10 @@ public class ApplicationDAOImpl implements ApplicationDAO {
try {
conn = this.getConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? " +
"AND ENROLMENT_ID = ? AND TENANT_ID = ?");
stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE ID = ?");
for (Application app : apps) {
stmt.setString(1, app.getApplicationIdentifier());
stmt.setInt(2, deviceId);
stmt.setInt(3, enrolmentId);
stmt.setInt(4, tenantId);
stmt.setInt(1, app.getId());
stmt.addBatch();
}
stmt.executeBatch();

@ -834,13 +834,14 @@ public class OperationManagerImpl implements OperationManager {
public void updateOperation(int enrolmentId, Operation operation, DeviceIdentifier deviceId)
throws OperationManagementException {
int operationId = operation.getId();
boolean isOperationUpdated = false;
try {
OperationManagementDAOFactory.beginTransaction();
if (operation.getStatus() != null) {
int failAttempts = 0;
while (true) {
try {
operationDAO.updateOperationStatus(enrolmentId, operationId,
isOperationUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.valueOf(operation.getStatus().
toString()));
@ -865,7 +866,10 @@ public class OperationManagerImpl implements OperationManager {
}
}
}
if (operation.getOperationResponse() != null) {
if (!isOperationUpdated) {
log.warn("Operation " + operationId + "'s status is not updated");
}
if (isOperationUpdated && operation.getOperationResponse() != null) {
OperationMonitoringTaskConfig operationMonitoringTaskConfig = DeviceManagementDataHolder
.getInstance().getDeviceManagementProvider().getDeviceMonitoringConfig(deviceId.getType());
List<MonitoringOperation> monitoringOperations = operationMonitoringTaskConfig.getMonitoringOperation();
@ -901,7 +905,7 @@ public class OperationManagerImpl implements OperationManager {
}
}
OperationResponseMeta responseMeta = null;
if (operation.getOperationResponse() != null) {
if (isOperationUpdated && operation.getOperationResponse() != null) {
int failAttempts = 0;
while (true) {
try {

@ -18,7 +18,8 @@
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>policy-mgt</artifactId>
@ -27,7 +28,6 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.core</artifactId>
<version>4.1.7-SNAPSHOT</version>
<packaging>bundle</packaging>
@ -188,6 +188,7 @@
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.wso2</groupId>

@ -55,7 +55,7 @@ public interface MonitoringDAO {
List<NonComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException;
List<NonComplianceData> getCompliance() throws MonitoringDAOException;
Map<Integer, NonComplianceData> getCompliance() throws MonitoringDAOException;
List<ComplianceData> getAllComplianceDevices(
PaginationRequest paginationRequest, String policyId, boolean complianceStatus, boolean isPending, String fromDate, String toDate)
@ -65,8 +65,4 @@ public interface MonitoringDAO {
void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException;
boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException;
void updateAttempts(List<Integer> deviceId, boolean reset) throws MonitoringDAOException;
}

@ -49,15 +49,14 @@ public class MonitoringDAOImpl implements MonitoringDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " +
"LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) ";
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " +
"LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?) ";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setInt(1, deviceId);
stmt.setInt(2, policyId);
stmt.setInt(3, 1);
stmt.setInt(4, 1);
stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId);
stmt.setTimestamp(4, currentTimestamp);
stmt.setInt(5, tenantId);
stmt.executeUpdate();
generatedKeys = stmt.getGeneratedKeys();
@ -90,16 +89,15 @@ public class MonitoringDAOImpl implements MonitoringDAO {
}
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " +
"LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) ";
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " +
"LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?) ";
stmt = conn.prepareStatement(query);
for (Map.Entry<Integer, Integer> map : devicePolicyMap.entrySet()) {
stmt.setInt(1, map.getKey());
stmt.setInt(2, map.getValue());
stmt.setInt(3, 1);
stmt.setInt(4, 1);
stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId);
stmt.setTimestamp(4, currentTimestamp);
stmt.setInt(5, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
@ -119,23 +117,22 @@ public class MonitoringDAOImpl implements MonitoringDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (log.isDebugEnabled()) {
for (PolicyDeviceWrapper wrapper : policyDeviceWrapper){
for (PolicyDeviceWrapper wrapper : policyDeviceWrapper) {
log.debug("Policy Id : " + wrapper.getPolicyId() + " - " + " Device Id : " + wrapper.getDeviceId());
}
}
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " +
"LAST_REQUESTED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?, ?) ";
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " +
"LAST_REQUESTED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?) ";
stmt = conn.prepareStatement(query);
for (PolicyDeviceWrapper wrapper : policyDeviceWrapper) {
stmt.setInt(1, wrapper.getDeviceId());
stmt.setInt(2, wrapper.getPolicyId());
stmt.setInt(3, 1);
stmt.setInt(4, 1);
stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId);
stmt.setInt(7, wrapper.getEnrolmentId());
stmt.setTimestamp(4, currentTimestamp);
stmt.setInt(5, tenantId);
stmt.setInt(6, wrapper.getEnrolmentId());
stmt.addBatch();
}
stmt.executeBatch();
@ -155,8 +152,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," +
" ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ? " +
"WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setTimestamp(1, currentTimestamp);
stmt.setInt(2, policyId);
@ -182,7 +179,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" +
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, LAST_SUCCESS_TIME = ?" +
" WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, 1);
@ -260,9 +257,9 @@ public class MonitoringDAOImpl implements MonitoringDAO {
while (resultSet.next()) {
complianceData.setId(resultSet.getInt("ID"));
complianceData.setDeviceId(resultSet.getInt("DEVICE_ID"));
complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID"));
complianceData.setPolicyId(resultSet.getInt("POLICY_ID"));
complianceData.setStatus(resultSet.getBoolean("STATUS"));
complianceData.setAttempts(resultSet.getInt("ATTEMPTS"));
complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME"));
complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME"));
complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME"));
@ -298,7 +295,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID"));
complianceData.setPolicyId(resultSet.getInt("POLICY_ID"));
complianceData.setStatus(resultSet.getBoolean("STATUS"));
complianceData.setAttempts(resultSet.getInt("ATTEMPTS"));
complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME"));
complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME"));
complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME"));
@ -314,11 +310,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
}
@Override
public List<NonComplianceData> getCompliance() throws MonitoringDAOException {
public Map<Integer, NonComplianceData> getCompliance() throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<NonComplianceData> complianceDataList = new ArrayList<>();
Map<Integer, NonComplianceData> complianceDataList = new HashMap<>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
@ -327,19 +323,19 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery();
NonComplianceData complianceData;
while (resultSet.next()) {
NonComplianceData complianceData = new NonComplianceData();
complianceData = new NonComplianceData();
complianceData.setId(resultSet.getInt("ID"));
complianceData.setDeviceId(resultSet.getInt("DEVICE_ID"));
complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID"));
complianceData.setPolicyId(resultSet.getInt("POLICY_ID"));
complianceData.setStatus(resultSet.getBoolean("STATUS"));
complianceData.setAttempts(resultSet.getInt("ATTEMPTS"));
complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME"));
complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME"));
complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME"));
complianceDataList.add(complianceData);
complianceDataList.put(complianceData.getEnrolmentId(), complianceData);
}
return complianceDataList;
} catch (SQLException e) {
@ -492,68 +488,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
}
@Override
public boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException {
boolean status = false;
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "";
if (reset) {
query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " +
"WHERE DEVICE_ID = ? AND TENANT_ID = ?";
} else {
query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " +
"WHERE DEVICE_ID = ? AND TENANT_ID = ?";
}
stmt = conn.prepareStatement(query);
stmt.setTimestamp(1, currentTimestamp);
stmt.setInt(2, deviceId);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
status = true;
} catch (SQLException e) {
throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@Override
public void updateAttempts(List<Integer> deviceIds, boolean reset) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "";
if (reset) {
query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " +
"WHERE DEVICE_ID = ? AND TENANT_ID = ?";
} else {
query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " +
"WHERE DEVICE_ID = ? AND TENANT_ID = ?";
}
stmt = conn.prepareStatement(query);
for (int deviceId : deviceIds) {
stmt.setTimestamp(1, currentTimestamp);
stmt.setInt(2, deviceId);
stmt.setInt(3, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
} catch (SQLException e) {
throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
private Connection getConnection() throws MonitoringDAOException {
return PolicyManagementDAOFactory.getConnection();
}

@ -1778,13 +1778,13 @@ public class PolicyDAOImpl implements PolicyDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?";
String query = "SELECT POLICY_ID, ENROLMENT_ID FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
devicePolicyIds.put(resultSet.getInt("DEVICE_ID"), resultSet.getInt("POLICY_ID"));
devicePolicyIds.put(resultSet.getInt("ENROLMENT_ID"), resultSet.getInt("POLICY_ID"));
}
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e);
@ -1805,7 +1805,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?";
String query = "SELECT POLICY_ID, DEVICE_ID FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery();

@ -44,7 +44,7 @@ public interface MonitoringManager {
NonComplianceData getDevicePolicyCompliance(Device device) throws PolicyComplianceException;
void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException;
void addMonitoringOperation(String deviceType, List<Device> devices) throws PolicyComplianceException;
List<String> getDeviceTypes() throws PolicyComplianceException;

@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceData;
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException;
@ -58,9 +59,9 @@ import java.util.Map;
public class MonitoringManagerImpl implements MonitoringManager {
private PolicyDAO policyDAO;
private MonitoringDAO monitoringDAO;
private ComplianceDecisionPoint complianceDecisionPoint;
private final PolicyDAO policyDAO;
private final MonitoringDAO monitoringDAO;
private final ComplianceDecisionPoint complianceDecisionPoint;
private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class);
private static final String OPERATION_MONITOR = "MONITOR";
@ -113,6 +114,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
complianceData.setPolicy(policy);
complianceFeatures = complianceData.getComplianceFeatures();
complianceData.setDeviceId(device.getId());
complianceData.setEnrolmentId(cmd.getEnrolmentId());
complianceData.setPolicyId(policy.getId());
}
@ -121,7 +123,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
} catch (MonitoringDAOException e) {
throw new PolicyComplianceException(
"Unable to add the none compliance features to database for device " +
device.getDeviceIdentifier() + " - " + device.getType(), e);
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
@ -145,7 +147,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException(
"Unable to add the none compliance features to database for device " +
device.getDeviceIdentifier() + " - " + device.getType(), e);
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
@ -253,106 +255,45 @@ public class MonitoringManagerImpl implements MonitoringManager {
}
@Override
public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException {
public void addMonitoringOperation(String deviceType, List<Device> devices) throws PolicyComplianceException {
//int tenantId = PolicyManagerUtil.getTenantId();
Map<Integer, Device> deviceIds = new HashMap<>();
List<NonComplianceData> complianceDatas = new ArrayList<>();
HashMap<Integer, Integer> devicePolicyIdMap = new HashMap<>();
Map<Integer, Device> notifiableDeviceEnrollments = new HashMap<>();
List<PolicyDeviceWrapper> firstTimeComplianceData = new ArrayList<>();
try {
PolicyManagementDAOFactory.openConnection();
//TODO: Return a map from getCompliance to reduce O(n^2) -> O(n)
List<NonComplianceData> cd = monitoringDAO.getCompliance();
Map<Integer, NonComplianceData> persistedComplianceData = monitoringDAO.getCompliance();
HashMap<Integer, Integer> appliedPolicyIds = policyDAO.getAppliedPolicyIds();
int enrollmentId;
for (Device device : devices) {
deviceIds.put(device.getId(), device);
for (NonComplianceData data : cd) {
if (device.getId() == data.getDeviceId() && device.getEnrolmentInfo().getId() == data
.getEnrolmentId()) {
complianceDatas.add(data);
}
}
}
List<Integer> deviceIDs = new ArrayList<>(deviceIds.keySet());
HashMap<Integer, Integer> temp = policyDAO.getAppliedPolicyIds();
for (Integer id : deviceIDs) {
if (temp != null && !temp.isEmpty() && temp.containsKey(id)) {
devicePolicyIdMap.put(id, temp.get(id));
enrollmentId = device.getEnrolmentInfo().getId();
if (persistedComplianceData.containsKey(enrollmentId)) {
notifiableDeviceEnrollments.put(enrollmentId, device);
} else if (appliedPolicyIds.containsKey(enrollmentId)){
PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper();
policyDeviceWrapper.setDeviceId(device.getId());
policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId());
policyDeviceWrapper.setPolicyId(appliedPolicyIds.get(enrollmentId));
firstTimeComplianceData.add(policyDeviceWrapper);
notifiableDeviceEnrollments.put(enrollmentId, device);
}
}
} catch (SQLException e) {
throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e);
} catch (MonitoringDAOException e) {
throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e);
throw new PolicyComplianceException("Error occurred while getting monitoring details.", e);
} catch (PolicyManagerDAOException e) {
throw new PolicyComplianceException("SQL error occurred while getting policy details.", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>();
Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
List<PolicyDeviceWrapper> firstTimeDevices = new ArrayList<>();
Map<Integer, NonComplianceData> tempMap = new HashMap<>();
if (!firstTimeComplianceData.isEmpty()) {
try {
if (complianceDatas != null || !complianceDatas.isEmpty()) {
for (NonComplianceData complianceData : complianceDatas) {
tempMap.put(complianceData.getDeviceId(), complianceData);
if (complianceData.getAttempts() == 0) {
deviceIdsToAddOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
} else {
deviceIdsWithExistingOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
}
}
}
for (Device device : devices) {
if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) {
deviceIdsToAddOperation.put(device.getId(), device);
PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper();
policyDeviceWrapper.setDeviceId(device.getId());
policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId());
policyDeviceWrapper.setPolicyId(devicePolicyIdMap.get(device.getId()));
firstTimeDevices.add(policyDeviceWrapper);
// firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId()));
}
}
if (log.isDebugEnabled()) {
log.debug("These devices are in the system for the first time");
for (PolicyDeviceWrapper wrapper : firstTimeDevices) {
log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " +
wrapper.getPolicyId());
}
}
PolicyManagementDAOFactory.beginTransaction();
if (!deviceIdsToAddOperation.isEmpty()) {
// monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds);
monitoringDAO.addComplianceDetails(firstTimeDevices);
monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false);
}
if (!deviceIdsWithExistingOperation.isEmpty()) {
monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false);
}
monitoringDAO.addComplianceDetails(firstTimeComplianceData);
PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Error occurred from monitoring dao.", e);
@ -362,10 +303,11 @@ public class MonitoringManagerImpl implements MonitoringManager {
} finally {
PolicyManagementDAOFactory.closeConnection();
}
}
if (!deviceIdsToAddOperation.isEmpty()) {
if (!notifiableDeviceEnrollments.isEmpty()) {
try {
this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values()));
this.addMonitoringOperationsToDatabase(deviceType, new ArrayList<>(notifiableDeviceEnrollments.values()));
} catch (InvalidDeviceException e) {
throw new PolicyComplianceException("Invalid Device Identifiers found.", e);
} catch (OperationManagementException e) {
@ -381,8 +323,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
try {
//when shutdown, it sets DeviceManagementService to null, therefore need to have a null check
if (PolicyManagementDataHolder.getInstance().getDeviceManagementService() != null) {
deviceTypes =
PolicyManagementDataHolder.getInstance().getDeviceManagementService().getPolicyMonitoringEnableDeviceTypes();
deviceTypes = PolicyManagementDataHolder.getInstance().getDeviceManagementService()
.getPolicyMonitoringEnableDeviceTypes();
}
} catch (DeviceManagementException e) {
throw new PolicyComplianceException("Error occurred while getting the device types.", e);
@ -436,32 +378,17 @@ public class MonitoringManagerImpl implements MonitoringManager {
return complianceFeatureList;
}
private void addMonitoringOperationsToDatabase(List<Device> devices)
throws PolicyComplianceException, OperationManagementException, InvalidDeviceException {
private void addMonitoringOperationsToDatabase(String deviceType, List<Device> devices)
throws OperationManagementException, InvalidDeviceException {
List<DeviceIdentifier> deviceIdentifiers = this.getDeviceIdentifiersFromDevices(devices);
CommandOperation monitoringOperation = new CommandOperation();
monitoringOperation.setEnabled(true);
monitoringOperation.setType(Operation.Type.COMMAND);
monitoringOperation.setCode(OPERATION_MONITOR);
// CommandOperation infoOperation = new CommandOperation();
// infoOperation.setEnabled(true);
// infoOperation.setType(Operation.Type.COMMAND);\\
// infoOperation.setCode(OPERATION_INFO);
// CommandOperation appListOperation = new CommandOperation();
// appListOperation.setEnabled(true);
// appListOperation.setType(Operation.Type.COMMAND);
// appListOperation.setCode(OPERATION_APP_LIST);
//TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()"
String type = null;
if (deviceIdentifiers.size() > 0) {
type = deviceIdentifiers.get(0).getType();
}
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();
service.addOperation(type, monitoringOperation, deviceIdentifiers);
// service.addOperation(infoOperation, deviceIdentifiers);
// service.addOperation(appListOperation, deviceIdentifiers);
monitoringOperation.setControl(Operation.Control.NO_REPEAT);
PolicyManagementDataHolder.getInstance().getDeviceManagementService()
.addOperation(deviceType, monitoringOperation, deviceIdentifiers);
}
private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) {

@ -23,8 +23,8 @@ 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.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -40,7 +40,7 @@ import java.util.Map;
public class MonitoringTask implements Task {
private static Log log = LogFactory.getLog(MonitoringTask.class);
private static final Log log = LogFactory.getLog(MonitoringTask.class);
Map<String, String> properties;
private boolean executeForTenants = false;
@ -62,10 +62,10 @@ public class MonitoringTask implements Task {
if (log.isDebugEnabled()) {
log.debug("Monitoring task started to run.");
}
if(System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))){
if (System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))) {
executeForTenants = true;
}
if(executeForTenants) {
if (executeForTenants) {
this.executeforAllTenants();
} else {
this.executeTask();
@ -83,10 +83,7 @@ public class MonitoringTask implements Task {
private boolean isPlatformExist(String deviceType) {
PolicyMonitoringManager policyMonitoringManager = PolicyManagementDataHolder.getInstance()
.getDeviceManagementService().getPolicyMonitoringManager(deviceType);
if (policyMonitoringManager != null) {
return true;
}
return false;
return policyMonitoringManager != null;
}
private void executeforAllTenants() {
@ -116,7 +113,7 @@ public class MonitoringTask implements Task {
}
}
private void executeTask(){
private void executeTask() {
MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager();
List<String> deviceTypes = new ArrayList<>();
@ -158,13 +155,14 @@ public class MonitoringTask implements Task {
}
}
if (log.isDebugEnabled()) {
log.debug("Following devices selected to send the notification for " + deviceType);
log.debug("Following '" + deviceType + "' devices selected to send the notification " +
"for policy monitoring");
for (Device device : notifiableDevices) {
log.debug(device.getDeviceIdentifier());
}
}
if (!notifiableDevices.isEmpty()) {
monitoringManager.addMonitoringOperation(notifiableDevices);
monitoringManager.addMonitoringOperation(deviceType, notifiableDevices);
}
}
}
@ -178,4 +176,5 @@ public class MonitoringTask implements Task {
log.info("No device types registered currently. So did not run the monitoring task.");
}
}
}

@ -259,7 +259,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
deviceList.add(device);
MonitoringManager manager = new MonitoringManagerImpl();
manager.addMonitoringOperation(deviceList);
manager.addMonitoringOperation(DEVICE_TYPE_A, deviceList);
policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures);
boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A));
@ -289,7 +289,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
deviceList.add(device);
MonitoringManager manager = new MonitoringManagerImpl();
manager.addMonitoringOperation(deviceList);
manager.addMonitoringOperation(DEVICE_TYPE_A, deviceList);
policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures);
boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A));

@ -316,7 +316,7 @@ public class MonitoringManagerImplTest extends BasePolicyManagementDAOTest{
@Test
public void testAddMonitoringOperation() throws Exception {
monitoringManager.addMonitoringOperation(deviceMgtService.getAllDevices());
monitoringManager.addMonitoringOperation(DEVICE_TYPE_E, deviceMgtService.getAllDevices());
}
@Test

@ -182,6 +182,9 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE (
DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_RES_RT' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE'))
CREATE INDEX IDX_DM_RES_RT ON DM_DEVICE_OPERATION_RESPONSE(RECEIVED_TIMESTAMP);
IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENID_OPID' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE'))
CREATE INDEX IDX_ENID_OPID ON DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, ENROLMENT_ID);
@ -190,11 +193,12 @@ CREATE INDEX IDX_DM_EN_OP_MAP_RES ON DM_DEVICE_OPERATION_RESPONSE(EN_OP_MAP_ID);
IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_OPERATION_RESPONSE_LARGE]') AND TYPE IN (N'U'))
CREATE TABLE DM_DEVICE_OPERATION_RESPONSE_LARGE (
ID INTEGER IDENTITY(1,1) NOT NULL,
ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
EN_OP_MAP_ID INTEGER NOT NULL,
OPERATION_RESPONSE VARBINARY(MAX) DEFAULT NULL,
RECEIVED_TIMESTAMP DATETIME2 DEFAULT NULL
RECEIVED_TIMESTAMP DATETIME2 DEFAULT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL
PRIMARY KEY (ID),
CONSTRAINT FK_DM_DEVICE_OPERATION_RESP_LARGE_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
@ -419,6 +423,9 @@ CREATE TABLE DM_APPLICATION (
DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_APPLICATION' AND OBJECT_ID = OBJECT_ID('DM_APPLICATION'))
CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID);
-- POLICY RELATED TABLES FINISHED --
-- POLICY AND DEVICE GROUP MAPPING --

@ -439,6 +439,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB;
CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID);
-- END OF POLICY RELATED TABLES --
-- POLICY AND DEVICE GROUP MAPPING --

Loading…
Cancel
Save