From 03f95e8cdf0f33e32247928c8556f8ad340a40a9 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 4 Apr 2024 11:35:15 +0530 Subject: [PATCH 1/5] Add Tenanted Device Task Configurations This allowing users to configure task frequencies independently for each tenant. --- .../metadata/mgt/DeviceFrequencyMap.java | 35 ++++ .../mgt/DeviceTaskConfigurationService.java | 47 +++++ .../DeviceManagementServiceComponent.java | 4 + .../DeviceTaskConfigurationServiceImpl.java | 178 ++++++++++++++++++ .../mgt/TaskConfigurationStartupHandler.java | 70 +++++++ .../mgt/dao/util/MetadataConstants.java | 1 + .../mgt/core/task/DeviceTaskManager.java | 3 +- .../mgt/core/device/mgt/core/task/Utils.java | 9 + .../task/impl/DeviceDetailsRetrieverTask.java | 16 +- .../core/task/impl/DeviceTaskManagerImpl.java | 7 +- .../mgt/core/task/DeviceTaskManagerTest.java | 19 +- .../mgt/common/spi/TenantManagerService.java | 1 + .../core/tenant/mgt/core/TenantManager.java | 7 + .../mgt/core/impl/TenantManagerImpl.java | 14 ++ .../core/impl/TenantManagerServiceImpl.java | 5 + .../core/internal/TenantMgtDataHolder.java | 10 + .../internal/TenantMgtServiceComponent.java | 4 + .../listener/DeviceMgtTenantListener.java | 1 + 18 files changed, 415 insertions(+), 16 deletions(-) create mode 100644 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 create mode 100644 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 create mode 100644 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 create mode 100644 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 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); -- 2.36.3 From b53cb5cb76e34f76ce4250bbc444425773fcdf92 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Wed, 26 Jun 2024 14:41:15 +0530 Subject: [PATCH 2/5] Refactor DeviceTaskConfigurationService with caching handling for improved performance --- .../mgt/DeviceTaskConfigurationService.java | 11 +- .../mgt/MetadataManagementService.java | 11 ++ .../DeviceTaskConfigurationServiceImpl.java | 163 ++++++++---------- .../mgt/MetadataManagementServiceImpl.java | 12 +- .../mgt/core/task/DeviceTaskManagerTest.java | 2 +- 5 files changed, 104 insertions(+), 95 deletions(-) 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 index f0520351cc..3e9a4ed88d 100644 --- 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 @@ -33,14 +33,17 @@ public interface DeviceTaskConfigurationService { /** * This method is useful to add custom Task Frequency for device types * - * @throws MetadataManagementException if error while adding default TaskFrequency + * @throws MetadataManagementException if error while adding TaskFrequency */ - void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException; + void addTaskFrequency(int tenantId, int frequency, String deviceType) throws MetadataManagementException; /** - * This method is useful to get existing Task Frequency by device type and tenant id + * Retrieves the task frequency for a specific device type and tenant from the cache or metadata service. * - * @throws MetadataManagementException if error while getting existing TaskFrequency + * @param deviceType the type of the device for which the task frequency is being retrieved. + * @param tenantId the ID of the tenant associated with the device. + * @return the task frequency for the specified device type and tenant. + * @throws MetadataManagementException if there is an error managing the metadata. */ int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException; 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/MetadataManagementService.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/MetadataManagementService.java index d02614ce2c..ee3018b606 100644 --- 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/MetadataManagementService.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/MetadataManagementService.java @@ -40,6 +40,17 @@ public interface MetadataManagementService { */ Metadata createMetadata(Metadata metadata) throws MetadataManagementException, MetadataKeyAlreadyExistsException; + /** + * Persist the provided Metadata entry. + * + * @param metadata the Metadata entry to be persisted + * @param tenantId the Metadata entry to be persisted in given tenantId + * @return the Metadata entry along with the updated Metadata.id + * @throws MetadataManagementException If a data source related exception occurred + * @throws MetadataKeyAlreadyExistsException If the provided Metadata.metaKey already exist + */ + Metadata createMetadata(Metadata metadata, int tenantId) throws MetadataManagementException, MetadataKeyAlreadyExistsException; + /** * Get the specified Metadata entry. * 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 index 31d36384f8..295df337f7 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/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 @@ -18,55 +18,52 @@ package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; 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.MetadataKeyAlreadyExistsException; 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.common.metadata.mgt.MetadataManagementService; 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; +import java.util.concurrent.TimeUnit; public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurationService { private static final Log log = LogFactory.getLog(DeviceTaskConfigurationServiceImpl.class); - private final MetadataDAO metadataDAO; + private final MetadataManagementService metadataManagementService; - public DeviceTaskConfigurationServiceImpl() { - this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); - } + private final Cache cache; - 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"); - } + public DeviceTaskConfigurationServiceImpl() { + this.metadataManagementService = new MetadataManagementServiceImpl(); + this.cache = CacheBuilder.newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .build(); } private Metadata constructTaskFrequencyMetadata(Map deviceFrequencies) { JsonObject jsonObject = new JsonObject(); - + int frequency; for (Map.Entry entry : deviceFrequencies.entrySet()) { String deviceType = entry.getKey(); - int frequency = entry.getValue(); - // Create a JsonObject for the current device type + frequency = entry.getValue(); JsonObject deviceObject = new JsonObject(); deviceObject.addProperty("frequency", String.valueOf(frequency)); jsonObject.add(deviceType, deviceObject); @@ -77,9 +74,20 @@ public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurati return metadata; } - - private int getDefaultTaskFrequency(DeviceManagementProviderService dms, String deviceType) { + /** + * Retrieves the default task frequency for a specific device type using device type xml. + * + * @param dms the DeviceManagementProviderService used to fetch the device monitoring configuration. + * @param deviceType the type of the device for which the frequency is being retrieved. + * @return the task frequency for the specified device type. + * @throws DeviceManagementException if the frequency configuration is not found for the specified device type. + */ + private int getDefaultTaskFrequency(DeviceManagementProviderService dms, String deviceType) throws DeviceManagementException { OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getDeviceMonitoringConfig(deviceType); + if (operationMonitoringTaskConfig == null) { + log.error("Frequency configuration not found for device type: " + deviceType); + throw new DeviceManagementException("Frequency configuration not found for device type: " + deviceType); + } return operationMonitoringTaskConfig.getFrequency(); } @@ -92,87 +100,64 @@ public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurati 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); + addMetaData(deviceFrequencies, tenantId); } catch (DeviceManagementException e) { - String msg = "Error occurred while trying to obtain device types."; - log.error(msg, e); - } finally { - MetadataManagementDAOFactory.closeConnection(); + log.error("Error occurred while trying to obtain device types.", e); + } catch (MetadataKeyAlreadyExistsException e) { + log.error("Specified metaKey is already exist.", e); } } + @Override - public void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException { + public void addTaskFrequency(int tenantId, int frequency, String deviceType) 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(); + deviceFrequencies.put(deviceType, frequency); + addMetaData(deviceFrequencies, tenantId); + } catch (MetadataKeyAlreadyExistsException e) { + log.error("Specified metaKey is already exist.", e); } } + /** + * Loads the device frequency map from the metadata management service. + * + * @return the device frequency map parsed from the stored metadata. + * @throws MetadataManagementException if an error occurs while retrieving the metadata. + */ + private DeviceFrequencyMap loadDeviceFrequencyMap() throws MetadataManagementException { + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.DEVICE_TASK_FREQUENCY); + String metaValue = metadata.getMetaValue(); + Gson gson = new Gson(); + return gson.fromJson(metaValue, DeviceFrequencyMap.class); + } + + /** + * Adds metadata for device frequencies to the metadata management service. + * + * @param deviceFrequencies a map containing device types as keys and their corresponding frequencies as values. + * @param tenantId the tenant ID for which the metadata is being added. + * @throws MetadataManagementException if there is an error managing the metadata. + * @throws MetadataKeyAlreadyExistsException if the metadata key already exists. + */ + private void addMetaData(Map deviceFrequencies, int tenantId) throws MetadataManagementException, MetadataKeyAlreadyExistsException { + Metadata taskFrequencyMetadata = constructTaskFrequencyMetadata(deviceFrequencies); + taskFrequencyMetadata.setMetaKey(MetadataConstants.DEVICE_TASK_FREQUENCY); + metadataManagementService.createMetadata(taskFrequencyMetadata, tenantId); + } + @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(); + DeviceFrequencyMap deviceFrequencyMap = cache.getIfPresent(Integer.toString(tenantId)); + if (deviceFrequencyMap == null) { + deviceFrequencyMap = loadDeviceFrequencyMap(); + cache.put(Integer.toString(tenantId), deviceFrequencyMap); + } + if (deviceFrequencyMap.containsKey(deviceType)) { + return deviceFrequencyMap.get(deviceType).getFrequency(); + } else { + log.error("Device type not found: " + deviceType); + throw new MetadataManagementException("Device type not found: " + deviceType); } } } 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/MetadataManagementServiceImpl.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/MetadataManagementServiceImpl.java index 121caf5ba3..e833cafbaf 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/MetadataManagementServiceImpl.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/MetadataManagementServiceImpl.java @@ -53,10 +53,20 @@ public class MetadataManagementServiceImpl implements MetadataManagementService @Override public Metadata createMetadata(Metadata metadata) throws MetadataManagementException, MetadataKeyAlreadyExistsException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + return createMetadata(tenantId, metadata); + } + + @Override + public Metadata createMetadata(Metadata metadata, int tenantId) + throws MetadataManagementException, MetadataKeyAlreadyExistsException { + return createMetadata(tenantId, metadata); + } + + private Metadata createMetadata (int tenantId, Metadata metadata) throws MetadataManagementException, MetadataKeyAlreadyExistsException { if (log.isDebugEnabled()) { log.debug("Creating Metadata : [" + metadata.toString() + "]"); } - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { MetadataManagementDAOFactory.beginTransaction(); if (metadataDAO.isExist(tenantId, metadata.getMetaKey())) { 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 1884e7a772..c19eb3ac75 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 @@ -157,7 +157,7 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException, MetadataManagementException { DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl(); - deviceTaskConfigurationService.addTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, FREQUENCY); + deviceTaskConfigurationService.addTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, FREQUENCY,TestDataHolder.TEST_DEVICE_TYPE); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); deviceDetailsRetrieverTask.setProperties(map); -- 2.36.3 From cb244642ea1b7cd8c6399bf4ae608339e7670654 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 27 Jun 2024 09:31:06 +0530 Subject: [PATCH 3/5] Implement Singleton Pattern for DeviceTaskConfigurationServiceImpl --- .../DeviceTaskConfigurationServiceImpl.java | 18 +++++++++++++----- .../mgt/TaskConfigurationStartupHandler.java | 2 +- .../mgt/core/device/mgt/core/task/Utils.java | 2 +- .../mgt/core/task/DeviceTaskManagerTest.java | 2 +- .../internal/TenantMgtServiceComponent.java | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) 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 index 295df337f7..ed657d6a18 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/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 @@ -49,13 +49,21 @@ public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurati private final MetadataManagementService metadataManagementService; - private final Cache cache; + private static DeviceTaskConfigurationServiceImpl instance; - public DeviceTaskConfigurationServiceImpl() { + private static final Cache cache = CacheBuilder.newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .build(); + + private DeviceTaskConfigurationServiceImpl() { this.metadataManagementService = new MetadataManagementServiceImpl(); - this.cache = CacheBuilder.newBuilder() - .expireAfterWrite(1, TimeUnit.HOURS) - .build(); + } + + public static synchronized DeviceTaskConfigurationServiceImpl getInstance() { + if (instance == null) { + instance = new DeviceTaskConfigurationServiceImpl(); + } + return instance; } private Metadata constructTaskFrequencyMetadata(Map deviceFrequencies) { 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 index d94c399065..b2ba57d4a6 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/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 @@ -43,7 +43,7 @@ public class TaskConfigurationStartupHandler implements ServerStartupObserver { Thread t = new Thread(new Runnable() { @Override public void run() { - DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl(); + DeviceTaskConfigurationService deviceTaskConfigurationService = DeviceTaskConfigurationServiceImpl.getInstance(); RealmService realmService = DeviceManagementDataHolder.getInstance().getRealmService(); if (log.isDebugEnabled()) { log.debug("Server has just started, hence started adding default task frequencies"); 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 316afeedbf..b59e001a24 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 @@ -71,7 +71,7 @@ public class Utils { } public static int getTenantedTaskFrequency(int tenantId, String deviceType) throws MetadataManagementException { - DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl(); + DeviceTaskConfigurationService deviceTaskConfigurationService = DeviceTaskConfigurationServiceImpl.getInstance(); return deviceTaskConfigurationService.getTaskFrequency(deviceType, tenantId); } 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 c19eb3ac75..cdfa9b99e8 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 @@ -156,7 +156,7 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { @Test(groups = "Device Task Manager Test Group", description = "Testing device detail retriever task execution") public void testDeviceDetailRetrieverTaskExecute() throws OperationManagementException, MetadataManagementException { DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); - DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl(); + DeviceTaskConfigurationService deviceTaskConfigurationService = DeviceTaskConfigurationServiceImpl.getInstance(); deviceTaskConfigurationService.addTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, FREQUENCY,TestDataHolder.TEST_DEVICE_TYPE); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); 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 d3b6ea4c3d..9b7cc879fa 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 @@ -72,7 +72,7 @@ public class TenantMgtServiceComponent { componentContext.getBundleContext().registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagementService, null); TenantMgtDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); - DeviceTaskConfigurationService deviceTaskConfigurationService = new DeviceTaskConfigurationServiceImpl(); + DeviceTaskConfigurationService deviceTaskConfigurationService = DeviceTaskConfigurationServiceImpl.getInstance(); TenantMgtDataHolder.getInstance().setDeviceTaskConfigurationService(deviceTaskConfigurationService); DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); if(log.isDebugEnabled()) { -- 2.36.3 From dd71027f4bbca3c8ed6edad6a46b24eea599fc02 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 11 Jul 2024 03:05:11 +0530 Subject: [PATCH 4/5] Add TaskManagementService for Device Task manager --- .../pom.xml | 10 ++ .../common/OperationMonitoringTaskConfig.java | 9 ++ .../pom.xml | 13 +- .../DeviceManagementPluginRepository.java | 27 +++- .../core/dto/DeviceTaskManagerWrapper.java | 80 +++++++++ .../internal/DeviceManagementDataHolder.java | 10 ++ .../DeviceTaskManagerServiceComponent.java | 95 +++++++++-- .../core/task/DeviceTaskManagerService.java | 17 +- .../task/impl/DeviceDetailsRetrieverTask.java | 76 +++++---- .../impl/DeviceTaskManagerServiceImpl.java | 153 +++++++----------- .../template/DeviceTypeManagerService.java | 1 + .../template/config/TaskConfiguration.java | 10 ++ .../mgt/common/spi/TaskManagementService.java | 2 + .../task/mgt/core/dao/DynamicTaskDAO.java | 2 + .../mgt/core/dao/impl/DynamicTaskDAOImpl.java | 23 +++ .../service/TaskManagementServiceImpl.java | 37 ++++- .../mgt/common/spi/TenantManagerService.java | 1 + .../pom.xml | 6 +- .../core/tenant/mgt/core/TenantManager.java | 8 + .../mgt/core/impl/TenantManagerImpl.java | 31 +++- .../core/impl/TenantManagerServiceImpl.java | 5 + .../core/internal/TenantMgtDataHolder.java | 33 ++++ .../internal/TenantMgtServiceComponent.java | 39 +++++ .../listener/DeviceMgtTenantListener.java | 1 + 24 files changed, 533 insertions(+), 156 deletions(-) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/DeviceTaskManagerWrapper.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml index d975ad3715..df0c5cc586 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml @@ -460,5 +460,15 @@ io.entgra.device.mgt.core.apimgt.webapp.publisher provided + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.task.mgt.core + provided + + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.task.mgt.common + provided + \ No newline at end of file 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/OperationMonitoringTaskConfig.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java index d077b69922..ff2885c9b9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java @@ -29,6 +29,15 @@ public class OperationMonitoringTaskConfig { private boolean isEnabled; private int frequency; private List monitoringOperation = new ArrayList<>(); + private String cronExpression; + + public String getCronExpression() { + return cronExpression; + } + + public void setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; + } public boolean isEnabled() { return isEnabled; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml index 9827ec7084..306f7adb2f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml @@ -115,7 +115,8 @@ org.apache.commons.codec.binary, io.entgra.device.mgt.core.server.bootup.heartbeat.beacon, io.entgra.device.mgt.core.device.mgt.extensions.logger.*, - io.entgra.device.mgt.core.notification.logger.* + io.entgra.device.mgt.core.notification.logger.*, + io.entgra.device.mgt.core.task.mgt.core.* !io.entgra.device.mgt.core.device.mgt.core.internal, @@ -371,6 +372,16 @@ io.entgra.device.mgt.core io.entgra.device.mgt.core.apimgt.extension.rest.api + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.task.mgt.core + provided + + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.task.mgt.common + provided + 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/DeviceManagementPluginRepository.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java index 6ad7b53c74..dc8927563d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java @@ -18,6 +18,10 @@ package io.entgra.device.mgt.core.device.mgt.core; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper; +import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException; +import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -52,6 +56,7 @@ import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; public class DeviceManagementPluginRepository implements DeviceManagerStartupListener { @@ -299,14 +304,16 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis getDeviceTaskManagerService(); OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService. getOperationMonitoringConfig(); + DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper(); + deviceTaskManagerWrapper.setDeviceType(deviceManagementService.getType()); + deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(operationMonitoringTaskConfig); if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) { if (deviceTaskManagerService == null) { DeviceMonitoringOperationDataHolder.getInstance().addOperationMonitoringConfigToMap( deviceManagementService.getType(), operationMonitoringTaskConfig); } else { - deviceTaskManagerService.startTask(deviceManagementService.getType(), - operationMonitoringTaskConfig); + deviceTaskManagerService.startTask(deviceTaskManagerWrapper); } } } catch (DeviceMgtTaskException e) { @@ -322,14 +329,24 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis getDeviceTaskManagerService(); OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService. getOperationMonitoringConfig(); + TaskManagementService taskManagementService= DeviceManagementDataHolder.getInstance().getTaskManagementService(); if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) { - deviceTaskManagerService.stopTask(deviceManagementService.getType(), - deviceManagementService.getOperationMonitoringConfig()); + Map> dynamicTasksForAllTenants = taskManagementService.getDynamicTasksForAllTenants(); + + for (Map.Entry> entry : dynamicTasksForAllTenants.entrySet()) { + List dynamicTasks = entry.getValue(); + for (DynamicTask task : dynamicTasks) { + deviceTaskManagerService.stopTask(task.getDynamicTaskId()); + } + } } } catch (DeviceMgtTaskException e) { throw new DeviceManagementException("Error occurred while removing task service for '" + deviceManagementService.getType() + "'", e); - } + } catch (TaskManagementException e) { + throw new DeviceManagementException("Error occurred while deleting task service for '" + + deviceManagementService.getType() + "'", e); + } } private void registerDeviceStatusMonitoringTask(DeviceType deviceType, DeviceManagementService deviceManagementService) throws 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/dto/DeviceTaskManagerWrapper.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/DeviceTaskManagerWrapper.java new file mode 100644 index 0000000000..898c1fbbef --- /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/dto/DeviceTaskManagerWrapper.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2018 - 2024 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.dto; + +import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + + +@ApiModel( + value = "DeviceTaskManagerWrapper", + description = "DeviceTaskManagerWrapper represents request payload of scheduling device details retriever task") +public class DeviceTaskManagerWrapper { + + @ApiModelProperty(name = "cron", + value = "Cron to be scheduled", + example = "0 0 * * * ? *, 0 0 0/8 * * ? *", + required = true) + private String cron; + + @ApiModelProperty(name = "payload", + value = "Operation payload") + private Object payload; + + @ApiModelProperty(name = "deviceType", + value = "Device type") + private String deviceType; + + public OperationMonitoringTaskConfig getOperationMonitoringTaskConfig() { + return operationMonitoringTaskConfig; + } + + public void setOperationMonitoringTaskConfig(OperationMonitoringTaskConfig operationMonitoringTaskConfig) { + this.operationMonitoringTaskConfig = operationMonitoringTaskConfig; + } + + private OperationMonitoringTaskConfig operationMonitoringTaskConfig; + + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getCron() { + return cron; + } + + public void setCron(String cron) { + this.cron = cron; + } + + public Object getPayload() { + return payload; + } + + public void setPayload(Object payload) { + this.payload = payload; + } +} 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/DeviceManagementDataHolder.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/DeviceManagementDataHolder.java index 48d573a5fa..c4a2c2324c 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/DeviceManagementDataHolder.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/DeviceManagementDataHolder.java @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServi import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuthorizationService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; +import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.DeviceStatusTaskPluginConfig; import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; @@ -100,6 +101,7 @@ public class DeviceManagementDataHolder { private DeviceStatusManagementService deviceStatusManagementService; private APIApplicationServices apiApplicationServices; private PublisherRESTAPIServices publisherRESTAPIServices; + private TaskManagementService taskManagementService; private final Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap( new HashMap<>()); @@ -457,4 +459,12 @@ public class DeviceManagementDataHolder { public void setGroupAccessAuthorizationService(GroupAccessAuthorizationService groupAccessAuthorizationService) { this.groupAccessAuthorizationService = groupAccessAuthorizationService; } + + public TaskManagementService getTaskManagementService() { + return taskManagementService; + } + + public void setTaskManagementService(TaskManagementService taskManagementService) { + this.taskManagementService = taskManagementService; + } } 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/DeviceTaskManagerServiceComponent.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/DeviceTaskManagerServiceComponent.java index 602e0700a1..05ce19cc21 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/DeviceTaskManagerServiceComponent.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/DeviceTaskManagerServiceComponent.java @@ -18,6 +18,10 @@ package io.entgra.device.mgt.core.device.mgt.core.internal; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper; +import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException; +import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -38,8 +42,16 @@ import io.entgra.device.mgt.core.device.mgt.core.status.task.impl.DeviceStatusTa import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; import io.entgra.device.mgt.core.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.user.api.Tenant; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.core.service.RealmService; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Map; /** @@ -50,6 +62,12 @@ import java.util.Map; * policy="dynamic" * bind="setTaskService" * unbind="unsetTaskService" + * @scr.reference name="io.entgra.task.mgt.service" + * interface="io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService" + * cardinality="1..1" + * policy="dynamic" + * bind="setTaskMgtService" + * unbind="unsetTaskMgtService" */ @SuppressWarnings("unused") @@ -87,14 +105,41 @@ public class DeviceTaskManagerServiceComponent { bundleContext.registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null); Map deviceConfigMap = DeviceMonitoringOperationDataHolder .getInstance().getOperationMonitoringConfigFromMap(); - for (String platformType : deviceConfigMap.keySet()) { - OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType); - if (taskConfig.isEnabled()) { - deviceTaskManagerService.startTask(platformType, taskConfig); + List tenants = getAllTenants(); + for (Tenant tenant : tenants) { + if (MultitenantConstants.SUPER_TENANT_ID == tenant.getId()) { + startTasksForTenant(deviceConfigMap,deviceTaskManagerService); + continue; + } + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId(), true); + startTasksForTenant(deviceConfigMap,deviceTaskManagerService); + } finally { + PrivilegedCarbonContext.endTenantFlow(); } } } + private List getAllTenants() { + List tenants = new ArrayList<>(); + try { + RealmService realmService = DeviceManagementDataHolder.getInstance().getRealmService(); + Tenant[] tenantArray = realmService.getTenantManager().getAllTenants(); + if (tenantArray != null && tenantArray.length != 0) { + tenants.addAll(Arrays.asList(tenantArray)); + } + Tenant superTenant = new Tenant(); + superTenant.setId(-1234); + tenants.add(superTenant); + return tenants; + } catch (UserStoreException e) { + String msg = "Unable to load tenants"; + log.error(msg, e); + return new ArrayList<>(); + } + } + private void startDeviceStatusMonitoringTask(BundleContext bundleContext) { DeviceStatusTaskManagerService deviceStatusTaskManagerService = new DeviceStatusTaskManagerServiceImpl(); DeviceManagementDataHolder.getInstance().setDeviceStatusTaskManagerService(deviceStatusTaskManagerService); @@ -111,6 +156,18 @@ public class DeviceTaskManagerServiceComponent { } } + private void startTasksForTenant(Map deviceConfigMap,DeviceTaskManagerService deviceTaskManagerService) throws DeviceMgtTaskException { + DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper(); + for (String platformType : deviceConfigMap.keySet()) { + deviceTaskManagerWrapper.setDeviceType(platformType); + OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType); + deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(taskConfig); + if (taskConfig.isEnabled()) { + deviceTaskManagerService.startTask(deviceTaskManagerWrapper); + } + } + } + private void startOperationTimeoutTask(BundleContext bundleContext) { OperationTimeoutTaskManagerService operationTimeoutTaskManagerService = new OperationTimeoutTaskManagerServiceImpl(); @@ -148,15 +205,19 @@ public class DeviceTaskManagerServiceComponent { } private void stopOperationMonitoringTask() - throws DeviceMgtTaskException { + throws DeviceMgtTaskException, TaskManagementException { DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance() .getDeviceTaskManagerService(); + TaskManagementService taskManagementService= DeviceManagementDataHolder.getInstance().getTaskManagementService(); Map deviceConfigMap = DeviceMonitoringOperationDataHolder .getInstance().getOperationMonitoringConfigFromMap(); - for (String platformType : deviceConfigMap.keySet()) { - OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType); - if (taskConfig.isEnabled()) { - deviceTaskManagerService.stopTask(platformType, taskConfig); + Map> dynamicTasksForAllTenants = taskManagementService.getDynamicTasksForAllTenants(); + + for (Map.Entry> entry : dynamicTasksForAllTenants.entrySet()) { + Integer tenantId = entry.getKey(); + List dynamicTasks = entry.getValue(); + for (DynamicTask task : dynamicTasks) { + deviceTaskManagerService.stopTask(task.getDynamicTaskId()); } } } @@ -205,4 +266,20 @@ public class DeviceTaskManagerServiceComponent { } DeviceManagementDataHolder.getInstance().setTaskService(null); } + + @SuppressWarnings("unused") + protected void setTaskMgtService(TaskManagementService taskManagementService) { + if (log.isDebugEnabled()) { + log.debug("Setting the task service"); + } + DeviceManagementDataHolder.getInstance().setTaskManagementService(taskManagementService); + } + + @SuppressWarnings("unused") + protected void unsetTaskMgtService(TaskManagementService taskManagementService) { + if (log.isDebugEnabled()) { + log.debug("Removing the task service"); + } + DeviceManagementDataHolder.getInstance().setTaskManagementService(null); + } } 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/DeviceTaskManagerService.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/DeviceTaskManagerService.java index b49fd30922..d83bf7aaa5 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/DeviceTaskManagerService.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/DeviceTaskManagerService.java @@ -19,33 +19,34 @@ package io.entgra.device.mgt.core.device.mgt.core.task; -import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskNotFoundException; public interface DeviceTaskManagerService { /** * This method will start the task. - * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig + * @param deviceTaskManagerWrapper - DeviceTaskManagerWrapper * @throws DeviceMgtTaskException */ - void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + void startTask(DeviceTaskManagerWrapper deviceTaskManagerWrapper) throws DeviceMgtTaskException; /** * This method will stop the task. - * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig * @throws DeviceMgtTaskException */ - void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + void stopTask(int taskId) throws DeviceMgtTaskException; /** * This will update the task frequency which it runs. - * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig + * @param deviceTaskManagerWrapper - DeviceTaskManagerWrapper * @throws DeviceMgtTaskException */ - void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) - throws DeviceMgtTaskException; + void updateTask(int taskId,DeviceTaskManagerWrapper deviceTaskManagerWrapper) + throws DeviceMgtTaskException, TaskNotFoundException, TaskManagementException; } 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 6ae29437fb..3c078efa5f 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 @@ -20,19 +20,19 @@ 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 io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask; +import io.entgra.device.mgt.core.task.mgt.common.constant.TaskMgtConstants; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.base.MultitenantConstants; -import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; import io.entgra.device.mgt.core.device.mgt.common.StartupOperationConfig; -import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManager; - -import java.util.List; +import org.wso2.carbon.context.PrivilegedCarbonContext; public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask { @@ -42,44 +42,62 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask { @Override public void executeDynamicTask() { - deviceType = getProperty("DEVICE_TYPE"); + String taskId = getProperty(TaskMgtConstants.Task.DYNAMIC_TASK_ID); + String taskName = getProperty(TaskMgtConstants.Task.LOCAL_TASK_NAME); + + if (StringUtils.isEmpty(taskId)) { + log.warn("Not executing the task as Task ID is undefined"); + if (log.isDebugEnabled()) { + log.debug("[Undefined Task] Task Name: " + taskName); + } + return; + } + + DynamicTask dynamicTask; + try { + dynamicTask = DeviceManagementDataHolder.getInstance().getTaskManagementService() + .getDynamicTask(Integer.parseInt(taskId)); + } catch (TaskManagementException e) { + log.error("Task [" + taskId + "] " + taskName + + " won't execute as unable to get task management service.", e); + return; + } + + if (dynamicTask == null) { + log.error("Task [" + taskId + "] " + taskName + + " won't execute as definition not available in the database."); + return; + } + + deviceType = dynamicTask.getProperties().get("DEVICE_TYPE"); + int tenantId = dynamicTask.getTenantId(); + deviceManagementProviderService = DeviceManagementDataHolder.getInstance() .getDeviceManagementProvider(); OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementProviderService .getDeviceMonitoringConfig(deviceType); StartupOperationConfig startupOperationConfig = deviceManagementProviderService .getStartupOperationConfig(deviceType); - this.executeForAllTenants(operationMonitoringTaskConfig, startupOperationConfig); + this.executeForATenant(operationMonitoringTaskConfig, startupOperationConfig, tenantId); } - private void executeForAllTenants(OperationMonitoringTaskConfig operationMonitoringTaskConfig, - StartupOperationConfig startupOperationConfig) { + private void executeForATenant(OperationMonitoringTaskConfig operationMonitoringTaskConfig, + StartupOperationConfig startupOperationConfig,int tenantId) { if (log.isDebugEnabled()) { - log.debug("Device details retrieving task started to run for all tenants."); + log.debug("Device details retrieving task started to run for tenant " + tenantId); } try { - List tenants = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDeviceEnrolledTenants(); - if (log.isDebugEnabled()) { - log.debug("Task is running for " + tenants.size() + " tenants and the device type is " + deviceType); + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); + this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenantId, deviceType)); + } finally { + PrivilegedCarbonContext.endTenantFlow(); } - for (Integer tenant : tenants) { - if (MultitenantConstants.SUPER_TENANT_ID == tenant) { - this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, deviceType)); - continue; - } - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true); - this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenant, deviceType)); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } + if (log.isDebugEnabled()) { + log.debug("Task is running for " + tenantId+ " tenants and the device type is " + deviceType); } - } 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); 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/DeviceTaskManagerServiceImpl.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/DeviceTaskManagerServiceImpl.java index 03e5932423..efe1337f9e 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/DeviceTaskManagerServiceImpl.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/DeviceTaskManagerServiceImpl.java @@ -20,17 +20,17 @@ package io.entgra.device.mgt.core.device.mgt.core.task.impl; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper; +import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException; +import io.entgra.device.mgt.core.task.mgt.common.exception.TaskNotFoundException; +import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; -import org.wso2.carbon.ntask.common.TaskException; -import org.wso2.carbon.ntask.core.TaskInfo; -import org.wso2.carbon.ntask.core.TaskManager; -import org.wso2.carbon.ntask.core.service.TaskService; import java.util.HashMap; import java.util.Map; @@ -39,122 +39,77 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { public static final String TASK_TYPE = "DEVICE_MONITORING"; public static final String TENANT_ID = "TENANT_ID"; - private static String TASK_CLASS = "io.entgra.device.mgt.core.device.mgt.core.task.impl.DeviceDetailsRetrieverTask"; + public static final String SCHEDULED_BY = "SCHEDULED_BY"; + public static final String SCHEDULED_ON = "SCHEDULED_ON"; + public static final String DEVICE_TYPE = "DEVICE_TYPE"; + public static final String OPPCONFIG = "OPPCONFIG"; + private final TaskManagementService taskManagementService = DeviceManagementDataHolder.getInstance().getTaskManagementService();; -// private DeviceTaskManager deviceTaskManager; private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class); @Override - public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + public void startTask(DeviceTaskManagerWrapper deviceTaskManagerWrapper) throws DeviceMgtTaskException { - log.info("Task adding for " + deviceType); - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { - TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); - taskService.registerTaskType(TASK_TYPE); - - if (log.isDebugEnabled()) { - log.debug("Device details retrieving task is started for the tenant id " + tenantId); - // log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager - // .getTaskFrequency()); - log.debug( - "Device details retrieving task is at frequency of : " + operationMonitoringTaskConfig - .getFrequency()); - } - - TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - - TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - // triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency()); - triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); - triggerInfo.setRepeatCount(-1); - + PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); Gson gson = new Gson(); - String operationConfigs = gson.toJson(operationMonitoringTaskConfig); - - Map properties = new HashMap<>(); - - properties.put(TENANT_ID, String.valueOf(tenantId)); - properties.put("DEVICE_TYPE", deviceType); - properties.put("OPPCONFIG", operationConfigs); - - String taskName = deviceType + String.valueOf(tenantId); - - if (!taskManager.isTaskScheduled(deviceType)) { - - TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo); - - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); - } else { - throw new DeviceMgtTaskException( - "Device details retrieving task is already started for this tenant " + tenantId); + String operationConfigs = gson.toJson(deviceTaskManagerWrapper.getOperationMonitoringTaskConfig()); + Map taskProperties = new HashMap<>(); + taskProperties.put(TENANT_ID,String.valueOf(carbonContext.getTenantId())); + taskProperties.put(SCHEDULED_BY,carbonContext.getUsername()); + taskProperties.put(SCHEDULED_ON,String.valueOf(System.currentTimeMillis())); + taskProperties.put(DEVICE_TYPE, deviceTaskManagerWrapper.getDeviceType()); + taskProperties.put(TASK_TYPE,TASK_TYPE); + taskProperties.put(OPPCONFIG,operationConfigs); + + DynamicTask dynamicTask = new DynamicTask(); + dynamicTask.setTaskClassName(DeviceDetailsRetrieverTask.class.getName()); + dynamicTask.setCronExpression(deviceTaskManagerWrapper.getOperationMonitoringTaskConfig().getCronExpression()); + dynamicTask.setEnabled(true); + dynamicTask.setName(deviceTaskManagerWrapper.getDeviceType() + carbonContext.getTenantId()); + dynamicTask.setProperties(taskProperties); + if(!taskManagementService.isTaskExist(dynamicTask.getName())){ + taskManagementService.createTask(dynamicTask); } - - } catch (TaskException e) { - throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId, - e); + log.info("Task adding for " + deviceTaskManagerWrapper.getDeviceType() + " tenant " + carbonContext.getTenantId()); + } catch (TaskManagementException e) { + throw new DeviceMgtTaskException("Error occurred while creating the task for tenant ", e); } - } @Override - public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + public void stopTask(int taskId) throws DeviceMgtTaskException { - try { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); - if (taskService != null && taskService.isServerInit()) { - TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - String taskName = deviceType + tenantId; - taskManager.deleteTask(taskName); - } - } catch (TaskException e) { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId, - e); + taskManagementService.deleteTask(taskId); + } catch (TaskManagementException e) { + String msg = "Error occurred while retrieving task manager to delete task '" + taskId + "'"; + log.error(msg); + throw new DeviceMgtTaskException(msg, e); + } catch (TaskNotFoundException e) { + String msg = "Task '" + taskId + "' not found while deleting the task"; + log.error(msg, e); + throw new DeviceMgtTaskException(msg, e); } - } @Override - public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) - throws DeviceMgtTaskException { - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - // deviceTaskManager = new DeviceTaskManagerImpl(); + public void updateTask(int taskId, DeviceTaskManagerWrapper deviceTaskManagerWrapper) + throws DeviceMgtTaskException, TaskNotFoundException, TaskManagementException { try { - TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); - TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - - if (taskManager.isTaskScheduled(deviceType)) { - String taskName = deviceType + tenantId; - taskManager.deleteTask(taskName); - TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); - triggerInfo.setRepeatCount(-1); - - Map properties = new HashMap<>(); - properties.put(TENANT_ID, String.valueOf(tenantId)); - - TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo); - - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); - } else { - throw new DeviceMgtTaskException( - "Device details retrieving task has not been started for this tenant " + - tenantId + ". Please start the task first."); - } - - } catch (TaskException e) { - throw new DeviceMgtTaskException("Error occurred while updating the task for tenant " + tenantId, - e); + DynamicTask dynamicTask = taskManagementService.getDynamicTask(taskId); + dynamicTask.setEnabled(true); + dynamicTask.setCronExpression(deviceTaskManagerWrapper.getCron()); + taskManagementService.updateTask(taskId,dynamicTask); + } catch (TaskManagementException e) { + String msg = "Error occurred while updating task '" + taskId + "'"; + log.error(msg); + } catch (TaskNotFoundException e) { + String msg = "Task '" + taskId + "' not found while updating the task"; + log.error(msg, e); } } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index b357c94cca..4ad82b1e35 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -131,6 +131,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { if (taskConfiguration != null) { operationMonitoringConfigs.setEnabled(taskConfiguration.isEnabled()); operationMonitoringConfigs.setFrequency(taskConfiguration.getFrequency()); + operationMonitoringConfigs.setCronExpression(taskConfiguration.getCronExpression()); List ops = taskConfiguration.getOperations(); if (ops != null && !ops.isEmpty()) { monitoringOperations = new ArrayList<>(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java index 2f87930f7a..7b0ce2092e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java @@ -32,6 +32,16 @@ public class TaskConfiguration { private boolean enabled; private int frequency; private List operations; + private String cronExpression; + + @XmlElement(name = "cronExpression", required = true) + public String getCronExpression() { + return cronExpression; + } + + public void setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; + } @XmlElement(name = "Enable", required = true) public boolean isEnabled() { diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java index ab8a02199f..3216dba9b7 100755 --- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java +++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java @@ -43,4 +43,6 @@ public interface TaskManagementService { DynamicTask getDynamicTask(int dynamicTaskId) throws TaskManagementException; List getActiveDynamicTasks() throws TaskManagementException; + + boolean isTaskExist(String taskName) throws TaskManagementException; } diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java index 7493e9a025..600292a8d3 100755 --- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java +++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java @@ -41,4 +41,6 @@ public interface DynamicTaskDAO { List getActiveDynamicTasks(int tenantId) throws TaskManagementDAOException; + boolean isTaskExist(String taskName, int tenantId) throws TaskManagementDAOException; + } diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java index 843b5c9189..8364f13540 100755 --- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java +++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java @@ -200,4 +200,27 @@ public class DynamicTaskDAOImpl implements DynamicTaskDAO { return dynamicTasks; } + @Override + public boolean isTaskExist(String taskName, int tenantId) throws TaskManagementDAOException { + PreparedStatement stmt; + ResultSet rs; + try { + Connection conn = TaskManagementDAOFactory.getConnection(); + String sql = "SELECT COUNT(*) FROM DYNAMIC_TASK WHERE NAME = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, taskName); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + + if (rs.next()) { + return rs.getInt(1) > 0; + } + return false; + } catch (SQLException e) { + String msg = "Error occurred while checking if task '" + taskName + "' exists"; + log.error(msg, e); + throw new TaskManagementDAOException(msg, e); + } + } + } diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java index c794a4e934..271102f731 100755 --- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java +++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java @@ -93,8 +93,11 @@ public class TaskManagementServiceImpl implements TaskManagementService { dynamicTaskPropDAO.addTaskProperties(dynamicTaskId, dynamicTask.getProperties(), tenantId); try { - serverHashIdx = TaskManagerDataHolder.getInstance().getHeartBeatService() - .getServerCtxInfo().getLocalServerHashIdx(); + serverHashIdx = 0; + if(TaskManagerDataHolder.getInstance().getHeartBeatService().isTaskPartitioningEnabled()){ + serverHashIdx = TaskManagerDataHolder.getInstance().getHeartBeatService() + .getServerCtxInfo().getLocalServerHashIdx(); + } nTaskName = TaskManagementUtil.generateNTaskName(dynamicTaskId, serverHashIdx); } catch (HeartBeatManagementException e) { String msg = "Unexpected exception when getting server hash index."; @@ -136,7 +139,13 @@ public class TaskManagementServiceImpl implements TaskManagementService { String msg = "Error occurred while scheduling task '" + dynamicTask.getName() + "'"; log.error(msg, e); throw new TaskManagementException(msg, e); - } finally { + } catch (Exception e) { + TaskManagementDAOFactory.rollbackTransaction(); + String msg = "Unexpected error occurred while scheduling task '" + dynamicTask.getName() + "'"; + log.error(msg, e); + throw new TaskManagementException(msg, e); + } + finally { TaskManagementDAOFactory.closeConnection(); } } @@ -430,6 +439,28 @@ public class TaskManagementServiceImpl implements TaskManagementService { return dynamicTasks; } + @Override + public boolean isTaskExist(String taskName) throws TaskManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + TaskManagementDAOFactory.beginTransaction(); + if (log.isDebugEnabled()) { + log.debug("Fetching the details of all active dynamic tasks"); + } + boolean taskExists = dynamicTaskDAO.isTaskExist(taskName, tenantId); + TaskManagementDAOFactory.commitTransaction(); + return taskExists; + } catch (TaskManagementDAOException e) { + String msg = "Error occurred while fetching all active dynamic tasks"; + log.error(msg, e); + throw new TaskManagementException(msg, e); + } catch (TransactionManagementException e) { + throw new RuntimeException(e); + } finally { + TaskManagementDAOFactory.closeConnection(); + } + } + // check whether task exist in the ntask core private boolean isTaskExists(String taskName) throws TaskManagementException, TaskException { if (StringUtils.isEmpty(taskName)) { 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 9f459412b4..0a17b3285b 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 @@ -29,4 +29,5 @@ public interface TenantManagerService { void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException; void deleteTenantApplicationData(int tenantId) throws TenantMgtException; void deleteTenantDeviceData(int tenantId) throws TenantMgtException; + void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException; } \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml index 7eafae8b99..cdd7f07108 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml @@ -68,7 +68,11 @@ io.entgra.device.mgt.core.device.mgt.core.config, io.entgra.device.mgt.core.device.mgt.core.dao.*, org.wso2.carbon.user.core.service, - org.wso2.carbon.context + org.wso2.carbon.context, + io.entgra.device.mgt.core.device.mgt.common.spi.*, + io.entgra.device.mgt.core.device.mgt.core.service.*, + io.entgra.device.mgt.core.device.mgt.core.task.*, + io.entgra.device.mgt.core.device.mgt.core.dto.* !io.entgra.device.mgt.core.tenant.mgt.core.internal, 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 dce4d36e28..0c732925a3 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 @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.tenant.mgt.core; +import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException; import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import org.wso2.carbon.stratos.common.beans.TenantInfoBean; @@ -64,4 +65,11 @@ public interface TenantManager { * @throws TenantMgtException Throws when deleting Tenant related device data */ void deleteTenantDeviceData(int tenantId) throws TenantMgtException; + + /** + * Add deviceDetailsRetrieverTask of a tenant + * @param tenantInfoBean ID of the tenant + * @throws TenantMgtException Throws when deleting Tenant related device data + */ + void addDeviceDetailsRetrieverTask(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 76e6058f68..62b5e20fd3 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 @@ -20,10 +20,15 @@ package io.entgra.device.mgt.core.tenant.mgt.core.impl; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import io.entgra.device.mgt.core.tenant.mgt.core.internal.TenantMgtDataHolder; @@ -42,7 +47,6 @@ import io.entgra.device.mgt.core.device.mgt.common.roles.config.Role; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; -import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -231,6 +235,31 @@ public class TenantManagerImpl implements TenantManager { } + @Override + public void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException{ + initTenantFlow(tenantInfoBean); + try { + DeviceManagementProviderService dms = TenantMgtDataHolder.getInstance().getDeviceManagementProvider(); + List deviceTypes = dms.getDeviceTypes(); + DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper(); + for (DeviceType deviceType : deviceTypes) { + deviceTaskManagerWrapper.setDeviceType(deviceType.getName()); + deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(dms.getDeviceMonitoringConfig(deviceType.getName())); + TenantMgtDataHolder.getInstance().getDeviceTaskManagerService().startTask(deviceTaskManagerWrapper); + } + } catch (DeviceMgtTaskException e) { + String msg = "Error occurred while adding default TaskFrequencies"; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting DeviceManagementService"; + 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 848e7ed3a6..a284acea56 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 @@ -53,4 +53,9 @@ public class TenantManagerServiceImpl implements TenantManagerService { public void deleteTenantDeviceData(int tenantId) throws TenantMgtException { TenantMgtDataHolder.getInstance().getTenantManager().deleteTenantDeviceData(tenantId); } + + @Override + public void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException { + TenantMgtDataHolder.getInstance().getTenantManager().addDeviceDetailsRetrieverTask(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 b386b436e7..0f54c8d6c2 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 @@ -20,6 +20,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.common.spi.DeviceManagementService; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; 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; @@ -37,6 +40,12 @@ public class TenantMgtDataHolder { private DeviceStatusManagementService deviceStatusManagementService; private DeviceTaskConfigurationService deviceTaskConfigurationService; + private DeviceTaskManagerService deviceTaskManagerService; + + private DeviceManagementService deviceManagementService; + + private DeviceManagementProviderService deviceManagerProvider; + public RealmService getRealmService() { return realmService; } @@ -45,6 +54,30 @@ public class TenantMgtDataHolder { this.realmService = realmService; } + public DeviceTaskManagerService getDeviceTaskManagerService() { + return deviceTaskManagerService; + } + + public void setDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) { + this.deviceTaskManagerService = deviceTaskManagerService; + } + + public DeviceManagementService getDeviceManagementService() { + return deviceManagementService; + } + + public void setDeviceManagementService(DeviceManagementService deviceManagementService) { + this.deviceManagementService = deviceManagementService; + } + + public DeviceManagementProviderService getDeviceManagementProvider() { + return deviceManagerProvider; + } + + public void setDeviceManagementProvider(DeviceManagementProviderService deviceManagerProvider) { + this.deviceManagerProvider = deviceManagerProvider; + } + public ApplicationManager getApplicationManager() { return applicationManager; } 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 9b7cc879fa..abbc06798a 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 @@ -20,8 +20,11 @@ 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.internal.DeviceManagementDataHolder; 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.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; 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; @@ -49,6 +52,18 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setRealmService" * unbind="unsetRealmService" + * @scr.reference name="org.wso2.carbon.device.manager" + * interface="io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService" + * cardinality="1..1" + * policy="dynamic" + * bind="setDeviceManagementProviderService" + * unbind="unsetDeviceManagementProviderService" + * @scr.reference name="org.wso2.carbon.device.task.manager" + * interface="io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService" + * cardinality="1..1" + * policy="dynamic" + * bind="setDeviceTaskManagerService" + * unbind="unsetDeviceTaskManagerService" */ @SuppressWarnings("unused") @@ -121,4 +136,28 @@ public class TenantMgtServiceComponent { } TenantMgtDataHolder.getInstance().setRealmService(null); } + + protected void setDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) { + if (log.isDebugEnabled()) { + } + TenantMgtDataHolder.getInstance().setDeviceTaskManagerService(deviceTaskManagerService); + } + + protected void unsetDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) { + if (log.isDebugEnabled()) { + } + TenantMgtDataHolder.getInstance().setDeviceTaskManagerService(null); + } + + protected void setDeviceManagementProviderService(DeviceManagementProviderService deviceManagementProviderService) { + if (log.isDebugEnabled()) { + } + TenantMgtDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService); + } + + protected void unsetDeviceManagementProviderService(DeviceManagementProviderService deviceManagementProviderService) { + if (log.isDebugEnabled()) { + } + TenantMgtDataHolder.getInstance().setDeviceTaskManagerService(null); + } } 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 e2e9c88864..3bd2edd2e5 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 @@ -40,6 +40,7 @@ public class DeviceMgtTenantListener implements TenantMgtListener { tenantManager.addDefaultAppCategories(tenantInfoBean); tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean); tenantManager.addDefaultDeviceTaskConfiguration(tenantInfoBean); + tenantManager.addDeviceDetailsRetrieverTask(tenantInfoBean); } catch (TenantMgtException e) { String msg = "Error occurred while executing tenant creation flow"; log.error(msg, e); -- 2.36.3 From a71d260e1038ecdb700523585130d4e8286aa014 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Tue, 19 Nov 2024 12:44:35 +0530 Subject: [PATCH 5/5] FIx build failure --- .../core/tenant/mgt/core/internal/TenantMgtServiceComponent.java | 1 + 1 file changed, 1 insertion(+) 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 25543016cf..c8c2c94ed4 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 @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfig import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; 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.device.mgt.core.metadata.mgt.WhiteLabelManagementServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; -- 2.36.3