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(); 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; void init() throws DeviceManagementException;
DeviceManager getDeviceManager(); DeviceManager getDeviceManager();

@ -83,7 +83,8 @@
org.apache.catalina.core, org.apache.catalina.core,
org.apache.commons.collections, org.apache.commons.collections,
org.wso2.carbon.email.sender.*, org.wso2.carbon.email.sender.*,
org.wso2.carbon org.wso2.carbon,
org.wso2.carbon.base
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.mgt.core.internal, !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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
@ -32,23 +33,29 @@ import java.util.Map;
public class DeviceManagementPluginRepository implements DeviceManagerStartupListener { public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
private Map<String, DeviceManagementService> providers; private Map<DeviceTypeIdentifier, DeviceManagementService> providers;
private boolean isInited; private boolean isInited;
private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class); private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class);
public DeviceManagementPluginRepository() { public DeviceManagementPluginRepository() {
providers = Collections.synchronizedMap(new HashMap<String, DeviceManagementService>()); providers = Collections.synchronizedMap(new HashMap<DeviceTypeIdentifier, DeviceManagementService>());
DeviceManagementServiceComponent.registerStartupListener(this); DeviceManagementServiceComponent.registerStartupListener(this);
} }
public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
String deviceType = provider.getType(); 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) { synchronized (providers) {
try { try {
if (isInited) { if (isInited) {
/* Initializing Device Management Service Provider */ /* Initializing Device Management Service Provider */
provider.init(); provider.init();
DeviceManagerUtil.registerDeviceType(deviceType); DeviceManagerUtil.registerDeviceType(deviceType, tenantId, isSharedWithAllTenants);
DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType, DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType,
provider.getDeviceManager().requireDeviceAuthorization()); provider.getDeviceManager().requireDeviceAuthorization());
@ -57,20 +64,47 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
throw new DeviceManagementException("Error occurred while adding device management provider '" + throw new DeviceManagementException("Error occurred while adding device management provider '" +
deviceType + "'", e); 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 { 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) { public DeviceManagementService getDeviceManagementService(String type, int tenantId) {
return providers.get(type); //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() { public Map<DeviceTypeIdentifier, DeviceManagementService> getAllDeviceManagementServices(int tenantId) {
return providers; 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 @Override
@ -79,7 +113,8 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
for (DeviceManagementService provider : providers.values()) { for (DeviceManagementService provider : providers.values()) {
try { try {
provider.init(); provider.init();
DeviceManagerUtil.registerDeviceType(provider.getType()); int tenantId=DeviceManagerUtil.getTenantId(provider.getProviderTenantDomain());
DeviceManagerUtil.registerDeviceType(provider.getType(), tenantId, provider.isSharedWithAllTenants());
//TODO: //TODO:
//This is a temporory fix. //This is a temporory fix.
//windows and IOS cannot resolve user info by extracting certs //windows and IOS cannot resolve user info by extracting certs

@ -27,18 +27,64 @@ import java.util.List;
*/ */
public interface DeviceTypeDAO { public interface DeviceTypeDAO {
void addDeviceType(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;
void updateDeviceType(DeviceType deviceType) 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;
List<DeviceType> getDeviceTypes() 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;
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(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;
} }

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

@ -67,7 +67,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public boolean saveConfiguration(TenantConfiguration configuration) throws DeviceManagementException { public boolean saveConfiguration(TenantConfiguration configuration) throws DeviceManagementException {
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(configuration.getType()).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(configuration.getType(), this.getTenantId()).getDeviceManager();
return dms.saveConfiguration(configuration); return dms.saveConfiguration(configuration);
} }
@ -79,7 +79,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException { public TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException {
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(deviceType, this.getTenantId()).getDeviceManager();
if (dms == null) { if (dms == null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device type '" + deviceType + "' does not have an associated device management " + log.debug("Device type '" + deviceType + "' does not have an associated device management " +
@ -185,7 +185,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
int enrolmentId = 0; int enrolmentId = 0;
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId);
enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
@ -226,7 +226,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
deviceDAO.updateDevice(type.getId(), device, tenantId); deviceDAO.updateDevice(type.getId(), device, tenantId);
enrollmentDAO.updateEnrollment(device.getEnrolmentInfo()); enrollmentDAO.updateEnrollment(device.getEnrolmentInfo());
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
@ -287,7 +287,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
return false; return false;
} }
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED);
@ -621,22 +621,40 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public List<DeviceType> getAvailableDeviceTypes() throws DeviceManagementException { public List<DeviceType> getAvailableDeviceTypes() throws DeviceManagementException {
List<DeviceType> deviceTypesInDatabase; List<DeviceType> deviceTypesProvidedByTenant;
List<DeviceType> publicSharedDeviceTypesInDB;
List<DeviceType> deviceTypesResponse = new ArrayList<>(); List<DeviceType> deviceTypesResponse = new ArrayList<>();
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
deviceTypesInDatabase = deviceDAO.getDeviceTypes(); int tenantId = this.getTenantId();
Map<String, DeviceManagementService> registeredTypes = pluginRepository.getAllDeviceManagementServices(); deviceTypesProvidedByTenant = deviceTypeDAO.getDeviceTypesByProvider(tenantId);
DeviceType deviceType; publicSharedDeviceTypesInDB = deviceTypeDAO.getSharedDeviceTypes();
if (registeredTypes != null && deviceTypesInDatabase != null) { Map<DeviceTypeIdentifier, DeviceManagementService> registeredTypes =
for (DeviceType aDeviceTypesInDatabase : deviceTypesInDatabase) { pluginRepository.getAllDeviceManagementServices(tenantId);
if (registeredTypes.get(aDeviceTypesInDatabase.getName()) != null) { Set<String> deviceTypeSetForTenant = new HashSet<>();
deviceType = new DeviceType();
deviceType.setId(aDeviceTypesInDatabase.getId()); if (registeredTypes != null) {
deviceType.setName(aDeviceTypesInDatabase.getName()); if (deviceTypesProvidedByTenant != null) {
deviceTypesResponse.add(deviceType); 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) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device types.", e); throw new DeviceManagementException("Error occurred while obtaining the device types.", e);
@ -714,7 +732,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {
DeviceManagementService dms = DeviceManagementService dms =
getPluginRepository().getDeviceManagementService(deviceId.getType()); getPluginRepository().getDeviceManagementService(deviceId.getType(), this.getTenantId());
dms.notifyOperationToDevices(operation, deviceIds); dms.notifyOperationToDevices(operation, deviceIds);
} }
} catch (DeviceManagementException deviceMgtEx) { } catch (DeviceManagementException deviceMgtEx) {
@ -1068,7 +1086,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException {
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType(), this.getTenantId());
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(status); device.getEnrolmentInfo().setStatus(status);
deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId()); deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId());
@ -1171,7 +1189,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private DeviceManager getDeviceManager(String deviceType) { private DeviceManager getDeviceManager(String deviceType) {
DeviceManagementService deviceManagementService = DeviceManagementService deviceManagementService =
this.getPluginRepository().getDeviceManagementService(deviceType); this.getPluginRepository().getDeviceManagementService(deviceType, this.getTenantId());
if (deviceManagementService == null) { if (deviceManagementService == null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device type '" + deviceType + "' does not have an associated device management " + 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.base.MultitenantConstants;
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.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService; import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.carbon.utils.NetworkUtils; 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. * Adds a new device type to the database if it does not exists.
* *
* @param typeName device type * @param typeName device type
* @param tenantId provider tenant Id
* @param isSharedWithAllTenants is this device type shared with all tenants.
* @return status of the operation * @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; boolean status;
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName); DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName, tenantId);
if (deviceType == null) { if (deviceType == null) {
DeviceType dt = new DeviceType(); deviceType = new DeviceType();
dt.setName(typeName); deviceType.setName(typeName);
deviceTypeDAO.addDeviceType(dt); deviceTypeDAO.addDeviceType(deviceType, tenantId, isSharedWithAllTenants);
} }
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
status = true; status = true;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error occurred while registering the device type '" + throw new DeviceManagementException("Error occurred while registering the device type '"
typeName + "'", e); + typeName + "'", e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("SQL occurred while registering the device type '" + throw new DeviceManagementException("SQL occurred while registering the device type '"
typeName + "'", e); + typeName + "'", e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
@ -135,26 +141,24 @@ public final class DeviceManagerUtil {
* @param typeName device type * @param typeName device type
* @return status of the operation * @return status of the operation
*/ */
public static boolean unregisterDeviceType(String typeName) throws DeviceManagementException { public static boolean unregisterDeviceType(String typeName, int tenantId) throws DeviceManagementException {
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName); DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName, tenantId);
if (deviceType != null) { if (deviceType != null) {
DeviceType dt = new DeviceType(); deviceTypeDAO.removeDeviceType(typeName, tenantId);
dt.setName(typeName);
deviceTypeDAO.removeDeviceType(typeName);
} }
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
return true; return true;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error occurred while registering the device type '" + throw new DeviceManagementException("Error occurred while registering the device type '" +
typeName + "'", e); typeName + "'", e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("SQL occurred while registering the device type '" + throw new DeviceManagementException("SQL occurred while registering the device type '" +
typeName + "'", e); typeName + "'", e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
@ -217,4 +221,27 @@ public final class DeviceManagerUtil {
return "http://" + hostName + ":" + port; 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);
}
}
} }

@ -27,41 +27,45 @@ import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
public class DeviceManagementRepositoryTests { public class DeviceManagementRepositoryTests {
private DeviceManagementPluginRepository repository; private DeviceManagementPluginRepository repository;
@BeforeClass @BeforeClass
public void initRepository() { public void initRepository() {
this.repository = new DeviceManagementPluginRepository(); this.repository = new DeviceManagementPluginRepository();
} }
@Test @Test
public void testAddDeviceManagementService() { public void testAddDeviceManagementService() {
DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
try { TestDataHolder.SUPER_TENANT_DOMAIN);
this.getRepository().addDeviceManagementProvider(sourceProvider); try {
} catch (DeviceManagementException e) { this.getRepository().addDeviceManagementProvider(sourceProvider);
Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e); } catch (DeviceManagementException e) {
} Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e);
DeviceManagementService targetProvider = }
this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); DeviceManagementService targetProvider =
Assert.assertEquals(targetProvider.getType(), sourceProvider.getType()); this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
} TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(targetProvider.getType(), sourceProvider.getType());
}
@Test(dependsOnMethods = "testAddDeviceManagementService") @Test(dependsOnMethods = "testAddDeviceManagementService")
public void testRemoveDeviceManagementService() { public void testRemoveDeviceManagementService() {
DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
try { TestDataHolder.SUPER_TENANT_DOMAIN);
this.getRepository().removeDeviceManagementProvider(sourceProvider); try {
} catch (DeviceManagementException e) { this.getRepository().removeDeviceManagementProvider(sourceProvider);
Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e); } catch (DeviceManagementException e) {
} Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e);
DeviceManagementService targetProvider = }
this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); DeviceManagementService targetProvider =
Assert.assertNull(targetProvider); this.getRepository().getDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE,
} TestDataHolder.SUPER_TENANT_ID);
Assert.assertNull(targetProvider);
}
private DeviceManagementPluginRepository getRepository() { private DeviceManagementPluginRepository getRepository() {
return repository; return repository;
} }
} }

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

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

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

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

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL, ID INT AUTO_INCREMENT NOT NULL,
NAME VARCHAR(300) DEFAULT 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) 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.Log;
import org.apache.commons.logging.LogFactory; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -375,7 +376,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<DeviceType> deviceTypes = new ArrayList<>(); List<DeviceType> deviceTypes = new ArrayList<>();
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceTypes = deviceTypeDAO.getDeviceTypes(tenantId);
} catch (Exception e) { } catch (Exception e) {
log.error("Error occurred while getting the device types.", e); log.error("Error occurred while getting the device types.", e);
} finally { } 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.Log;
import org.apache.commons.logging.LogFactory; 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.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -184,8 +185,9 @@ public class ProfileManagerImpl implements ProfileManager {
List<DeviceType> deviceTypes; List<DeviceType> deviceTypes;
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
deviceTypes = deviceTypeDAO.getDeviceTypes(); deviceTypes = deviceTypeDAO.getDeviceTypes(tenantId);
} catch (SQLException e) { } catch (SQLException e) {
throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
@ -236,7 +238,8 @@ public class ProfileManagerImpl implements ProfileManager {
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceType = deviceTypeDAO.getDeviceType(deviceTypeName, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new ProfileManagementException("Error occurred while retrieving device type information", e); throw new ProfileManagementException("Error occurred while retrieving device type information", e);
} catch (SQLException e) { } catch (SQLException e) {

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

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( 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, NAME VARCHAR(300) NULL DEFAULT NULL,
PROVIDER_TENANT_ID INTEGER DEFAULT 0,
SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (ID) PRIMARY KEY (ID)
); );

@ -25,6 +25,8 @@ DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ( CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` (
`ID` INT(11) NOT NULL AUTO_INCREMENT, `ID` INT(11) NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(300) NULL DEFAULT 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`)) PRIMARY KEY (`ID`))
ENGINE = InnoDB ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1; DEFAULT CHARACTER SET = latin1;

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL, ID INT AUTO_INCREMENT NOT NULL,
NAME VARCHAR(300) DEFAULT 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) PRIMARY KEY (ID)
); );

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

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
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,
PRIMARY KEY (ID) PRIMARY KEY (ID)
)ENGINE = InnoDB; )ENGINE = InnoDB;

@ -1,6 +1,8 @@
CREATE TABLE DM_DEVICE_TYPE ( CREATE TABLE DM_DEVICE_TYPE (
ID NUMBER(10) NOT NULL, ID NUMBER(10) NOT NULL,
NAME VARCHAR2(300) DEFAULT 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) CONSTRAINT PK_DM_DEVICE_TYPE PRIMARY KEY (ID)
) )
/ /

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID BIGSERIAL PRIMARY KEY, 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 ( CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE (

Loading…
Cancel
Save