|
|
@ -26,8 +26,10 @@ import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
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.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.EventMetaData;
|
|
|
|
import org.wso2.carbon.device.mgt.common.event.config.EventMetaData;
|
|
|
|
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;
|
|
|
@ -36,6 +38,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
|
|
|
|
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;
|
|
|
@ -55,24 +58,47 @@ public class GroupEventOperationExecutor implements Runnable {
|
|
|
|
private final String eventSource;
|
|
|
|
private final String eventSource;
|
|
|
|
private final EventMetaData eventMetaData;
|
|
|
|
private final EventMetaData eventMetaData;
|
|
|
|
private final int tenantId;
|
|
|
|
private final int tenantId;
|
|
|
|
|
|
|
|
private final String operationCode;
|
|
|
|
|
|
|
|
private final GroupManagementProviderService groupManagementService;
|
|
|
|
|
|
|
|
private EventCreateCallback callback;
|
|
|
|
|
|
|
|
|
|
|
|
public GroupEventOperationExecutor(EventMetaData eventMetaData, List<Integer> groupIds, int tenantId, String eventSource) {
|
|
|
|
public GroupEventOperationExecutor(EventMetaData eventMetaData, List<Integer> groupIds, int tenantId,
|
|
|
|
|
|
|
|
String eventSource, String operationCode) {
|
|
|
|
this.eventMetaData = eventMetaData;
|
|
|
|
this.eventMetaData = eventMetaData;
|
|
|
|
this.groupIds = groupIds;
|
|
|
|
this.groupIds = groupIds;
|
|
|
|
this.tenantId = tenantId;
|
|
|
|
this.tenantId = tenantId;
|
|
|
|
this.eventSource = eventSource;
|
|
|
|
this.eventSource = eventSource;
|
|
|
|
|
|
|
|
this.operationCode = operationCode;
|
|
|
|
|
|
|
|
this.groupManagementService = DeviceManagementDataHolder.getInstance().getGroupManagementProviderService();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
log.info("Starting event operation creation task for event " + eventSource + " tenant " + tenantId);
|
|
|
|
log.info("Starting event operation creation task");
|
|
|
|
|
|
|
|
if (operationCode == null) {
|
|
|
|
|
|
|
|
log.error("Failed to start event operation task. Operation code is null");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Event creation operation started for groups with IDs " + Arrays.toString(groupIds.toArray()));
|
|
|
|
log.info("Starting " + operationCode + " operation creation task for event " + eventSource
|
|
|
|
|
|
|
|
+ " tenant " + tenantId + " group Ids "+ Arrays.toString(groupIds.toArray()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
|
|
|
GroupManagementProviderService groupManagementService = DeviceManagementDataHolder
|
|
|
|
ProfileOperation operation = new ProfileOperation();
|
|
|
|
.getInstance().getGroupManagementProviderService();
|
|
|
|
operation.setType(Operation.Type.PROFILE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) {
|
|
|
|
|
|
|
|
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG);
|
|
|
|
|
|
|
|
buildEventConfigOperation(operation);
|
|
|
|
|
|
|
|
} else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)){
|
|
|
|
|
|
|
|
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_REVOKE);
|
|
|
|
|
|
|
|
buildEventRevokeOperation(operation);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (EventConfigurationException e) {
|
|
|
|
|
|
|
|
log.error("Event creation failed with message : " + e.getMessage(), e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
Set<Device> devices = new HashSet<>();
|
|
|
|
Set<Device> devices = new HashSet<>();
|
|
|
|
for (Integer groupId : groupIds) {
|
|
|
|
for (Integer groupId : groupIds) {
|
|
|
|
DeviceGroup group;
|
|
|
|
DeviceGroup group;
|
|
|
@ -86,7 +112,7 @@ public class GroupEventOperationExecutor implements Runnable {
|
|
|
|
if (group != null) {
|
|
|
|
if (group != null) {
|
|
|
|
List<Device> allDevicesOfGroup = groupManagementService.getAllDevicesOfGroup(group.getName(), false);
|
|
|
|
List<Device> allDevicesOfGroup = groupManagementService.getAllDevicesOfGroup(group.getName(), false);
|
|
|
|
if (allDevicesOfGroup == null || allDevicesOfGroup.isEmpty()) {
|
|
|
|
if (allDevicesOfGroup == null || allDevicesOfGroup.isEmpty()) {
|
|
|
|
log.info("No devices found in group " + group.getName());
|
|
|
|
log.warn("No devices found in group " + group.getName());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
devices.addAll(allDevicesOfGroup);
|
|
|
|
devices.addAll(allDevicesOfGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -95,44 +121,61 @@ public class GroupEventOperationExecutor implements Runnable {
|
|
|
|
log.error("Failed to retrieve devices of group with ID " + groupId + " and name " + group.getName(), e);
|
|
|
|
log.error("Failed to retrieve devices of group with ID " + groupId + " and name " + group.getName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProfileOperation operation = new ProfileOperation();
|
|
|
|
|
|
|
|
operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG);
|
|
|
|
|
|
|
|
operation.setType(Operation.Type.PROFILE);
|
|
|
|
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
|
|
|
|
createGeoFenceOperation(operation);
|
|
|
|
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (devices.isEmpty()) {
|
|
|
|
log.debug("Starting tenant flow for tenant id " + tenantId);
|
|
|
|
log.warn("No devices found for the specified groups " + Arrays.toString(groupIds.toArray()));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
|
|
|
for (Device device : devices) {
|
|
|
|
for (Device device : devices) {
|
|
|
|
if (device.getType().equalsIgnoreCase("android")) { //TODO introduce a proper mechanism for event handling for each device types
|
|
|
|
if (device.getType().equalsIgnoreCase("android")) {
|
|
|
|
|
|
|
|
//TODO introduce a proper mechanism for event handling for each device types
|
|
|
|
deviceIdentifiers.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
|
|
|
|
deviceIdentifiers.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
|
|
|
DeviceManagementProviderService deviceManagementProvider = DeviceManagementDataHolder
|
|
|
|
.getInstance().getDeviceManagementProvider();
|
|
|
|
.getInstance().getDeviceManagementProvider();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!deviceIdentifiers.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Creating event operations stared for devices" + Arrays.toString(deviceIdentifiers.toArray()));
|
|
|
|
log.debug("Creating event operations stared for devices" + Arrays.toString(deviceIdentifiers.toArray()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deviceManagementProvider.addOperation("android", operation, deviceIdentifiers); //TODO introduce a proper mechanism
|
|
|
|
deviceManagementProvider.addOperation("android", operation, deviceIdentifiers);
|
|
|
|
} else {
|
|
|
|
//TODO introduce a proper mechanism
|
|
|
|
log.info("Device identifiers are empty, Hence ignoring adding event operation");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
log.error("Creating event operation failed with error ", e);
|
|
|
|
log.error("Creating event operation failed with error ", e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (InvalidDeviceException e) {
|
|
|
|
} catch (InvalidDeviceException e) {
|
|
|
|
log.error("Creating event operation failed.\n" +
|
|
|
|
log.error("Creating event operation failed.\n" +
|
|
|
|
"Could not found device/devices for the defined device identifiers.", e);
|
|
|
|
"Could not found device/devices for the defined device identifiers.", e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Event operation creation task completed");
|
|
|
|
log.info("Event operation creation task completed");
|
|
|
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
|
|
|
callback.onCompleteEventOperation(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void buildEventRevokeOperation(ProfileOperation operation) {
|
|
|
|
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
|
|
|
|
createGeoFenceRevokeOperation(operation);
|
|
|
|
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void createGeoFenceOperation(ProfileOperation operation) {
|
|
|
|
private void createGeoFenceRevokeOperation(ProfileOperation operation) {
|
|
|
|
|
|
|
|
GeoFenceEventMeta geoFenceMeta = (GeoFenceEventMeta) eventMetaData;
|
|
|
|
|
|
|
|
EventRevokeOperation eventRevokeOperation = new EventRevokeOperation();
|
|
|
|
|
|
|
|
eventRevokeOperation.setEventSource(eventSource);
|
|
|
|
|
|
|
|
eventRevokeOperation.setId(geoFenceMeta.getId());
|
|
|
|
|
|
|
|
operation.setPayLoad(new Gson().toJson(eventRevokeOperation));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void buildEventConfigOperation(ProfileOperation operation) throws EventConfigurationException {
|
|
|
|
|
|
|
|
if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) {
|
|
|
|
|
|
|
|
createGeoFenceConfigOperation(operation);
|
|
|
|
|
|
|
|
} //extend with another cases to handle other types of events
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void createGeoFenceConfigOperation(ProfileOperation operation) throws EventConfigurationException {
|
|
|
|
GeoFenceEventMeta geoFenceMeta = (GeoFenceEventMeta) eventMetaData;
|
|
|
|
GeoFenceEventMeta geoFenceMeta = (GeoFenceEventMeta) eventMetaData;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
GeoLocationProviderService geoLocationProviderService = DeviceManagementDataHolder
|
|
|
|
GeoLocationProviderService geoLocationProviderService = DeviceManagementDataHolder
|
|
|
@ -149,9 +192,13 @@ public class GroupEventOperationExecutor implements Runnable {
|
|
|
|
eventOperation.setEventTriggers(eventConfigList);
|
|
|
|
eventOperation.setEventTriggers(eventConfigList);
|
|
|
|
eventOperations.add(eventOperation);
|
|
|
|
eventOperations.add(eventOperation);
|
|
|
|
operation.setPayLoad(new Gson().toJson(eventOperations));
|
|
|
|
operation.setPayLoad(new Gson().toJson(eventOperations));
|
|
|
|
} catch (GeoLocationBasedServiceException e) {
|
|
|
|
}catch (GeoLocationBasedServiceException e) {
|
|
|
|
log.error("Failed to retrieve event data of Geo fence " + geoFenceMeta.getId()
|
|
|
|
throw new EventConfigurationException("Failed to retrieve event data of Geo fence " + geoFenceMeta.getId()
|
|
|
|
+ " : " + geoFenceMeta.getFenceName(), e);
|
|
|
|
+ " : " + geoFenceMeta.getFenceName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setCallback(EventCreateCallback callback) {
|
|
|
|
|
|
|
|
this.callback = callback;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|