Merge pull request #987 from sinthuja/origin-wso2-master

Adding more unit testcases for operation management.
revert-70aa11f8
Megala Uthayakumar 7 years ago committed by GitHub
commit f94c7f1916

@ -63,7 +63,8 @@ public interface OperationManager {
* @throws OperationManagementException If some unusual behaviour is observed while fetching the
* operation list.
*/
PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementException;
PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request)
throws OperationManagementException;
/**
* Method to retrieve the list of available operations to a device.
@ -73,15 +74,12 @@ public interface OperationManager {
* @throws OperationManagementException If some unusual behaviour is observed while fetching the
* operation list.
*/
List<? extends Operation> getPendingOperations(
DeviceIdentifier deviceId) throws OperationManagementException;
List<? extends Operation> getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException;
Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException;
void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException;
void deleteOperation(int operationId) throws OperationManagementException;
Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException;
@ -93,9 +91,8 @@ public interface OperationManager {
Activity getOperationByActivityId(String activity) throws OperationManagementException;
Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException;
List<Operation> getOperationUpdatedAfter(long timestamp) throws OperationManagementException;
Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId)
throws OperationManagementException;
List<Activity> getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException;
@ -103,12 +100,6 @@ public interface OperationManager {
int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException;
/**
* Operation manger implementation can have a push notification stratergy
* @param notificationStrategy eg: mqtt/xmpp
*/
void setNotificationStrategy(NotificationStrategy notificationStrategy);
/**
* retrive the push notification strategy.
* @return NotificationStrategy

@ -1,22 +0,0 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.mgt.core.operation.mgt;
public interface OperationManagementServiceProvider {
}

@ -105,10 +105,6 @@ public class OperationManagerImpl implements OperationManager {
return notificationStrategy;
}
public void setNotificationStrategy(NotificationStrategy notificationStrategy) {
this.notificationStrategy = notificationStrategy;
}
public OperationManagerImpl(String deviceType, NotificationStrategy notificationStrategy) {
this(deviceType);
this.notificationStrategy = notificationStrategy;
@ -122,7 +118,7 @@ public class OperationManagerImpl implements OperationManager {
log.debug("operation:[" + operation.toString() + "]");
for (DeviceIdentifier deviceIdentifier : deviceIds) {
log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" +
deviceIdentifier.getType() + "]");
deviceIdentifier.getType() + "]");
}
}
try {
@ -137,7 +133,7 @@ public class OperationManagerImpl implements OperationManager {
//Send the operation statuses only for admin triggered operations
String deviceType = validDeviceIds.get(0).getType();
activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult,
deviceType));
deviceType));
return activity;
}
@ -160,7 +156,7 @@ public class OperationManagerImpl implements OperationManager {
boolean hasExistingTaskOperation;
int enrolmentId;
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto.
getControl()) {
getControl()) {
isNotRepeated = true;
}
@ -177,10 +173,10 @@ public class OperationManagerImpl implements OperationManager {
}
} else if (isNotRepeated) {
operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.PENDING,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.REPEATED);
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.PENDING,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.REPEATED);
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
} else {
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
@ -200,8 +196,8 @@ public class OperationManagerImpl implements OperationManager {
operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.COMPLETED);
} catch (PushNotificationExecutionFailedException e) {
log.error("Error occurred while sending push notifications to " +
deviceId.getType() + " device carrying id '" +
deviceId + "'", e);
deviceId.getType() + " device carrying id '" +
deviceId + "'", e);
// Reschedule if push notification failed.
operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED);
}
@ -219,7 +215,7 @@ public class OperationManagerImpl implements OperationManager {
//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, deviceAuthorizationResult,
deviceType));
deviceType));
}
return activity;
} else {
@ -242,7 +238,7 @@ public class OperationManagerImpl implements OperationManager {
//Add the invalid DeviceIds
for (String id : deviceIdValidationResult.getErrorDeviceIdList()) {
activityStatus = new ActivityStatus();
activityStatus.setDeviceIdentifier(new DeviceIdentifier(id,deviceType));
activityStatus.setDeviceIdentifier(new DeviceIdentifier(id, deviceType));
activityStatus.setStatus(ActivityStatus.Status.INVALID);
activityStatuses.add(activityStatus);
}
@ -288,7 +284,7 @@ public class OperationManagerImpl implements OperationManager {
}
} catch (DeviceAccessAuthorizationException e) {
throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" +
this.getUser(), e);
this.getUser(), e);
}
deviceIDHolder.setValidDeviceIDList(authorizedDeviceList);
deviceIDHolder.setErrorDeviceIdList(unAuthorizedDeviceList);
@ -310,8 +306,8 @@ public class OperationManagerImpl implements OperationManager {
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
}
EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId);
@ -331,8 +327,8 @@ public class OperationManagerImpl implements OperationManager {
}
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
@ -350,15 +346,15 @@ public class OperationManagerImpl implements OperationManager {
String owner = request.getOwner();
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'" );
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'");
}
EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId, owner);
if (enrolmentInfo == null) {
throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" +
deviceId.getType());
"Identifier:" + deviceId.getId() + " and given type" +
deviceId.getType());
}
int enrolmentId = enrolmentInfo.getId();
try {
@ -376,8 +372,8 @@ public class OperationManagerImpl implements OperationManager {
paginationResult.setRecordsFiltered(count);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
@ -390,7 +386,7 @@ public class OperationManagerImpl implements OperationManager {
@Override
public List<? extends Operation> getPendingOperations(DeviceIdentifier deviceId) throws
OperationManagementException {
OperationManagementException {
if (log.isDebugEnabled()) {
log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() + "]");
}
@ -399,16 +395,16 @@ public class OperationManagerImpl implements OperationManager {
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
}
//
EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId);
if (enrolmentInfo == null) {
throw new OperationManagementException("Device not found for the given device Identifier:" +
deviceId.getId() + " and given type:" +
deviceId.getType());
deviceId.getId() + " and given type:" +
deviceId.getType());
}
int enrolmentId = enrolmentInfo.getId();
//Changing the enrollment status & attempt count if the device is marked as inactive or unreachable
@ -437,8 +433,8 @@ public class OperationManagerImpl implements OperationManager {
Collections.sort(operations, new OperationCreateTimeComparator());
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"pending operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
"pending operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
@ -457,15 +453,15 @@ public class OperationManagerImpl implements OperationManager {
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
}
EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId);
if (enrolmentInfo == null) {
throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" +
deviceId.getType());
"Identifier:" + deviceId.getId() + " and given type" +
deviceId.getType());
}
int enrolmentId = enrolmentInfo.getId();
//Changing the enrollment status & attempt count if the device is marked as inactive or unreachable
@ -479,23 +475,23 @@ public class OperationManagerImpl implements OperationManager {
try {
OperationManagementDAOFactory.openConnection();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getNextOperation(
enrolmentInfo.getId());
enrolmentInfo.getId());
if (dtoOperation != null) {
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND.equals(dtoOperation.getType()
)) {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation;
commandOperation =
(org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO.
getOperation(dtoOperation.getId());
getOperation(dtoOperation.getId());
dtoOperation.setEnabled(commandOperation.isEnabled());
} else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG.equals(dtoOperation.
getType())) {
getType())) {
dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
} else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE.equals(dtoOperation.
getType())) {
getType())) {
dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
} else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY.equals(dtoOperation.
getType())) {
getType())) {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
}
operation = OperationDAOUtil.convertOperation(dtoOperation);
@ -520,15 +516,15 @@ public class OperationManagerImpl implements OperationManager {
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
}
EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId);
if (enrolmentInfo == null) {
throw new OperationManagementException(
"Device not found for device id:" + deviceId.getId() + " " + "type:" +
deviceId.getType());
deviceId.getType());
}
try {
@ -537,9 +533,9 @@ public class OperationManagerImpl implements OperationManager {
boolean isUpdated = false;
if (operation.getStatus() != null) {
isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.valueOf(operation.getStatus().
toString()));
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.valueOf(operation.getStatus().
toString()));
}
if (operation.getOperationResponse() != null) {
operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse());
@ -549,28 +545,7 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException(
"Error occurred while updating the operation: " + operationId + " status:" +
operation.getStatus(), e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
}
@Override
public void deleteOperation(int operationId) throws OperationManagementException {
try {
OperationManagementDAOFactory.beginTransaction();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation =
operationDAO.getOperation(operationId);
if (operation == null) {
throw new OperationManagementException("Operation not found for operation id : " + operationId);
}
lookupOperationDAO(operation).deleteOperation(operationId);
OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException e) {
OperationManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException("Error occurred while deleting the operation: " + operationId, e);
operation.getStatus(), e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally {
@ -584,56 +559,57 @@ public class OperationManagerImpl implements OperationManager {
Operation operation = null;
if (log.isDebugEnabled()) {
log.debug("Operation Id: " + operationId + " Device Type: " + deviceId.getType() + " Device Identifier: " +
deviceId.getId());
deviceId.getId());
}
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
}
EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId);
if (enrolmentInfo == null) {
throw new OperationManagementException("Device not found for given device identifier: " +
deviceId.getId() + " type: " + deviceId.getType());
deviceId.getId() + " type: " + deviceId.getType());
}
try {
OperationManagementDAOFactory.openConnection();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.
getOperationByDeviceAndId(enrolmentInfo.getId(),
operationId);
if (dtoOperation.getType().
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation deviceSpecificOperation = operationDAO.
getOperationByDeviceAndId(enrolmentInfo.getId(),
operationId);
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = deviceSpecificOperation;
if (deviceSpecificOperation.getType().
equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation;
commandOperation =
(org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO.
getOperation(dtoOperation.getId());
getOperation(deviceSpecificOperation.getId());
dtoOperation.setEnabled(commandOperation.isEnabled());
} else if (dtoOperation.getType().
} else if (deviceSpecificOperation.getType().
equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(
dtoOperation = configOperationDAO.getOperation(deviceSpecificOperation.getId());
} else if (deviceSpecificOperation.getType().equals(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) {
dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(
dtoOperation = profileOperationDAO.getOperation(deviceSpecificOperation.getId());
} else if (deviceSpecificOperation.getType().equals(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
dtoOperation = policyOperationDAO.getOperation(deviceSpecificOperation.getId());
}
if (dtoOperation == null) {
throw new OperationManagementException("Operation not found for operation Id:" + operationId +
" device id:" + deviceId.getId());
" device id:" + deviceId.getId());
}
operation = OperationDAOUtil.convertOperation(dtoOperation);
dtoOperation.setStatus(deviceSpecificOperation.getStatus());
operation = OperationDAOUtil.convertOperation(deviceSpecificOperation);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
"operations assigned for '" + deviceId.getType() +
"' device '" + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening connection to the data source",
e);
e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
@ -649,15 +625,15 @@ public class OperationManagerImpl implements OperationManager {
if (!isActionAuthorized(deviceId)) {
throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" +
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
deviceId.getType() + "' device, which carries the identifier '" +
deviceId.getId() + "'");
}
EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId);
if (enrolmentInfo == null) {
throw new OperationManagementException(
"Device not found for device id:" + deviceId.getId() + " " + "type:" +
deviceId.getType());
deviceId.getType());
}
try {
@ -667,14 +643,14 @@ public class OperationManagerImpl implements OperationManager {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString());
dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus));
dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.
Status.PENDING));
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.
Status.PENDING));
dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.
Status.PENDING));
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.
Status.PENDING));
dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.
Status.PENDING));
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.
Status.PENDING));
Operation operation;
@ -685,9 +661,9 @@ public class OperationManagerImpl implements OperationManager {
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() +
"' device '" +
deviceId.getId() + "' and status:" + status.toString(), e);
"operations assigned for '" + deviceId.getType() +
"' device '" +
deviceId.getId() + "' and status:" + status.toString(), e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
@ -703,32 +679,32 @@ public class OperationManagerImpl implements OperationManager {
try {
OperationManagementDAOFactory.openConnection();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation(
operationId);
operationId);
if (dtoOperation == null) {
throw new OperationManagementException("Operation not found for given Id:" + operationId);
}
if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation;
commandOperation =
(org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO.
getOperation(dtoOperation.getId());
getOperation(dtoOperation.getId());
dtoOperation.setEnabled(commandOperation.isEnabled());
} else if (dtoOperation.getType().
equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.
PROFILE)) {
PROFILE)) {
dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.
POLICY)) {
POLICY)) {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
}
operation = OperationDAOUtil.convertOperation(dtoOperation);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the operation with operation Id '" +
operationId, e);
operationId, e);
} catch (SQLException e) {
throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
} finally {
@ -737,62 +713,6 @@ public class OperationManagerImpl implements OperationManager {
return operation;
}
// @Override
// public Operation getOperationByActivityId(String activity) throws OperationManagementException {
// // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer.
// Operation operation;
// int enrollmentOpMappingId = Integer.parseInt(
// activity.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, ""));
// if (enrollmentOpMappingId == 0) {
// throw new IllegalArgumentException("Operation ID cannot be null or zero (0).");
// }
// try {
// OperationManagementDAOFactory.openConnection();
// org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation =
// operationDAO.getOperationFromEnrollment(enrollmentOpMappingId);
//
// if (dtoOperation == null) {
// throw new OperationManagementException("Operation not found for given activity Id:" + activity);
// }
// org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status status = dtoOperation.getStatus();
// if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
// org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation;
// commandOperation =
// (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO.
// getOperation(dtoOperation.getId());
// dtoOperation.setEnabled(commandOperation.isEnabled());
// } else if (dtoOperation.getType().
// equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
// dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
// } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.
// PROFILE)) {
// dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
// } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.
// POLICY)) {
// dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
// }
// operation = OperationDAOUtil.convertOperation(dtoOperation);
// int enrolmentId = operationDAO.getEnrolmentIdFromMappingId(enrollmentOpMappingId);
// if (enrolmentId != 0) {
// operation.setResponses(operationDAO.getOperationResponses(enrolmentId, operation.getId()));
// }
//
// operation.setStatus(Operation.Status.valueOf(status.toString()));
// operation.setActivityId(activity);
//
// } catch (SQLException e) {
// throw new OperationManagementException("Error occurred while opening a connection to the data source", e);
// } catch (OperationManagementDAOException e) {
// throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" +
// activity, e);
// } finally {
// OperationManagementDAOFactory.closeConnection();
// }
//
// // return this.getOperation(operationId);
// return operation;
// }
@Override
public Activity getOperationByActivityId(String activity) throws OperationManagementException {
// This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer.
@ -808,7 +728,7 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while opening a connection to the data source.", e);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" +
activity, e);
activity, e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
@ -836,11 +756,6 @@ public class OperationManagerImpl implements OperationManager {
}
}
@Override
public List<Operation> getOperationUpdatedAfter(long timestamp) throws OperationManagementException {
return null;
}
@Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException {
try {
@ -850,7 +765,7 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while opening a connection to the data source.", e);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while getting the activity list changed after a " +
"given time.", e);
"given time.", e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
@ -866,7 +781,7 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while opening a connection to the data source.", e);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while getting the activity list changed after a " +
"given time.", e);
"given time.", e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
@ -881,7 +796,7 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while opening a connection to the data source.", e);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while getting the activity count changed after a " +
"given time.", e);
"given time.", e);
} finally {
OperationManagementDAOFactory.closeConnection();
}
@ -904,12 +819,18 @@ public class OperationManagerImpl implements OperationManager {
private OperationDAO lookupOperationDAO(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation) {
if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) {
if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation ||
operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
return commandOperationDAO;
} else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation) {
} else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation ||
operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE)) {
return profileOperationDAO;
} else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation) {
} else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation ||
operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
return configOperationDAO;
} else if (operation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation ||
operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY)) {
return policyOperationDAO;
} else {
return operationDAO;
}
@ -966,8 +887,8 @@ public class OperationManagerImpl implements OperationManager {
enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, status, tenantId);
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving metadata of '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'", e);
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
@ -995,15 +916,15 @@ public class OperationManagerImpl implements OperationManager {
}
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving enrollment data of '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'", e);
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
} catch (DeviceAccessAuthorizationException e) {
throw new OperationManagementException("Error occurred while checking the device access permissions for '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'", e);
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "' of owner '" + owner + "'", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
@ -1018,8 +939,8 @@ public class OperationManagerImpl implements OperationManager {
enrolmentInfo = deviceDAO.getActiveEnrolment(deviceId, tenantId);
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving enrollment data of '" +
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'", e);
deviceId.getType() + "' device carrying the identifier '" +
deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new OperationManagementException(
"Error occurred while opening a connection to the data source", e);
@ -1040,7 +961,7 @@ public class OperationManagerImpl implements OperationManager {
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException("Error occurred while updating enrollment status of device of " +
"enrolment-id '" + enrolmentId + "'", e);
"enrolment-id '" + enrolmentId + "'", e);
} catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally {
@ -1055,7 +976,7 @@ public class OperationManagerImpl implements OperationManager {
List<MonitoringOperation> monitoringOperations = deviceManagementProviderService.getMonitoringOperationList(deviceType);//Get task list from each device type
for(MonitoringOperation op : monitoringOperations){
for (MonitoringOperation op : monitoringOperations) {
if (operation.getCode().equals(op.getTaskName())) {
return true;
}

@ -26,8 +26,6 @@ public class OperationMgtConstants {
}
public static final String DEVICE_ID_NOT_FOUND = "Device not found for device id: %s";
public static final String DEVICE_ID_SERVICE_NOT_FOUND =
"Issue in retrieving device management service instance for device found at %s";
}
public final class OperationCodes {

@ -33,12 +33,8 @@ public interface OperationDAO {
void updateOperation(Operation operation) throws OperationManagementDAOException;
void deleteOperation(int operationId) throws OperationManagementDAOException;
Operation getOperation(int operationId) throws OperationManagementDAOException;
Operation getOperationFromEnrollment(int enrollmentOpMappingId) throws OperationManagementDAOException;
Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException;
List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status)
@ -66,24 +62,16 @@ public interface OperationDAO {
void addOperationResponse(int enrolmentId, int operationId, Object operationResponse)
throws OperationManagementDAOException;
List<OperationResponse> getOperationResponses(int enrolmentId, int operationId) throws OperationManagementDAOException;
Activity getActivity(int operationId) throws OperationManagementDAOException;
Activity getActivityByDevice(int operationId, int deviceId) throws OperationManagementDAOException;
int getEnrolmentIdFromMappingId(int enrollmentOpMappingId) throws OperationManagementDAOException;
List<Operation> getOperationsUpdatedAfter(long timestamp) throws OperationManagementDAOException;
List<Activity> getActivitiesUpdatedAfter(long timestamp) throws OperationManagementDAOException;
List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException;
int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException;
boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException;
/**
* This method provides operation mappings for given status
* @param opStatus Operation status

@ -18,6 +18,7 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationEnrolmentMapping;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping;

@ -70,22 +70,6 @@ public class CommandOperationDAOImpl extends GenericOperationDAOImpl {
}
}
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
super.deleteOperation(id);
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE FROM DM_COMMAND_OPERATION WHERE OPERATION_ID = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
public CommandOperation getOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;

@ -58,22 +58,6 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl {
return operationId;
}
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
super.deleteOperation(id);
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE FROM DM_CONFIG_OPERATION WHERE OPERATION_ID = ?") ;
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
@Override
public void updateOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;

@ -249,68 +249,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
}
}
@Override
public List<OperationResponse> getOperationResponses(int enrolmentId, int operationId) throws
OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<OperationResponse> responces = new ArrayList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT * FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ? AND OPERATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
stmt.setInt(2, operationId);
rs = stmt.executeQuery();
while (rs.next()) {
OperationResponse response = new OperationResponse();
response.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
byte[] contentBytes;
try {
contentBytes = (byte[]) rs.getBytes("OPERATION_RESPONSE");
bais = new ByteArrayInputStream(contentBytes);
ois = new ObjectInputStream(bais);
response.setResponse(ois.readObject().toString());
} finally {
if (bais != null) {
try {
bais.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
}
responces.add(response);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL Error occurred while retrieving the operation responses for " +
"operation id " + operationId + " and enrolment id " + enrolmentId, e);
} catch (ClassNotFoundException e) {
throw new OperationManagementDAOException("Error occurred while converting the operation responses to string" +
" for operation id " + operationId + " and enrolment id " + enrolmentId, e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while converting the operation responses to string" +
" for operation id " + operationId + " and enrolment id " + enrolmentId, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return responces;
}
@Override
public Activity getActivity(int operationId) throws OperationManagementDAOException {
@ -674,80 +612,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
return 0;
}
@Override
public int getEnrolmentIdFromMappingId(int enrollmentOpMappingId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT * FROM DM_ENROLMENT_OP_MAPPING WHERE ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentOpMappingId);
rs = stmt.executeQuery();
if (rs.next()) {
return rs.getInt("ENROLMENT_ID");
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL Error occurred while retrieving the enrolment id " +
" for the mapping id '" + enrollmentOpMappingId, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return -1;
}
@Override
public List<Operation> getOperationsUpdatedAfter(long timestamp) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Operation> operations = new ArrayList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION AS o \n" +
"INNER JOIN DM_ENROLMENT_OP_MAPPING AS eom ON eom.OPERATION_ID=o.ID WHERE eom.UPDATED_TIMESTAMP = ?";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp);
rs = stmt.executeQuery();
if (rs.next()) {
Operation operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
operation.setCode(rs.getString("OPERATION_CODE"));
operations.add(operation);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while retrieving the operations updated " +
"after a given time", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE FROM DM_OPERATION WHERE ID = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
@Override
public Operation getOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
@ -783,49 +647,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
return operation;
}
@Override
public Operation getOperationFromEnrollment(int enrollmentOpMappingId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
Operation operation = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, OPERATION_CODE, \n" +
" om.STATUS, om.UPDATED_TIMESTAMP FROM DM_OPERATION o \n" +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm WHERE dm.ID = ? ) om \n" +
"ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC ";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentOpMappingId);
rs = stmt.executeQuery();
if (rs.next()) {
operation = new Operation();
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
// operation.setReceivedTimeStamp("");
// } else {
// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
// }
if (rs.getLong("UPDATED_TIMESTAMP") == 0) {
operation.setReceivedTimeStamp("");
} else {
operation.setReceivedTimeStamp(
new java.sql.Timestamp((rs.getLong("UPDATED_TIMESTAMP") * 1000)).toString());
}
operation.setCode(rs.getString("OPERATION_CODE"));
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
OperationDAOUtil.setActivityId(operation, rs.getInt("ID"));
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation .", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operation;
}
@Override
public Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
@ -833,9 +654,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
Operation operation = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE, " +
String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, om.STATUS, o.OPERATION_CODE, " +
"om.ID AS OM_MAPPING_ID, " +
"om.UPDATED_TIMESTAMP FROM (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," +
"om.UPDATED_TIMESTAMP FROM (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP," +
"OPERATION_CODE FROM DM_OPERATION WHERE id = ?) o INNER JOIN (SELECT * FROM " +
"DM_ENROLMENT_OP_MAPPING dm where dm.OPERATION_ID = ? AND dm.ENROLMENT_ID = ?) om " +
"ON o.ID = om.OPERATION_ID ";
@ -850,6 +671,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
// operation.setReceivedTimeStamp("");
// } else {
@ -1176,31 +998,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
return operations;
}
@Override
public boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException {
boolean status = false;
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = OperationManagementDAOFactory.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " +
"WHERE ENROLMENT_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setTimestamp(1, currentTimestamp);
stmt.setInt(2, enrolmentId);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
status = true;
} catch (SQLException e) {
throw new OperationManagementDAOException("Unable to reset the attempt count in database.", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@Override
public Map<Integer, List<OperationMapping>> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus,
int limit) throws OperationManagementDAOException {

@ -122,22 +122,6 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl {
}
}
@Override
public void deleteOperation(int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
super.deleteOperation(operationId);
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?");
stmt.setInt(1, operationId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
@Override
public Operation getOperation(int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null;

@ -123,22 +123,6 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl {
}
}
@Override
public void deleteOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
super.deleteOperation(id);
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("DELETE FROM DM_PROFILE_OPERATION WHERE OPERATION_ID=?");
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while deleting operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
public Operation getOperation(int id) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;

@ -1,154 +0,0 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.mgt.core.push.notification.mgt;
import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
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.OperationManager;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
import java.util.List;
public class PushNotificationBasedOperationManager implements OperationManager {
private OperationManager operationManager;
private NotificationStrategy notificationProvider;
public PushNotificationBasedOperationManager(
OperationManager operationManager, NotificationStrategy notificationProvider) {
this.operationManager = operationManager;
this.notificationProvider = notificationProvider;
}
@Override
public Activity addOperation(Operation operation,
List<DeviceIdentifier> devices) throws OperationManagementException, InvalidDeviceException {
Activity activity = this.operationManager.addOperation(operation, devices);
for (DeviceIdentifier deviceId : devices) {
try {
this.notificationProvider.execute(new NotificationContext(deviceId, operation));
} catch (PushNotificationExecutionFailedException e) {
throw new OperationManagementException("Error occurred while sending push notification to device", e);
}
}
return activity;
}
@Override
public List<? extends Operation> getOperations(
DeviceIdentifier deviceId) throws OperationManagementException {
return this.operationManager.getOperations(deviceId);
}
@Override
public PaginationResult getOperations(DeviceIdentifier deviceId,
PaginationRequest request) throws OperationManagementException {
return this.operationManager.getOperations(deviceId, request);
}
@Override
public List<? extends Operation> getPendingOperations(
DeviceIdentifier deviceId) throws OperationManagementException {
return this.operationManager.getPendingOperations(deviceId);
}
@Override
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
return this.operationManager.getNextPendingOperation(deviceId);
}
@Override
public void updateOperation(DeviceIdentifier deviceId,
Operation operation) throws OperationManagementException {
this.operationManager.updateOperation(deviceId, operation);
}
@Override
public void deleteOperation(int operationId) throws OperationManagementException {
this.operationManager.deleteOperation(operationId);
}
@Override
public Operation getOperationByDeviceAndOperationId(
DeviceIdentifier deviceId, int operationId) throws OperationManagementException {
return this.operationManager.getOperationByDeviceAndOperationId(deviceId, operationId);
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(
DeviceIdentifier deviceId,
Operation.Status status) throws OperationManagementException {
try {
return this.operationManager.getOperationsByDeviceAndStatus(deviceId, status);
} catch (DeviceManagementException e) {
throw new OperationManagementException("Error occurred while retrieving the list of operations by " +
"device and status", e);
}
}
@Override
public Operation getOperation(int operationId) throws OperationManagementException {
return this.operationManager.getOperation(operationId);
}
@Override
public Activity getOperationByActivityId(String activity) throws OperationManagementException {
return this.operationManager.getOperationByActivityId(activity);
}
@Override
public Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException {
return this.operationManager.getOperationByActivityIdAndDevice(activity, deviceId);
}
@Override
public List<Operation> getOperationUpdatedAfter(long timestamp) throws OperationManagementException {
return this.operationManager.getOperationUpdatedAfter(timestamp);
}
@Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException {
return this.operationManager.getActivitiesUpdatedAfter(timestamp);
}
@Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException {
return this.operationManager.getActivitiesUpdatedAfter(timestamp, limit, offset);
}
@Override
public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException {
return this.operationManager.getActivityCountUpdatedAfter(timestamp);
}
@Override
public void setNotificationStrategy(NotificationStrategy notificationStrategy) {
}
@Override
public NotificationStrategy getNotificationStrategy() {
return notificationProvider;
}
}

@ -553,8 +553,6 @@ public interface DeviceManagementProviderService {
void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException;
void deleteOperation(String type, int operationId) throws OperationManagementException;
Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException;

@ -1429,11 +1429,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
.updateOperation(deviceId, operation);
}
@Override
public void deleteOperation(String type, int operationId) throws OperationManagementException {
pluginRepository.getOperationManager(type, this.getTenantId()).deleteOperation(operationId);
}
@Override
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId,
int operationId) throws OperationManagementException {

@ -18,8 +18,6 @@
package org.wso2.carbon.device.mgt.core.operation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -35,9 +33,10 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
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.OperationManager;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.TestDeviceManagementService;
import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
@ -59,13 +58,17 @@ import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class OperationManagementTests{
private static final Log log = LogFactory.getLog(OperationManagementTests.class);
/**
* This is the testcase which covers the methods from {@link OperationManager}
*/
public class OperationManagementTests {
private static final String DEVICE_TYPE = "OP_TEST_TYPE";
private static final String DEVICE_ID_PREFIX = "OP-TEST-DEVICE-ID-";
@ -80,11 +83,11 @@ public class OperationManagementTests{
private List<DeviceIdentifier> deviceIds = new ArrayList<>();
private OperationManager operationMgtService;
private Activity commandActivity;
@BeforeClass
public void init() throws Exception {
DeviceConfigurationManager.getInstance().initConfig();
log.info("Initializing");
for (int i = 0; i < NO_OF_DEVICES; i++) {
deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE));
}
@ -107,7 +110,8 @@ public class OperationManagementTests{
throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!");
}
}
this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE);
NotificationStrategy notificationStrategy = new TestNotificationStrategy();
this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy);
}
private RegistryService getRegistryService() throws RegistryException {
@ -122,30 +126,57 @@ public class OperationManagementTests{
@Test
public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE),
this.commandActivity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE),
this.deviceIds);
validateOperationResponse(activity);
validateOperationResponse(this.commandActivity, ActivityStatus.Status.PENDING);
}
@Test(expectedExceptions = InvalidDeviceException.class)
public void addEmptyDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE),
new ArrayList<>());
}
@Test(expectedExceptions = InvalidDeviceException.class)
public void addNonInitializedDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(deviceIdentifier);
this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE),
deviceIdentifiers);
}
@Test
public void addNonAdminUserDevicesCommandOperation() throws DeviceManagementException, OperationManagementException,
InvalidDeviceException {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID, true);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(NON_ADMIN_USER);
Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE),
deviceIds);
PrivilegedCarbonContext.endTenantFlow();
validateOperationResponse(activity, ActivityStatus.Status.UNAUTHORIZED);
}
@Test(dependsOnMethods = "addCommandOperation")
public void addPolicyOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
Activity activity = this.operationMgtService.addOperation(getOperation(new PolicyOperation(), Operation.Type.POLICY, POLICY_OPERATION_CODE),
this.deviceIds);
validateOperationResponse(activity);
validateOperationResponse(activity, ActivityStatus.Status.PENDING);
}
@Test(dependsOnMethods = "addPolicyOperation")
public void addConfigOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
Activity activity = this.operationMgtService.addOperation(getOperation(new ConfigOperation(), Operation.Type.CONFIG, CONFIG_OPERATION_CODE),
this.deviceIds);
validateOperationResponse(activity);
validateOperationResponse(activity, ActivityStatus.Status.PENDING);
}
@Test(dependsOnMethods = "addConfigOperation")
public void addProfileOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException {
Activity activity = this.operationMgtService.addOperation(getOperation(new ProfileOperation(), Operation.Type.PROFILE, PROFILE_OPERATION_CODE),
this.deviceIds);
validateOperationResponse(activity);
validateOperationResponse(activity, ActivityStatus.Status.PENDING);
}
private Operation getOperation(Operation operation, Operation.Type type, String code) {
@ -156,11 +187,11 @@ public class OperationManagementTests{
return operation;
}
private void validateOperationResponse(Activity activity) {
private void validateOperationResponse(Activity activity, ActivityStatus.Status expectedStatus) {
Assert.assertEquals(activity.getActivityStatus().size(), NO_OF_DEVICES, "The operation reponse for add operation only have - " +
activity.getActivityStatus().size());
for (ActivityStatus status : activity.getActivityStatus()) {
Assert.assertEquals(status.getStatus(), ActivityStatus.Status.PENDING);
Assert.assertEquals(status.getStatus(), expectedStatus);
}
}
@ -209,7 +240,7 @@ public class OperationManagementTests{
try {
this.operationMgtService.getOperations(deviceIdentifier, request);
} catch (OperationManagementException ex) {
if (ex.getMessage() == null){
if (ex.getMessage() == null) {
Assert.assertTrue(ex.getMessage().contains("User '" + NON_ADMIN_USER + "' is not authorized"));
}
}
@ -219,9 +250,14 @@ public class OperationManagementTests{
@Test(dependsOnMethods = "getPaginatedRequestAsAdmin")
public void updateOperation() throws OperationManagementException {
//This is required to introduce a delay for the update operation of the device.
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
}
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
List operations = this.operationMgtService.getPendingOperations(deviceIdentifier);
Assert.assertTrue(operations!= null && operations.size()==4);
Assert.assertTrue(operations != null && operations.size() == 4);
Operation operation = (Operation) operations.get(0);
operation.setStatus(Operation.Status.COMPLETED);
operation.setOperationResponse("The operation is successfully completed");
@ -229,4 +265,110 @@ public class OperationManagementTests{
List pendingOperations = this.operationMgtService.getPendingOperations(deviceIdentifier);
Assert.assertEquals(pendingOperations.size(), 3);
}
@Test(dependsOnMethods = "updateOperation")
public void getNextPendingOperation() throws OperationManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
Operation operation = this.operationMgtService.getNextPendingOperation(deviceIdentifier);
Assert.assertTrue(operation.getType().equals(Operation.Type.POLICY));
}
@Test(dependsOnMethods = "getNextPendingOperation")
public void getOperationByDeviceAndOperationId() throws OperationManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
String operationId = this.commandActivity.getActivityId().
replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "");
Operation operation = this.operationMgtService.getOperationByDeviceAndOperationId(deviceIdentifier,
Integer.parseInt(operationId));
Assert.assertTrue(operation.getStatus().equals(Operation.Status.COMPLETED));
Assert.assertTrue(operation.getType().equals(Operation.Type.COMMAND));
}
@Test(dependsOnMethods = "getOperationByDeviceAndOperationId")
public void getOperationsByDeviceAndStatus() throws OperationManagementException, DeviceManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
List operation = this.operationMgtService.getOperationsByDeviceAndStatus(deviceIdentifier, Operation.Status.PENDING);
Assert.assertEquals(operation.size(), 3);
}
@Test(dependsOnMethods = "getOperationsByDeviceAndStatus")
public void getOperation() throws OperationManagementException, DeviceManagementException {
String operationId = this.commandActivity.getActivityId().
replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "");
Operation operation = this.operationMgtService.getOperation(Integer.parseInt(operationId));
Assert.assertEquals(operation.getType(), Operation.Type.COMMAND);
}
@Test(dependsOnMethods = "getOperation")
public void getOperationActivity() throws OperationManagementException {
Activity activity = this.operationMgtService.getOperationByActivityId(commandActivity.getActivityId());
Assert.assertEquals(activity.getType(), Activity.Type.COMMAND);
Assert.assertEquals(activity.getActivityStatus().size(), this.deviceIds.size());
Assert.assertEquals(activity.getActivityStatus().get(0).getStatus(), ActivityStatus.Status.COMPLETED);
for (int i = 1; i < this.deviceIds.size(); i++) {
Assert.assertEquals(activity.getActivityStatus().get(i).getStatus(), ActivityStatus.Status.PENDING);
}
}
@Test(dependsOnMethods = "getOperationActivity")
public void getOperationByActivityIdAndDevice() throws OperationManagementException {
Activity activity = this.operationMgtService.
getOperationByActivityIdAndDevice(this.commandActivity.getActivityId(), this.deviceIds.get(0));
Assert.assertEquals(activity.getType(), Activity.Type.COMMAND);
Assert.assertEquals(activity.getActivityStatus().size(), 1);
Assert.assertEquals(activity.getActivityStatus().get(0).getStatus(), ActivityStatus.Status.COMPLETED);
}
@Test(dependsOnMethods = "updateOperation")
public void getOperationUpdatedAfterWithLimitAndOffet() throws OperationManagementException, ParseException {
String timestamp = this.commandActivity.getCreatedTimeStamp();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy");
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date = dateFormat.parse(timestamp);
List<Activity> operations = this.operationMgtService.getActivitiesUpdatedAfter(date.getTime() / 1000, 10, 0);
Assert.assertTrue(operations != null && operations.size() == 1,
"The operations updated after the created should be 1");
Activity operation = operations.get(0);
Assert.assertTrue(operation.getActivityStatus() != null && operation.getActivityStatus().size() == 1,
"The operation should be having the activity status of atleast one device");
Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getId(),
deviceIds.get(0).getId());
Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getType(),
deviceIds.get(0).getType());
}
@Test(dependsOnMethods = "updateOperation")
public void getOperationUpdatedAfter() throws OperationManagementException, ParseException {
String timestamp = this.commandActivity.getCreatedTimeStamp();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy");
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date = dateFormat.parse(timestamp);
List<Activity> operations = this.operationMgtService.getActivitiesUpdatedAfter(date.getTime() / 1000);
Assert.assertTrue(operations != null && operations.size() == 1,
"The operations updated after the created should be 1");
Activity operation = operations.get(0);
Assert.assertTrue(operation.getActivityStatus() != null && operation.getActivityStatus().size() == 1,
"The operation should be having the activity status of atleast one device");
Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getId(),
deviceIds.get(0).getId());
Assert.assertEquals(operation.getActivityStatus().get(0).getDeviceIdentifier().getType(),
deviceIds.get(0).getType());
}
@Test(dependsOnMethods = "getOperationUpdatedAfter")
public void getActivityCountUpdatedAfter() throws OperationManagementException, ParseException {
String timestamp = this.commandActivity.getCreatedTimeStamp();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy");
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date = dateFormat.parse(timestamp);
int activityCount = this.operationMgtService.getActivityCountUpdatedAfter(date.getTime() / 1000);
Assert.assertTrue(activityCount == 1,
"The activities updated after the created should be 1");
}
@Test
public void getNotificationStrategy() {
Assert.assertTrue(this.operationMgtService.getNotificationStrategy() != null);
}
}

@ -0,0 +1,51 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.mgt.core.operation;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
public class TestNotificationStrategy implements NotificationStrategy {
@Override
public void init() {
}
@Override
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
}
@Override
public NotificationContext buildContext() {
return null;
}
@Override
public void undeploy() {
}
@Override
public PushNotificationConfig getConfig() {
return null;
}
}
Loading…
Cancel
Save