Fixing enrolment id issues

revert-70aa11f8
geethkokila 9 years ago
parent dc2680ac5e
commit 6ef4bdd73e

@ -28,6 +28,7 @@ public class ComplianceData {
private int id;
private int deviceId;
private int enrolmentId;
private int policyId;
List<ComplianceFeature> 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;
}

@ -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;
}
}

@ -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 <Device Id, Policy Id>
* @throws MonitoringDAOException
*/
@Deprecated
void addComplianceDetails(Map<Integer, Integer> devicePolicyMap) throws MonitoringDAOException;
void setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException;
void addComplianceDetails(List<PolicyDeviceWrapper> 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<ComplianceFeature>
complianceFeatures)
throws MonitoringDAOException;
ComplianceData getCompliance(int deviceId) throws MonitoringDAOException;
ComplianceData getCompliance(int deviceId, int enrolmentId) throws MonitoringDAOException;
List<ComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException;
List<ComplianceData> getCompliance() throws MonitoringDAOException;
List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException;
void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException;

@ -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> 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<ComplianceData> getCompliance() throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ComplianceData> 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"));

@ -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<ComplianceFeature> complianceFeatures =
monitoringDAO.getNoneComplianceFeatures(complianceData.getId());
complianceData.setComplianceFeatures(complianceFeatures);
@ -228,17 +228,25 @@ public class MonitoringManagerImpl implements MonitoringManager {
//int tenantId = PolicyManagerUtil.getTenantId();
Map<Integer, Device> deviceIds = new HashMap<>();
List<ComplianceData> complianceDatas;
List<ComplianceData> complianceDatas = new ArrayList<>();
HashMap<Integer, Integer> devicePolicyIdMap;
try {
PolicyManagementDAOFactory.openConnection();
List<ComplianceData> 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<Integer> deviceIDs = new ArrayList<>(deviceIds.keySet());
try {
PolicyManagementDAOFactory.openConnection();
complianceDatas = monitoringDAO.getCompliance(deviceIDs);
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<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
Map<Integer, Device> inactiveDeviceIds = new HashMap<>();
Map<Integer, Device> deviceToMarkUnreachable = new HashMap<>();
Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>();
//Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>();
List<PolicyDeviceWrapper> firstTimeDevices = new ArrayList<>();
Map<Integer, ComplianceData> 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<Integer, Integer> 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()) {

Loading…
Cancel
Save