Refactoring mobile application subscription API

merge-requests/7/head
Madawa Soysa 7 years ago
parent a0fa98d57b
commit 690eff6660

@ -0,0 +1,85 @@
/*
* Copyright (c) 2018, 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.application.mgt.common;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
public class EnterpriseInstallationDetails {
/**
* This enum represents the type of entities which an application can be installed on.
*
* e.g: An application can be installed on all the devices belong to a user or a specific device group.
*/
@ApiModel
public enum EnterpriseEntity {
USER, ROLE, DEVICE_GROUP
}
@ApiModelProperty(
name = "applicationUUID",
value = "Application ID",
required = true,
example = "4354c752-109f-11e8-b642-0ed5f89f718b"
)
private String applicationUUID;
@ApiModelProperty(
name = "entityType",
value = "Enterprise entity type",
required = true,
example = "USER"
)
private EnterpriseEntity entityType;
@ApiModelProperty(
name = "entityValueList",
value = "List of users/roles or device groups.",
required = true,
example = "user1,user2, user3"
)
private List<String> entityValueList;
public String getApplicationUUID() {
return applicationUUID;
}
public void setApplicationUUID(String applicationUUID) {
this.applicationUUID = applicationUUID;
}
public EnterpriseEntity getEntityType() {
return entityType;
}
public void setEntityType(EnterpriseEntity entityType) {
this.entityType = entityType;
}
public List<String> getEntityValueList() {
return entityValueList;
}
public void setEntityValueList(List<String> entityValueList) {
this.entityValueList = entityValueList;
}
}

@ -25,30 +25,16 @@ public class InstallationDetails {
@ApiModelProperty( @ApiModelProperty(
name = "applicationUUID", name = "applicationUUID",
value = "Application ID", value = "Application ID",
required = true) required = true
)
private String applicationUUID; private String applicationUUID;
@ApiModelProperty(
name = "versionName",
value = "Version name",
required = true)
private String versionName;
@ApiModelProperty(
name = "userNameList",
value = "List of user names.",
required = true)
private List<String> userNameList;
@ApiModelProperty(
name = "roleNameList",
value = "List of role names.",
required = true)
private List<String> roleNameList;
@ApiModelProperty( @ApiModelProperty(
name = "deviceIdentifiers", name = "deviceIdentifiers",
value = "List of device identifiers.", value = "List of device identifiers.",
required = true, required = true,
dataType = "List[org.wso2.carbon.device.mgt.common.DeviceIdentifier]") dataType = "List[org.wso2.carbon.device.mgt.common.DeviceIdentifier]"
)
private List<DeviceIdentifier> deviceIdentifiers; private List<DeviceIdentifier> deviceIdentifiers;
public String getApplicationUUID() { public String getApplicationUUID() {
@ -59,30 +45,6 @@ public class InstallationDetails {
this.applicationUUID = applicationUUID; this.applicationUUID = applicationUUID;
} }
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public List<String> getUserNameList() {
return userNameList;
}
public void setUserNameList(List<String> userNameList) {
this.userNameList = userNameList;
}
public List<String> getRoleNameList() {
return roleNameList;
}
public void setRoleNameList(List<String> roleNameList) {
this.roleNameList = roleNameList;
}
public List<DeviceIdentifier> getDeviceIdentifiers() { public List<DeviceIdentifier> getDeviceIdentifiers() {
return deviceIdentifiers; return deviceIdentifiers;
} }

@ -18,10 +18,15 @@
*/ */
package org.wso2.carbon.device.application.mgt.common.services; package org.wso2.carbon.device.application.mgt.common.services;
import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.LifecycleStateTransition;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
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.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import java.util.List; import java.util.List;

@ -29,35 +29,42 @@ import java.util.List;
public interface SubscriptionManager { public interface SubscriptionManager {
/** /**
* To install an application to given list of devices. * To install an application to given list of devices.
* @param applicationUUID Application ID * @param applicationUUID ID of the application to install
* @param deviceList Device list * @param deviceList list of device ID's to install the application
* @return Failed Device List which the application was unable to install * @return Failed Device List which the application was unable to install
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException if unable to install the application to the given devices
*/ */
List<DeviceIdentifier> installApplicationForDevices(String applicationUUID, String versionName, List<DeviceIdentifier> installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList)
List<DeviceIdentifier> deviceList)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**
* To install an application to given list of users. * To install an application to given list of users.
* @param applicationUUID Application ID * @param applicationUUID ID of the application to install
* @param userList User list * @param userList list of users to install the application
* @return Failed Device List which the application was unable to install * @return Failed Device List which the application was unable to install
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException if unable to install the application to devices belong to given users
*/ */
List<DeviceIdentifier> installApplicationForUsers(String applicationUUID, List<DeviceIdentifier> installApplicationForUsers(String applicationUUID, List<String> userList)
List<String> userList, String versionName)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**
* To install an application to given list of users. * To install an application to given list of roles.
* @param applicationUUID Application ID * @param applicationUUID ID of the application to install
* @param roleList Role list * @param roleList list of roles to install the application
* @return Failed Device List which the application was unable to install * @return Failed Device List which the application was unable to install
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException if unable to install the application to devices belong to given roles
*/ */
List<DeviceIdentifier> installApplicationForRoles(String applicationUUID, List<DeviceIdentifier> installApplicationForRoles(String applicationUUID, List<String> roleList)
List<String> roleList, String versionName) throws ApplicationManagementException;
/**
* To install an application to given list of roles.
* @param applicationUUID ID of the application to install
* @param deviceGroupList list of device groups to install the application
* @return Failed Device List which the application was unable to install
* @throws ApplicationManagementException if unable to install the application to devices belong to given groups
*/
List<DeviceIdentifier> installApplicationForGroups(String applicationUUID, List<String> deviceGroupList)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**
@ -67,8 +74,6 @@ public interface SubscriptionManager {
* @return Failed Device List which the application was unable to uninstall * @return Failed Device List which the application was unable to uninstall
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException Application Management Exception
*/ */
List<DeviceIdentifier> uninstallApplication(String applicationUUID, List<DeviceIdentifier> uninstallApplication(String applicationUUID, List<DeviceIdentifier> deviceList)
List<DeviceIdentifier> deviceList)
throws ApplicationManagementException; throws ApplicationManagementException;
} }

@ -23,7 +23,15 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.LifecycleStateTransition;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
import org.wso2.carbon.device.application.mgt.common.User;
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.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException;
@ -38,16 +46,17 @@ import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.exception.ValidationException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import java.util.ArrayList;
import java.util.List;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* Default Concrete implementation of Application Management related implementations. * Default Concrete implementation of Application Management related implementations.

@ -19,19 +19,27 @@ 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.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.application.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.ApplicationReleaseManager;
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.util.ApplicationManagementUtil;
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.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
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.app.mgt.DeviceApplicationMapping; import org.wso2.carbon.device.mgt.common.app.mgt.DeviceApplicationMapping;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -45,106 +53,103 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
private static final String INSTALL_APPLICATION = "INSTALL_APPLICATION"; private static final String INSTALL_APPLICATION = "INSTALL_APPLICATION";
@Override @Override
public List<DeviceIdentifier> installApplicationForDevices(String applicationUUID, String versionName, public List<DeviceIdentifier> installApplicationForDevices(String applicationUUID,
List<DeviceIdentifier> deviceList) List<DeviceIdentifier> deviceList) throws ApplicationManagementException {
throws ApplicationManagementException { if (log.isDebugEnabled()) {
return installApplication(applicationUUID, deviceList, versionName); log.debug("Install application: " + applicationUUID + " to " + deviceList.size() + "devices.");
}
return installApplication(applicationUUID, deviceList);
} }
@Override @Override
public List<DeviceIdentifier> installApplicationForUsers(String applicationUUID, List<String> userList, public List<DeviceIdentifier> installApplicationForUsers(String applicationUUID, List<String> userList)
String versionName) throws ApplicationManagementException { throws ApplicationManagementException {
log.info("Install application: " + applicationUUID + " to: " + userList.size() + " users."); if (log.isDebugEnabled()) {
log.debug("Install application: " + applicationUUID + " to " + userList.size() + " users.");
}
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) { for (Device device : devicesOfUser) {
deviceList.add(new DeviceIdentifier(device deviceList.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
.getDeviceIdentifier(), device.getType())); }
if (log.isDebugEnabled()) {
log.debug(devicesOfUser.size() + " found for the provided user list");
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Error when extracting the device list from user[" + user + "].", e); throw new ApplicationManagementException("Error when extracting the device list of user[" + user + "].",
e);
} }
} }
return installApplication(applicationUUID, deviceList, versionName); return installApplication(applicationUUID, deviceList);
} }
@Override @Override
public List<DeviceIdentifier> installApplicationForRoles(String applicationUUID, List<String> roleList, public List<DeviceIdentifier> installApplicationForRoles(String applicationUUID, List<String> roleList)
String versionName) throws ApplicationManagementException { throws ApplicationManagementException {
log.info("Install application: " + applicationUUID + " to: " + roleList.size() + " roles."); if (log.isDebugEnabled()) {
log.debug("Install application: " + applicationUUID + " to " + roleList.size() + " roles.");
}
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) { for (Device device : devicesOfRole) {
deviceList.add(new DeviceIdentifier(device deviceList.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
.getDeviceIdentifier(), device.getType())); }
if (log.isDebugEnabled()) {
log.debug(devicesOfRole.size() + " found for role: " + role);
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Error when extracting the device list from role[" + role + "].", e); throw new ApplicationManagementException(
"Error when extracting the device list from role[" + role + "].", e);
}
}
return installApplication(applicationUUID, deviceList);
}
@Override
public List<DeviceIdentifier> installApplicationForGroups(String applicationUUID, List<String> deviceGroupList)
throws ApplicationManagementException {
if (log.isDebugEnabled()) {
log.debug("Install application: " + applicationUUID + " to " + deviceGroupList.size() + " groups.");
}
GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService();
List<DeviceIdentifier> deviceList = new ArrayList<>();
for (String groupName : deviceGroupList) {
try {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(groupName);
int deviceCount = groupManagementProviderService.getDeviceCount(deviceGroup.getGroupId());
List<Device> devicesOfGroups = groupManagementProviderService
.getDevices(deviceGroup.getGroupId(), 0, deviceCount);
for (Device device : devicesOfGroups) {
deviceList.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
}
} catch (GroupManagementException e) {
throw new ApplicationManagementException(
"Error when extracting the device list from group[" + groupName + "].", e);
} }
} }
return installApplication(applicationUUID, deviceList, versionName); return installApplication(applicationUUID, deviceList);
} }
@Override @Override
public List<DeviceIdentifier> uninstallApplication(String applicationUUID, public List<DeviceIdentifier> uninstallApplication(String applicationUUID, List<DeviceIdentifier> deviceList)
List<DeviceIdentifier> deviceList)
throws ApplicationManagementException { throws ApplicationManagementException {
return null; return null;
} }
private List<DeviceIdentifier> installApplication(String applicationUUID, List<DeviceIdentifier> deviceList, private List<DeviceIdentifier> installApplication(String applicationUUID, List<DeviceIdentifier> deviceList)
String versionName) throws ApplicationManagementException { throws ApplicationManagementException {
//todo: 1. get application and release.
ApplicationReleaseManager applicationReleaseManager = ApplicationManagementUtil
.getApplicationReleaseManagerInstance();
ApplicationRelease applicationRelease = applicationReleaseManager.getReleaseByUuid(applicationUUID);
//todo: 2. check type and filter devices.
//todo: 3. generate url based on application attributes and app release attributes
//Todo: check if app type is installable for all the device types: apk -> android, ipa -> ios, webclip -> both
return null; return null;
// List<DeviceIdentifier> failedDeviceList = new ArrayList<>(deviceList);
// // Todo: try whether we can optimise this by sending bulk inserts to db
// // Todo: atomicity is not maintained as deveice managment provider service uses separate db connection. fix this??
// log.info("Install application: " + applicationUUID + "[" + versionName + "]" + " to: "
// + deviceList.size() + " devices.");
// for (DeviceIdentifier device : deviceList) {
// org.wso2.carbon.device.mgt.common.DeviceIdentifier deviceIdentifier = new org.wso2.carbon.device.mgt
// .common.DeviceIdentifier(device.getId(), device.getType());
// try {
// DeviceManagementProviderService dmpService = HelperUtil.getDeviceManagementProviderService();
// if (!dmpService.isEnrolled(deviceIdentifier)) {
// log.error("Device with ID: [" + device.getId() + "] is not enrolled to install the application.");
// } else {
// if (log.isDebugEnabled()) {
// log.debug("Installing application to : " + device.getId());
// }
// //Todo: generating one time download link for the application and put install operation to device.
//
// // put app install operation to the device
// ProfileOperation operation = new ProfileOperation();
// operation.setCode(INSTALL_APPLICATION);
// operation.setType(Operation.Type.PROFILE);
// operation.setPayLoad("{'type':'enterprise', 'url':'http://10.100.5.76:8000/app-debug.apk', 'app':'"
// + applicationUUID + "'}");
// List<org.wso2.carbon.device.mgt.common.DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
// deviceIdentifiers.add(deviceIdentifier);
// dmpService.addOperation(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID,
// operation, deviceIdentifiers);
//
// DeviceApplicationMapping deviceApp = new DeviceApplicationMapping();
// deviceApp.setDeviceIdentifier(device.getId());
// deviceApp.setApplicationUUID(applicationUUID);
// deviceApp.setVersionName(versionName);
// deviceApp.setInstalled(false);
// dmpService.addDeviceApplicationMapping(deviceApp);
//// DeviceManagementDAOFactory.openConnection();
//// ApplicationManagementDAOFactory.getSubscriptionDAO().addDeviceApplicationMapping(device.getId(), applicationUUID, false);
// failedDeviceList.remove(device);
// }
// } catch (DeviceManagementException | OperationManagementException | InvalidDeviceException e) {
// log.error("Error while installing application to device[" + deviceIdentifier.getId() + "]", e);
// }
// finally {
// DeviceManagementDAOFactory.closeConnection();
// }
// }
// return failedDeviceList;
} }
} }

@ -21,8 +21,11 @@ package org.wso2.carbon.device.application.mgt.core.util;
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.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import java.util.List;
import java.util.UUID; import java.util.UUID;
/** /**
@ -33,6 +36,7 @@ public class HelperUtil {
private static Log log = LogFactory.getLog(HelperUtil.class); private static Log log = LogFactory.getLog(HelperUtil.class);
private static DeviceManagementProviderService deviceManagementProviderService; private static DeviceManagementProviderService deviceManagementProviderService;
private static GroupManagementProviderService groupManagementProviderService;
public static String generateApplicationUuid() { public static String generateApplicationUuid() {
return UUID.randomUUID().toString(); return UUID.randomUUID().toString();
@ -56,4 +60,21 @@ public class HelperUtil {
return deviceManagementProviderService; return deviceManagementProviderService;
} }
public static GroupManagementProviderService getGroupManagementProviderService() {
if (groupManagementProviderService == null) {
synchronized (HelperUtil.class) {
if (groupManagementProviderService == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
groupManagementProviderService = (GroupManagementProviderService) ctx
.getOSGiService(GroupManagementProviderService.class, null);
if (groupManagementProviderService == null) {
String msg = "Group management provider service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
}
}
}
return groupManagementProviderService;
}
} }

@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.publisher.api.beans;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorListItem;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

@ -17,14 +17,28 @@
*/ */
package org.wso2.carbon.device.application.mgt.publisher.api.services; package org.wso2.carbon.device.application.mgt.publisher.api.services;
import io.swagger.annotations.*; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.Info;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorResponse;
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.EnterpriseInstallationDetails;
import org.wso2.carbon.device.application.mgt.common.InstallationDetails; import org.wso2.carbon.device.application.mgt.common.InstallationDetails;
import javax.validation.Valid; import javax.validation.Valid;
import javax.ws.rs.*; import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -43,9 +57,8 @@ import javax.ws.rs.core.Response;
} }
), ),
tags = { tags = {
@Tag(name = "subscription_management, device_management", description = "Subscription Management " + @Tag(name = "subscription_management, device_management", description = "Subscription Management "
"related " + "related APIs")
+ "APIs")
} }
) )
@Scopes( @Scopes(
@ -82,7 +95,7 @@ public interface SubscriptionManagementAPI {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "POST", httpMethod = "POST",
value = "Install an application", value = "Install an application",
notes = "This will install an application to a given list of devices/users/roles", notes = "This will install an application to a given list of devices",
tags = "Subscription Management", tags = "Subscription Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -95,23 +108,73 @@ public interface SubscriptionManagementAPI {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully installed the application.", message = "OK. \n Successfully installed the application.",
response = Application.class), response = Application.class
),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n " + message = "Not Modified. \n Empty body because the application is already installed."
"Empty body because the application is already installed."), ),
@ApiResponse(
code = 404,
message = "Not Found. \n Application cannot be found to install."
),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while installing the application.", message = "Internal Server Error. \n Error occurred while installing the application."
response = ErrorResponse.class) )
}) })
Response installApplication( Response installApplication(
@ApiParam( @ApiParam(
name = "installationDetails", name = "installationDetails",
value = "The application ID and list of devices/users/roles", value = "The application ID and list of devices/users/roles",
required = true) required = true
)
@Valid InstallationDetails installationDetails); @Valid InstallationDetails installationDetails);
@POST
@Path("/enterprise-install-application")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Install an application to the devices belong to an enterprise entity",
notes = "This will install an application to a given list of groups/users/roles",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:install")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully installed the application.",
response = Application.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the application is already installed."
),
@ApiResponse(
code = 404,
message = "Not Found. \n Application cannot be found to install."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while installing the application."
)
})
Response enterpriseInstallApplication(
@ApiParam(
name = "enterpriseInstallationDetails",
value = "The application ID and list of devices/users/roles",
required = true)
@Valid EnterpriseInstallationDetails enterpriseInstallationDetails);
@POST @POST
@Path("/uninstall-application") @Path("/uninstall-application")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -121,7 +184,7 @@ public interface SubscriptionManagementAPI {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "POST", httpMethod = "POST",
value = "Uninstall an application", value = "Uninstall an application",
notes = "This will uninstall an application to a given list of devices/users/roles", notes = "This will uninstall an application from given list of devices",
tags = "Subscription Management", tags = "Subscription Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -134,23 +197,73 @@ public interface SubscriptionManagementAPI {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully uninstalled the application.", message = "OK. \n Successfully uninstalled the application.",
response = Application.class), response = Application.class
),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n " + message = "Not Modified. \n Empty body because the application is already uninstalled."
"Empty body because the application is already uninstalled."), ),
@ApiResponse(
code = 404,
message = "Not Found. \n Application cannot be found to uninstall."
),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while installing the application.", message = "Internal Server Error. \n Error occurred while uninstalling the application."
response = ErrorResponse.class) )
}) })
Response uninstallApplication( Response uninstallApplication(
@ApiParam( @ApiParam(
name = "installationDetails", name = "installationDetails",
value = "The application ID and list of devices/users/roles", value = "The application ID and list of devices",
required = true) required = true)
@Valid InstallationDetails installationDetails); @Valid InstallationDetails installationDetails);
@POST
@Path("/enterprise-uninstall-application")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Uninstall an application from the devices belong to an enterprise entity",
notes = "This will uninstall an application from devices belong to given list of groups/users/roles",
tags = "Subscription Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:subscription:uninstall")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully uninstalled the application.",
response = Application.class
),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the application is already uninstalled."
),
@ApiResponse(
code = 404,
message = "Not Found. \n Application cannot be found to uninstall."
),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while uninstalling the application."
)
})
Response enterpriseUninstallApplication(
@ApiParam(
name = "enterpriseInstallationDetails",
value = "The application ID and list of groups/users/roles",
required = true
)
@Valid EnterpriseInstallationDetails enterpriseInstallationDetails);
@GET @GET
@Path("/application/{applicationUUID}/device/{deviceId}") @Path("/application/{applicationUUID}/device/{deviceId}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -173,23 +286,27 @@ public interface SubscriptionManagementAPI {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully installed the application.", message = "OK. \n Successfully installed the application.",
response = Application.class), response = Application.class
),
@ApiResponse( @ApiResponse(
code = 304, code = 304,
message = "Not Modified. \n " + message = "Not Modified. \n " +
"Empty body because the application is already installed."), "Empty body because the application is already installed."
),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while fetching the application.", message = "Internal Server Error. \n Error occurred while fetching the application."
response = ErrorResponse.class) )
}) })
Response getApplication( Response getApplication(
@ApiParam( @ApiParam(
name = "applicationUUID", name = "applicationUUID",
value = "Application ID") value = "Application ID"
)
@QueryParam("applicationUUID") String applicationUUID, @QueryParam("applicationUUID") String applicationUUID,
@ApiParam( @ApiParam(
name = "deviceId", name = "deviceId",
value = "The device ID") value = "The device ID"
)
@QueryParam("deviceId") String deviceId); @QueryParam("deviceId") String deviceId);
} }

@ -20,6 +20,8 @@ package org.wso2.carbon.device.application.mgt.store.api.services.impl;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
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.device.application.mgt.common.EnterpriseInstallationDetails;
import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorResponse;
import org.wso2.carbon.device.application.mgt.store.api.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.APIUtil;
import org.wso2.carbon.device.application.mgt.publisher.api.services.SubscriptionManagementAPI; import org.wso2.carbon.device.application.mgt.publisher.api.services.SubscriptionManagementAPI;
import org.wso2.carbon.device.application.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.application.mgt.common.DeviceIdentifier;
@ -34,6 +36,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
/** /**
* Implementation of Subscription Management related APIs. * Implementation of Subscription Management related APIs.
@ -47,33 +50,78 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
@Override @Override
@POST @POST
@Path("/install-application") @Path("/install-application")
public Response installApplication(@ApiParam(name = "installationDetails", value = "The application ID and list" + public Response installApplication(@ApiParam(name = "installationDetails", value = "Application ID and list of" +
" the devices/users/roles", required = true) @Valid InstallationDetails installationDetails) { "devices", required = true) @Valid InstallationDetails installationDetails) {
Object result;
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
String applicationUUID = installationDetails.getApplicationUUID();
if (applicationUUID.isEmpty() || installationDetails.getDeviceIdentifiers().isEmpty()) {
String msg = "Some or all data in the incoming request is empty. Therefore unable to proceed with the "
+ "installation.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
try { try {
String applicationUUID = installationDetails.getApplicationUUID(); List<DeviceIdentifier> failedDevices = subscriptionManager.installApplicationForDevices(applicationUUID,
String versionName = installationDetails.getVersionName(); installationDetails.getDeviceIdentifiers());
if (!installationDetails.getDeviceIdentifiers().isEmpty()) { HashMap<String, Object> response = new HashMap<>();
List<DeviceIdentifier> deviceList = installationDetails.getDeviceIdentifiers(); response.put("failedDevices", failedDevices);
result = subscriptionManager.installApplicationForDevices(applicationUUID, versionName, deviceList); return Response.status(Response.Status.OK).entity(response).build();
} else if (!installationDetails.getUserNameList().isEmpty()) { } catch (ApplicationManagementException e) {
List<String> userList = installationDetails.getUserNameList(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
result = subscriptionManager.installApplicationForUsers(applicationUUID, userList, versionName); .entity("Error occurred while installing the application for devices" + ": " + e.getMessage())
} else if (!installationDetails.getRoleNameList().isEmpty()) { .build();
List<String> roleList = installationDetails.getRoleNameList(); }
result = subscriptionManager.installApplicationForRoles(applicationUUID, roleList, versionName); }
@Override
public Response enterpriseInstallApplication(EnterpriseInstallationDetails enterpriseInstallationDetails) {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
String msg;
String applicationUUID = enterpriseInstallationDetails.getApplicationUUID();
EnterpriseInstallationDetails.EnterpriseEntity enterpriseEntity = enterpriseInstallationDetails.getEntityType();
List<String> entityValueList = enterpriseInstallationDetails.getEntityValueList();
List<DeviceIdentifier> failedDevices;
if (applicationUUID.isEmpty()) {
msg = "Application UUID is empty in the incoming request. Therefore unable to proceed with the "
+ "installation.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
if (enterpriseEntity == null || entityValueList.isEmpty()) {
msg = "Some or all details of the entity is empty in the incoming request. Therefore unable to proceed "
+ "with the installation.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
try{
if (EnterpriseInstallationDetails.EnterpriseEntity.USER.equals(enterpriseEntity)) {
failedDevices = subscriptionManager
.installApplicationForUsers(applicationUUID, entityValueList);
} else if (EnterpriseInstallationDetails.EnterpriseEntity.ROLE.equals(enterpriseEntity)) {
failedDevices = subscriptionManager
.installApplicationForRoles(applicationUUID, entityValueList);
} else if (EnterpriseInstallationDetails.EnterpriseEntity.DEVICE_GROUP.equals(enterpriseEntity)) {
failedDevices = subscriptionManager
.installApplicationForGroups(applicationUUID, entityValueList);
} else { } else {
result = "Missing request data!"; msg = "Entity type does not match either USER, ROLE or DEVICE_GROUP. Therefore unable to proceed with "
return Response.status(Response.Status.BAD_REQUEST).entity(result).build(); + "the installation";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} }
HashMap<String, Object> response = new HashMap<>(); HashMap<String, Object> response = new HashMap<>();
response.put("failedDevices", result); response.put("failedDevices", failedDevices);
return Response.status(Response.Status.OK).entity(response).build(); return Response.status(Response.Status.OK).entity(response).build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while installing the application"; return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
log.error(msg, e); .entity("Error occurred while installing the application for devices" + ": " + e.getMessage())
return Response.status(Response.Status.BAD_REQUEST).build(); .build();
} }
} }
@ -83,6 +131,12 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
return null; return null;
} }
@Override
public Response enterpriseUninstallApplication(
EnterpriseInstallationDetails enterpriseInstallationDetails) {
return null;
}
@Override @Override
public Response getApplication(@ApiParam(name = "applicationUUID", value = "Application ID") String public Response getApplication(@ApiParam(name = "applicationUUID", value = "Application ID") String
applicationUUID, @ApiParam(name = "deviceId", value = "The device ID") applicationUUID, @ApiParam(name = "deviceId", value = "The device ID")

Loading…
Cancel
Save