@ -19,15 +19,19 @@ package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
import org.wso2.carbon.context.PrivilegedCarbonContext ;
import org.wso2.carbon.device.application.mgt.common.Application ;
import org.wso2.carbon.device.application.mgt.common.Application ;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse ;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse ;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier ;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException ;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException ;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager ;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager ;
import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager ;
import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager ;
import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO ;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory ;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil ;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil ;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil ;
import org.wso2.carbon.device.application.mgt.core.util.HelperUtil ;
import org.wso2.carbon.device.application.mgt.core.util.HelperUtil ;
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.DeviceManagementException ;
import org.wso2.carbon.device.mgt.common.DeviceManagementException ;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException ;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException ;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup ;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup ;
@ -52,6 +56,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
private static final Log log = LogFactory . getLog ( SubscriptionManagerImpl . class ) ;
private static final Log log = LogFactory . getLog ( SubscriptionManagerImpl . class ) ;
private static final String INSTALL_APPLICATION = "INSTALL_APPLICATION" ;
private static final String INSTALL_APPLICATION = "INSTALL_APPLICATION" ;
private SubscriptionDAO subscriptionDAO ;
public SubscriptionManagerImpl ( ) {
this . subscriptionDAO = ApplicationManagementDAOFactory . getSubscriptionDAO ( ) ;
}
@Override
@Override
public ApplicationInstallResponse installApplicationForDevices ( String applicationUUID ,
public ApplicationInstallResponse installApplicationForDevices ( String applicationUUID ,
@ -59,7 +68,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Install application: " + applicationUUID + " to " + deviceList . size ( ) + "devices." ) ;
log . debug ( "Install application: " + applicationUUID + " to " + deviceList . size ( ) + "devices." ) ;
}
}
return installApplication ( applicationUUID , deviceList ) ;
ApplicationManager applicationManager = ApplicationManagementUtil . getApplicationManagerInstance ( ) ;
Application application = applicationManager . getApplicationByRelease ( applicationUUID ) ;
return installApplication ( application , deviceList ) ;
}
}
@Override
@Override
@ -68,13 +80,15 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Install application: " + applicationUUID + " to " + userList . size ( ) + " users." ) ;
log . debug ( "Install application: " + applicationUUID + " to " + userList . size ( ) + " users." ) ;
}
}
ApplicationManager applicationManager = ApplicationManagementUtil . getApplicationManagerInstance ( ) ;
Application application = applicationManager . getApplicationByRelease ( applicationUUID ) ;
List < DeviceIdentifier > deviceList = new ArrayList < > ( ) ;
List < DeviceIdentifier > deviceList = new ArrayList < > ( ) ;
for ( String user : userList ) {
for ( String user : userList ) {
try {
try {
List < Device > devicesOfUser = HelperUtil . getDeviceManagementProviderService ( ) . getDevicesOfUser ( user ) ;
List < Device > devicesOfUser = HelperUtil . getDeviceManagementProviderService ( ) . getDevicesOfUser ( user ) ;
for ( Device device : devicesOfUser ) {
devicesOfUser . stream ( )
deviceList . add ( new DeviceIdentifier ( device . getDeviceIdentifier ( ) , device . getType ( ) ) ) ;
. map ( device - > new DeviceIdentifier ( device . getDeviceIdentifier ( ) , device . getType ( ) ) )
}
. forEach ( deviceList : : add ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( devicesOfUser . size ( ) + " found for the provided user list" ) ;
log . debug ( devicesOfUser . size ( ) + " found for the provided user list" ) ;
}
}
@ -83,7 +97,22 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
e ) ;
e ) ;
}
}
}
}
return installApplication ( applicationUUID , deviceList ) ;
ApplicationInstallResponse response = installApplication ( application , deviceList ) ;
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
String subscriber = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
int applicationReleaseId = application . getApplicationReleases ( ) . get ( 0 ) . getId ( ) ;
try {
ConnectionManagerUtil . openDBConnection ( ) ;
subscriptionDAO . subscribeUserToApplication ( tenantId , subscriber , userList , application . getId ( ) ,
applicationReleaseId ) ;
} finally {
ConnectionManagerUtil . closeDBConnection ( ) ;
}
return response ;
}
}
@Override
@Override
@ -92,13 +121,15 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Install application: " + applicationUUID + " to " + roleList . size ( ) + " roles." ) ;
log . debug ( "Install application: " + applicationUUID + " to " + roleList . size ( ) + " roles." ) ;
}
}
ApplicationManager applicationManager = ApplicationManagementUtil . getApplicationManagerInstance ( ) ;
Application application = applicationManager . getApplicationByRelease ( applicationUUID ) ;
List < DeviceIdentifier > deviceList = new ArrayList < > ( ) ;
List < DeviceIdentifier > deviceList = new ArrayList < > ( ) ;
for ( String role : roleList ) {
for ( String role : roleList ) {
try {
try {
List < Device > devicesOfRole = HelperUtil . getDeviceManagementProviderService ( ) . getAllDevicesOfRole ( role ) ;
List < Device > devicesOfRole = HelperUtil . getDeviceManagementProviderService ( ) . getAllDevicesOfRole ( role ) ;
for ( Device device : devicesOfRole ) {
devicesOfRole . stream ( )
deviceList . add ( new DeviceIdentifier ( device . getDeviceIdentifier ( ) , device . getType ( ) ) ) ;
. map ( device - > new DeviceIdentifier ( device . getDeviceIdentifier ( ) , device . getType ( ) ) )
}
. forEach ( deviceList : : add ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( devicesOfRole . size ( ) + " found for role: " + role ) ;
log . debug ( devicesOfRole . size ( ) + " found for role: " + role ) ;
}
}
@ -107,7 +138,22 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
"Error when extracting the device list from role[" + role + "]." , e ) ;
"Error when extracting the device list from role[" + role + "]." , e ) ;
}
}
}
}
return installApplication ( applicationUUID , deviceList ) ;
ApplicationInstallResponse response = installApplication ( application , deviceList ) ;
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
String subscriber = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
int applicationReleaseId = application . getApplicationReleases ( ) . get ( 0 ) . getId ( ) ;
try {
ConnectionManagerUtil . openDBConnection ( ) ;
subscriptionDAO . subscribeRoleToApplication ( tenantId , subscriber , roleList , application . getId ( ) ,
applicationReleaseId ) ;
} finally {
ConnectionManagerUtil . closeDBConnection ( ) ;
}
return response ;
}
}
@Override
@Override
@ -116,23 +162,42 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Install application: " + applicationUUID + " to " + deviceGroupList . size ( ) + " groups." ) ;
log . debug ( "Install application: " + applicationUUID + " to " + deviceGroupList . size ( ) + " groups." ) ;
}
}
ApplicationManager applicationManager = ApplicationManagementUtil . getApplicationManagerInstance ( ) ;
Application application = applicationManager . getApplicationByRelease ( applicationUUID ) ;
GroupManagementProviderService groupManagementProviderService = HelperUtil . getGroupManagementProviderService ( ) ;
GroupManagementProviderService groupManagementProviderService = HelperUtil . getGroupManagementProviderService ( ) ;
List < DeviceGroup > groupList = new ArrayList < > ( ) ;
List < DeviceIdentifier > deviceList = new ArrayList < > ( ) ;
List < DeviceIdentifier > deviceList = new ArrayList < > ( ) ;
for ( String groupName : deviceGroupList ) {
for ( String groupName : deviceGroupList ) {
try {
try {
DeviceGroup deviceGroup = groupManagementProviderService . getGroup ( groupName ) ;
DeviceGroup deviceGroup = groupManagementProviderService . getGroup ( groupName ) ;
groupList . add ( deviceGroup ) ;
int deviceCount = groupManagementProviderService . getDeviceCount ( deviceGroup . getGroupId ( ) ) ;
int deviceCount = groupManagementProviderService . getDeviceCount ( deviceGroup . getGroupId ( ) ) ;
List < Device > devicesOfGroups = groupManagementProviderService
List < Device > devicesOfGroups = groupManagementProviderService
. getDevices ( deviceGroup . getGroupId ( ) , 0 , deviceCount ) ;
. getDevices ( deviceGroup . getGroupId ( ) , 0 , deviceCount ) ;
for ( Device device : devicesOfGroups ) {
devicesOfGroups . stream ( )
deviceList . add ( new DeviceIdentifier ( device . getDeviceIdentifier ( ) , device . getType ( ) ) ) ;
. map ( device - > new DeviceIdentifier ( device . getDeviceIdentifier ( ) , device . getType ( ) ) )
}
. forEach ( deviceList : : add ) ;
} catch ( GroupManagementException e ) {
} catch ( GroupManagementException e ) {
throw new ApplicationManagementException (
throw new ApplicationManagementException (
"Error when extracting the device list from group[" + groupName + "]." , e ) ;
"Error when extracting the device list from group[" + groupName + "]." , e ) ;
}
}
}
}
return installApplication ( applicationUUID , deviceList ) ;
ApplicationInstallResponse response = installApplication ( application , deviceList ) ;
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
String subscriber = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
int applicationReleaseId = application . getApplicationReleases ( ) . get ( 0 ) . getId ( ) ;
try {
ConnectionManagerUtil . openDBConnection ( ) ;
subscriptionDAO . subscribeGroupToApplication ( tenantId , subscriber , groupList , application . getId ( ) ,
applicationReleaseId ) ;
} finally {
ConnectionManagerUtil . closeDBConnection ( ) ;
}
return response ;
}
}
@Override
@Override
@ -141,15 +206,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return null ;
return null ;
}
}
private ApplicationInstallResponse installApplication ( String applicationUUID , List < DeviceIdentifier > deviceList )
private ApplicationInstallResponse installApplication ( Application application ,
throws ApplicationManagementException {
List < DeviceIdentifier > deviceIdentifierList ) throws ApplicationManagementException {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
. getDeviceManagementProviderService ( ) ;
. getDeviceManagementProviderService ( ) ;
ApplicationManager applicationManager = ApplicationManagementUtil . getApplicationManagerInstance ( ) ;
ApplicationInstallResponse response = validateDevices ( deviceIdentifierList , application . getType ( ) ) ;
Application application = applicationManager . getApplicationByRelease ( applicationUUID ) ;
/ *
ApplicationInstallResponse response = validateDevices ( deviceList , application . getType ( ) ) ;
Group the valid device list by device type . Following lambda expression produces a map containing device type
as the key and the list of device identifiers as the corresponding value .
* /
Map < String , List < DeviceIdentifier > > deviceTypeIdentifierMap = response . getSuccessfulDevices ( ) . stream ( )
Map < String , List < DeviceIdentifier > > deviceTypeIdentifierMap = response . getSuccessfulDevices ( ) . stream ( )
. collect ( Collectors . groupingBy ( DeviceIdentifier : : getType ) ) ;
. collect ( Collectors . groupingBy ( DeviceIdentifier : : getType ) ) ;
@ -160,6 +226,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
. addOperation ( entry . getKey ( ) , operation , entry . getValue ( ) ) ;
. addOperation ( entry . getKey ( ) , operation , entry . getValue ( ) ) ;
response . setActivity ( activity ) ;
response . setActivity ( activity ) ;
} catch ( OperationManagementException e ) {
} catch ( OperationManagementException e ) {
response . setSuccessfulDevices ( null ) ;
response . setFailedDevices ( deviceIdentifierList ) ;
throw new ApplicationManagementException ( "Error occurred while adding the application install "
throw new ApplicationManagementException ( "Error occurred while adding the application install "
+ "operation to devices" , e ) ;
+ "operation to devices" , e ) ;
} catch ( InvalidDeviceException e ) {
} catch ( InvalidDeviceException e ) {
@ -168,7 +236,24 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
}
}
}
//todo: add device application mapping
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
String subscriber = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
int applicationReleaseId = application . getApplicationReleases ( ) . get ( 0 ) . getId ( ) ;
try {
ConnectionManagerUtil . openDBConnection ( ) ;
List < Device > deviceList = new ArrayList < > ( ) ;
for ( DeviceIdentifier deviceIdentifier : response . getSuccessfulDevices ( ) ) {
try {
deviceList . add ( deviceManagementProviderService . getDevice ( deviceIdentifier ) ) ;
} catch ( DeviceManagementException e ) {
log . error ( "Unable to fetch device for device identifier: " + deviceIdentifier . toString ( ) ) ;
}
}
subscriptionDAO . subscribeDeviceToApplication ( tenantId , subscriber , deviceList , application . getId ( ) ,
applicationReleaseId ) ;
} finally {
ConnectionManagerUtil . closeDBConnection ( ) ;
}
return response ;
return response ;
}
}