Add caching layer for DeviceStatusManagementServiceImpl

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

@ -18,6 +18,8 @@
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 com.google.gson.reflect.TypeToken;
@ -42,6 +44,7 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService {
@ -54,6 +57,10 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
private final Gson gson;
private static final Cache<String, List<AllowedDeviceStatus>> cache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.HOURS)
.build();
private DeviceStatusManagementServiceImpl() {
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
this.gson = new Gson();
@ -282,27 +289,29 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
log.error(msg);
throw new IllegalArgumentException(msg);
}
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);
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);
cache.put(Integer.toString(tenantId), allowedDeviceStatus);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metaValue, listType);
for (AllowedDeviceStatus status : statusList) {
for (AllowedDeviceStatus status : allowedDeviceStatus) {
if (deviceType.equalsIgnoreCase(status.getType())) {
List<String> allowedStatus = status.getStatus();
return allowedStatus.contains(deviceStatus);
}
}
return false; // Device type not found in metadata
}

Loading…
Cancel
Save