diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceFrequencyMap.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceFrequencyMap.java new file mode 100644 index 0000000000..fa5ca3328a --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceFrequencyMap.java @@ -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 { + public static class Device { + private int frequency; + + public int getFrequency() { + return frequency; + } + + public void setFrequency(int frequency) { + this.frequency = frequency; + } + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceTaskConfigurationService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceTaskConfigurationService.java new file mode 100644 index 0000000000..f0520351cc --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceTaskConfigurationService.java @@ -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; + +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java index b3bc1f2f0d..b36ad79eed 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -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.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 org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -416,6 +417,9 @@ public class DeviceManagementServiceComponent { ExecutorService executorService = Executors.newFixedThreadPool(50); DeviceManagementDataHolder.getInstance().setEventConfigExecutors(executorService); + + /* Registering Task Configuration StartupHandler Service */ + bundleContext.registerService(ServerStartupObserver.class, new TaskConfigurationStartupHandler(), null); } private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceTaskConfigurationServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceTaskConfigurationServiceImpl.java new file mode 100644 index 0000000000..31d36384f8 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceTaskConfigurationServiceImpl.java @@ -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 deviceFrequencies) { + JsonObject jsonObject = new JsonObject(); + + for (Map.Entry 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 deviceFrequencies = new HashMap<>(); + DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(); + List 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 deviceFrequencies = new HashMap<>(); + DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(); + List 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(); + } + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/TaskConfigurationStartupHandler.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/TaskConfigurationStartupHandler.java new file mode 100644 index 0000000000..d94c399065 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/TaskConfigurationStartupHandler.java @@ -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(); + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java index 5d2c44af42..e3ff271383 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java @@ -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"; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManager.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManager.java index 1ec11d9b3d..33351eac6f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManager.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManager.java @@ -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; // /** diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/Utils.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/Utils.java index fce1336c86..316afeedbf 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/Utils.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/Utils.java @@ -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.metadata.mgt.DeviceTaskConfigurationServiceImpl; 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 = new DeviceTaskConfigurationServiceImpl(); + return deviceTaskConfigurationService.getTaskFrequency(deviceType, tenantId); + } + + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 9ddeea4e7a..6ae29437fb 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -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); } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index cce42b48a0..678f24cab1 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -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 operations = this.getValidOperationNames(); + List operations = this.getValidOperationNames(frequency); if (operations.isEmpty()) { if (log.isDebugEnabled()) { log.debug("No operations are available."); @@ -125,12 +125,11 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { } } - private List getValidOperationNames() throws DeviceMgtTaskException { + private List getValidOperationNames(int frequency) throws DeviceMgtTaskException { List monitoringOperations = this.getOperationList(); List opNames = new ArrayList<>(); Long milliseconds = System.currentTimeMillis(); - int frequency = this.getTaskFrequency(); Map mp = Utils.getTenantedTaskOperationMap(map, deviceType); for (MonitoringOperation top : monitoringOperations) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java index e232bffe44..1884e7a772 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java @@ -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 deviceIds; private DeviceTaskManager deviceTaskManager; 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.") - 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 operationList = this.operationManager.getOperations(deviceId); Assert.assertNotNull(operationList); @@ -134,25 +137,27 @@ 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 = new DeviceTaskConfigurationServiceImpl(); + deviceTaskConfigurationService.addTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, FREQUENCY); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); deviceDetailsRetrieverTask.setProperties(map); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java index a43f20f24c..5577ef4368 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java @@ -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; } \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java index 97eb706be7..3cf7e7fc62 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java @@ -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; } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java index 5dc2eb7f08..e3212ab769 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java @@ -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) { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java index da21a52100..21d0350e66 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java @@ -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); + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java index 9658d68848..b386b436e7 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java @@ -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; + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java index fa2f26c972..d3b6ea4c3d 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -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"); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java index 978e69b0e4..69437fc84b 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -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);