|
|
@ -18,13 +18,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.core.event.config;
|
|
|
|
package org.wso2.carbon.device.mgt.core.event.config;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
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.event.config.EventConfigurationProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.common.event.config.EventOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.common.event.config.EventOperation;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.event.config.EventRevokeOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoFenceEventMeta;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoFenceEventMeta;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
|
|
|
@ -32,11 +35,13 @@ 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.geo.service.GeofenceData;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.task.EventCreateCallback;
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
@ -46,11 +51,18 @@ public class DeviceEventOperationExecutor implements Runnable {
|
|
|
|
private final int groupId;
|
|
|
|
private final int groupId;
|
|
|
|
private final List<DeviceIdentifier> deviceIdentifiers;
|
|
|
|
private final List<DeviceIdentifier> deviceIdentifiers;
|
|
|
|
private final int tenantId;
|
|
|
|
private final int tenantId;
|
|
|
|
|
|
|
|
private final String operationCode;
|
|
|
|
|
|
|
|
private final GeoLocationProviderService geoLocationProviderService;
|
|
|
|
|
|
|
|
private final EventConfigurationProviderService eventConfigurationService;
|
|
|
|
|
|
|
|
private EventCreateCallback callback;
|
|
|
|
|
|
|
|
|
|
|
|
public DeviceEventOperationExecutor(int groupId, List<DeviceIdentifier> deviceIdentifiers, int tenantId) {
|
|
|
|
public DeviceEventOperationExecutor(int groupId, List<DeviceIdentifier> deviceIdentifiers, int tenantId, String operationCode) {
|
|
|
|
this.groupId = groupId;
|
|
|
|
this.groupId = groupId;
|
|
|
|
this.deviceIdentifiers = deviceIdentifiers;
|
|
|
|
this.deviceIdentifiers = deviceIdentifiers;
|
|
|
|
this.tenantId = tenantId;
|
|
|
|
this.tenantId = tenantId;
|
|
|
|
|
|
|
|
this.operationCode = operationCode;
|
|
|
|
|
|
|
|
this.geoLocationProviderService = DeviceManagementDataHolder.getInstance().getGeoLocationProviderService();
|
|
|
|
|
|
|
|
this.eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -58,66 +70,113 @@ public class DeviceEventOperationExecutor implements Runnable {
|
|
|
|
log.info("Starting event operation creation task for devices in group " + groupId + " tenant " + tenantId);
|
|
|
|
log.info("Starting event operation creation task for devices in group " + groupId + " tenant " + tenantId);
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Event creation operation started for devices with IDs " + Arrays.toString(deviceIdentifiers.toArray()));
|
|
|
|
log.debug("Event creation operation started for devices with IDs " + Arrays.toString(deviceIdentifiers.toArray()));
|
|
|
|
|
|
|
|
log.debug("Starting tenant flow for tenant with ID : " + tenantId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProfileOperation operation = new ProfileOperation();
|
|
|
|
ProfileOperation operation = new ProfileOperation();
|
|
|
|
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG);
|
|
|
|
if (operationCode != null) {
|
|
|
|
operation.setType(Operation.Type.PROFILE);
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
EventConfigurationProviderService eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId);
|
|
|
|
operation.setCode(operationCode);
|
|
|
|
if (eventSources == null || eventSources.isEmpty()) {
|
|
|
|
operation.setType(Operation.Type.PROFILE);
|
|
|
|
log.info("No events applied for queried group with ID " + groupId);
|
|
|
|
if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) {
|
|
|
|
}
|
|
|
|
buildEventConfigOperationObject(operation);
|
|
|
|
for (String eventSource : eventSources) {
|
|
|
|
} else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)) {
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
buildEventRevokeOperation(operation);
|
|
|
|
setGeoFenceOperationContent(operation);
|
|
|
|
}
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
|
|
|
|
|
|
|
|
} catch (EventConfigurationException e) {
|
|
|
|
|
|
|
|
log.error("Failed to retrieve event sources of group " + groupId + ". Event creation operation failed.", e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
} catch (GeoLocationBasedServiceException e) {
|
|
|
|
|
|
|
|
log.error("Failed to retrieve geo fences for group " + groupId + ". Event creation operation failed.", e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (EventConfigurationException e) {
|
|
|
|
|
|
|
|
log.error("Failed to retrieve event sources of group " + groupId, e);
|
|
|
|
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
|
|
|
}
|
|
|
|
.getInstance().getDeviceManagementProvider();
|
|
|
|
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
|
|
|
try {
|
|
|
|
.getInstance().getDeviceManagementProvider();
|
|
|
|
if (!deviceIdentifiers.isEmpty()) {
|
|
|
|
try {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (!deviceIdentifiers.isEmpty()) {
|
|
|
|
log.debug("Creating event operations stared");
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
}
|
|
|
|
log.debug("Creating event operations stared");
|
|
|
|
deviceManagementProvider.addOperation("android", operation, deviceIdentifiers); //TODO introduce a proper mechanism
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.info("Device identifiers are empty, Hence ignoring adding event operation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deviceManagementProvider.addOperation("android", operation, deviceIdentifiers); //TODO introduce a proper mechanism
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
} else {
|
|
|
|
log.error("Creating event operation failed with error ", e);
|
|
|
|
log.info("Device identifiers are empty, Hence ignoring adding event operation");
|
|
|
|
} catch (InvalidDeviceException e) {
|
|
|
|
|
|
|
|
log.error("Creating event operation failed.\n" +
|
|
|
|
|
|
|
|
"Could not found device/devices for the defined device identifiers.", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("Event operation creation succeeded");
|
|
|
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
|
|
|
callback.onCompleteEventOperation(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
|
|
|
|
log.error("Creating event operation failed with error ", e);
|
|
|
|
|
|
|
|
} catch (InvalidDeviceException e) {
|
|
|
|
|
|
|
|
log.error("Creating event operation failed.\n" +
|
|
|
|
|
|
|
|
"Could not found device/devices for the defined device identifiers.", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Event operation creation succeeded");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void setGeoFenceOperationContent(ProfileOperation operation) {
|
|
|
|
private void buildEventRevokeOperation(ProfileOperation operation) throws GeoLocationBasedServiceException, EventConfigurationException {
|
|
|
|
|
|
|
|
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId);
|
|
|
|
|
|
|
|
if (eventSources == null || eventSources.isEmpty()) {
|
|
|
|
|
|
|
|
String msg = "No events applied for queried group with ID " + groupId;
|
|
|
|
|
|
|
|
log.info(msg);
|
|
|
|
|
|
|
|
throw new EventConfigurationException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String eventSource : eventSources) {
|
|
|
|
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
|
|
|
|
setGeoFenceRevokeOperationContent(operation);
|
|
|
|
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void buildEventConfigOperationObject(ProfileOperation operation) throws EventConfigurationException, GeoLocationBasedServiceException {
|
|
|
|
|
|
|
|
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId);
|
|
|
|
|
|
|
|
if (eventSources == null || eventSources.isEmpty()) {
|
|
|
|
|
|
|
|
String msg = "No events applied for queried group with ID " + groupId;
|
|
|
|
|
|
|
|
log.info(msg);
|
|
|
|
|
|
|
|
throw new EventConfigurationException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String eventSource : eventSources) {
|
|
|
|
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
|
|
|
|
setGeoFenceConfigOperationContent(operation);
|
|
|
|
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setGeoFenceConfigOperationContent(ProfileOperation operation) throws GeoLocationBasedServiceException {
|
|
|
|
log.info("Geo fence events found attached with group " + groupId + ", Started retrieving geo fences");
|
|
|
|
log.info("Geo fence events found attached with group " + groupId + ", Started retrieving geo fences");
|
|
|
|
GeoLocationProviderService geoLocationProviderService = DeviceManagementDataHolder.getInstance().getGeoLocationProviderService();
|
|
|
|
List<GeofenceData> geoFencesOfGroup = geoLocationProviderService.getGeoFencesOfGroup(groupId, tenantId, true);
|
|
|
|
try {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
List<GeofenceData> geoFencesOfGroup = geoLocationProviderService.getGeoFencesOfGroup(groupId, tenantId, true);
|
|
|
|
log.debug("Retrieved " + geoFencesOfGroup.size() + " geo fences defined for the group " + groupId);
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Retrieved " + geoFencesOfGroup.size() + " geo fences defined for the group " + groupId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (GeofenceData geofenceData : geoFencesOfGroup) {
|
|
|
|
|
|
|
|
GeoFenceEventMeta geoFenceEventMeta = new GeoFenceEventMeta(geofenceData);
|
|
|
|
|
|
|
|
EventOperation eventOperation = new EventOperation();
|
|
|
|
|
|
|
|
eventOperation.setEventDefinition(geoFenceEventMeta);
|
|
|
|
|
|
|
|
eventOperation.setEventSource(DeviceManagementConstants.EventServices.GEOFENCE);
|
|
|
|
|
|
|
|
eventOperation.setEventTriggers(geofenceData.getEventConfig());
|
|
|
|
|
|
|
|
if (operation.getPayLoad() != null) {
|
|
|
|
|
|
|
|
operation.setPayLoad(operation.getPayLoad().toString().concat(eventOperation.toJSON()));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
operation.setPayLoad(eventOperation.toJSON());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (GeoLocationBasedServiceException e) {
|
|
|
|
|
|
|
|
log.error("Failed to retrieve geo fences for group " + groupId);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<EventOperation> eventOperationList = new ArrayList<>();
|
|
|
|
|
|
|
|
for (GeofenceData geofenceData : geoFencesOfGroup) {
|
|
|
|
|
|
|
|
GeoFenceEventMeta geoFenceEventMeta = new GeoFenceEventMeta(geofenceData);
|
|
|
|
|
|
|
|
EventOperation eventOperation = new EventOperation();
|
|
|
|
|
|
|
|
eventOperation.setEventDefinition(geoFenceEventMeta);
|
|
|
|
|
|
|
|
eventOperation.setEventSource(DeviceManagementConstants.EventServices.GEOFENCE);
|
|
|
|
|
|
|
|
eventOperation.setEventTriggers(geofenceData.getEventConfig());
|
|
|
|
|
|
|
|
eventOperationList.add(eventOperation);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operation.setPayLoad(new Gson().toJson(eventOperationList));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setGeoFenceRevokeOperationContent(ProfileOperation operation) throws GeoLocationBasedServiceException {
|
|
|
|
|
|
|
|
List<GeofenceData> geoFencesOfGroup = geoLocationProviderService.getGeoFencesOfGroup(groupId, tenantId, true);
|
|
|
|
|
|
|
|
List<EventRevokeOperation> revokeOperationList = new ArrayList<>();
|
|
|
|
|
|
|
|
for (GeofenceData geofenceData : geoFencesOfGroup) {
|
|
|
|
|
|
|
|
EventRevokeOperation eventRevokeOperation = new EventRevokeOperation();
|
|
|
|
|
|
|
|
eventRevokeOperation.setEventSource(DeviceManagementConstants.EventServices.GEOFENCE);
|
|
|
|
|
|
|
|
eventRevokeOperation.setId(geofenceData.getId());
|
|
|
|
|
|
|
|
revokeOperationList.add(eventRevokeOperation);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operation.setPayLoad(new Gson().toJson(revokeOperationList));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setCallback(EventCreateCallback callback) {
|
|
|
|
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|