Add Tenant TaskConfigurations

dummy commit
device-task-configuration
Pramila Niroshan 8 months ago
parent d050736aaf
commit 174b10a4d6

@ -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
*
* @throws MetadataManagementException if error while adding default TaskFrequency
*/
void addDefaultTaskFrequency(int tenantId) throws MetadataManagementException;
/**
* This method is useful to reset existing TaskFrequency to default values in xml
*
* @throws MetadataManagementException if error while resetting default TaskFrequency
*/
void resetToDefaultTaskFrequency() throws MetadataManagementException;
/**
* This method is useful to get existing TaskFrequency by device type and tenant id
*
* @throws MetadataManagementException if error while getting existing TaskFrequency
*/
int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException;
}

@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.internal;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
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.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.DeviceStatusTaskPluginConfig;
@ -98,6 +99,7 @@ public class DeviceManagementDataHolder {
private DeviceStatusManagementService deviceStatusManagementService;
private APIApplicationServices apiApplicationServices;
private PublisherRESTAPIServices publisherRESTAPIServices;
private DeviceTaskConfigurationService deviceTaskConfigurationService;
private final Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap(
new HashMap<>());
@ -398,6 +400,14 @@ public class DeviceManagementDataHolder {
this.deviceStatusManagementService = deviceStatusManagementService;
}
public DeviceTaskConfigurationService getDeviceTaskConfigurationService() {
return deviceTaskConfigurationService;
}
public void setDeviceTaskConfigurationService(DeviceTaskConfigurationService deviceTaskConfigurationService) {
this.deviceTaskConfigurationService = deviceTaskConfigurationService;
}
public TraccarManagementService getTraccarManagementService() {
TraccarManagementService traccarManagementService;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();

@ -18,7 +18,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.core.metadata.mgt.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.*;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -33,6 +33,7 @@ import io.entgra.device.mgt.core.device.mgt.common.event.config.EventConfigurati
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoLocationProviderService;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
import io.entgra.device.mgt.core.device.mgt.common.notification.mgt.NotificationManagementService;
@ -62,8 +63,6 @@ import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.DeviceInform
import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import io.entgra.device.mgt.core.device.mgt.core.event.config.EventConfigurationProviderServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.geo.service.GeoLocationProviderServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.WhiteLabelManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.notification.mgt.NotificationManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory;
@ -416,6 +415,19 @@ public class DeviceManagementServiceComponent {
ExecutorService executorService = Executors.newFixedThreadPool(50);
DeviceManagementDataHolder.getInstance().setEventConfigExecutors(executorService);
// /* Registering Device Task manager Service */
// DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
// DeviceManagementDataHolder.getInstance().setDeviceTaskConfigurationService(deviceTaskConfigurationService);
// try {
// deviceTaskConfigurationService.addDefaultTaskFrequency(tenantId);
// } catch (Throwable e) {
// log.error("Error occurred while adding default device task frequency", e);
//
// }
// bundleContext.registerService(DeviceTaskConfigurationService.class.getName(), deviceTaskConfigurationService, null);
/* Registering Device Task manager Service */
bundleContext.registerService(ServerStartupObserver.class, new TaskConfigurationStartupHandler(), null);
}
private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException {

@ -0,0 +1,164 @@
/*
* 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(String deviceType) {
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
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();
log.error("addDefaultTaskFrequency");
for (DeviceType deviceType : deviceTypes) {
log.error(deviceType.getName());
log.error(getDefaultTaskFrequency(deviceType.getName()));
deviceFrequencies.put(deviceType.getName(), getDefaultTaskFrequency(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) {
throw new RuntimeException(e);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
}
@Override
public void resetToDefaultTaskFrequency() throws MetadataManagementException {
}
@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();
if(metaValue == null){
addDefaultTaskFrequency(tenantId);
}
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,58 @@
/*
* Copyright (c) 2018 - 2023, 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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.ServerStartupObserver;
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() {
if (log.isDebugEnabled()) {
log.debug("Server has just started, hence started publishing unpublished APIs");
log.debug("Total number of unpublished APIs: ");
}
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
try {
deviceTaskConfigurationService.addDefaultTaskFrequency(MultitenantConstants.SUPER_TENANT_ID);
} catch (MetadataManagementException e) {
throw new RuntimeException(e);
}
log.info("Finish Adding default task");
}
});
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 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;
import io.entgra.device.mgt.core.device.mgt.common.DynamicTaskContext;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
public interface DeviceTaskManager {
@ -58,7 +59,7 @@ public interface DeviceTaskManager {
* This method will add the operations to devices
* @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;
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.wso2.carbon.context.PrivilegedCarbonContext;
import java.util.ArrayList;
@ -67,4 +70,10 @@ public class Utils {
}
}
public static int getTenantedTaskFrequency(int tenantId, String deviceType) throws MetadataManagementException {
DeviceTaskConfigurationService deviceTaskConfigurationService = DeviceManagementDataHolder.getInstance().getDeviceTaskConfigurationService();
return deviceTaskConfigurationService.getTaskFrequency(deviceType, tenantId);
}
}

@ -18,6 +18,8 @@
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.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
@ -64,13 +66,13 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
}
for (Integer tenant : tenants) {
if (MultitenantConstants.SUPER_TENANT_ID == tenant) {
this.executeTask(operationMonitoringTaskConfig, startupOperationConfig);
this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(MultitenantConstants.SUPER_TENANT_ID , deviceType));
continue;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true);
this.executeTask(operationMonitoringTaskConfig, startupOperationConfig);
this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenant, deviceType));
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
@ -78,6 +80,9 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
} catch (DeviceManagementException e) {
log.error("Error occurred while trying to get the available tenants " +
"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
*/
private void executeTask(OperationMonitoringTaskConfig operationMonitoringTaskConfig,
StartupOperationConfig startupOperationConfig) {
StartupOperationConfig startupOperationConfig, int frequency) {
DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType,
operationMonitoringTaskConfig,
startupOperationConfig);
@ -97,11 +102,14 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
//pass the configurations also from here, monitoring tasks
try {
if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) {
deviceTaskManager.addOperations(getTaskContext());
deviceTaskManager.addOperations(getTaskContext(), frequency);
}
} catch (DeviceMgtTaskException e) {
log.error("Error occurred while trying to add the operations to " +
"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
public void addOperations(DynamicTaskContext dynamicTaskContext) throws DeviceMgtTaskException {
public void addOperations(DynamicTaskContext dynamicTaskContext, int frequency) throws DeviceMgtTaskException {
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider();
//list operations for device type
List<String> operations = this.getValidOperationNames();
List<String> operations = this.getValidOperationNames(frequency);
if (operations.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("No operations are available.");
@ -125,12 +125,14 @@ 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<String> opNames = new ArrayList<>();
Long milliseconds = System.currentTimeMillis();
int frequency = this.getTaskFrequency();
//int frequency = Utils.getTenantedTaskFrequency(tenantId,deviceType);
log.info("frequency" + frequency);
System.out.println("frequency" + frequency);
Map<String, Long> mp = Utils.getTenantedTaskOperationMap(map, deviceType);
for (MonitoringOperation top : monitoringOperations) {

@ -18,6 +18,8 @@
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 io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log;
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 String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE";
private static final int FREQUENCY = 60000;
private List<DeviceIdentifier> deviceIds;
private DeviceTaskManager deviceTaskManager;
private DeviceManagementProviderService deviceMgtProviderService;
@ -89,6 +92,8 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
HeartBeatManagementService heartBeatManagementService = new TestHeartBeatManagementService();
DeviceManagementDataHolder.getInstance()
.setHeartBeatService(heartBeatManagementService);
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceTaskConfigurationService(deviceTaskConfigurationService);
this.operationManager = PowerMockito.spy(
new OperationManagerImpl(TestDataHolder.TEST_DEVICE_TYPE, deviceManagementService));
try {
@ -121,9 +126,9 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
}
@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.");
this.deviceTaskManager.addOperations(null);
this.deviceTaskManager.addOperations(null,FREQUENCY);
for (DeviceIdentifier deviceId : deviceIds) {
List<? extends Operation> operationList = this.operationManager.getOperations(deviceId);
Assert.assertNotNull(operationList);
@ -134,25 +139,28 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
@Test(groups = "Device Task Manager Test Group",
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(
new TestDeviceManagementService(NEW_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN));
DeviceTaskManager taskManager = new DeviceTaskManagerImpl(NEW_DEVICE_TYPE,
TestDataHolder.generateMonitoringTaskConfig(true, 50000, 3));
taskManager.addOperations(null);
taskManager.addOperations(null, FREQUENCY);
}
@Test(groups = "Device Task Manager Test Group", dependsOnMethods = "testAddOperationsWithoutDevices",
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,
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")
public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException {
public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException, MetadataManagementException {
DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask();
DeviceTaskConfigurationService deviceTaskConfigurationService = DeviceManagementDataHolder
.getInstance().getDeviceTaskConfigurationService();
deviceTaskConfigurationService.addDefaultTaskFrequency(MultitenantConstants.SUPER_TENANT_ID);
Map<String, String> map = new HashMap<>();
map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE);
deviceDetailsRetrieverTask.setProperties(map);

@ -26,4 +26,5 @@ public interface TenantManagerService {
void addDefaultAppCategories(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
*/
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 TaskFrequency";
log.error(msg, e);
throw new TenantMgtException(msg, e);
} finally {
endTenantFlow();
}
}
private void initTenantFlow(TenantInfoBean tenantInfoBean) {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();

@ -38,4 +38,9 @@ public class TenantManagerServiceImpl implements TenantManagerService {
public void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException {
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.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.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
import org.wso2.carbon.user.core.service.RealmService;
@ -34,6 +35,7 @@ public class TenantMgtDataHolder {
private RealmService realmService;
private DeviceStatusManagementService deviceStatusManagementService;
private DeviceTaskConfigurationService deviceTaskConfigurationService;
public RealmService getRealmService() {
return realmService;
@ -78,4 +80,12 @@ public class TenantMgtDataHolder {
public void setDeviceStatusManagementService(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.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.DeviceTaskConfigurationServiceImpl;
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.impl.TenantManagerImpl;
@ -70,6 +72,8 @@ public class TenantMgtServiceComponent {
componentContext.getBundleContext().registerService(DeviceStatusManagementService.class.getName(),
deviceStatusManagementService, null);
TenantMgtDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService);
DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl();
TenantMgtDataHolder.getInstance().setDeviceTaskConfigurationService(deviceTaskConfigurationService);
DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener();
if(log.isDebugEnabled()) {
log.info("Tenant management listener is registering");

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

Loading…
Cancel
Save