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 =
"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);

@ -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();
}

@ -144,6 +144,15 @@
<artifactId>org.wso2.carbon.ntask.core</artifactId>
</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-->
<dependency>
<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;
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 {

@ -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);
}
}
}

@ -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);
}
}

@ -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<DeviceType> 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<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<>();
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<String, String> 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<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

@ -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);

@ -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);
}

@ -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<ProfileFeature> 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<Device> devices) throws PolicyComplianceException {
try {
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
//int tenantId = PolicyManagerUtil.getTenantId();
Map<Integer, Device> deviceIds = new HashMap<>();
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
for (Device device : devices) {
deviceIds.put(device.getId(), device);
}
//int tenantId = PolicyManagerUtil.getTenantId();
Map<Integer, Device> deviceIds = new HashMap<>();
List<ComplianceData> complianceDatas;
HashMap<Integer, Integer> devicePolicyIdMap;
List<Integer> deviceIDs = new ArrayList<>(deviceIds.keySet());
List<ComplianceData> complianceDatas = monitoringDAO.getCompliance(deviceIDs);
HashMap<Integer, Integer> devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs);
for (Device device : devices) {
deviceIds.put(device.getId(), device);
}
Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>();
Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
Map<Integer, Device> inactiveDeviceIds = new HashMap<>();
Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>();
List<Integer> 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<Integer, ComplianceData> tempMap = new HashMap<>();
Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>();
Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
Map<Integer, Device> inactiveDeviceIds = new HashMap<>();
Map<Integer, Integer> firstTimeDeviceIdsWithPolicyIds = new HashMap<>();
Map<Integer, ComplianceData> 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<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.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<DeviceIdentifier> deviceIdentifierList,
Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
List<Device> 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<Integer> policyIdList;
List<Policy> 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<Device> deviceList = new ArrayList<>();
List<Integer> deviceIds;
try {
PolicyManagementDAOFactory.openConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
List<Device> allDevices = service.getAllDevices();
PolicyManagementDAOFactory.openConnection();
//int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
HashMap<Integer, Device> 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) {

@ -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<Profile> getAllProfiles() throws ProfileManagementException {
List<Profile> profileList;
List<DeviceType> deviceTypes;
try {
try {
DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes();
} finally {
DeviceManagementDAOFactory.closeConnection();
}
PolicyManagementDAOFactory.openConnection();
profileList = profileDAO.getAllProfiles();
List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures();
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
for (Profile profile : profileList) {
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);
} finally {
PolicyManagementDAOFactory.closeConnection();
DeviceManagementDAOFactory.closeConnection();
// DeviceManagementDAOFactory.closeConnection();
}
return profileList;
}
@ -214,9 +232,16 @@ public class ProfileManagerImpl implements ProfileManager {
public List<Profile> getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException {
List<Profile> profileList;
List<ProfileFeature> 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;
}

@ -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<DeviceType> deviceTypes = new ArrayList<>();
try {
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
MonitoringManager monitoringManager = new MonitoringManagerImpl();
for (DeviceType deviceType : deviceTypes) {
PolicyMonitoringService monitoringService =
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName());
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName());
if (monitoringService != null && !devices.isEmpty()) {
monitoringManager.addMonitoringOperation(devices);
List<Device> 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<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName());
if (monitoringService != null && !devices.isEmpty()) {
monitoringManager.addMonitoringOperation(devices);
List<Device> 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.");
}
}

@ -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<String, String> 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<String, String> 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());

@ -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";
}

@ -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 {

@ -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<Device> 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);
}
}

@ -86,20 +86,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
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();
@ -108,7 +95,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
log.debug("Printing device taken by calling the service layer with device type.");
List<Device> 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<Device> 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());
}
}
}

@ -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<Policy> 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<Policy> 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<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 -->
<!--<Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1</Url>-->
<!--<DriverClassName>org.h2.Driver</DriverClassName>-->
<!--<User>wso2carbon</User>-->
<!--<Password>wso2carbon</Password>-->
<Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1</Url>
<DriverClassName>org.h2.Driver</DriverClassName>
<User>wso2carbon</User>
<Password>wso2carbon</Password>
<!-- For MySql -->
<Url>jdbc:mysql://localhost:3306/WSO2CDM</Url>
<DriverClassName>com.mysql.jdbc.Driver</DriverClassName>
<User>root</User>
<Password></Password>
<!--<Url>jdbc:mysql://localhost:3306/WSO2CDM</Url>-->
<!--<DriverClassName>com.mysql.jdbc.Driver</DriverClassName>-->
<!--<User>root</User>-->
<!--<Password></Password>-->
</DataSourceConfig>

Loading…
Cancel
Save