Added device status change upon inactivity mechanism

revert-70aa11f8
harshanl 8 years ago
parent 39519800ab
commit dd03d0b740

@ -120,8 +120,8 @@ public class OperationManagerImpl implements OperationManager {
boolean isNotRepeated = false; boolean isNotRepeated = false;
boolean hasExistingTaskOperation; boolean hasExistingTaskOperation;
int enrolmentId; int enrolmentId;
if (operationDto.getControl() == if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto.
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { getControl()) {
isNotRepeated = true; isNotRepeated = true;
} }
@ -357,12 +357,25 @@ public class OperationManagerImpl implements OperationManager {
deviceId.getId() + "'"); deviceId.getId() + "'");
} }
int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); //
if (enrolmentId < 0) { EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId);
if (enrolmentInfo == null) {
throw new OperationManagementException("Device not found for the given device Identifier:" + throw new OperationManagementException("Device not found for the given device Identifier:" +
deviceId.getId() + " and given type:" + deviceId.getId() + " and given type:" +
deviceId.getType()); deviceId.getType());
} }
int enrolmentId = enrolmentInfo.getId();
//Changing the enrollment status & attempt count if the device is marked as inactive or unreachable
switch (enrolmentInfo.getStatus()) {
case ACTIVE:
this.resetAttemptCount(enrolmentId);
break;
case INACTIVE:
case UNREACHABLE:
this.resetAttemptCount(enrolmentId);
this.setEnrolmentStatus(deviceId, EnrolmentInfo.Status.ACTIVE);
break;
}
try { try {
OperationManagementDAOFactory.openConnection(); OperationManagementDAOFactory.openConnection();
@ -886,6 +899,65 @@ public class OperationManagerImpl implements OperationManager {
return enrolmentId; return enrolmentId;
} }
private EnrolmentInfo getEnrolmentInfo(DeviceIdentifier deviceId) throws OperationManagementException {
EnrolmentInfo enrolmentInfo;
try {
DeviceManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
String user = this.getUser();
enrolmentInfo = deviceDAO.getEnrolment(deviceId, user, tenantId);
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving enrollment data of '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return enrolmentInfo;
}
private boolean setEnrolmentStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws OperationManagementException {
boolean updateStatus;
try {
DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
String user = this.getUser();
updateStatus = deviceDAO.setEnrolmentStatus(deviceId, user, status, tenantId);
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException("Error occurred while updating enrollment status of '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'", e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return updateStatus;
}
private boolean resetAttemptCount(int enrolmentId) throws OperationManagementException {
boolean resetStatus;
try {
OperationManagementDAOFactory.beginTransaction();
resetStatus = operationDAO.resetAttemptCount(enrolmentId);
OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException e) {
OperationManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException("Error occurred while resetting attempt count of device id : '" +
enrolmentId + "'", e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
return resetStatus;
}
private boolean isTaskScheduledOperation(Operation operation) { private boolean isTaskScheduledOperation(Operation operation) {
TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
getTaskConfiguration(); getTaskConfiguration();
@ -896,5 +968,4 @@ public class OperationManagerImpl implements OperationManager {
} }
return false; return false;
} }
}
}

@ -79,4 +79,6 @@ public interface OperationDAO {
int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException; int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException;
boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException;
} }

@ -37,6 +37,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil;
import java.io.*; import java.io.*;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -1072,4 +1073,29 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
return operations; return operations;
} }
@Override
public boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException {
boolean status = false;
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = OperationManagementDAOFactory.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " +
"WHERE ENROLMENT_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setTimestamp(1, currentTimestamp);
stmt.setInt(2, enrolmentId);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
status = true;
} catch (SQLException e) {
throw new OperationManagementDAOException("Unable to reset the attempt count in database.", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
} }

@ -43,6 +43,16 @@ public interface DeviceManagementProviderService {
*/ */
List<Device> getAllDevices(String deviceType) throws DeviceManagementException; List<Device> getAllDevices(String deviceType) throws DeviceManagementException;
/**
* Method to retrieve all the devices of a given device type without other device information.
*
* @param deviceType Device-type of the required devices
* @return List of devices of given device-type.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices.
*/
List<Device> getAllDevicesWithoutInfo(String deviceType) throws DeviceManagementException;
/** /**
* Method to retrieve all the devices registered in the system. * Method to retrieve all the devices registered in the system.
* *

@ -713,6 +713,80 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return devices; return devices;
} }
@Override
public List<Device> getAllDevicesWithoutInfo(String deviceType) throws DeviceManagementException {
List<Device> devices = new ArrayList<>();
List<Device> allDevices;
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevices(deviceType, this.getTenantId());
if (allDevices == null) {
if (log.isDebugEnabled()) {
log.debug("No device is found upon the type '" + deviceType + "'");
}
return null;
}
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving all devices of type '" +
deviceType + "' that are being managed within the scope of current tenant", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
for (Device device : allDevices) {
DeviceInfo info = null;
try {
DeviceManagementDAOFactory.openConnection();
info = deviceInfoDAO.getDeviceInformation(device.getId());
DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId());
if (info != null) {
info.setLocation(location);
}
} catch (DeviceDetailsMgtDAOException e) {
log.error("Error occurred while retrieving advance info of '" + device.getType() +
"' that carries the id '" + device.getDeviceIdentifier() + "'");
} catch (SQLException e) {
log.error("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
device.setDeviceInfo(info);
try {
DeviceManagementDAOFactory.openConnection();
List<Application> applications = applicationDAO.getInstalledApplications(device.getId());
device.setApplications(applications);
} catch (DeviceManagementDAOException e) {
log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " +
"which carries the id '" + device.getId() + "'", e);
} catch (SQLException e) {
log.error("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
DeviceManager deviceManager = this.getDeviceManager(deviceType);
if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " +
"Therefore, not attempting method 'isEnrolled'");
}
devices.add(device);
continue;
}
Device dmsDevice =
deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties());
}
devices.add(device);
}
return devices;
}
@Override @Override
public void sendEnrolmentInvitation(EmailMetaInfo metaInfo) throws DeviceManagementException { public void sendEnrolmentInvitation(EmailMetaInfo metaInfo) throws DeviceManagementException {
Map<String, TypedValue<Class<?>, Object>> params = new HashMap<>(); Map<String, TypedValue<Class<?>, Object>> params = new HashMap<>();

@ -32,6 +32,8 @@ public interface ComplianceDecisionPoint {
void setDevicesAsUnreachable(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException; void setDevicesAsUnreachable(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException;
void setDevicesAsInactive(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException;
void setDevicesAsUnreachableWith(List<Device> devices) throws PolicyComplianceException; void setDevicesAsUnreachableWith(List<Device> devices) throws PolicyComplianceException;
void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;

@ -204,4 +204,4 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
return monitoringManager.isCompliance(deviceIdentifier); return monitoringManager.isCompliance(deviceIdentifier);
} }
} }

@ -60,9 +60,8 @@ public interface MonitoringDAO {
void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException;
void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException;
void updateAttempts(List<Integer> deviceId, boolean reset) throws MonitoringDAOException; void updateAttempts(List<Integer> deviceId, boolean reset) throws MonitoringDAOException;
}
}

@ -400,7 +400,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} }
@Override @Override
public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { public boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException {
boolean status = false;
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
@ -420,11 +421,13 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setInt(2, deviceId); stmt.setInt(2, deviceId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
status = true;
} catch (SQLException e) { } catch (SQLException e) {
throw new MonitoringDAOException("Unable to update the attempts data in database.", e); throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
return status;
} }
@Override @Override

@ -66,6 +66,22 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
} }
@Override
public void setDevicesAsInactive(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException {
try {
DeviceManagementProviderService service = this.getDeviceManagementProviderService();
for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
Device device = service.getDevice(deviceIdentifier);
service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(),
EnrolmentInfo.Status.INACTIVE);
}
} catch (DeviceManagementException e) {
String msg = "Error occurred while setting the device as inactive";
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override @Override
public void setDevicesAsUnreachableWith(List<Device> devices) throws PolicyComplianceException { public void setDevicesAsUnreachableWith(List<Device> devices) throws PolicyComplianceException {
try { try {

@ -16,7 +16,6 @@
* under the License. * under the License.
*/ */
package org.wso2.carbon.policy.mgt.core.mgt.impl; package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -75,7 +74,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<ComplianceFeature> complianceFeatures = new ArrayList<>(); List<ComplianceFeature> complianceFeatures = new ArrayList<>();
try { try {
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); DeviceManagementProviderService service =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
PolicyManager manager = PolicyManagementDataHolder.getInstance().getPolicyManager(); PolicyManager manager = PolicyManagementDataHolder.getInstance().getPolicyManager();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier); Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier);
@ -90,7 +90,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
ComplianceData cmd = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); ComplianceData cmd = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId());
complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier,
policy, deviceResponse); policy, deviceResponse);
complianceData.setId(cmd.getId()); complianceData.setId(cmd.getId());
complianceData.setPolicy(policy); complianceData.setPolicy(policy);
@ -100,8 +100,9 @@ public class MonitoringManagerImpl implements MonitoringManager {
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyComplianceException("Error occurred while opening a data source connection", e); throw new PolicyComplianceException("Error occurred while opening a data source connection", e);
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + throw new PolicyComplianceException(
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); "Unable to add the none compliance features to database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -112,19 +113,20 @@ public class MonitoringManagerImpl implements MonitoringManager {
try { try {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
monitoringDAO.setDeviceAsNoneCompliance(device.getId(), device.getEnrolmentInfo().getId(), monitoringDAO.setDeviceAsNoneCompliance(device.getId(), device.getEnrolmentInfo().getId(),
policy.getId()); policy.getId());
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Compliance status primary key " + complianceData.getId()); log.debug("Compliance status primary key " + complianceData.getId());
} }
monitoringDAO.deleteNoneComplianceData(complianceData.getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId());
monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(), monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(),
complianceFeatures); complianceFeatures);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + throw new PolicyComplianceException(
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); "Unable to add the none compliance features to database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -146,8 +148,9 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable to remove the none compliance features from database for device " + throw new PolicyComplianceException(
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); "Unable to remove the none compliance features from database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -159,10 +162,10 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new PolicyComplianceException("Unable tor retrieve device data from DB for " + throw new PolicyComplianceException("Unable tor retrieve device data from DB for " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} catch (PolicyManagerDAOException | PolicyManagementException e) { } catch (PolicyManagerDAOException | PolicyManagementException e) {
throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " + throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} }
return complianceFeatures; return complianceFeatures;
} }
@ -170,21 +173,22 @@ public class MonitoringManagerImpl implements MonitoringManager {
@Override @Override
public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { try {
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); DeviceManagementProviderService service =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
ComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() ComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo()
.getId()); .getId());
if (complianceData == null || !complianceData.isStatus()) { if (complianceData == null || !complianceData.isStatus()) {
return false; return false;
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() +
" - " + deviceIdentifier.getType(), e); " - " + deviceIdentifier.getType(), e);
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() + throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() +
" - " + deviceIdentifier.getType(), e); " - " + deviceIdentifier.getType(), e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -195,12 +199,12 @@ public class MonitoringManagerImpl implements MonitoringManager {
@Override @Override
public ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws public ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws
PolicyComplianceException { PolicyComplianceException {
ComplianceData complianceData; ComplianceData complianceData;
try { try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); DeviceManagementProviderService service =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId());
List<ComplianceFeature> complianceFeatures = List<ComplianceFeature> complianceFeatures =
@ -209,11 +213,11 @@ public class MonitoringManagerImpl implements MonitoringManager {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() +
" - " + deviceIdentifier.getType(), e); " - " + deviceIdentifier.getType(), e);
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() + throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() +
" - " + deviceIdentifier.getType(), e); " - " + deviceIdentifier.getType(), e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -232,6 +236,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
try { try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
//TODO: Return a map from getCompliance to reduce O(n^2) -> O(n)
List<ComplianceData> cd = monitoringDAO.getCompliance(); List<ComplianceData> cd = monitoringDAO.getCompliance();
for (Device device : devices) { for (Device device : devices) {
@ -266,7 +271,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>(); Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>();
Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>(); Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
Map<Integer, Device> inactiveDeviceIds = new HashMap<>(); Map<Integer, Device> inactiveDeviceIds = new HashMap<>();
Map<Integer, Device> deviceToMarkUnreachable = new HashMap<>(); Map<Integer, Device> devicesToMarkUnreachable = new HashMap<>();
//Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); //Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>();
List<PolicyDeviceWrapper> firstTimeDevices = new ArrayList<>(); List<PolicyDeviceWrapper> firstTimeDevices = new ArrayList<>();
@ -281,18 +286,18 @@ public class MonitoringManagerImpl implements MonitoringManager {
if (complianceData.getAttempts() == 0) { if (complianceData.getAttempts() == 0) {
deviceIdsToAddOperation.put(complianceData.getDeviceId(), deviceIdsToAddOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId())); deviceIds.get(complianceData.getDeviceId()));
} else { } else {
deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), deviceIdsWithExistingOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId())); deviceIds.get(complianceData.getDeviceId()));
if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkUnreachable()) { if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkUnreachable()) {
deviceToMarkUnreachable.put(complianceData.getDeviceId(), devicesToMarkUnreachable.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId())); deviceIds.get(complianceData.getDeviceId()));
} }
} }
if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkInactive()) { if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkInactive()) {
inactiveDeviceIds.put(complianceData.getDeviceId(), inactiveDeviceIds.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId())); deviceIds.get(complianceData.getDeviceId()));
} }
} }
} }
@ -316,14 +321,14 @@ public class MonitoringManagerImpl implements MonitoringManager {
log.debug("These devices are in the system for the first time"); log.debug("These devices are in the system for the first time");
for (PolicyDeviceWrapper wrapper : firstTimeDevices) { for (PolicyDeviceWrapper wrapper : firstTimeDevices) {
log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " + log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " +
wrapper.getPolicyId()); wrapper.getPolicyId());
} }
} }
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (!deviceIdsToAddOperation.isEmpty()) { if (!deviceIdsToAddOperation.isEmpty()) {
// monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); // monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds);
monitoringDAO.addComplianceDetails(firstTimeDevices); monitoringDAO.addComplianceDetails(firstTimeDevices);
monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false); monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false);
} }
@ -356,11 +361,17 @@ public class MonitoringManagerImpl implements MonitoringManager {
// TODO : This should be uncommented, this is to mark the device as unreachable, But given the current // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current
// implementation we are not able to do so. // implementation we are not able to do so.
// if(!deviceToMarkUnreachable.isEmpty()) { if (!devicesToMarkUnreachable.isEmpty()) {
// ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices(
// new ArrayList<>(deviceToMarkUnreachable.values()))); new ArrayList<>(devicesToMarkUnreachable.values())));
// } }
if (!inactiveDeviceIds.isEmpty()) {
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
decisionPoint.setDevicesAsInactive(this.getDeviceIdentifiersFromDevices(
new ArrayList<>(inactiveDeviceIds.values())));
}
} }
@ -369,7 +380,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<String> deviceTypes = new ArrayList<>(); List<String> deviceTypes = new ArrayList<>();
try { try {
deviceTypes = PolicyManagementDataHolder.getInstance().getDeviceManagementService().getAvailableDeviceTypes(); deviceTypes =
PolicyManagementDataHolder.getInstance().getDeviceManagementService().getAvailableDeviceTypes();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new PolicyComplianceException("Error occurred while getting the device types.", e); throw new PolicyComplianceException("Error occurred while getting the device types.", e);
} }
@ -384,14 +396,14 @@ public class MonitoringManagerImpl implements MonitoringManager {
monitoringOperation.setEnabled(true); monitoringOperation.setEnabled(true);
monitoringOperation.setType(Operation.Type.COMMAND); monitoringOperation.setType(Operation.Type.COMMAND);
monitoringOperation.setCode(OPERATION_MONITOR); monitoringOperation.setCode(OPERATION_MONITOR);
// CommandOperation infoOperation = new CommandOperation(); // CommandOperation infoOperation = new CommandOperation();
// infoOperation.setEnabled(true); // infoOperation.setEnabled(true);
// infoOperation.setType(Operation.Type.COMMAND);\\ // infoOperation.setType(Operation.Type.COMMAND);\\
// infoOperation.setCode(OPERATION_INFO); // infoOperation.setCode(OPERATION_INFO);
// CommandOperation appListOperation = new CommandOperation(); // CommandOperation appListOperation = new CommandOperation();
// appListOperation.setEnabled(true); // appListOperation.setEnabled(true);
// appListOperation.setType(Operation.Type.COMMAND); // appListOperation.setType(Operation.Type.COMMAND);
// appListOperation.setCode(OPERATION_APP_LIST); // appListOperation.setCode(OPERATION_APP_LIST);
//TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()" //TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()"
String type = null; String type = null;
@ -400,8 +412,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();
service.addOperation(type, monitoringOperation, deviceIdentifiers); service.addOperation(type, monitoringOperation, deviceIdentifiers);
// service.addOperation(infoOperation, deviceIdentifiers); // service.addOperation(infoOperation, deviceIdentifiers);
// service.addOperation(appListOperation, deviceIdentifiers); // service.addOperation(appListOperation, deviceIdentifiers);
} }
private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) { private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) {

@ -1,146 +0,0 @@
/*
* 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.core.service;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService;
import java.util.List;
public class PolicyManagementService implements PolicyManagerService {
PolicyManagerService policyManagerService;
public PolicyManagementService() {
policyManagerService = new PolicyManagerServiceImpl();
}
@Override
public Profile addProfile(Profile profile) throws PolicyManagementException {
return policyManagerService.addProfile(profile);
}
@Override
public Profile updateProfile(Profile profile) throws PolicyManagementException {
return policyManagerService.updateProfile(profile);
}
@Override
public Policy addPolicy(Policy policy) throws PolicyManagementException {
return policyManagerService.addPolicy(policy);
}
@Override
public Policy updatePolicy(Policy policy) throws PolicyManagementException {
return policyManagerService.updatePolicy(policy);
}
@Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException {
return policyManagerService.deletePolicy(policy);
}
@Override
public boolean deletePolicy(int policyId) throws PolicyManagementException {
return policyManagerService.deletePolicy(policyId);
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return policyManagerService.getEffectivePolicy(deviceIdentifier);
}
@Override
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws
FeatureManagementException {
return policyManagerService.getEffectiveFeatures(deviceIdentifier);
}
@Override
public List<Policy> getPolicies(String deviceType) throws PolicyManagementException {
return policyManagerService.getPolicies(deviceType);
}
@Override
public List<Feature> getFeatures() throws FeatureManagementException {
return policyManagerService.getFeatures();
}
@Override
public PolicyAdministratorPoint getPAP() throws PolicyManagementException {
return policyManagerService.getPAP();
}
@Override
public PolicyInformationPoint getPIP() throws PolicyManagementException {
return policyManagerService.getPIP();
}
@Override
public PolicyEvaluationPoint getPEP() throws PolicyManagementException {
return policyManagerService.getPEP();
}
@Override
public TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException {
return policyManagerService.getTaskScheduleService();
}
@Override
public int getPolicyCount() throws PolicyManagementException {
return policyManagerService.getPolicyCount();
}
@Override
public Policy getAppliedPolicyToDevice(
DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return policyManagerService.getAppliedPolicyToDevice(deviceIdentifier);
}
@Override
public List<ComplianceFeature> checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Object
deviceResponse) throws PolicyComplianceException {
return policyManagerService.checkPolicyCompliance(deviceIdentifier, deviceResponse);
}
@Override
public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws
PolicyComplianceException {
return policyManagerService.checkCompliance(deviceIdentifier, response);
}
@Override
public ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
return policyManagerService.getDeviceCompliance(deviceIdentifier);
}
@Override
public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
return policyManagerService.isCompliance(deviceIdentifier);
}
}

@ -97,8 +97,7 @@ public class MonitoringTask implements Task {
} }
for (Device device : devices) { for (Device device : devices) {
EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus();
if (status.equals(EnrolmentInfo.Status.INACTIVE) || if (status.equals(EnrolmentInfo.Status.BLOCKED) ||
status.equals(EnrolmentInfo.Status.BLOCKED) ||
status.equals(EnrolmentInfo.Status.REMOVED) || status.equals(EnrolmentInfo.Status.REMOVED) ||
status.equals(EnrolmentInfo.Status.UNCLAIMED) || status.equals(EnrolmentInfo.Status.UNCLAIMED) ||
status.equals(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED) || status.equals(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED) ||

Loading…
Cancel
Save