From 90f8f4aab6a72fb33f7cdc457e78014d61fbe0db Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 7 Aug 2015 13:03:34 +0530 Subject: [PATCH] Adding test cases and fixing some logic issues and fixing the null pointer from the policy core --- .../mgt/core/PolicyManagerServiceImpl.java | 2 +- .../policy/mgt/core/dao/MonitoringDAO.java | 4 +- .../core/dao/PolicyManagementDAOFactory.java | 9 ++ .../mgt/core/dao/impl/FeatureDAOImpl.java | 19 ++-- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 39 ++++---- .../mgt/core/dao/impl/PolicyDAOImpl.java | 49 +++++++--- .../core/mgt/impl/MonitoringManagerImpl.java | 90 ++++++++++++------- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 52 ++++++----- .../mgt/core/util/PolicyManagerUtil.java | 13 +++ .../mgt/core/BasePolicyManagementDAOTest.java | 2 +- .../policy/mgt/core/MonitoringTestCase.java | 53 ++++++----- .../policy/mgt/core/PolicyDAOTestCase.java | 13 +-- .../config/datasource/data-source-config.xml | 15 ++++ .../src/test/resources/sql/CreateH2TestDB.sql | 1 + .../src/main/resources/dbscripts/cdm/h2.sql | 1 + 15 files changed, 239 insertions(+), 123 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index ec9bd3066b..e96e4ff6a0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -94,7 +94,7 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { Policy policy = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). getEffectivePolicy(deviceIdentifier); - if (policy != null) { + if (policy == null) { return null; } List deviceIdentifiers = new ArrayList(); 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 f073fafa0e..695ed65f59 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 @@ -37,9 +37,9 @@ public interface MonitoringDAO { */ void addComplianceDetails(Map devicePolicyMap) throws MonitoringDAOException; - int setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException; + void setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException; - int setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; + void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index e59aa9b82d..891ce50dd5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -128,6 +128,15 @@ public class PolicyManagementDAOFactory { e); } } +// if (log.isDebugEnabled()) { +// log.debug(" Print the connction : ::::::: " + currentConnection.get().toString()); +// StackTraceElement[] sts = Thread.currentThread().getStackTrace(); +// for (StackTraceElement st: sts) { +// log.debug(st.getClassName() + " -- " + st.getLineNumber()); +//// break; +// } +// log.debug(Thread.currentThread().getStackTrace()); +// } return currentConnection.get(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index f5933350cf..aafb6dbdd5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -29,6 +29,7 @@ import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -177,9 +178,9 @@ public class FeatureDAOImpl implements FeatureDAO { stmt.setString(2, feature.getFeatureCode()); stmt.setInt(3, feature.getDeviceTypeId()); if (conn.getMetaData().getDriverName().contains("H2")) { - stmt.setObject(4, feature.getContent(), Types.JAVA_OBJECT); + stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); } else { - stmt.setObject(4, feature.getContent()); + stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); } stmt.addBatch(); //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 @@ -198,6 +199,10 @@ public class FeatureDAOImpl implements FeatureDAO { String msg = "Error occurred while adding the feature list to the database."; log.error(msg, e); throw new FeatureManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Error occurred while adding the feature list to the database."; + log.error(msg, e); + throw new FeatureManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } @@ -217,9 +222,9 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); for (ProfileFeature feature : features) { if (conn.getMetaData().getDriverName().contains("H2")) { - stmt.setObject(1, feature.getContent(), Types.JAVA_OBJECT); + stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent())); } else { - stmt.setObject(1, feature.getContent()); + stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent())); } stmt.setInt(2, profileId); stmt.setString(3, feature.getFeatureCode()); @@ -232,6 +237,10 @@ public class FeatureDAOImpl implements FeatureDAO { String msg = "Error occurred while adding the feature list to the database."; log.error(msg, e); throw new FeatureManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Error occurred while adding the feature list to the database."; + log.error(msg, e); + throw new FeatureManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -313,7 +322,7 @@ public class FeatureDAOImpl implements FeatureDAO { contentBytes = (byte[]) resultSet.getBytes("CONTENT"); bais = new ByteArrayInputStream(contentBytes); ois = new ObjectInputStream(bais); - profileFeature.setContent(ois.readObject().toString()); + profileFeature.setContent((Object) ois.readObject().toString()); } finally { if (bais != null) { try { 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 6cf68e6556..7549688e5d 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 @@ -78,6 +78,13 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet generatedKeys = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + if (log.isDebugEnabled()) { + log.debug("Adding the compliance details for devices and policies"); + for (Map.Entry map : devicePolicyMap.entrySet()) { + log.debug(map.getKey() + " -- " + map.getValue()); + } + } + try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + @@ -104,7 +111,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public int setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException { + public void setDeviceAsNoneCompliance(int deviceId, int policyId) throws + MonitoringDAOException { Connection conn; PreparedStatement stmt = null; @@ -112,24 +120,15 @@ public class MonitoringDAOImpl implements MonitoringDAO { Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); try { conn = this.getConnection(); -// String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, -// LAST_FAILED_TIME, " + -// "ATTEMPTS) VALUES (?, ?, ?, ?, ?) "; String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," + " ATTEMPTS=0 WHERE DEVICE_ID = ?"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, policyId); stmt.setInt(3, deviceId); stmt.executeUpdate(); - generatedKeys = stmt.getGeneratedKeys(); - if (generatedKeys.next()) { - return generatedKeys.getInt(1); - } else { - return 0; - } } catch (SQLException e) { String msg = "Error occurred while updating the none compliance to the database."; @@ -142,7 +141,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public int setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException { + public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; @@ -152,19 +151,19 @@ public class MonitoringDAOImpl implements MonitoringDAO { conn = this.getConnection(); String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + " WHERE DEVICE_ID = ?"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query); stmt.setInt(1, 1); stmt.setTimestamp(2, currentTimestamp); stmt.setInt(3, deviceId); stmt.executeUpdate(); - generatedKeys = stmt.getGeneratedKeys(); - if (generatedKeys.next()) { - return generatedKeys.getInt(1); - } else { - return 0; - } +// generatedKeys = stmt.getGeneratedKeys(); +// if (generatedKeys.next()) { +// return generatedKeys.getInt(1); +// } else { +// return 0; +// } } catch (SQLException e) { String msg = "Error occurred while deleting the none compliance to the database."; @@ -214,7 +213,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - ComplianceData complianceData = null; + ComplianceData complianceData = new ComplianceData(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 109068c15e..2b21f36942 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -413,7 +413,7 @@ public class PolicyDAOImpl implements PolicyDAO { } catch (SQLException e) { String msg = "Error occurred while inserting the criterion to policy (" + policy.getPolicyName() + ") " + - "to database."; + "to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { @@ -433,7 +433,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + - "CONTENT) VALUES (?, ?, ?, ?)"; + "CONTENT) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(query); for (PolicyCriterion criterion : policyCriteria) { @@ -444,7 +444,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, criterion.getId()); stmt.setString(2, name); stmt.setString(3, prop.getProperty(name)); - stmt.setObject(4, criterion.getObjectMap()); + stmt.setBytes(4, PolicyManagerUtil.getBytes(criterion.getObjectMap())); stmt.addBatch(); } stmt.executeBatch(); @@ -455,6 +455,10 @@ public class PolicyDAOImpl implements PolicyDAO { String msg = "Error occurred while inserting the criterion properties to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Error occurred while inserting the criterion properties to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -474,9 +478,9 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " + - "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + - "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + - "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; + "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + + "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + + "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); resultSet = stmt.executeQuery(); @@ -522,7 +526,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + - " WHERE ID = ?"; + " WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getTenantId()); @@ -760,7 +764,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); stmt.setInt(2, policy.getId()); - stmt.setObject(3, policy); + stmt.setBytes(3, PolicyManagerUtil.getBytes(policy)); stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); stmt.setInt(6, tenantId); @@ -771,6 +775,10 @@ public class PolicyDAOImpl implements PolicyDAO { String msg = "Error occurred while adding the evaluated feature list to device."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Error occurred while adding the evaluated feature list to device."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -813,10 +821,10 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + - "APPLIED = ? WHERE DEVICE_ID = ?"; + "APPLIED = ? WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); - stmt.setObject(2, policy); + stmt.setBytes(2, PolicyManagerUtil.getBytes(policy)); stmt.setTimestamp(3, currentTimestamp); stmt.setBoolean(4, false); stmt.setInt(5, deviceId); @@ -826,6 +834,10 @@ public class PolicyDAOImpl implements PolicyDAO { String msg = "Error occurred while updating the evaluated feature list to device."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Error occurred while updating the evaluated feature list to device."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -1067,7 +1079,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE)" + - " VALUES (?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); @@ -1243,6 +1255,12 @@ public class PolicyDAOImpl implements PolicyDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); resultSet = stmt.executeQuery(); +// log.debug("Logging the statement................." + stmt.toString()); +// log.debug("+++++++++++++++++++++++++++++"); +// log.debug(conn.toString()); + + +// log.debug("+++++++++++++++++++++++++++++"); while (resultSet.next()) { ByteArrayInputStream bais = null; @@ -1288,6 +1306,15 @@ public class PolicyDAOImpl implements PolicyDAO { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); this.closeConnection(); } +// +// if (policy != null && log.isDebugEnabled()) { +// log.debug("Applied policy logging details started ------------------"); +// log.debug("Applied policy name " + policy.getPolicyName() + "for the device id " + deviceId); +// log.debug(policy.getCompliance()); +// log.debug(policy.getId()); +// log.debug(policy.getPriorityId()); +// log.debug("Applied policy logging details finished...."); +// } return policy; } 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 117bfc2600..e2acdb0884 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 @@ -75,46 +75,64 @@ public class MonitoringManagerImpl implements MonitoringManager { public List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Object deviceResponse) throws PolicyComplianceException { - List complianceFeatures; + List complianceFeatures = new ArrayList<>(); try { - PolicyManagementDAOFactory.beginTransaction(); + DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); PolicyManager manager = new PolicyManagerImpl(); - Device device = service.getDevice(deviceIdentifier); Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier); //policyDAO.getAppliedPolicy(device // .getId()); - PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance(). - getPolicyMonitoringService(deviceIdentifier.getType()); - - ComplianceData complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, - policy, deviceResponse); - complianceData.setPolicy(policy); - complianceFeatures = complianceData.getComplianceFeatures(); - complianceData.setDeviceId(device.getId()); - complianceData.setPolicyId(policy.getId()); - - if (!complianceFeatures.isEmpty()) { - int complianceId = monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); - complianceData.setId(complianceId); - monitoringDAO.addNoneComplianceFeatures(complianceId, device.getId(), complianceFeatures); - complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData); - List profileFeatures = policy.getProfile().getProfileFeaturesList(); - for (ComplianceFeature compFeature : complianceFeatures) { - for (ProfileFeature profFeature : profileFeatures) { - if (profFeature.getFeatureCode().equalsIgnoreCase(compFeature.getFeatureCode())) { - compFeature.setFeature(profFeature); + if (policy != null) { + PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance(). + getPolicyMonitoringService(deviceIdentifier.getType()); + + ComplianceData complianceData; + // This was retrieved from database because compliance id must be present for other dao operations to + // run. + ComplianceData cmd = monitoringDAO.getCompliance(device.getId()); + complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, + policy, deviceResponse); + complianceData.setId(cmd.getId()); + complianceData.setPolicy(policy); + complianceFeatures = complianceData.getComplianceFeatures(); + complianceData.setDeviceId(device.getId()); + complianceData.setPolicyId(policy.getId()); + + PolicyManagementDAOFactory.beginTransaction(); + //This was added because update query below that did not return the update table primary key. + + if (!complianceFeatures.isEmpty()) { + + monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); + if (log.isDebugEnabled()) { + log.debug("Compliance status primary key " + complianceData.getId()); + } +// complianceData.setId(cmf.getId()); + monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures); + complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData); + List profileFeatures = policy.getProfile().getProfileFeaturesList(); + for (ComplianceFeature compFeature : complianceFeatures) { + for (ProfileFeature profFeature : profileFeatures) { + if (profFeature.getFeatureCode().equalsIgnoreCase(compFeature.getFeatureCode())) { + compFeature.setFeature(profFeature); + } } } + + } else { + monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); + //complianceData.setId(cmf.getId()); + monitoringDAO.deleteNoneComplianceData(complianceData.getId()); } + PolicyManagementDAOFactory.commitTransaction(); } else { - int complianceId = monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); - complianceData.setId(complianceId); - monitoringDAO.deleteNoneComplianceData(complianceId); + if (log.isDebugEnabled()) { + log.debug("There is no policy applied to this device, hence compliance monitoring was not called."); + } } - PolicyManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { try { @@ -220,7 +238,7 @@ public class MonitoringManagerImpl implements MonitoringManager { public void addMonitoringOperation(List devices) throws PolicyComplianceException { try { - PolicyManagementDAOFactory.beginTransaction(); + ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); @@ -238,7 +256,7 @@ public class MonitoringManagerImpl implements MonitoringManager { Map deviceIdsToAddOperation = new HashMap<>(); Map deviceIdsWithExistingOperation = new HashMap<>(); Map inactiveDeviceIds = new HashMap<>(); - Map firstTimeDeviceId = new HashMap<>(); + Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); Map tempMap = new HashMap<>(); @@ -265,13 +283,23 @@ public class MonitoringManagerImpl implements MonitoringManager { for (Device device : devices) { if (!tempMap.containsKey(device.getId())) { deviceIdsToAddOperation.put(device.getId(), device); - firstTimeDeviceId.put(device.getId(), devicePolicyIdMap.get(device.getId())); + 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()); } } + + PolicyManagementDAOFactory.beginTransaction(); + if (!deviceIdsToAddOperation.isEmpty()) { this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); - monitoringDAO.addComplianceDetails(firstTimeDeviceId); + monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); } if (!deviceIdsWithExistingOperation.isEmpty()) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index fa0eb8303e..e32de22c95 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -94,11 +94,16 @@ public class PolicyManagerImpl implements PolicyManager { if (policy.getPolicyCriterias() != null) { List criteria = policy.getPolicyCriterias(); for (PolicyCriterion criterion : criteria) { - if (!policyDAO.checkCriterionExists(criterion.getName())) { + + Criterion cr = policyDAO.getCriterion(criterion.getName()); + + if (cr.getId() == 0) { Criterion criteriaObj = new Criterion(); criteriaObj.setName(criterion.getName()); policyDAO.addCriterion(criteriaObj); criterion.setCriteriaId(criteriaObj.getId()); + } else { + criterion.setCriteriaId(cr.getId()); } } @@ -126,7 +131,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); @@ -137,7 +142,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the profile related to policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (FeatureManagerDAOException e) { @@ -147,7 +152,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the features of profile related to policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -210,7 +215,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while updating the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -255,7 +260,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (ProfileManagerDAOException e) { @@ -265,7 +270,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the profile for policy (" - + policy.getId() + ")"; + + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (FeatureManagerDAOException e) { @@ -275,7 +280,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the profile features for policy (" - + policy.getId() + ")"; + + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -308,7 +313,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the policy (" - + policyId + ")"; + + policyId + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (ProfileManagerDAOException e) { @@ -318,7 +323,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the profile for policy (" - + policyId + ")"; + + policyId + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (FeatureManagerDAOException e) { @@ -328,7 +333,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the profile features for policy (" - + policyId + ")"; + + policyId + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -370,7 +375,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { @@ -417,7 +422,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -455,7 +460,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ") to user list."; + + policy.getId() + " - " + policy.getPolicyName() + ") to user list."; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -589,12 +594,12 @@ public class PolicyManagerImpl implements PolicyManager { Collections.sort(policies); } catch (PolicyManagerDAOException e) { String msg = "Error occurred while getting the policies for device identifier (" + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; + deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { String msg = "Error occurred while getting device related to device identifier (" + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; + deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -731,7 +736,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policy.getId() + ")"; + deviceId + " - " + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { @@ -751,11 +756,12 @@ public class PolicyManagerImpl implements PolicyManager { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); deviceId = device.getId(); - boolean exist = policyDAO.checkPolicyAvailable(deviceId); + // boolean exist = policyDAO.checkPolicyAvailable(deviceId); + Policy policySaved = policyDAO.getAppliedPolicy(deviceId); + PolicyManagementDAOFactory.beginTransaction(); - if (exist) { - Policy policySaved = policyDAO.getAppliedPolicy(deviceId); - if (!policy.equals(policySaved)) { + if (policySaved != null && policySaved.getId() != 0) { + if (policy.getId() != policySaved.getId()){ policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } } else { @@ -769,7 +775,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policy.getId() + ")"; + deviceId + " - " + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { @@ -814,7 +820,7 @@ public class PolicyManagerImpl implements PolicyManager { return true; } catch (PolicyManagerDAOException e) { String msg = "Error occurred while setting the policy has applied to device (" + - deviceIdentifier.getId() + ")"; + deviceIdentifier.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 911755c17b..d63a0083d0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -34,7 +34,9 @@ import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -123,4 +125,15 @@ public class PolicyManagerUtil { return policyOperation; } + + public static byte[] getBytes(Object obj) throws java.io.IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(obj); + oos.flush(); + oos.close(); + bos.close(); + byte[] data = bos.toByteArray(); + return data; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java index a391495b30..8cad5f5b78 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java @@ -59,7 +59,7 @@ public abstract class BasePolicyManagementDAOTest { OperationManagementDAOFactory.init(dataSource); } - public void initiatePriviledgeCaronContext() throws Exception { + public void initiatePrivilegedCaronContext() throws Exception { if (System.getProperty("carbon.home") == null) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index c99e4a8c49..986f642e8f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -93,13 +93,18 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { PolicyManager manager = new PolicyManagerImpl(); Policy policy = manager.getAppliedPolicyToDevice(identifier); - log.debug(policy.getId()); - log.debug(policy.getPolicyName()); - log.debug(policy.getCompliance()); + if (policy != null) { + log.debug(policy.getId()); + log.debug(policy.getPolicyName()); + log.debug(policy.getCompliance()); + } else { + log.debug("Applied policy was a null object."); + } } - @Test(dependsOnMethods = ("testMonitorDao")) + + @Test(dependsOnMethods = ("getDeviceAppliedPolicy")) public void addComplianceOperation() throws PolicyManagementException, DeviceManagementException, PolicyComplianceException { @@ -131,47 +136,44 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { public void checkComplianceFromMonitoringService() throws PolicyManagementException, DeviceManagementException, PolicyComplianceException { + + PolicyMonitoringServiceTest monitoringServiceTest = new PolicyMonitoringServiceTest(); PolicyManagementDataHolder.getInstance().setPolicyMonitoringService(monitoringServiceTest.getType(), monitoringServiceTest); DeviceManagementProviderService adminService = new DeviceManagementProviderServiceImpl(); - PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); - + // PolicyManager policyManagerService = new PolicyManagerImpl(); List devices = adminService.getAllDevices(); + for (Device device : devices) { + log.debug(device.getDeviceIdentifier()); + log.debug(device.getType()); + log.debug(device.getName()); + } monitoringServiceTest.notifyDevices(devices); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(devices.get(0).getDeviceIdentifier()); - deviceIdentifier.setType(devices.get(0).getType()); - - Policy policy = policyManagerService.getAppliedPolicyToDevice(deviceIdentifier); + PolicyManager manager = new PolicyManagerImpl(); + Policy policy = manager.getAppliedPolicyToDevice(identifier); Object ob = new Object(); - - // This has to be removed, until milan sends the full request. - - policy = policyManagerService.getPolicies(ANDROID).get(0); - - // remove above - - monitoringServiceTest.checkPolicyCompliance(deviceIdentifier, policy, ob); - -// MonitoringTask task = new MonitoringTask(); -// task.execute(); + monitoringServiceTest.checkPolicyCompliance(identifier, policy, ob); } @Test(dependsOnMethods = ("checkComplianceFromMonitoringService")) - public void checkCompliance() throws DeviceManagementException, PolicyComplianceException { + public void checkCompliance() throws DeviceManagementException, PolicyComplianceException, PolicyManagementException { + PolicyMonitoringServiceTest monitoringServiceTest = new PolicyMonitoringServiceTest(); + PolicyManagementDataHolder.getInstance().setPolicyMonitoringService(monitoringServiceTest.getType(), + monitoringServiceTest); DeviceManagementProviderService adminService = new DeviceManagementProviderServiceImpl(); + List devices = adminService.getAllDevices(); @@ -183,7 +185,10 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { MonitoringManager monitoringManager = new MonitoringManagerImpl(); - monitoringManager.checkPolicyCompliance(deviceIdentifier, ob); + log.debug(identifier.getId()); + log.debug(identifier.getType()); + + monitoringManager.checkPolicyCompliance(identifier, ob); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index a5e4f681f2..4c0ecc1727 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -57,7 +57,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { public void init() throws Exception { initDatSource(); // System.setProperty("GetTenantIDForTest", "Super"); - initiatePriviledgeCaronContext(); + initiatePrivilegedCaronContext(); } @@ -82,23 +82,26 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { List devices = deviceDAO.getDevices(-1234); + log.debug("--- Printing device taken by calling the device dao layer by tenant id."); for (Device device : devices) { - log.debug(device.getDeviceIdentifier() + " ----- X"); + log.debug(device.getDeviceIdentifier()); } + + log.debug("--- Printing device taken by calling the device dao layer by tenant id and device type."); List devices2 = deviceDAO.getDevices("android", -1234); for (Device device : devices2) { - log.debug(device.getDeviceIdentifier() + " ----- XX"); + log.debug(device.getDeviceIdentifier()); } - DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); + log.debug("Printing device taken by calling the service layer with device type."); List devices3 = service.getAllDevices("android"); for (Device device : devices3) { - log.debug(device.getDeviceIdentifier() + " ----- XXX"); + log.debug(device.getDeviceIdentifier()); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml index b07866513a..7450e52f77 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml @@ -18,8 +18,23 @@ --> + + + + + jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 org.h2.Driver wso2carbon wso2carbon + + + + + + + + + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 60bcedf490..219386f226 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -320,6 +320,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( LAST_FAILED_TIME TIMESTAMP NULL, ATTEMPTS INT NULL, PRIMARY KEY (ID), + UNIQUE INDEX DEVICE_ID_UNIQUE (DEVICE_ID ASC), CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY FOREIGN KEY (POLICY_ID) REFERENCES DM_POLICY (ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 96053f298d..20545e5e9d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -306,6 +306,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( LAST_FAILED_TIME TIMESTAMP NULL, ATTEMPTS INT NULL, PRIMARY KEY (ID), + UNIQUE INDEX DEVICE_ID_UNIQUE (DEVICE_ID ASC), CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY FOREIGN KEY (POLICY_ID) REFERENCES DM_POLICY (ID)