Add Tenanted Device Task Configurations

This allowing users to configure task frequencies independently for each tenant.
pull/370/head
Pramila Niroshan 8 months ago
parent d050736aaf
commit 03f95e8cdf

@ -0,0 +1,35 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.device.mgt.common.metadata.mgt;
import java.util.HashMap;
public class DeviceFrequencyMap extends HashMap<String, DeviceFrequencyMap.Device> {
public static class Device {
private int frequency;
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
}
}

@ -0,0 +1,47 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.device.mgt.common.metadata.mgt;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
public interface DeviceTaskConfigurationService {
/**
* This method is useful to add Default Task Frequency from device type xml
*
* @throws MetadataManagementException if error while adding default TaskFrequency
*/
void addDefaultTaskFrequency(int tenantId) throws MetadataManagementException;
/**
* This method is useful to add custom Task Frequency for device types
*
* @throws MetadataManagementException if error while adding default TaskFrequency
*/
void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException;
/**
* This method is useful to get existing Task Frequency by device type and tenant id
*
* @throws MetadataManagementException if error while getting existing TaskFrequency
*/
int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException;
}

@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.core.internal;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.TaskConfigurationStartupHandler;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -416,6 +417,9 @@ public class DeviceManagementServiceComponent {
ExecutorService executorService = Executors.newFixedThreadPool(50); ExecutorService executorService = Executors.newFixedThreadPool(50);
DeviceManagementDataHolder.getInstance().setEventConfigExecutors(executorService); DeviceManagementDataHolder.getInstance().setEventConfigExecutors(executorService);
/* Registering Task Configuration StartupHandler Service */
bundleContext.registerService(ServerStartupObserver.class, new TaskConfigurationStartupHandler(), null);
} }
private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException { private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException {

@ -0,0 +1,178 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceFrequencyMap;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType;
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataDAO;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurationService {
private static final Log log = LogFactory.getLog(DeviceTaskConfigurationServiceImpl.class);
private final MetadataDAO metadataDAO;
public DeviceTaskConfigurationServiceImpl() {
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
}
private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
metadataDAO.addMetadata(tenantId, metadata);
if (log.isDebugEnabled()) {
log.debug(key + " metadata entry has been inserted successfully");
}
}
private Metadata constructTaskFrequencyMetadata(Map<String, Integer> deviceFrequencies) {
JsonObject jsonObject = new JsonObject();
for (Map.Entry<String, Integer> entry : deviceFrequencies.entrySet()) {
String deviceType = entry.getKey();
int frequency = entry.getValue();
// Create a JsonObject for the current device type
JsonObject deviceObject = new JsonObject();
deviceObject.addProperty("frequency", String.valueOf(frequency));
jsonObject.add(deviceType, deviceObject);
}
Metadata metadata = new Metadata();
metadata.setMetaKey(MetadataConstants.DEVICE_TASK_FREQUENCY);
metadata.setMetaValue(jsonObject.toString());
return metadata;
}
private int getDefaultTaskFrequency(DeviceManagementProviderService dms, String deviceType) {
OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getDeviceMonitoringConfig(deviceType);
return operationMonitoringTaskConfig.getFrequency();
}
@Override
public void addDefaultTaskFrequency(int tenantId) throws MetadataManagementException {
try {
Map<String, Integer> deviceFrequencies = new HashMap<>();
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
List<DeviceType> deviceTypes = dms.getDeviceTypes();
for (DeviceType deviceType : deviceTypes) {
deviceFrequencies.put(deviceType.getName(), getDefaultTaskFrequency(dms, deviceType.getName()));
}
MetadataManagementDAOFactory.beginTransaction();
if (!metadataDAO.isExist(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY)) {
Metadata defaultTaskFrequencyMetadata = constructTaskFrequencyMetadata(deviceFrequencies);
// Add default TaskFrequency
addMetadataEntry(tenantId, defaultTaskFrequencyMetadata, MetadataConstants.DEVICE_TASK_FREQUENCY);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while inserting default task frequency metadata entry.";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while trying to obtain device types.";
log.error(msg, e);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
}
@Override
public void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException {
try {
Map<String, Integer> deviceFrequencies = new HashMap<>();
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
List<DeviceType> deviceTypes = dms.getDeviceTypes();
for (DeviceType deviceType : deviceTypes) {
deviceFrequencies.put(deviceType.getName(), frequency);
}
MetadataManagementDAOFactory.beginTransaction();
if (!metadataDAO.isExist(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY)) {
Metadata defaultTaskFrequencyMetadata = constructTaskFrequencyMetadata(deviceFrequencies);
// Add Task Frequency entry for given Frequency
addMetadataEntry(tenantId, defaultTaskFrequencyMetadata, MetadataConstants.DEVICE_TASK_FREQUENCY);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while inserting default task frequency metadata entry.";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while trying to obtain device types.";
log.error(msg, e);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
}
@Override
public int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException {
try {
MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY);
String metaValue = metadata.getMetaValue();
Gson gson = new Gson();
DeviceFrequencyMap deviceFrequencyMap = gson.fromJson(metaValue, DeviceFrequencyMap.class);
// Check if the device type exists in the map
if (deviceFrequencyMap.containsKey(deviceType)) {
// Retrieve the frequency for the given device type
return deviceFrequencyMap.get(deviceType).getFrequency();
} else {
throw new MetadataManagementException("Device type not found: " + deviceType);
}
} catch (MetadataManagementDAOException e) {
String msg = "Error occurred while retrieving device task frequency metadata for tenant:" + tenantId;
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
}
}

@ -0,0 +1,70 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
public class TaskConfigurationStartupHandler implements ServerStartupObserver {
private static final Log log = LogFactory.getLog(TaskConfigurationStartupHandler.class);
@Override
public void completingServerStartup() {
}
@Override
public void completedServerStartup() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
RealmService realmService = DeviceManagementDataHolder.getInstance().getRealmService();
if (log.isDebugEnabled()) {
log.debug("Server has just started, hence started adding default task frequencies");
}
try {
deviceTaskConfigurationService.addDefaultTaskFrequency(MultitenantConstants.SUPER_TENANT_ID);
Tenant[] tenantArray = realmService.getTenantManager().getAllTenants();
if (tenantArray != null) {
for (Tenant tenant : tenantArray) {
deviceTaskConfigurationService.addDefaultTaskFrequency(tenant.getId());
}
}
} catch (MetadataManagementException e) {
String msg = "Error occurred while adding default task frequency metadata entry.";
log.error(msg, e);
} catch (UserStoreException e) {
log.error("Error occurred while trying to get the available tenants.", e);
}
log.info("Finish adding default task frequencies");
}
});
t.start();
}
}

@ -23,4 +23,5 @@ public class MetadataConstants {
public static final String ALLOWED_DEVICE_STATUS_META_KEY = "allowed_device_status"; public static final String ALLOWED_DEVICE_STATUS_META_KEY = "allowed_device_status";
public static final String IS_DEVICE_STATUS_CHECK_META_KEY = "device-status-check"; public static final String IS_DEVICE_STATUS_CHECK_META_KEY = "device-status-check";
public static final String DEVICE_TASK_FREQUENCY = "device_task_frequency";
} }

@ -20,6 +20,7 @@
package io.entgra.device.mgt.core.device.mgt.core.task; package io.entgra.device.mgt.core.device.mgt.core.task;
import io.entgra.device.mgt.core.device.mgt.common.DynamicTaskContext; import io.entgra.device.mgt.core.device.mgt.common.DynamicTaskContext;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
public interface DeviceTaskManager { public interface DeviceTaskManager {
@ -58,7 +59,7 @@ public interface DeviceTaskManager {
* This method will add the operations to devices * This method will add the operations to devices
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void addOperations(DynamicTaskContext dynamicTaskContext) throws DeviceMgtTaskException; void addOperations(DynamicTaskContext dynamicTaskContext, int frequency) throws DeviceMgtTaskException, MetadataManagementException;
// /** // /**

@ -18,6 +18,9 @@
package io.entgra.device.mgt.core.device.mgt.core.task; package io.entgra.device.mgt.core.device.mgt.core.task;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceTaskConfigurationServiceImpl;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import java.util.ArrayList; import java.util.ArrayList;
@ -67,4 +70,10 @@ public class Utils {
} }
} }
public static int getTenantedTaskFrequency(int tenantId, String deviceType) throws MetadataManagementException {
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
return deviceTaskConfigurationService.getTaskFrequency(deviceType, tenantId);
}
} }

@ -18,6 +18,8 @@
package io.entgra.device.mgt.core.device.mgt.core.task.impl; package io.entgra.device.mgt.core.device.mgt.core.task.impl;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.core.task.Utils;
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.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
@ -64,13 +66,13 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
} }
for (Integer tenant : tenants) { for (Integer tenant : tenants) {
if (MultitenantConstants.SUPER_TENANT_ID == tenant) { if (MultitenantConstants.SUPER_TENANT_ID == tenant) {
this.executeTask(operationMonitoringTaskConfig, startupOperationConfig); this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, deviceType));
continue; continue;
} }
try { try {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true);
this.executeTask(operationMonitoringTaskConfig, startupOperationConfig); this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenant, deviceType));
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
@ -78,6 +80,9 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Error occurred while trying to get the available tenants " + log.error("Error occurred while trying to get the available tenants " +
"from device manager provider service.", e); "from device manager provider service.", e);
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting task frequency metadata entry.";
log.error(msg, e);
} }
} }
@ -87,7 +92,7 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
* @param startupOperationConfig which contains startup operations and realted details * @param startupOperationConfig which contains startup operations and realted details
*/ */
private void executeTask(OperationMonitoringTaskConfig operationMonitoringTaskConfig, private void executeTask(OperationMonitoringTaskConfig operationMonitoringTaskConfig,
StartupOperationConfig startupOperationConfig) { StartupOperationConfig startupOperationConfig, int frequency) {
DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType,
operationMonitoringTaskConfig, operationMonitoringTaskConfig,
startupOperationConfig); startupOperationConfig);
@ -97,11 +102,14 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
//pass the configurations also from here, monitoring tasks //pass the configurations also from here, monitoring tasks
try { try {
if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) { if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) {
deviceTaskManager.addOperations(getTaskContext()); deviceTaskManager.addOperations(getTaskContext(), frequency);
} }
} catch (DeviceMgtTaskException e) { } catch (DeviceMgtTaskException e) {
log.error("Error occurred while trying to add the operations to " + log.error("Error occurred while trying to add the operations to " +
"device to retrieve device details.", e); "device to retrieve device details.", e);
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting task frequency metadata entry.";
log.error(msg, e);
} }
} }

@ -100,11 +100,11 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
@Override @Override
public void addOperations(DynamicTaskContext dynamicTaskContext) throws DeviceMgtTaskException { public void addOperations(DynamicTaskContext dynamicTaskContext, int frequency) throws DeviceMgtTaskException {
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider(); getDeviceManagementProvider();
//list operations for device type //list operations for device type
List<String> operations = this.getValidOperationNames(); List<String> operations = this.getValidOperationNames(frequency);
if (operations.isEmpty()) { if (operations.isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No operations are available."); log.debug("No operations are available.");
@ -125,12 +125,11 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager {
} }
} }
private List<String> getValidOperationNames() throws DeviceMgtTaskException { private List<String> getValidOperationNames(int frequency) throws DeviceMgtTaskException {
List<MonitoringOperation> monitoringOperations = this.getOperationList(); 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();
Map<String, Long> mp = Utils.getTenantedTaskOperationMap(map, deviceType); Map<String, Long> mp = Utils.getTenantedTaskOperationMap(map, deviceType);
for (MonitoringOperation top : monitoringOperations) { for (MonitoringOperation top : monitoringOperations) {

@ -18,6 +18,8 @@
package io.entgra.device.mgt.core.device.mgt.core.task; package io.entgra.device.mgt.core.device.mgt.core.task;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceTaskConfigurationServiceImpl;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -63,6 +65,7 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceTaskManagerTest.class); private static final Log log = LogFactory.getLog(DeviceTaskManagerTest.class);
private static final String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE"; private static final String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE";
private static final int FREQUENCY = 60000;
private List<DeviceIdentifier> deviceIds; private List<DeviceIdentifier> deviceIds;
private DeviceTaskManager deviceTaskManager; private DeviceTaskManager deviceTaskManager;
private DeviceManagementProviderService deviceMgtProviderService; private DeviceManagementProviderService deviceMgtProviderService;
@ -121,9 +124,9 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
} }
@Test(groups = "Device Task Manager Test Group", description = "Testing adding operations to devices.") @Test(groups = "Device Task Manager Test Group", description = "Testing adding operations to devices.")
public void testAddOperation() throws DeviceMgtTaskException, OperationManagementException { public void testAddOperation() throws DeviceMgtTaskException, OperationManagementException, MetadataManagementException {
log.info("Attempting to add operations for devices."); log.info("Attempting to add operations for devices.");
this.deviceTaskManager.addOperations(null); this.deviceTaskManager.addOperations(null,FREQUENCY);
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {
List<? extends Operation> operationList = this.operationManager.getOperations(deviceId); List<? extends Operation> operationList = this.operationManager.getOperations(deviceId);
Assert.assertNotNull(operationList); Assert.assertNotNull(operationList);
@ -134,25 +137,27 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
@Test(groups = "Device Task Manager Test Group", @Test(groups = "Device Task Manager Test Group",
description = "Testing adding operations when no devices are available") description = "Testing adding operations when no devices are available")
public void testAddOperationsWithoutDevices() throws DeviceManagementException, DeviceMgtTaskException { public void testAddOperationsWithoutDevices() throws DeviceManagementException, DeviceMgtTaskException, MetadataManagementException {
this.deviceMgtProviderService.registerDeviceType( this.deviceMgtProviderService.registerDeviceType(
new TestDeviceManagementService(NEW_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN)); new TestDeviceManagementService(NEW_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN));
DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE,
TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3));
taskManager.addOperations(null); taskManager.addOperations(null, FREQUENCY);
} }
@Test(groups = "Device Task Manager Test Group", dependsOnMethods = "testAddOperationsWithoutDevices", @Test(groups = "Device Task Manager Test Group", dependsOnMethods = "testAddOperationsWithoutDevices",
description = "Testing adding operations when no operations are scheduled") description = "Testing adding operations when no operations are scheduled")
public void testAddOperationsWithoutOperations() throws DeviceMgtTaskException { public void testAddOperationsWithoutOperations() throws DeviceMgtTaskException, MetadataManagementException {
DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE, DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE,
TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3)); TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3));
taskManager.addOperations(null); taskManager.addOperations(null, FREQUENCY);
} }
@Test(groups = "Device Task Manager Test Group", description = "Testing device detail retriever task execution") @Test(groups = "Device Task Manager Test Group", description = "Testing device detail retriever task execution")
public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException { public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException, MetadataManagementException {
DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask();
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
deviceTaskConfigurationService.addTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, FREQUENCY);
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE);
deviceDetailsRetrieverTask.setProperties(map); deviceDetailsRetrieverTask.setProperties(map);

@ -26,4 +26,5 @@ public interface TenantManagerService {
void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException;
void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException; void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException;
void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException;
} }

@ -43,4 +43,11 @@ public interface TenantManager {
* @throws TenantMgtException Throws when error occurred while adding default application categories * @throws TenantMgtException Throws when error occurred while adding default application categories
*/ */
void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException; void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException;
/**
* Add default device task configuration to a tenant described by the tenant info bean
* @param tenantInfoBean The info bean that provides tenant info
* @throws TenantMgtException Throws when error occurred while adding default TaskFrequency
*/
void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException;
} }

@ -113,6 +113,20 @@ public class TenantManagerImpl implements TenantManager {
} }
} }
@Override
public void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException {
initTenantFlow(tenantInfoBean);
try {
TenantMgtDataHolder.getInstance().getDeviceTaskConfigurationService().addDefaultTaskFrequency(tenantInfoBean.getTenantId());
} catch (MetadataManagementException e) {
String msg = "Error occurred while adding default TaskFrequencies";
log.error(msg, e);
throw new TenantMgtException(msg, e);
} finally {
endTenantFlow();
}
}
private void initTenantFlow(TenantInfoBean tenantInfoBean) { private void initTenantFlow(TenantInfoBean tenantInfoBean) {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();

@ -38,4 +38,9 @@ public class TenantManagerServiceImpl implements TenantManagerService {
public void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException { public void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException {
TenantMgtDataHolder.getInstance().getTenantManager().addDefaultDeviceStatusFilters(tenantInfoBean); TenantMgtDataHolder.getInstance().getTenantManager().addDefaultDeviceStatusFilters(tenantInfoBean);
} }
@Override
public void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException {
TenantMgtDataHolder.getInstance().getTenantManager().addDefaultDeviceTaskConfiguration(tenantInfoBean);
}
} }

@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
@ -34,6 +35,7 @@ public class TenantMgtDataHolder {
private RealmService realmService; private RealmService realmService;
private DeviceStatusManagementService deviceStatusManagementService; private DeviceStatusManagementService deviceStatusManagementService;
private DeviceTaskConfigurationService deviceTaskConfigurationService;
public RealmService getRealmService() { public RealmService getRealmService() {
return realmService; return realmService;
@ -78,4 +80,12 @@ public class TenantMgtDataHolder {
public void setDeviceStatusManagementService(DeviceStatusManagementService deviceStatusManagementService) { public void setDeviceStatusManagementService(DeviceStatusManagementService deviceStatusManagementService) {
this.deviceStatusManagementService = deviceStatusManagementService; this.deviceStatusManagementService = deviceStatusManagementService;
} }
public DeviceTaskConfigurationService getDeviceTaskConfigurationService() {
return deviceTaskConfigurationService;
}
public void setDeviceTaskConfigurationService(DeviceTaskConfigurationService deviceTaskConfigurationService) {
this.deviceTaskConfigurationService = deviceTaskConfigurationService;
}
} }

@ -19,7 +19,9 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceTaskConfigurationServiceImpl;
import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerService; import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerService;
import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl; import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl;
@ -70,6 +72,8 @@ public class TenantMgtServiceComponent {
componentContext.getBundleContext().registerService(DeviceStatusManagementService.class.getName(), componentContext.getBundleContext().registerService(DeviceStatusManagementService.class.getName(),
deviceStatusManagementService, null); deviceStatusManagementService, null);
TenantMgtDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); TenantMgtDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService);
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
TenantMgtDataHolder.getInstance().setDeviceTaskConfigurationService(deviceTaskConfigurationService);
DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener();
if(log.isDebugEnabled()) { if(log.isDebugEnabled()) {
log.info("Tenant management listener is registering"); log.info("Tenant management listener is registering");

@ -39,6 +39,7 @@ public class DeviceMgtTenantListener implements TenantMgtListener {
tenantManager.addDefaultRoles(tenantInfoBean); tenantManager.addDefaultRoles(tenantInfoBean);
tenantManager.addDefaultAppCategories(tenantInfoBean); tenantManager.addDefaultAppCategories(tenantInfoBean);
tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean); tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean);
tenantManager.addDefaultDeviceTaskConfiguration(tenantInfoBean);
} catch (TenantMgtException e) { } catch (TenantMgtException e) {
String msg = "Error occurred while executing tenant creation flow"; String msg = "Error occurred while executing tenant creation flow";
log.error(msg, e); log.error(msg, e);

Loading…
Cancel
Save