|
|
@ -18,55 +18,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
|
|
|
|
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.Gson;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
|
|
|
|
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.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.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.DeviceFrequencyMap;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
|
|
|
|
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.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.dto.DeviceType;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
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.metadata.mgt.dao.util.MetadataConstants;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurationService {
|
|
|
|
public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurationService {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceTaskConfigurationServiceImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceTaskConfigurationServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
private final MetadataDAO metadataDAO;
|
|
|
|
private final MetadataManagementService metadataManagementService;
|
|
|
|
|
|
|
|
|
|
|
|
public DeviceTaskConfigurationServiceImpl() {
|
|
|
|
private final Cache<String, DeviceFrequencyMap> cache;
|
|
|
|
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
|
|
|
|
public DeviceTaskConfigurationServiceImpl() {
|
|
|
|
metadataDAO.addMetadata(tenantId, metadata);
|
|
|
|
this.metadataManagementService = new MetadataManagementServiceImpl();
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
this.cache = CacheBuilder.newBuilder()
|
|
|
|
log.debug(key + " metadata entry has been inserted successfully");
|
|
|
|
.expireAfterWrite(1, TimeUnit.HOURS)
|
|
|
|
}
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Metadata constructTaskFrequencyMetadata(Map<String, Integer> deviceFrequencies) {
|
|
|
|
private Metadata constructTaskFrequencyMetadata(Map<String, Integer> deviceFrequencies) {
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
|
|
|
int frequency;
|
|
|
|
for (Map.Entry<String, Integer> entry : deviceFrequencies.entrySet()) {
|
|
|
|
for (Map.Entry<String, Integer> entry : deviceFrequencies.entrySet()) {
|
|
|
|
String deviceType = entry.getKey();
|
|
|
|
String deviceType = entry.getKey();
|
|
|
|
int frequency = entry.getValue();
|
|
|
|
frequency = entry.getValue();
|
|
|
|
// Create a JsonObject for the current device type
|
|
|
|
|
|
|
|
JsonObject deviceObject = new JsonObject();
|
|
|
|
JsonObject deviceObject = new JsonObject();
|
|
|
|
deviceObject.addProperty("frequency", String.valueOf(frequency));
|
|
|
|
deviceObject.addProperty("frequency", String.valueOf(frequency));
|
|
|
|
jsonObject.add(deviceType, deviceObject);
|
|
|
|
jsonObject.add(deviceType, deviceObject);
|
|
|
@ -77,9 +74,20 @@ public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurati
|
|
|
|
return metadata;
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
return operationMonitoringTaskConfig.getFrequency();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -92,87 +100,64 @@ public class DeviceTaskConfigurationServiceImpl implements DeviceTaskConfigurati
|
|
|
|
for (DeviceType deviceType : deviceTypes) {
|
|
|
|
for (DeviceType deviceType : deviceTypes) {
|
|
|
|
deviceFrequencies.put(deviceType.getName(), getDefaultTaskFrequency(dms, deviceType.getName()));
|
|
|
|
deviceFrequencies.put(deviceType.getName(), getDefaultTaskFrequency(dms, deviceType.getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetadataManagementDAOFactory.beginTransaction();
|
|
|
|
addMetaData(deviceFrequencies, tenantId);
|
|
|
|
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) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String msg = "Error occurred while trying to obtain device types.";
|
|
|
|
log.error("Error occurred while trying to obtain device types.", e);
|
|
|
|
log.error(msg, e);
|
|
|
|
} catch (MetadataKeyAlreadyExistsException e) {
|
|
|
|
} finally {
|
|
|
|
log.error("Specified metaKey is already exist.", e);
|
|
|
|
MetadataManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void addTaskFrequency(int tenantId, int frequency) throws MetadataManagementException {
|
|
|
|
public void addTaskFrequency(int tenantId, int frequency, String deviceType) throws MetadataManagementException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Map<String, Integer> deviceFrequencies = new HashMap<>();
|
|
|
|
Map<String, Integer> deviceFrequencies = new HashMap<>();
|
|
|
|
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
|
|
|
|
deviceFrequencies.put(deviceType, frequency);
|
|
|
|
List<DeviceType> deviceTypes = dms.getDeviceTypes();
|
|
|
|
addMetaData(deviceFrequencies, tenantId);
|
|
|
|
for (DeviceType deviceType : deviceTypes) {
|
|
|
|
} catch (MetadataKeyAlreadyExistsException e) {
|
|
|
|
deviceFrequencies.put(deviceType.getName(), frequency);
|
|
|
|
log.error("Specified metaKey is already exist.", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
* Loads the device frequency map from the metadata management service.
|
|
|
|
try {
|
|
|
|
*
|
|
|
|
MetadataManagementDAOFactory.openConnection();
|
|
|
|
* @return the device frequency map parsed from the stored metadata.
|
|
|
|
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.DEVICE_TASK_FREQUENCY);
|
|
|
|
* @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();
|
|
|
|
String metaValue = metadata.getMetaValue();
|
|
|
|
Gson gson = new Gson();
|
|
|
|
Gson gson = new Gson();
|
|
|
|
DeviceFrequencyMap deviceFrequencyMap = gson.fromJson(metaValue, DeviceFrequencyMap.class);
|
|
|
|
return gson.fromJson(metaValue, DeviceFrequencyMap.class);
|
|
|
|
// Check if the device type exists in the map
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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)) {
|
|
|
|
if (deviceFrequencyMap.containsKey(deviceType)) {
|
|
|
|
// Retrieve the frequency for the given device type
|
|
|
|
|
|
|
|
return deviceFrequencyMap.get(deviceType).getFrequency();
|
|
|
|
return deviceFrequencyMap.get(deviceType).getFrequency();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.error("Device type not found: " + deviceType);
|
|
|
|
throw new MetadataManagementException("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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|