From f82b54312c65bcfaf0c0a02ffa8d25b8a2e14a30 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 17 Oct 2019 15:45:07 +0530 Subject: [PATCH] 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 00929e652d4..23a32a1a6b3 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 e2ef347269e..41473271974 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); }