|
|
@ -24,15 +24,15 @@ import org.wso2.carbon.context.CarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
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.exceptions.DeviceManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
|
|
|
|
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.exceptions.TransactionManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
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.common.operation.mgt.Activity;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
|
|
|
@ -227,67 +227,21 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
+ operationCode);
|
|
|
|
+ operationCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Activity activity = new Activity();
|
|
|
|
Activity activity = new Activity();
|
|
|
|
//Send the operation statuses only for admin triggered operations
|
|
|
|
|
|
|
|
String deviceType = validDeviceIds.get(0).getType();
|
|
|
|
|
|
|
|
activity.setActivityStatus(this.getActivityStatus(deviceValidationResult,
|
|
|
|
activity.setActivityStatus(this.getActivityStatus(deviceValidationResult,
|
|
|
|
deviceAuthorizationResult));
|
|
|
|
deviceAuthorizationResult));
|
|
|
|
return activity;
|
|
|
|
return activity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
persistsOperation(operation, operationDto, enrolments);
|
|
|
|
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
|
|
|
|
|
|
|
|
operationDto.setId(operationId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean isScheduled = false;
|
|
|
|
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check whether device list is greater than batch size notification strategy has enable to send push
|
|
|
|
|
|
|
|
// notification using scheduler task
|
|
|
|
|
|
|
|
if (DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
|
|
|
|
|
|
|
|
getPushNotificationConfiguration().getSchedulerBatchSize() <= authorizedDeviceIds.size() &&
|
|
|
|
|
|
|
|
notificationStrategy != null) {
|
|
|
|
|
|
|
|
isScheduled = notificationStrategy.getConfig().isScheduled();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int failAttempts = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationDto,
|
|
|
|
|
|
|
|
new ArrayList<>(enrolments.values()), isScheduled, tenantId);
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
if (++failAttempts > 3) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while updating operation mapping. Operation ID: " +
|
|
|
|
|
|
|
|
operationId;
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new OperationManagementException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn("Unable to update operation status. Operation ID: " + operationId +
|
|
|
|
|
|
|
|
", Attempt: " + failAttempts + ", Error: " + e.getMessage());
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Thread.sleep(2000);
|
|
|
|
|
|
|
|
} catch (InterruptedException ignore) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isScheduled && notificationStrategy != null) {
|
|
|
|
|
|
|
|
for (Device d : enrolments.values()) {
|
|
|
|
|
|
|
|
this.sendNotification(operation, d);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Activity activity = new Activity();
|
|
|
|
Activity activity = new Activity();
|
|
|
|
activity.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId);
|
|
|
|
activity.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operation.getId());
|
|
|
|
activity.setCode(operationCode);
|
|
|
|
activity.setCode(operationCode);
|
|
|
|
activity.setCreatedTimeStamp(new Date().toString());
|
|
|
|
activity.setCreatedTimeStamp(new Date().toString());
|
|
|
|
activity.setType(Activity.Type.valueOf(operationDto.getType().toString()));
|
|
|
|
activity.setType(Activity.Type.valueOf(operationDto.getType().toString()));
|
|
|
|
//For now set the operation statuses only for admin triggered operations
|
|
|
|
//For now set the operation statuses only for admin triggered operations
|
|
|
|
if (!isScheduledOperation) {
|
|
|
|
if (!isScheduledOperation) {
|
|
|
|
//Get the device-type from 1st valid DeviceIdentifier. We know the 1st element is definitely there.
|
|
|
|
|
|
|
|
String deviceType = validDeviceIds.get(0).getType();
|
|
|
|
|
|
|
|
activity.setActivityStatus(this.getActivityStatus(deviceValidationResult,
|
|
|
|
activity.setActivityStatus(this.getActivityStatus(deviceValidationResult,
|
|
|
|
deviceAuthorizationResult));
|
|
|
|
deviceAuthorizationResult));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -305,6 +259,46 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void addTaskOperation(List<Device> devices, Operation operation) throws OperationManagementException {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
operation.setInitiatedBy(SYSTEM);
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto =
|
|
|
|
|
|
|
|
OperationDAOUtil.convertOperation(operation);
|
|
|
|
|
|
|
|
String operationCode = operationDto.getCode();
|
|
|
|
|
|
|
|
Map<Integer, Device> enrolments = new HashMap<>();
|
|
|
|
|
|
|
|
for (Device device : devices) {
|
|
|
|
|
|
|
|
enrolments.put(device.getEnrolmentInfo().getId(), device);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operationDto.getControl() ==
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) {
|
|
|
|
|
|
|
|
Map<Integer, Integer> pendingOperationIDs = operationDAO
|
|
|
|
|
|
|
|
.getExistingOperationIDs(enrolments.keySet().toArray(new Integer[0]), operationCode);
|
|
|
|
|
|
|
|
Device device;
|
|
|
|
|
|
|
|
for (Integer enrolmentId : pendingOperationIDs.keySet()) {
|
|
|
|
|
|
|
|
operation.setId(pendingOperationIDs.get(enrolmentId));
|
|
|
|
|
|
|
|
device = enrolments.get(enrolmentId);
|
|
|
|
|
|
|
|
this.sendNotification(operation, device);
|
|
|
|
|
|
|
|
//No need to keep this enrollment as it has a pending operation
|
|
|
|
|
|
|
|
enrolments.remove(enrolmentId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enrolments.size() == 0) {
|
|
|
|
|
|
|
|
//No operations to be add. All are repeated.
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
persistsOperation(operation, operationDto, enrolments);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while adding task operation", e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while initiating the transaction", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void addTaskOperation(String deviceType, Operation operation) throws OperationManagementException {
|
|
|
|
public void addTaskOperation(String deviceType, Operation operation) throws OperationManagementException {
|
|
|
|
List<String> validStatuses = Arrays.asList(EnrolmentInfo.Status.ACTIVE.toString(),
|
|
|
|
List<String> validStatuses = Arrays.asList(EnrolmentInfo.Status.ACTIVE.toString(),
|
|
|
@ -356,9 +350,35 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
persistsOperation(operation, operationDto, enrolments);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Thread.sleep(2000);
|
|
|
|
|
|
|
|
} catch (InterruptedException ignore) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (hasRecords);
|
|
|
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while getting devices to add operations", e);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while adding operation", e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while initiating the transaction", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
DeviceManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void persistsOperation(Operation operation,
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto,
|
|
|
|
|
|
|
|
Map<Integer, Device> enrolments)
|
|
|
|
|
|
|
|
throws OperationManagementDAOException, OperationManagementException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
|
|
|
|
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
|
|
|
|
operationDto.setId(operationId);
|
|
|
|
operationDto.setId(operationId);
|
|
|
|
|
|
|
|
operation.setId(operationId);
|
|
|
|
|
|
|
|
|
|
|
|
boolean isScheduled = false;
|
|
|
|
boolean isScheduled = false;
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
@ -399,23 +419,6 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
this.sendNotification(operation, device);
|
|
|
|
this.sendNotification(operation, device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
|
|
|
|
Thread.sleep(2000);
|
|
|
|
|
|
|
|
} catch (InterruptedException ignore) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (hasRecords);
|
|
|
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while getting devices to add operations", e);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while adding operation", e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while initiating the transaction", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
DeviceManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void sendNotification(Operation operation, Device device) {
|
|
|
|
private void sendNotification(Operation operation, Device device) {
|
|
|
|