|
|
|
@ -18,13 +18,16 @@
|
|
|
|
|
|
|
|
|
|
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.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
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.EventConfigurationProviderService;
|
|
|
|
|
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.geo.service.GeoFenceEventMeta;
|
|
|
|
|
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.operation.mgt.Operation;
|
|
|
|
|
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.operation.mgt.OperationMgtConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
@ -46,11 +51,18 @@ public class DeviceEventOperationExecutor implements Runnable {
|
|
|
|
|
private final int groupId;
|
|
|
|
|
private final List<DeviceIdentifier> deviceIdentifiers;
|
|
|
|
|
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.deviceIdentifiers = deviceIdentifiers;
|
|
|
|
|
this.tenantId = tenantId;
|
|
|
|
|
this.operationCode = operationCode;
|
|
|
|
|
this.geoLocationProviderService = DeviceManagementDataHolder.getInstance().getGeoLocationProviderService();
|
|
|
|
|
this.eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -58,66 +70,113 @@ public class DeviceEventOperationExecutor implements Runnable {
|
|
|
|
|
log.info("Starting event operation creation task for devices in group " + groupId + " tenant " + tenantId);
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
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();
|
|
|
|
|
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG);
|
|
|
|
|
operation.setType(Operation.Type.PROFILE);
|
|
|
|
|
EventConfigurationProviderService eventConfigurationService = DeviceManagementDataHolder.getInstance().getEventConfigurationService();
|
|
|
|
|
try {
|
|
|
|
|
List<String> eventSources = eventConfigurationService.getEventsSourcesOfGroup(groupId, tenantId);
|
|
|
|
|
if (eventSources == null || eventSources.isEmpty()) {
|
|
|
|
|
log.info("No events applied for queried group with ID " + groupId);
|
|
|
|
|
}
|
|
|
|
|
for (String eventSource : eventSources) {
|
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
|
setGeoFenceOperationContent(operation);
|
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
|
if (operationCode != null) {
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
|
|
|
|
try {
|
|
|
|
|
operation.setCode(operationCode);
|
|
|
|
|
operation.setType(Operation.Type.PROFILE);
|
|
|
|
|
if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) {
|
|
|
|
|
buildEventConfigOperationObject(operation);
|
|
|
|
|
} else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)) {
|
|
|
|
|
buildEventRevokeOperation(operation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} 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();
|
|
|
|
|
try {
|
|
|
|
|
if (!deviceIdentifiers.isEmpty()) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Creating event operations stared");
|
|
|
|
|
|
|
|
|
|
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
|
|
|
|
.getInstance().getDeviceManagementProvider();
|
|
|
|
|
try {
|
|
|
|
|
if (!deviceIdentifiers.isEmpty()) {
|
|
|
|
|
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
|
|
|
|
|
} else {
|
|
|
|
|
log.info("Device identifiers are empty, Hence ignoring adding event operation");
|
|
|
|
|
} 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");
|
|
|
|
|
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");
|
|
|
|
|
GeoLocationProviderService geoLocationProviderService = DeviceManagementDataHolder.getInstance().getGeoLocationProviderService();
|
|
|
|
|
try {
|
|
|
|
|
List<GeofenceData> geoFencesOfGroup = geoLocationProviderService.getGeoFencesOfGroup(groupId, tenantId, true);
|
|
|
|
|
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<GeofenceData> geoFencesOfGroup = geoLocationProviderService.getGeoFencesOfGroup(groupId, tenantId, true);
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Retrieved " + geoFencesOfGroup.size() + " geo fences defined for the 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|