adding separate tasks for each device type

revert-70aa11f8
madhawap 8 years ago
parent 78bef05e11
commit b889f6e384

@ -30,10 +30,8 @@ import java.util.List;
@XmlRootElement(name = "TaskConfiguration") @XmlRootElement(name = "TaskConfiguration")
public class TaskConfiguration { public class TaskConfiguration {
private boolean enabled; private boolean enabled;
private int frequency; private int frequency;
private String taskClazz;
private List<Operation> operations; private List<Operation> operations;
@XmlElement(name = "Enable", required = true) @XmlElement(name = "Enable", required = true)
@ -54,15 +52,6 @@ public class TaskConfiguration {
this.frequency = frequency; this.frequency = frequency;
} }
@XmlElement(name = "TaskClass", required = true)
public String getTaskClazz() {
return taskClazz;
}
public void setTaskClazz(String taskClazz) {
this.taskClazz = taskClazz;
}
@XmlElementWrapper(name="Operations") @XmlElementWrapper(name="Operations")
@XmlElement(name = "Operation", required = true) @XmlElement(name = "Operation", required = true)
public List<Operation> getOperations() { public List<Operation> getOperations() {

@ -22,8 +22,9 @@ 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.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig;
import org.wso2.carbon.device.mgt.common.TaskOperation;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
@ -51,7 +52,8 @@ public class DeviceTypeManagerService implements DeviceManagementService {
private PushNotificationConfig pushNotificationConfig; private PushNotificationConfig pushNotificationConfig;
private ProvisioningConfig provisioningConfig; private ProvisioningConfig provisioningConfig;
private String type; private String type;
private List<TaskOperation> taskOperations; private OperationMonitoringTaskConfig operationMonitoringConfigs;
private List<MonitoringOperation> monitoringOperations;
public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier,
DeviceTypeConfiguration deviceTypeConfiguration) { DeviceTypeConfiguration deviceTypeConfiguration) {
@ -59,7 +61,8 @@ public class DeviceTypeManagerService implements DeviceManagementService {
this.deviceManager = new DeviceTypeManager(deviceTypeConfigIdentifier, deviceTypeConfiguration); this.deviceManager = new DeviceTypeManager(deviceTypeConfigIdentifier, deviceTypeConfiguration);
this.setType(deviceTypeConfiguration.getName()); this.setType(deviceTypeConfiguration.getName());
this.populatePushNotificationConfig(deviceTypeConfiguration.getPushNotificationProvider()); this.populatePushNotificationConfig(deviceTypeConfiguration.getPushNotificationProvider());
this.setTask(deviceTypeConfiguration); this.operationMonitoringConfigs = new OperationMonitoringTaskConfig();
this.setOperationMonitoringConfig(deviceTypeConfiguration);
} }
@Override @Override
@ -68,24 +71,27 @@ public class DeviceTypeManagerService implements DeviceManagementService {
} }
@Override @Override
public List<TaskOperation> getTasksForPlatform(){ public OperationMonitoringTaskConfig getOperationMonitoringConfig(){
return taskOperations; return operationMonitoringConfigs;
} }
private void setTask(DeviceTypeConfiguration deviceTypeConfiguration) { private void setOperationMonitoringConfig(DeviceTypeConfiguration deviceTypeConfiguration) {
//Read the config file and take the list of operations there in the config //Read the config file and take the list of operations there in the config
TaskConfiguration taskConfiguration = deviceTypeConfiguration.getTaskConfiguration(); TaskConfiguration taskConfiguration = deviceTypeConfiguration.getTaskConfiguration();
if (taskConfiguration != null) { if (taskConfiguration != null) {
operationMonitoringConfigs.setEnabled(taskConfiguration.isEnabled());
operationMonitoringConfigs.setFrequency(taskConfiguration.getFrequency());
List<TaskConfiguration.Operation> ops = taskConfiguration.getOperations(); List<TaskConfiguration.Operation> ops = taskConfiguration.getOperations();
if (ops != null && !ops.isEmpty()) { if (ops != null && !ops.isEmpty()) {
taskOperations = new ArrayList<>(); monitoringOperations = new ArrayList<>();
for (TaskConfiguration.Operation op : ops) { for (TaskConfiguration.Operation op : ops) {
TaskOperation taskOperation = new TaskOperation(); MonitoringOperation monitoringOperation = new MonitoringOperation();
taskOperation.setTaskName(op.getOperationName()); monitoringOperation.setTaskName(op.getOperationName());
taskOperation.setRecurrentTimes(op.getRecurrency()); monitoringOperation.setRecurrentTimes(op.getRecurrency());
taskOperations.add(taskOperation); monitoringOperations.add(monitoringOperation);
} }
} }
operationMonitoringConfigs.setMonitoringOperation(monitoringOperations);
} }
} }

@ -102,6 +102,8 @@
</tableConfig> </tableConfig>
</DataSource> </DataSource>
<TaskConfiguration> <TaskConfiguration>
<Enable>true</Enable>
<Frequency>600000</Frequency>
<Operations> <Operations>
<Operation> <Operation>
<Name>DEVICE_INFO</Name> <Name>DEVICE_INFO</Name>

@ -21,7 +21,7 @@ package org.wso2.carbon.device.mgt.common;
/** /**
* *
*/ */
public class TaskOperation { public class MonitoringOperation {
private String taskName; private String taskName;
private int recurrentTimes; private int recurrentTimes;
@ -42,5 +42,6 @@ public class TaskOperation {
this.recurrentTimes = recurrentTimes; this.recurrentTimes = recurrentTimes;
} }
} }

@ -0,0 +1,40 @@
package org.wso2.carbon.device.mgt.common;
import java.util.List;
/**
*
*/
public class OperationMonitoringTaskConfig {
private boolean isEnabled;
private int frequency;
private List<MonitoringOperation> monitoringOperation;
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
public List<MonitoringOperation> getMonitoringOperation() {
return monitoringOperation;
}
public void setMonitoringOperation(List<MonitoringOperation> monitoringOperation) {
this.monitoringOperation = monitoringOperation;
}
}

@ -20,13 +20,11 @@ package org.wso2.carbon.device.mgt.common.spi;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig;
import org.wso2.carbon.device.mgt.common.TaskOperation;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import java.util.List;
/** /**
* Composite interface that acts as the SPI exposing all device management as well as application management * Composite interface that acts as the SPI exposing all device management as well as application management
* functionalities. * functionalities.
@ -37,7 +35,7 @@ public interface DeviceManagementService {
String getType(); String getType();
List<TaskOperation> getTasksForPlatform();//getTasksConfiguraitons OperationMonitoringTaskConfig getOperationMonitoringConfig();
DeviceManager getDeviceManager(); DeviceManager getDeviceManager();

@ -22,6 +22,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig;
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.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
@ -31,8 +32,11 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
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.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener; import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener;
import org.wso2.carbon.device.mgt.core.internal.DeviceMonitoringOperationDataHolder;
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.operation.mgt.OperationManagerRepository; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerRepository;
import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.util.Collections; import java.util.Collections;
@ -72,6 +76,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
provider.getDeviceManager() provider.getDeviceManager()
.requireDeviceAuthorization()); .requireDeviceAuthorization());
registerPushNotificationStrategy(provider); registerPushNotificationStrategy(provider);
registerMonitoringTask(provider);
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new DeviceManagementException("Error occurred while adding device management provider '" + throw new DeviceManagementException("Error occurred while adding device management provider '" +
@ -87,7 +92,8 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
} }
} }
public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { public void removeDeviceManagementProvider(DeviceManagementService provider)
throws DeviceManagementException {
String deviceTypeName = provider.getType().toLowerCase(); String deviceTypeName = provider.getType().toLowerCase();
DeviceTypeIdentifier deviceTypeIdentifier; DeviceTypeIdentifier deviceTypeIdentifier;
ProvisioningConfig provisioningConfig = provider.getProvisioningConfig(); ProvisioningConfig provisioningConfig = provider.getProvisioningConfig();
@ -99,7 +105,13 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName, providerTenantId); deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName, providerTenantId);
providers.remove(deviceTypeIdentifier); providers.remove(deviceTypeIdentifier);
} }
OperationManager operationManager = operationManagerRepository.getOperationManager(deviceTypeIdentifier); unregisterPushNotificationStrategy(deviceTypeIdentifier);
unregisterMonitoringTask(provider);
}
private void unregisterPushNotificationStrategy(DeviceTypeIdentifier deviceTypeIdentifier) {
OperationManager operationManager = operationManagerRepository.getOperationManager(
deviceTypeIdentifier);
if (operationManager != null) { if (operationManager != null) {
NotificationStrategy notificationStrategy = operationManager.getNotificationStrategy(); NotificationStrategy notificationStrategy = operationManager.getNotificationStrategy();
if (notificationStrategy != null) { if (notificationStrategy != null) {
@ -155,16 +167,60 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
pushNoteConfig.getType() + "'."); pushNoteConfig.getType() + "'.");
} }
NotificationStrategy notificationStrategy = provider.getNotificationStrategy(pushNoteConfig); NotificationStrategy notificationStrategy = provider.getNotificationStrategy(pushNoteConfig);
operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl( operationManagerRepository.addOperationManager(deviceTypeIdentifier,
notificationStrategy)); new OperationManagerImpl(deviceTypeIdentifier.getDeviceType(), notificationStrategy));
} else { } else {
operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl()); operationManagerRepository.addOperationManager(deviceTypeIdentifier,
new OperationManagerImpl(deviceTypeIdentifier.getDeviceType()));
} }
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
} }
private void registerMonitoringTask(DeviceManagementService deviceManagementService)
throws DeviceManagementException {
try {
DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance()
.getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService
.getOperationMonitoringConfig();
if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) {
if (deviceTaskManagerService == null) {
DeviceMonitoringOperationDataHolder.getInstance().addOperationMonitoringConfigToMap(
deviceManagementService.getType(), operationMonitoringTaskConfig);
} else {
deviceTaskManagerService.startTask(deviceManagementService.getType(),
operationMonitoringTaskConfig);
// TODO: In here a race condition can arise. Need to handle it.
}
}
} catch (DeviceMgtTaskException e) {
throw new DeviceManagementException("Error occurred while adding task service for '" +
deviceManagementService.getType() + "'", e);
}
}
private void unregisterMonitoringTask(DeviceManagementService deviceManagementService)
throws DeviceManagementException {
try {
DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance()
.getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService
.getOperationMonitoringConfig();
if (operationMonitoringTaskConfig != null) {
deviceTaskManagerService.stopTask(deviceManagementService.getType(),
deviceManagementService.getOperationMonitoringConfig());
}
} catch (DeviceMgtTaskException e) {
throw new DeviceManagementException("Error occurred while removing task service for '" +
deviceManagementService.getType() + "'", e);
}
}
public OperationManager getOperationManager(String deviceType, int tenantId) { public OperationManager getOperationManager(String deviceType, int tenantId) {
//Priority need to be given to the tenant before public. //Priority need to be given to the tenant before public.
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType.toLowerCase(), tenantId); DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType.toLowerCase(), tenantId);
@ -189,6 +245,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
DeviceManagerUtil.registerDeviceType(deviceTypeName, tenantId, DeviceManagerUtil.registerDeviceType(deviceTypeName, tenantId,
provisioningConfig.isSharedWithAllTenants()); provisioningConfig.isSharedWithAllTenants());
registerPushNotificationStrategy(provider); registerPushNotificationStrategy(provider);
registerMonitoringTask(provider);
//TODO: //TODO:
//This is a temporory fix. //This is a temporory fix.
//windows and IOS cannot resolve user info by extracting certs //windows and IOS cannot resolve user info by extracting certs
@ -207,5 +264,4 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
this.isInited = true; this.isInited = true;
} }
} }
} }

@ -19,6 +19,7 @@
package org.wso2.carbon.device.mgt.core.internal; package org.wso2.carbon.device.mgt.core.internal;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager;
@ -28,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig;
import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository; import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository;
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.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService;
import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.email.sender.core.service.EmailSenderService;
import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.ntask.core.service.TaskService;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
@ -36,6 +38,7 @@ import org.wso2.carbon.user.core.tenant.TenantManager;
import org.wso2.carbon.utils.ConfigurationContextService; import org.wso2.carbon.utils.ConfigurationContextService;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
public class DeviceManagementDataHolder { public class DeviceManagementDataHolder {
@ -56,6 +59,18 @@ public class DeviceManagementDataHolder {
private TaskService taskService; private TaskService taskService;
private EmailSenderService emailSenderService; private EmailSenderService emailSenderService;
private PushNotificationProviderRepository pushNotificationProviderRepository; private PushNotificationProviderRepository pushNotificationProviderRepository;
private DeviceTaskManagerService deviceTaskManagerService;
private Map<String, OperationMonitoringTaskConfig> map = new HashMap<>();
public void addToMap(OperationMonitoringTaskConfig taskConfig) {
this.map.put("aa", taskConfig);
}
public Map<String, OperationMonitoringTaskConfig> getMap(){
return this.map;
}
private APIManagerConfiguration apiManagerConfiguration; private APIManagerConfiguration apiManagerConfiguration;
@ -210,4 +225,11 @@ public class DeviceManagementDataHolder {
return pushNotificationProviderRepository; return pushNotificationProviderRepository;
} }
public DeviceTaskManagerService getDeviceTaskManagerService() {
return deviceTaskManagerService;
}
public void setDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) {
this.deviceTaskManagerService = deviceTaskManagerService;
}
} }

@ -53,6 +53,7 @@ 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.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService;
import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer;
import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.email.sender.core.service.EmailSenderService;
import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.ndatasource.core.DataSourceService;
@ -402,4 +403,18 @@ public class DeviceManagementServiceComponent {
DeviceManagementDataHolder.getInstance().setEmailSenderService(null); DeviceManagementDataHolder.getInstance().setEmailSenderService(null);
} }
protected void setDeviceTaskManagerService(DeviceTaskManagerService emailSenderService) {
if (log.isDebugEnabled()) {
}
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(emailSenderService);
}
protected void unsetDeviceTaskManagerService(DeviceTaskManagerService emailSenderService) {
if (log.isDebugEnabled()) {
}
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
}
} }

@ -0,0 +1,48 @@
/*
* Copyright (c) 2014, 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.device.mgt.core.internal;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
*
*/
public class DeviceMonitoringOperationDataHolder {
private static DeviceMonitoringOperationDataHolder thisInstance = new DeviceMonitoringOperationDataHolder();
private Map<String, OperationMonitoringTaskConfig> OperationConfigMap = Collections.synchronizedMap(
new HashMap<String, OperationMonitoringTaskConfig>());
public static DeviceMonitoringOperationDataHolder getInstance() {
return thisInstance;
}
public void addOperationMonitoringConfigToMap(String deviceType, OperationMonitoringTaskConfig OperationConfig) {
this.OperationConfigMap.put(deviceType, OperationConfig);
}
public Map<String, OperationMonitoringTaskConfig> getOperationMonitoringConfigFromMap(){
return this.OperationConfigMap;
}
}

@ -22,15 +22,19 @@ package org.wso2.carbon.device.mgt.core.internal;
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.osgi.service.component.ComponentContext; import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService;
import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl;
import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService;
import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl;
import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.ntask.core.service.TaskService;
import java.util.ArrayList;
import java.util.Map;
/** /**
* @scr.component name="org.wso2.carbon.device.task.manager" immediate="true" * @scr.component name="org.wso2.carbon.device.task.manager" immediate="true"
* @scr.reference name="device.ntask.component" * @scr.reference name="device.ntask.component"
@ -45,6 +49,8 @@ public class DeviceTaskManagerServiceComponent {
private static Log log = LogFactory.getLog(DeviceManagementServiceComponent.class); private static Log log = LogFactory.getLog(DeviceManagementServiceComponent.class);
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected void activate(ComponentContext componentContext) { protected void activate(ComponentContext componentContext) {
try { try {
@ -52,13 +58,14 @@ public class DeviceTaskManagerServiceComponent {
log.debug("Initializing device details retrieving task manager bundle."); log.debug("Initializing device details retrieving task manager bundle.");
} }
// This will start the device details retrieving task. // This will start the device details retrieving task.
boolean taskEnable = // DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl();
DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). // DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(
isEnabled(); // deviceTaskManagerService);
if (taskEnable) { // componentContext.getBundleContext().registerService(DeviceTaskManagerService.class,
DeviceTaskManagerService taskManagerService = new DeviceTaskManagerServiceImpl(); // deviceTaskManagerService, null);
taskManagerService.startTask();
} getDeviceOperationMonitoringConfig(componentContext);
componentContext.getBundleContext().registerService(DeviceInformationManager.class, componentContext.getBundleContext().registerService(DeviceInformationManager.class,
new DeviceInformationManagerImpl(), null); new DeviceInformationManagerImpl(), null);
@ -69,6 +76,26 @@ public class DeviceTaskManagerServiceComponent {
} }
} }
private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) throws DeviceMgtTaskException {
DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(
deviceTaskManagerService);
componentContext.getBundleContext().registerService(DeviceTaskManagerService.class,
deviceTaskManagerService, null);
Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder
.getInstance().getOperationMonitoringConfigFromMap();
for (String platformType : new ArrayList<String>(deviceConfigMap.keySet())) {
deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType));
deviceConfigMap.remove(platformType);
}
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected void deactivate(ComponentContext componentContext) { protected void deactivate(ComponentContext componentContext) {
try { try {

@ -25,9 +25,9 @@ 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.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TaskOperation;
import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
@ -61,9 +61,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated * This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated
@ -83,6 +81,7 @@ public class OperationManagerImpl implements OperationManager {
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private EnrollmentDAO enrollmentDAO; private EnrollmentDAO enrollmentDAO;
private NotificationStrategy notificationStrategy; private NotificationStrategy notificationStrategy;
private String deviceType;
public OperationManagerImpl() { public OperationManagerImpl() {
commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO();
@ -95,6 +94,11 @@ public class OperationManagerImpl implements OperationManager {
enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
} }
public OperationManagerImpl(String deviceType) {
this();
this.deviceType = deviceType;
}
public NotificationStrategy getNotificationStrategy() { public NotificationStrategy getNotificationStrategy() {
return notificationStrategy; return notificationStrategy;
} }
@ -103,8 +107,8 @@ public class OperationManagerImpl implements OperationManager {
this.notificationStrategy = notificationStrategy; this.notificationStrategy = notificationStrategy;
} }
public OperationManagerImpl(NotificationStrategy notificationStrategy) { public OperationManagerImpl(String deviceType, NotificationStrategy notificationStrategy) {
this(); this(deviceType);
this.notificationStrategy = notificationStrategy; this.notificationStrategy = notificationStrategy;
} }
@ -243,7 +247,7 @@ public class OperationManagerImpl implements OperationManager {
List<String> unAuthorizedDeviceList = new ArrayList<>(); List<String> unAuthorizedDeviceList = new ArrayList<>();
DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); DeviceIDHolder deviceIDHolder = new DeviceIDHolder();
try { try {
if (operation != null && isAuthenticationSkippedOperation(operation, deviceIds)) { if (operation != null && isAuthenticationSkippedOperation(operation)) {
authorizedDeviceList = deviceIds; authorizedDeviceList = deviceIds;
} else { } else {
boolean isAuthorized; boolean isAuthorized;
@ -885,11 +889,11 @@ public class OperationManagerImpl implements OperationManager {
return CarbonContext.getThreadLocalCarbonContext().getUsername(); return CarbonContext.getThreadLocalCarbonContext().getUsername();
} }
private boolean isAuthenticationSkippedOperation(Operation operation, List<DeviceIdentifier> deviceIds) { private boolean isAuthenticationSkippedOperation(Operation operation) {
//This is to check weather operations are coming from the task related to retrieving device information. //This is to check weather operations are coming from the task related to retrieving device information.
DeviceTaskManager taskManager = new DeviceTaskManagerImpl(); DeviceTaskManager taskManager = new DeviceTaskManagerImpl(deviceType);
if (taskManager.isTaskOperation(operation.getCode(), deviceIds)) { if (taskManager.isTaskOperation(operation.getCode())) {
return true; return true;
} }
@ -1034,28 +1038,30 @@ public class OperationManagerImpl implements OperationManager {
} }
private boolean isTaskScheduledOperation(Operation operation, List<DeviceIdentifier> deviceIds) { private boolean isTaskScheduledOperation(Operation operation, List<DeviceIdentifier> deviceIds) {
List<TaskOperation> taskOperations = new ArrayList<>();
Map<String, List<TaskOperation>> deviceTypeSpecificTasks = new HashMap<>();
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider(); getDeviceManagementProvider();
deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList();//Get task list from each device type List<MonitoringOperation> monitoringOperations = deviceManagementProviderService.getMonitoringOperationList(deviceType);//Get task list from each device type
for(DeviceIdentifier deviceIdentifier : deviceIds){ for(MonitoringOperation op : monitoringOperations){
String deviceType = deviceIdentifier.getType();
for(String dti : deviceTypeSpecificTasks.keySet()){
if (dti.equals(deviceType)) {
taskOperations = deviceTypeSpecificTasks.get(dti);
for(TaskOperation op : taskOperations){
if (operation.getCode().equals(op.getTaskName())) { if (operation.getCode().equals(op.getTaskName())) {
return true; return true;
} }
} }
}
}
// for(String dti : taskOperation){
} // if (dti.equals(deviceType)) {
// monitoringOperations = deviceTypeSpecificTasks.get(dti);
//
// }
// }
//
// for(DeviceIdentifier deviceIdentifier : deviceIds){
// String deviceType = deviceIdentifier.getType();
//
//
//
// }
// TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). // TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
// getTaskConfiguration(); // getTaskConfiguration();

@ -17,17 +17,24 @@
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.wso2.carbon.device.mgt.common.*; 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.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import java.util.HashMap;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Proxy class for all Device Management related operations that take the corresponding plugin type in * Proxy class for all Device Management related operations that take the corresponding plugin type in
@ -282,6 +289,10 @@ public interface DeviceManagementProviderService {
int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException; int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException;
Map<String, List<TaskOperation>> getTaskList(); List<MonitoringOperation> getMonitoringOperationList(String deviceType);
int getDeviceMonitoringFrequency(String deviceType);
boolean isDeviceMonitoringEnabled(String deviceType);
} }

@ -31,9 +31,10 @@ import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TaskOperation;
import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
@ -1217,24 +1218,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public Map<String, List<TaskOperation>> getTaskList() { public List<MonitoringOperation> getMonitoringOperationList(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
Map<DeviceTypeIdentifier, DeviceManagementService> deviceManagementServiceMap = // Map<DeviceTypeIdentifier, DeviceManagementService> deviceManagementServiceMap =
pluginRepository.getAllDeviceManagementServices(tenantId); // pluginRepository.getAllDeviceManagementServices(tenantId);
DeviceManagementService dms; DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId);
String deviceType; // ;
List<TaskOperation> taskOperations; // OperationMonitoringTaskConfig operationMonitoringTaskConfig;
Map<String, List<TaskOperation>> deviceTypeSpecificTasks = new HashMap<>(); //Map<String, List<MonitoringOperation>> deviceTypeSpecificMonitoringOperations = new HashMap<>();
for(DeviceTypeIdentifier dti : deviceManagementServiceMap.keySet()){ // for(DeviceTypeIdentifier dti : deviceManagementServiceMap.keySet()){
dms = deviceManagementServiceMap.get(dti); // dms = deviceManagementServiceMap.get(dti);
taskOperations = dms.getTasksForPlatform(); //
if (taskOperations != null) { // }
deviceType = dms.getType(); OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig();
deviceTypeSpecificTasks.put(deviceType, taskOperations); return operationMonitoringTaskConfig.getMonitoringOperation();
} }
@Override
public int getDeviceMonitoringFrequency(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId);
OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig();
return operationMonitoringTaskConfig.getFrequency();
} }
return deviceTypeSpecificTasks;
@Override
public boolean isDeviceMonitoringEnabled(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId);
OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig();
return operationMonitoringTaskConfig.isEnabled();
} }
@Override @Override

@ -19,20 +19,16 @@
package org.wso2.carbon.device.mgt.core.task; package org.wso2.carbon.device.mgt.core.task;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import java.util.List;
public interface DeviceTaskManager { public interface DeviceTaskManager {
/** // /**
* This method will get the operation list from configurations. // * This method will get the operation list from configurations.
* // *
* @return - list of Task Operations. // * @return - list of Task Operations.
* @throws DeviceMgtTaskException // * @throws DeviceMgtTaskException
*/ // */
List<org.wso2.carbon.device.mgt.common.TaskOperation> getOperationList(String deviceType) // List<MonitoringOperation> getOperationList(String deviceType)
throws DeviceMgtTaskException; // throws DeviceMgtTaskException;
/** /**
* This method will take the monitoring frequency. * This method will take the monitoring frequency.
@ -41,12 +37,12 @@ public interface DeviceTaskManager {
*/ */
int getTaskFrequency() throws DeviceMgtTaskException; int getTaskFrequency() throws DeviceMgtTaskException;
/** // /**
* This method will return the task clazz from configurations. // * This method will return the task clazz from configurations.
* @return - Fully qualified class name. // * @return - Fully qualified class name.
* @throws DeviceMgtTaskException // * @throws DeviceMgtTaskException
*/ // */
String getTaskImplementedClazz() throws DeviceMgtTaskException; // String getTaskImplementedClazz() throws DeviceMgtTaskException;
/** /**
* This method checks wheather task is enabled in config file. * This method checks wheather task is enabled in config file.
@ -63,12 +59,12 @@ public interface DeviceTaskManager {
void addOperations() throws DeviceMgtTaskException; void addOperations() throws DeviceMgtTaskException;
/** // /**
* This method will return the operation names which should be added in each iterations. // * This method will return the operation names which should be added in each iterations.
* @return // * @return
* @throws DeviceMgtTaskException // * @throws DeviceMgtTaskException
*/ // */
List<String> getValidOperationNames(String deviceType) throws DeviceMgtTaskException; // List<String> getValidOperationNames(String deviceType) throws DeviceMgtTaskException;
/** /**
* This method will check wheather give operation is added by the task. * This method will check wheather give operation is added by the task.
@ -76,6 +72,6 @@ public interface DeviceTaskManager {
* @return - true or false * @return - true or false
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
boolean isTaskOperation(String opName, List<DeviceIdentifier> deviceIds); boolean isTaskOperation(String opName);
} }

@ -19,26 +19,33 @@
package org.wso2.carbon.device.mgt.core.task; package org.wso2.carbon.device.mgt.core.task;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
public interface DeviceTaskManagerService { public interface DeviceTaskManagerService {
/** /**
* This method will stop the task. * This method will start the task.
* @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void startTask() throws DeviceMgtTaskException; void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
throws DeviceMgtTaskException;
/** /**
* This method will start the task. * This method will stop the task.
* @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void stopTask() throws DeviceMgtTaskException; void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
throws DeviceMgtTaskException;
/** /**
* This will update the task frequency which it runs. * This will update the task frequency which it runs.
* @param frequency - integer * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void updateTask(int frequency) throws DeviceMgtTaskException; void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
throws DeviceMgtTaskException;
} }

@ -31,15 +31,15 @@ public class DeviceDetailsRetrieverTask implements Task {
private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class);
// private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); // private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl();
private String deviceType;
@Override @Override
public void setProperties(Map<String, String> map) { public void setProperties(Map<String, String> map) {
deviceType = map.get("DEVICE_TYPE");
} }
@Override @Override
public void init() { public void init() {
} }
@Override @Override
@ -49,7 +49,7 @@ public class DeviceDetailsRetrieverTask implements Task {
log.debug("Device details retrieving task started to run."); log.debug("Device details retrieving task started to run.");
} }
DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType);
try { try {
deviceTaskManager.addOperations(); deviceTaskManager.addOperations();

@ -22,13 +22,11 @@ package org.wso2.carbon.device.mgt.core.task.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.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.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.TaskOperation; import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
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.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -42,65 +40,51 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
public class DeviceTaskManagerImpl implements DeviceTaskManager { public class DeviceTaskManagerImpl implements DeviceTaskManager {
private static Log log = LogFactory.getLog(DeviceTaskManagerImpl.class); private static Log log = LogFactory.getLog(DeviceTaskManagerImpl.class);
private String deviceType;
private static Map<Integer, Map<String, Long>> map = new HashMap<>(); private static Map<Integer, Map<String, Long>> map = new HashMap<>();
public DeviceTaskManagerImpl(String deviceType) {
this.deviceType = deviceType;
}
@Override
//get device type specific operations //get device type specific operations
public List<TaskOperation> getOperationList(String deviceType) throws DeviceMgtTaskException { public List<MonitoringOperation> getOperationList() throws DeviceMgtTaskException {
List<TaskOperation> taskOperations = new ArrayList<>(); DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder
Map<String, List<TaskOperation>> deviceTypeSpecificTasks; .getInstance().
//This Map contains task list against device type
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider(); getDeviceManagementProvider();
deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList();//Get task list from each device type return deviceManagementProviderService.getMonitoringOperationList(
for(String dti : deviceTypeSpecificTasks.keySet()){ deviceType);//Get task list from each device type
if (dti.equals(deviceType)) {
taskOperations = deviceTypeSpecificTasks.get(dti);
}
}
return taskOperations;
} }
private List<String> getDeviceTypes() { @Override
List<String> operationPlatforms = new ArrayList<>(); public int getTaskFrequency() throws DeviceMgtTaskException {
Map<String, List<TaskOperation>> deviceTypeSpecificTasks; DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder
.getInstance().
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider(); getDeviceManagementProvider();
deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList();
Set<String> platformTypes = deviceTypeSpecificTasks.keySet(); return deviceManagementProviderService.getDeviceMonitoringFrequency(deviceType);
for(String platformType : platformTypes ){
operationPlatforms.add(platformType);
}
return operationPlatforms;
}
@Override
public int getTaskFrequency() throws DeviceMgtTaskException {
return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration().
getFrequency();
} }
@Override // @Override
public String getTaskImplementedClazz() throws DeviceMgtTaskException { // public String getTaskImplementedClazz() throws DeviceMgtTaskException {
return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). // return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration().
getTaskClazz(); // getTaskClazz();
} // }
@Override @Override
public boolean isTaskEnabled() throws DeviceMgtTaskException { public boolean isTaskEnabled() throws DeviceMgtTaskException {
return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder
isEnabled(); .getInstance().
getDeviceManagementProvider();
return deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType);
} }
@ -111,10 +95,8 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
try { try {
List<Device> devices; List<Device> devices;
List<String> operations; List<String> operations;
List<String> deviceTypes = this.getDeviceTypes();//list available device types
for(String deviceType : deviceTypes){ operations = this.getValidOperationNames(); //list operations for each device type
operations = this.getValidOperationNames(deviceType); //list operations for each device type
devices = deviceManagementProviderService.getAllDevices(deviceType);//list devices for each type devices = deviceManagementProviderService.getAllDevices(deviceType);//list devices for each type
if (!devices.isEmpty()) { if (!devices.isEmpty()) {
for (String str : operations) { for (String str : operations) {
@ -130,8 +112,6 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
log.debug("No devices are available to perform the operations."); log.debug("No devices are available to perform the operations.");
} }
} }
}
} catch (InvalidDeviceException e) { } catch (InvalidDeviceException e) {
throw new DeviceMgtTaskException("Invalid DeviceIdentifiers found.", e); throw new DeviceMgtTaskException("Invalid DeviceIdentifiers found.", e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -141,16 +121,15 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
} }
} }
@Override private List<String> getValidOperationNames() throws DeviceMgtTaskException {
public List<String> getValidOperationNames(String deviceType) throws DeviceMgtTaskException {
List<TaskOperation> taskOperations = this.getOperationList(deviceType); List<MonitoringOperation> monitoringOperations = this.getOperationList();
List<String> opNames = new ArrayList<>(); List<String> opNames = new ArrayList<>();
Long milliseconds = System.currentTimeMillis(); Long milliseconds = System.currentTimeMillis();
int frequency = this.getTaskFrequency(); int frequency = this.getTaskFrequency();
Map<String, Long> mp = Utils.getTenantedTaskOperationMap(map); Map<String, Long> mp = Utils.getTenantedTaskOperationMap(map);
for (TaskOperation top : taskOperations) { for (MonitoringOperation top : monitoringOperations) {
if (!mp.containsKey(top.getTaskName())) { if (!mp.containsKey(top.getTaskName())) {
opNames.add(top.getTaskName()); opNames.add(top.getTaskName());
mp.put(top.getTaskName(), milliseconds); mp.put(top.getTaskName(), milliseconds);
@ -173,13 +152,11 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
@Override @Override
public boolean isTaskOperation(String opName, List<DeviceIdentifier> deviceIds) { public boolean isTaskOperation(String opName) {
for(DeviceIdentifier deviceIdentifier : deviceIds){
String deviceType = deviceIdentifier.getType();
try { try {
List<TaskOperation> taskOperations = this.getOperationList(deviceType); List<MonitoringOperation> monitoringOperations = this.getOperationList();
for (TaskOperation taop : taskOperations) { for (MonitoringOperation taop : monitoringOperations) {
if (taop.getTaskName().equalsIgnoreCase(opName)) { if (taop.getTaskName().equalsIgnoreCase(opName)) {
return true; return true;
} }
@ -187,7 +164,6 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
} catch (DeviceMgtTaskException e) { } catch (DeviceMgtTaskException e) {
// ignoring the error, no need to throw, If error occurs, return value will be false. // ignoring the error, no need to throw, If error occurs, return value will be false.
} }
}
return false; return false;

@ -22,9 +22,9 @@ package org.wso2.carbon.device.mgt.core.task.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.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
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.task.DeviceMgtTaskException; import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService;
import org.wso2.carbon.ntask.common.TaskException; import org.wso2.carbon.ntask.common.TaskException;
import org.wso2.carbon.ntask.core.TaskInfo; import org.wso2.carbon.ntask.core.TaskInfo;
@ -36,23 +36,21 @@ import java.util.Map;
public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
public static final String TASK_TYPE = "DEVICE_DETAILS"; public static final String TASK_TYPE = "DEVICE_MONITORING";
public static final String TASK_NAME = "DEVICE_DETAILS_TASK";
public static final String TENANT_ID = "TENANT_ID"; public static final String TENANT_ID = "TENANT_ID";
private static String TASK_CLASS = "org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask"; private static String TASK_CLASS = "org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask";
private DeviceTaskManager deviceTaskManager; // private DeviceTaskManager deviceTaskManager;
private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class); private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class);
@Override @Override
public void startTask() throws DeviceMgtTaskException { public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException {
deviceTaskManager = new DeviceTaskManagerImpl(); // String TASK_NAME = deviceType;
if (!deviceTaskManager.isTaskEnabled()) {
throw new DeviceMgtTaskException("Task cannot be started, Please enable the task in cdm-config.xml file."); log.info("Task adding for " + deviceType);
}
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -62,23 +60,27 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device details retrieving task is started for the tenant id " + tenantId); log.debug("Device details retrieving task is started for the tenant id " + tenantId);
log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager.getTaskFrequency()); // log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager
// .getTaskFrequency());
log.debug("Device details retrieving task is at frequency of : " + operationMonitoringTaskConfig
.getFrequency());
} }
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency()); // triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency());
triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency());
triggerInfo.setRepeatCount(-1); triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(TENANT_ID, String.valueOf(tenantId)); properties.put(TENANT_ID, String.valueOf(tenantId));
properties.put("DEVICE_TYPE", deviceType);
if (!taskManager.isTaskScheduled(TASK_NAME)) { if (!taskManager.isTaskScheduled(deviceType)) {
TaskInfo taskInfo = new TaskInfo(TASK_NAME, TASK_CLASS, TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo);
properties, triggerInfo);
taskManager.registerTask(taskInfo); taskManager.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName()); taskManager.rescheduleTask(taskInfo.getName());
@ -94,13 +96,15 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
} }
@Override @Override
public void stopTask() throws DeviceMgtTaskException { public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException {
// String TASK_NAME = deviceType;
try { try {
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
if (taskService.isServerInit()) { if (taskService.isServerInit()) {
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
taskManager.deleteTask(TASK_NAME); taskManager.deleteTask(deviceType);
} }
} catch (TaskException e) { } catch (TaskException e) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -110,29 +114,27 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
} }
@Override @Override
public void updateTask(int frequency) throws DeviceMgtTaskException { public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException {
if (!deviceTaskManager.isTaskEnabled()) { // String TASK_NAME = deviceType;
throw new DeviceMgtTaskException("Task cannot be updated, Please enable the task in cdm-config.xml file.");
}
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceTaskManager = new DeviceTaskManagerImpl(); // deviceTaskManager = new DeviceTaskManagerImpl();
try { try {
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
if (taskManager.isTaskScheduled(TASK_NAME)) { if (taskManager.isTaskScheduled(deviceType)) {
taskManager.deleteTask(TASK_NAME); taskManager.deleteTask(deviceType);
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
triggerInfo.setIntervalMillis(frequency); triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency());
triggerInfo.setRepeatCount(-1); triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(TENANT_ID, String.valueOf(tenantId)); properties.put(TENANT_ID, String.valueOf(tenantId));
TaskInfo taskInfo = new TaskInfo(TASK_NAME, TASK_CLASS, properties, TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties,
triggerInfo); triggerInfo);
taskManager.registerTask(taskInfo); taskManager.registerTask(taskInfo);

@ -19,14 +19,12 @@ package org.wso2.carbon.device.mgt.core;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig;
import org.wso2.carbon.device.mgt.common.TaskOperation;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import java.util.List;
public class TestDeviceManagementService implements DeviceManagementService { public class TestDeviceManagementService implements DeviceManagementService {
private String providerType; private String providerType;
@ -42,7 +40,7 @@ public class TestDeviceManagementService implements DeviceManagementService {
} }
@Override @Override
public List<TaskOperation> getTasksForPlatform(){ public OperationMonitoringTaskConfig getOperationMonitoringConfig(){
return null; return null;
} }

@ -48,10 +48,10 @@
<Platform>windows</Platform> <Platform>windows</Platform>
</Platforms> </Platforms>
</PolicyConfiguration> </PolicyConfiguration>
<TaskConfiguration> <!--<TaskConfiguration>-->
<Enable>true</Enable> <!--<Enable>true</Enable>-->
<Frequency>600000</Frequency> <!--<Frequency>600000</Frequency>-->
</TaskConfiguration> <!--</TaskConfiguration>-->
<!-- Default Page size configuration for paginated DM APIs--> <!-- Default Page size configuration for paginated DM APIs-->
<PaginationConfiguration> <PaginationConfiguration>
<DeviceListPageSize>20</DeviceListPageSize> <DeviceListPageSize>20</DeviceListPageSize>

Loading…
Cancel
Save