Add improvements to policy cache

policy-cache-improvements
Rajitha Kumara 2 years ago
parent c566d95f77
commit a5218f538e

@ -42,7 +42,6 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
@ -69,7 +68,7 @@
org.wso2.carbon.device.mgt.core.*,
org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.ntask.*,
org.wso2.carbon.caching.*
com.google.common.*
</Import-Package>
<Export-Package>
!org.wso2.carbon.policy.mgt.core.internal,
@ -123,6 +122,12 @@
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>

@ -0,0 +1,30 @@
package org.wso2.carbon.policy.mgt.core.cache.guava;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
public class GuavaPolicyCacheException extends PolicyManagementException {
private static final long serialVersionUID = 458698541247633587L;
private String errorMessage;
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getErrorMessage() {
return errorMessage;
}
public GuavaPolicyCacheException() {
super();
}
public GuavaPolicyCacheException(Throwable cause) {
super(cause);
}
public GuavaPolicyCacheException(String errorMessage, Throwable cause) {
super(cause);
setErrorMessage(errorMessage);
}
}

@ -0,0 +1,48 @@
package org.wso2.carbon.policy.mgt.core.cache.guava;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import java.util.Objects;
public class GuavaPolicyCacheKey {
private final int tenantId;
private final int policyId;
private volatile int hashCode;
public GuavaPolicyCacheKey(int policyId) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
this.tenantId = tenantId;
this.policyId = policyId;
}
public int getTenantId() {
return tenantId;
}
public int getPolicyId() {
return policyId;
}
@Override
public String toString() {
return tenantId+"/"+policyId;
}
@Override
public boolean equals(Object other) {
if(!(other instanceof GuavaPolicyCacheKey)) {
return false;
}
GuavaPolicyCacheKey otherPolicyCacheKey = (GuavaPolicyCacheKey) other;
return (tenantId == otherPolicyCacheKey.tenantId
&& policyId == otherPolicyCacheKey.policyId);
}
@Override
public int hashCode() {
if(hashCode == 0) {
hashCode = Objects.hash(tenantId, policyId);
}
return hashCode;
}
}

@ -0,0 +1,57 @@
package org.wso2.carbon.policy.mgt.core.cache.guava;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import java.util.List;
public interface GuavaPolicyCacheManager {
/*
* return list of policies requested by policy ids,
* if the requested ids contains invalid policy ids this will only return policies for valid ids
* */
List<Policy> getPolicies(List<Integer> policyIds) throws PolicyManagementException;
/* return all polices - ensures to return all the policies exists in the database */
List<Policy> getAllPolicies() throws PolicyManagementException;
List<Policy> getAllPolicies(String deviceType) throws PolicyManagementException;
/*
* return the policy requested by policy id,
* if the requested id invalid this will return null
* */
Policy getPolicy(Policy policy) throws PolicyManagementException;
/*
* return the policy requested by policy id,
* if the requested id invalid this will return null
* */
Policy getPolicy(int policyId) throws PolicyManagementException;
/* add policy to the cache - make sure to execute when creating policies */
void addPolicies(List<Policy> policies);
/* add policy to the cache - make sure to run when creating a policy */
void addPolicy(Policy policy);
/* update the policies using provided policies */
void updatePolicies(List<Policy> policies);
/* update the policies using provided policy */
void updatePolicy(Policy policy);
void refreshPolicy(Policy policy);
void refreshPolicy(int policyId);
void invalidatePolicy(Policy policy);
void invalidatePolicy(int policyId);
void invalidateAll();
/* refresh the cache with newly loaded values from the database */
void rePopulateCache() throws PolicyManagementException;
}

@ -0,0 +1,161 @@
package org.wso2.carbon.policy.mgt.core.cache.guava.impl;
import com.google.common.cache.LoadingCache;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.guava.GuavaPolicyCacheKey;
import org.wso2.carbon.policy.mgt.core.cache.guava.GuavaPolicyCacheManager;
import org.wso2.carbon.policy.mgt.core.cache.guava.utils.GuavaPolicyCacheBuilder;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
public class GuavaPolicyCacheManagerImpl implements GuavaPolicyCacheManager {
private static final Log log = LogFactory.getLog(GuavaPolicyCacheManagerImpl.class);
private static LoadingCache<GuavaPolicyCacheKey, Policy> policyCache;
private static volatile GuavaPolicyCacheManagerImpl policyCacheManager;
private GuavaPolicyCacheManagerImpl() {
}
public static GuavaPolicyCacheManagerImpl getInstance() {
if(policyCacheManager == null) {
synchronized (GuavaPolicyCacheManagerImpl.class) {
if(policyCacheManager == null) {
policyCacheManager = new GuavaPolicyCacheManagerImpl();
setPolicyCache();
}
}
}
return policyCacheManager;
}
@Override
public List<Policy> getPolicies(List<Integer> policyIds) throws PolicyManagementException {
List<Policy> resultPolicyList;
Map<GuavaPolicyCacheKey, Policy> cacheMap;
try {
cacheMap = policyCache.getAll(generateCacheKeys(policyIds));
resultPolicyList = cacheMap.values().stream().
filter(policy -> policy != GuavaPolicyCacheBuilder.NULL).
collect(Collectors.toList());
} catch (ExecutionException ex) {
log.error("Error occurred when retrieving policies from cache");
throw new PolicyManagementException("Error occurred (fails to return policies for policy ids "+policyIds.toString()+")", ex);
}
return resultPolicyList;
}
@Override
public List<Policy> getAllPolicies() throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
List<Integer> tenantSpecificPolicyIds = policyManager.getAllPolicyIds();
return getPolicies(tenantSpecificPolicyIds);
}
@Override
public List<Policy> getAllPolicies(String deviceType) throws PolicyManagementException {
List<Policy> allPolicies = getAllPolicies();
return allPolicies.stream().
filter(policy -> deviceType.equals(policy.getOwnershipType())).
collect(Collectors.toList());
}
@Override
public Policy getPolicy(Policy policy) throws PolicyManagementException {
return getPolicy(policy.getId());
}
@Override
public Policy getPolicy(int policyId) throws PolicyManagementException {
Policy resultPolicy;
try {
GuavaPolicyCacheKey key = new GuavaPolicyCacheKey(policyId);
resultPolicy = policyCache.get(key);
if(resultPolicy == GuavaPolicyCacheBuilder.NULL) {
resultPolicy = null;
}
}catch (ExecutionException ex) {
log.error("Error occurred when retrieving policy from cache");
throw new PolicyManagementException("Error occurred (fails to fetch requested policy for policy id "+policyId+")", ex);
}
return resultPolicy;
}
@Override
public void addPolicies(List<Policy> policies) {
Map<GuavaPolicyCacheKey, Policy> policyMap = new HashMap<>();
for(Policy policy : policies) {
GuavaPolicyCacheKey key = new GuavaPolicyCacheKey(policy.getId());
policyMap.put(key, policy);
}
policyCache.putAll(policyMap);
}
@Override
public void addPolicy(Policy policy) {
GuavaPolicyCacheKey key = new GuavaPolicyCacheKey(policy.getId());
policyCache.put(key, policy);
}
@Override
public void updatePolicies(List<Policy> policies) {
addPolicies(policies);
}
@Override
public void updatePolicy(Policy policy) {
addPolicy(policy);
}
@Override
public void refreshPolicy(Policy policy) {
refreshPolicy(policy.getId());
}
@Override
public void refreshPolicy(int policyId) {
GuavaPolicyCacheKey key = new GuavaPolicyCacheKey(policyId);
policyCache.refresh(key);
}
@Override
public void invalidatePolicy(Policy policy) {
invalidatePolicy(policy.getId());
}
@Override
public void invalidatePolicy(int policyId) {
GuavaPolicyCacheKey key = new GuavaPolicyCacheKey(policyId);
policyCache.invalidate(key);
}
@Override
public void invalidateAll() {
policyCache.invalidateAll();
}
@Override
public void rePopulateCache() throws PolicyManagementException {
invalidateAll();
getAllPolicies();
}
private static void setPolicyCache() {
GuavaPolicyCacheBuilder cacheBuilder = new GuavaPolicyCacheBuilder();
policyCache = cacheBuilder.withCacheBuilder().withCacheLoader().build();
}
private List<GuavaPolicyCacheKey> generateCacheKeys(List<Integer> policyIds) {
return policyIds.stream().map(GuavaPolicyCacheKey::new).collect(Collectors.toList());
}
}

@ -0,0 +1,77 @@
package org.wso2.carbon.policy.mgt.core.cache.guava.utils;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.guava.GuavaPolicyCacheKey;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class GuavaPolicyCacheBuilder {
private static final Log log = LogFactory.getLog(GuavaPolicyCacheBuilder.class);
private CacheLoader<GuavaPolicyCacheKey, Policy> cacheLoader;
private CacheBuilder<Object, Object> cacheBuilder;
public static final Policy NULL = new Policy();
public GuavaPolicyCacheBuilder withCacheLoader() {
cacheLoader = new CacheLoader<GuavaPolicyCacheKey, Policy>() {
@Override
public Policy load(GuavaPolicyCacheKey key) throws Exception {
return singleLoad(key);
}
@Override
public Map<GuavaPolicyCacheKey, Policy> loadAll(Iterable<? extends GuavaPolicyCacheKey> keys) throws Exception {
return bulkLoad(keys);
}
};
return this;
}
private Policy singleLoad(GuavaPolicyCacheKey key) throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
Policy policy = policyManager.getPolicy(key.getPolicyId());
if(policy == null) {
return NULL;
}
return policy;
}
private Map<GuavaPolicyCacheKey, Policy> bulkLoad(Iterable<? extends GuavaPolicyCacheKey> keys) throws PolicyManagementException {
PolicyManager policyManager = new PolicyManagerImpl();
Map<GuavaPolicyCacheKey, Policy> cacheMap = new HashMap<>();
for(GuavaPolicyCacheKey key : keys) {
cacheMap.put(key, NULL);
}
List<Policy> policies = policyManager.getPolicies();
for(Policy policy : policies) {
GuavaPolicyCacheKey key = new GuavaPolicyCacheKey(policy.getId());
cacheMap.put(key, policy);
}
return cacheMap;
}
public GuavaPolicyCacheBuilder withCacheBuilder() {
// TODO : refactor to use configuration file
cacheBuilder = CacheBuilder.newBuilder().
maximumSize(GuavaPolicyCacheConstants.MAX_ENTRIES).
expireAfterWrite(GuavaPolicyCacheConstants.EXPIRE_AFTER_WRITE_IN_MIN, TimeUnit.MINUTES);
return this;
}
public LoadingCache<GuavaPolicyCacheKey, Policy> build() {
return cacheBuilder.build(cacheLoader);
}
}

@ -0,0 +1,7 @@
package org.wso2.carbon.policy.mgt.core.cache.guava.utils;
public final class GuavaPolicyCacheConstants {
public static final long MAX_ENTRIES = 1000;
public static final long EXPIRE_AFTER_WRITE_IN_MIN = 30;
}

@ -123,4 +123,6 @@ public interface FeatureDAO {
*/
boolean deleteProfileFeatures(int featureId) throws FeatureManagerDAOException;
List<ProfileFeature> getProfileFeatures(List<Integer> profileIds) throws FeatureManagerDAOException;
}

@ -224,4 +224,6 @@ public interface PolicyDAO {
* @throws PolicyManagerDAOException when there is an error while retrieving the policies from database
*/
List<Policy> getAllPolicies(PaginationRequest request) throws PolicyManagerDAOException;
List<Integer> getAllPolicyIds() throws PolicyManagerDAOException;
}

@ -86,4 +86,6 @@ public interface ProfileDAO {
*/
List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException;
List<Profile> getProfiles(List<Integer> profileIds) throws ProfileManagerDAOException;
}

@ -34,6 +34,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ProfileDAOImpl implements ProfileDAO {
@ -281,6 +282,40 @@ public class ProfileDAOImpl implements ProfileDAO {
return profileList;
}
@Override
public List<Profile> getProfiles(List<Integer> profileIds) throws ProfileManagerDAOException {
Connection connection;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Profile> profileList = new ArrayList<>();
String profileIdsSQL = profileIds.stream().map(String::valueOf).collect(Collectors.joining(", ", "(", ")"));
try {
connection = this.getConnection();
String query = "SELECT * FROM DM_PROFILE WHERE ID IN (?)";
query = query.replace("(?)", profileIdsSQL);
stmt = connection.prepareStatement(query);
resultSet = stmt.executeQuery();
while(resultSet.next()) {
Profile profile = new Profile();
profile.setProfileId(resultSet.getInt("ID"));
profile.setProfileName(resultSet.getString("PROFILE_NAME"));
profile.setTenantId(resultSet.getInt("TENANT_ID"));
profile.setDeviceType(resultSet.getString("DEVICE_TYPE"));
profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME"));
profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME"));
profileList.add(profile);
}
} catch (SQLException e) {
String msg = "Error occurred while reading the profile list "+profileIds.toString()+" from the database.";
log.error(msg, e);
throw new ProfileManagerDAOException(msg, e);
}finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
return profileList;
}
private Connection getConnection() throws ProfileManagerDAOException {
return PolicyManagementDAOFactory.getConnection();

@ -40,6 +40,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Abstract implementation of FeatureDAO which holds generic SQL queries.
@ -312,6 +313,71 @@ public abstract class AbstractFeatureDAO implements FeatureDAO {
return featureList;
}
@Override
public List<ProfileFeature> getProfileFeatures(List<Integer> profileIds) throws FeatureManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ProfileFeature> featureList = new ArrayList<ProfileFeature>();
String profileIdsSQL = profileIds.stream().map(String::valueOf).collect(Collectors.joining(", ", "(", ")"));
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT ID, PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT FROM DM_PROFILE_FEATURES " +
"WHERE TENANT_ID = ? AND PROFILE_ID IN (?)";
query = query.replace("(?)", profileIdsSQL);
stmt = conn.prepareStatement(query);
stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
ProfileFeature profileFeature = new ProfileFeature();
profileFeature.setFeatureCode(resultSet.getString("FEATURE_CODE"));
profileFeature.setDeviceType(resultSet.getString("DEVICE_TYPE"));
profileFeature.setId(resultSet.getInt("ID"));
profileFeature.setProfileId(resultSet.getInt("PROFILE_ID"));
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
byte[] contentBytes;
try {
contentBytes = (byte[]) resultSet.getBytes("CONTENT");
bais = new ByteArrayInputStream(contentBytes);
ois = new ObjectInputStream(bais);
profileFeature.setContent((Object) ois.readObject().toString());
} finally {
if (bais != null) {
try {
bais.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
}
featureList.add(profileFeature);
}
} catch (SQLException e) {
throw new FeatureManagerDAOException("Unable to get the list of the features from database.", e);
} catch (IOException e) {
throw new FeatureManagerDAOException("Unable to read the byte stream for content", e);
} catch (ClassNotFoundException e) {
throw new FeatureManagerDAOException("Class not found while converting the object", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return featureList;
}
private Connection getConnection() throws FeatureManagerDAOException {
return PolicyManagementDAOFactory.getConnection();
}

@ -44,6 +44,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyCriterion;
import org.wso2.carbon.policy.mgt.common.Criterion;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
@ -65,6 +66,7 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* Abstract implementation of PolicyDAO which holds generic SQL queries.
@ -1022,7 +1024,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
Policy policy = new Policy();
Policy policy = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
@ -1033,6 +1035,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
policy = new Policy();
policy.setId(policyId);
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
@ -1841,6 +1844,58 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
}
}
// return tenant specific policy Ids
@Override
public List<Integer> getAllPolicyIds() throws PolicyManagerDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Integer> policyIdsInDB = new ArrayList<>();
try {
conn = this.getConnection();
String query = "SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery();
while(resultSet.next()) {
policyIdsInDB.add(resultSet.getInt("ID"));
}
return policyIdsInDB;
} catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database ";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
}
}
public List<Policy> getPolicies(List<Integer> policyIds) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
String policyIdsSQL = policyIds.stream().map(String::valueOf).collect(Collectors.joining(", ", "(", ")"));
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_POLICY WHERE TENANT_ID = ? AND ID IN (?)";
query = query.replace("(?)", policyIdsSQL);
stmt = conn.prepareStatement(query);
stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery();
return this.extractPolicyListFromDbResult(resultSet, tenantId);
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while reading the policy list "+policyIds.toString()+" from the database", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
}
}
protected List<Policy> extractPolicyListFromDbResult(ResultSet resultSet, int tenantId) throws SQLException {
List<Policy> policies = new ArrayList<>();
while (resultSet.next()) {

@ -28,6 +28,7 @@ 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.task.impl.DynamicPartitionedScheduleTask;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.guava.impl.GuavaPolicyCacheManagerImpl;
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.mgt.PolicyManager;
@ -50,7 +51,8 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
UpdatedPolicyDeviceListBean updatedPolicyDeviceList = policyManager.applyChangesMadeToPolicies();
List<String> deviceTypes = updatedPolicyDeviceList.getChangedDeviceTypes();
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().rePopulateCache();
}
if (log.isDebugEnabled()) {
log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size());

@ -33,8 +33,7 @@ import org.wso2.carbon.ntask.core.TaskInfo;
import org.wso2.carbon.ntask.core.TaskManager;
import org.wso2.carbon.ntask.core.service.TaskService;
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.cache.guava.impl.GuavaPolicyCacheManagerImpl;
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.ProfileManager;
@ -66,7 +65,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public Policy addPolicy(Policy policy) throws PolicyManagementException {
Policy resultantPolicy = policyManager.addPolicy(policy);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().addPolicy(resultantPolicy);
}
return resultantPolicy;
}
@ -75,7 +75,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public Policy updatePolicy(Policy policy) throws PolicyManagementException {
Policy resultantPolicy = policyManager.updatePolicy(policy);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().updatePolicy(resultantPolicy);
}
return resultantPolicy;
}
@ -84,7 +85,10 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagementException {
boolean bool = policyManager.updatePolicyPriorities(policies);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
if(bool) {
GuavaPolicyCacheManagerImpl.getInstance().updatePolicies(policies);
}
}
return bool;
}
@ -93,7 +97,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public void activatePolicy(int policyId) throws PolicyManagementException {
policyManager.activatePolicy(policyId);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().refreshPolicy(policyId);
}
}
@ -101,7 +106,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public void inactivatePolicy(int policyId) throws PolicyManagementException {
policyManager.inactivatePolicy(policyId);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().refreshPolicy(policyId);
}
}
@ -109,8 +115,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public boolean deletePolicy(Policy policy) throws PolicyManagementException {
boolean bool = policyManager.deletePolicy(policy);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManager policyCacheManager = PolicyCacheManagerImpl.getInstance();
policyCacheManager.rePopulateCache();
//PolicyCacheManager policyCacheManager = PolicyCacheManagerImpl.getInstance();
//policyCacheManager.rePopulateCache();
if(bool) {
GuavaPolicyCacheManagerImpl.getInstance().invalidatePolicy(policy);
}
}
return bool;
}
@ -119,8 +128,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public boolean deletePolicy(int policyId) throws PolicyManagementException {
boolean bool = policyManager.deletePolicy(policyId);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManager policyCacheManager = PolicyCacheManagerImpl.getInstance();
policyCacheManager.rePopulateCache();
//PolicyCacheManager policyCacheManager = PolicyCacheManagerImpl.getInstance();
//policyCacheManager.rePopulateCache();
if(bool) {
GuavaPolicyCacheManagerImpl.getInstance().invalidatePolicy(policyId);
}
}
return bool;
}
@ -189,7 +201,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
PolicyManagementException {
policy = policyManager.addPolicyToDevice(deviceIdentifierList, policy);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().updatePolicy(policy);
}
return policy;
}
@ -198,7 +211,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException {
policy = policyManager.addPolicyToRole(roleNames, policy);
if (policyConfiguration.getCacheEnable()) {
PolicyCacheManagerImpl.getInstance().rePopulateCache();
//PolicyCacheManagerImpl.getInstance().rePopulateCache();
GuavaPolicyCacheManagerImpl.getInstance().updatePolicy(policy);
}
return policy;
}
@ -206,7 +220,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public List<Policy> getPolicies() throws PolicyManagementException {
if (policyConfiguration.getCacheEnable()) {
return PolicyCacheManagerImpl.getInstance().getAllPolicies();
//return PolicyCacheManagerImpl.getInstance().getAllPolicies();
return GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
return policyManager.getPolicies();
}
@ -215,7 +230,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public Policy getPolicy(int policyId) throws PolicyManagementException {
if (policyConfiguration.getCacheEnable()) {
return PolicyCacheManagerImpl.getInstance().getPolicy(policyId);
//return PolicyCacheManagerImpl.getInstance().getPolicy(policyId);
return GuavaPolicyCacheManagerImpl.getInstance().getPolicy(policyId);
} else {
return policyManager.getPolicy(policyId);
}
@ -318,17 +334,19 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public int getPolicyCount() throws PolicyManagementException {
if (policyConfiguration.getCacheEnable()) {
return PolicyCacheManagerImpl.getInstance().getAllPolicies().size();
} else {
return policyManager.getPolicyCount();
}
//if (policyConfiguration.getCacheEnable()) {
//return PolicyCacheManagerImpl.getInstance().getAllPolicies().size();
//return GuavaPolicyCacheManagerImpl.getInstance().getAllPoliciesCount();
//} else {
return policyManager.getAllPolicyIds().size();
//}
}
@Override
public List<Policy> getPolicies(String policyType) throws PolicyManagementException {
if (policyConfiguration.getCacheEnable()) {
return PolicyCacheManagerImpl.getInstance().getAllPolicies(policyType);
//return PolicyCacheManagerImpl.getInstance().getAllPolicies(policyType);
return GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies(policyType);
} else {
return policyManager.getPolicies(policyType);
}

@ -99,4 +99,8 @@ public interface PolicyManager {
* while retrieving device groups
*/
List<Policy> getPolicyList(PaginationRequest request) throws PolicyManagementException;
List<Integer> getAllPolicyIds() throws PolicyManagementException;
List<Policy> getPolicies(List<Integer> policyIds) throws PolicyManagementException;
}

@ -37,4 +37,6 @@ public interface ProfileManager {
List<Profile> getAllProfiles() throws ProfileManagementException;
List<Profile> getProfilesOfDeviceType(String deviceType) throws ProfileManagementException;
List<Profile> getProfiles(List<Integer> profileIds) throws ProfileManagementException;
}

@ -63,6 +63,7 @@ import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.policy.mgt.common.Criterion;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
import org.wso2.carbon.policy.mgt.core.cache.guava.impl.GuavaPolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO;
import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException;
@ -86,6 +87,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class PolicyManagerImpl implements PolicyManager {
@ -526,7 +528,8 @@ public class PolicyManagerImpl implements PolicyManager {
try {
List<Policy> existingPolicies;
if (policyConfiguration.getCacheEnable()) {
existingPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
//existingPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
existingPolicies = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
existingPolicies = this.getPolicies();
}
@ -807,14 +810,24 @@ public class PolicyManagerImpl implements PolicyManager {
List<Device> deviceList;
List<String> roleNames;
List<String> userNames;
// need to change after check on the complexity
try {
PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getPolicy(policyId);
if(policy == null) {
return null;
}
roleNames = policyDAO.getPolicyAppliedRoles(policyId);
userNames = policyDAO.getPolicyAppliedUsers(policyId);
policy.setRoles(roleNames);
policy.setUsers(userNames);
policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policyId));
List<DeviceGroupWrapper> deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policyId);
if(!deviceGroupWrappers.isEmpty()) {
deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers);
}
policy.setDeviceGroups(deviceGroupWrappers);
if (log.isDebugEnabled()) {
log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() +
" having policy id " + policy.getId());
@ -825,6 +838,8 @@ public class PolicyManagerImpl implements PolicyManager {
policyId + ")", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} catch (GroupManagementException e) {
throw new PolicyManagementException("Error occurred while getting device groups", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
@ -930,7 +945,8 @@ public class PolicyManagerImpl implements PolicyManager {
List<Policy> tempPolicyList;
if (policyConfiguration.getCacheEnable()) {
tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
//tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
tempPolicyList = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
tempPolicyList = this.getPolicies();
}
@ -952,7 +968,8 @@ public class PolicyManagerImpl implements PolicyManager {
List<Policy> policies = new ArrayList<>();
List<Policy> allPolicies;
if (policyConfiguration.getCacheEnable()) {
allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
//allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
allPolicies = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
allPolicies = this.getPolicies();
}
@ -983,7 +1000,8 @@ public class PolicyManagerImpl implements PolicyManager {
List<Policy> tempPolicyList;
if (policyConfiguration.getCacheEnable()) {
tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
//tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
tempPolicyList = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
tempPolicyList = this.getPolicies();
}
@ -1015,7 +1033,8 @@ public class PolicyManagerImpl implements PolicyManager {
}
List<Policy> tempPolicyList;
if (policyConfiguration.getCacheEnable()) {
tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
//tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies();
tempPolicyList = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
tempPolicyList = this.getPolicies();
}
@ -1113,7 +1132,8 @@ public class PolicyManagerImpl implements PolicyManager {
try {
List<Policy> allPolicies;
if (policyConfiguration.getCacheEnable()) {
allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
//allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
allPolicies = GuavaPolicyCacheManagerImpl.getInstance().getAllPolicies();
} else {
allPolicies = this.getPolicies();
}
@ -1537,4 +1557,46 @@ public class PolicyManagerImpl implements PolicyManager {
}
return policyList;
}
@Override
public List<Integer> getAllPolicyIds() throws PolicyManagementException {
try {
PolicyManagementDAOFactory.openConnection();
return policyDAO.getAllPolicyIds();
} catch (PolicyManagerDAOException ex) {
throw new PolicyManagementException("Error occurred while getting all the policy ids.", ex);
} catch (SQLException ex) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", ex);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
}
public List<Policy> getPolicies(List<Integer> policyIds) throws PolicyManagementException {
List<Policy> policyList;
List<Profile> profileList;
try {
PolicyManagementDAOFactory.openConnection();
policyList = policyDAO.getAllPolicies();
List<Integer> profileIds = policyList.stream().map(Policy::getProfileId).collect(Collectors.toList());
profileList = profileManager.getProfiles(profileIds);
this.buildPolicyList(policyList, profileList);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting all the policies.", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} catch (GroupManagementException e) {
throw new PolicyManagementException("Error occurred while getting device groups.", e);
} catch (ProfileManagementException e) {
throw new PolicyManagementException("Error occurred while getting all the profiles.", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
// connection is already closed
for (Policy policy : policyList) {
policy.setDevices(this.getPolicyAppliedDevicesIds(policy.getId()));
}
return policyList;
}
}

@ -141,7 +141,7 @@ public class ProfileManagerImpl implements ProfileManager {
List<ProfileFeature> featureList;
try {
PolicyManagementDAOFactory.openConnection();
profile = profileDAO.getProfile(profileId);
profile = profileDAO.getProfile(profileId); // return null when there is no profile to requested policy id
if (profile == null) {
throw new ProfileManagementException("Profile is not available with profile id (" + profileId + ")");
}
@ -221,4 +221,33 @@ public class ProfileManagerImpl implements ProfileManager {
}
return profileList;
}
}
public List<Profile> getProfiles(List<Integer> profileIds) throws ProfileManagementException {
List<Profile> profileList;
try {
PolicyManagementDAOFactory.openConnection();
profileList = profileDAO.getProfiles(profileIds);
List<ProfileFeature> featureList = featureDAO.getProfileFeatures(profileIds);
for (Profile profile : profileList) {
List<ProfileFeature> list = new ArrayList<>();
for (ProfileFeature profileFeature : featureList) {
if (profile.getProfileId() == profileFeature.getProfileId()) {
list.add(profileFeature);
}
}
profile.setProfileFeaturesList(list);
}
} catch (ProfileManagerDAOException e) {
throw new ProfileManagementException("Error occurred while getting profiles", e);
} catch (FeatureManagerDAOException e) {
throw new ProfileManagementException("Error occurred while getting features related to profiles", e);
} catch (SQLException e) {
throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
return profileList;
}
}

@ -48,6 +48,7 @@ import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.guava.impl.GuavaPolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;

Loading…
Cancel
Save