Merge pull request 'Improve device status management service' (#510) from tcdlpds/device-mgt-core:master into master

Reviewed-on: community/device-mgt-core#510
status
Navod Zoysa 2 months ago
commit 35f02ba7ac

@ -257,14 +257,12 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
metadataManagementService.createMetadata(metaData);
return apiApplicationKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while creating meta data for meta key: " + applicationName;
} catch (MetadataKeyAlreadyExistsException e) {
String msg = "Since meta key:" + applicationName + " already exists, meta data creating process failed.";
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (MetadataKeyAlreadyExistsException e) {
String msg =
"Since meta key:" + applicationName + " already exists, meta data creating process " +
"failed.";
} catch (MetadataManagementException e) {
String msg = "Error occurred while creating meta data for meta key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (BadRequestException e) {

@ -196,10 +196,10 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
APIPublisherDataHolder.getInstance().setPermScopeMapping(permScopeMap);
log.info(Constants.PERM_SCOPE_MAPPING_META_KEY + "entry updated successfully");
} catch (MetadataManagementException e) {
log.error("Error encountered while updating permission scope mapping metadata with default scopes");
} catch (MetadataKeyAlreadyExistsException e) {
log.error("Metadata entry already exists for " + Constants.PERM_SCOPE_MAPPING_META_KEY);
} catch (MetadataManagementException e) {
log.error("Error encountered while updating permission scope mapping metadata with default scopes");
}
}
}

@ -20,13 +20,20 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.DeviceStatusFilterService;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyNotFoundException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import javax.ws.rs.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
@ -42,15 +49,14 @@ public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService
@GET
@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);
if (result != null) {
return Response.status(Response.Status.OK).entity(result).build();
}
return Response.status(Response.Status.NO_CONTENT).entity(false).build();
return Response.status(Response.Status.OK).entity(deviceManagementProviderService
.getDeviceStatusFilters(deviceType, CarbonContext.getThreadLocalCarbonContext().getTenantId())).build();
} catch (MetadataKeyNotFoundException e) {
String msg = "Couldn't find the device status filter details for device type: " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting device status filter of the tenant.";
log.error(msg, e);

@ -36,8 +36,6 @@ import org.apache.commons.logging.LogFactory;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
/**
@ -147,21 +145,4 @@ public class MetadataServiceImpl implements MetadataService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
/**
* Useful to send files as application/octet-stream responses
*/
private Response sendFileStream(byte[] content) throws IOException {
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
Response.ResponseBuilder response = Response
.ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
response.status(Response.Status.OK);
response.header("Content-Length", content.length);
return response.build();
} catch (IOException e) {
String msg = "Error occurred while creating input stream from buffer array. ";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}

@ -21,7 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.common.exceptions;
/**
* Custom exception class to be used in MetadataMgmt related functionalities.
*/
public class MetadataKeyAlreadyExistsException extends Exception {
public class MetadataKeyAlreadyExistsException extends MetadataManagementException {
private static final long serialVersionUID = -1814347544027733436L;
private String errorMessage;

@ -21,7 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.common.exceptions;
/**
* Custom exception class to be used in MetadataMgmt related functionalities.
*/
public class MetadataKeyNotFoundException extends Exception {
public class MetadataKeyNotFoundException extends MetadataManagementException {
private static final long serialVersionUID = 5260831982626354815L;
private String errorMessage;

@ -38,7 +38,7 @@ public interface DeviceStatusManagementService {
*
* @throws MetadataManagementException if error while resetting default device status
*/
void resetToDefaultDeviceStausFilter() throws MetadataManagementException;
void resetToDefaultDeviceStatusFilter(int tenantId) throws MetadataManagementException;
/**
* This method is useful to update existing allowed device status

@ -346,15 +346,15 @@ public class DeviceManagementServiceComponent {
bundleContext.registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null);
/* Registering DeviceState Filter Service */
DeviceStatusManagementService deviceStatusManagemntService = new DeviceStatusManagementServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagemntService);
DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService);
try {
deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(tenantId);
deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist(tenantId);
} catch (Throwable e) {
log.error("Error occurred while adding default tenant device status", e);
}
bundleContext.registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagemntService, null);
bundleContext.registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagementService, null);
/* Registering Event Configuration Service */
EventConfigurationProviderService eventConfigurationService = new EventConfigurationProviderServiceImpl();

@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
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.MetadataKeyNotFoundException;
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;
@ -42,12 +43,12 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService {
private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class);
private final MetadataDAO metadataDAO;
private static final Gson gson = new Gson();
public DeviceStatusManagementServiceImpl() {
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
@ -57,12 +58,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) 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);
if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY)) {
metadataDAO.addMetadata(tenantId, constructDeviceStatusMetadata(getDefaultDeviceStatus()));
}
if (!metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) {
metadataDAO.addMetadata(tenantId, constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()));
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
@ -80,8 +80,27 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
}
@Override
public void resetToDefaultDeviceStausFilter() throws MetadataManagementException {
public void resetToDefaultDeviceStatusFilter(int tenantId) throws MetadataManagementException {
try {
MetadataManagementDAOFactory.beginTransaction();
Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus());
Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck());
// Add default device status and device status check metadata entries
metadataDAO.addMetadata(tenantId, defaultDeviceStatusMetadata);
metadataDAO.addMetadata(tenantId, defaultDeviceStatusCheckMetadata);
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
MetadataManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while inserting default device status metadata entries.";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while starting the transaction to reset default device status filters.";
log.error(msg, e);
throw new MetadataManagementException(msg, e);
} finally {
MetadataManagementDAOFactory.closeConnection();
}
}
@Override
@ -91,7 +110,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
// Retrieve the current device status metadata
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
if (metadata != null) {
Gson gson = new Gson();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> currentStatusList = gson.fromJson(metadata.getMetaValue(), listType);
@ -105,7 +123,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
}
}
metadata.setMetaValue(gson.toJson(currentStatusList));
updateMetadataEntry(tenantId, metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
metadataDAO.updateMetadata(tenantId, metadata);
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
@ -124,14 +142,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
@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;
metadataDAO.updateMetadata(tenantId, constructDeviceStatusCheckMetadata(isChecked));
return true;
}
MetadataManagementDAOFactory.commitTransaction();
} catch (MetadataManagementDAOException e) {
@ -146,7 +162,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
} finally {
MetadataManagementDAOFactory.closeConnection();
}
return success;
return false;
}
@Override
@ -154,11 +170,15 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
try {
MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
Gson gson = new Gson();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType);
return statusList;
if (metadata == null) {
String msg =
"Couldn't find the meta data value for meta key: " + MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY;
log.error(msg);
throw new MetadataKeyNotFoundException(msg);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
return gson.fromJson(metadata.getMetaValue(), listType);
} catch (MetadataManagementDAOException e) {
String msg = "Error occurred while retrieving device status meta data for tenant:" + tenantId;
log.error(msg, e);
@ -177,8 +197,14 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
try {
MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
Gson gson = new Gson();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {}.getType();
if (metadata == null) {
String msg = "Couldn't find the meta details of meta Key: "
+ MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY + " and tenant Id: " + tenantId;
log.error(msg);
throw new MetadataKeyNotFoundException(msg);
}
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType);
for (AllowedDeviceStatus status : statusList) {
@ -207,6 +233,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
try {
MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
if (metadata == null) {
String msg = "Couldn't find the meta data value for meta key: "
+ MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY + " and tenant Id: " + tenantId;
log.error(msg);
throw new MetadataKeyNotFoundException(msg);
}
String metaValue = metadata.getMetaValue();
return Boolean.parseBoolean(metaValue);
} catch (MetadataManagementDAOException e) {
@ -227,8 +259,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
try {
MetadataManagementDAOFactory.openConnection();
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
if (metadata == null) {
String msg = "Couldn't find the meta data value for meta key: "
+ MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY + " and tenant Id: " + tenantId;
log.error(msg);
throw new MetadataKeyNotFoundException(msg);
}
Gson gson = new Gson();
Type listType = new TypeToken<List<AllowedDeviceStatus>>() {
}.getType();
List<AllowedDeviceStatus> statusList = gson.fromJson(metadata.getMetaValue(), listType);
@ -254,22 +291,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
}
}
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 void updateMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
metadataDAO.updateMetadata(tenantId, metadata);
if (log.isDebugEnabled()) {
log.debug(key + " metadata entry has been updated successfully");
}
}
/**
* To construct device status Meta data by using received device status items
*
* @param deviceStatusItems {@link List<DeviceStatusItem>}
* @return {@link Metadata}
*/
private Metadata constructDeviceStatusMetadata(List<DeviceStatusItem> deviceStatusItems) {
Gson gson = new Gson();
String deviceStatusItemsJsonString = gson.toJson(deviceStatusItems);
Metadata metadata = new Metadata();
@ -279,6 +307,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
return metadata;
}
/**
* To construct device status check Meta data to either enable it or disable it.
*
* @param deviceStatusCheck True or False
* @return {@link Metadata}
*/
private Metadata constructDeviceStatusCheckMetadata(boolean deviceStatusCheck) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("deviceStatusCheck", String.valueOf(deviceStatusCheck));
@ -289,6 +323,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
return metadata;
}
/**
* Get default list of device status items from the configuration.
*
* @return List of device status items
*/
private List<DeviceStatusItem> getDefaultDeviceStatus() {
DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations();
List<DeviceStatusItem> deviceStatusItems = new ArrayList<>();
@ -305,6 +344,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement
return deviceStatusItems;
}
/**
* Get Default device status check from the configuration.
*
* @return default status check value, it will be either 'True' or 'False'
*/
private boolean getDefaultDeviceStatusCheck() {
DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations();
boolean deviceStatusCheck = false;

@ -107,13 +107,13 @@ public class MetaRepositoryBasedLicenseManager implements LicenseManager {
} else {
metadataManagementService.createMetadata(metadata);
}
} catch (MetadataManagementException e) {
String msg = "Error occurred while saving the licence value in meta data repository";
} catch (MetadataKeyAlreadyExistsException e) {
String msg = "Error occurred while saving the licence key and licence key exist.Licence Key: "
+ licenceKey;
log.error(msg, e);
throw new LicenseManagementException(msg, e);
} catch (MetadataKeyAlreadyExistsException e) {
String msg =
"Error occurred while saving the licence key and licence key exist. Licence Key: " + licenceKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while saving the licence value in meta data repository";
log.error(msg, e);
throw new LicenseManagementException(msg, e);
}

Loading…
Cancel
Save