From 4e61f771e719094ae4b188efa7c42fb11a78d9c8 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 12:58:48 +0530 Subject: [PATCH 01/26] Add corrective action bean class --- .../common/policy/mgt/CorrectiveAction.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java new file mode 100644 index 0000000000..dece7db618 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.mgt.common.policy.mgt; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; +import java.util.List; + +@ApiModel( + value = "CorrectiveAction", + description = "This bean carries all information related corrective action which is required " + + "when a policy is violated." +) +public class CorrectiveAction implements Serializable { + + private static final long serialVersionUID = -3414709449056070148L; + + @ApiModelProperty( + name = "action", + value = "Corrective action type (POLICY or OPERATION) to trigger when a policy is violated.", + example = "POLICY", + required = true + ) + private String actionType; + + @ApiModelProperty( + name = "policyId", + value = "When corrective action is POLICY, the corrective policy ID to be applied when a policy " + + "is violated.", + example = "1" + ) + private int policyId; + + @ApiModelProperty( + name = "operations", + value = "When corrective action is OPERATION, the list of operations in features to be applied " + + "when a policy is violated." + ) + private List operations; + + public String getActionType() { + return actionType; + } + + public void setActionType(String actionType) { + this.actionType = actionType; + } + + public int getPolicyId() { + return policyId; + } + + public void setPolicyId(int policyId) { + this.policyId = policyId; + } + + public List getOperations() { + return operations; + } + + public void setOperations(List operations) { + this.operations = operations; + } +} From 4e06e95039b362e9b8093472c24341997043a8c0 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 12:59:24 +0530 Subject: [PATCH 02/26] Add correctiveAction to policy bean class --- .../device/mgt/common/policy/mgt/Policy.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) 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 977fd60d38..452521286b 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 @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.mgt.common.policy.mgt; @@ -184,6 +201,13 @@ public class Policy implements Comparable, Serializable { example = "GENERAL") private String policyType; + @ApiModelProperty( + name = "correctiveActions", + value = "List of corrective actions to be applied when the policy is violated", + example = "[{'actionType': 'POLICY', 'policyId': 1}]" + ) + private List correctiveActions; + @XmlElement public int getId() { return id; @@ -355,6 +379,16 @@ public class Policy implements Comparable, Serializable { this.policyType = policyType; } + @XmlElement + public List getCorrectiveActions() { + return correctiveActions; + } + + public void setCorrectiveActions( + List correctiveActions) { + this.correctiveActions = correctiveActions; + } + @Override public int compareTo(Policy o) { if (this.priorityId == o.priorityId) From bb1a3f6230267bfd840a1f933c73ea9f884b96ba Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 13:34:14 +0530 Subject: [PATCH 03/26] Add correctiveAction to policy wrapper bean class --- .../device/mgt/jaxrs/beans/PolicyWrapper.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) 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 a5306c825f..92e07aeab1 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 @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.mgt.jaxrs.beans; @@ -21,6 +38,7 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper; import javax.validation.constraints.NotNull; @@ -108,6 +126,12 @@ public class PolicyWrapper { @NotNull private String policyType; + @ApiModelProperty( + name = "correctiveActions", + value = "List of corrective actions to be applied when the policy is violated" + ) + private List correctiveActions; + public String getPolicyType() { return policyType; } @@ -196,4 +220,11 @@ public class PolicyWrapper { this.deviceGroups = deviceGroups; } + public List getCorrectiveActions() { + return correctiveActions; + } + + public void setCorrectiveActions(List correctiveActions) { + this.correctiveActions = correctiveActions; + } } From 622e1842de82b37ac443e9c66bf625f567aa50c5 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 13:39:14 +0530 Subject: [PATCH 04/26] Add corrective actions in policy wrapper to policy object --- .../impl/PolicyManagementServiceImpl.java | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) 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 b5b92e7a0c..5f9f9c9063 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 @@ -1,19 +1,36 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2016, 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 + * 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 + * 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. + * 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. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. * */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; @@ -117,6 +134,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setPolicyName(policyWrapper.getPolicyName()); policy.setDescription(policyWrapper.getDescription()); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile())); + policy.setCorrectiveActions(policyWrapper.getCorrectiveActions()); policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setActive(policyWrapper.isActive()); policy.setRoles(policyWrapper.getRoles()); From ff0de037b684ce06695ea83fce505f6c81532a5f Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 15:26:08 +0530 Subject: [PATCH 05/26] Add DAO methods and implementation of policy corrective actions --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 53 ++++++++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 125 ++++++++++++++++++ 2 files changed, 178 insertions(+) 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 2ea593ee4b..ec2d89cd71 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 @@ -14,11 +14,29 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.policy.mgt.core.dao; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; @@ -53,6 +71,41 @@ public interface PolicyDAO { */ Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException; + /** + * This method is used to add corrective actions of policy in the database based on the policy ID + * @param correctiveActions list of corrective actions to be added to database + * @param policyId is used uniquely identify the policy to which corrective actions are to be added + * @throws PolicyManagerDAOException is thrown when there is an error in adding corrective actions to database + */ + void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + throws PolicyManagerDAOException; + + /** + * This method is used to retrieve corrective actions of policy from the database based on the policy ID + * @param policyId is used uniquely identify the policy from which corrective actions are retrieved + * @return list of retrieved {@link CorrectiveAction} + * @throws PolicyManagerDAOException is thrown when there is an error in retrieving corrective actions to database + */ + List getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException; + + /** + * This method is used to update corrective actions of policy in the database based on the policy ID + * @param correctiveActions list of corrective actions to be updated in the database + * @param policyId is used uniquely identify the policy to which corrective actions are to be updated + * @throws PolicyManagerDAOException is thrown when there is an error in updating corrective actions to database + */ + void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + throws PolicyManagerDAOException; + + /** + * This method is used to delete corrective actions of policy in the database based on the policy ID + * @param correctiveActions list of corrective actions to be deleted in the database + * @param policyId is used uniquely identify the policy to which corrective actions are to be deleted + * @throws PolicyManagerDAOException is thrown when there is an error in deleting corrective actions to database + */ + void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + throws PolicyManagerDAOException; + Policy updateUserOfPolicy(List usersToAdd, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToDevice(List devices, Policy policy) 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 9be99cde93..00929e652d 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 @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.policy.mgt.core.dao.impl; @@ -22,6 +39,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; @@ -244,6 +262,108 @@ public class PolicyDAOImpl implements PolicyDAO { return policy; } + @Override + public void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + throws PolicyManagerDAOException { + try { + Connection conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_CORRECTIVE_ACTION " + + "(ACTION_TYPE, " + + "CORRECTIVE_POLICY_ID, " + + "POLICY_ID) VALUES (?, ?, ?)"; + try (PreparedStatement insertStmt = conn.prepareStatement(query)) { + for (CorrectiveAction correctiveAction : correctiveActions) { + insertStmt.setString(1, correctiveAction.getActionType()); + insertStmt.setInt(2, correctiveAction.getPolicyId()); + insertStmt.setInt(3, policyId); + insertStmt.addBatch(); + } + insertStmt.executeBatch(); + } + } catch (SQLException e) { + String msg = "Error occurred while adding corrective actions of policy ID " + policyId; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + @Override + public List getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException { + try { + Connection conn = this.getConnection(); + String query = "SELECT " + + "ACTION_TYPE, " + + "CORRECTIVE_POLICY_ID " + + "FROM DM_POLICY_CORRECTIVE_ACTION " + + "WHERE POLICY_ID = ?"; + try (PreparedStatement selectStmt = conn.prepareStatement(query)) { + List correctiveActions = new ArrayList<>(); + selectStmt.setInt(1, policyId); + try (ResultSet rs = selectStmt.executeQuery()) { + CorrectiveAction correctiveAction; + while (rs.next()) { + correctiveAction = new CorrectiveAction(); + correctiveAction.setActionType(rs.getString("ACTION_TYPE")); + correctiveAction.setPolicyId(rs.getInt("CORRECTIVE_POLICY_ID")); + correctiveActions.add(correctiveAction); + } + } + return correctiveActions; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving corrective actions of policy ID " + policyId; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + @Override + public void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + throws PolicyManagerDAOException { + try { + Connection conn = this.getConnection(); + String query = "UPDATE DM_POLICY_CORRECTIVE_ACTION " + + "SET CORRECTIVE_POLICY_ID = ? " + + "WHERE ACTION_TYPE = ? " + + "AND POLICY_ID = ?"; + try (PreparedStatement updateStmt = conn.prepareStatement(query)) { + for (CorrectiveAction correctiveAction : correctiveActions) { + updateStmt.setInt(1, correctiveAction.getPolicyId()); + updateStmt.setString(2, correctiveAction.getActionType()); + updateStmt.setInt(3, policyId); + updateStmt.addBatch(); + } + updateStmt.executeBatch(); + } + } catch (SQLException e) { + String msg = "Error occurred while updating corrective actions of policy ID " + policyId; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + @Override + public void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + throws PolicyManagerDAOException { + try { + Connection conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION " + + "WHERE ACTION_TYPE = ? " + + "AND POLICY_ID = ?"; + try (PreparedStatement deleteStmt = conn.prepareStatement(query)) { + for (CorrectiveAction correctiveAction : correctiveActions) { + deleteStmt.setString(1, correctiveAction.getActionType()); + deleteStmt.setInt(2, policyId); + deleteStmt.addBatch(); + } + deleteStmt.executeBatch(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting corrective actions of policy ID " + policyId; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } @Override public Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException { @@ -1352,6 +1472,11 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, policyId); stmt.executeUpdate(); + String deleteCorrectiveActions = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(deleteCorrectiveActions); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + if (log.isDebugEnabled()) { log.debug("Policy (" + policyId + ") related configs deleted from database."); } From f82b54312c65bcfaf0c0a02ffa8d25b8a2e14a30 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 15:45:07 +0530 Subject: [PATCH 06/26] Add implementation of corrective actions in policy service --- .../mgt/core/dao/impl/PolicyDAOImpl.java | 3 + .../mgt/core/mgt/impl/PolicyManagerImpl.java | 116 +++++++++++++++--- 2 files changed, 104 insertions(+), 15 deletions(-) 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 00929e652d..23a32a1a6b 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 @@ -1472,6 +1472,9 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, policyId); stmt.executeUpdate(); + if (log.isDebugEnabled()) { + log.debug("Deleting corrective actions of policy ID " + policyId); + } String deleteCorrectiveActions = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(deleteCorrectiveActions); stmt.setInt(1, policyId); 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 e2ef347269..4147327197 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 @@ -1,19 +1,36 @@ /* -* 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. + * 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. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.policy.mgt.core.mgt.impl; @@ -28,6 +45,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; 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.policy.mgt.CorrectiveAction; 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; @@ -124,6 +142,14 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } + if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("Adding corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); + } + policyDAO.addCorrectiveActionsOfPolicy(policy.getCorrectiveActions(), policy.getId()); + } + if (policy.isActive()) { policyDAO.activatePolicy(policy.getId()); } @@ -248,6 +274,55 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } + List updatedCorrectiveActions = policy.getCorrectiveActions(); + List existingCorrectiveActions = previousPolicy.getCorrectiveActions(); + List correctiveActionsToUpdate = new ArrayList<>(); + List correctiveActionsToDelete = new ArrayList<>(); + List correctiveActionsToAdd = new ArrayList<>(); + List correctiveActionTypesToUpdate = new ArrayList<>(); + List existingCorrectiveActionTypes = new ArrayList<>(); + + if (updatedCorrectiveActions != null) { + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) { + correctiveActionsToUpdate.add(updatedCorrectiveAction); + existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); + } + } + correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); + } + + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) { + correctiveActionsToAdd.add(updatedCorrectiveAction); + } + } + } + + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { + correctiveActionsToDelete.add(existingCorrectiveAction); + } + } + + if (log.isDebugEnabled()) { + log.debug("Updating corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); + } + + if (!correctiveActionsToUpdate.isEmpty()) { + policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, previousPolicy.getId()); + } + + if (!correctiveActionsToAdd.isEmpty()) { + policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, previousPolicy.getId()); + } + + if (!correctiveActionsToDelete.isEmpty()) { + policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, previousPolicy.getId()); + } + PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -578,6 +653,12 @@ public class PolicyManagerImpl implements PolicyManager { policy.setRoles(roleNames); policy.setUsers(userNames); + if (log.isDebugEnabled()) { + log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); + } + policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId)); + } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + policyId + ")", e); @@ -1179,6 +1260,11 @@ public class PolicyManagerImpl implements PolicyManager { deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); } policy.setDeviceGroups(deviceGroupWrappers); + if (log.isDebugEnabled()) { + log.debug("Retrieving corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); + } + policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policy.getId())); } Collections.sort(policyList); } From 1c36eec841874bd04ad51165f3842da2b4b03717 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 18:20:12 +0530 Subject: [PATCH 07/26] Add constants of corrective action policy --- .../core/util/PolicyManagementConstants.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index c680789c82..e945f98127 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.policy.mgt.core.util; @@ -45,6 +62,11 @@ public final class PolicyManagementConstants { public static final String DELEGATION_TASK_NAME = "DELEGATION"; public static final String DELEGATION_TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask"; + public static final String GENERAL_POLICY_TYPE = "GENERAL"; + public static final String CORRECTIVE_POLICY_TYPE = "CORRECTIVE"; + public static final String POLICY_CORRECTIVE_ACTION_TYPE = "POLICY"; + public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY"; + /** Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME, and so on. Any caller should be prevented from constructing objects of From 8bf894110373bb110a14a877ab8e0538e34741ef Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 18:21:49 +0530 Subject: [PATCH 08/26] Modify transform policy method to support retrieving corrective policy as a feature --- .../mgt/core/util/PolicyManagerUtil.java | 79 +++++++++++++++++-- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 67a53f2d30..0d31dc2e07 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -14,10 +14,28 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.policy.mgt.core.util; +import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; @@ -28,17 +46,21 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; 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.config.tenant.PlatformConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.policy.mgt.common.PolicyTransformException; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import javax.cache.Cache; import javax.cache.CacheManager; @@ -116,28 +138,71 @@ public class PolicyManagerUtil { return buff.toString(); } - public static Operation transformPolicy(Policy policy) { + public static Operation transformPolicy(Policy policy) throws PolicyTransformException { List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); - List profileOperationList = new ArrayList(); PolicyOperation policyOperation = new PolicyOperation(); policyOperation.setEnabled(true); policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY); policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE); + if (policy.getPolicyType() != null && + PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType()) && + policy.getCorrectiveActions() != null) { + for (CorrectiveAction correctiveAction : policy.getCorrectiveActions()) { + if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE + .equalsIgnoreCase(correctiveAction.getActionType())) { + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); + try { + Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId()); + if (correctivePolicy == null || PolicyManagementConstants.CORRECTIVE_POLICY_TYPE + .equalsIgnoreCase(correctivePolicy.getPolicyType())) { + String msg = "No corrective policy was found for the policy " + policy.getPolicyName() + + " and policy ID " + policy.getId(); + log.error(msg); + throw new PolicyTransformException(msg); + } else { + List correctiveProfileOperations = createProfileOperations( + correctivePolicy.getProfile().getProfileFeaturesList()); + ProfileFeature correctivePolicyFeature = new ProfileFeature(); + correctivePolicyFeature.setProfileId(correctivePolicy.getProfileId()); + correctivePolicyFeature.setContent(new Gson().toJson(correctiveProfileOperations)); + correctivePolicyFeature.setDeviceType(correctivePolicy.getProfile().getDeviceType()); + correctivePolicyFeature.setFeatureCode( + PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE); + correctivePolicyFeature.setId(correctivePolicy.getId()); + effectiveFeatures.add(correctivePolicyFeature); + } + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving corrective policy for policy " + + policy.getPolicyName() + " and policy ID " + policy.getId(); + log.error(msg, e); + throw new PolicyTransformException(msg, e); + } + // Currently only supported POLICY corrective action type so the break is added. This should be + // removed when we start supporting other corrective action types + break; + } + } + } + + policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures)); + policyOperation.setPayLoad(policyOperation.getProfileOperations()); + return policyOperation; + } + + public static List createProfileOperations(List effectiveFeatures) { + List profileOperations = new ArrayList<>(); for (ProfileFeature feature : effectiveFeatures) { ProfileOperation profileOperation = new ProfileOperation(); - profileOperation.setCode(feature.getFeatureCode()); profileOperation.setEnabled(true); profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); profileOperation.setPayLoad(feature.getContent()); - profileOperationList.add(profileOperation); + profileOperations.add(profileOperation); } - policyOperation.setProfileOperations(profileOperationList); - policyOperation.setPayLoad(policyOperation.getProfileOperations()); - return policyOperation; + return profileOperations; } From 1e0ca09f1298069f1a32600388fe6ad3d7dc6c0d Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 18:24:53 +0530 Subject: [PATCH 09/26] Handle policy transform exception --- .../mgt/common/PolicyTransformException.java | 39 ++++++++++++++ .../mgt/core/PolicyManagerServiceImpl.java | 54 +++++++++++++------ .../PolicyEnforcementDelegatorImpl.java | 47 +++++++++++----- 3 files changed, 112 insertions(+), 28 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTransformException.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTransformException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTransformException.java new file mode 100644 index 0000000000..720986a39a --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTransformException.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.policy.mgt.common; + +public class PolicyTransformException extends Exception { + + private static final long serialVersionUID = -4976670117832668628L; + + public PolicyTransformException(String message, Throwable cause) { + super(message, cause); + } + + public PolicyTransformException(Throwable cause) { + super(cause); + } + + public PolicyTransformException(String msg) { + super(msg); + } + + public PolicyTransformException() { + super(); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 11c2f31721..fd3b9f019d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -1,20 +1,37 @@ /* -* 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. -*/ + * 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. + * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.policy.mgt.core; @@ -134,6 +151,11 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); throw new PolicyManagementException(msg, e); + } catch (PolicyTransformException e) { + String msg = "Error occurred while transforming policy object to operation object type for device " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyManagementException(msg, e); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java index 03caa2b001..3981c4c4d5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java @@ -1,20 +1,36 @@ /* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * 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 + * 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 + * 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. + * 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. * + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.policy.mgt.core.enforcement; @@ -31,6 +47,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.PolicyTransformException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; @@ -136,6 +153,12 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato String msg = "Error occurred while adding the operation to device."; log.error(msg, e); throw new PolicyDelegationException(msg, e); + } catch (PolicyTransformException e) { + String msg = "Error occurred while transforming policy object to operation object type for policy " + + policy.getId() + " - " + policy.getPolicyName() + " for devices " + + deviceIdentifiers.toString(); + log.error(msg, e); + throw new PolicyDelegationException(msg, e); } } From ca2a1b85c30c0a7a6536c14414a1e12d5cc31375 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 19:20:12 +0530 Subject: [PATCH 10/26] Add UI controller method to retrieve all policies by policy type --- .../modules/business-controllers/policy.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js index 147b664ba6..8c881ef3df 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js @@ -62,6 +62,8 @@ policyModule = function () { policyObjectToView["priorityId"] = policyObjectFromRestEndpoint["priorityId"]; policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"]; policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"]; + policyObjectFromRestEndpoint["policyType"] = policyListFromRestEndpoint["policyType"]; + policyObjectFromRestEndpoint["correctiveActions"] = policyListFromRestEndpoint["correctiveActions"]; if (policyObjectToView["platform"] == "ios") { policyObjectToView["deviceTypeIcon"] = "apple"; } else { @@ -168,6 +170,27 @@ policyModule = function () { } }; + /** + * Retrieve all policies based on policy type + */ + publicMethods.getAllPoliciesByType = function (policyType) { + var carbonUser = session.get(constants["USER_SESSION_KEY"]); + if (!carbonUser) { + log.error("User object was not found in the session"); + userModule.logout(function () { + response.sendRedirect(devicemgtProps["appContext"] + "login"); + }); + } + try { + var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + + "/policies/type/" + policyType + "?offset=0&limit=100"; + return serviceInvokers.XMLHttp.get(url, privateMethods.handleGetAllPoliciesResponse); + } catch (e) { + log.error("Error occurred while retrieving policies by policy type " + policyType); + throw e; + } + }; + /* Get policies count from backend services. */ From 62c01aa508cc7154bd34393a583ff57def3703a0 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 19:21:59 +0530 Subject: [PATCH 11/26] Add a new unit for corrective action --- .../corrective-action.json | 3 +++ .../public/js/policy-corrective-action.js | 25 +++++++++++++++++++ .../templates/policy-corrective-action.hbs | 24 ++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/corrective-action.json create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/js/policy-corrective-action.js create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/templates/policy-corrective-action.hbs diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/corrective-action.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/corrective-action.json new file mode 100644 index 0000000000..fd25901297 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/corrective-action.json @@ -0,0 +1,3 @@ +{ + "version" : "1.0.0" +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/js/policy-corrective-action.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/js/policy-corrective-action.js new file mode 100644 index 0000000000..7af005f2ce --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/js/policy-corrective-action.js @@ -0,0 +1,25 @@ +/* + * + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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. + */ + + +$(document).ready(function () { + + $("select.select2").select2(); + +}); \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/templates/policy-corrective-action.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/templates/policy-corrective-action.hbs new file mode 100644 index 0000000000..fa3da3680d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.corrective-action/public/templates/policy-corrective-action.hbs @@ -0,0 +1,24 @@ + +{{#equal deviceType "android"}} +
+
+
+ Select Corrective Policy + + + + + +
+
+
+
+{{/equal}} \ No newline at end of file From 1d2444f16fc8e539ffba4bfadafe25a23e980c3c Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 19:31:22 +0530 Subject: [PATCH 12/26] Add policy type and corrective action UI for create policy --- .../units/cdmf.unit.policy.create/create.hbs | 70 +++++++++++++++---- .../units/cdmf.unit.policy.create/create.js | 2 + .../public/js/policy-create.js | 67 +++++++++++++++++- 3 files changed, 124 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs index e3f6cdfde9..c67f099c5b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs @@ -2,36 +2,44 @@ {{#if isAuthorized}}