Merge branches 'master' and 'master' of https://github.com/wso2/carbon-device-mgt

revert-70aa11f8
Dilshan Edirisuriya 9 years ago
commit b391681438

@ -32,6 +32,7 @@ public class EnrolmentInfo implements Serializable{
BYOD, COPE BYOD, COPE
} }
private int id;
private Device device; private Device device;
private Long dateOfEnrolment; private Long dateOfEnrolment;
private Long dateOfLastUpdate; private Long dateOfLastUpdate;
@ -48,6 +49,14 @@ public class EnrolmentInfo implements Serializable{
this.status = status; this.status = status;
} }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Long getDateOfEnrolment() { public Long getDateOfEnrolment() {
return dateOfEnrolment; return dateOfEnrolment;
} }

@ -66,8 +66,11 @@ public class PermissionManager {
/* Un-marshaling Device Management configuration */ /* Un-marshaling Device Management configuration */
JAXBContext cdmContext = JAXBContext.newInstance(PermissionConfiguration.class); JAXBContext cdmContext = JAXBContext.newInstance(PermissionConfiguration.class);
Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); Unmarshaller unmarshaller = cdmContext.createUnmarshaller();
PermissionConfiguration permissionConfiguration = (PermissionConfiguration) unmarshaller.unmarshal(permissionStream); PermissionConfiguration permissionConfiguration = (PermissionConfiguration)
this.addPermissions(permissionConfiguration.getPermissions()); unmarshaller.unmarshal(permissionStream);
if((permissionConfiguration != null) && (permissionConfiguration.getPermissions() != null)){
this.addPermissions(permissionConfiguration.getPermissions());
}
} }
} catch (JAXBException e) { } catch (JAXBException e) {
throw new DeviceManagementException("Error occurred while initializing Data Source config", e); throw new DeviceManagementException("Error occurred while initializing Data Source config", e);

@ -24,34 +24,64 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "PolicyConfiguration") @XmlRootElement(name = "PolicyConfiguration")
public class PolicyConfiguration { public class PolicyConfiguration {
private String monitoringClass; private String monitoringClass;
private int maxRetries; private boolean monitoringEnable;
private int minRetriesToMarkUnreachable; private int monitoringFrequency;
private int maxRetries;
@XmlElement(name = "monitoringClass", required = true) private int minRetriesToMarkUnreachable;
public String getMonitoringClass() { private int minRetriesToMarkInactive;
return monitoringClass;
} @XmlElement(name = "monitoringClass", required = true)
public String getMonitoringClass() {
public void setMonitoringClass(String monitoringClass) { return monitoringClass;
this.monitoringClass = monitoringClass; }
}
public void setMonitoringClass(String monitoringClass) {
@XmlElement(name = "maxRetries", required = true) this.monitoringClass = monitoringClass;
public int getMaxRetries() { }
return maxRetries;
} @XmlElement(name = "maxRetries", required = true)
public int getMaxRetries() {
public void setMaxRetries(int maxRetries) { return maxRetries;
this.maxRetries = maxRetries; }
}
public void setMaxRetries(int maxRetries) {
@XmlElement(name = "minRetriesToMarkUnreachable", required = true) this.maxRetries = maxRetries;
public int getMinRetriesToMarkUnreachable() { }
return minRetriesToMarkUnreachable;
} @XmlElement(name = "minRetriesToMarkUnreachable", required = true)
public int getMinRetriesToMarkUnreachable() {
public void setMinRetriesToMarkUnreachable(int minRetriesToMarkUnreachable) { return minRetriesToMarkUnreachable;
this.minRetriesToMarkUnreachable = minRetriesToMarkUnreachable; }
}
public void setMinRetriesToMarkUnreachable(int minRetriesToMarkUnreachable) {
this.minRetriesToMarkUnreachable = minRetriesToMarkUnreachable;
}
@XmlElement(name = "monitoringEnable", required = true)
public boolean getMonitoringEnable() {
return monitoringEnable;
}
public void setMonitoringEnable(boolean monitoringEnable) {
this.monitoringEnable = monitoringEnable;
}
@XmlElement(name = "minRetriesToMarkInactive", required = true)
public int getMinRetriesToMarkInactive() {
return minRetriesToMarkInactive;
}
public void setMinRetriesToMarkInactive(int minRetriesToMarkInactive) {
this.minRetriesToMarkInactive = minRetriesToMarkInactive;
}
@XmlElement(name = "monitoringFrequency", required = true)
public int getMonitoringFrequency() {
return monitoringFrequency;
}
public void setMonitoringFrequency(int monitoringFrequency) {
this.monitoringFrequency = monitoringFrequency;
}
} }

@ -113,7 +113,7 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " +
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
"t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
@ -210,7 +210,7 @@ public class DeviceDAOImpl implements DeviceDAO {
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + "e.DATE_OF_ENROLMENT , e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " +
"d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?";
@ -475,6 +475,7 @@ public class DeviceDAOImpl implements DeviceDAO {
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
enrolmentInfo.setOwner(rs.getString("OWNER")); enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
@ -493,7 +494,7 @@ public class DeviceDAOImpl implements DeviceDAO {
String sql = String sql =
"SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " +
"AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " +
"AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";

@ -55,8 +55,6 @@
<Bundle-Description>Policy Management Core Bundle</Bundle-Description> <Bundle-Description>Policy Management Core Bundle</Bundle-Description>
<Private-Package>org.wso2.carbon.policy.mgt.core.internal</Private-Package> <Private-Package>org.wso2.carbon.policy.mgt.core.internal</Private-Package>
<Import-Package> <Import-Package>
org.apache.axis2.*;
version="${axis2.osgi.version.range}",
org.osgi.framework, org.osgi.framework,
org.osgi.service.component, org.osgi.service.component,
org.apache.commons.logging, org.apache.commons.logging,

@ -19,7 +19,8 @@
package org.wso2.carbon.policy.mgt.core.cache; package org.wso2.carbon.policy.mgt.core.cache;
import org.wso2.carbon.device.mgt.core.policy.mgt.policy.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import java.util.List; import java.util.List;
@ -29,7 +30,9 @@ public interface PolicyCacheManager {
void updateAllPolicies(List<Policy> policies); void updateAllPolicies(List<Policy> policies);
List<Policy> getAllPolicies(); List<Policy> getAllPolicies() throws PolicyManagementException;
void rePopulateCache() throws PolicyManagementException;
void removeAllPolicies(); void removeAllPolicies();
@ -37,9 +40,11 @@ public interface PolicyCacheManager {
void updatePolicy(Policy policy); void updatePolicy(Policy policy);
void updatePolicy(int policyId) throws PolicyManagementException;
void removePolicy(int policyId); void removePolicy(int policyId);
Policy getPolicy(int policyId); Policy getPolicy(int policyId) throws PolicyManagementException;
void addPolicyToDevice(int deviceId, int policyId); void addPolicyToDevice(int deviceId, int policyId);

@ -19,51 +19,145 @@
package org.wso2.carbon.policy.mgt.core.cache.impl; package org.wso2.carbon.policy.mgt.core.cache.impl;
import org.wso2.carbon.device.mgt.core.policy.mgt.policy.Policy; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set;
public class PolicyCacheManagerImpl implements PolicyCacheManager { public class PolicyCacheManagerImpl implements PolicyCacheManager {
private static final Log log = LogFactory.getLog(PolicyCacheManagerImpl.class);
private static HashMap<Integer, HashMap<Integer, Policy>> tenantedPolicyMap = new HashMap<>();
private static PolicyCacheManagerImpl policyCacheManager;
private PolicyCacheManagerImpl() {
}
public static PolicyCacheManager getInstance() {
if (policyCacheManager == null) {
synchronized (PolicyCacheManagerImpl.class) {
if (policyCacheManager == null) {
policyCacheManager = new PolicyCacheManagerImpl();
}
}
}
return policyCacheManager;
}
@Override @Override
public void addAllPolicies(List<Policy> policies) { public void addAllPolicies(List<Policy> policies) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.isEmpty()) {
for (Policy policy : policies) {
map.put(policy.getId(), policy);
}
}
} }
@Override @Override
public void updateAllPolicies(List<Policy> policies) { public void updateAllPolicies(List<Policy> policies) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
map.clear();
if (map.isEmpty()) {
for (Policy policy : policies) {
map.put(policy.getId(), policy);
}
}
}
@Override
public List<Policy> getAllPolicies() throws PolicyManagementException {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.isEmpty()) {
PolicyManager policyManager = new PolicyManagerImpl();
this.addAllPolicies(policyManager.getPolicies());
}
if (log.isDebugEnabled()) {
log.debug("No of policies stored in the cache .. : " + map.size());
Set<Integer> keySet = map.keySet();
for (Integer x : keySet) {
log.debug("Policy id in maps .. : " + map.get(x).getId() + " policy name : " + map.get(x).
getPolicyName() + " Activated : " + map.get(x).isActive());
}
}
return new ArrayList<>(map.values());
} }
@Override @Override
public List<Policy> getAllPolicies() { public void rePopulateCache() throws PolicyManagementException {
return null;
this.removeAllPolicies();
this.getAllPolicies();
} }
@Override @Override
public void removeAllPolicies() { public void removeAllPolicies() {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
map.clear();
} }
@Override @Override
public void addPolicy(Policy policy) { public void addPolicy(Policy policy) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (!map.containsKey(policy.getId())) {
map.put(policy.getId(), policy);
} else {
log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store.");
}
} }
@Override @Override
public void updatePolicy(Policy policy) { public void updatePolicy(Policy policy) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.containsKey(policy.getId())) {
map.remove(policy.getId());
map.put(policy.getId(), policy);
}
}
@Override
public void updatePolicy(int policyId) throws PolicyManagementException {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.containsKey(policyId)) {
this.removePolicy(policyId);
}
PolicyManager policyManager = new PolicyManagerImpl();
Policy policy = policyManager.getPolicy(policyId);
map.put(policyId, policy);
} }
@Override @Override
public void removePolicy(int policyId) { public void removePolicy(int policyId) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.containsKey(policyId)) {
map.remove(policyId);
} else {
log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed.");
}
} }
@Override @Override
public Policy getPolicy(int policyId) { public Policy getPolicy(int policyId) throws PolicyManagementException {
return null; HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (!map.containsKey(policyId)) {
this.removeAllPolicies();
this.getAllPolicies();
}
return map.get(policyId);
} }
@Override @Override
@ -80,4 +174,14 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager {
public int getPolicyIdOfDevice(int deviceId) { public int getPolicyIdOfDevice(int deviceId) {
return 0; return 0;
} }
private HashMap<Integer, Policy> getTenantRelatedMap(){
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if(!tenantedPolicyMap.containsKey(tenantId)){
HashMap<Integer, Policy> policyMap = new HashMap<>();
tenantedPolicyMap.put(tenantId, policyMap);
}
return tenantedPolicyMap.get(tenantId);
}
} }

@ -106,23 +106,23 @@ public interface PolicyDAO {
List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException;
void addEffectivePolicyToDevice(int deviceId, Policy policy) void addEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; void setPolicyApplied(int deviceId, int enrollmentId) throws PolicyManagerDAOException;
void updateEffectivePolicyToDevice(int deviceId, Policy policy) void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException;
int getPolicyCount() throws PolicyManagerDAOException; int getPolicyCount() throws PolicyManagerDAOException;
int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException; int getAppliedPolicyId(int deviceId, int enrollmentId) throws PolicyManagerDAOException;
Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException; Policy getAppliedPolicy(int deviceId, int enrollmentId) throws PolicyManagerDAOException;
HashMap<Integer, Integer> getAppliedPolicyIds(List<Integer> deviceIds) throws PolicyManagerDAOException; HashMap<Integer, Integer> getAppliedPolicyIds(List<Integer> deviceIds) throws PolicyManagerDAOException;
HashMap<Integer, Integer> getAppliedPolicyIdsDeviceIds() throws PolicyManagerDAOException; HashMap<Integer, Integer> getAppliedPolicyIdsDeviceIds() throws PolicyManagerDAOException;
} }

@ -119,17 +119,22 @@ public class PolicyDAOImpl implements PolicyDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)"; String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID, ENROLMENT_ID, DEVICE) VALUES (?, ?, " +
"?, ?)";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
for (Device device : devices) { for (Device device : devices) {
stmt.setInt(1, device.getId()); stmt.setInt(1, device.getId());
stmt.setInt(2, policy.getId()); stmt.setInt(2, policy.getId());
stmt.setInt(3, device.getEnrolmentInfo().getId());
stmt.setBytes(4, PolicyManagerUtil.getBytes(device));
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while adding the device ids with policy to " + throw new PolicyManagerDAOException("Error occurred while adding the device ids with policy to " +
"database", e); "database", e);
} catch (IOException e) {
throw new PolicyManagerDAOException("Error occurred while getting the byte array from device.", e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -881,7 +886,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { public void addEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws
PolicyManagerDAOException {
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());
@ -889,7 +895,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " + String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " +
"CREATED_TIME, UPDATED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)"; "CREATED_TIME, UPDATED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, policy.getId()); stmt.setInt(2, policy.getId());
@ -897,6 +903,7 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp); stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId); stmt.setInt(6, tenantId);
stmt.setInt(7, enrolmentId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException | IOException e) { } catch (SQLException | IOException e) {
throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e); throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e);
@ -907,7 +914,7 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public void setPolicyApplied(int deviceId) throws PolicyManagerDAOException { public void setPolicyApplied(int deviceId, int enrollmentId) throws PolicyManagerDAOException {
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());
@ -915,12 +922,13 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? AND" + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? AND" +
" TENANT_ID = ?"; " TENANT_ID = ? AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setTimestamp(1, currentTimestamp); stmt.setTimestamp(1, currentTimestamp);
stmt.setBoolean(2, true); stmt.setBoolean(2, true);
stmt.setInt(3, deviceId); stmt.setInt(3, deviceId);
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
stmt.setInt(5, enrollmentId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while updating applied policy to device (" + throw new PolicyManagerDAOException("Error occurred while updating applied policy to device (" +
@ -932,7 +940,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { public void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws
PolicyManagerDAOException {
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());
@ -940,7 +949,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " +
"APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ?"; "APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, policy.getId()); stmt.setInt(1, policy.getId());
stmt.setBytes(2, PolicyManagerUtil.getBytes(policy)); stmt.setBytes(2, PolicyManagerUtil.getBytes(policy));
@ -948,6 +957,7 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setBoolean(4, false); stmt.setBoolean(4, false);
stmt.setInt(5, deviceId); stmt.setInt(5, deviceId);
stmt.setInt(6, tenantId); stmt.setInt(6, tenantId);
stmt.setInt(7, enrolmentId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException | IOException e) { } catch (SQLException | IOException e) {
@ -959,7 +969,7 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException { public boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -967,10 +977,12 @@ public class PolicyDAOImpl implements PolicyDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? AND " +
"ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, enrollmentId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
exist = resultSet.next(); exist = resultSet.next();
} catch (SQLException e) { } catch (SQLException e) {
@ -1282,17 +1294,18 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException { public int getAppliedPolicyId(int deviceId, int enrollmentId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, enrollmentId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
@ -1307,7 +1320,7 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException { public Policy getAppliedPolicy(int deviceId, int enrollmentId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1315,10 +1328,12 @@ public class PolicyDAOImpl implements PolicyDAO {
Policy policy = null; Policy policy = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? AND " +
"ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, enrollmentId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {

@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.ntask.core.Task;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
@ -57,6 +58,8 @@ public class DelegationTask implements Task {
PolicyManager policyManager = new PolicyManagerImpl(); PolicyManager policyManager = new PolicyManagerImpl();
List<DeviceType> deviceTypes = policyManager.applyChangesMadeToPolicies(); List<DeviceType> deviceTypes = policyManager.applyChangesMadeToPolicies();
PolicyCacheManagerImpl.getInstance().rePopulateCache();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size()); log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size());
} }

@ -27,6 +27,8 @@ import org.wso2.carbon.ntask.core.TaskInfo;
import org.wso2.carbon.ntask.core.TaskManager; import org.wso2.carbon.ntask.core.TaskManager;
import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.ntask.core.service.TaskService;
import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager;
import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
@ -47,12 +49,14 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
private PolicyManager policyManager; private PolicyManager policyManager;
private ProfileManager profileManager; private ProfileManager profileManager;
private FeatureManager featureManager; private FeatureManager featureManager;
private PolicyCacheManager cacheManager;
// private PolicyEnforcementDelegator delegator; // private PolicyEnforcementDelegator delegator;
public PolicyAdministratorPointImpl() { public PolicyAdministratorPointImpl() {
this.policyManager = new PolicyManagerImpl(); this.policyManager = new PolicyManagerImpl();
this.profileManager = new ProfileManagerImpl(); this.profileManager = new ProfileManagerImpl();
this.featureManager = new FeatureManagerImpl(); this.featureManager = new FeatureManagerImpl();
this.cacheManager = PolicyCacheManagerImpl.getInstance();
// this.delegator = new PolicyEnforcementDelegatorImpl(); // this.delegator = new PolicyEnforcementDelegatorImpl();
} }
@ -64,6 +68,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
// } catch (PolicyDelegationException e) { // } catch (PolicyDelegationException e) {
// throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); // throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e);
// } // }
PolicyCacheManagerImpl.getInstance().addPolicy(resultantPolicy);
return resultantPolicy; return resultantPolicy;
} }
@ -75,32 +80,41 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
// } catch (PolicyDelegationException e) { // } catch (PolicyDelegationException e) {
// throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); // throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e);
// } // }
PolicyCacheManagerImpl.getInstance().updatePolicy(resultantPolicy);
return resultantPolicy; return resultantPolicy;
} }
@Override @Override
public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagementException { public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagementException {
return policyManager.updatePolicyPriorities(policies); boolean bool = policyManager.updatePolicyPriorities(policies);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
return bool;
} }
@Override @Override
public void activatePolicy(int policyId) throws PolicyManagementException { public void activatePolicy(int policyId) throws PolicyManagementException {
policyManager.activatePolicy(policyId); policyManager.activatePolicy(policyId);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
} }
@Override @Override
public void inactivatePolicy(int policyId) throws PolicyManagementException { public void inactivatePolicy(int policyId) throws PolicyManagementException {
policyManager.inactivatePolicy(policyId); policyManager.inactivatePolicy(policyId);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
} }
@Override @Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException { public boolean deletePolicy(Policy policy) throws PolicyManagementException {
return policyManager.deletePolicy(policy); boolean bool = policyManager.deletePolicy(policy);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
return bool;
} }
@Override @Override
public boolean deletePolicy(int policyId) throws PolicyManagementException { public boolean deletePolicy(int policyId) throws PolicyManagementException {
return policyManager.deletePolicy(policyId); boolean bool =policyManager.deletePolicy(policyId);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
return bool;
} }
@Override @Override
@ -127,15 +141,23 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId);
TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); if (!taskManager.isTaskScheduled(taskName)) {
taskManager.registerTask(taskInfo); TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ,
taskManager.rescheduleTask(taskInfo.getName()); properties, triggerInfo);
taskManager.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName());
} else {
throw new PolicyManagementException("There is a task already running for policy changes. Please try " +
"to apply " +
"changes after few minutes.");
}
} catch (TaskException e) { } catch (TaskException e) {
String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext. String msg = "Error occurred while creating the policy delegation task for tenant " +
getThreadLocalCarbonContext().getTenantId(); PrivilegedCarbonContext.
getThreadLocalCarbonContext().getTenantId();
log.error(msg, e); log.error(msg, e);
throw new PolicyManagementException(msg, e); throw new PolicyManagementException(msg, e);
} }
@ -180,22 +202,26 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws
PolicyManagementException { PolicyManagementException {
return policyManager.addPolicyToDevice(deviceIdentifierList, policy); policy = policyManager.addPolicyToDevice(deviceIdentifierList, policy);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
return policy;
} }
@Override @Override
public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException { public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException {
return policyManager.addPolicyToRole(roleNames, policy); policy = policyManager.addPolicyToRole(roleNames, policy);
PolicyCacheManagerImpl.getInstance().rePopulateCache();
return policy;
} }
@Override @Override
public List<Policy> getPolicies() throws PolicyManagementException { public List<Policy> getPolicies() throws PolicyManagementException {
return policyManager.getPolicies(); return PolicyCacheManagerImpl.getInstance().getAllPolicies();
} }
@Override @Override
public Policy getPolicy(int policyId) throws PolicyManagementException { public Policy getPolicy(int policyId) throws PolicyManagementException {
return policyManager.getPolicy(policyId); return PolicyCacheManagerImpl.getInstance().getPolicy(policyId);
} }
@Override @Override
@ -306,7 +332,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public int getPolicyCount() throws PolicyManagementException { public int getPolicyCount() throws PolicyManagementException {
return policyManager.getPolicyCount(); return PolicyCacheManagerImpl.getInstance().getAllPolicies().size();
} }
} }

@ -21,6 +21,8 @@ package org.wso2.carbon.policy.mgt.core.internal;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext; import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.ntask.core.service.TaskService;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
@ -84,8 +86,12 @@ public class PolicyManagementServiceComponent {
componentContext.getBundleContext().registerService( componentContext.getBundleContext().registerService(
PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null);
TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl(); PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
taskScheduleService.startTask(30000); getDeviceManagementConfigRepository().getPolicyConfiguration();
if(policyConfiguration.getMonitoringEnable()) {
TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl();
taskScheduleService.startTask(policyConfiguration.getMonitoringFrequency());
}
} catch (Throwable t) { } catch (Throwable t) {
log.error("Error occurred while initializing the Policy management core.", t); log.error("Error occurred while initializing the Policy management core.", t);
@ -165,7 +171,7 @@ public class PolicyManagementServiceComponent {
protected void setPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { protected void setPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Setting Policy Monitoring Service"); log.debug("Setting Policy Monitoring Service for " + policyMonitoringService.getType());
} }
// TODO: FIX THE device type by taking from properties // TODO: FIX THE device type by taking from properties
PolicyManagementDataHolder.getInstance().setPolicyMonitoringService(policyMonitoringService.getType(), PolicyManagementDataHolder.getInstance().setPolicyMonitoringService(policyMonitoringService.getType(),
@ -174,7 +180,7 @@ public class PolicyManagementServiceComponent {
protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Removing the Policy Monitoring Service"); log.debug("Removing the Policy Monitoring Service for " + policyMonitoringService.getType());
} }
// TODO: FIX THE device type by taking from properties // TODO: FIX THE device type by taking from properties
PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(policyMonitoringService.getType()); PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(policyMonitoringService.getType());

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.mgt;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; 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.ComplianceFeature;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
@ -39,4 +40,6 @@ public interface MonitoringManager {
void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException; void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException;
List<DeviceType> getDeviceTypes() throws PolicyComplianceException;
} }

@ -26,8 +26,13 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfigRepository;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
@ -54,18 +59,24 @@ import java.util.Map;
public class MonitoringManagerImpl implements MonitoringManager { public class MonitoringManagerImpl implements MonitoringManager {
private PolicyDAO policyDAO; private PolicyDAO policyDAO;
private DeviceDAO deviceDAO; // private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
private MonitoringDAO monitoringDAO; private MonitoringDAO monitoringDAO;
private ComplianceDecisionPoint complianceDecisionPoint; private ComplianceDecisionPoint complianceDecisionPoint;
private PolicyConfiguration policyConfiguration;
private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class);
private static final String OPERATION_MONITOR = "MONITOR"; private static final String OPERATION_MONITOR = "MONITOR";
public MonitoringManagerImpl() { public MonitoringManagerImpl() {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); // this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO(); this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO();
this.complianceDecisionPoint = new ComplianceDecisionPointImpl(); this.complianceDecisionPoint = new ComplianceDecisionPointImpl();
this.policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
getDeviceManagementConfigRepository().getPolicyConfiguration();
} }
@Override @Override
@ -237,11 +248,14 @@ public class MonitoringManagerImpl implements MonitoringManager {
throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyComplianceException("SQL error occurred while getting policy details.", e); throw new PolicyComplianceException("SQL error occurred while getting policy details.", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
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, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>();
Map<Integer, ComplianceData> tempMap = new HashMap<>(); Map<Integer, ComplianceData> tempMap = new HashMap<>();
@ -258,8 +272,12 @@ public class MonitoringManagerImpl implements MonitoringManager {
} else { } else {
deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), deviceIdsWithExistingOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId())); deviceIds.get(complianceData.getDeviceId()));
if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkUnreachable()) {
deviceToMarkUnreachable.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
}
} }
if (complianceData.getAttempts() >= 20) { if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkInactive()) {
inactiveDeviceIds.put(complianceData.getDeviceId(), inactiveDeviceIds.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId())); deviceIds.get(complianceData.getDeviceId()));
} }
@ -288,9 +306,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
if (!deviceIdsWithExistingOperation.isEmpty()) { if (!deviceIdsWithExistingOperation.isEmpty()) {
monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false);
//TODO: Add attempts. This has to be fixed in the get pending operation tables too. This will be
// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices(
// new ArrayList<>(deviceIdsWithExistingOperation.values())));
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
@ -312,6 +327,29 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
} }
// 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.
// if(!deviceToMarkUnreachable.isEmpty()) {
// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices(
// new ArrayList<>(deviceToMarkUnreachable.values())));
// }
}
@Override
public List<DeviceType> getDeviceTypes() throws PolicyComplianceException {
List<DeviceType> deviceTypes = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes();
} catch (Exception e) {
log.error("Error occurred while getting the device types.", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return deviceTypes;
} }
private void addMonitoringOperationsToDatabase(List<Device> devices) private void addMonitoringOperationsToDatabase(List<Device> devices)
@ -325,8 +363,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
service.addOperation(monitoringOperation, deviceIdentifiers); service.addOperation(monitoringOperation, deviceIdentifiers);
// PolicyManagementDataHolder.getInstance().getDeviceManagementService().
// addOperation(monitoringOperation, deviceIdentifiers);
} }
private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) { private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) {

@ -29,6 +29,8 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager;
import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
@ -44,6 +46,7 @@ public class PolicyManagerImpl implements PolicyManager {
private ProfileDAO profileDAO; private ProfileDAO profileDAO;
private FeatureDAO featureDAO; private FeatureDAO featureDAO;
private ProfileManager profileManager; private ProfileManager profileManager;
private PolicyCacheManager policyCacheManager;
private static Log log = LogFactory.getLog(PolicyManagerImpl.class); private static Log log = LogFactory.getLog(PolicyManagerImpl.class);
public PolicyManagerImpl() { public PolicyManagerImpl() {
@ -188,8 +191,20 @@ public class PolicyManagerImpl implements PolicyManager {
public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagementException { public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagementException {
boolean bool; boolean bool;
try { try {
// List<Policy> existingPolicies = this.getPolicies();
List<Policy> existingPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
bool = policyDAO.updatePolicyPriorities(policies); bool = policyDAO.updatePolicyPriorities(policies);
// This logic is added because ui sends only policy id and priority to update priorities.
for (Policy policy : policies) {
for (Policy exPolicy : existingPolicies) {
if (policy.getId() == exPolicy.getId()) {
policy.setProfile(exPolicy.getProfile());
}
}
}
policyDAO.recordUpdatedPolicies(policies); policyDAO.recordUpdatedPolicies(policies);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -542,7 +557,8 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
List<Policy> tempPolicyList = this.getPolicies(); // List<Policy> tempPolicyList = this.getPolicies();
List<Policy> tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
for (Policy policy : tempPolicyList) { for (Policy policy : tempPolicyList) {
for (Integer i : policyIdList) { for (Integer i : policyIdList) {
@ -559,22 +575,29 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public List<Policy> getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException { public List<Policy> getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<>(); List<Policy> policies = new ArrayList<>();
try { // try {
List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); // List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName);
List<Policy> allPolicies = this.getPolicies(); // List<Policy> allPolicies = this.getPolicies();
List<Policy> allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
for (Profile profile : profileList) {
for (Policy policy : allPolicies) { for (Policy policy : allPolicies) {
if (policy.getProfileId() == profile.getProfileId()) { if (policy.getProfile().getDeviceType().getName().equalsIgnoreCase(deviceTypeName)) {
policy.setProfile(profile); policies.add(policy);
policies.add(policy);
}
}
} }
Collections.sort(policies);
} catch (ProfileManagementException e) {
throw new PolicyManagementException("Error occurred while getting all the profile features.", e);
} }
// for (Profile profile : profileList) {
// for (Policy policy : allPolicies) {
// if (policy.getProfileId() == profile.getProfileId()) {
// policy.setProfile(profile);
// policies.add(policy);
// }
// }
// }
Collections.sort(policies);
// } catch (ProfileManagementException e) {
// throw new PolicyManagementException("Error occurred while getting all the profile features.", e);
// }
return policies; return policies;
} }
@ -596,7 +619,8 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
List<Policy> tempPolicyList = this.getPolicies(); // List<Policy> tempPolicyList = this.getPolicies();
List<Policy> tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
for (Policy policy : tempPolicyList) { for (Policy policy : tempPolicyList) {
for (Integer i : policyIdList) { for (Integer i : policyIdList) {
@ -625,7 +649,8 @@ public class PolicyManagerImpl implements PolicyManager {
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
List<Policy> tempPolicyList = this.getPolicies(); // List<Policy> tempPolicyList = this.getPolicies();
List<Policy> tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
for (Policy policy : tempPolicyList) { for (Policy policy : tempPolicyList) {
for (Integer i : policyIdList) { for (Integer i : policyIdList) {
@ -654,14 +679,14 @@ public class PolicyManagerImpl implements PolicyManager {
HashMap<Integer, Device> allDeviceMap = new HashMap<>(); HashMap<Integer, Device> allDeviceMap = new HashMap<>();
if(!allDevices.isEmpty()) { if (!allDevices.isEmpty()) {
allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices); allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices);
} }
for (int deviceId : deviceIds) { for (int deviceId : deviceIds) {
if(allDeviceMap.containsKey(deviceId)){ if (allDeviceMap.containsKey(deviceId)) {
if(log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId); log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId);
} }
deviceList.add(allDeviceMap.get(deviceId)); deviceList.add(allDeviceMap.get(deviceId));
@ -695,11 +720,11 @@ public class PolicyManagerImpl implements PolicyManager {
deviceId = device.getId(); deviceId = device.getId();
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
boolean exist = policyDAO.checkPolicyAvailable(deviceId); boolean exist = policyDAO.checkPolicyAvailable(deviceId, device.getEnrolmentInfo().getId());
if (exist) { if (exist) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policy); policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy);
} else { } else {
policyDAO.addEffectivePolicyToDevice(deviceId, policy); policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy);
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -725,7 +750,8 @@ public class PolicyManagerImpl implements PolicyManager {
// List<Policy> inactivePolicies = new ArrayList<>(); // List<Policy> inactivePolicies = new ArrayList<>();
List<Integer> updatedPolicyIds = new ArrayList<>(); List<Integer> updatedPolicyIds = new ArrayList<>();
List<Policy> allPolicies = this.getPolicies(); // List<Policy> allPolicies = this.getPolicies();
List<Policy> allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
for (Policy policy : allPolicies) { for (Policy policy : allPolicies) {
if (policy.isUpdated()) { if (policy.isUpdated()) {
@ -766,13 +792,13 @@ public class PolicyManagerImpl implements PolicyManager {
deviceId = device.getId(); deviceId = device.getId();
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
Policy policySaved = policyDAO.getAppliedPolicy(deviceId); Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId());
if (policySaved != null && policySaved.getId() != 0) { if (policySaved != null && policySaved.getId() != 0) {
if (policy.getId() != policySaved.getId()) { if (policy.getId() != policySaved.getId()) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policy); policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy);
} }
} else { } else {
policyDAO.addEffectivePolicyToDevice(deviceId, policy); policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy);
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -796,7 +822,7 @@ public class PolicyManagerImpl implements PolicyManager {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
exist = policyDAO.checkPolicyAvailable(device.getId()); exist = policyDAO.checkPolicyAvailable(device.getId(), device.getEnrolmentInfo().getId());
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while checking whether device has a policy " + throw new PolicyManagementException("Error occurred while checking whether device has a policy " +
"to apply.", e); "to apply.", e);
@ -818,7 +844,7 @@ public class PolicyManagerImpl implements PolicyManager {
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policyDAO.setPolicyApplied(device.getId()); policyDAO.setPolicyApplied(device.getId(), device.getEnrolmentInfo().getId());
return true; return true;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" + throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" +
@ -858,7 +884,7 @@ public class PolicyManagerImpl implements PolicyManager {
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
//int policyId = policyDAO.getAppliedPolicyId(device.getId()); //int policyId = policyDAO.getAppliedPolicyId(device.getId());
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getAppliedPolicy(device.getId()); policy = policyDAO.getAppliedPolicy(device.getId(), device.getEnrolmentInfo().getId());
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting device id.", e); throw new PolicyManagementException("Error occurred while getting device id.", e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {

@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.ntask.core.Task;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
@ -63,24 +64,29 @@ public class MonitoringTask implements Task {
log.debug("Monitoring task started to run."); log.debug("Monitoring task started to run.");
} }
MonitoringManager monitoringManager = new MonitoringManagerImpl();
List<DeviceType> deviceTypes = new ArrayList<>(); List<DeviceType> deviceTypes = new ArrayList<>();
try { try {
DeviceManagementDAOFactory.openConnection(); deviceTypes = monitoringManager.getDeviceTypes();
deviceTypes = deviceTypeDAO.getDeviceTypes(); } catch (PolicyComplianceException e) {
} catch (Exception e) { log.error("Error occurred while getting the device types.");
log.error("Error occurred while getting the device types.", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
} }
if (!deviceTypes.isEmpty()) { if (!deviceTypes.isEmpty()) {
try { try {
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManagementDataHolder.getInstance().getDeviceManagementService();
MonitoringManager monitoringManager = new MonitoringManagerImpl();
for (DeviceType deviceType : deviceTypes) { for (DeviceType deviceType : deviceTypes) {
if(log.isDebugEnabled()){
log.debug("Running task for device type : " + deviceType.getName() );
}
PolicyMonitoringService monitoringService = PolicyMonitoringService monitoringService =
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName());
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName());
@ -91,7 +97,7 @@ public class MonitoringTask implements Task {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Removing inactive and blocked devices from the list for the device type : " + log.debug("Removing inactive and blocked devices from the list for the device type : " +
deviceType); deviceType.getName());
} }
for (Device device : devices) { for (Device device : devices) {
if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.INACTIVE) || if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.INACTIVE) ||
@ -102,7 +108,7 @@ public class MonitoringTask implements Task {
} }
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Following devices selected to send the notification for " + deviceType); log.debug("Following devices selected to send the notification for " + deviceType.getName());
for (Device device : notifiableDevices) { for (Device device : notifiableDevices) {
log.debug(device.getDeviceIdentifier()); log.debug(device.getDeviceIdentifier());
} }

@ -22,6 +22,8 @@ package org.wso2.carbon.policy.mgt.core.task;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.ntask.common.TaskException; import org.wso2.carbon.ntask.common.TaskException;
import org.wso2.carbon.ntask.core.TaskInfo; import org.wso2.carbon.ntask.core.TaskInfo;
import org.wso2.carbon.ntask.core.TaskManager; import org.wso2.carbon.ntask.core.TaskManager;
@ -37,47 +39,64 @@ import java.util.Map;
public class TaskScheduleServiceImpl implements TaskScheduleService { public class TaskScheduleServiceImpl implements TaskScheduleService {
private static Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); private static Log log = LogFactory.getLog(TaskScheduleServiceImpl.class);
private PolicyConfiguration policyConfiguration;
public TaskScheduleServiceImpl() {
this.policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
getDeviceManagementConfigRepository().getPolicyConfiguration();
}
@Override @Override
public void startTask(int monitoringFrequency) throws PolicyMonitoringTaskException { public void startTask(int monitoringFrequency) throws PolicyMonitoringTaskException {
if (monitoringFrequency <= 0) {
throw new PolicyMonitoringTaskException("Time interval cannot be 0 or less than 0.");
}
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE);
if (log.isDebugEnabled()) { if (policyConfiguration.getMonitoringEnable()) {
log.debug("Monitoring task is started for the tenant id " + tenantId);
if (monitoringFrequency <= 0) {
throw new PolicyMonitoringTaskException("Time interval cannot be 0 or less than 0.");
} }
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE);
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); if (log.isDebugEnabled()) {
log.debug("Monitoring task is started for the tenant id " + tenantId);
}
TriggerInfo triggerInfo = new TriggerInfo(); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
triggerInfo.setIntervalMillis(monitoringFrequency); TriggerInfo triggerInfo = new TriggerInfo();
triggerInfo.setRepeatCount(-1); triggerInfo.setIntervalMillis(monitoringFrequency);
triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId);
TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, triggerInfo); if (!taskManager.isTaskScheduled(taskName)) {
taskManager.registerTask(taskInfo); TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ,
taskManager.rescheduleTask(taskInfo.getName()); properties, triggerInfo);
taskManager.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName());
} else {
throw new PolicyMonitoringTaskException("Monitoring task is already started for this tenant " +
tenantId);
}
} catch (TaskException e) {
String msg = "Error occurred while creating the task for tenant " + PrivilegedCarbonContext.
getThreadLocalCarbonContext().getTenantId();
log.error(msg, e);
throw new PolicyMonitoringTaskException(msg, e);
}
} catch (TaskException e) {
throw new PolicyMonitoringTaskException("Error occurred while creating the task for tenant " +
PrivilegedCarbonContext.
getThreadLocalCarbonContext().getTenantId(), e);
}
} else {
throw new PolicyMonitoringTaskException("Policy monitoring is not enabled in the cdm-config.xml.");
}
} }
@ -90,10 +109,9 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
taskManager.deleteTask(taskName); taskManager.deleteTask(taskName);
} catch (TaskException e) { } catch (TaskException e) {
String msg = "Error occurred while deleting the task for tenant " + PrivilegedCarbonContext. throw new PolicyMonitoringTaskException("Error occurred while deleting the task for tenant " +
getThreadLocalCarbonContext().getTenantId(); PrivilegedCarbonContext.
log.error(msg, e); getThreadLocalCarbonContext().getTenantId(), e);
throw new PolicyMonitoringTaskException(msg, e);
} }
} }
@ -103,29 +121,32 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId);
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
taskManager.deleteTask(taskName); if (taskManager.isTaskScheduled(taskName)) {
TriggerInfo triggerInfo = new TriggerInfo();
triggerInfo.setIntervalMillis(monitoringFrequency); taskManager.deleteTask(taskName);
triggerInfo.setRepeatCount(-1); TriggerInfo triggerInfo = new TriggerInfo();
triggerInfo.setIntervalMillis(monitoringFrequency);
triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put("tenantId", String.valueOf(tenantId)); properties.put("tenantId", String.valueOf(tenantId));
TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, triggerInfo); TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties,
triggerInfo);
taskManager.registerTask(taskInfo); taskManager.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName()); taskManager.rescheduleTask(taskInfo.getName());
} else {
throw new PolicyMonitoringTaskException("Monitoring task has not been started for this tenant " +
tenantId + ". Please start the task first.");
}
} catch (TaskException e) { } catch (TaskException e) {
String msg = "Error occurred while updating the task for tenant " + PrivilegedCarbonContext. throw new PolicyMonitoringTaskException("Error occurred while updating the task for tenant " +
getThreadLocalCarbonContext().getTenantId(); PrivilegedCarbonContext.
log.error(msg, e); getThreadLocalCarbonContext().getTenantId(), e);
throw new PolicyMonitoringTaskException(msg, e);
} }
} }

@ -26,6 +26,9 @@ import org.testng.annotations.BeforeSuite;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -61,6 +64,7 @@ public abstract class BasePolicyManagementDAOTest {
this.initSQLScript(); this.initSQLScript();
this.initialize(); this.initialize();
this.initiatePrivilegedCaronContext(); this.initiatePrivilegedCaronContext();
DeviceConfigurationManager.getInstance().initConfig();
} }
public void initialize() throws TaskException { public void initialize() throws TaskException {

@ -32,12 +32,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceIm
import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl;
import org.wso2.carbon.policy.mgt.core.util.*; import org.wso2.carbon.policy.mgt.core.util.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -87,7 +81,6 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
} }
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
PolicyManagementDataHolder.getInstance().setDeviceManagementService(service); PolicyManagementDataHolder.getInstance().setDeviceManagementService(service);
@ -119,38 +112,39 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
@Test(dependsOnMethods = ("addFeatures")) @Test(dependsOnMethods = ("addFeatures"))
public void addProfileFeatures() throws ProfileManagementException { public void addProfileFeatures() throws ProfileManagementException {
ProfileManager profileManager = new ProfileManagerImpl(); // ProfileManager profileManager = new ProfileManagerImpl();
Profile profile = ProfileCreator.getProfile2(FeatureCreator.getFeatureList2()); // Profile profile = ProfileCreator.getProfile2(FeatureCreator.getFeatureList2());
// profileManager.addProfile(profile); // profileManager.addProfile(profile);
// profileFeatureList = profile.getProfileFeaturesList(); // profileFeatureList = profile.getProfileFeaturesList();
} }
@Test(dependsOnMethods = ("addProfileFeatures")) @Test(dependsOnMethods = ("addProfileFeatures"))
public void addPolicy() throws PolicyManagementException, ProfileManagementException { public void addPolicy() throws PolicyManagementException, ProfileManagementException {
ProfileManager profileManager = new ProfileManagerImpl(); // ProfileManager profileManager = new ProfileManagerImpl();
Profile profile = ProfileCreator.getProfile5(FeatureCreator.getFeatureList5()); Profile profile = ProfileCreator.getProfile5(FeatureCreator.getFeatureList5());
profileManager.addProfile(profile); // profileManager.addProfile(profile);
PolicyManager policyManager = new PolicyManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
policy = PolicyCreator.createPolicy(profile); policy = PolicyCreator.createPolicy(profile);
policyManager.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId());
} }
@Test(dependsOnMethods = ("addPolicy")) @Test(dependsOnMethods = ("addPolicy"))
public void addPolicyToRole() throws PolicyManagementException { public void addPolicyToRole() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
List<String> roles = new ArrayList<String>(); List<String> roles = new ArrayList<String>();
roles.add("Test_ROLE_01"); roles.add("Test_ROLE_01");
roles.add("Test_ROLE_02"); roles.add("Test_ROLE_02");
roles.add("Test_ROLE_03"); roles.add("Test_ROLE_03");
policyManager.addPolicyToRole(roles, policy); pap.addPolicyToRole(roles, policy);
} }
@Test(dependsOnMethods = ("addPolicyToRole")) @Test(dependsOnMethods = ("addPolicyToRole"))
public void addPolicyToDevice() throws PolicyManagementException { public void addPolicyToDevice() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
Device device = DeviceCreator.getSingleDevice(); Device device = DeviceCreator.getSingleDevice();
List<DeviceIdentifier> deviceIdentifierList = new ArrayList<DeviceIdentifier>(); List<DeviceIdentifier> deviceIdentifierList = new ArrayList<DeviceIdentifier>();
@ -159,29 +153,31 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
deviceIdentifier.setType("android"); deviceIdentifier.setType("android");
deviceIdentifierList.add(deviceIdentifier); deviceIdentifierList.add(deviceIdentifier);
policyManager.addPolicyToDevice(deviceIdentifierList, policy); pap.addPolicyToDevice(deviceIdentifierList, policy);
} }
@Test(dependsOnMethods = ("addPolicyToDevice")) @Test(dependsOnMethods = ("addPolicyToDevice"))
public void addNewPolicy() throws PolicyManagementException, ProfileManagementException { public void addNewPolicy() throws PolicyManagementException, ProfileManagementException {
ProfileManager profileManager = new ProfileManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
Profile profile = ProfileCreator.getProfile3(FeatureCreator.getFeatureList3()); Profile profile = ProfileCreator.getProfile3(FeatureCreator.getFeatureList3());
profileManager.addProfile(profile); //pap.addProfile(profile);
PolicyManager policyManager = new PolicyManagerImpl(); // PolicyManager policyManager = new PolicyManagerImpl();
Policy policy = PolicyCreator.createPolicy2(profile); Policy policy = PolicyCreator.createPolicy2(profile);
policyManager.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId());
} }
@Test(dependsOnMethods = ("addPolicyToDevice")) @Test(dependsOnMethods = ("addPolicyToDevice"))
public void addThirdPolicy() throws PolicyManagementException, ProfileManagementException { public void addThirdPolicy() throws PolicyManagementException, ProfileManagementException {
ProfileManager profileManager = new ProfileManagerImpl(); //ProfileManager profileManager = new ProfileManagerImpl();
Profile profile = ProfileCreator.getProfile4(FeatureCreator.getFeatureList4()); Profile profile = ProfileCreator.getProfile4(FeatureCreator.getFeatureList4());
profileManager.addProfile(profile); //profileManager.addProfile(profile);
PolicyManager policyManager = new PolicyManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
Policy policy = PolicyCreator.createPolicy4(profile); Policy policy = PolicyCreator.createPolicy4(profile);
policyManager.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId());
} }
@Test(dependsOnMethods = ("addNewPolicy")) @Test(dependsOnMethods = ("addNewPolicy"))
@ -273,26 +269,29 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
@Test(dependsOnMethods = ("getRoleRelatedPolicy")) @Test(dependsOnMethods = ("getRoleRelatedPolicy"))
public void addSecondPolicy() throws PolicyManagementException, ProfileManagementException { public void addSecondPolicy() throws PolicyManagementException, ProfileManagementException {
ProfileManager profileManager = new ProfileManagerImpl(); // ProfileManager profileManager = new ProfileManagerImpl();
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList()); Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList());
profileManager.addProfile(profile); //profileManager.addProfile(profile);
PolicyManager policyManager = new PolicyManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
Policy policy = PolicyCreator.createPolicy3(profile); Policy policy = PolicyCreator.createPolicy3(profile);
policyManager.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId());
} }
@Test(dependsOnMethods = ("addSecondPolicy")) @Test(dependsOnMethods = ("addSecondPolicy"))
public void updatedPolicy() throws PolicyManagementException { public void updatedPolicy() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl(); PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
Profile profile = ProfileCreator.getProfile3(FeatureCreator.getFeatureList3()); Profile profile = ProfileCreator.getProfile3(FeatureCreator.getFeatureList3());
Policy policy = PolicyCreator.createPolicy3(profile); Policy policy = PolicyCreator.createPolicy3(profile);
policy.setPolicyName("Policy_05"); policy.setPolicyName("Policy_05");
policy = policyManager.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId());
List<String> users = new ArrayList<>(); List<String> users = new ArrayList<>();
users.add("Udara"); users.add("Udara");
users.add("Dileesha"); users.add("Dileesha");
policy.setUsers(users); policy.setUsers(users);
policyManager.updatePolicy(policy); pap.updatePolicy(policy);
pap.activatePolicy(policy.getId());
} }
@Test(dependsOnMethods = ("updatedPolicy")) @Test(dependsOnMethods = ("updatedPolicy"))

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
~ 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.
-->
<DeviceMgtConfiguration>
<ManagementRepository>
<DataSourceConfiguration>
<JndiLookupDefinition>
<Name>jdbc/DM_DS</Name>
</JndiLookupDefinition>
</DataSourceConfiguration>
<EmailClientConfiguration>
<minimumThread>8</minimumThread>
<maximumThread>100</maximumThread>
<keepAliveTime>20</keepAliveTime>
<ThreadQueueCapacity>1000</ThreadQueueCapacity>
</EmailClientConfiguration>
<IdentityConfiguration>
<ServerUrl>https://localhost:9443</ServerUrl>
<AdminUsername>admin</AdminUsername>
<AdminPassword>admin</AdminPassword>
</IdentityConfiguration>
<PolicyConfiguration>
<monitoringClass>org.wso2.carbon.policy.mgt</monitoringClass>
<monitoringEnable>true</monitoringEnable>
<monitoringFrequency>60000</monitoringFrequency>
<maxRetries>5</maxRetries>
<minRetriesToMarkUnreachable>8</minRetriesToMarkUnreachable>
<minRetriesToMarkInactive>20</minRetriesToMarkInactive>
</PolicyConfiguration>
</ManagementRepository>
</DeviceMgtConfiguration>

@ -0,0 +1,39 @@
<Notifications>
<NotificationMessage type="enrol">
<Subject>Enroll your Device with WSO2 MDM</Subject>
<Header>Dear {first-name},</Header>
<Body>
You have been registered to the WSO2 MDM. Below is the link to enroll.
</Body>
<Url>{downloadUrl}</Url>
<Footer1>
Best Regards,
</Footer1>
<Footer2>
WSO2 MDM Team.
</Footer2>
<Footer3>
http://www.wso2.com
</Footer3>
</NotificationMessage>
<NotificationMessage type="userRegistration">
<Subject>Enroll your Device with WSO2 MDM</Subject>
<Header>Dear {first-name},</Header>
<Body>
You have been registered to WSO2 MDM with following credentials.
Username: {user-name}
Password: {password}
Below is the link to enroll.
</Body>
<Url>{downloadUrl}</Url>
<Footer1>
Best Regards,
</Footer1>
<Footer2>
WSO2 MDM Team.
</Footer2>
<Footer3>
http://www.wso2.com
</Footer3>
</NotificationMessage>
</Notifications>

@ -160,6 +160,8 @@ CREATE TABLE IF NOT EXISTS DM_POLICY (
CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY (
ID INT(11) NOT NULL AUTO_INCREMENT , ID INT(11) NOT NULL AUTO_INCREMENT ,
DEVICE_ID INT(11) NOT NULL , DEVICE_ID INT(11) NOT NULL ,
ENROLMENT_ID INT(11) NOT NULL,
DEVICE BLOB NOT NULL,
POLICY_ID INT(11) NOT NULL , POLICY_ID INT(11) NOT NULL ,
PRIMARY KEY (ID) , PRIMARY KEY (ID) ,
CONSTRAINT FK_POLICY_DEVICE_POLICY CONSTRAINT FK_POLICY_DEVICE_POLICY
@ -245,8 +247,9 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY (
CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED (
ID INT NOT NULL AUTO_INCREMENT , ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT NOT NULL , DEVICE_ID INT NOT NULL,
ENROLMENT_ID INT(11) NOT NULL,
POLICY_ID INT NOT NULL , POLICY_ID INT NOT NULL ,
POLICY_CONTENT BLOB NULL , POLICY_CONTENT BLOB NULL ,
TENANT_ID INT NOT NULL, TENANT_ID INT NOT NULL,
@ -316,6 +319,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES (
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT NOT NULL, DEVICE_ID INT NOT NULL,
ENROLMENT_ID INT(11) NOT NULL,
POLICY_ID INT NOT NULL, POLICY_ID INT NOT NULL,
TENANT_ID INT NOT NULL, TENANT_ID INT NOT NULL,
STATUS INT NULL, STATUS INT NULL,
@ -352,12 +356,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT (
POLICY_ID INT NOT NULL, POLICY_ID INT NOT NULL,
DEVICE_TYPE_ID INT NOT NULL, DEVICE_TYPE_ID INT NOT NULL,
TENANT_ID INT(11) NOT NULL, TENANT_ID INT(11) NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID)
CONSTRAINT FK_POLICY_CHANGE_MGT_POLICY
FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
); );
-- POLICY RELATED TABLES FINISHED -- -- POLICY RELATED TABLES FINISHED --

@ -37,8 +37,11 @@
</IdentityConfiguration> </IdentityConfiguration>
<PolicyConfiguration> <PolicyConfiguration>
<monitoringClass>org.wso2.carbon.policy.mgt</monitoringClass> <monitoringClass>org.wso2.carbon.policy.mgt</monitoringClass>
<monitoringEnable>true</monitoringEnable>
<monitoringFrequency>60000</monitoringFrequency>
<maxRetries>5</maxRetries> <maxRetries>5</maxRetries>
<minRetriesToMarkUnreachable>8</minRetriesToMarkUnreachable> <minRetriesToMarkUnreachable>8</minRetriesToMarkUnreachable>
<minRetriesToMarkInactive>20</minRetriesToMarkInactive>
</PolicyConfiguration> </PolicyConfiguration>
</ManagementRepository> </ManagementRepository>
</DeviceMgtConfiguration> </DeviceMgtConfiguration>

@ -145,6 +145,8 @@ CREATE TABLE IF NOT EXISTS DM_POLICY (
CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY (
ID INT(11) NOT NULL AUTO_INCREMENT , ID INT(11) NOT NULL AUTO_INCREMENT ,
DEVICE_ID INT(11) NOT NULL , DEVICE_ID INT(11) NOT NULL ,
ENROLMENT_ID INT(11) NOT NULL,
DEVICE BLOB NOT NULL,
POLICY_ID INT(11) NOT NULL , POLICY_ID INT(11) NOT NULL ,
PRIMARY KEY (ID) , PRIMARY KEY (ID) ,
CONSTRAINT FK_POLICY_DEVICE_POLICY CONSTRAINT FK_POLICY_DEVICE_POLICY
@ -232,6 +234,7 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY (
CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED (
ID INT NOT NULL AUTO_INCREMENT , ID INT NOT NULL AUTO_INCREMENT ,
DEVICE_ID INT NOT NULL , DEVICE_ID INT NOT NULL ,
ENROLMENT_ID INT(11) NOT NULL,
POLICY_ID INT NOT NULL , POLICY_ID INT NOT NULL ,
POLICY_CONTENT BLOB NULL , POLICY_CONTENT BLOB NULL ,
TENANT_ID INT NOT NULL, TENANT_ID INT NOT NULL,
@ -297,6 +300,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES (
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT NOT NULL, DEVICE_ID INT NOT NULL,
ENROLMENT_ID INT(11) NOT NULL,
POLICY_ID INT NOT NULL, POLICY_ID INT NOT NULL,
TENANT_ID INT NOT NULL, TENANT_ID INT NOT NULL,
STATUS INT NULL, STATUS INT NULL,
@ -319,12 +323,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT (
POLICY_ID INT NOT NULL, POLICY_ID INT NOT NULL,
DEVICE_TYPE_ID INT NOT NULL, DEVICE_TYPE_ID INT NOT NULL,
TENANT_ID INT(11) NOT NULL, TENANT_ID INT(11) NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID)
CONSTRAINT FK_POLICY_CHANGE_MGT_POLICY
FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
); );

Loading…
Cancel
Save