Commiting the monitoring changes and test case changes

revert-70aa11f8
geethkokila 9 years ago
parent cada581396
commit bcb2c1ddb1

@ -179,7 +179,7 @@ public class DeviceDAOImpl implements DeviceDAO {
String sql = String sql =
"SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "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 " + "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 = ? " + "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 = ?"; "AND d.TENANT_ID = ?) d1 WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);

@ -492,10 +492,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
Device device; Device device;
try { try {
DeviceManagementDAOFactory.openConnection();
device = deviceDAO.getDevice(deviceId, this.getTenantId()); device = deviceDAO.getDevice(deviceId, this.getTenantId());
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for id " + throw new DeviceManagementException("Error occurred while obtaining the device for id " +
"'" + deviceId.getId() + "'", e); "'" + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for id " +
"'" + deviceId.getId() + "'", e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }

@ -144,6 +144,15 @@
<artifactId>org.wso2.carbon.ntask.core</artifactId> <artifactId>org.wso2.carbon.ntask.core</artifactId>
</dependency> </dependency>
<!-- Registry dependencies-->
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.registry.core</artifactId>
</dependency>
<!-- Policy Management Dependencies--> <!-- Policy Management Dependencies-->
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>

@ -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<String, String> map) {
}
@Override
public void init() {
}
@Override
public void execute() {
try {
PolicyManager policyManager = new PolicyManagerImpl();
List<DeviceType> 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<Device> 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<Integer, Integer> deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds();
List<Device> 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);
}
}
}

@ -18,11 +18,9 @@
*/ */
package org.wso2.carbon.policy.mgt.core.enforcement; 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.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import java.util.HashMap;
import java.util.List; import java.util.List;
public interface PolicyEnforcementDelegator { public interface PolicyEnforcementDelegator {

@ -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.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; 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 org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegator, Runnable { public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegator{
private static final Log log = LogFactory.getLog(PolicyEnforcementDelegatorImpl.class); 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);
}
}
} }

@ -166,10 +166,8 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
} }
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String msg = "Error occurred while re-enforcing the policy to device " + deviceIdentifier.getId() + " - " + throw new PolicyComplianceException("Error occurred while re-enforcing the policy to device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType(); deviceIdentifier.getType(), e);
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} }
} }

@ -20,19 +20,13 @@ package org.wso2.carbon.policy.mgt.core.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.ntask.common.TaskException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.ntask.core.TaskInfo;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.TaskManager;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.ntask.core.service.TaskService;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.*;
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.policy.mgt.core.internal.PolicyManagementDataHolder; 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.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; 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.FeatureManagerImpl;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; 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.mgt.impl.ProfileManagerImpl;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@ -111,39 +106,75 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override @Override
public void publishChanges() throws PolicyManagementException { public void publishChanges() throws PolicyManagementException {
List<DeviceType> deviceTypes = policyManager.applyChangesMadeToPolicies(); try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
taskService.registerTaskType(PolicyManagementConstants.DELEGATION_TASK_TYPE);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size() ); 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() triggerInfo.setIntervalMillis(0);
.getDeviceManagementService(); triggerInfo.setRepeatCount(1);
List<Device> 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<Integer, Integer> deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds();
List<Device> toBeNotified = new ArrayList<>();
for (Device device : devices) { Map<String, String> properties = new HashMap<>();
if (deviceIdPolicy.containsKey(device.getId())) { properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));
toBeNotified.add(device);
} String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId);
}
if (!toBeNotified.isEmpty()) { TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo);
// PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified);
Thread thread = new Thread(new PolicyEnforcementDelegatorImpl(toBeNotified)); taskManager.registerTask(taskInfo);
thread.start(); 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<DeviceType> 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<Device> 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<Integer, Integer> deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds();
// List<Device> 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 @Override

@ -61,14 +61,17 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
@Override @Override
public PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public PIPDevice getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
PIPDevice pipDevice = new PIPDevice(); PIPDevice pipDevice = new PIPDevice();
org.wso2.carbon.device.mgt.common.Device device; Device device;
DeviceType deviceType = new DeviceType(); DeviceType deviceType = new DeviceType();
deviceType.setName(deviceIdentifier.getType()); deviceType.setName(deviceIdentifier.getType());
deviceManagementService = getDeviceManagementService(); DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl();
try { try {
device = deviceManagementService.getDevice(deviceIdentifier); device = deviceManagementService.getDevice(deviceIdentifier);
Thread.currentThread();
if (device != null) {
/*deviceManagementService.getDeviceType(deviceIdentifier.getType());*/ /*deviceManagementService.getDeviceType(deviceIdentifier.getType());*/
pipDevice.setDevice(device); pipDevice.setDevice(device);
pipDevice.setRoles(getRoleOfDevice(device)); pipDevice.setRoles(getRoleOfDevice(device));
@ -81,6 +84,13 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
// pipDevice.setLongitude(); // pipDevice.setLongitude();
// pipDevice.setAltitude(); // pipDevice.setAltitude();
// pipDevice.setTimestamp(); // 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) { } catch (DeviceManagementException e) {
String msg = "Error occurred when retrieving the data related to device from the database."; String msg = "Error occurred when retrieving the data related to device from the database.";
log.error(msg, e); log.error(msg, e);

@ -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.PolicyManagementConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; 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.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; import org.wso2.carbon.user.core.service.RealmService;
/** /**
@ -82,6 +84,9 @@ public class PolicyManagementServiceComponent {
componentContext.getBundleContext().registerService( componentContext.getBundleContext().registerService(
PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null);
TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl();
taskScheduleService.startTask(30000);
} catch (Throwable t) { } catch (Throwable t) {
log.error("Error occurred while initializing the Policy management core.", t); log.error("Error occurred while initializing the Policy management core.", t);
} }

@ -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. //This was added because update query below that did not return the update table primary key.
if (complianceFeatures != null && !complianceFeatures.isEmpty()) { if (complianceFeatures != null && !complianceFeatures.isEmpty()) {
try {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId());
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Compliance status primary key " + complianceData.getId()); log.debug("Compliance status primary key " + complianceData.getId());
} }
monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures); monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(),
complianceFeatures);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} finally {
PolicyManagementDAOFactory.closeConnection();
}
complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData); complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData);
List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList(); List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList();
for (ComplianceFeature compFeature : complianceFeatures) { for (ComplianceFeature compFeature : complianceFeatures) {
@ -122,10 +128,14 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
} }
} else { } else {
try {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId());
monitoringDAO.deleteNoneComplianceData(complianceData.getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId());
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} finally {
PolicyManagementDAOFactory.closeConnection();
}
} }
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -144,8 +154,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + throw new PolicyComplianceException("Unable to add the none compliance features to database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return complianceFeatures; return complianceFeatures;
} }
@ -206,19 +214,30 @@ public class MonitoringManagerImpl implements MonitoringManager {
@Override @Override
public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException { public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException {
try {
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
//int tenantId = PolicyManagerUtil.getTenantId(); //int tenantId = PolicyManagerUtil.getTenantId();
Map<Integer, Device> deviceIds = new HashMap<>(); Map<Integer, Device> deviceIds = new HashMap<>();
List<ComplianceData> complianceDatas;
HashMap<Integer, Integer> devicePolicyIdMap;
for (Device device : devices) { for (Device device : devices) {
deviceIds.put(device.getId(), device); deviceIds.put(device.getId(), device);
} }
List<Integer> deviceIDs = new ArrayList<>(deviceIds.keySet()); List<Integer> deviceIDs = new ArrayList<>(deviceIds.keySet());
List<ComplianceData> complianceDatas = monitoringDAO.getCompliance(deviceIDs); try {
HashMap<Integer, Integer> devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs); 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<Integer, Device> deviceIdsToAddOperation = new HashMap<>(); Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>();
Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>(); Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
@ -227,7 +246,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
Map<Integer, ComplianceData> tempMap = new HashMap<>(); Map<Integer, ComplianceData> tempMap = new HashMap<>();
try {
if (complianceDatas != null || !complianceDatas.isEmpty()) { if (complianceDatas != null || !complianceDatas.isEmpty()) {
for (ComplianceData complianceData : complianceDatas) { for (ComplianceData complianceData : complianceDatas) {
@ -248,7 +267,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
for (Device device : devices) { for (Device device : devices) {
if (!tempMap.containsKey(device.getId())) { if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) {
deviceIdsToAddOperation.put(device.getId(), device); deviceIdsToAddOperation.put(device.getId(), device);
firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId())); firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId()));
} }
@ -264,7 +283,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (!deviceIdsToAddOperation.isEmpty()) { if (!deviceIdsToAddOperation.isEmpty()) {
this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values()));
monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds);
} }
@ -274,20 +292,26 @@ public class MonitoringManagerImpl implements MonitoringManager {
// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( // decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices(
// new ArrayList<>(deviceIdsWithExistingOperation.values()))); // new ArrayList<>(deviceIdsWithExistingOperation.values())));
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Error occurred from monitoring dao.", e); 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) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); 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<Device> devices) private void addMonitoringOperationsToDatabase(List<Device> devices)

@ -20,12 +20,10 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.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.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -45,7 +43,6 @@ public class PolicyManagerImpl implements PolicyManager {
private PolicyDAO policyDAO; private PolicyDAO policyDAO;
private ProfileDAO profileDAO; private ProfileDAO profileDAO;
private FeatureDAO featureDAO; private FeatureDAO featureDAO;
private DeviceDAO deviceDAO;
private ProfileManager profileManager; private ProfileManager profileManager;
private static Log log = LogFactory.getLog(PolicyManagerImpl.class); private static Log log = LogFactory.getLog(PolicyManagerImpl.class);
@ -53,7 +50,6 @@ public class PolicyManagerImpl implements PolicyManager {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
this.profileDAO = PolicyManagementDAOFactory.getProfileDAO(); this.profileDAO = PolicyManagementDAOFactory.getProfileDAO();
this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.profileManager = new ProfileManagerImpl(); this.profileManager = new ProfileManagerImpl();
} }
@ -306,15 +302,18 @@ public class PolicyManagerImpl implements PolicyManager {
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList,
Policy policy) throws PolicyManagementException { Policy policy) throws PolicyManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
List<Device> deviceList = new ArrayList<>(); List<Device> deviceList = new ArrayList<>();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) {
deviceList.add(service.getDevice(deviceIdentifier)); deviceList.add(service.getDevice(deviceIdentifier));
} }
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
policy = policyDAO.addPolicyToDevice(deviceList, policy); policy = policyDAO.addPolicyToDevice(deviceList, policy);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
@ -525,9 +524,11 @@ public class PolicyManagerImpl implements PolicyManager {
List<Integer> policyIdList; List<Integer> policyIdList;
List<Policy> policies = new ArrayList<>(); List<Policy> policies = new ArrayList<>();
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyIdsOfDevice(device); policyIdList = policyDAO.getPolicyIdsOfDevice(device);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" + throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" +
@ -643,12 +644,14 @@ public class PolicyManagerImpl implements PolicyManager {
List<Device> deviceList = new ArrayList<>(); List<Device> deviceList = new ArrayList<>();
List<Integer> deviceIds; List<Integer> deviceIds;
try { try {
PolicyManagementDAOFactory.openConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
List<Device> allDevices = service.getAllDevices(); List<Device> allDevices = service.getAllDevices();
PolicyManagementDAOFactory.openConnection();
//int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
HashMap<Integer, Device> allDeviceMap = new HashMap<>(); HashMap<Integer, Device> allDeviceMap = new HashMap<>();
if(!allDevices.isEmpty()) { if(!allDevices.isEmpty()) {
@ -790,9 +793,9 @@ public class PolicyManagerImpl implements PolicyManager {
boolean exist; boolean exist;
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection();
exist = policyDAO.checkPolicyAvailable(device.getId()); exist = policyDAO.checkPolicyAvailable(device.getId());
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while checking whether device has a policy " + throw new PolicyManagementException("Error occurred while checking whether device has a policy " +
@ -811,9 +814,10 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
PolicyManagementDAOFactory.openConnection();
policyDAO.setPolicyApplied(device.getId()); policyDAO.setPolicyApplied(device.getId());
return true; return true;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {

@ -42,13 +42,13 @@ public class ProfileManagerImpl implements ProfileManager {
private static Log log = LogFactory.getLog(ProfileManagerImpl.class); private static Log log = LogFactory.getLog(ProfileManagerImpl.class);
private ProfileDAO profileDAO; private ProfileDAO profileDAO;
private FeatureDAO featureDAO; private FeatureDAO featureDAO;
private DeviceDAO deviceDAO; // private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
public ProfileManagerImpl() { public ProfileManagerImpl() {
profileDAO = PolicyManagementDAOFactory.getProfileDAO(); profileDAO = PolicyManagementDAOFactory.getProfileDAO();
featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); // deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
} }
@ -149,36 +149,54 @@ public class ProfileManagerImpl implements ProfileManager {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
profile = profileDAO.getProfiles(profileId); profile = profileDAO.getProfiles(profileId);
featureList = featureDAO.getFeaturesForProfile(profileId); featureList = featureDAO.getFeaturesForProfile(profileId);
deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId());
profile.setProfileFeaturesList(featureList); profile.setProfileFeaturesList(featureList);
profile.setDeviceType(deviceType);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e); throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
throw new ProfileManagementException("Error occurred while getting features related profile id (" + throw new ProfileManagementException("Error occurred while getting features related profile id (" +
profileId + ")", e); profileId + ")", e);
} catch (DeviceManagementDAOException e) {
throw new ProfileManagementException("Error occurred while getting device type related profile id (" +
profileId + ")", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); 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(); DeviceManagementDAOFactory.closeConnection();
} }
profile.setDeviceType(deviceType);
return profile; return profile;
} }
@Override @Override
public List<Profile> getAllProfiles() throws ProfileManagementException { public List<Profile> getAllProfiles() throws ProfileManagementException {
List<Profile> profileList; List<Profile> profileList;
List<DeviceType> deviceTypes;
try {
try { try {
DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes();
} finally {
DeviceManagementDAOFactory.closeConnection();
}
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
profileList = profileDAO.getAllProfiles(); profileList = profileDAO.getAllProfiles();
List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures(); List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures();
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
for (Profile profile : profileList) { for (Profile profile : profileList) {
List<ProfileFeature> list = new ArrayList<ProfileFeature>(); List<ProfileFeature> list = new ArrayList<ProfileFeature>();
@ -205,7 +223,7 @@ public class ProfileManagerImpl implements ProfileManager {
throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
DeviceManagementDAOFactory.closeConnection(); // DeviceManagementDAOFactory.closeConnection();
} }
return profileList; return profileList;
} }
@ -214,9 +232,16 @@ public class ProfileManagerImpl implements ProfileManager {
public List<Profile> getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException { public List<Profile> getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException {
List<Profile> profileList; List<Profile> profileList;
List<ProfileFeature> featureList; List<ProfileFeature> featureList;
DeviceType deviceType;
try {
try { try {
DeviceManagementDAOFactory.openConnection();
deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
profileList = profileDAO.getProfilesOfDeviceType(deviceType); profileList = profileDAO.getProfilesOfDeviceType(deviceType);
featureList = featureDAO.getAllProfileFeatures(); 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); throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
DeviceManagementDAOFactory.closeConnection();
} }
return profileList; return profileList;
} }

@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; 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.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -62,8 +63,19 @@ public class MonitoringTask implements Task {
log.debug("Monitoring task started to run."); log.debug("Monitoring task started to run.");
} }
List<DeviceType> deviceTypes = new ArrayList<>();
try { try {
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes(); 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 = DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManagementDataHolder.getInstance().getDeviceManagementService();
MonitoringManager monitoringManager = new MonitoringManagerImpl(); MonitoringManager monitoringManager = new MonitoringManagerImpl();
@ -102,8 +114,10 @@ public class MonitoringTask implements Task {
log.debug("Monitoring task running completed."); log.debug("Monitoring task running completed.");
} }
} catch (Exception e) { } catch (Exception e) {
String msg = "Error occurred while trying to run a task."; log.error("Error occurred while trying to run a task.", e);
log.error(msg, e); }
} else {
log.info("No device types registered currently. So did not run the monitoring task.");
} }
} }

@ -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.common.PolicyMonitoringTaskException;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; 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.PolicyManagementConstants;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import org.wso2.carbon.ntask.core.TaskInfo.TriggerInfo; import org.wso2.carbon.ntask.core.TaskInfo.TriggerInfo;
import java.util.HashMap; import java.util.HashMap;
@ -48,13 +47,13 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
taskService.registerTaskType(PolicyManagementConstants.TASK_TYPE); taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Monitoring task is started for the tenant id " + tenantId); 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(); TriggerInfo triggerInfo = new TriggerInfo();
@ -64,9 +63,9 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); 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.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName()); taskManager.rescheduleTask(taskInfo.getName());
@ -86,9 +85,9 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
public void stopTask() throws PolicyMonitoringTaskException { public void stopTask() throws PolicyMonitoringTaskException {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); 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(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
taskManager.deleteTask(taskName); taskManager.deleteTask(taskName);
} catch (TaskException e) { } catch (TaskException e) {
String msg = "Error occurred while deleting the task for tenant " + PrivilegedCarbonContext. 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 { public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); 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(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
taskManager.deleteTask(taskName); taskManager.deleteTask(taskName);
@ -117,7 +116,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put("tenantId", String.valueOf(tenantId)); 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.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName()); taskManager.rescheduleTask(taskInfo.getName());

@ -32,14 +32,18 @@ public final class PolicyManagementConstants {
public static final String BLOCK = "BLOCK"; public static final String BLOCK = "BLOCK";
public static final String TASK_TYPE = "MONITORING_TASK"; public static final String MONITORING_TASK_TYPE = "MONITORING_TASK";
public static final String TASK_NAME = "MONITORING"; public static final String MONITORING_TASK_NAME = "MONITORING";
public static final String TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.task.MonitoringTask"; 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_MANAGER = "DM_CACHE_MANAGER";
public static final String DM_CACHE = "DM_CACHE"; 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";
} }

@ -28,9 +28,18 @@ import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; 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.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.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig; 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.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 org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -49,7 +58,13 @@ public abstract class BasePolicyManagementDAOTest {
@BeforeSuite @BeforeSuite
public void setupDataSource() throws Exception { public void setupDataSource() throws Exception {
this.initDatSource(); this.initDatSource();
// this.initSQLScript(); this.initSQLScript();
this.initialize();
this.initiatePrivilegedCaronContext();
}
public void initialize() throws TaskException {
} }
public void initDatSource() throws Exception { public void initDatSource() throws Exception {

@ -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.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.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.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; 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.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; 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.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.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; 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.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; 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.mgt.impl.PolicyManagerImpl;
import org.wso2.carbon.policy.mgt.core.services.PolicyMonitoringServiceTest; import org.wso2.carbon.policy.mgt.core.services.PolicyMonitoringServiceTest;
import org.wso2.carbon.policy.mgt.core.task.MonitoringTask;
import java.util.List; import java.util.List;
@ -70,14 +70,13 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest {
List<Device> devices = service.getAllDevices(ANDROID); List<Device> devices = service.getAllDevices(ANDROID);
for (Policy policy : policies) { for (Policy policy : policies) {
log.debug(policy.getPolicyName() + "-----P"); log.debug("Policy Name : " + policy.getPolicyName());
} }
for (Device device : devices) { for (Device device : devices) {
log.debug(device.getDeviceIdentifier() + " ----- D"); log.debug("Device Name : " + device.getDeviceIdentifier());
} }
identifier.setType(ANDROID); identifier.setType(ANDROID);
identifier.setId(devices.get(0).getDeviceIdentifier()); identifier.setId(devices.get(0).getDeviceIdentifier());
@ -160,10 +159,12 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest {
PolicyManager manager = new PolicyManagerImpl(); PolicyManager manager = new PolicyManagerImpl();
Policy policy = manager.getAppliedPolicyToDevice(identifier); Policy policy = manager.getAppliedPolicyToDevice(identifier);
if(policy != null) {
Object ob = new Object(); Object ob = new Object();
monitoringServiceTest.checkPolicyCompliance(identifier, policy, ob); monitoringServiceTest.checkPolicyCompliance(identifier, policy, ob);
} }
}
@Test(dependsOnMethods = ("checkComplianceFromMonitoringService")) @Test(dependsOnMethods = ("checkComplianceFromMonitoringService"))
@ -193,4 +194,5 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest {
monitoringManager.checkPolicyCompliance(identifier, ob); monitoringManager.checkPolicyCompliance(identifier, ob);
} }
} }

@ -86,20 +86,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
enrolmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234); enrolmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234);
} }
// List<Device> 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<Device> devices2 = deviceDAO.getDevices("android", -1234);
//
// for (Device device : devices2) {
// log.debug(device.getDeviceIdentifier());
// }
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); 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."); log.debug("Printing device taken by calling the service layer with device type.");
List<Device> devices3 = service.getAllDevices("android"); List<Device> devices3 = service.getAllDevices("android");
log.debug("Device list size ..........................!" + devices3.size()); log.debug("Device list size ...! " + devices3.size());
for (Device device : devices3) { for (Device device : devices3) {
log.debug(device.getDeviceIdentifier()); log.debug(device.getDeviceIdentifier());
} }
@ -118,12 +105,14 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
@Test(dependsOnMethods = ("addDevice")) @Test(dependsOnMethods = ("addDevice"))
public void addFeatures() throws FeatureManagementException { 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(); featureList = FeatureCreator.getFeatureList();
// featureManager.addFeatures(featureList); // featureManager.addFeatures(featureList);
for (Feature feature : featureList) { // for (Feature feature : featureList) {
// featureManager.addFeature(feature); // featureManager.addFeature(feature);
} // }
} }
@ -368,7 +357,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl();
policyAdministratorPoint.deletePolicy(1); policyAdministratorPoint.deletePolicy(1);
log.debug("First policy deleted."); log.debug("First policy deleted..!");
} }
@ -382,15 +371,14 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
List<Device> devices = service.getAllDevices("android"); List<Device> devices = service.getAllDevices("android");
for (Policy policy : policies) { for (Policy policy : policies) {
log.debug(policy.getPolicyName() + "-----P"); log.debug("Policy Name : " + policy.getPolicyName());
} }
for (Device device : devices) { for (Device device : devices) {
log.debug(device.getDeviceIdentifier() + " ----- D"); log.debug("Device Name : " + device.getDeviceIdentifier());
} }
} }
} }

@ -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.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; 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.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.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest; import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest;
@ -50,20 +54,22 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest {
} }
@Test @Test
public void activatePolicies() throws PolicyManagementException { public void activatePolicies() throws PolicyManagementException, TaskException {
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP(); PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP();
List<Policy> policies = policyManagerService.getPolicies(ANDROID); List<Policy> policies = policyManagerService.getPolicies(ANDROID);
for (Policy policy : policies) { for (Policy policy : policies) {
log.debug("Policy status : " + policy.getPolicyName() + " - " + policy.isActive() + " - " + policy log.debug("Policy status : " + policy.getPolicyName() + " - " + policy.isActive() + " - " + policy
.isUpdated()); .isUpdated() + " Policy id : " + policy.getId());
if (!policy.isActive()) { if (!policy.isActive()) {
administratorPoint.activatePolicy(policy.getId()); administratorPoint.activatePolicy(policy.getId());
} }
} }
administratorPoint.publishChanges(); // This cannot be called due to task service cannot be started from the
//administratorPoint.publishChanges();
} }
@Test(dependsOnMethods = ("activatePolicies")) @Test(dependsOnMethods = ("activatePolicies"))
@ -92,14 +98,14 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest {
@Test(dependsOnMethods = ("getEffectivePolicy")) @Test(dependsOnMethods = ("getEffectivePolicy"))
public void updatePriorities() throws PolicyManagementException { public void updatePriorities() throws PolicyManagementException, TaskException {
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP(); PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP();
List<Policy> policies = administratorPoint.getPolicies(); List<Policy> policies = administratorPoint.getPolicies();
log.debug("Re-enforcing policy started...."); log.debug("Re-enforcing policy started...!");
int sixe = policies.size(); int sixe = policies.size();
@ -110,10 +116,18 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest {
x++; x++;
} }
administratorPoint.updatePolicyPriorities(policies);
// administratorPoint.publishChanges();
}
@Test(dependsOnMethods = ("updatePriorities"))
public void checkDelegations() {
administratorPoint.updatePolicyPriorities(policies); log.debug("Delegation methods calls started because tasks cannot be started due to osgi constraints.....!");
administratorPoint.publishChanges();
DelegationTask delegationTask = new DelegationTask();
delegationTask.execute();
} }
public void sortPolicies(List<Policy> policyList) { public void sortPolicies(List<Policy> policyList) {

@ -0,0 +1,51 @@
<tasks-configuration xmlns:svns="http://org.wso2.securevault/configuration">
<!--
The currently running server mode; possible values are:-
STANDALONE, CLUSTERED, REMOTE, AUTO.
In AUTO mode, the server startup checks whether clustering is enabled in the system,
if so, CLUSTERED mode will be used, or else, the the server mode will be STANDALONE.
-->
<taskServerMode>AUTO</taskServerMode>
<!--
To be used in CLUSTERED mode to notify how many servers are there in
the task server cluster, the servers wait till this amount of servers
are activated before the tasks are scheduled -->
<taskServerCount>2</taskServerCount>
<!-- The default location resolver configuration -->
<defaultLocationResolver>
<locationResolverClass>org.wso2.carbon.ntask.core.impl.RoundRobinTaskLocationResolver</locationResolverClass>
</defaultLocationResolver>
<!--
if task-type-pattern matches and task-name-pattern matches, check existing addresses of address-pattern,
and if addresses exist, select address in round-robin fashion, if not move onto next rule in sequence.
<property name="rule-[order]">[task-type-pattern],[task-name-pattern],[address-pattern]</property>
-->
<!--defaultLocationResolver>
<locationResolverClass>org.wso2.carbon.ntask.core.impl.RuleBasedLocationResolver</locationResolverClass>
<properties>
<property name="rule-1">HIVE_TASK,.*,192.168.2.*</property>
<property name="rule-5">.*,.*,.*</property>
</properties>
</defaultLocationResolver-->
<!-- The address to which the remote task server should dispatch the trigger messages to,
usually this would be an endpoint to a load balancer -->
<taskClientDispatchAddress>https://localhost:9448</taskClientDispatchAddress>
<!-- The address of the remote task server -->
<remoteServerAddress>https://localhost:9443</remoteServerAddress>
<!-- The username to authenticate to the remote task server -->
<remoteServerUsername>admin</remoteServerUsername>
<!-- The password to authenticate to the remote task server -->
<remoteServerPassword>admin</remoteServerPassword>
<!-- Below contain a sample to be used when using with secure vault -->
<!--remoteServerPassword svns:secretAlias="remote.task.server.password"></remoteServerPassword-->
</tasks-configuration>

@ -23,18 +23,18 @@
<!-- For H2 --> <!-- For H2 -->
<!--<Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1</Url>--> <Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1</Url>
<!--<DriverClassName>org.h2.Driver</DriverClassName>--> <DriverClassName>org.h2.Driver</DriverClassName>
<!--<User>wso2carbon</User>--> <User>wso2carbon</User>
<!--<Password>wso2carbon</Password>--> <Password>wso2carbon</Password>
<!-- For MySql --> <!-- For MySql -->
<Url>jdbc:mysql://localhost:3306/WSO2CDM</Url> <!--<Url>jdbc:mysql://localhost:3306/WSO2CDM</Url>-->
<DriverClassName>com.mysql.jdbc.Driver</DriverClassName> <!--<DriverClassName>com.mysql.jdbc.Driver</DriverClassName>-->
<User>root</User> <!--<User>root</User>-->
<Password></Password> <!--<Password></Password>-->
</DataSourceConfig> </DataSourceConfig>

Loading…
Cancel
Save