|
|
|
@ -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<String, DeviceFrequencyMap> 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<String, Integer> deviceFrequencies) {
|
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
|
|
|
|
|
int frequency;
|
|
|
|
|
for (Map.Entry<String, Integer> 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<String, Integer> deviceFrequencies = new HashMap<>();
|
|
|
|
|
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
|
|
|
|
|
List<DeviceType> deviceTypes = dms.getDeviceTypes();
|
|
|
|
|
for (DeviceType deviceType : deviceTypes) {
|
|
|
|
|
deviceFrequencies.put(deviceType.getName(), frequency);
|
|
|
|
|
}
|
|
|
|
|
MetadataManagementDAOFactory.beginTransaction();
|
|
|
|
|
if (!metadataDAO.isExist(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY)) {
|
|
|
|
|
Metadata defaultTaskFrequencyMetadata = constructTaskFrequencyMetadata(deviceFrequencies);
|
|
|
|
|
// Add Task Frequency entry for given Frequency
|
|
|
|
|
addMetadataEntry(tenantId, defaultTaskFrequencyMetadata, MetadataConstants.DEVICE_TASK_FREQUENCY);
|
|
|
|
|
}
|
|
|
|
|
MetadataManagementDAOFactory.commitTransaction();
|
|
|
|
|
} catch (MetadataManagementDAOException e) {
|
|
|
|
|
MetadataManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
String msg = "Error occurred while inserting default task frequency metadata entry.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new MetadataManagementException(msg, e);
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
String msg = "Error occurred while opening a connection to the data source";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new MetadataManagementException(msg, e);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
String msg = "Error occurred while trying to obtain device types.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
} finally {
|
|
|
|
|
MetadataManagementDAOFactory.closeConnection();
|
|
|
|
|
deviceFrequencies.put(deviceType, frequency);
|
|
|
|
|
addMetaData(deviceFrequencies, tenantId);
|
|
|
|
|
} catch (MetadataKeyAlreadyExistsException e) {
|
|
|
|
|
log.error("Specified metaKey is already exist.", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getTaskFrequency(String deviceType, int tenantId) throws MetadataManagementException {
|
|
|
|
|
try {
|
|
|
|
|
MetadataManagementDAOFactory.openConnection();
|
|
|
|
|
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY);
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
DeviceFrequencyMap deviceFrequencyMap = gson.fromJson(metaValue, DeviceFrequencyMap.class);
|
|
|
|
|
// Check if the device type exists in the map
|
|
|
|
|
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<String, Integer> 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 {
|
|
|
|
|
DeviceFrequencyMap deviceFrequencyMap = cache.getIfPresent(Integer.toString(tenantId));
|
|
|
|
|
if (deviceFrequencyMap == null) {
|
|
|
|
|
deviceFrequencyMap = loadDeviceFrequencyMap();
|
|
|
|
|
cache.put(Integer.toString(tenantId), deviceFrequencyMap);
|
|
|
|
|
}
|
|
|
|
|
if (deviceFrequencyMap.containsKey(deviceType)) {
|
|
|
|
|
// Retrieve the frequency for the given device type
|
|
|
|
|
return deviceFrequencyMap.get(deviceType).getFrequency();
|
|
|
|
|
} else {
|
|
|
|
|
log.error("Device type not found: " + deviceType);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|