From 6ef4bdd73e08e56b0f2b66cbade947c3ee0ac215 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Wed, 2 Sep 2015 22:55:52 +0530 Subject: [PATCH] Fixing enrolment id issues --- .../mgt/common/monitor/ComplianceData.java | 9 ++ .../common/monitor/PolicyDeviceWrapper.java | 51 +++++++++++ .../policy/mgt/core/dao/MonitoringDAO.java | 13 ++- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 89 +++++++++++++++++-- .../core/mgt/impl/MonitoringManagerImpl.java | 62 ++++++++----- 5 files changed, 194 insertions(+), 30 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/PolicyDeviceWrapper.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceData.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceData.java index 0304e690f01..cb41b1ed9d4 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceData.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceData.java @@ -28,6 +28,7 @@ public class ComplianceData { private int id; private int deviceId; + private int enrolmentId; private int policyId; List complianceFeatures; private boolean status; @@ -52,6 +53,14 @@ public class ComplianceData { this.id = id; } + public int getEnrolmentId() { + return enrolmentId; + } + + public void setEnrolmentId(int enrolmentId) { + this.enrolmentId = enrolmentId; + } + public Timestamp getLastRequestedTime() { return lastRequestedTime; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/PolicyDeviceWrapper.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/PolicyDeviceWrapper.java new file mode 100644 index 00000000000..51560bed00a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/PolicyDeviceWrapper.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.common.monitor; + +public class PolicyDeviceWrapper { + + private int policyId; + private int deviceId; + private int enrolmentId; + + public int getPolicyId() { + return policyId; + } + + public void setPolicyId(int policyId) { + this.policyId = policyId; + } + + public int getDeviceId() { + return deviceId; + } + + public void setDeviceId(int deviceId) { + this.deviceId = deviceId; + } + + public int getEnrolmentId() { + return enrolmentId; + } + + public void setEnrolmentId(int enrolmentId) { + this.enrolmentId = enrolmentId; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java index 695ed65f595..522b20f3703 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.dao; 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.PolicyDeviceWrapper; import java.util.HashMap; import java.util.List; @@ -35,20 +36,26 @@ public interface MonitoringDAO { * @param devicePolicyMap * @throws MonitoringDAOException */ + + @Deprecated void addComplianceDetails(Map devicePolicyMap) throws MonitoringDAOException; - void setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException; + void addComplianceDetails(List policyDeviceWrappers) throws MonitoringDAOException; + + void setDeviceAsNoneCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException; - void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; + void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException; void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) throws MonitoringDAOException; - ComplianceData getCompliance(int deviceId) throws MonitoringDAOException; + ComplianceData getCompliance(int deviceId, int enrolmentId) throws MonitoringDAOException; List getCompliance(List deviceIds) throws MonitoringDAOException; + List getCompliance() throws MonitoringDAOException; + List getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException; void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 312988732be..b7d58353d51 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; 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.PolicyDeviceWrapper; import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; @@ -109,7 +110,43 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public void setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException { + public void addComplianceDetails(List policyDeviceWrapper) throws MonitoringDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + if (log.isDebugEnabled()) { + 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 (?, ?, ?, ?, ?, ?, ?) "; + 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.addBatch(); + } + stmt.executeBatch(); + } catch (SQLException e) { + throw new MonitoringDAOException("Error occurred while adding the none compliance to the database.", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); + } + } + + @Override + public void setDeviceAsNoneCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; @@ -118,12 +155,13 @@ public class MonitoringDAOImpl implements MonitoringDAO { 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 = ?"; + " ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, policyId); stmt.setInt(3, deviceId); stmt.setInt(4, tenantId); + stmt.setInt(5, enrolmentId); stmt.executeUpdate(); } catch (SQLException e) { @@ -135,7 +173,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException { + public void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; @@ -144,12 +182,13 @@ public class MonitoringDAOImpl implements MonitoringDAO { try { conn = this.getConnection(); String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + - " WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + " WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, 1); stmt.setTimestamp(2, currentTimestamp); stmt.setInt(3, deviceId); stmt.setInt(4, tenantId); + stmt.setInt(5, enrolmentId); stmt.executeUpdate(); // generatedKeys = stmt.getGeneratedKeys(); @@ -198,7 +237,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public ComplianceData getCompliance(int deviceId) throws MonitoringDAOException { + public ComplianceData getCompliance(int deviceId, int enrolmentId) throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; @@ -208,10 +247,11 @@ public class MonitoringDAOImpl implements MonitoringDAO { try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); + stmt.setInt(3, enrolmentId); resultSet = stmt.executeQuery(); @@ -253,6 +293,43 @@ public class MonitoringDAOImpl implements MonitoringDAO { ComplianceData complianceData = new ComplianceData(); 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); + } + return complianceDataList; + } catch (SQLException e) { + throw new MonitoringDAOException("Unable to retrieve compliance data from database.", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + } + } + + @Override + public List getCompliance() throws MonitoringDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List complianceDataList = new ArrayList<>(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE TENANT_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); + + resultSet = stmt.executeQuery(); + while (resultSet.next()) { + ComplianceData complianceData = new ComplianceData(); + 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")); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index e3a4d99efa9..e72b1d25fa2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -39,10 +39,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceIm import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.ProfileFeature; -import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; -import org.wso2.carbon.policy.mgt.common.monitor.ComplianceDecisionPoint; -import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; -import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; +import org.wso2.carbon.policy.mgt.common.monitor.*; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.impl.ComplianceDecisionPointImpl; @@ -98,7 +95,7 @@ public class MonitoringManagerImpl implements MonitoringManager { // run. try { PolicyManagementDAOFactory.openConnection(); - ComplianceData cmd = monitoringDAO.getCompliance(device.getId()); + ComplianceData cmd = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, policy, deviceResponse); @@ -118,7 +115,8 @@ public class MonitoringManagerImpl implements MonitoringManager { if (complianceFeatures != null && !complianceFeatures.isEmpty()) { try { PolicyManagementDAOFactory.beginTransaction(); - monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); + monitoringDAO.setDeviceAsNoneCompliance(device.getId(), device.getEnrolmentInfo().getId(), + policy.getId()); if (log.isDebugEnabled()) { log.debug("Compliance status primary key " + complianceData.getId()); } @@ -141,7 +139,8 @@ public class MonitoringManagerImpl implements MonitoringManager { } else { try { PolicyManagementDAOFactory.beginTransaction(); - monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); + monitoringDAO.setDeviceAsCompliance(device.getId(), device.getEnrolmentInfo().getId(), policy + .getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId()); PolicyManagementDAOFactory.commitTransaction(); } finally { @@ -175,7 +174,8 @@ public class MonitoringManagerImpl implements MonitoringManager { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); PolicyManagementDAOFactory.openConnection(); - ComplianceData complianceData = monitoringDAO.getCompliance(device.getId()); + ComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() + .getId()); if (complianceData == null || !complianceData.isStatus()) { return false; } @@ -203,7 +203,7 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); - complianceData = monitoringDAO.getCompliance(device.getId()); + complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); List complianceFeatures = monitoringDAO.getNoneComplianceFeatures(complianceData.getId()); complianceData.setComplianceFeatures(complianceFeatures); @@ -228,17 +228,25 @@ public class MonitoringManagerImpl implements MonitoringManager { //int tenantId = PolicyManagerUtil.getTenantId(); Map deviceIds = new HashMap<>(); - List complianceDatas; + List complianceDatas = new ArrayList<>(); HashMap devicePolicyIdMap; - for (Device device : devices) { - deviceIds.put(device.getId(), device); - } - - List deviceIDs = new ArrayList<>(deviceIds.keySet()); try { PolicyManagementDAOFactory.openConnection(); - complianceDatas = monitoringDAO.getCompliance(deviceIDs); + List cd = monitoringDAO.getCompliance(); + + for (Device device : devices) { + deviceIds.put(device.getId(), device); + + for (ComplianceData data : cd) { + if (device.getId() == data.getDeviceId() && device.getEnrolmentInfo().getId() == data + .getEnrolmentId()) { + complianceDatas.add(data); + } + } + } + List deviceIDs = new ArrayList<>(deviceIds.keySet()); + devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs); } catch (SQLException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); @@ -254,7 +262,9 @@ public class MonitoringManagerImpl implements MonitoringManager { Map deviceIdsWithExistingOperation = new HashMap<>(); Map inactiveDeviceIds = new HashMap<>(); Map deviceToMarkUnreachable = new HashMap<>(); - Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); + //Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); + + List firstTimeDevices = new ArrayList<>(); Map tempMap = new HashMap<>(); @@ -285,21 +295,31 @@ public class MonitoringManagerImpl implements MonitoringManager { for (Device device : devices) { if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) { deviceIdsToAddOperation.put(device.getId(), device); - firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId())); + + 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 (Map.Entry map : firstTimeDeviceIdsWithPolicyIds.entrySet()) { - log.debug("First time device primary key : " + map.getKey() + " & policy id " + map.getValue()); + 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(firstTimeDeviceIdsWithPolicyIds); + monitoringDAO.addComplianceDetails(firstTimeDevices); } if (!deviceIdsWithExistingOperation.isEmpty()) {