From c45895685bb66a253c40dc99f351ddeea3c099de Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 29 Mar 2016 11:40:58 +0530 Subject: [PATCH] Added device type multi tenancy capability. --- .../mgt/common/DeviceTypeIdentifier.java | 69 ++++ .../common/spi/DeviceManagementService.java | 13 + .../DeviceManagementPluginRepository.java | 57 +++- .../device/mgt/core/dao/DeviceTypeDAO.java | 58 +++- .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 312 ++++++++++-------- .../DeviceManagementProviderServiceImpl.java | 56 ++-- .../mgt/core/util/DeviceManagerUtil.java | 59 +++- .../core/DeviceManagementRepositoryTests.java | 68 ++-- .../mgt/core/TestDeviceManagementService.java | 12 +- ...licationManagementProviderServiceTest.java | 2 +- .../mgt/core/common/TestDataHolder.java | 1 + .../mgt/core/dao/DevicePersistTests.java | 2 +- .../src/test/resources/sql/h2.sql | 6 +- .../core/mgt/impl/MonitoringManagerImpl.java | 4 +- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 7 +- .../policy/mgt/core/PolicyDAOTestCase.java | 2 +- .../src/test/resources/sql/CreateH2TestDB.sql | 4 +- .../test/resources/sql/CreateMySqlTestDB.sql | 2 + .../src/main/resources/dbscripts/cdm/h2.sql | 6 +- .../main/resources/dbscripts/cdm/mssql.sql | 8 +- .../main/resources/dbscripts/cdm/mysql.sql | 2 + .../main/resources/dbscripts/cdm/oracle.sql | 2 + .../resources/dbscripts/cdm/postgresql.sql | 4 +- 23 files changed, 518 insertions(+), 238 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceTypeIdentifier.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceTypeIdentifier.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceTypeIdentifier.java new file mode 100644 index 0000000000..1eb5e28e46 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceTypeIdentifier.java @@ -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; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java index bf2e44f3d8..143fd1e8db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java @@ -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(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index afeee53c8a..594d1be5e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -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 providers; + private Map providers; private boolean isInited; private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class); public DeviceManagementPluginRepository() { - providers = Collections.synchronizedMap(new HashMap()); + providers = Collections.synchronizedMap(new HashMap()); DeviceManagementServiceComponent.registerStartupListener(this); } 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) { 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 getAllDeviceManagementServices() { - return providers; + public Map getAllDeviceManagementServices(int tenantId) { + Map 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index d7217c90d1..53827abd6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -27,18 +27,64 @@ import java.util.List; */ 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 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 getDeviceTypes(int tenantId) throws DeviceManagementDAOException; - List 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 getDeviceTypesByProvider(int tenandId) throws DeviceManagementDAOException; + /** + * @return sharedWithAllDeviceTypes This returns public shared device types. + * @throws DeviceManagementDAOException + */ + List 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; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 0014e56f6f..bccddcee40 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -32,143 +32,179 @@ import java.util.List; public class DeviceTypeDAOImpl implements DeviceTypeDAO { - @Override - public void addDeviceType(DeviceType deviceType) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_TYPE (NAME) VALUES (?)"); - stmt.setString(1, deviceType.getName()); - stmt.execute(); - } catch (SQLException 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 { - - } - - @Override - public List getDeviceTypes() throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - List deviceTypes = new ArrayList<>(); - try { - conn = this.getConnection(); - String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE"; - 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 List getDeviceTypes(int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - List deviceTypes = new ArrayList<>(); - 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"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - 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; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - 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()) { - deviceType = new DeviceType(); - deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); - deviceType.setName(rs.getString("DEVICE_TYPE")); - } - return deviceType; - } catch (SQLException 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 { - 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 = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, type); - rs = stmt.executeQuery(); - - if (rs.next()) { - deviceType = new DeviceType(); - deviceType.setId(rs.getInt("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) throws DeviceManagementDAOException { - - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } + @Override + 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,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); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void updateDeviceType(DeviceType deviceType, int tenantId) + throws DeviceManagementDAOException { + } + + @Override + public List getDeviceTypes(int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List deviceTypes = new ArrayList<>(); + 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"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + 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 List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List deviceTypes = new ArrayList<>(); + try { + conn = this.getConnection(); + String sql = + "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(); + + 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 List getSharedDeviceTypes() throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List 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; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + 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()) { + deviceType = new DeviceType(); + deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setName(rs.getString("DEVICE_TYPE")); + } + return deviceType; + } catch (SQLException e) { + throw new DeviceManagementDAOException( + "Error occurred while fetching the registered device type", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + @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(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index f70eb5dca5..0e4e490fa6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -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 getAvailableDeviceTypes() throws DeviceManagementException { - List deviceTypesInDatabase; + List deviceTypesProvidedByTenant; + List publicSharedDeviceTypesInDB; List deviceTypesResponse = new ArrayList<>(); try { DeviceManagementDAOFactory.openConnection(); - deviceTypesInDatabase = deviceDAO.getDeviceTypes(); - Map 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()); - deviceTypesResponse.add(deviceType); + int tenantId = this.getTenantId(); + deviceTypesProvidedByTenant = deviceTypeDAO.getDeviceTypesByProvider(tenantId); + publicSharedDeviceTypesInDB = deviceTypeDAO.getSharedDeviceTypes(); + Map registeredTypes = + pluginRepository.getAllDeviceManagementServices(tenantId); + Set 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 " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 629787f529..ec798f2bdc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -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,26 +141,24 @@ 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; } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while registering the device type '" + - typeName + "'", e); + typeName + "'", e); } catch (TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("SQL occurred while registering the device type '" + - typeName + "'", e); + typeName + "'", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -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); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java index 127ee0d707..925d8a9e8d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java @@ -27,41 +27,45 @@ import org.wso2.carbon.device.mgt.core.common.TestDataHolder; public class DeviceManagementRepositoryTests { - private DeviceManagementPluginRepository repository; + private DeviceManagementPluginRepository repository; - @BeforeClass - public void initRepository() { - this.repository = new DeviceManagementPluginRepository(); - } + @BeforeClass + public void initRepository() { + this.repository = new DeviceManagementPluginRepository(); + } - @Test - public void testAddDeviceManagementService() { - DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); - 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); - Assert.assertEquals(targetProvider.getType(), sourceProvider.getType()); - } + @Test + public void testAddDeviceManagementService() { + 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, + TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(targetProvider.getType(), sourceProvider.getType()); + } - @Test(dependsOnMethods = "testAddDeviceManagementService") - public void testRemoveDeviceManagementService() { - DeviceManagementService sourceProvider = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); - 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); - Assert.assertNull(targetProvider); - } + @Test(dependsOnMethods = "testAddDeviceManagementService") + public void testRemoveDeviceManagementService() { + 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, + TestDataHolder.SUPER_TENANT_ID); + Assert.assertNull(targetProvider); + } - private DeviceManagementPluginRepository getRepository() { - return repository; - } + private DeviceManagementPluginRepository getRepository() { + return repository; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java index 63128cbbdd..c94a00fe8d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java @@ -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 { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java index b98745f8a2..4c7996e381 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java @@ -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) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index 1d381abf1e..78dcab56ea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -29,6 +29,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 Device generateDummyDeviceData(String deviceType){ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java index d7996be3a1..22914836b7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java @@ -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() + "'"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index d172e2f5d2..f112ff49a7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -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) ); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index f4c83d990f..fa5e8ae7f3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -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 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 { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index 394124cbeb..bdfe27c420 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -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 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) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 868931c422..9e44132e06 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -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); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 8b101ba8c1..83c3142af4 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -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) ); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index a85059fa83..f5466feaf6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -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; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 3a268960d1..f4c5357eed 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -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) ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index bb55d05862..3287ebafe5 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -1,7 +1,9 @@ CREATE TABLE DM_DEVICE_TYPE ( - ID INTEGER IDENTITY NOT NULL, - NAME VARCHAR(300) DEFAULT NULL, - PRIMARY KEY (ID) + 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) ); CREATE TABLE DM_DEVICE_CERTIFICATE ( diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index 545f939108..74defa7d89 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -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; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index 728233d24b..c5032611d5 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -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) ) / diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql index a1581e19d3..ef531b5a2a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -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 (