Merge pull request #6 from ayyoob/merge-devicetype

merge device type
revert-70aa11f8
ayyoob 9 years ago
commit 81ae512f13

@ -0,0 +1,69 @@
/*
* 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.common;
import java.io.Serializable;
/**
* This class holds the information of the device type and its provider tenant.
*/
public class DeviceTypeIdentifier implements Serializable {
private String deviceType;
private int tenantId;
private static final int DEFAULT_SHARE_WITH_ALL_TENANTS_ID = -1;
public DeviceTypeIdentifier(String deviceType, int tenantId) {
this.deviceType = deviceType;
this.tenantId = tenantId;
}
public DeviceTypeIdentifier(String deviceType) {
this.deviceType = deviceType;
this.tenantId = DEFAULT_SHARE_WITH_ALL_TENANTS_ID;
}
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
public String getDeviceType() {
return this.deviceType;
}
public int getTenantId() {
return this.tenantId;
}
@Override
public int hashCode() {
int result = this.deviceType.hashCode();
result = 31 * result + ("@" + this.tenantId).hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
return (obj instanceof DeviceTypeIdentifier) && deviceType.equals(
((DeviceTypeIdentifier) obj).deviceType) && tenantId == ((DeviceTypeIdentifier) obj).tenantId;
}
public boolean isSharedWithAllTenant() {
return tenantId == DEFAULT_SHARE_WITH_ALL_TENANTS_ID;
}
}

@ -39,6 +39,19 @@ public interface DeviceManagementService extends ApplicationManager {
*/
String getType();
/**
* This returns the tenant domain of the provider.
* @return
*/
String getProviderTenantDomain();
/**
* returns true if the device type is shared between all tenants and false if its not.
*
* @return
*/
boolean isSharedWithAllTenants();
void init() throws DeviceManagementException;
DeviceManager getDeviceManager();

@ -83,7 +83,8 @@
org.apache.catalina.core,
org.apache.commons.collections,
org.wso2.carbon.email.sender.*,
org.wso2.carbon
org.wso2.carbon,
org.wso2.carbon.base
</Import-Package>
<Export-Package>
!org.wso2.carbon.device.mgt.core.internal,

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
@ -32,23 +33,29 @@ import java.util.Map;
public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
private Map<String, DeviceManagementService> providers;
private Map<DeviceTypeIdentifier, DeviceManagementService> providers;
private boolean isInited;
private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class);
public DeviceManagementPluginRepository() {
providers = Collections.synchronizedMap(new HashMap<String, DeviceManagementService>());
providers = Collections.synchronizedMap(new HashMap<DeviceTypeIdentifier, DeviceManagementService>());
DeviceManagementServiceComponent.registerStartupListener(this);
}
public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
String deviceType = provider.getType();
String tenantDomain = provider.getProviderTenantDomain();
boolean isSharedWithAllTenants = provider.isSharedWithAllTenants();
int tenantId = DeviceManagerUtil.getTenantId(tenantDomain);
if (tenantId == -1) {
throw new DeviceManagementException("No tenant available for tenant domain " + tenantDomain);
}
synchronized (providers) {
try {
if (isInited) {
/* Initializing Device Management Service Provider */
provider.init();
DeviceManagerUtil.registerDeviceType(deviceType);
DeviceManagerUtil.registerDeviceType(deviceType, tenantId, isSharedWithAllTenants);
DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType,
provider.getDeviceManager().requireDeviceAuthorization());
@ -57,20 +64,47 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
throw new DeviceManagementException("Error occurred while adding device management provider '" +
deviceType + "'", e);
}
providers.put(deviceType, provider);
if (isSharedWithAllTenants) {
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType);
providers.put(deviceTypeIdentifier, provider);
} else {
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType, tenantId);
providers.put(deviceTypeIdentifier, provider);
}
}
}
public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
providers.remove(provider.getType());
String deviceTypeName=provider.getType();
if(provider.isSharedWithAllTenants()){
DeviceTypeIdentifier deviceTypeIdentifier =new DeviceTypeIdentifier(deviceTypeName);
providers.remove(deviceTypeIdentifier);
}else{
int providerTenantId=DeviceManagerUtil.getTenantId(provider.getProviderTenantDomain());
DeviceTypeIdentifier deviceTypeIdentifier =new DeviceTypeIdentifier(deviceTypeName, providerTenantId);
providers.remove(deviceTypeIdentifier);
}
}
public DeviceManagementService getDeviceManagementService(String type) {
return providers.get(type);
public DeviceManagementService getDeviceManagementService(String type, int tenantId) {
//Priority need to be given to the tenant before public.
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(type, tenantId);
DeviceManagementService provider = providers.get(deviceTypeIdentifier);
if (provider == null) {
deviceTypeIdentifier = new DeviceTypeIdentifier(type);
provider = providers.get(deviceTypeIdentifier);
}
return provider;
}
public Map<String, DeviceManagementService> getAllDeviceManagementServices() {
return providers;
public Map<DeviceTypeIdentifier, DeviceManagementService> getAllDeviceManagementServices(int tenantId) {
Map<DeviceTypeIdentifier, DeviceManagementService> tenantProviders = new HashMap<>();
for (DeviceTypeIdentifier identifier : providers.keySet()) {
if (identifier.getTenantId() == tenantId || identifier.isSharedWithAllTenant()) {
tenantProviders.put(identifier, providers.get(identifier));
}
}
return tenantProviders;
}
@Override
@ -79,7 +113,8 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
for (DeviceManagementService provider : providers.values()) {
try {
provider.init();
DeviceManagerUtil.registerDeviceType(provider.getType());
int tenantId=DeviceManagerUtil.getTenantId(provider.getProviderTenantDomain());
DeviceManagerUtil.registerDeviceType(provider.getType(), tenantId, provider.isSharedWithAllTenants());
//TODO:
//This is a temporory fix.
//windows and IOS cannot resolve user info by extracting certs

@ -27,18 +27,64 @@ import java.util.List;
*/
public interface DeviceTypeDAO {
void addDeviceType(DeviceType deviceType) throws DeviceManagementDAOException;
void updateDeviceType(DeviceType deviceType) throws DeviceManagementDAOException;
/**
* @param deviceType device that needs to be added
* @param providerTenantId provider tenant id whom the device type is associated with.
* @param isSharedWithAllTenants is this a shared device type or not.
* @throws DeviceManagementDAOException
*/
void addDeviceType(DeviceType deviceType, int providerTenantId, boolean isSharedWithAllTenants)
throws DeviceManagementDAOException;
List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException;
/**
* @param deviceType deviceType that needs to be updated.
* @param providerTenantId provider tenant id whom the device type is associated with.
* @throws DeviceManagementDAOException
*/
void updateDeviceType(DeviceType deviceType, int providerTenantId) throws DeviceManagementDAOException;
/**
* @param tenantId get device type detail of a specific tenant.
* @return list of all device types that are associated with the tenant this includes the shared device types.
* @throws DeviceManagementDAOException
*/
List<DeviceType> getDeviceTypes(int tenantId) throws DeviceManagementDAOException;
/**
* @param tenandId of the device type provider.
* @return return only the device types that are associated with the provider tenant.
* @throws DeviceManagementDAOException
*/
List<DeviceType> getDeviceTypesByProvider(int tenandId) throws DeviceManagementDAOException;
/**
* @return sharedWithAllDeviceTypes This returns public shared device types.
* @throws DeviceManagementDAOException
*/
List<DeviceType> getSharedDeviceTypes() throws DeviceManagementDAOException;
/**
* @param id retrieve the device type with its id.
* @return the device type associated with the id.
* @throws DeviceManagementDAOException
*/
DeviceType getDeviceType(int id) throws DeviceManagementDAOException;
DeviceType getDeviceType(String name) throws DeviceManagementDAOException;
/**
* @param name retreive the device type with it name.
* @param tenantId retreive the device type with its tenant id.
* @return the device type associated with its name and tenant id.
* @throws DeviceManagementDAOException
*/
DeviceType getDeviceType(String name, int tenantId) throws DeviceManagementDAOException;
void removeDeviceType(String type) throws DeviceManagementDAOException;
/**
* remove the device type from tenant.
*
* @param name remove the device type with it name.
* @param tenantId remove the device type with its tenant id.
* @throws DeviceManagementDAOException
*/
void removeDeviceType(String name, int tenantId) throws DeviceManagementDAOException;
}

@ -33,37 +33,44 @@ import java.util.List;
public class DeviceTypeDAOImpl implements DeviceTypeDAO {
@Override
public void addDeviceType(DeviceType deviceType) throws DeviceManagementDAOException {
public void addDeviceType(DeviceType deviceType, int providerTenantId, boolean isSharedWithAllTenants)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_TYPE (NAME) VALUES (?)");
stmt = conn.prepareStatement(
"INSERT INTO DM_DEVICE_TYPE (NAME,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS) VALUES (?,?,?)");
stmt.setString(1, deviceType.getName());
stmt.setInt(2, providerTenantId);
stmt.setBoolean(3, isSharedWithAllTenants);
stmt.execute();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while registering the device type " +
"'" + deviceType.getName() + "'", e);
throw new DeviceManagementDAOException(
"Error occurred while registering the device type '" + deviceType.getName() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void updateDeviceType(DeviceType deviceType) throws DeviceManagementDAOException {
public void updateDeviceType(DeviceType deviceType, int tenantId)
throws DeviceManagementDAOException {
}
@Override
public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException {
public List<DeviceType> getDeviceTypes(int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE";
String sql =
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =" +
"? OR SHARED_WITH_ALL_TENANTS = TRUE";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
rs = stmt.executeQuery();
while (rs.next()) {
@ -81,7 +88,7 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
}
@Override
public List<DeviceType> getDeviceTypes(int tenantId) throws DeviceManagementDAOException {
public List<DeviceType> getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
@ -89,8 +96,7 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
try {
conn = this.getConnection();
String sql =
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =" +
"? OR SHARED_WITH_ALL_TENANTS = TRUE";
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
rs = stmt.executeQuery();
@ -109,6 +115,34 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
}
}
@Override
public List<DeviceType> getSharedDeviceTypes() throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<DeviceType> deviceTypes = new ArrayList<>();
try {
conn = this.getConnection();
String sql =
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where " +
"SHARED_WITH_ALL_TENANTS = TRUE";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
DeviceType deviceType = new DeviceType();
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(rs.getString("DEVICE_TYPE"));
deviceTypes.add(deviceType);
}
return deviceTypes;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public DeviceType getDeviceType(int id) throws DeviceManagementDAOException {
Connection conn;
@ -119,7 +153,6 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE WHERE ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
DeviceType deviceType = null;
while (rs.next()) {
@ -129,41 +162,44 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
}
return deviceType;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the registered device type", e);
throw new DeviceManagementDAOException(
"Error occurred while fetching the registered device type", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public DeviceType getDeviceType(String type) throws DeviceManagementDAOException {
public DeviceType getDeviceType(String type, int tenantId) throws
DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
DeviceType deviceType = null;
try {
conn = this.getConnection();
String sql = "SELECT ID From DM_DEVICE_TYPE WHERE NAME = ?";
String sql = "SELECT ID AS DEVICE_TYPE_ID FROM DM_DEVICE_TYPE WHERE (PROVIDER_TENANT_ID =? OR " +
"SHARED_WITH_ALL_TENANTS = TRUE) AND NAME =?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setInt(1, tenantId);
stmt.setString(2, type);
rs = stmt.executeQuery();
if (rs.next()) {
deviceType = new DeviceType();
deviceType.setId(rs.getInt("ID"));
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(type);
}
return deviceType;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetch device type id for device type " +
"'" + type + "'", e);
throw new DeviceManagementDAOException(
"Error occurred while fetch device type id for device type '" + type + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public void removeDeviceType(String type) throws DeviceManagementDAOException {
public void removeDeviceType(String type, int tenantId) throws DeviceManagementDAOException {
}

@ -67,7 +67,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override
public boolean saveConfiguration(TenantConfiguration configuration) throws DeviceManagementException {
DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(configuration.getType()).getDeviceManager();
this.getPluginRepository().getDeviceManagementService(configuration.getType(), this.getTenantId()).getDeviceManager();
return dms.saveConfiguration(configuration);
}
@ -79,7 +79,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override
public TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException {
DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager();
this.getPluginRepository().getDeviceManagementService(deviceType, this.getTenantId()).getDeviceManager();
if (dms == null) {
if (log.isDebugEnabled()) {
log.debug("Device type '" + deviceType + "' does not have an associated device management " +
@ -185,7 +185,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
int enrolmentId = 0;
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType());
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId);
enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId);
DeviceManagementDAOFactory.commitTransaction();
@ -226,7 +226,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try {
int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType());
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
deviceDAO.updateDevice(type.getId(), device, tenantId);
enrollmentDAO.updateEnrollment(device.getEnrolmentInfo());
DeviceManagementDAOFactory.commitTransaction();
@ -287,7 +287,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
return false;
}
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED);
@ -621,22 +621,40 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override
public List<DeviceType> getAvailableDeviceTypes() throws DeviceManagementException {
List<DeviceType> deviceTypesInDatabase;
List<DeviceType> deviceTypesProvidedByTenant;
List<DeviceType> publicSharedDeviceTypesInDB;
List<DeviceType> deviceTypesResponse = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
deviceTypesInDatabase = deviceDAO.getDeviceTypes();
Map<String, DeviceManagementService> registeredTypes = pluginRepository.getAllDeviceManagementServices();
DeviceType deviceType;
if (registeredTypes != null && deviceTypesInDatabase != null) {
for (DeviceType aDeviceTypesInDatabase : deviceTypesInDatabase) {
if (registeredTypes.get(aDeviceTypesInDatabase.getName()) != null) {
deviceType = new DeviceType();
deviceType.setId(aDeviceTypesInDatabase.getId());
deviceType.setName(aDeviceTypesInDatabase.getName());
int tenantId = this.getTenantId();
deviceTypesProvidedByTenant = deviceTypeDAO.getDeviceTypesByProvider(tenantId);
publicSharedDeviceTypesInDB = deviceTypeDAO.getSharedDeviceTypes();
Map<DeviceTypeIdentifier, DeviceManagementService> registeredTypes =
pluginRepository.getAllDeviceManagementServices(tenantId);
Set<String> deviceTypeSetForTenant = new HashSet<>();
if (registeredTypes != null) {
if (deviceTypesProvidedByTenant != null) {
for (DeviceType deviceType : deviceTypesProvidedByTenant) {
DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName(), tenantId);
if (registeredTypes.get(providerKey) != null) {
deviceTypesResponse.add(deviceType);
deviceTypeSetForTenant.add(deviceType.getName());
}
}
}
// Get the device from the public space, however if there is another device with same name then give
// priority to that
if (publicSharedDeviceTypesInDB != null) {
for (DeviceType deviceType : publicSharedDeviceTypesInDB) {
DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName());
if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains(
deviceType.getName())) {
deviceTypesResponse.add(deviceType);
}
}
}
}
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device types.", e);
@ -714,7 +732,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try {
for (DeviceIdentifier deviceId : deviceIds) {
DeviceManagementService dms =
getPluginRepository().getDeviceManagementService(deviceId.getType());
getPluginRepository().getDeviceManagementService(deviceId.getType(), this.getTenantId());
dms.notifyOperationToDevices(operation, deviceIds);
}
} catch (DeviceManagementException deviceMgtEx) {
@ -1068,7 +1086,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType(), this.getTenantId());
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(status);
deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId());
@ -1171,7 +1189,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private DeviceManager getDeviceManager(String deviceType) {
DeviceManagementService deviceManagementService =
this.getPluginRepository().getDeviceManagementService(deviceType);
this.getPluginRepository().getDeviceManagementService(deviceType, this.getTenantId());
if (deviceManagementService == null) {
if (log.isDebugEnabled()) {
log.debug("Device type '" + deviceType + "' does not have an associated device management " +

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
@ -33,6 +34,8 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.user.api.TenantManager;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.carbon.utils.NetworkUtils;
@ -100,29 +103,32 @@ public final class DeviceManagerUtil {
* Adds a new device type to the database if it does not exists.
*
* @param typeName device type
* @param tenantId provider tenant Id
* @param isSharedWithAllTenants is this device type shared with all tenants.
* @return status of the operation
*/
public static boolean registerDeviceType(String typeName) throws DeviceManagementException {
public static boolean registerDeviceType(String typeName, int tenantId, boolean isSharedWithAllTenants)
throws DeviceManagementException {
boolean status;
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName);
DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName, tenantId);
if (deviceType == null) {
DeviceType dt = new DeviceType();
dt.setName(typeName);
deviceTypeDAO.addDeviceType(dt);
deviceType = new DeviceType();
deviceType.setName(typeName);
deviceTypeDAO.addDeviceType(deviceType, tenantId, isSharedWithAllTenants);
}
DeviceManagementDAOFactory.commitTransaction();
status = true;
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error occurred while registering the device type '" +
typeName + "'", e);
throw new DeviceManagementException("Error occurred while registering the device type '"
+ typeName + "'", e);
} catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("SQL occurred while registering the device type '" +
typeName + "'", e);
throw new DeviceManagementException("SQL occurred while registering the device type '"
+ typeName + "'", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
@ -135,15 +141,13 @@ public final class DeviceManagerUtil {
* @param typeName device type
* @return status of the operation
*/
public static boolean unregisterDeviceType(String typeName) throws DeviceManagementException {
public static boolean unregisterDeviceType(String typeName, int tenantId) throws DeviceManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName);
DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName, tenantId);
if (deviceType != null) {
DeviceType dt = new DeviceType();
dt.setName(typeName);
deviceTypeDAO.removeDeviceType(typeName);
deviceTypeDAO.removeDeviceType(typeName, tenantId);
}
DeviceManagementDAOFactory.commitTransaction();
return true;
@ -217,4 +221,27 @@ public final class DeviceManagerUtil {
return "http://" + hostName + ":" + port;
}
/**
* returns the tenant Id of the specific tenant Domain
*
* @param tenantDomain
* @return
* @throws DeviceManagementException
*/
public static int getTenantId(String tenantDomain) throws DeviceManagementException {
try {
if (tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
return MultitenantConstants.SUPER_TENANT_ID;
}
TenantManager tenantManager = DeviceManagementDataHolder.getInstance().getTenantManager();
int tenantId = tenantManager.getTenantId(tenantDomain);
if (tenantId == -1) {
throw new DeviceManagementException("invalid tenant Domain :" + tenantDomain);
}
return tenantId;
} catch (UserStoreException e) {
throw new DeviceManagementException("invalid tenant Domain :" + tenantDomain);
}
}
}

@ -36,27 +36,31 @@ public class DeviceManagementRepositoryTests {
@Test
public void testAddDeviceManagementService() {
DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
TestDataHolder.SUPER_TENANT_DOMAIN);
try {
this.getRepository().addDeviceManagementProvider(sourceProvider);
} catch (DeviceManagementException e) {
Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e);
}
DeviceManagementService targetProvider =
this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(targetProvider.getType(), sourceProvider.getType());
}
@Test(dependsOnMethods = "testAddDeviceManagementService")
public void testRemoveDeviceManagementService() {
DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
TestDataHolder.SUPER_TENANT_DOMAIN);
try {
this.getRepository().removeDeviceManagementProvider(sourceProvider);
} catch (DeviceManagementException e) {
Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e);
}
DeviceManagementService targetProvider =
this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
TestDataHolder.SUPER_TENANT_ID);
Assert.assertNull(targetProvider);
}

@ -34,15 +34,25 @@ import java.util.List;
public class TestDeviceManagementService implements DeviceManagementService {
private String providerType;
private String tenantDomain;
public TestDeviceManagementService(String deviceType){
public TestDeviceManagementService(String deviceType, String tenantDomain){
providerType = deviceType;
this.tenantDomain = tenantDomain;
}
@Override
public String getType() {
return providerType;
}
@Override
public String getProviderTenantDomain() { return tenantDomain;}
@Override
public boolean isSharedWithAllTenants() {
return true;
}
@Override
public void init() throws DeviceManagementException {

@ -43,7 +43,7 @@ public class ApplicationManagementProviderServiceTest {
public void init() {
deviceManagementPluginRepository = new DeviceManagementPluginRepository();
TestDeviceManagementService testDeviceManagementService =
new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN);
try {
deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService);
} catch (DeviceManagementException e) {

@ -31,6 +31,7 @@ public class TestDataHolder {
public static DeviceType initialTestDeviceType;
public static String TEST_DEVICE_TYPE = "Test";
public static Integer SUPER_TENANT_ID = -1234;
public static String SUPER_TENANT_DOMAIN="carbon.super";
public static String initialDeviceIdentifier = "12345";
public static String OWNER = "admin";

@ -54,7 +54,7 @@ public class DevicePersistTests extends BaseDeviceManagementTest {
DeviceType deviceType = TestDataHolder.generateDeviceTypeData(TestDataHolder.TEST_DEVICE_TYPE);
try {
DeviceManagementDAOFactory.beginTransaction();
deviceTypeDAO.addDeviceType(deviceType);
deviceTypeDAO.addDeviceType(deviceType, TestDataHolder.SUPER_TENANT_ID, true);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding device type '" + deviceType.getName() + "'";

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
NAME VARCHAR(300) DEFAULT NULL,
ID INT AUTO_INCREMENT NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (ID)
);

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -375,7 +376,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<DeviceType> deviceTypes = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceTypes = deviceTypeDAO.getDeviceTypes(tenantId);
} catch (Exception e) {
log.error("Error occurred while getting the device types.", e);
} finally {

@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -184,8 +185,9 @@ public class ProfileManagerImpl implements ProfileManager {
List<DeviceType> deviceTypes;
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes();
deviceTypes = deviceTypeDAO.getDeviceTypes(tenantId);
} catch (SQLException e) {
throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} catch (DeviceManagementDAOException e) {
@ -236,7 +238,8 @@ public class ProfileManagerImpl implements ProfileManager {
try {
DeviceManagementDAOFactory.openConnection();
deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceType = deviceTypeDAO.getDeviceType(deviceTypeName, tenantId);
} catch (DeviceManagementDAOException e) {
throw new ProfileManagementException("Error occurred while retrieving device type information", e);
} catch (SQLException e) {

@ -60,7 +60,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType());
deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType(), -1234, true);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementDAOException("Error occurred while adding dummy device type", e);

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
ID INT AUTO_INCREMENT NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (ID)
);

@ -25,6 +25,8 @@ DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(300) NULL DEFAULT NULL,
`PROVIDER_TENANT_ID` INTEGER DEFAULT 0,
`SHARED_WITH_ALL_TENANTS` BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (`ID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
NAME VARCHAR(300) DEFAULT NULL,
ID INT AUTO_INCREMENT NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (ID)
);

@ -1,6 +1,8 @@
CREATE TABLE DM_DEVICE_TYPE (
ID INTEGER IDENTITY NOT NULL,
NAME VARCHAR(300) DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (ID)
);

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INTEGER AUTO_INCREMENT NOT NULL,
NAME VARCHAR(300) DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (ID)
)ENGINE = InnoDB;

@ -1,6 +1,8 @@
CREATE TABLE DM_DEVICE_TYPE (
ID NUMBER(10) NOT NULL,
NAME VARCHAR2(300) DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
CONSTRAINT PK_DM_DEVICE_TYPE PRIMARY KEY (ID)
)
/

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID BIGSERIAL PRIMARY KEY,
NAME VARCHAR(300) DEFAULT NULL
NAME VARCHAR(300) DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE (

Loading…
Cancel
Save