From bcb2c1ddb12b33fd8b417e3b59e9114282e056c8 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Sat, 29 Aug 2015 17:39:07 +0530 Subject: [PATCH] Commiting the monitoring changes and test case changes --- .../mgt/core/dao/impl/DeviceDAOImpl.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 4 + .../org.wso2.carbon.policy.mgt.core/pom.xml | 9 ++ .../mgt/core/enforcement/DelegationTask.java | 94 +++++++++++++++ .../PolicyEnforcementDelegator.java | 2 - .../PolicyEnforcementDelegatorImpl.java | 11 +- .../impl/ComplianceDecisionPointImpl.java | 6 +- .../impl/PolicyAdministratorPointImpl.java | 111 +++++++++++------- .../core/impl/PolicyInformationPointImpl.java | 36 ++++-- .../PolicyManagementServiceComponent.java | 5 + .../core/mgt/impl/MonitoringManagerImpl.java | 90 ++++++++------ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 32 ++--- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 47 ++++++-- .../policy/mgt/core/task/MonitoringTask.java | 86 ++++++++------ .../core/task/TaskScheduleServiceImpl.java | 19 ++- .../core/util/PolicyManagementConstants.java | 10 +- .../mgt/core/BasePolicyManagementDAOTest.java | 17 ++- .../policy/mgt/core/MonitoringTestCase.java | 20 ++-- .../policy/mgt/core/PolicyDAOTestCase.java | 36 ++---- .../mgt/core/PolicyEvaluationTestCase.java | 28 +++-- .../repository/conf/etc/tasks-config.xml | 51 ++++++++ .../config/datasource/data-source-config.xml | 16 +-- 22 files changed, 506 insertions(+), 226 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 6d8d4107bf..69c297d7f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -179,7 +179,7 @@ public class DeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME " + + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME " + "AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 197a9321e3..0d47afaa18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -492,10 +492,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { Device device; try { + DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, this.getTenantId()); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + "'" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for id " + + "'" + deviceId.getId() + "'", e); } finally { DeviceManagementDAOFactory.closeConnection(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index c818c48bef..953650fd8e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -144,6 +144,15 @@ org.wso2.carbon.ntask.core + + + org.wso2.carbon + org.wso2.carbon.registry.api + + + org.wso2.carbon + org.wso2.carbon.registry.core + org.wso2.carbon.devicemgt diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java new file mode 100644 index 0000000000..33ce6bd606 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -0,0 +1,94 @@ +/* + * 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. + */ + + +package org.wso2.carbon.policy.mgt.core.enforcement; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.ntask.core.Task; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; +import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DelegationTask implements Task { + + private static final Log log = LogFactory.getLog(DelegationTask.class); + + @Override + public void setProperties(Map map) { + + } + + @Override + public void init() { + + } + + @Override + public void execute() { + + try { + PolicyManager policyManager = new PolicyManagerImpl(); + List deviceTypes = policyManager.applyChangesMadeToPolicies(); + + if (log.isDebugEnabled()) { + log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size()); + } + if (!deviceTypes.isEmpty()) { + DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() + .getDeviceManagementService(); + List devices = new ArrayList<>(); + for (DeviceType deviceType : deviceTypes) { + try { + devices.addAll(service.getAllDevices(deviceType.getName())); + } catch (DeviceManagementException e) { + throw new PolicyManagementException("Error occurred while taking the devices", e); + } + } + HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); + List toBeNotified = new ArrayList<>(); + + for (Device device : devices) { + if (deviceIdPolicy.containsKey(device.getId())) { + toBeNotified.add(device); + } + } + if (!toBeNotified.isEmpty()) { + PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); + enforcementDelegator.delegate(); + } + } + + } catch (PolicyManagementException e) { + log.error("Error occurred while getting the policies applied to devices.", e); + } catch (PolicyDelegationException e) { + log.error("Error occurred while running the delegation task.", e); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java index 9ffb0c9e3d..218fe0398e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java @@ -18,11 +18,9 @@ */ package org.wso2.carbon.policy.mgt.core.enforcement; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Policy; -import java.util.HashMap; import java.util.List; public interface PolicyEnforcementDelegator { 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 fa7d1f478f..3e3b605c85 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 @@ -30,13 +30,12 @@ import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; 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; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.util.ArrayList; import java.util.List; -public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegator, Runnable { +public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegator{ private static final Log log = LogFactory.getLog(PolicyEnforcementDelegatorImpl.class); @@ -108,12 +107,4 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato } - @Override - public void run() { - try { - this.delegate(); - } catch (PolicyDelegationException e) { - log.error("Error occurred while running the policy change delegation thread.", e); - } - } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java index 971d2e268a..d78e6b16e2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java @@ -166,10 +166,8 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { } } catch (OperationManagementException e) { - String msg = "Error occurred while re-enforcing the policy to device " + deviceIdentifier.getId() + " - " + - deviceIdentifier.getType(); - log.error(msg, e); - throw new PolicyComplianceException(msg, e); + throw new PolicyComplianceException("Error occurred while re-enforcing the policy to device " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(), e); } } 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 bec741f8ff..c6680d1a74 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 @@ -20,19 +20,13 @@ package org.wso2.carbon.policy.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.Profile; -import org.wso2.carbon.policy.mgt.common.ProfileManagementException; -import org.wso2.carbon.policy.mgt.core.enforcement.PolicyEnforcementDelegator; -import org.wso2.carbon.policy.mgt.core.enforcement.PolicyEnforcementDelegatorImpl; +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.TaskInfo; +import org.wso2.carbon.ntask.core.TaskManager; +import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; @@ -40,10 +34,11 @@ import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @@ -111,39 +106,75 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public void publishChanges() throws PolicyManagementException { - List deviceTypes = policyManager.applyChangesMadeToPolicies(); + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); + taskService.registerTaskType(PolicyManagementConstants.DELEGATION_TASK_TYPE); - if(log.isDebugEnabled()) { - log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size() ); - } + if (log.isDebugEnabled()) { + log.debug("Policy delegations task is started for the tenant id " + tenantId); + } - if (!deviceTypes.isEmpty()) { + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.DELEGATION_TASK_TYPE); + TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() - .getDeviceManagementService(); - List devices = new ArrayList<>(); - for (DeviceType deviceType : deviceTypes) { - try { - devices.addAll(service.getAllDevices(deviceType.getName())); - } catch (DeviceManagementException e) { - throw new PolicyManagementException("Error occurred while taking the devices", e); - } - } - HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); - List toBeNotified = new ArrayList<>(); + triggerInfo.setIntervalMillis(0); + triggerInfo.setRepeatCount(1); - for (Device device : devices) { - if (deviceIdPolicy.containsKey(device.getId())) { - toBeNotified.add(device); - } - } - if (!toBeNotified.isEmpty()) { - // PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); - Thread thread = new Thread(new PolicyEnforcementDelegatorImpl(toBeNotified)); - thread.start(); - } + Map properties = new HashMap<>(); + properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); + + String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId); + + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); + + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); + + + } catch (TaskException e) { + String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(); + log.error(msg, e); + throw new PolicyManagementException(msg, e); } + +// List deviceTypes = policyManager.applyChangesMadeToPolicies(); +// +// if(log.isDebugEnabled()) { +// log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size() ); +// } +// +// if (!deviceTypes.isEmpty()) { +// +// +// DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() +// .getDeviceManagementService(); +// List devices = new ArrayList<>(); +// for (DeviceType deviceType : deviceTypes) { +// try { +// devices.addAll(service.getAllDevices(deviceType.getName())); +// } catch (DeviceManagementException e) { +// throw new PolicyManagementException("Error occurred while taking the devices", e); +// } +// } +// HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); +// List toBeNotified = new ArrayList<>(); +// +// for (Device device : devices) { +// if (deviceIdPolicy.containsKey(device.getId())) { +// toBeNotified.add(device); +// } +// } +// if (!toBeNotified.isEmpty()) { +// +// // ExecutorService executorService = getExecutor(); +// // PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); +//// Thread thread = new Thread(new PolicyEnforcementDelegatorImpl(toBeNotified)); +//// thread.start(); +// } +// } } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index 5a335094f6..d3591b28e5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -61,26 +61,36 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { @Override public PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { PIPDevice pipDevice = new PIPDevice(); - org.wso2.carbon.device.mgt.common.Device device; + Device device; DeviceType deviceType = new DeviceType(); deviceType.setName(deviceIdentifier.getType()); - deviceManagementService = getDeviceManagementService(); + DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl(); try { device = deviceManagementService.getDevice(deviceIdentifier); + Thread.currentThread(); + + if (device != null) { /*deviceManagementService.getDeviceType(deviceIdentifier.getType());*/ - pipDevice.setDevice(device); - pipDevice.setRoles(getRoleOfDevice(device)); - pipDevice.setDeviceType(deviceType); - pipDevice.setDeviceIdentifier(deviceIdentifier); - pipDevice.setUserId(device.getEnrolmentInfo().getOwner()); - pipDevice.setOwnershipType(device.getEnrolmentInfo().getOwnership().toString()); - - // TODO : Find a way to retrieve the timestamp and location (lat, long) of the device - // pipDevice.setLongitude(); - // pipDevice.setAltitude(); - // pipDevice.setTimestamp(); + pipDevice.setDevice(device); + pipDevice.setRoles(getRoleOfDevice(device)); + pipDevice.setDeviceType(deviceType); + pipDevice.setDeviceIdentifier(deviceIdentifier); + pipDevice.setUserId(device.getEnrolmentInfo().getOwner()); + pipDevice.setOwnershipType(device.getEnrolmentInfo().getOwnership().toString()); + + // TODO : Find a way to retrieve the timestamp and location (lat, long) of the device + // pipDevice.setLongitude(); + // pipDevice.setAltitude(); + // pipDevice.setTimestamp(); + } else { + // Remove this + for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { + log.debug("StackTraceElement : " + ste); + } + throw new PolicyManagementException("Device details cannot be null."); + } } catch (DeviceManagementException e) { String msg = "Error occurred when retrieving the data related to device from the database."; log.error(msg, e); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index f0b98fa6f6..676595230b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -31,6 +31,8 @@ import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; import org.wso2.carbon.policy.mgt.core.config.PolicyManagementConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; +import org.wso2.carbon.policy.mgt.core.task.TaskScheduleServiceImpl; import org.wso2.carbon.user.core.service.RealmService; /** @@ -82,6 +84,9 @@ public class PolicyManagementServiceComponent { componentContext.getBundleContext().registerService( PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); + TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl(); + taskScheduleService.startTask(30000); + } catch (Throwable t) { log.error("Error occurred while initializing the Policy management core.", t); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 70e45127d3..b0ba65a2a8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -105,13 +105,19 @@ public class MonitoringManagerImpl implements MonitoringManager { //This was added because update query below that did not return the update table primary key. if (complianceFeatures != null && !complianceFeatures.isEmpty()) { - PolicyManagementDAOFactory.beginTransaction(); - monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); - if (log.isDebugEnabled()) { - log.debug("Compliance status primary key " + complianceData.getId()); + try { + PolicyManagementDAOFactory.beginTransaction(); + monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); + if (log.isDebugEnabled()) { + log.debug("Compliance status primary key " + complianceData.getId()); + } + monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), + complianceFeatures); + + PolicyManagementDAOFactory.commitTransaction(); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures); - PolicyManagementDAOFactory.commitTransaction(); complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData); List profileFeatures = policy.getProfile().getProfileFeaturesList(); for (ComplianceFeature compFeature : complianceFeatures) { @@ -122,10 +128,14 @@ public class MonitoringManagerImpl implements MonitoringManager { } } } else { - PolicyManagementDAOFactory.beginTransaction(); - monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); - monitoringDAO.deleteNoneComplianceData(complianceData.getId()); - PolicyManagementDAOFactory.commitTransaction(); + try { + PolicyManagementDAOFactory.beginTransaction(); + monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); + monitoringDAO.deleteNoneComplianceData(complianceData.getId()); + PolicyManagementDAOFactory.commitTransaction(); + } finally { + PolicyManagementDAOFactory.closeConnection(); + } } } else { if (log.isDebugEnabled()) { @@ -144,8 +154,6 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); - } finally { - PolicyManagementDAOFactory.closeConnection(); } return complianceFeatures; } @@ -206,28 +214,39 @@ public class MonitoringManagerImpl implements MonitoringManager { @Override public void addMonitoringOperation(List devices) throws PolicyComplianceException { - try { - ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); - //int tenantId = PolicyManagerUtil.getTenantId(); - Map deviceIds = new HashMap<>(); + ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); - for (Device device : devices) { - deviceIds.put(device.getId(), device); - } + //int tenantId = PolicyManagerUtil.getTenantId(); + Map deviceIds = new HashMap<>(); + List complianceDatas; + HashMap devicePolicyIdMap; - List deviceIDs = new ArrayList<>(deviceIds.keySet()); - List complianceDatas = monitoringDAO.getCompliance(deviceIDs); - HashMap devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs); + for (Device device : devices) { + deviceIds.put(device.getId(), device); + } - Map deviceIdsToAddOperation = new HashMap<>(); - Map deviceIdsWithExistingOperation = new HashMap<>(); - Map inactiveDeviceIds = new HashMap<>(); - Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); + List deviceIDs = new ArrayList<>(deviceIds.keySet()); + try { + PolicyManagementDAOFactory.openConnection(); + complianceDatas = monitoringDAO.getCompliance(deviceIDs); + devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs); + } catch (SQLException e) { + throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); + } catch (MonitoringDAOException e) { + throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); + } catch (PolicyManagerDAOException e) { + throw new PolicyComplianceException("SQL error occurred while getting policy details.", e); + } - Map tempMap = new HashMap<>(); + Map deviceIdsToAddOperation = new HashMap<>(); + Map deviceIdsWithExistingOperation = new HashMap<>(); + Map inactiveDeviceIds = new HashMap<>(); + Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); + Map tempMap = new HashMap<>(); + try { if (complianceDatas != null || !complianceDatas.isEmpty()) { for (ComplianceData complianceData : complianceDatas) { @@ -248,7 +267,7 @@ public class MonitoringManagerImpl implements MonitoringManager { } for (Device device : devices) { - if (!tempMap.containsKey(device.getId())) { + if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) { deviceIdsToAddOperation.put(device.getId(), device); firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId())); } @@ -264,7 +283,6 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.beginTransaction(); if (!deviceIdsToAddOperation.isEmpty()) { - this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); } @@ -274,20 +292,26 @@ public class MonitoringManagerImpl implements MonitoringManager { // decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( // new ArrayList<>(deviceIdsWithExistingOperation.values()))); } - PolicyManagementDAOFactory.commitTransaction(); + } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred from monitoring dao.", e); - } catch (OperationManagementException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyComplianceException("Error occurred while adding monitoring operation to devices", e); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); } finally { PolicyManagementDAOFactory.closeConnection(); } + + if (!deviceIdsToAddOperation.isEmpty()) { + try { + this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); + } catch (OperationManagementException e) { + throw new PolicyComplianceException("Error occurred while adding monitoring operation to devices", e); + } + } + } private void addMonitoringOperationsToDatabase(List devices) 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 d72eb83099..9c879e5493 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 @@ -20,12 +20,10 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; 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.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -45,7 +43,6 @@ public class PolicyManagerImpl implements PolicyManager { private PolicyDAO policyDAO; private ProfileDAO profileDAO; private FeatureDAO featureDAO; - private DeviceDAO deviceDAO; private ProfileManager profileManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); @@ -53,7 +50,6 @@ public class PolicyManagerImpl implements PolicyManager { this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); this.profileDAO = PolicyManagementDAOFactory.getProfileDAO(); this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.profileManager = new ProfileManagerImpl(); } @@ -306,15 +302,18 @@ public class PolicyManagerImpl implements PolicyManager { public Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws PolicyManagementException { try { - PolicyManagementDAOFactory.beginTransaction(); - if (policy.getId() == 0) { - policyDAO.addPolicy(policy); - } + List deviceList = new ArrayList<>(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { deviceList.add(service.getDevice(deviceIdentifier)); } + + PolicyManagementDAOFactory.beginTransaction(); + if (policy.getId() == 0) { + policyDAO.addPolicy(policy); + } + policy = policyDAO.addPolicyToDevice(deviceList, policy); PolicyManagementDAOFactory.commitTransaction(); @@ -525,9 +524,11 @@ public class PolicyManagerImpl implements PolicyManager { List policyIdList; List policies = new ArrayList<>(); try { - PolicyManagementDAOFactory.openConnection(); + DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); + + PolicyManagementDAOFactory.openConnection(); policyIdList = policyDAO.getPolicyIdsOfDevice(device); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" + @@ -643,12 +644,14 @@ public class PolicyManagerImpl implements PolicyManager { List deviceList = new ArrayList<>(); List deviceIds; try { - PolicyManagementDAOFactory.openConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); List allDevices = service.getAllDevices(); + PolicyManagementDAOFactory.openConnection(); + //int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); + + HashMap allDeviceMap = new HashMap<>(); if(!allDevices.isEmpty()) { @@ -790,9 +793,9 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist; try { - PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); + PolicyManagementDAOFactory.openConnection(); exist = policyDAO.checkPolicyAvailable(device.getId()); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while checking whether device has a policy " + @@ -811,9 +814,10 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { - PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); + + PolicyManagementDAOFactory.openConnection(); policyDAO.setPolicyApplied(device.getId()); return true; } catch (PolicyManagerDAOException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index 931b97a37c..c5574c2daf 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -42,13 +42,13 @@ public class ProfileManagerImpl implements ProfileManager { private static Log log = LogFactory.getLog(ProfileManagerImpl.class); private ProfileDAO profileDAO; private FeatureDAO featureDAO; - private DeviceDAO deviceDAO; +// private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; public ProfileManagerImpl() { profileDAO = PolicyManagementDAOFactory.getProfileDAO(); featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); - deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); +// deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); } @@ -149,36 +149,54 @@ public class ProfileManagerImpl implements ProfileManager { PolicyManagementDAOFactory.openConnection(); profile = profileDAO.getProfiles(profileId); featureList = featureDAO.getFeaturesForProfile(profileId); - deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); profile.setProfileFeaturesList(featureList); - profile.setDeviceType(deviceType); } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting features related profile id (" + profileId + ")", e); - } catch (DeviceManagementDAOException e) { - throw new ProfileManagementException("Error occurred while getting device type related profile id (" + - profileId + ")", e); } catch (SQLException e) { throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); + + } + + try { + DeviceManagementDAOFactory.openConnection(); + deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); + } catch (DeviceManagementDAOException e) { + throw new ProfileManagementException("Error occurred while getting features related profile id (" + + profileId + ")", e); + } catch (SQLException e) { + throw new ProfileManagementException("SQL exception occurred while getting features related profile id (" + + profileId + ")", e); + } finally { DeviceManagementDAOFactory.closeConnection(); } + + profile.setDeviceType(deviceType); return profile; } @Override public List getAllProfiles() throws ProfileManagementException { List profileList; + List deviceTypes; try { + try { + DeviceManagementDAOFactory.openConnection(); + deviceTypes = deviceTypeDAO.getDeviceTypes(); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + PolicyManagementDAOFactory.openConnection(); profileList = profileDAO.getAllProfiles(); List featureList = featureDAO.getAllProfileFeatures(); - List deviceTypes = deviceTypeDAO.getDeviceTypes(); + for (Profile profile : profileList) { List list = new ArrayList(); @@ -205,7 +223,7 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); - DeviceManagementDAOFactory.closeConnection(); + // DeviceManagementDAOFactory.closeConnection(); } return profileList; } @@ -214,9 +232,16 @@ public class ProfileManagerImpl implements ProfileManager { public List getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException { List profileList; List featureList; + DeviceType deviceType; try { + try { + DeviceManagementDAOFactory.openConnection(); + deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } PolicyManagementDAOFactory.openConnection(); - DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + profileList = profileDAO.getProfilesOfDeviceType(deviceType); featureList = featureDAO.getAllProfileFeatures(); @@ -240,7 +265,7 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); - DeviceManagementDAOFactory.closeConnection(); + } return profileList; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index ac4563bd8a..1c4492e239 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -23,6 +23,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.DeviceType; @@ -62,48 +63,61 @@ public class MonitoringTask implements Task { log.debug("Monitoring task started to run."); } + List deviceTypes = new ArrayList<>(); try { - List deviceTypes = deviceTypeDAO.getDeviceTypes(); - DeviceManagementProviderService deviceManagementProviderService = - PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - MonitoringManager monitoringManager = new MonitoringManagerImpl(); - - for (DeviceType deviceType : deviceTypes) { - PolicyMonitoringService monitoringService = - PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); - List devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); - if (monitoringService != null && !devices.isEmpty()) { - monitoringManager.addMonitoringOperation(devices); - - List notifiableDevices = new ArrayList<>(); - - if (log.isDebugEnabled()) { - log.debug("Removing inactive and blocked devices from the list for the device type : " + - deviceType); - } - for (Device device : devices) { - if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.INACTIVE) || - device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.BLOCKED)) { - continue; - } else { - notifiableDevices.add(device); + DeviceManagementDAOFactory.openConnection(); + deviceTypes = deviceTypeDAO.getDeviceTypes(); + } catch (Exception e) { + log.error("Error occurred while getting the device types.", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + if (!deviceTypes.isEmpty()) { + try { + + DeviceManagementProviderService deviceManagementProviderService = + PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + MonitoringManager monitoringManager = new MonitoringManagerImpl(); + + for (DeviceType deviceType : deviceTypes) { + PolicyMonitoringService monitoringService = + PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); + List devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); + if (monitoringService != null && !devices.isEmpty()) { + monitoringManager.addMonitoringOperation(devices); + + List notifiableDevices = new ArrayList<>(); + + if (log.isDebugEnabled()) { + log.debug("Removing inactive and blocked devices from the list for the device type : " + + deviceType); } - } - if (log.isDebugEnabled()) { - log.debug("Following devices selected to send the notification for " + deviceType); - for (Device device : notifiableDevices) { - log.debug(device.getDeviceIdentifier()); + for (Device device : devices) { + if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.INACTIVE) || + device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.BLOCKED)) { + continue; + } else { + notifiableDevices.add(device); + } + } + if (log.isDebugEnabled()) { + log.debug("Following devices selected to send the notification for " + deviceType); + for (Device device : notifiableDevices) { + log.debug(device.getDeviceIdentifier()); + } } + monitoringService.notifyDevices(notifiableDevices); } - monitoringService.notifyDevices(notifiableDevices); } + if (log.isDebugEnabled()) { + log.debug("Monitoring task running completed."); + } + } catch (Exception e) { + log.error("Error occurred while trying to run a task.", e); } - if (log.isDebugEnabled()) { - log.debug("Monitoring task running completed."); - } - } catch (Exception e) { - String msg = "Error occurred while trying to run a task."; - log.error(msg, e); + } else { + log.info("No device types registered currently. So did not run the monitoring task."); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java index 0f874a1468..a8121f99ac 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java @@ -29,7 +29,6 @@ import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; -import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.ntask.core.TaskInfo.TriggerInfo; import java.util.HashMap; @@ -48,13 +47,13 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - taskService.registerTaskType(PolicyManagementConstants.TASK_TYPE); + taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE); if (log.isDebugEnabled()) { log.debug("Monitoring task is started for the tenant id " + tenantId); } - TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); TriggerInfo triggerInfo = new TriggerInfo(); @@ -64,9 +63,9 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { Map properties = new HashMap<>(); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); - String taskName = PolicyManagementConstants.TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.TASK_CLAZZ, properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); @@ -86,9 +85,9 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { public void stopTask() throws PolicyMonitoringTaskException { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String taskName = PolicyManagementConstants.TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); taskManager.deleteTask(taskName); } catch (TaskException e) { String msg = "Error occurred while deleting the task for tenant " + PrivilegedCarbonContext. @@ -102,10 +101,10 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String taskName = PolicyManagementConstants.TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); taskManager.deleteTask(taskName); @@ -117,7 +116,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { Map properties = new HashMap<>(); properties.put("tenantId", String.valueOf(tenantId)); - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.TASK_CLAZZ, properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); 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 2c033151ca..e8354b3e99 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 @@ -32,14 +32,18 @@ public final class PolicyManagementConstants { public static final String BLOCK = "BLOCK"; - public static final String TASK_TYPE = "MONITORING_TASK"; - public static final String TASK_NAME = "MONITORING"; - public static final String TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.task.MonitoringTask"; + public static final String MONITORING_TASK_TYPE = "MONITORING_TASK"; + public static final String MONITORING_TASK_NAME = "MONITORING"; + public static final String MONITORING_TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.task.MonitoringTask"; public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DM_CACHE = "DM_CACHE"; + public static final String DELEGATION_TASK_TYPE = "DELEGATION__TASK"; + public static final String DELEGATION_TASK_NAME = "DELEGATION"; + public static final String DELEGATION_TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask"; + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java index 617bf9ad86..6b15d82d2e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java @@ -28,9 +28,18 @@ import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.services.PolicyMonitoringServiceTest; +import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import javax.sql.DataSource; @@ -49,7 +58,13 @@ public abstract class BasePolicyManagementDAOTest { @BeforeSuite public void setupDataSource() throws Exception { this.initDatSource(); - // this.initSQLScript(); + this.initSQLScript(); + this.initialize(); + this.initiatePrivilegedCaronContext(); + } + + public void initialize() throws TaskException { + } public void initDatSource() throws Exception { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index bde1b14d8b..34eb288682 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -29,21 +29,21 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementAdminService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; -import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.services.PolicyMonitoringServiceTest; -import org.wso2.carbon.policy.mgt.core.task.MonitoringTask; import java.util.List; @@ -70,14 +70,13 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { List devices = service.getAllDevices(ANDROID); for (Policy policy : policies) { - log.debug(policy.getPolicyName() + "-----P"); + log.debug("Policy Name : " + policy.getPolicyName()); } for (Device device : devices) { - log.debug(device.getDeviceIdentifier() + " ----- D"); + log.debug("Device Name : " + device.getDeviceIdentifier()); } - identifier.setType(ANDROID); identifier.setId(devices.get(0).getDeviceIdentifier()); @@ -160,9 +159,11 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { PolicyManager manager = new PolicyManagerImpl(); Policy policy = manager.getAppliedPolicyToDevice(identifier); - Object ob = new Object(); + if(policy != null) { + Object ob = new Object(); - monitoringServiceTest.checkPolicyCompliance(identifier, policy, ob); + monitoringServiceTest.checkPolicyCompliance(identifier, policy, ob); + } } @@ -193,4 +194,5 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { monitoringManager.checkPolicyCompliance(identifier, ob); } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 6fdb405973..b836aaac4b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -86,20 +86,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { enrolmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234); } -// List devices = deviceDAO.getDevices(-1234); -// -// log.debug("--- Printing device taken by calling the device dao layer by tenant id."); -// for (Device device : devices) { -// log.debug(device.getDeviceIdentifier()); -// } -// -// -// log.debug("--- Printing device taken by calling the device dao layer by tenant id and device type."); -// List devices2 = deviceDAO.getDevices("android", -1234); -// -// for (Device device : devices2) { -// log.debug(device.getDeviceIdentifier()); -// } + DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); @@ -108,7 +95,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { log.debug("Printing device taken by calling the service layer with device type."); List devices3 = service.getAllDevices("android"); - log.debug("Device list size ..........................!" + devices3.size()); + log.debug("Device list size ...! " + devices3.size()); for (Device device : devices3) { log.debug(device.getDeviceIdentifier()); } @@ -118,12 +105,14 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { @Test(dependsOnMethods = ("addDevice")) public void addFeatures() throws FeatureManagementException { - FeatureManager featureManager = new FeatureManagerImpl(); + //This test case was removed because architecture was changed + +// FeatureManager featureManager = new FeatureManagerImpl(); featureList = FeatureCreator.getFeatureList(); - //featureManager.addFeatures(featureList); - for (Feature feature : featureList) { -// featureManager.addFeature(feature); - } +// featureManager.addFeatures(featureList); +// for (Feature feature : featureList) { +// featureManager.addFeature(feature); +// } } @@ -368,7 +357,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); policyAdministratorPoint.deletePolicy(1); - log.debug("First policy deleted."); + log.debug("First policy deleted..!"); } @@ -382,15 +371,14 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { List devices = service.getAllDevices("android"); for (Policy policy : policies) { - log.debug(policy.getPolicyName() + "-----P"); + log.debug("Policy Name : " + policy.getPolicyName()); } for (Device device : devices) { - log.debug(device.getDeviceIdentifier() + " ----- D"); + log.debug("Device Name : " + device.getDeviceIdentifier()); } } - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java index 49fe843cbe..e1ebc13e9d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java @@ -28,7 +28,11 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest; @@ -50,20 +54,22 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { } @Test - public void activatePolicies() throws PolicyManagementException { + public void activatePolicies() throws PolicyManagementException, TaskException { PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP(); + List policies = policyManagerService.getPolicies(ANDROID); for (Policy policy : policies) { log.debug("Policy status : " + policy.getPolicyName() + " - " + policy.isActive() + " - " + policy - .isUpdated()); + .isUpdated() + " Policy id : " + policy.getId()); if (!policy.isActive()) { administratorPoint.activatePolicy(policy.getId()); } } - administratorPoint.publishChanges(); + // This cannot be called due to task service cannot be started from the + //administratorPoint.publishChanges(); } @Test(dependsOnMethods = ("activatePolicies")) @@ -92,14 +98,14 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { @Test(dependsOnMethods = ("getEffectivePolicy")) - public void updatePriorities() throws PolicyManagementException { + public void updatePriorities() throws PolicyManagementException, TaskException { PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP(); List policies = administratorPoint.getPolicies(); - log.debug("Re-enforcing policy started...."); + log.debug("Re-enforcing policy started...!"); int sixe = policies.size(); @@ -110,10 +116,18 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { x++; } + administratorPoint.updatePolicyPriorities(policies); + // administratorPoint.publishChanges(); + } + + @Test(dependsOnMethods = ("updatePriorities")) + public void checkDelegations() { - administratorPoint.updatePolicyPriorities(policies); - administratorPoint.publishChanges(); + log.debug("Delegation methods calls started because tasks cannot be started due to osgi constraints.....!"); + + DelegationTask delegationTask = new DelegationTask(); + delegationTask.execute(); } public void sortPolicies(List policyList) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml new file mode 100644 index 0000000000..dec2307f1d --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/etc/tasks-config.xml @@ -0,0 +1,51 @@ + + + + AUTO + + + 2 + + + + org.wso2.carbon.ntask.core.impl.RoundRobinTaskLocationResolver + + + + + + + https://localhost:9448 + + + https://localhost:9443 + + + admin + + + admin + + + + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml index 2a9ebfc1b0..3b7e74c0a1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml @@ -23,18 +23,18 @@ - - - - + jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 + org.h2.Driver + wso2carbon + wso2carbon - jdbc:mysql://localhost:3306/WSO2CDM - com.mysql.jdbc.Driver - root - + + + +