mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Add device subscription functionality for APPM
This commit is contained in:
parent
ab841939a4
commit
c1c821e0f7
@ -25,14 +25,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ApplicationInstallResponse {
|
public class ApplicationInstallResponse {
|
||||||
@ApiModelProperty(
|
|
||||||
name = "installedDevices",
|
|
||||||
value = "List of successful devices",
|
|
||||||
dataType = "List[org.wso2.carbon.device.mgt.common.DeviceIdentifier]"
|
|
||||||
)
|
|
||||||
private List<DeviceIdentifier> installedDevices;
|
|
||||||
|
|
||||||
|
|
||||||
@ApiModelProperty(
|
@ApiModelProperty(
|
||||||
name = "alreadyInstalledDevices",
|
name = "alreadyInstalledDevices",
|
||||||
value = "List of devices that application release is already installed.",
|
value = "List of devices that application release is already installed.",
|
||||||
@ -46,14 +38,6 @@ public class ApplicationInstallResponse {
|
|||||||
)
|
)
|
||||||
private Activity activity;
|
private Activity activity;
|
||||||
|
|
||||||
public List<DeviceIdentifier> getInstalledDevices() {
|
|
||||||
return installedDevices;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setInstalledDevices(List<DeviceIdentifier> installedDevices) {
|
|
||||||
this.installedDevices = installedDevices;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Activity getActivity() {
|
public Activity getActivity() {
|
||||||
return activity;
|
return activity;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,5 +18,5 @@
|
|||||||
package org.wso2.carbon.device.application.mgt.common;
|
package org.wso2.carbon.device.application.mgt.common;
|
||||||
|
|
||||||
public enum SubsciptionType {
|
public enum SubsciptionType {
|
||||||
USER, ROLE, DEVICE_GROUP
|
USER, ROLE, GROUP, DEVICE
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,10 +33,10 @@ public interface SubscriptionManager {
|
|||||||
* To install an application to given list of devices.
|
* To install an application to given list of devices.
|
||||||
* @param applicationUUID ID of the application to install
|
* @param applicationUUID ID of the application to install
|
||||||
* @param deviceList list of device ID's to install the application
|
* @param deviceList list of device ID's to install the application
|
||||||
* @return {@link ApplicationInstallResponseTmp} object which contains installed application and devices
|
* @return {@link ApplicationInstallResponse} object which contains installed application and devices
|
||||||
* @throws ApplicationManagementException if unable to install the application to the given devices
|
* @throws ApplicationManagementException if unable to install the application to the given devices
|
||||||
*/
|
*/
|
||||||
ApplicationInstallResponseTmp installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList)
|
ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList)
|
||||||
throws ApplicationManagementException;
|
throws ApplicationManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -443,7 +443,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
|
|||||||
StringJoiner joiner = new StringJoiner(",",
|
StringJoiner joiner = new StringJoiner(",",
|
||||||
"SELECT DS.DM_DEVICE_ID "
|
"SELECT DS.DM_DEVICE_ID "
|
||||||
+ "FROM AP_DEVICE_SUBSCRIPTION DS "
|
+ "FROM AP_DEVICE_SUBSCRIPTION DS "
|
||||||
+ "WHERE US.DM_DEVICE_ID IN (", ") AND TENANT_ID = ?");
|
+ "WHERE DS.DM_DEVICE_ID IN (", ") AND TENANT_ID = ?");
|
||||||
deviceIds.stream().map(ignored -> "?").forEach(joiner::add);
|
deviceIds.stream().map(ignored -> "?").forEach(joiner::add);
|
||||||
String query = joiner.toString();
|
String query = joiner.toString();
|
||||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||||
|
|||||||
@ -86,15 +86,55 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApplicationInstallResponseTmp installApplicationForDevices(String applicationUUID,
|
public ApplicationInstallResponse installApplicationForDevices(String applicationUUID,
|
||||||
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
|
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size()
|
log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size()
|
||||||
+ "devices.");
|
+ "devices.");
|
||||||
}
|
}
|
||||||
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
|
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
|
||||||
validateAppInstallingForDevicesRequest(applicationDTO, deviceIdentifiers);
|
List<Integer> operationTriggeredDeviceIds = new ArrayList<>();
|
||||||
return installToDevicesTmp(applicationDTO, deviceIdentifiers);
|
List <Device> filteredDevices = validateAppInstallingForDevicesRequest(applicationDTO, deviceIdentifiers);
|
||||||
|
List<Integer> filteredDeviceIds = new ArrayList<>();
|
||||||
|
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>();
|
||||||
|
Map<DeviceIdentifier , Integer> compatibleDevices = new HashMap<>();
|
||||||
|
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions;
|
||||||
|
|
||||||
|
for (Device device : filteredDevices){
|
||||||
|
filteredDeviceIds.add(device.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds);
|
||||||
|
for (Device device : filteredDevices) {
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),
|
||||||
|
device.getType());
|
||||||
|
DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptions.get(device.getId());
|
||||||
|
if (deviceSubscriptionDTO != null && !deviceSubscriptionDTO.isUnsubscribed()
|
||||||
|
&& Operation.Status.COMPLETED.toString().equals(deviceSubscriptionDTO.getStatus())) {
|
||||||
|
installedDeviceIdentifiers.add(deviceIdentifier);
|
||||||
|
} else {
|
||||||
|
compatibleDevices.put(deviceIdentifier, device.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Activity activity = installToDevices(applicationDTO, deviceIdentifiers, deviceIdentifiers.get(0).getType());
|
||||||
|
|
||||||
|
List<ActivityStatus> activityStatuses = activity.getActivityStatus();
|
||||||
|
for (ActivityStatus status : activityStatuses) {
|
||||||
|
if (status.getStatus().equals(ActivityStatus.Status.PENDING)){
|
||||||
|
operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
|
||||||
|
applicationInstallResponse.setActivity(activity);
|
||||||
|
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
|
||||||
|
|
||||||
|
int operationId = Integer
|
||||||
|
.parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]);
|
||||||
|
addDeviceSubscriptionForUser(applicationDTO.getApplicationReleaseDTOs().get(0).getId(),
|
||||||
|
operationTriggeredDeviceIds, new ArrayList<>(deviceSubscriptions.keySet()), null, operationId,
|
||||||
|
SubsciptionType.DEVICE.toString());
|
||||||
|
return applicationInstallResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApplicationDTO getApplicationDTO(String uuid) throws ApplicationManagementException {
|
private ApplicationDTO getApplicationDTO(String uuid) throws ApplicationManagementException {
|
||||||
@ -132,9 +172,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateAppInstallingForDevicesRequest(ApplicationDTO applicationDTO,
|
private List <Device> validateAppInstallingForDevicesRequest(ApplicationDTO applicationDTO,
|
||||||
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
|
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
|
||||||
DeviceType deviceType = null;
|
DeviceType deviceType = null;
|
||||||
|
List <Device> existingDevices = new ArrayList<>();
|
||||||
|
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
|
||||||
|
.getDeviceManagementProviderService();
|
||||||
if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) {
|
if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) {
|
||||||
deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
|
deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
|
||||||
}
|
}
|
||||||
@ -149,7 +192,23 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
log.error(msg);
|
log.error(msg);
|
||||||
throw new BadRequestException(msg);
|
throw new BadRequestException(msg);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
Device device = deviceManagementProviderService.getDevice(deviceIdentifier, false);
|
||||||
|
if (device == null) {
|
||||||
|
String msg = "Couldn't found an device for device identifier " + deviceIdentifier.getId()
|
||||||
|
+ " and device type: " + deviceIdentifier.getType();
|
||||||
|
log.error(msg);
|
||||||
|
} else {
|
||||||
|
existingDevices.add(device);
|
||||||
}
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
String msg = "Error occuered when getting device data for divice identifier " + deviceIdentifier.getId()
|
||||||
|
+ " and device type " + deviceIdentifier.getType();
|
||||||
|
log.error(msg);
|
||||||
|
throw new ApplicationManagementException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return existingDevices;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -163,10 +222,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
//todo check valid user list - throw BadRequest exception
|
//todo check valid user list - throw BadRequest exception
|
||||||
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
|
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
|
||||||
DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
|
DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
|
||||||
List<DeviceIdentifier> operationTriggeredDeviceIdentifiers = new ArrayList<>();
|
|
||||||
Map<DeviceIdentifier , Integer> compatibleDevices = new HashMap<>();
|
Map<DeviceIdentifier , Integer> compatibleDevices = new HashMap<>();
|
||||||
List<Integer> operationTriggeredDeviceIds = new ArrayList<>();
|
List<Integer> operationTriggeredDeviceIds = new ArrayList<>();
|
||||||
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>();
|
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>();
|
||||||
|
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions = new HashMap<>();
|
||||||
|
|
||||||
for (String user : userList) {
|
for (String user : userList) {
|
||||||
try {
|
try {
|
||||||
@ -174,13 +233,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
List<Integer> filteredDeviceIds = new ArrayList<>();
|
List<Integer> filteredDeviceIds = new ArrayList<>();
|
||||||
List<Device> filteredDevices = new ArrayList<>();
|
List<Device> filteredDevices = new ArrayList<>();
|
||||||
|
|
||||||
|
//todo improve for web clips
|
||||||
for (Device device : userDevices) {
|
for (Device device : userDevices) {
|
||||||
if (appDeviceType.getName().equals(device.getType())) {
|
if (appDeviceType.getName().equals(device.getType())) {
|
||||||
filteredDevices.add(device);
|
filteredDevices.add(device);
|
||||||
filteredDeviceIds.add(device.getId());
|
filteredDeviceIds.add(device.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds);
|
deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds);
|
||||||
for (Device device : filteredDevices) {
|
for (Device device : filteredDevices) {
|
||||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),
|
||||||
device.getType());
|
device.getType());
|
||||||
@ -205,22 +265,21 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
for (ActivityStatus status : activityStatuses) {
|
for (ActivityStatus status : activityStatuses) {
|
||||||
if (status.getStatus().equals(ActivityStatus.Status.PENDING)){
|
if (status.getStatus().equals(ActivityStatus.Status.PENDING)){
|
||||||
operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier()));
|
operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier()));
|
||||||
operationTriggeredDeviceIdentifiers.add(status.getDeviceIdentifier());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
|
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
|
||||||
applicationInstallResponse.setActivity(activity);
|
applicationInstallResponse.setActivity(activity);
|
||||||
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
|
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
|
||||||
applicationInstallResponse.setInstalledDevices(operationTriggeredDeviceIdentifiers);
|
|
||||||
|
|
||||||
int operationId = Integer
|
int operationId = Integer
|
||||||
.parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]);
|
.parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]);
|
||||||
addDeviceSubscriptionForUser(applicationDTO.getApplicationReleaseDTOs().get(0).getId(),
|
addDeviceSubscriptionForUser(applicationDTO.getApplicationReleaseDTOs().get(0).getId(),
|
||||||
operationTriggeredDeviceIds, userList, operationId);
|
operationTriggeredDeviceIds, new ArrayList<>(deviceSubscriptions.keySet()), userList, operationId, SubsciptionType.USER.toString());
|
||||||
return applicationInstallResponse;
|
return applicationInstallResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDeviceSubscriptionForUser(int applicationReleaseId, List<Integer> deviceIds, List<String> userList, int operationId)
|
private void addDeviceSubscriptionForUser(int applicationReleaseId, List<Integer> deviceIds,
|
||||||
|
List<Integer> subDeviceIds, List<String> userList, int operationId, String subType)
|
||||||
throws ApplicationManagementException {
|
throws ApplicationManagementException {
|
||||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||||
String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||||
@ -229,6 +288,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
List<Integer> deviceResubscribingIds = new ArrayList<>();
|
List<Integer> deviceResubscribingIds = new ArrayList<>();
|
||||||
List<Integer> deviceSubscriptingIds;
|
List<Integer> deviceSubscriptingIds;
|
||||||
|
|
||||||
|
if (SubsciptionType.USER.toString().equals(subType)){
|
||||||
List<String> subscribedUsers = subscriptionDAO.getSubscribedUsernames(userList, tenantId);
|
List<String> subscribedUsers = subscriptionDAO.getSubscribedUsernames(userList, tenantId);
|
||||||
if (!subscribedUsers.isEmpty()) {
|
if (!subscribedUsers.isEmpty()) {
|
||||||
subscriptionDAO
|
subscriptionDAO
|
||||||
@ -236,17 +296,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|||||||
userList.removeAll(subscribedUsers);
|
userList.removeAll(subscribedUsers);
|
||||||
}
|
}
|
||||||
subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, userList, applicationReleaseId);
|
subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, userList, applicationReleaseId);
|
||||||
|
}
|
||||||
|
|
||||||
List<Integer> subscribedDevices = subscriptionDAO.getSubscribedDeviceIds(deviceIds, tenantId);
|
if (!subDeviceIds.isEmpty()) {
|
||||||
if (!subscribedDevices.isEmpty()) {
|
|
||||||
deviceResubscribingIds = subscriptionDAO
|
deviceResubscribingIds = subscriptionDAO
|
||||||
.updateDeviceSubscription(subscriber, deviceIds, SubsciptionType.USER.toString(),
|
.updateDeviceSubscription(subscriber, subDeviceIds, subType, Operation.Status.PENDING.toString(),
|
||||||
Operation.Status.PENDING.toString(), applicationReleaseId, tenantId);
|
applicationReleaseId, tenantId);
|
||||||
deviceIds.removeAll(subscribedDevices);
|
deviceIds.removeAll(subDeviceIds);
|
||||||
}
|
}
|
||||||
deviceSubscriptingIds = subscriptionDAO
|
deviceSubscriptingIds = subscriptionDAO
|
||||||
.subscribeDeviceToApplication(subscriber, deviceIds, SubsciptionType.USER.toString(),
|
.subscribeDeviceToApplication(subscriber, deviceIds, subType, Operation.Status.PENDING.toString(),
|
||||||
Operation.Status.PENDING.toString(), applicationReleaseId, tenantId);
|
applicationReleaseId, tenantId);
|
||||||
deviceSubscriptingIds.addAll(deviceResubscribingIds);
|
deviceSubscriptingIds.addAll(deviceResubscribingIds);
|
||||||
subscriptionDAO.addOperationMapping(operationId, deviceSubscriptingIds, tenantId);
|
subscriptionDAO.addOperationMapping(operationId, deviceSubscriptingIds, tenantId);
|
||||||
ConnectionManagerUtil.commitDBTransaction();
|
ConnectionManagerUtil.commitDBTransaction();
|
||||||
|
|||||||
@ -64,7 +64,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
|
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
|
||||||
ApplicationInstallResponseTmp response = subscriptionManager
|
ApplicationInstallResponse response = subscriptionManager
|
||||||
.installApplicationForDevices(uuid, deviceIdentifiers);
|
.installApplicationForDevices(uuid, deviceIdentifiers);
|
||||||
return Response.status(Response.Status.OK).entity(response).build();
|
return Response.status(Response.Status.OK).entity(response).build();
|
||||||
} catch (NotFoundException e) {
|
} catch (NotFoundException e) {
|
||||||
@ -205,7 +205,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ApplicationInstallResponseTmp response = subscriptionManager.installApplicationForDevices(applicationUUID,
|
ApplicationInstallResponse response = subscriptionManager.installApplicationForDevices(applicationUUID,
|
||||||
installationDetails.getDeviceIdentifiers());
|
installationDetails.getDeviceIdentifiers());
|
||||||
return Response.status(Response.Status.OK).entity(response).build();
|
return Response.status(Response.Status.OK).entity(response).build();
|
||||||
} catch (ApplicationManagementException e) {
|
} catch (ApplicationManagementException e) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user