Resolve comments

fix-subtenat-device-status-issue
Pramila Niroshan 5 months ago
parent 9e364ddb97
commit b991701d50

@ -43,10 +43,9 @@ public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService
@Path("/{deviceType}")
public Response getDeviceStatusFilters(@PathParam("deviceType") String deviceType) {
List<String> result;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService();
result = deviceManagementProviderService.getDeviceStatusFilters(deviceType, tenantId);
result = deviceManagementProviderService.getDeviceStatusFilters(deviceType);
if (result != null) {
return Response.status(Response.Status.OK).entity(result).build();
}
@ -82,10 +81,9 @@ public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService
@QueryParam("isEnabled")
boolean isEnabled) {
boolean result;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService();
result = deviceManagementProviderService.updateDefaultDeviceStatusCheck(tenantId, isEnabled);
result = deviceManagementProviderService.updateDefaultDeviceStatusCheck(isEnabled);
if (result) {
return Response.status(Response.Status.OK).entity("Successfully updated device status check.").build();
} else {

@ -31,7 +31,7 @@ public interface DeviceStatusManagementService {
*
* @throws MetadataManagementException if error while adding default device status
*/
void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException;
void addDefaultDeviceStatusFilterIfNotExist() throws MetadataManagementException;
/**
* This method is useful to reset existing device status to default values in xml
@ -53,21 +53,21 @@ public interface DeviceStatusManagementService {
*
* @throws MetadataManagementException if error while updating existing device status
*/
boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked)
boolean updateDefaultDeviceStatusCheck(boolean isChecked)
throws MetadataManagementException;
/**
* This method is useful to get existing device status filters
*
* @throws MetadataManagementException if error while getting existing device status
*/
List<AllowedDeviceStatus> getDeviceStatusFilters(int tenantId) throws MetadataManagementException;
List<AllowedDeviceStatus> getDeviceStatusFilters() throws MetadataManagementException;
/**
* This method is useful to get existing device status filters by device type and tenant id
*
* @throws MetadataManagementException if error while getting existing device status
*/
List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException;
List<String> getDeviceStatusFilters(String deviceType) throws MetadataManagementException;
/**
* This method is useful to get existing device status filters

@ -386,7 +386,7 @@ public class DeviceManagementServiceComponent {
DeviceStatusManagementService deviceStatusManagemntService = DeviceStatusManagementServiceImpl.getInstance();
DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagemntService);
try {
deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(tenantId);
deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist();
} catch (Throwable e) {
log.error("Error occurred while adding default tenant device status", e);

@ -23,8 +23,8 @@ import com.google.common.cache.CacheBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
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.AllowedDeviceStatus;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
@ -32,9 +32,7 @@ import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManageme
import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusConfigurations;
import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusItem;
import io.entgra.device.mgt.core.device.mgt.core.config.ui.UIConfigurationManager;
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.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
@ -51,9 +49,9 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class);
private static DeviceStatusManagementServiceImpl instance;
private static volatile DeviceStatusManagementServiceImpl instance;
private final MetadataDAO metadataDAO;
private static final MetadataManagementService metadataManagementService = DeviceManagementDataHolder.getInstance().getMetadataManagementService();
private final Gson gson;
@ -62,7 +60,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
.build();
private DeviceStatusManagementServiceImpl() {
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
this.gson = new Gson();
}
@ -74,57 +71,30 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
}
@Override
public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException {
public void addDefaultDeviceStatusFilterIfNotExist() throws MetadataManagementException {
try {
MetadataManagementDAOFactory.beginTransaction();
if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY) && !metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) {
Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus());
Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck());
// Add default device status and device status check metadata entries
addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while inserting default device status 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);
} finally {
MetadataManagementDAOFactory.closeConnection();
addMetadataEntry(defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
addMetadataEntry(defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
} catch (MetadataKeyAlreadyExistsException e) {
log.error("Metadata is already exist.", e);
}
}
/**
* Adds default device status filters for a given tenant.
*
* @param tenantId the tenant ID for which to add the device status filters.
* @throws MetadataManagementException if an error occurs while adding the metadata or managing the transaction.
*/
private void addDefaultDeviceStatusFilters(int tenantId) throws MetadataManagementException {
private void addDefaultDeviceStatusFilters() throws MetadataManagementException {
try {
MetadataManagementDAOFactory.beginTransaction();
if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY)) {
Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus());
// Add default device status metadata entries
addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while inserting default device status 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);
} finally {
MetadataManagementDAOFactory.closeConnection();
addMetadataEntry(defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
} catch (MetadataKeyAlreadyExistsException e) {
log.error("Metadata is already exist.", e);
}
}
@ -136,24 +106,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
*/
private void addDefaultDeviceStatusCheck(int tenantId) throws MetadataManagementException {
try {
MetadataManagementDAOFactory.beginTransaction();
if (!metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) {
Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck());
// Add default device status check metadata entries
addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while inserting default device status 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);
} finally {
MetadataManagementDAOFactory.closeConnection();
addMetadataEntry(defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
} catch (MetadataKeyAlreadyExistsException e) {
log.error("Metadata is already exist.", e);
}
}
@ -164,80 +121,46 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
@Override
public void updateDefaultDeviceStatusFilters(int tenantId, String deviceType, List<String> deviceStatus) throws MetadataManagementException {
try {
if (StringUtils.isEmpty(deviceType)) {
String msg = "Device type must not be empty or null";
log.error(msg);
throw new IllegalArgumentException(msg);
}
MetadataManagementDAOFactory.beginTransaction();
// Retrieve the current device status metadata
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
if (metadata != null) {
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> currentStatusList = gson.fromJson(metadata.getMetaValue(), listType);
// Find the status for the specified deviceType
for (AllowedDeviceStatus status : currentStatusList) {
if (deviceType.equalsIgnoreCase(status.getType())) {
// Update the status list for the specified deviceType
status.setStatus(deviceStatus);
break;
}
if (StringUtils.isEmpty(deviceType)) {
String msg = "Device type must not be empty or null";
log.error(msg);
throw new IllegalArgumentException(msg);
}
// Retrieve the current device status metadata
Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
if (metadata != null) {
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> currentStatusList = gson.fromJson(metadata.getMetaValue(), listType);
// Find the status for the specified deviceType
for (AllowedDeviceStatus status : currentStatusList) {
if (deviceType.equalsIgnoreCase(status.getType())) {
// Update the status list for the specified deviceType
status.setStatus(deviceStatus);
break;
}
metadata.setMetaValue(gson.toJson(currentStatusList));
updateMetadataEntry(tenantId, metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating device status 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);
} finally {
MetadataManagementDAOFactory.closeConnection();
metadata.setMetaValue(gson.toJson(currentStatusList));
updateMetadataEntry(metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
}
}
@Override
public boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked) throws MetadataManagementException {
boolean success = false;
try {
MetadataManagementDAOFactory.beginTransaction();
if (metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) {
Metadata isDeviceStatusChecked = constructDeviceStatusCheckMetadata(isChecked);
// Add default device status check metadata entries
updateMetadataEntry(tenantId, isDeviceStatusChecked, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
success = true;
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating device status check 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);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
return success;
public boolean updateDefaultDeviceStatusCheck(boolean isChecked) throws MetadataManagementException {
Metadata isDeviceStatusChecked = constructDeviceStatusCheckMetadata(isChecked);
// Add default device status check metadata entries
updateMetadataEntry(isDeviceStatusChecked, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
return true;
}
@Override
public List<AllowedDeviceStatus> getDeviceStatusFilters(int tenantId) throws MetadataManagementException {
return retrieveAndParseMetadata(tenantId);
public List<AllowedDeviceStatus> getDeviceStatusFilters() throws MetadataManagementException {
return retrieveAndParseMetadata();
}
public List<String> getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException {
List<AllowedDeviceStatus> statusList = retrieveAndParseMetadata(tenantId);
public List<String> getDeviceStatusFilters(String deviceType) throws MetadataManagementException {
List<AllowedDeviceStatus> statusList = retrieveAndParseMetadata();
for (AllowedDeviceStatus status : statusList) {
if (deviceType.equalsIgnoreCase(status.getType())) {
return status.getStatus();
@ -249,12 +172,10 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
/**
* Retrieves and parses the allowed device status metadata for the specified tenant.
* @param tenantId the tenant ID for which to retrieve and parse the metadata.
* @return a list of {@link AllowedDeviceStatus} objects parsed from the metadata.
* @throws MetadataManagementException if an error occurs while retrieving or parsing the metadata.
*/
private List<AllowedDeviceStatus> retrieveAndParseMetadata(int tenantId) throws MetadataManagementException {
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
private List<AllowedDeviceStatus> retrieveAndParseMetadata() throws MetadataManagementException {
Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
String metaValue;
if (metadata != null) {
@ -262,7 +183,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
} else {
List<DeviceStatusItem> defaultStatusList = getDefaultDeviceStatus();
metaValue = gson.toJson(defaultStatusList);
addDefaultDeviceStatusFilters(tenantId);
addDefaultDeviceStatusFilters();
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType();
return gson.fromJson(metaValue, listType);
@ -272,7 +193,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
@Override
public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException {
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
if (metadata != null) {
return Boolean.parseBoolean(metadata.getMetaValue());
@ -291,19 +211,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
}
List<AllowedDeviceStatus> allowedDeviceStatus = cache.getIfPresent(Integer.toString(tenantId));
if (allowedDeviceStatus == null) {
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
String metaValue;
if (metadata != null) {
metaValue = metadata.getMetaValue();
} else {
List<DeviceStatusItem> statusList = getDefaultDeviceStatus();
metaValue = gson.toJson(statusList);
addDefaultDeviceStatusFilters(tenantId);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
allowedDeviceStatus = gson.fromJson(metaValue, listType);
allowedDeviceStatus = retrieveAndParseMetadata();
cache.put(Integer.toString(tenantId), allowedDeviceStatus);
}
for (AllowedDeviceStatus status : allowedDeviceStatus) {
@ -315,22 +223,23 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
return false; // Device type not found in metadata
}
private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
metadataDAO.addMetadata(tenantId, metadata);
private void addMetadataEntry(Metadata metadata, String key) throws MetadataManagementException, MetadataKeyAlreadyExistsException {
metadata.setMetaKey(key);
metadataManagementService.createMetadata(metadata);
if (log.isDebugEnabled()) {
log.debug(key + " metadata entry has been inserted successfully");
}
}
private void updateMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
metadataDAO.updateMetadata(tenantId, metadata);
private void updateMetadataEntry(Metadata metadata, String key) throws MetadataManagementException {
metadata.setMetaKey(key);
metadataManagementService.updateMetadata(metadata);
if (log.isDebugEnabled()) {
log.debug(key + " metadata entry has been updated successfully");
}
}
private Metadata constructDeviceStatusMetadata(List<DeviceStatusItem> deviceStatusItems) {
Gson gson = new Gson();
String deviceStatusItemsJsonString = gson.toJson(deviceStatusItems);
Metadata metadata = new Metadata();
@ -348,6 +257,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
return metadata;
}
/**
* Retrieves the default device status items.
* This method fetches the device status configurations from the UI configuration manager.
* If the configurations are available, it returns the list of device status items.
*
* @return a list of {@code DeviceStatusItem} objects representing the default device statuses.
*/
private List<DeviceStatusItem> getDefaultDeviceStatus() {
DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations();
List<DeviceStatusItem> deviceStatusItems = new ArrayList<>();
@ -363,6 +279,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
return deviceStatusItems;
}
/**
* Retrieves the default device status check flag.
* This method fetches the device status configurations from the UI configuration manager.
* If the configurations are available, it returns the device status check flag.
*
* @return a boolean value indicating the default device status check flag.
*/
private boolean getDefaultDeviceStatusCheck() {
DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations();
boolean deviceStatusCheck = false;

@ -20,7 +20,9 @@
package io.entgra.device.mgt.core.device.mgt.core.search;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.BeforeClass;
@ -43,6 +45,9 @@ public class DeviceDetails extends BaseDeviceManagementTest {
DeviceManagementProviderService deviceManagementProviderService = new DeviceManagementProviderServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService);
MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl();
DeviceManagementDataHolder.getInstance().setMetadataManagementService(metadataManagementService);
DeviceStatusManagementService deviceStatusManagementService = DeviceStatusManagementServiceImpl.getInstance();
DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService);
}

@ -215,7 +215,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
if (!isMock()) {
DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder
.getInstance().getDeviceStatusManagementService();
deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist(MultitenantConstants.SUPER_TENANT_ID);
deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist();
Device device = TestDataHolder.generateDummyDeviceData(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE));
boolean enrollment = deviceMgtService.enrollDevice(device);
Assert.assertTrue(enrollment);

@ -113,7 +113,7 @@ public class TenantManagerImpl implements TenantManager {
initTenantFlow(tenantInfoBean);
try {
TenantMgtDataHolder.getInstance().getDeviceStatusManagementService().
addDefaultDeviceStatusFilterIfNotExist(tenantInfoBean.getTenantId());
addDefaultDeviceStatusFilterIfNotExist();
} catch (MetadataManagementException e) {
String msg = "Error occurred while adding default device status filter";
log.error(msg, e);

Loading…
Cancel
Save