From db8645e6c5c7d3b9529756db64c7f80af04284ce Mon Sep 17 00:00:00 2001 From: Pahansith Gunathilake Date: Thu, 3 Oct 2019 17:26:31 +0000 Subject: [PATCH] Add policy type --- .../device/mgt/jaxrs/beans/PolicyWrapper.java | 14 ++++ .../service/api/PolicyManagementService.java | 83 +++++++++++++++++++ .../impl/PolicyManagementServiceImpl.java | 30 +++++++ .../device/mgt/common/policy/mgt/Policy.java | 14 ++++ .../public/js/policy-create.js | 2 + .../device/mgt/jaxrs/beans/PolicyWrapper.java | 12 +++ .../impl/PolicyManagementServiceImpl.java | 1 + .../mgt/common/PolicyAdministratorPoint.java | 7 ++ .../mgt/core/cache/PolicyCacheManager.java | 9 ++ .../cache/impl/PolicyCacheManagerImpl.java | 68 ++++++++++----- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 + .../mgt/core/dao/impl/PolicyDAOImpl.java | 72 ++++++++++++---- .../impl/PolicyAdministratorPointImpl.java | 8 ++ .../policy/mgt/core/mgt/PolicyManager.java | 2 + .../mgt/core/mgt/impl/PolicyManagerImpl.java | 76 ++++++++++++----- .../core/PolicyManagerServiceImplTest.java | 1 + .../policy/mgt/core/util/PolicyCreator.java | 6 +- .../src/test/resources/sql/CreateH2TestDB.sql | 1 + .../src/main/resources/dbscripts/cdm/h2.sql | 1 + .../main/resources/dbscripts/cdm/mssql.sql | 1 + .../main/resources/dbscripts/cdm/mysql.sql | 1 + .../main/resources/dbscripts/cdm/oracle.sql | 1 + .../resources/dbscripts/cdm/postgresql.sql | 1 + 23 files changed, 355 insertions(+), 58 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java index 06e99cd74d..a5306c825f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java @@ -23,6 +23,7 @@ import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper; +import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.util.List; @@ -102,6 +103,19 @@ public class PolicyWrapper { required = true) private List deviceGroups; + @ApiModelProperty(name = "policyType", value = "Type of the corresponding policy", + required = true) + @NotNull + private String policyType; + + public String getPolicyType() { + return policyType; + } + + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + public Profile getProfile() { return profile; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java index 0b25518d46..a438570798 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java @@ -697,4 +697,87 @@ public interface PolicyManagementService { @PathParam("deviceId") @Size(max = 45) String deviceId); + + @GET + @Path("/type/{policyType}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting Details of Policies", + responseContainer = "List", + notes = "Retrieve the details of all the policies filtered by policy type in WSO2 EMM.", + response = Policy.class, + tags = "Device Policy Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:policies:get-details") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched policies.", + response = Policy.class, + responseContainer = "List", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client already has the latest version " + + "of the requested resource."), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = ("Internal Server Error. \n Server error occurred while fetching the policies."), + response = ErrorResponse.class) + }) + Response getPolicies(@ApiParam( + name = "policyType", + value = "The policy type, such as general policy, Geo policy.", + required = true) + @PathParam("policyType") + String policyType, + @ApiParam( + name = "If-Modified-Since", + value = "Checks if the requested variant was modified, since the specified date-time. \n" + + "Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z.\n" + + "Example: Mon, 05 Jan 2014 15:10:00 +0200", + required = false) + @HeaderParam("If-Modified-Since") + String ifModifiedSince, + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items", + required = false, + defaultValue = "0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many policy details you require from the starting pagination index/offset.", + required = false, + defaultValue = "5") + @QueryParam("limit") + int limit + ); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index 01dd4880c1..be89add49e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -123,6 +123,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setUsers(policyWrapper.getUsers()); policy.setCompliance(policyWrapper.getCompliance()); policy.setDeviceGroups(policyWrapper.getDeviceGroups()); + policy.setPolicyType(policyWrapper.getPolicyType()); //TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here. List devices = new ArrayList(); List deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); @@ -398,4 +399,33 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { return Response.status(Response.Status.OK).entity(policy).build(); } + @GET + @Path("/type/{policyType}") + @Override + public Response getPolicies( + @PathParam("policyType") String policyType, + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + + RequestValidationUtil.validatePaginationParameters(offset, limit); + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + List policies; + List filteredPolicies; + PolicyList targetPolicies = new PolicyList(); + try { + PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); + policies = policyAdministratorPoint.getPolicies(policyType); + targetPolicies.setCount(policies.size()); + filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit); + targetPolicies.setList(filteredPolicies); + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving all available policies"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + + return Response.status(Response.Status.OK).entity(targetPolicies).build(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java index 54d21f1f81..977fd60d38 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java @@ -177,6 +177,12 @@ public class Policy implements Comparable, Serializable { example = "[]") private List deviceGroups; + @ApiModelProperty( + name = "policyType", + value = "Type of the corresponding policy", + required = true, + example = "GENERAL") + private String policyType; @XmlElement public int getId() { @@ -341,6 +347,14 @@ public class Policy implements Comparable, Serializable { this.deviceGroups = deviceGroups; } + public String getPolicyType() { + return policyType; + } + + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + @Override public int compareTo(Policy o) { if (this.priorityId == o.priorityId) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js index c7701e5cd9..032a9aef90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js @@ -458,6 +458,8 @@ var savePolicy = function (policy, isActive, serviceURL) { payload["deviceGroups"] = policy["selectedGroups"]; } + payload["policyType"] = "GENERAL"; + invokerUtil.post( serviceURL, payload, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java index 6b68d4c6b0..8d2e37e60d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java @@ -102,6 +102,18 @@ public class PolicyWrapper { required = true) private List deviceGroups; + @ApiModelProperty(name = "policyType", value = "Type of the corresponding policy", + required = true) + private String policyType; + + public String getPolicyType() { + return policyType; + } + + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + public Profile getProfile() { return profile; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index 413bb473de..53680a8117 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -124,6 +124,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setUsers(policyWrapper.getUsers()); policy.setCompliance(policyWrapper.getCompliance()); policy.setDeviceGroups(policyWrapper.getDeviceGroups()); + policy.setPolicyType(policyWrapper.getPolicyType()); //TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here. List devices = new ArrayList(); List deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index eab6f6214f..e9bdbdc014 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -155,4 +155,11 @@ public interface PolicyAdministratorPoint { List getProfiles() throws PolicyManagementException; int getPolicyCount() throws PolicyManagementException; + + /** + * @param policyType type of the policy + * @return policy list of the specific type + * @throws PolicyManagementException + */ + List getPolicies(String policyType) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java index 8a4db21d7b..ce71b7a213 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java @@ -113,4 +113,13 @@ public interface PolicyCacheManager { * @return - Id of the policy. */ int getPolicyIdOfDevice(int deviceId); + + /** + * This method will return the all policies belongs to the specific type. + * + * @param policyType - type of the policy + * @return - list of policies + * @throws PolicyManagementException + */ + List getAllPolicies(String policyType) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java index 8b0bb71c1c..51c0dc4e81 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java @@ -30,6 +30,7 @@ import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import javax.cache.Cache; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -74,31 +75,13 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { @Override public List getAllPolicies() throws PolicyManagementException { - Cache> lCache = getPolicyListCache(); - if (!lCache.containsKey(1)) { - PolicyManager policyManager = new PolicyManagerImpl(); - this.addAllPolicies(policyManager.getPolicies()); - } + updateCache(lCache); if (log.isDebugEnabled()) { - List cachedPolicy = lCache.get(1); - for (Policy policy : cachedPolicy) { - log.debug("Policy id in cache .. : " + policy.getId() + " policy name : " + policy. - getPolicyName() + " Activated : " + policy.isActive()); - - List users = policy.getUsers(); - for (String user : users) { - log.debug("Users in cached policy : " + user); - } - List roles = policy.getRoles(); - for (String role : roles) { - log.debug("Roles in cached policy : " + role); - } - } - + showDebugLog(lCache); } + lCache = getPolicyListCache(); return lCache.get(1); - } @Override @@ -218,4 +201,47 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { return 0; } + @Override + public List getAllPolicies(String policyType) throws PolicyManagementException { + Cache> lCache = getPolicyListCache(); + updateCache(lCache); + if (log.isDebugEnabled()) { + showDebugLog(lCache); + } + lCache = getPolicyListCache(); + List policyListByType = new ArrayList<>(); + List cachedPolicyList = lCache.get(1); + Iterator iterator = cachedPolicyList.iterator(); + while (iterator.hasNext()) { + Policy policy = iterator.next(); + if (policy.getPolicyType().equals(policyType)) { + policyListByType.add(policy); + } + } + return policyListByType; + } + + private void updateCache(Cache> lCache) throws PolicyManagementException { + if (!lCache.containsKey(1)) { + PolicyManager policyManager = new PolicyManagerImpl(); + this.addAllPolicies(policyManager.getPolicies()); + } + } + + private void showDebugLog(Cache> lCache) { + List cachedPolicy = lCache.get(1); + for (Policy policy : cachedPolicy) { + log.debug("Policy id in cache .. : " + policy.getId() + " policy name : " + policy. + getPolicyName() + " Activated : " + policy.isActive()); + + List users = policy.getUsers(); + for (String user : users) { + log.debug("Users in cached policy : " + user); + } + List roles = policy.getRoles(); + for (String role : roles) { + log.debug("Roles in cached policy : " + role); + } + } + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index bda6bb88c4..2ea593ee4b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -150,4 +150,6 @@ public interface PolicyDAO { HashMap getAppliedPolicyIds() throws PolicyManagerDAOException; HashMap getAppliedPolicyIdsDeviceIds() throws PolicyManagerDAOException; + + List getAllPolicies(String policyType) throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 580c89ecc3..9be99cde93 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -979,21 +979,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); - while (resultSet.next()) { - Policy policy = new Policy(); - policy.setId(resultSet.getInt("ID")); - policy.setProfileId(resultSet.getInt("PROFILE_ID")); - policy.setPolicyName(resultSet.getString("NAME")); - policy.setTenantId(tenantId); - policy.setPriorityId(resultSet.getInt("PRIORITY")); - policy.setCompliance(resultSet.getString("COMPLIANCE")); - policy.setOwnershipType(resultSet.getString("OWNERSHIP_TYPE")); - policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED"))); - policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); - policy.setDescription(resultSet.getString("DESCRIPTION")); - policies.add(policy); - } - return policies; + return this.extractPolicyListFromDbResult(resultSet, tenantId); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { @@ -1437,7 +1423,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," + - "UPDATED, ACTIVE, DESCRIPTION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; + "UPDATED, ACTIVE, DESCRIPTION, POLICY_TYPE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, new String[]{"id"}); stmt.setString(1, policy.getPolicyName()); @@ -1449,6 +1435,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(7, 0); stmt.setInt(8, 0); stmt.setString(9, policy.getDescription()); + stmt.setString(10, policy.getPolicyType()); int affectedRows = stmt.executeUpdate(); @@ -1704,4 +1691,57 @@ public class PolicyDAOImpl implements PolicyDAO { return devicePolicyIds; } + @Override + public List getAllPolicies(String policyType) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + conn = this.getConnection(); + String query = "SELECT ID, " + + "PROFILE_ID, " + + "NAME, " + + "PRIORITY, " + + "COMPLIANCE, " + + "OWNERSHIP_TYPE, " + + "UPDATED, " + + "ACTIVE, " + + "DESCRIPTION, " + + "POLICY_TYPE "+ + "FROM DM_POLICY WHERE TENANT_ID = ? AND POLICY_TYPE = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); + stmt.setString(2, policyType); + resultSet = stmt.executeQuery(); + return this.extractPolicyListFromDbResult(resultSet, tenantId); + } 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); + } + } + + private List extractPolicyListFromDbResult(ResultSet resultSet, int tenantId) throws SQLException { + List policies = new ArrayList<>(); + while (resultSet.next()) { + Policy policy = new Policy(); + policy.setId(resultSet.getInt("ID")); + policy.setProfileId(resultSet.getInt("PROFILE_ID")); + policy.setPolicyName(resultSet.getString("NAME")); + policy.setTenantId(tenantId); + policy.setPriorityId(resultSet.getInt("PRIORITY")); + policy.setCompliance(resultSet.getString("COMPLIANCE")); + policy.setOwnershipType(resultSet.getString("OWNERSHIP_TYPE")); + policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED"))); + policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); + policy.setDescription(resultSet.getString("DESCRIPTION")); + policy.setPolicyType(resultSet.getString("POLICY_TYPE")); + policies.add(policy); + } + return policies; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 9cbd11aefc..c555b27b97 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -377,4 +377,12 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { } } + @Override + public List getPolicies(String policyType) throws PolicyManagementException { + if (policyConfiguration.getCacheEnable()) { + return PolicyCacheManagerImpl.getInstance().getAllPolicies(policyType); + } else { + return policyManager.getPolicies(policyType); + } + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index 68c57b5049..187bbaaae2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -83,4 +83,6 @@ public interface PolicyManager { Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; HashMap getAppliedPolicyIdsDeviceIds() throws PolicyManagementException; + + List getPolicies(String type) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index a656b66ea8..e55b9fee38 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -626,25 +626,7 @@ public class PolicyManagerImpl implements PolicyManager { try { PolicyManagementDAOFactory.openConnection(); policyList = policyDAO.getAllPolicies(); - - for (Policy policy : policyList) { - for (Profile profile : profileList) { - if (policy.getProfileId() == profile.getProfileId()) { - policy.setProfile(profile); - } - } - policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); - policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); - policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policy.getId())); - - List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); - if (!deviceGroupWrappers.isEmpty()) { - deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); - } - policy.setDeviceGroups(deviceGroupWrappers); - - } - Collections.sort(policyList); + this.buildPolicyList(policyList, profileList); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting all the policies.", e); } catch (SQLException e) { @@ -1144,4 +1126,60 @@ public class PolicyManagerImpl implements PolicyManager { return policyRevokeOperation; } + @Override + public List getPolicies(String type) throws PolicyManagementException { + List policyList; + List profileList; + try { + profileList = profileManager.getAllProfiles(); + } catch (ProfileManagementException e) { + throw new PolicyManagementException("Error occurred while getting all the profiles.", e); + } + try { + PolicyManagementDAOFactory.openConnection(); + policyList = policyDAO.getAllPolicies(type); + this.buildPolicyList(policyList, profileList); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while getting all the policies. "; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source "; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (GroupManagementException e) { + String msg = "Error occurred while getting device groups. "; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } finally { + PolicyManagementDAOFactory.closeConnection(); + } + + for (Policy policy : policyList) { + policy.setDevices(this.getPolicyAppliedDevicesIds(policy.getId())); + } + + return policyList; + } + + private void buildPolicyList(List policyList, List profileList) + throws PolicyManagerDAOException, GroupManagementException { + for (Policy policy : policyList) { + for (Profile profile : profileList) { + if (policy.getProfileId() == profile.getProfileId()) { + policy.setProfile(profile); + } + } + policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); + policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); + policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policy.getId())); + + List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); + if (!deviceGroupWrappers.isEmpty()) { + deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); + } + policy.setDeviceGroups(deviceGroupWrappers); + } + Collections.sort(policyList); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index cbc3f5c0bb..833216c231 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -130,6 +130,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { policy1.setDeviceGroups(deviceGroupWrappers); List devices = new ArrayList(); policy1.setDevices(devices); + policy1.setPolicyType("GENERAL"); policy1.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); policy1 = policyManagerService.addPolicy(policy1); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java index 9dc81df195..b4d14788f7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java @@ -40,6 +40,7 @@ public class PolicyCreator { policy.setCompliance("NOTIFY"); policy.setOwnershipType("COPE"); policy.setDescription("This is the first policy."); + policy.setPolicyType("GENERAL"); return policy; } @@ -51,6 +52,7 @@ public class PolicyCreator { policy.setPolicyName("Test_Policy_02"); policy.setGeneric(true); policy.setProfile(profile); + policy.setPolicyType("GENERAL"); policy.setDevices(DeviceCreator.getDeviceList2(DeviceTypeCreator.getDeviceType())); policy.setCompliance("ENFORCE"); @@ -110,7 +112,7 @@ public class PolicyCreator { policy.setRoles(roles); policy.setCompliance("ENFORCE"); policy.setOwnershipType("BYOD"); - + policy.setPolicyType("GENERAL"); PolicyCriterion criterion = new PolicyCriterion(); @@ -144,7 +146,7 @@ public class PolicyCreator { policy.setCompliance("MONITOR"); policy.setOwnershipType("BYOD"); - + policy.setPolicyType("GENERAL"); List roles = new ArrayList(); roles.add("Role_04"); roles.add("Role_05"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 6c77071c0c..379d4a4652 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -200,6 +200,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( PRIORITY INT NOT NULL, ACTIVE INT(2) NOT NULL, UPDATED INT(1) NULL, + POLICY_TYPE VARCHAR(45) NULL, PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 7d0cff5556..145c43028d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -193,6 +193,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( PRIORITY INT NOT NULL, ACTIVE INT(2) NOT NULL, UPDATED INT(1) NULL, + POLICY_TYPE VARCHAR(45) NULL, PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 4a3882f06a..40199a33c5 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -246,6 +246,7 @@ CREATE TABLE DM_POLICY ( PRIORITY INTEGER NOT NULL, ACTIVE BIT NOT NULL DEFAULT 0, UPDATED BIT NULL DEFAULT 0, + POLICY_TYPE VARCHAR(45) NULL, PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID) REFERENCES DM_PROFILE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 9d1f0bd4e4..677fa2e0a0 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -224,6 +224,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( PRIORITY INT NOT NULL, ACTIVE INT(2) NOT NULL, UPDATED INT(1) NULL, + POLICY_TYPE VARCHAR(45) NULL, PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 9b5cc5dc1b..ab76a8de5b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -357,6 +357,7 @@ CREATE TABLE DM_POLICY ( PRIORITY NUMBER(10) NOT NULL, ACTIVE NUMBER(10) NOT NULL, UPDATED NUMBER(10) NULL, + POLICY_TYPE VARCHAR2(45) NULL, CONSTRAINT PK_DM_PROFILE_DM_POLICY PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 02d717f7e9..859111374e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -194,6 +194,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( PRIORITY INTEGER NOT NULL, ACTIVE INTEGER NOT NULL, UPDATED INTEGER NULL, + POLICY_TYPE VARCHAR(45) NULL, CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) REFERENCES DM_PROFILE (ID )