diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java index e97323fd232..b016a3177bc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java @@ -75,7 +75,7 @@ public class GeofenceWrapper { @ApiModelProperty( name = "groupIds", - value = "Group ids to add geo fences", + value = "Group ids mapped with geo fences", required = true) private List groupIds; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index be5592b3404..ce7fecc8063 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -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.PaginationRequest; 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.EventConfigurationException; 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.*; +import org.wso2.carbon.device.mgt.common.geo.service.Alert; +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.core.geo.GeoCluster; 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.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 java.io.IOException; 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 mapRequestEvent(List eventConfig) { List savingEventList = new ArrayList<>(); 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); if (geofenceData == null) { String msg = "No valid Geofence found for ID " + fenceId; + log.error(msg); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } if (requireEventData) { @@ -661,6 +681,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { return Response.status(Response.Status.OK).entity(getMappedResponseBean(geofenceData)).build(); } catch (GeoLocationBasedServiceException e) { 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(); } } @@ -692,6 +713,11 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { return geofenceWrapper; } + /** + * Get event list to send with the response + * @param eventConfig event list retrieved + * @return list of response event beans + */ private List getEventConfigBean(List eventConfig) { List eventList = new ArrayList<>(); org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig eventData; @@ -733,16 +759,16 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { if (!geoFences.isEmpty() && requireEventData) { geoFences = geoService.attachEventObjects(geoFences); } - return getResponse(geoFences); + return buildResponse(geoFences); } if (name != null && !name.isEmpty()) { List geoFences = geoService.getGeoFences(name); if (requireEventData) { geoFences = geoService.attachEventObjects(geoFences); } - return getResponse(geoFences); + return buildResponse(geoFences); } - return getResponse(geoService.getGeoFences()); + return buildResponse(geoService.getGeoFences()); } catch (GeoLocationBasedServiceException e) { String msg = "Failed to retrieve geofence data"; log.error(msg, e); @@ -750,7 +776,12 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { } } - private Response getResponse(List 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 fencesList) { List geofenceList = new ArrayList<>(); for (GeofenceData geofenceData : fencesList) { geofenceList.add(getMappedResponseBean(geofenceData)); @@ -761,7 +792,6 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { return Response.status(Response.Status.OK).entity(paginationResult).build(); } - @DELETE @Override @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) { GeofenceData geofenceData = new GeofenceData(); geofenceData.setFenceName(geofenceWrapper.getFenceName()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index b227f9a231d..964429a1928 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -684,6 +684,10 @@ public class RequestValidationUtil { && 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) { boolean isGeoJsonExists = false; 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) { if (eventConfig == null ||eventConfig.isEmpty()) { String msg = "Event configuration is mandatory, since should not be null or empty"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java index b4e6ad8be29..bd557dfd7f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java @@ -476,7 +476,7 @@ public class GeofenceDAOImpl implements GeofenceDAO { "AND G.FENCE_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, geofenceId); - return getEventConfigs(eventList, stmt); + return getEventConfigs(stmt); } } catch (SQLException e) { String msg = "Error occurred while updating Geofence record with id "; @@ -521,7 +521,14 @@ public class GeofenceDAOImpl implements GeofenceDAO { } } - private List getEventConfigs(List 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 getEventConfigs(PreparedStatement stmt) throws SQLException { + List eventList = new ArrayList<>(); ResultSet resultSet = stmt.executeQuery(); EventConfig event; while (resultSet.next()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java index e72d733ec3c..c27fc72b81f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java @@ -131,7 +131,6 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration eventConfigDAO.updateEventRecords(eventsToUpdate); } - if (!groupIdsToDelete.isEmpty()) { if (log.isDebugEnabled()) { log.debug("Deleting event group mapping records of groups"); @@ -160,6 +159,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { String msg = "Failed to start/open transaction to store device event configurations"; + log.error(msg, e); throw new EventConfigurationException(msg, e); } catch (EventManagementDAOException e) { String msg = "Error occurred while saving event records"; @@ -227,6 +227,7 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { String msg = "Failed to start/open transaction to delete device event configurations"; + log.error(msg, e); throw new EventConfigurationException(msg, e); } catch (EventManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 9dca01c56f4..2f8d1872aae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -1330,8 +1330,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic * @param tenantId id of the fence owning tenant */ private void createEventRevokeTask(GeofenceData geofenceData, int tenantId) { - GeoFenceEventOperationManager eventManager = new GeoFenceEventOperationManager(OperationMgtConstants.OperationCodes.EVENT_REVOKE, - tenantId, values -> createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, geofenceData, tenantId)); + GeoFenceEventOperationManager eventManager = + new GeoFenceEventOperationManager(OperationMgtConstants.OperationCodes.EVENT_REVOKE, tenantId, + values -> createEventTask(OperationMgtConstants.OperationCodes.EVENT_CONFIG, geofenceData, tenantId)); ScheduledExecutorService eventOperationExecutor = Executors.newSingleThreadScheduledExecutor(); eventOperationExecutor.schedule(eventManager .getEventOperationExecutor(geofenceData), 10, TimeUnit.SECONDS); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 25cca94229b..95d68727e78 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -607,6 +607,10 @@ public final class DeviceManagerUtil { return eventsPublisherService; } + /** + * Retrieve EventConfigurationProviderService osgi service component + * @return {@link EventConfigurationProviderService} service component + */ public static EventConfigurationProviderService getEventConfigService() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); 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() { DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); int geoCacheExpiry = config.getGeoFenceCacheConfiguration().getExpiryTime(); @@ -713,6 +720,10 @@ public final class DeviceManagerUtil { return deviceCache; } + /** + * Get geofence cache object + * @return {@link Cache} + */ public static Cache getGeoCache() { DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); CacheManager manager = getCacheManager(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 9187676f008..e2f42210c4e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -1451,6 +1451,12 @@ public class PolicyManagerImpl implements PolicyManager { 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 getSingleCorrectiveAction (List allCorrectiveActions, int policyId) { List correctiveActionsOfPolicy = new ArrayList<>(); @@ -1464,6 +1470,13 @@ public class PolicyManagerImpl implements PolicyManager { 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 allCorrectiveActions, Profile profile) { for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index fe4cb939be0..95ae637c51b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -142,6 +142,12 @@ public class PolicyManagerUtil { 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 { List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); PolicyOperation policyOperation = new PolicyOperation();