Merge branch 'corrective-policy' into 'corrective-policy'

Fix code formatting issues in geo fence and corrective policy features

See merge request entgra/carbon-device-mgt!703
corrective-policy
Dharmakeerthi Lasantha 4 years ago
commit d4dde59fc5

@ -75,7 +75,7 @@ public class GeofenceWrapper {
@ApiModelProperty( @ApiModelProperty(
name = "groupIds", name = "groupIds",
value = "Group ids to add geo fences", value = "Group ids mapped with geo fences",
required = true) required = true)
private List<Integer> groupIds; private List<Integer> groupIds;

@ -40,11 +40,17 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.event.config.EventConfig; import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException; import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.geo.service.Alert;
import org.wso2.carbon.device.mgt.common.geo.service.*; import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException;
import org.wso2.carbon.device.mgt.common.geo.service.Event;
import org.wso2.carbon.device.mgt.common.geo.service.GeoFence;
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService;
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
@ -64,7 +70,15 @@ import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.ws.rs.*; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.Response; import javax.ws.rs.core.Response;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -624,6 +638,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
} }
} }
/**
* Extract request event data from the payload and attach it to the DTO
* @param eventConfig request event payload
* @return generated event beans list according to the payload data
*/
private List<EventConfig> mapRequestEvent(List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventConfig) { private List<EventConfig> mapRequestEvent(List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventConfig) {
List<EventConfig> savingEventList = new ArrayList<>(); List<EventConfig> savingEventList = new ArrayList<>();
for (org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig event : eventConfig) { for (org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig event : eventConfig) {
@ -652,6 +671,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
GeofenceData geofenceData = geoService.getGeoFences(fenceId); GeofenceData geofenceData = geoService.getGeoFences(fenceId);
if (geofenceData == null) { if (geofenceData == null) {
String msg = "No valid Geofence found for ID " + fenceId; String msg = "No valid Geofence found for ID " + fenceId;
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} }
if (requireEventData) { if (requireEventData) {
@ -661,6 +681,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
return Response.status(Response.Status.OK).entity(getMappedResponseBean(geofenceData)).build(); return Response.status(Response.Status.OK).entity(getMappedResponseBean(geofenceData)).build();
} catch (GeoLocationBasedServiceException e) { } catch (GeoLocationBasedServiceException e) {
String msg = "Server error occurred while retrieving Geofence for Id " + fenceId; String msg = "Server error occurred while retrieving Geofence for Id " + fenceId;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }
@ -692,6 +713,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
return geofenceWrapper; return geofenceWrapper;
} }
/**
* Get event list to send with the response
* @param eventConfig event list retrieved
* @return list of response event beans
*/
private List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> getEventConfigBean(List<EventConfig> eventConfig) { private List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> getEventConfigBean(List<EventConfig> eventConfig) {
List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventList = new ArrayList<>(); List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventList = new ArrayList<>();
org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig eventData; org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig eventData;
@ -733,16 +759,16 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
if (!geoFences.isEmpty() && requireEventData) { if (!geoFences.isEmpty() && requireEventData) {
geoFences = geoService.attachEventObjects(geoFences); geoFences = geoService.attachEventObjects(geoFences);
} }
return getResponse(geoFences); return buildResponse(geoFences);
} }
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
List<GeofenceData> geoFences = geoService.getGeoFences(name); List<GeofenceData> geoFences = geoService.getGeoFences(name);
if (requireEventData) { if (requireEventData) {
geoFences = geoService.attachEventObjects(geoFences); geoFences = geoService.attachEventObjects(geoFences);
} }
return getResponse(geoFences); return buildResponse(geoFences);
} }
return getResponse(geoService.getGeoFences()); return buildResponse(geoService.getGeoFences());
} catch (GeoLocationBasedServiceException e) { } catch (GeoLocationBasedServiceException e) {
String msg = "Failed to retrieve geofence data"; String msg = "Failed to retrieve geofence data";
log.error(msg, e); log.error(msg, e);
@ -750,7 +776,12 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
} }
} }
private Response getResponse(List<GeofenceData> fencesList) { /**
* Build the response payload from the data retrieved from the database
* @param fencesList retrieved geofence data to send in response
* @return HttpResponse object
*/
private Response buildResponse(List<GeofenceData> fencesList) {
List<GeofenceWrapper> geofenceList = new ArrayList<>(); List<GeofenceWrapper> geofenceList = new ArrayList<>();
for (GeofenceData geofenceData : fencesList) { for (GeofenceData geofenceData : fencesList) {
geofenceList.add(getMappedResponseBean(geofenceData)); geofenceList.add(getMappedResponseBean(geofenceData));
@ -761,7 +792,6 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
return Response.status(Response.Status.OK).entity(paginationResult).build(); return Response.status(Response.Status.OK).entity(paginationResult).build();
} }
@DELETE @DELETE
@Override @Override
@Path("/geo-fence/{fenceId}") @Path("/geo-fence/{fenceId}")
@ -816,6 +846,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
} }
} }
/**
* Parse geofence data from the request payload to the GeofenceData DTO
* @param geofenceWrapper request payload data
* @return GeofenceData object built from the request data
*/
private GeofenceData mapRequestGeofenceData(GeofenceWrapper geofenceWrapper) { private GeofenceData mapRequestGeofenceData(GeofenceWrapper geofenceWrapper) {
GeofenceData geofenceData = new GeofenceData(); GeofenceData geofenceData = new GeofenceData();
geofenceData.setFenceName(geofenceWrapper.getFenceName()); geofenceData.setFenceName(geofenceWrapper.getFenceName());

@ -684,6 +684,10 @@ public class RequestValidationUtil {
&& StringUtils.isEmpty(emailAddress); && StringUtils.isEmpty(emailAddress);
} }
/**
* Check the request payload attributes are correct for create a geofence
* @param geofenceWrapper request payload data
*/
public static void validateGeofenceData(GeofenceWrapper geofenceWrapper) { public static void validateGeofenceData(GeofenceWrapper geofenceWrapper) {
boolean isGeoJsonExists = false; boolean isGeoJsonExists = false;
if (geofenceWrapper.getFenceName() == null || geofenceWrapper.getFenceName().trim().isEmpty()) { if (geofenceWrapper.getFenceName() == null || geofenceWrapper.getFenceName().trim().isEmpty()) {
@ -721,6 +725,10 @@ public class RequestValidationUtil {
} }
} }
/**
* Check the request payload attributes are correct for create an event record
* @param eventConfig request payload data
*/
public static void validateEventConfigurationData(List<EventConfig> eventConfig) { public static void validateEventConfigurationData(List<EventConfig> eventConfig) {
if (eventConfig == null ||eventConfig.isEmpty()) { if (eventConfig == null ||eventConfig.isEmpty()) {
String msg = "Event configuration is mandatory, since should not be null or empty"; String msg = "Event configuration is mandatory, since should not be null or empty";

@ -476,7 +476,7 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"AND G.FENCE_ID = ?"; "AND G.FENCE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, geofenceId); stmt.setInt(1, geofenceId);
return getEventConfigs(eventList, stmt); return getEventConfigs(stmt);
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while updating Geofence record with id "; String msg = "Error occurred while updating Geofence record with id ";
@ -521,7 +521,14 @@ public class GeofenceDAOImpl implements GeofenceDAO {
} }
} }
private List<EventConfig> getEventConfigs(List<EventConfig> eventList, PreparedStatement stmt) throws SQLException { /**
* Retrieve the geofence event extracted from the DB
* @param stmt prepared statement to retrieve data from the DB
* @return Retrieved Event list from the DB
* @throws SQLException for the errors occur while accessing the DB
*/
private List<EventConfig> getEventConfigs(PreparedStatement stmt) throws SQLException {
List<EventConfig> eventList = new ArrayList<>();
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
EventConfig event; EventConfig event;
while (resultSet.next()) { while (resultSet.next()) {

@ -131,7 +131,6 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
eventConfigDAO.updateEventRecords(eventsToUpdate); eventConfigDAO.updateEventRecords(eventsToUpdate);
} }
if (!groupIdsToDelete.isEmpty()) { if (!groupIdsToDelete.isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Deleting event group mapping records of groups"); log.debug("Deleting event group mapping records of groups");
@ -160,6 +159,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Failed to start/open transaction to store device event configurations"; String msg = "Failed to start/open transaction to store device event configurations";
log.error(msg, e);
throw new EventConfigurationException(msg, e); throw new EventConfigurationException(msg, e);
} catch (EventManagementDAOException e) { } catch (EventManagementDAOException e) {
String msg = "Error occurred while saving event records"; String msg = "Error occurred while saving event records";
@ -227,6 +227,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Failed to start/open transaction to delete device event configurations"; String msg = "Failed to start/open transaction to delete device event configurations";
log.error(msg, e);
throw new EventConfigurationException(msg, e); throw new EventConfigurationException(msg, e);
} catch (EventManagementDAOException e) { } catch (EventManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();

@ -1330,8 +1330,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
* @param tenantId id of the fence owning tenant * @param tenantId id of the fence owning tenant
*/ */
private void createEventRevokeTask(GeofenceData geofenceData, int tenantId) { private void createEventRevokeTask(GeofenceData geofenceData, int tenantId) {
GeoFenceEventOperationManager eventManager = new GeoFenceEventOperationManager(OperationMgtConstants.OperationCodes.EVENT_REVOKE, GeoFenceEventOperationManager eventManager =
tenantId, values -> createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, geofenceData, tenantId)); new GeoFenceEventOperationManager(OperationMgtConstants.OperationCodes.EVENT_REVOKE, tenantId,
values -> createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, geofenceData, tenantId));
ScheduledExecutorService eventOperationExecutor = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService eventOperationExecutor = Executors.newSingleThreadScheduledExecutor();
eventOperationExecutor.schedule(eventManager eventOperationExecutor.schedule(eventManager
.getEventOperationExecutor(geofenceData), 10, TimeUnit.SECONDS); .getEventOperationExecutor(geofenceData), 10, TimeUnit.SECONDS);

@ -607,6 +607,10 @@ public final class DeviceManagerUtil {
return eventsPublisherService; return eventsPublisherService;
} }
/**
* Retrieve EventConfigurationProviderService osgi service component
* @return {@link EventConfigurationProviderService} service component
*/
public static EventConfigurationProviderService getEventConfigService() { public static EventConfigurationProviderService getEventConfigService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
EventConfigurationProviderService eventConfigService = EventConfigurationProviderService eventConfigService =
@ -657,6 +661,9 @@ public final class DeviceManagerUtil {
} }
} }
/**
* Enable Geofence caching according to the configurations proviced by cdm-config.xml
*/
public static void initializeGeofenceCache() { public static void initializeGeofenceCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
int geoCacheExpiry = config.getGeoFenceCacheConfiguration().getExpiryTime(); int geoCacheExpiry = config.getGeoFenceCacheConfiguration().getExpiryTime();
@ -713,6 +720,10 @@ public final class DeviceManagerUtil {
return deviceCache; return deviceCache;
} }
/**
* Get geofence cache object
* @return {@link Cache<GeoCacheKey, GeofenceData>}
*/
public static Cache<GeoCacheKey, GeofenceData> getGeoCache() { public static Cache<GeoCacheKey, GeofenceData> getGeoCache() {
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
CacheManager manager = getCacheManager(); CacheManager manager = getCacheManager();

@ -1451,6 +1451,12 @@ public class PolicyManagerImpl implements PolicyManager {
Collections.sort(policyList); Collections.sort(policyList);
} }
/**
* Get the corrective action list of a specific policy
* @param allCorrectiveActions stored corrective actions of all policies
* @param policyId Id of the policy to get corrective action
* @return
*/
private List<CorrectiveAction> getSingleCorrectiveAction private List<CorrectiveAction> getSingleCorrectiveAction
(List<CorrectiveAction> allCorrectiveActions, int policyId) { (List<CorrectiveAction> allCorrectiveActions, int policyId) {
List<CorrectiveAction> correctiveActionsOfPolicy = new ArrayList<>(); List<CorrectiveAction> correctiveActionsOfPolicy = new ArrayList<>();
@ -1464,6 +1470,13 @@ public class PolicyManagerImpl implements PolicyManager {
return correctiveActionsOfPolicy; return correctiveActionsOfPolicy;
} }
/**
* Set the corrective actions of a specific policy against with the policy profile.
* This method is using with the new implementation of corrective policies which is able to apply multiple corrective
* policies based on a feature code of a policy
* @param allCorrectiveActions corrective action list retrieved from the DB
* @param profile profile of the selected policy
*/
private void setMultipleCorrectiveActions(List<CorrectiveAction> allCorrectiveActions, private void setMultipleCorrectiveActions(List<CorrectiveAction> allCorrectiveActions,
Profile profile) { Profile profile) {
for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) { for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) {

@ -142,6 +142,12 @@ public class PolicyManagerUtil {
return buff.toString(); return buff.toString();
} }
/**
* Transform policy into a Operation
* @param policy policy to be transformed
* @return policy operation object
* @throws PolicyTransformException for errors occurred while transforming a policy
*/
public static Operation transformPolicy(Policy policy) throws PolicyTransformException { public static Operation transformPolicy(Policy policy) throws PolicyTransformException {
List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList(); List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList();
PolicyOperation policyOperation = new PolicyOperation(); PolicyOperation policyOperation = new PolicyOperation();

Loading…
Cancel
Save