diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java index 8ac709519a..351fd1656b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java @@ -19,9 +19,8 @@ package org.wso2.carbon.device.mgt.common; public class DeviceIdentifier { - private String type; - private String id; + private String type; public String getType() { return type; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementServiceException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementServiceException.java deleted file mode 100644 index 15520c16c7..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementServiceException.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2014, 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; - -public class DeviceManagementServiceException extends Exception { - - private static final long serialVersionUID = -8933146283800122640L; - private String errorMessage; - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public DeviceManagementServiceException(String msg, Exception nestedEx) { - super(msg, nestedEx); - setErrorMessage(msg); - } - - public DeviceManagementServiceException(String message, Throwable cause) { - super(message, cause); - setErrorMessage(message); - } - - public DeviceManagementServiceException(String msg) { - super(msg); - setErrorMessage(msg); - } - - public DeviceManagementServiceException() { - super(); - } - - public DeviceManagementServiceException(Throwable cause) { - super(cause); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FeatureManager.java new file mode 100644 index 0000000000..41f4211d41 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FeatureManager.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015, 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.util.List; + +public interface FeatureManager { + + boolean addFeature(Feature feature) throws DeviceManagementException; + + boolean getFeature(String name) throws DeviceManagementException; + + List getFeatures() throws DeviceManagementException; + + boolean removeFeature(String name) throws DeviceManagementException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/License.java similarity index 97% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/License.java index dc7a24135e..7044eb4916 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/License.java @@ -18,7 +18,7 @@ * */ -package org.wso2.carbon.device.mgt.core.config.license; +package org.wso2.carbon.device.mgt.common.license.mgt; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseManagementException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseManagementException.java index 7668f49682..8a0e462f39 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseManagementException.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.license.mgt; +package org.wso2.carbon.device.mgt.common.license.mgt; public class LicenseManagementException extends Exception { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseManager.java similarity index 71% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseManager.java index 18dfa06d01..6031de79ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseManager.java @@ -16,14 +16,12 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.license.mgt; - -import org.wso2.carbon.device.mgt.core.config.license.License; +package org.wso2.carbon.device.mgt.common.license.mgt; public interface LicenseManager { - License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException; + License getLicense(String deviceType, String languageCode) throws LicenseManagementException; - void addLicense(License license) throws LicenseManagementException; + boolean addLicense(String deviceType, License license) throws LicenseManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseNotFoundException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseNotFoundException.java index c1c6ce9117..ee25364fcf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/license/mgt/LicenseNotFoundException.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.license.mgt; +package org.wso2.carbon.device.mgt.common.license.mgt; public class LicenseNotFoundException extends RuntimeException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Operation.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java index 82f18f7068..0f3d46a473 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Operation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.common; +package org.wso2.carbon.device.mgt.common.operation.mgt; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationExecutionException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationExecutionException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationExecutionException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationExecutionException.java index 1df7c95de4..422f689af3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationExecutionException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationExecutionException.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.wso2.carbon.device.mgt.common; +package org.wso2.carbon.device.mgt.common.operation.mgt; public class OperationExecutionException extends Exception { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationManagementException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManagementException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationManagementException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManagementException.java index ceace67fd3..ec4d40b0a4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationManagementException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManagementException.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.wso2.carbon.device.mgt.common; +package org.wso2.carbon.device.mgt.common.operation.mgt; public class OperationManagementException extends Exception { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java similarity index 89% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationManager.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index b3a5b2a347..3f134c69ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -15,7 +15,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.wso2.carbon.device.mgt.common; +package org.wso2.carbon.device.mgt.common.operation.mgt; + +import org.wso2.carbon.device.mgt.common.*; import java.util.List; @@ -60,8 +62,8 @@ public interface OperationManager { * TODO: Move this into a separate FeatureManager * @param deviceType - Device type * @return a list of Feature objects. - * @throws FeatureManagementException + * @throws org.wso2.carbon.device.mgt.common.FeatureManagementException */ - public List getFeaturesForDeviceType(String deviceType) throws FeatureManagementException; + public List getFeatures(String deviceType) throws FeatureManagementException; } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java similarity index 89% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagerService.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java index 2002520ca1..ab42c1894d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java @@ -18,10 +18,7 @@ package org.wso2.carbon.device.mgt.common.spi; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.OperationManager; +import org.wso2.carbon.device.mgt.common.*; import java.util.List; @@ -29,15 +26,22 @@ import java.util.List; * This represents the service provider interface that has to be implemented by any of new * device type plugin implementation intended to be managed through CDM. */ -public interface DeviceManagerService { +public interface DeviceManager { /** - * Method to retrieve the provider type that implements DeviceManagerService interface. + * Method to retrieve the provider type that implements DeviceManager interface. * * @return Returns provider type */ String getProviderType(); + /** + * Method to return feature manager implementation associated with a particular platform-specific plugin. + * + * @return Returns an instance of feature manager + */ + FeatureManager getFeatureManager(); + /** * Method to enrolling a particular device of type mobile, IoT, etc within CDM. * @@ -129,12 +133,5 @@ public interface DeviceManagerService { */ boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException; - /** - * Method to retrieve the Operation manager implementation associated with a given plugin. - * - * @return An appropriate instance of the underlying operation management implementation - * @throws DeviceManagementException - */ - OperationManager getOperationManager() throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 9fd1737a82..7dde13ebe5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -72,7 +72,9 @@ org.wso2.carbon.registry.core.service, org.wso2.carbon.registry.core.session, org.w3c.dom, - org.wso2.carbon.email.verification.util + org.wso2.carbon.governance.api.exception, + org.wso2.carbon.governance.api.generic, + org.wso2.carbon.governance.api.generic.dataobjects !org.wso2.carbon.device.mgt.core.internal, @@ -153,6 +155,10 @@ org.wso2.carbon org.wso2.carbon.base + + org.wso2.carbon.governance + org.wso2.carbon.governance.api + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java index db8aaaeacb..048317a1e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java @@ -20,7 +20,8 @@ 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.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.util.HashMap; @@ -28,35 +29,36 @@ import java.util.Map; public class DeviceManagementRepository { - private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); - private Map providers; + private Map providers; public DeviceManagementRepository() { - providers = new HashMap(); + providers = new HashMap(); } - public void addDeviceManagementProvider(DeviceManagerService provider) { + public void addDeviceManagementProvider(DeviceManager provider) throws DeviceManagementException { String deviceType = provider.getProviderType(); try { DeviceManagerUtil.registerDeviceType(deviceType); } catch (DeviceManagementException e) { - log.error("Exception occurred while registering the device type.", e); + throw new DeviceManagementException("Error occurred while adding device management provider '" + + deviceType + "'"); } providers.put(deviceType, provider); } - public void removeDeviceManagementProvider(DeviceManagerService provider) { + public void removeDeviceManagementProvider(DeviceManager provider) throws DeviceManagementException { String deviceType = provider.getProviderType(); - try { DeviceManagerUtil.unregisterDeviceType(deviceType); } catch (DeviceManagementException e) { - log.error("Exception occurred while registering the device type.", e); + throw new DeviceManagementException("Error occurred while removing device management provider '" + + deviceType + "'", e); } providers.remove(deviceType); } - public DeviceManagerService getDeviceManagementProvider(String type) { + public DeviceManager getDeviceManagementProvider(String type) { return providers.get(type); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java new file mode 100644 index 0000000000..79b628349b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -0,0 +1,255 @@ +/* + * Copyright (c) 2014, 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.core; + +import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +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; +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.dto.Status; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; + +import java.util.ArrayList; +import java.util.List; + +public class DeviceManagementServiceProviderImpl implements DeviceManagementService { + + private DeviceDAO deviceDAO; + private DeviceTypeDAO deviceTypeDAO; + private DeviceManagementRepository pluginRepository; + private OperationManager operationManager; + private LicenseManager licenseManager; + + public DeviceManagementServiceProviderImpl(DeviceManagementRepository pluginRepository) { + this.pluginRepository = pluginRepository; + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + this.operationManager = new OperationManagerImpl(); + this.licenseManager = new LicenseManagerImpl(); + } + + @Override + public String getProviderType() { + return null; + } + + @Override + public FeatureManager getFeatureManager() { + return null; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(device.getType()); + boolean status = dms.enrollDevice(device); + try { + org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device); + DeviceType deviceType = this.getDeviceTypeDAO().getDeviceType(device.getType()); + deviceDto.setStatus(Status.ACTIVE); + deviceDto.setDeviceTypeId(deviceType.getId()); + this.getDeviceDAO().addDevice(deviceDto); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while enrolling the device " + + "'" + device.getId() + "'", e); + } + return status; + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(device.getType()); + boolean status = dms.modifyEnrollment(device); + try { + this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while modifying the device " + + "'" + device.getId() + "'", e); + } + return status; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.disenrollDevice(deviceId); + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.isEnrolled(deviceId); + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.isActive(deviceId); + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) + throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.setActive(deviceId, status); + } + + @Override + public List getAllDevices() throws DeviceManagementException { + return null; + } + + @Override + public List getAllDevices(String type) throws DeviceManagementException { + DeviceManager dms = this.getPluginRepository().getDeviceManagementProvider(type); + List devicesList = new ArrayList(); + try { + DeviceType dt = this.getDeviceTypeDAO().getDeviceType(type); + List devices = + this.getDeviceDAO().getDevices(dt.getId()); + + for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) { + DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); + Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType); + DeviceIdentifier deviceIdentifier = + DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType); + Device dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesList.add(convertedDevice); + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for type " + + "'" + type + "'", e); + } + return devicesList; + } + + @Override + public List getDeviceListOfUser(String username) throws DeviceManagementException { + return null; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + Device convertedDevice = null; + try { + DeviceType deviceType = + this.getDeviceTypeDAO().getDeviceType(deviceId.getType()); + org.wso2.carbon.device.mgt.core.dto.Device device = + this.getDeviceDAO().getDevice(deviceId); + if (device != null) { + convertedDevice = DeviceManagementDAOUtil + .convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceType.getId())); + Device dmsDevice = dms.getDevice(deviceId); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for id " + + "'" + deviceId.getId() + "'", e); + } + return convertedDevice; + } + + @Override + public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(device.getType()); + return dms.updateDeviceInfo(device); + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.setOwnership(deviceId, ownershipType); + } + + public OperationManager getOperationManager(String type) throws DeviceManagementException { + return operationManager; + } + + @Override + public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { + return licenseManager.getLicense(deviceType, languageCode); + } + + @Override + public boolean addLicense(String type, License license) throws LicenseManagementException { + return licenseManager.addLicense(type, license); + } + + public DeviceDAO getDeviceDAO() { + return deviceDAO; + } + + public DeviceTypeDAO getDeviceTypeDAO() { + return deviceTypeDAO; + } + + public DeviceManagementRepository getPluginRepository() { + return pluginRepository; + } + + @Override + public boolean addOperation(Operation operation, + List devices) throws OperationManagementException { + return operationManager.addOperation(operation, devices); + } + + @Override + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return operationManager.getOperations(deviceId); + } + + @Override + public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return operationManager.getPendingOperations(deviceId); + } + + @Override + public List getFeatures(String deviceType) throws FeatureManagementException { + return operationManager.getFeatures(deviceType); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java deleted file mode 100644 index 1f2c4df376..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2014, 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.core; - -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.OperationManager; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; - -import java.util.List; - -/** - * Proxy class for all Device Management related operations that take the corresponding plugin type in - * and resolve the appropriate plugin implementation - */ -public interface DeviceManager { - - public boolean enrollDevice(Device device) throws DeviceManagementException; - - public boolean modifyEnrollment(Device device) throws DeviceManagementException; - - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException; - - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException; - - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException; - - public boolean setActive(DeviceIdentifier deviceId, boolean status) - throws DeviceManagementException; - - public List getAllDevices(String type) throws DeviceManagementException; - - public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException; - - public boolean updateDeviceInfo(Device device) throws DeviceManagementException; - - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException; - - public OperationManager getOperationManager(String type) throws DeviceManagementException; - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java deleted file mode 100644 index 64ebbf0e46..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright (c) 2014, 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.core; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.OperationManager; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; -import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; -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; -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.dto.Status; - -import java.util.ArrayList; -import java.util.List; - -public class DeviceManagerImpl implements DeviceManager { - - private static Log log = LogFactory.getLog(DeviceManagerImpl.class); - private DeviceDAO deviceDAO; - private DeviceTypeDAO deviceTypeDAO; - private DeviceManagementConfig config; - private DeviceManagementRepository pluginRepository; - - public DeviceManagerImpl(DeviceManagementConfig config, - DeviceManagementRepository pluginRepository) { - this.config = config; - this.pluginRepository = pluginRepository; - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - } - - @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(device.getType()); - boolean status = dms.enrollDevice(device); - try { - org.wso2.carbon.device.mgt.core.dto.Device deviceDto = - DeviceManagementDAOUtil.convertDevice(device); - Integer deviceTypeId = - this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); - deviceDto.setStatus(Status.ACTIVE); - deviceDto.setDeviceTypeId(deviceTypeId); - this.getDeviceDAO().addDevice(deviceDto); - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException( - "Error occurred while enrolling the device '" + device.getId() + "'", - e); - } - return status; - } - - @Override - public boolean modifyEnrollment(Device device) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(device.getType()); - boolean status = dms.modifyEnrollment(device); - try { - this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException( - "Error occurred while modifying the device '" + device.getId() + "'", - e); - } - return status; - } - - @Override - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.disenrollDevice(deviceId); - } - - @Override - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.isEnrolled(deviceId); - } - - @Override - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.isActive(deviceId); - } - - @Override - public boolean setActive(DeviceIdentifier deviceId, boolean status) - throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.setActive(deviceId, status); - } - - @Override - public List getAllDevices(String type) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); - List devicesList = new ArrayList(); - try { - Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(type); - List devices = - this.getDeviceDAO().getDevices(deviceTypeId); - - for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) { - DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); - Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType); - DeviceIdentifier deviceIdentifier = - DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType); - Device dmsDevice = dms.getDevice(deviceIdentifier); - if (dmsDevice != null) { - convertedDevice.setProperties(dmsDevice.getProperties()); - convertedDevice.setFeatures(dmsDevice.getFeatures()); - } - devicesList.add(convertedDevice); - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException( - "Error occurred while obtaining the device for type '" + type + "'", e); - } - return devicesList; - } - - @Override - public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - Device convertedDevice = null; - try { - Integer deviceTypeId = - this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(deviceId.getType()); - org.wso2.carbon.device.mgt.core.dto.Device device = - this.getDeviceDAO().getDeviceByDeviceIdentifier(deviceTypeId, deviceId.getId()); - if (device != null) { - convertedDevice = DeviceManagementDAOUtil - .convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceTypeId)); - Device dmsDevice = dms.getDevice(deviceId); - if (dmsDevice != null) { - convertedDevice.setProperties(dmsDevice.getProperties()); - convertedDevice.setFeatures(dmsDevice.getFeatures()); - } - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException( - "Error occurred while obtaining the device for id '" + deviceId.getId() + "'", - e); - } - return convertedDevice; - } - - @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(device.getType()); - return dms.updateDeviceInfo(device); - } - - @Override - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.setOwnership(deviceId, ownershipType); - } - - public OperationManager getOperationManager(String type) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); - return dms.getOperationManager(); - } - - public DeviceDAO getDeviceDAO() { - return deviceDAO; - } - - public DeviceTypeDAO getDeviceTypeDAO() { - return deviceTypeDAO; - } - - public DeviceManagementRepository getPluginRepository() { - return pluginRepository; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java index b378fa70c2..ba36bdd470 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java @@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.core.config.license; +import org.wso2.carbon.device.mgt.common.license.mgt.License; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java index 43782e7518..824de24335 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java @@ -22,7 +22,7 @@ package org.wso2.carbon.device.mgt.core.config.license; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.utils.CarbonUtils; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 4e3a7f86ca..51ed642899 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.Status; @@ -32,27 +33,31 @@ public interface DeviceDAO { void updateDevice(Device device) throws DeviceManagementDAOException; - void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException; + void updateDeviceStatus(int deviceId, Status status) throws DeviceManagementDAOException; - void deleteDevice(Long deviceId) throws DeviceManagementDAOException; + void deleteDevice(int deviceId) throws DeviceManagementDAOException; - Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException; + Device getDevice(int deviceId) throws DeviceManagementDAOException; - /** - * @param type - Device type. - * @param identifier - Device identifier. - * @return the Device object which matches given data - * @throws DeviceManagementDAOException - */ - Device getDeviceByDeviceIdentifier(Integer type, String identifier) - throws DeviceManagementDAOException; + Device getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; List getDevices() throws DeviceManagementDAOException; + List getDeviceIds(List devices) throws DeviceManagementDAOException; + /** * @param type - The device type id. * @return a list of devices based on the type id. * @throws DeviceManagementDAOException */ - List getDevices(Integer type) throws DeviceManagementDAOException; + List getDevices(int type) throws DeviceManagementDAOException; + + /** + * Get the list of devices belongs to a user. + * @param username Requested user. + * @return List of devices of the user. + * @throws DeviceManagementDAOException + */ + List getDeviceListOfUser(String username) throws DeviceManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index df6ee073a7..10df0539a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -90,4 +90,5 @@ public class DeviceManagementDAOFactory { public static DataSource getDataSource() { return dataSource; } + } 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 bed0d07ddd..b59781b876 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 @@ -33,10 +33,10 @@ public interface DeviceTypeDAO { List getDeviceTypes() throws DeviceManagementDAOException; - DeviceType getDeviceType(Integer id) throws DeviceManagementDAOException; + DeviceType getDeviceType(int id) throws DeviceManagementDAOException; - Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException; + DeviceType getDeviceType(String name) throws DeviceManagementDAOException; - void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException; + void removeDeviceType(String type) 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/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 54010a8554..4f665ed90f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -20,10 +20,12 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.Status; import javax.sql.DataSource; @@ -37,164 +39,192 @@ import java.util.List; public class DeviceDAOImpl implements DeviceDAO { - private DataSource dataSource; - private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); - public DeviceDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } + private DataSource dataSource; + private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); - @Override - public void addDevice(Device device) throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String createDBQuery = - "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " + - "OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + public DeviceDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } - stmt = conn.prepareStatement(createDBQuery); - stmt.setString(1, device.getDescription()); - stmt.setString(2, device.getName()); - stmt.setLong(3, new Date().getTime()); - stmt.setLong(4, new Date().getTime()); - stmt.setString(5, device.getOwnerShip()); - stmt.setString(6, device.getStatus().toString()); - stmt.setInt(7, device.getDeviceTypeId()); - stmt.setString(8, device.getDeviceIdentificationId()); - stmt.setString(9, device.getOwnerId()); - stmt.setInt(10, device.getTenantId()); - stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while enrolling device '" + device.getName() + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); - } - } + @Override + public void addDevice(Device device) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = + "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " + + "OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, device.getDescription()); + stmt.setString(2, device.getName()); + stmt.setLong(3, new Date().getTime()); + stmt.setLong(4, new Date().getTime()); + stmt.setString(5, device.getOwnerShip()); + stmt.setString(6, device.getStatus().toString()); + stmt.setInt(7, device.getDeviceTypeId()); + stmt.setString(8, device.getDeviceIdentificationId()); + stmt.setString(9, device.getOwnerId()); + stmt.setInt(10, device.getTenantId()); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while enrolling device " + + "'" + device.getName() + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } - @Override - public void updateDevice(Device device) throws DeviceManagementDAOException { + @Override + public void updateDevice(Device device) throws DeviceManagementDAOException { - } + } - @Override - public void updateDeviceStatus(Long deviceId, Status status) - throws DeviceManagementDAOException { + @Override + public void updateDeviceStatus(int deviceId, Status status) throws DeviceManagementDAOException { - } + } - @Override - public void deleteDevice(Long deviceId) throws DeviceManagementDAOException { + @Override + public void deleteDevice(int deviceId) throws DeviceManagementDAOException { - } + } - @Override - public Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException { - return null; - } + @Override + public Device getDevice(int deviceId) throws DeviceManagementDAOException { + return null; + } - @Override - public Device getDeviceByDeviceIdentifier(Integer type, String identifier) - throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - Device device = null; - try { - conn = this.getConnection(); - String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + - "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + - "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + - "WHERE DM_DEVICE.DEVICE_TYPE_ID=? AND " + - "DM_DEVICE.DEVICE_IDENTIFICATION=?"; - stmt = conn.prepareStatement(selectDBQueryForType); - stmt.setInt(1, type); - stmt.setString(2, identifier); - resultSet = stmt.executeQuery(); - while (resultSet.next()) { - device = new Device(); - device.setId(resultSet.getInt(1)); - device.setDescription(resultSet.getString(2)); - device.setName(resultSet.getString(3)); - device.setDateOfEnrollment(resultSet.getLong(4)); - device.setDateOfLastUpdate(resultSet.getLong(5)); - //TODO:- Ownership is not a enum in DeviceDAO - device.setOwnerShip(resultSet.getString(6)); - device.setStatus(Status.valueOf(resultSet.getString(7))); - device.setDeviceTypeId(resultSet.getInt(8)); - device.setDeviceIdentificationId(resultSet.getString(9)); - device.setOwnerId(resultSet.getString(10)); - device.setTenantId(resultSet.getInt(11)); - } - } catch (SQLException e) { - String msg = "Error occurred while listing devices for type '" + type + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - return device; - } + @Override + public Device getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + Device device = null; + try { + conn = this.getConnection(); + String sql = + "SELECT d.ID, d.DESCRIPTION, d.NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " + + "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DEVICE_TYPE dt WHERE " + + "dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceIdentifier.getType()); + stmt.setString(2, deviceIdentifier.getId()); - @Override - public List getDevices() throws DeviceManagementDAOException { - return null; - } + rs = stmt.executeQuery(); + if (rs.next()) { + device = new Device(); + device.setId(rs.getInt("ID")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setName(rs.getString("NAME")); + device.setDateOfEnrollment(rs.getLong("DATE_OF_ENROLLMENT")); + device.setDateOfLastUpdate(rs.getLong("DATE_OF_LAST_UPDATE")); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(rs.getString("OWNERSHIP")); + device.setStatus(Status.valueOf(rs.getString("STATUS"))); + device.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); + device.setDeviceIdentificationId(rs.getString("DEVICE_IDENTIFICATION")); + device.setOwnerId(rs.getString("OWNER")); + device.setTenantId(rs.getInt("TENANT_ID")); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type " + + "'" + deviceIdentifier.getType() + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + } + return device; + } - @Override - public List getDevices(Integer type) throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - List devicesList = null; - try { - conn = this.getConnection(); - String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + - "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + - "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + - "WHERE DM_DEVICE.DEVICE_TYPE_ID=?"; - stmt = conn.prepareStatement(selectDBQueryForType); - stmt.setInt(1, type); - resultSet = stmt.executeQuery(); - devicesList = new ArrayList(); - while (resultSet.next()) { - Device device = new Device(); - device.setId(resultSet.getInt(1)); - device.setDescription(resultSet.getString(2)); - device.setName(resultSet.getString(3)); - device.setDateOfEnrollment(resultSet.getLong(4)); - device.setDateOfLastUpdate(resultSet.getLong(5)); - //TODO:- Ownership is not a enum in DeviceDAO - device.setOwnerShip(resultSet.getString(6)); - device.setStatus(Status.valueOf(resultSet.getString(7))); - device.setDeviceTypeId(resultSet.getInt(8)); - device.setDeviceIdentificationId(resultSet.getString(9)); - device.setOwnerId(resultSet.getString(10)); - device.setTenantId(resultSet.getInt(11)); - devicesList.add(device); - } - } catch (SQLException e) { - String msg = "Error occurred while listing devices for type '" + type + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - return devicesList; - } + @Override + public List getDevices() throws DeviceManagementDAOException { + return null; + } - private Connection getConnection() throws DeviceManagementDAOException { - try { - return dataSource.getConnection(); - } catch (SQLException e) { - throw new DeviceManagementDAOException( - "Error occurred while obtaining a connection from the device " + - "management metadata repository datasource", e); - } - } + @Override + public List getDeviceIds(List devices) throws DeviceManagementDAOException { + List deviceIds = new ArrayList(); + try { + Connection conn = this.getConnection(); + String sql = "SELECT DISTINCT ID FROM DEVICE WHERE NAME IN (?) AND ID IN (?)"; + PreparedStatement stmt = conn.prepareStatement(sql); + //stmt.setArray(1, new java.sql.Date[0]); + stmt.setString(2, ""); + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + deviceIds.add(rs.getInt("ID")); + } + return deviceIds; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving device ids", e); + } + } + + private String getDeviceNameString(List devices) { + StringBuilder sb = new StringBuilder(); + for (DeviceIdentifier device : devices) { + sb.append(device.getId()); + } + return sb.toString(); + } + + @Override + public List getDevices(int type) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List devicesList = null; + try { + conn = this.getConnection(); + String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + + "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + + "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + + "WHERE DM_DEVICE.DEVICE_TYPE_ID = ?"; + stmt = conn.prepareStatement(selectDBQueryForType); + stmt.setInt(1, type); + resultSet = stmt.executeQuery(); + devicesList = new ArrayList(); + while (resultSet.next()) { + Device device = new Device(); + device.setId(resultSet.getInt(1)); + device.setDescription(resultSet.getString(2)); + device.setName(resultSet.getString(3)); + device.setDateOfEnrollment(resultSet.getLong(4)); + device.setDateOfLastUpdate(resultSet.getLong(5)); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(resultSet.getString(6)); + device.setStatus(Status.valueOf(resultSet.getString(7))); + device.setDeviceTypeId(resultSet.getInt(8)); + device.setDeviceIdentificationId(resultSet.getString(9)); + device.setOwnerId(resultSet.getString(10)); + device.setTenantId(resultSet.getInt(11)); + devicesList.add(device); + } + } catch (SQLException e) { + String msg = "Error occurred while listing devices for type '" + type + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return devicesList; + } + + @Override + public List getDeviceListOfUser(String username) throws DeviceManagementDAOException { + return null; + } + + private Connection getConnection() throws DeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + throw new DeviceManagementDAOException( + "Error occurred while obtaining a connection from the device " + + "management metadata repository datasource", e); + } + } } 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 a7bf9d3802..caac62456f 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 @@ -50,9 +50,8 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { stmt.setString(1, deviceType.getName()); stmt.execute(); } catch (SQLException e) { - String msg = "Error occurred while registering the device type '" + deviceType.getName() + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while registering the device type " + + "'" + deviceType.getName() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); } @@ -65,85 +64,88 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { @Override public List getDeviceTypes() throws DeviceManagementDAOException { - Connection conn = this.getConnection(); + Connection conn = null; PreparedStatement stmt = null; - List deviceTypes = new ArrayList(); + ResultSet rs = null; + List deviceTypes = null; try { - stmt = conn.prepareStatement("SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE"); - ResultSet results = stmt.executeQuery(); + 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 (results.next()) { + while (rs.next()) { + deviceTypes = new ArrayList(); DeviceType deviceType = new DeviceType(); - deviceType.setId(results.getLong("DEVICE_TYPE_ID")); - deviceType.setName(results.getString("DEVICE_TYPE")); + deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setName(rs.getString("DEVICE_TYPE")); deviceTypes.add(deviceType); } + return deviceTypes; } catch (SQLException e) { - String msg = "Error occurred while fetching the registered device types"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e); } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } - return deviceTypes; } @Override - public DeviceType getDeviceType(Integer id) throws DeviceManagementDAOException { - Connection conn = this.getConnection(); + public DeviceType getDeviceType(int id) throws DeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; - DeviceType deviceType = null; + ResultSet rs = null; try { - stmt = conn.prepareStatement("SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE WHERE ID=?"); + 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); - ResultSet results = stmt.executeQuery(); - while (results.next()) { + rs = stmt.executeQuery(); + DeviceType deviceType = null; + while (rs.next()) { deviceType = new DeviceType(); - deviceType.setId(results.getLong("DEVICE_TYPE_ID")); - deviceType.setName(results.getString("DEVICE_TYPE")); + deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setName(rs.getString("DEVICE_TYPE")); } + return deviceType; } catch (SQLException e) { - String msg = "Error occurred while fetching the registered device type"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while fetching the registered device type", e); } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } - return deviceType; } @Override - public Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException { - - Connection conn = this.getConnection(); + public DeviceType getDeviceType(String type) throws DeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; - ResultSet resultSet = null; - Integer deviceTypeId = null; - + ResultSet rs = null; try { - String createDBQuery = "SELECT * From DM_DEVICE_TYPE DT WHERE DT.NAME=?"; - stmt = conn.prepareStatement(createDBQuery); + conn = this.getConnection(); + String sql = "SELECT ID From DM_DEVICE_TYPE WHERE NAME = ?"; + stmt = conn.prepareStatement(sql); stmt.setString(1, type); - resultSet = stmt.executeQuery(); + rs = stmt.executeQuery(); - while (resultSet.next()) { - deviceTypeId = resultSet.getInt(1); + int id = -1; + if (rs.next()) { + id = rs.getInt("ID"); } + DeviceType deviceType = new DeviceType(); + deviceType.setId(id); + deviceType.setName(type); + return deviceType; } catch (SQLException e) { - String msg = "Error occurred while fetch device type id for device type '" + type + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while fetch device type id for device type " + + "'" + type + "'", e); } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } - - return deviceTypeId; } @Override - public void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException { + public void removeDeviceType(String type) throws DeviceManagementDAOException { } @@ -157,4 +159,5 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { throw new DeviceManagementDAOException(msg, e); } } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java index fae390ac6b..260f546311 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java @@ -23,7 +23,7 @@ import java.io.Serializable; public class Device implements Serializable { private static final long serialVersionUID = -8101106997837486245L; - private Integer id; + private int id; private String description; private String name; private Long dateOfEnrollment; @@ -33,13 +33,22 @@ public class Device implements Serializable { private String ownerId; private String ownerShip; private int tenantId; - private Integer deviceTypeId; + private int deviceTypeId; + private DeviceType deviceType; - public Integer getDeviceTypeId() { + public DeviceType getDeviceType() { + return deviceType; + } + + public void setDeviceType(DeviceType deviceType) { + this.deviceType = deviceType; + } + + public int getDeviceTypeId() { return deviceTypeId; } - public void setDeviceTypeId(Integer deviceTypeId) { + public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java index cd23498202..85f1f948f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java @@ -23,14 +23,14 @@ import java.io.Serializable; public class DeviceType implements Serializable { private static final long serialVersionUID = 7927802716452548282L; - private Long id; + private int id; private String name; - public Long getId() { + public int getId() { return id; } - public void setId(Long id) { + public void setId(int id) { this.id = id; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index bc7a51db48..67c3ac1f5b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -19,10 +19,10 @@ package org.wso2.carbon.device.mgt.core.internal; -import org.wso2.carbon.device.mgt.core.DeviceManager; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; -import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; import org.wso2.carbon.email.verification.util.EmailVerifcationSubscriber; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; @@ -31,7 +31,7 @@ public class DeviceManagementDataHolder { private RealmService realmService; private TenantManager tenantManager; - private DeviceManager deviceManager; + private DeviceManagementService deviceManagerProvider; private LicenseManager licenseManager; private RegistryService registryService; private LicenseConfig licenseConfig; @@ -74,12 +74,12 @@ public class DeviceManagementDataHolder { return tenantManager; } - public DeviceManager getDeviceManager() { - return deviceManager; + public DeviceManagementService getDeviceManagementProvider() { + return deviceManagerProvider; } - public void setDeviceManager(DeviceManager deviceManager) { - this.deviceManager = deviceManager; + public void setDeviceManagementProvider(DeviceManagementService deviceManagerProvider) { + this.deviceManagerProvider = deviceManagerProvider; } public RegistryService getRegistryService() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 332eaaa3fa..b451e2e868 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -22,23 +22,22 @@ import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; -import org.wso2.carbon.device.mgt.core.DeviceManager; -import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; +import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; -import org.wso2.carbon.device.mgt.core.config.license.License; +import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException; -import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementService; -import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.email.verification.util.EmailVerifcationSubscriber; import org.wso2.carbon.registry.core.service.RegistryService; @@ -53,11 +52,11 @@ import org.wso2.carbon.user.core.service.RealmService; * bind="setRealmService" * unbind="unsetRealmService" * @scr.reference name="device.manager.service" - * interface="org.wso2.carbon.device.mgt.common.spi.DeviceManagerService" + * interface="org.wso2.carbon.device.mgt.common.spi.DeviceManager" * cardinality="0..n" * policy="dynamic" - * bind="setDeviceManagerService" - * unbind="unsetDeviceManagerService" + * bind="setDeviceManager" + * unbind="unsetDeviceManager" * @scr.reference name="registry.service" * interface="org.wso2.carbon.registry.core.service.RegistryService" * cardinality="1..1" @@ -75,6 +74,7 @@ public class DeviceManagementServiceComponent { if (log.isDebugEnabled()) { log.debug("Initializing device management core bundle"); } + /* Initializing Device Management Configuration */ DeviceConfigurationManager.getInstance().initConfig(); DeviceManagementConfig config = @@ -83,8 +83,9 @@ public class DeviceManagementServiceComponent { DataSourceConfig dsConfig = config.getDeviceMgtRepository().getDataSourceConfig(); DeviceManagementDAOFactory.init(dsConfig); - DeviceManager deviceManager = new DeviceManagerImpl(config, this.getPluginRepository()); - DeviceManagementDataHolder.getInstance().setDeviceManager(deviceManager); + DeviceManagementService deviceManagementProvider = + new DeviceManagementServiceProviderImpl(this.getPluginRepository()); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProvider); LicenseConfigurationManager.getInstance().initConfig(); LicenseConfig licenseConfig = @@ -99,25 +100,17 @@ public class DeviceManagementServiceComponent { System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP); if (setupOption != null) { if (log.isDebugEnabled()) { - log.debug( - "-Dsetup is enabled. Device management repository schema initialization " + - "is about to begin"); + log.debug("-Dsetup is enabled. Device management repository schema initialization is about to " + + "begin"); } this.setupDeviceManagementSchema(dsConfig); - // this.setupDefaultLicenses(licenseConfig); + this.setupDefaultLicenses(licenseConfig); } - if (log.isDebugEnabled()) { - log.debug("Registering OSGi service DeviceManagementService"); - } - /* Registering Device Management service */ - BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(DeviceManagementService.class.getName(), - new DeviceManagementService(), null); - /* Registering License Management service */ - bundleContext.registerService(LicenseManagementService.class.getName(), - new LicenseManagementService(), null); - if (log.isDebugEnabled()) { + /* Registering declarative service instances exposed by DeviceManagementServiceComponent */ + this.registerServices(componentContext); + + if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } } catch (Throwable e) { @@ -126,6 +119,16 @@ public class DeviceManagementServiceComponent { } } + private void registerServices(ComponentContext componentContext) { + if (log.isDebugEnabled()) { + log.debug("Registering OSGi service DeviceManagementServiceImpl"); + } + /* Registering Device Management Service */ + BundleContext bundleContext = componentContext.getBundleContext(); + bundleContext.registerService(DeviceManagementService.class.getName(), + new DeviceManagementServiceImpl(), null); + } + private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException { DeviceManagementSchemaInitializer initializer = @@ -140,50 +143,54 @@ public class DeviceManagementServiceComponent { "database schema", e); } if (log.isDebugEnabled()) { - log.debug( - "Device management metadata repository schema has been successfully initialized"); + log.debug("Device management metadata repository schema has been successfully initialized"); } } private void setupDefaultLicenses(LicenseConfig licenseConfig) throws LicenseManagementException { - LicenseManager licenseManager = - DeviceManagementDataHolder.getInstance().getLicenseManager(); + LicenseManager licenseManager = DeviceManagementDataHolder.getInstance().getLicenseManager(); for (License license : licenseConfig.getLicenses()) { - License extLicense = - licenseManager.getLicense(license.getName(), license.getLanguage()); - if (extLicense != null) { - continue; + License extLicense = licenseManager.getLicense(license.getName(), license.getLanguage()); + if (extLicense == null) { + licenseManager.addLicense(license.getName(), license);; } - licenseManager.addLicense(license); } } /** * Sets Device Manager service. * - * @param deviceManagerService An instance of DeviceManagerService + * @param deviceManager An instance of DeviceManager */ - protected void setDeviceManagerService(DeviceManagerService deviceManagerService) { + protected void setDeviceManager(DeviceManager deviceManager) { if (log.isDebugEnabled()) { - log.debug("Setting Device Management Service Provider : '" + - deviceManagerService.getProviderType() + "'"); + log.debug("Setting Device Management Service Provider: '" + deviceManager.getProviderType() + "'"); } - this.getPluginRepository().addDeviceManagementProvider(deviceManagerService); - } + try { + this.getPluginRepository().addDeviceManagementProvider(deviceManager); + } catch (DeviceManagementException e) { + log.error("Error occurred while adding device management provider '" + + deviceManager.getProviderType() + "'"); + } + } /** * Unsets Device Management service. * - * @param deviceManagerService An Instance of DeviceManagerService + * @param deviceManager An Instance of DeviceManager */ - protected void unsetDeviceManagerService(DeviceManagerService deviceManagerService) { + protected void unsetDeviceManager(DeviceManager deviceManager) { if (log.isDebugEnabled()) { - log.debug("Unsetting Device Management Service Provider : '" + - deviceManagerService.getProviderType() + "'"); + log.debug("Unsetting Device Management Service Provider : '" + deviceManager.getProviderType() + "'"); } - this.getPluginRepository().removeDeviceManagementProvider(deviceManagerService); - } + try { + this.getPluginRepository().removeDeviceManagementProvider(deviceManager); + } catch (DeviceManagementException e) { + log.error("Error occurred while removing device management provider '" + + deviceManager.getProviderType() + "'"); + } + } /** * Sets Realm Service. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java new file mode 100644 index 0000000000..85535f3f03 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2015, 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.core.license.mgt; + +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.governance.api.generic.GenericArtifactManager; +import org.wso2.carbon.registry.api.Registry; +import org.wso2.carbon.registry.core.exceptions.RegistryException; + +import java.util.HashMap; +import java.util.Map; + +public class GenericArtifactManagerFactory { + + private static Map tenantArtifactManagers = + new HashMap(); + private static final Object lock = new Object(); + + public static GenericArtifactManager getTenantAwareGovernanceArtifactManager() throws + LicenseManagementException { + Registry registry; + int tenantId; + try { + tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + registry = + DeviceManagementDataHolder.getInstance().getRegistryService().getGovernanceSystemRegistry( + tenantId); + } catch (RegistryException e) { + throw new LicenseManagementException("Error occurred while initializing tenant system registry " + + "to be used to manipulate License artifacts", e); + } + + try { + GenericArtifactManager artifactManager; + synchronized (lock) { + artifactManager = + tenantArtifactManagers.get(tenantId); + if (artifactManager == null) { + /* Hack, to fix https://wso2.org/jira/browse/REGISTRY-2427 */ + //GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry); + artifactManager = + new GenericArtifactManager((org.wso2.carbon.registry.core.Registry) registry, + DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY); + tenantArtifactManagers.put(tenantId, artifactManager); + } + } + return artifactManager; + } catch (RegistryException e) { + throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " + + "associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'"); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java index d6fe3df5da..638a9dc881 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java @@ -18,7 +18,9 @@ */ package org.wso2.carbon.device.mgt.core.license.mgt; -import org.wso2.carbon.device.mgt.core.config.license.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; public class LicenseManagementService implements LicenseManager { @@ -29,8 +31,8 @@ public class LicenseManagementService implements LicenseManager { } @Override - public void addLicense(License license) throws LicenseManagementException { - DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(license); + public boolean addLicense(String deviceType, License license) throws LicenseManagementException { + return DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(deviceType, license); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java index dca15462bc..e1ded0d3ee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java @@ -1,57 +1,113 @@ /* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2015, 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 + * 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 + * 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. + * 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.core.license.mgt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; -import org.wso2.carbon.device.mgt.core.config.license.License; -import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.governance.api.exception.GovernanceException; +import org.wso2.carbon.governance.api.generic.GenericArtifactFilter; +import org.wso2.carbon.governance.api.generic.GenericArtifactManager; +import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; +import javax.xml.namespace.QName; import java.text.DateFormat; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Locale; public class LicenseManagerImpl implements LicenseManager { - private static Log log = LogFactory.getLog(DeviceManagerImpl.class); - private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); + private static Log log = LogFactory.getLog(DeviceManagementServiceProviderImpl.class); + private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); - @Override - public License getLicense(final String deviceType, final String languageCode) - throws LicenseManagementException { - License deviceLicense = new License(); - LicenseConfig licenseConfig = DeviceManagementDataHolder.getInstance().getLicenseConfig(); - for (License license : licenseConfig.getLicenses()) { - if ((deviceType.equals(license.getName())) && - (languageCode.equals(license.getLanguage()))) { - deviceLicense = license; - break; - } - } - return deviceLicense; - } + @Override + public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException { + GenericArtifactManager artifactManager = + GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); + try { + GenericArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() { + @Override + public boolean matches(GenericArtifact artifact) throws GovernanceException { + String attributeNameVal = artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.NAME); + String attributeLangVal = artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.LANGUAGE); + return (attributeNameVal != null && attributeLangVal != null && attributeNameVal.equals + (deviceType) && attributeLangVal.equals(languageCode)); + } + }); + if (artifacts == null || artifacts.length <= 0) { + return null; + } + return this.populateLicense(artifacts[0]); + } catch (GovernanceException e) { + throw new LicenseManagementException("Error occurred while retrieving license corresponding to " + + "device type '" + deviceType + "'"); + } catch (ParseException e) { + throw new LicenseManagementException("Error occurred while parsing the ToDate/FromDate date string " + + "of the license configured upon the device type '" + deviceType + "'"); + } + } - @Override - public void addLicense(License license) throws LicenseManagementException { + private License populateLicense(GenericArtifact artifact) throws GovernanceException, ParseException { + License license = new License(); + license.setName(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.NAME)); + license.setProvider(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER)); + license.setVersion(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VERSION)); + license.setLanguage(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE)); + license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.TEXT)); + license.setValidFrom(format.parse(artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.VALID_FROM))); + license.setValidTo(format.parse(artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.VALID_TO))); + return license; + } - } + @Override + public boolean addLicense(String deviceType, License license) throws LicenseManagementException { + GenericArtifactManager artifactManager = + GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); + try { + GenericArtifact artifact = + artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", + DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY)); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.NAME, license.getName()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VERSION, license.getVersion()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER, license.getProvider()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE, license.getLanguage()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.TEXT, license.getText()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO, + license.getValidTo().toString()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM, + license.getValidFrom().toString()); + artifactManager.addGenericArtifact(artifact); + return true; + } catch (GovernanceException e) { + throw new LicenseManagementException("Error occurred while adding license for device type " + + deviceType + "'", e); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java new file mode 100644 index 0000000000..a971ab9d79 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +public class CommandOperation extends Operation { + + private boolean enabled; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java new file mode 100644 index 0000000000..948902d1bf --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +import java.util.ArrayList; +import java.util.List; + +public class ConfigOperation extends Operation { + + private List properties; + + public ConfigOperation() { + properties = new ArrayList(); + } + + public List getConfigProperties() { + return properties; + } + + public void addConfigProperty(String name, Object value, Class type) { + properties.add(new Property(name, value, type)); + } + + public class Property { + private String name; + private Object value; + private Class type; + + public Property(String name, Object value, Class type) { + this.name = name; + this.value = value; + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + public Class getType() { + return type; + } + + public void setType(Class type) { + this.type = type; + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java new file mode 100644 index 0000000000..2dd3b844f7 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; + +import java.util.List; + +/** + * This class implements all the functionalities exposed as part of the OperationManager. Any transaction initiated + * upon persisting information related to operation state, etc has to be managed, demarcated and terminated via the + * methods available in OperationManagementDAOFactory. + */ +public class OperationManagerImpl implements OperationManager { + + private static final Log log = LogFactory.getLog(OperationManagerImpl.class); + + private OperationDAO commandOperationDAO; + private OperationDAO configOperationDAO; + private OperationDAO simpleOperationDAO; + private OperationMappingDAO operationMappingDAO; + private DeviceDAO deviceDAO; + + public OperationManagerImpl() { + commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); + configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO(); + simpleOperationDAO = OperationManagementDAOFactory.getSimpleOperationDAO(); + operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); + deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + } + + @Override + public boolean addOperation(Operation operation, + List devices) throws OperationManagementException { + try { + OperationManagementDAOFactory.beginTransaction(); + int operationId = this.lookupOperationDAO(operation).addOperation(operation); + operationMappingDAO.addOperationMapping(operationId, null); + OperationManagementDAOFactory.commitTransaction(); + return true; + } catch (OperationManagementDAOException e) { + try { + OperationManagementDAOFactory.rollbackTransaction(); + } catch (OperationManagementDAOException e1) { + log.warn("Error occurred while roll-backing the transaction", e1); + } + throw new OperationManagementException("Error occurred while adding operation", e); + } + } + + @Override + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return null; + } + + @Override + public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return null; + } + + @Override + public List getFeatures(String deviceType) throws FeatureManagementException { + return null; + } + + private OperationDAO lookupOperationDAO(Operation operation) { + if (operation instanceof CommandOperation) { + return commandOperationDAO; + } else if (operation instanceof ConfigOperation) { + return configOperationDAO; + } else { + return simpleOperationDAO; + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java new file mode 100644 index 0000000000..2f31c3107e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +public class SimpleOperation extends Operation { +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java new file mode 100644 index 0000000000..e834c756f9 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +import java.util.List; + +public interface OperationDAO { + + int addOperation(Operation operation) throws OperationManagementDAOException; + + int updateOperation(Operation operation) throws OperationManagementDAOException; + + int deleteOperation(int id) throws OperationManagementDAOException; + + Operation getOperation(int id) throws OperationManagementDAOException; + + List getOperations() throws OperationManagementDAOException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java new file mode 100644 index 0000000000..e3e75f2afd --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao; + +public class OperationManagementDAOException extends Exception { + + private static final long serialVersionUID = -3151279311929070299L; + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public OperationManagementDAOException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public OperationManagementDAOException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public OperationManagementDAOException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public OperationManagementDAOException() { + super(); + } + + public OperationManagementDAOException(Throwable cause) { + super(cause); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java new file mode 100644 index 0000000000..8600b00e96 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.CommandOperationDAOImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.ConfigOperationDAOImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.OperationMappingDAOImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.SimpleOperationDAOImpl; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Hashtable; +import java.util.List; + +public class OperationManagementDAOFactory { + + private static DataSource dataSource; + private static final Log log = LogFactory.getLog(OperationManagementDAOFactory.class); + private static ThreadLocal currentConnection = new ThreadLocal(); + + public static OperationDAO getCommandOperationDAO() { + return new CommandOperationDAOImpl(); + } + + public static OperationDAO getConfigOperationDAO() { + return new ConfigOperationDAOImpl(); + } + + public static OperationDAO getSimpleOperationDAO() { + return new SimpleOperationDAOImpl(); + } + + public static OperationMappingDAO getOperationMappingDAO() { + return new OperationMappingDAOImpl(); + } + + public static void init(DataSource dtSource) { + dataSource = dtSource; + } + + public static void beginTransaction() throws OperationManagementDAOException { + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while retrieving datasource connection", e); + } + } + + public static Connection getConnection() { + return currentConnection.get(); + } + + public static void commitTransaction() throws OperationManagementDAOException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.commit(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence commit " + + "has not been attempted"); + } + } + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while committing the transaction", e); + } + } + + public static void rollbackTransaction() throws OperationManagementDAOException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.rollback(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence rollback " + + "has not been attempted"); + } + } + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while rollbacking the transaction", e); + } + } + + /** + * Resolve data source from the data source definition + * + * @param config data source configuration + * @return data source resolved from the data source definition + */ + private static DataSource resolveDataSource(DataSourceConfig config) { + DataSource dataSource = null; + if (config == null) { + throw new RuntimeException( + "Device Management Repository data source configuration " + "is null and " + + "thus, is not initialized"); + } + JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); + if (jndiConfig != null) { + if (log.isDebugEnabled()) { + log.debug("Initializing Device Management Repository data source using the JNDI " + + "Lookup Definition"); + } + List jndiPropertyList = + jndiConfig.getJndiProperties(); + if (jndiPropertyList != null) { + Hashtable jndiProperties = new Hashtable(); + for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { + jndiProperties.put(prop.getName(), prop.getValue()); + } + dataSource = DeviceManagementDAOUtil + .lookupDataSource(jndiConfig.getJndiName(), jndiProperties); + } else { + dataSource = + DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); + } + } + return dataSource; + } + + public static DataSource getDataSource() { + return dataSource; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java new file mode 100644 index 0000000000..bd0b5588c5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class OperationManagementDAOUtil { + + private static final Log log = LogFactory.getLog(OperationManagementDAOUtil.class); + + public static void cleanupResources(Statement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing the result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing the statement", e); + } + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java new file mode 100644 index 0000000000..39873c306a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + +import java.util.List; + +public interface OperationMappingDAO { + + void addOperationMapping(int operationId, List deviceIds) throws OperationManagementDAOException; + + void removeOperationMapping(int operationId, List deviceIds) throws OperationManagementDAOException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java new file mode 100644 index 0000000000..de8ba47afa --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao.impl; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.*; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Timestamp; +import java.util.Date; +import java.sql.SQLException; + +public abstract class AbstractOperationDAO implements OperationDAO { + + public int addOperation(Operation operation) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement( + "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS) VALUES (?, ?, ?, ?)"); + stmt.setString(1, operation.getType().toString()); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setTimestamp(3, null); + stmt.setBoolean(4, false); + stmt.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + int id = -1; + if (rs.next()) { + id = rs.getInt(1); + } + return id; + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java new file mode 100644 index 0000000000..c892315d03 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao.impl; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class CommandOperationDAOImpl extends AbstractOperationDAO { + + @Override + public int addOperation(Operation operation) throws OperationManagementDAOException { + int operationId = super.addOperation(operation); + CommandOperation commandOp = (CommandOperation) operation; + Connection conn = OperationManagementDAOFactory.getConnection(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); + stmt.setInt(1, operationId); + stmt.setBoolean(2, commandOp.isEnabled()); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while adding command operation", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return operationId; + } + + @Override + public int updateOperation(Operation operation) throws OperationManagementDAOException { + return 0; + } + + @Override + public int deleteOperation(int id) throws OperationManagementDAOException { + return 0; + } + + @Override + public Operation getOperation(int id) throws OperationManagementDAOException { + return null; + } + + @Override + public List getOperations() throws OperationManagementDAOException { + return null; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java new file mode 100644 index 0000000000..2a7e71f424 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao.impl; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; + +import javax.sql.DataSource; +import java.util.List; + +public class ConfigOperationDAOImpl extends AbstractOperationDAO { + + @Override + public int addOperation(Operation operation) throws OperationManagementDAOException { + return 0; + } + + @Override + public int updateOperation(Operation operation) throws OperationManagementDAOException { + return 0; + } + + @Override + public int deleteOperation(int id) throws OperationManagementDAOException { + return 0; + } + + @Override + public Operation getOperation(int id) throws OperationManagementDAOException { + return null; + } + + @Override + public List getOperations() throws OperationManagementDAOException { + return null; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java new file mode 100644 index 0000000000..ec05cc2129 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao.impl; + +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +public class OperationMappingDAOImpl implements OperationMappingDAO { + + @Override + public void addOperationMapping(int operationId, List deviceIds) throws OperationManagementDAOException { + PreparedStatement stmt = null; + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "INSERT INTO DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID) VALUES(?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, 0); + stmt.setInt(2, operationId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void removeOperationMapping(int operationId, + List deviceIds) throws OperationManagementDAOException { + PreparedStatement stmt = null; + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, 0); + stmt.setInt(2, operationId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, null); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java new file mode 100644 index 0000000000..09f53bb3b5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015, 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.core.operation.mgt.dao.impl; + +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; + +import javax.sql.DataSource; +import java.util.List; + +public class SimpleOperationDAOImpl extends AbstractOperationDAO { + + @Override + public int updateOperation(Operation operation) throws OperationManagementDAOException { + return 0; + } + + @Override + public int deleteOperation(int id) throws OperationManagementDAOException { + return 0; + } + + @Override + public Operation getOperation(int id) throws OperationManagementDAOException { + return null; + } + + @Override + public List getOperations() throws OperationManagementDAOException { + return null; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index cb908edd7e..b024829c55 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -4,9 +4,9 @@ * 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 + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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 @@ -18,79 +18,21 @@ package org.wso2.carbon.device.mgt.core.service; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.OperationManager; -import org.wso2.carbon.device.mgt.core.DeviceManager; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import java.util.List; -public class DeviceManagementService implements DeviceManager { +/** + * Proxy class for all Device Management related operations that take the corresponding plugin type in + * and resolve the appropriate plugin implementation + */ +public interface DeviceManagementService extends DeviceManager, LicenseManager, OperationManager { - @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().enrollDevice(device); - } + List getAllDevices(String type) throws DeviceManagementException; - @Override - public boolean modifyEnrollment(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().modifyEnrollment(device); - } - - @Override - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager() - .disenrollDevice(deviceId); - } - - @Override - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().isEnrolled(deviceId); - } - - @Override - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().isActive(deviceId); - } - - @Override - public boolean setActive(DeviceIdentifier deviceId, boolean status) - throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager() - .setActive(deviceId, status); - } - - @Override - public List getAllDevices(String type) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().getAllDevices(type); - } - - @Override - public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId) - throws DeviceManagementException { - - Device device = - DeviceManagementDataHolder.getInstance().getDeviceManager().getDevice(deviceId); - return device; - } - - @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().updateDeviceInfo(device); - } - - @Override - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager() - .setOwnership(deviceId, ownershipType); - } - - @Override - public OperationManager getOperationManager(String type) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager(). - getOperationManager(type); - } + List getDeviceListOfUser(String username) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java new file mode 100644 index 0000000000..c7b6937031 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2014, 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.core.service; + +import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; + +import java.util.List; + +public class DeviceManagementServiceImpl implements DeviceManagementService { + + @Override + public String getProviderType() { + return null; + } + + @Override + public FeatureManager getFeatureManager() { + return null; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().enrollDevice(device); + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().modifyEnrollment(device); + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().disenrollDevice(deviceId); + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().isEnrolled(deviceId); + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().isActive(deviceId); + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().setActive(deviceId, status); + } + + @Override + public List getAllDevices() throws DeviceManagementException { + return null; + } + + @Override + public List getAllDevices(String type) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getAllDevices(type); + } + + @Override + public List getDeviceListOfUser(String username) throws DeviceManagementException{ + return null; + } + + @Override + public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId); + } + + @Override + public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().updateDeviceInfo(device); + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().setOwnership(deviceId, + ownershipType); + } + + @Override + public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getLicense(deviceType, + languageCode); + } + + @Override + public boolean addLicense(String type, License license) throws LicenseManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().addLicense(type, license); + } + + @Override + public boolean addOperation(Operation operation, + List devices) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().addOperation(operation, devices); + } + + @Override + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperations(deviceId); + } + + @Override + public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getPendingOperations(deviceId); + } + + @Override + public List getFeatures(String deviceType) throws FeatureManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getFeatures(deviceType); + } + +} 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 7660a0124b..9a24a982d3 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 @@ -51,11 +51,10 @@ public final class DeviceManagerUtil { DocumentBuilder docBuilder = factory.newDocumentBuilder(); return docBuilder.parse(file); } catch (Exception e) { - throw new DeviceManagementException( - "Error occurred while parsing file, while converting " + - "to a org.w3c.dom.Document : " + e.getMessage(), e); + throw new DeviceManagementException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document", e); } - } + } /** * Resolve data source from the data source definition. @@ -66,16 +65,13 @@ public final class DeviceManagerUtil { public static DataSource resolveDataSource(DataSourceConfig config) { DataSource dataSource = null; if (config == null) { - throw new RuntimeException( - "Device Management Repository data source configuration " + - "is null and thus, is not initialized"); + throw new RuntimeException("Device Management Repository data source configuration is null and thus, " + + "is not initialized"); } JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); if (jndiConfig != null) { if (log.isDebugEnabled()) { - log.debug( - "Initializing Device Management Repository data source using the JNDI " + - "Lookup Definition"); + log.debug("Initializing Device Management Repository data source using the JNDI Lookup Definition"); } List jndiPropertyList = jndiConfig.getJndiProperties(); @@ -84,11 +80,9 @@ public final class DeviceManagerUtil { for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { jndiProperties.put(prop.getName(), prop.getValue()); } - dataSource = DeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), jndiProperties); + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties); } else { - dataSource = - DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); } } return dataSource; @@ -104,7 +98,7 @@ public final class DeviceManagerUtil { boolean status; try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType); if (deviceTypeId == null) { DeviceType dt = new DeviceType(); dt.setName(deviceType); @@ -112,14 +106,14 @@ public final class DeviceManagerUtil { } status = true; } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while registering the device type " + deviceType; - throw new DeviceManagementException(msg, e); + throw new DeviceManagementException("Error occurred while registering the device type '" + + deviceType + "'", e); } return status; } /** - * Unregisters an existing device type from the device management metadata repository. + * Un-registers an existing device type from the device management metadata repository. * * @param deviceType device type * @return status of the operation @@ -127,27 +121,19 @@ public final class DeviceManagerUtil { public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException { try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType); if (deviceTypeId == null) { DeviceType dt = new DeviceType(); dt.setName(deviceType); - deviceTypeDAO.removeDeviceType(dt); + deviceTypeDAO.removeDeviceType(deviceType); } return true; } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while registering the device type " + deviceType; - throw new DeviceManagementException(msg, e); + throw new DeviceManagementException("Error occurred while registering the device type '" + + deviceType + "'", e); } } - public static Map convertPropertiesToMap(List properties) { - Map propertiesMap = new HashMap(); - for (Device.Property prop : properties) { - propertiesMap.put(prop.getName(), prop.getValue()); - } - return propertiesMap; - } - public static void sendEmail(){ } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java new file mode 100644 index 0000000000..e0e32265ec --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2015, 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.core; + +import org.apache.tomcat.jdbc.pool.PoolProperties; +import org.testng.Assert; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +public class DeviceManagementBaseTest { + + private DataSource dataSource; + + public void init() { + this.initDataSource(); + try { + this.initDeviceManagementDatabaseSchema(); + } catch (SQLException e) { + Assert.fail("Error occurred while initializing database schema", e); + } + } + + private void initDeviceManagementDatabaseSchema() throws SQLException { + Connection conn = null; + Statement stmt = null; + try { + if (dataSource == null) { + Assert.fail("Device management datasource is not initialized peroperly"); + } + conn = dataSource.getConnection(); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + } + } + + private void initDataSource() { + PoolProperties properties = new PoolProperties(); + properties.setUrl("jdbc:h2:mem:MDM_DB;DB_CLOSE_DELAY=-1"); + properties.setDriverClassName("org.h2.Driver"); + properties.setUsername("wso2carbon"); + properties.setPassword("wso2carbon"); + this.dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties); + } + + protected DataSource getDataSource() { + return dataSource; + } + +} 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 79c19a0e0e..2b85365f2d 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 @@ -20,7 +20,8 @@ package org.wso2.carbon.device.mgt.core; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; public class DeviceManagementRepositoryTests { @@ -33,23 +34,27 @@ public class DeviceManagementRepositoryTests { @Test public void testAddDeviceManagementService() { - DeviceManagerService sourceProvider = new TestDeviceManagerService(); - this.getRepository().addDeviceManagementProvider(sourceProvider); - - DeviceManagerService targetProvider = - this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); - + DeviceManager sourceProvider = new TestDeviceManager(); + try { + this.getRepository().addDeviceManagementProvider(sourceProvider); + } catch (DeviceManagementException e) { + Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e); + } + DeviceManager targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManager.DEVICE_TYPE_TEST); Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); } @Test(dependsOnMethods = "testAddDeviceManagementService") public void testRemoveDeviceManagementService() { - DeviceManagerService sourceProvider = new TestDeviceManagerService(); - this.getRepository().removeDeviceManagementProvider(sourceProvider); - - DeviceManagerService targetProvider = - this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); - + DeviceManager sourceProvider = new TestDeviceManager(); + try { + this.getRepository().removeDeviceManagementProvider(sourceProvider); + } catch (DeviceManagementException e) { + Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e); + } + DeviceManager targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManager.DEVICE_TYPE_TEST); Assert.assertNull(targetProvider); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java new file mode 100644 index 0000000000..6c29280d8f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2015, 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.core; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.operation.mgt.*; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; + +import java.util.ArrayList; +import java.util.List; + +public class DeviceOperationManagementTests extends DeviceManagementBaseTest { + + private OperationManager operationManager; + + @BeforeClass(alwaysRun = true) + public void init() { + super.init(); + this.initOperationManager(); + OperationManagementDAOFactory.init(this.getDataSource()); + } + + public void initOperationManager() { + this.operationManager = new OperationManagerImpl(); + } + + @Test + public void testAddOperation() throws Exception { + + CommandOperation op = new CommandOperation(); + op.setEnabled(true); + op.setType(Operation.Type.COMMAND); + + List deviceIds = new ArrayList(); + DeviceIdentifier deviceId = new DeviceIdentifier(); + deviceId.setId("Test"); + deviceId.setType("Android"); + deviceIds.add(deviceId); + + try { + operationManager.addOperation(op, deviceIds); + } catch (OperationManagementException e) { + e.printStackTrace(); + throw new Exception(e); + } + } + + public void testGetOperations() { + try { + operationManager.getOperations(null); + } catch (OperationManagementException e) { + e.printStackTrace(); + } + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java similarity index 87% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index 0870b10307..4c44ee0c59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -20,18 +20,23 @@ package org.wso2.carbon.device.mgt.core; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.OperationManager; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import java.util.List; -public class TestDeviceManagerService implements DeviceManagerService { +public class TestDeviceManager implements DeviceManager { public static final String DEVICE_TYPE_TEST = "Test"; @Override public String getProviderType() { - return TestDeviceManagerService.DEVICE_TYPE_TEST; + return TestDeviceManager.DEVICE_TYPE_TEST; + } + + @Override + public FeatureManager getFeatureManager() { + return null; } @Override @@ -84,9 +89,4 @@ public class TestDeviceManagerService implements DeviceManagerService { return false; } - @Override - public OperationManager getOperationManager() throws DeviceManagementException { - return null; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java index 7ccc88d343..486859d61f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java @@ -114,17 +114,17 @@ public class DeviceManagementDAOTests { deviceType.setName("IOS"); deviceTypeMgtDAO.addDeviceType(deviceType); - Long id = null; + int id = -1; Connection conn = null; PreparedStatement stmt = null; - String sql = "SELECT ID, NAME from DM_DEVICE_TYPE DType where DType.NAME='IOS'"; + String sql = "SELECT dt.ID, dt.NAME FROM DM_DEVICE_TYPE dt where dt.NAME = 'IOS'"; try { conn = this.getDataSource().getConnection(); stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); if (rs.next()) { - id = rs.getLong("ID"); + id = rs.getInt("ID"); } } catch (SQLException e) { throw new DeviceManagementDAOException("error in fetch device type by name IOS", e); @@ -147,9 +147,9 @@ public class DeviceManagementDAOTests { device.setDeviceIdentificationId("111"); DeviceType deviceType = new DeviceType(); - deviceType.setId(Long.parseLong("1")); + deviceType.setId(Integer.parseInt("1")); - device.setDeviceTypeId(deviceType.getId().intValue()); + device.setDeviceTypeId(deviceType.getId()); device.setOwnerShip(OwnerShip.BYOD.toString()); device.setOwnerId("111"); device.setTenantId(-1234); 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 new file mode 100644 index 0000000000..c87c9fcf4e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -0,0 +1,58 @@ +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( + ID INT auto_increment NOT NULL, + NAME VARCHAR(300) NULL DEFAULT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE ( + ID INTEGER auto_increment NOT NULL, + DESCRIPTION TEXT NULL DEFAULT NULL, + NAME VARCHAR(100) NULL DEFAULT NULL, + DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(15) NULL DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, + OWNER VARCHAR(45) NULL DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_OPERATION ( + ID INTEGER AUTO_INCREMENT NOT NULL, + TYPE VARCHAR(50) NOT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + RECEIVED_TIMESTAMP TIMESTAMP NULL, + STATUS VARCHAR(50) NULL, + PRIMARY KEY (ID) +) + +CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +) + +CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +) + +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +) + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index dbce1ebf52..797d214a0e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -28,6 +28,7 @@ + \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java b/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java index 04faae5f03..a43922b4ab 100644 --- a/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java @@ -51,7 +51,7 @@ public class FeatureFilterImpl implements FeatureFilter { public List extractFeatures(List policyList) { List featureList = new ArrayList(); for (Policy policy : policyList) { - featureList.addAll(policy.getFeaturesList()); + featureList.addAll(policy.getProfile().getFeaturesList()); } return featureList; } diff --git a/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java index cbb398654b..22af278741 100644 --- a/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java @@ -63,7 +63,7 @@ public class PolicyFilterImpl implements PolicyFilter { List policies = new ArrayList(); for (Policy policy : policyList) { - if (policy.getDeviceType().equalsIgnoreCase(deviceType)) { + if (policy.getProfile().getDeviceType().getName().equalsIgnoreCase(deviceType)) { policies.add(policy); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/PolicyInformationServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/PolicyInformationServiceImpl.java index 5ce43efddf..2cdb6bcf9f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/PolicyInformationServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/PolicyInformationServiceImpl.java @@ -18,17 +18,18 @@ package org.wso2.carbon.policy.information.point; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.PIPDeviceData; import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyInformationService; +import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import java.util.List; -public class PolicyInformationServiceImpl implements PolicyInformationService{ +public class PolicyInformationServiceImpl implements PolicyInformationPoint { @Override - public void getDeviceData(PIPDeviceData pipDeviceData) { - + public PIPDeviceData getDeviceData(DeviceIdentifier deviceIdentifier) { + return null; } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/internal/PolicyInformationPointBundleActivator.java b/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/internal/PolicyInformationPointBundleActivator.java index c25b2855db..b28a545dd6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/internal/PolicyInformationPointBundleActivator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.information.point/src/main/java/org/wso2/carbon/policy/information/point/internal/PolicyInformationPointBundleActivator.java @@ -24,7 +24,7 @@ import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.wso2.carbon.policy.information.point.PolicyInformationServiceImpl; -import org.wso2.carbon.policy.mgt.common.PolicyInformationService; +import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; public class PolicyInformationPointBundleActivator implements BundleActivator { @@ -38,7 +38,7 @@ public class PolicyInformationPointBundleActivator implements BundleActivator { log.debug("Activating Policy information Point bundle."); } - pipServiceRegRef = bundleContext.registerService(PolicyInformationService.class.getName(), + pipServiceRegRef = bundleContext.registerService(PolicyInformationPoint.class.getName(), new PolicyInformationServiceImpl(), null); if (log.isDebugEnabled()) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml index c90bdcccb7..1685a5b72a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml @@ -54,7 +54,9 @@ Policy Management Common Bundle org.wso2.carbon.policy.mgt.common.internal - org.apache.commons.logging + org.apache.commons.logging, + org.wso2.carbon.device.mgt.common.*, + org.wso2.carbon.device.mgt.core.dto.* org.wso2.carbon.policy.mgt.common.* @@ -82,6 +84,10 @@ org.testng testng + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java index 2a180ae441..0023cf6326 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java @@ -23,9 +23,18 @@ public class Feature { private int id; private String code; private String name; + private String description; private Object attribute; private String ruleValue; + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + public String getRuleValue() { return ruleValue; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java index 172e1cd529..7f9dd8e4fd 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/FeatureManagerService.java @@ -30,5 +30,5 @@ public interface FeatureManagerService { List getFeatures() throws FeatureManagementException; - List getFeaturesOfPolicy(int policyId) throws FeatureManagementException; + List getFeaturesOfPolicy(int profileId) throws FeatureManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDeviceData.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDeviceData.java index caf5c81e2c..d234fa96b1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDeviceData.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDeviceData.java @@ -18,14 +18,18 @@ package org.wso2.carbon.policy.mgt.common; + +import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; + import java.sql.Timestamp; import java.util.List; import java.util.Map; public class PIPDeviceData { - String deviceId; - String deviceType; + Device device; + DeviceType deviceType; String ownershipType; List userIds; List roles; @@ -36,4 +40,75 @@ public class PIPDeviceData { /*This will be used to record attributes to which would come from other PDPs*/ Map attributes; + public Device getDevice() { + return device; + } + + public void setDevice(Device device) { + this.device = device; + } + + public DeviceType getDeviceType() { + return deviceType; + } + + public void setDeviceType(DeviceType deviceType) { + this.deviceType = deviceType; + } + + public String getOwnershipType() { + return ownershipType; + } + + public void setOwnershipType(String ownershipType) { + this.ownershipType = ownershipType; + } + + public List getUserIds() { + return userIds; + } + + public void setUserIds(List userIds) { + this.userIds = userIds; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + public String getAltitude() { + return altitude; + } + + public void setAltitude(String altitude) { + this.altitude = altitude; + } + + public String getLongitude() { + return longitude; + } + + public void setLongitude(String longitude) { + this.longitude = longitude; + } + + public Timestamp getTimestamp() { + return timestamp; + } + + public void setTimestamp(Timestamp timestamp) { + this.timestamp = timestamp; + } + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index f0f930f57f..130f367fd5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -28,13 +28,12 @@ import java.util.Map; public class Policy { private int id; // Identifier of the policy. private int priorityId; // Priority of the policies. This will be used only for simple evaluation. + private Profile profile; // Profile id private String policyName; // Name of the policy. - private List featuresList; // Features included in the policies. private boolean generic; // If true, this should be applied to all related device. private List roleList; // Roles which this policy should be applied. private String ownershipType; // Ownership type (COPE, BYOD, CPE) private List DeviceList; // Individual devices this policy should be applied - private String deviceType; // Device type to apply the policy. /*Dynamic policy attributes*/ @@ -71,6 +70,14 @@ public class Policy { this.priorityId = priorityId; } + public Profile getProfile() { + return profile; + } + + public void setProfile(Profile profile) { + this.profile = profile; + } + public String getPolicyName() { return policyName; } @@ -79,14 +86,6 @@ public class Policy { this.policyName = policyName; } - public List getFeaturesList() { - return featuresList; - } - - public void setFeaturesList(List featuresList) { - this.featuresList = featuresList; - } - public boolean isGeneric() { return generic; } @@ -119,14 +118,6 @@ public class Policy { DeviceList = deviceList; } - public String getDeviceType() { - return deviceType; - } - - public void setDeviceType(String deviceType) { - this.deviceType = deviceType; - } - public int getStartTime() { return startTime; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java similarity index 72% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorService.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 4534254ad9..2174b64c12 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -22,7 +22,7 @@ package org.wso2.carbon.policy.mgt.common; * This interface defines the policy management which should be implemented by the plugins */ -public interface PolicyAdministratorService { +public interface PolicyAdministratorPoint { /** * This method adds a policy to the platform @@ -31,7 +31,10 @@ public interface PolicyAdministratorService { * @return primary key (generated key) */ - int addPolicy(Policy policy); + Policy addPolicy(Policy policy) throws PolicyManagementException; + + + Policy updatePolicy(Policy policy) throws PolicyManagementException; /** * This method adds a policy per device which should be implemented by the related plugins. @@ -42,17 +45,7 @@ public interface PolicyAdministratorService { * @return primary key (generated key) */ - int addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException; - - /** - * This method adds a policy to device type by the related device type plugins. - * - * @param deviceType - * @param policy - * @return primary key (generated key) - */ - - int addPolicyToDeviceType(String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException; + Policy addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException; /** * This method adds the policy to specific role. @@ -61,7 +54,7 @@ public interface PolicyAdministratorService { * @param policy * @return primary key (generated key) */ - int addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException; + Policy addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException; /** * This method returns the policy of whole platform @@ -102,6 +95,7 @@ public interface PolicyAdministratorService { /** * This method checks weather a policy is available for a device. + * * @param deviceId * @param deviceType * @return @@ -112,20 +106,38 @@ public interface PolicyAdministratorService { /** * This method checks weather a policy is used by a particular device. + * * @param deviceId * @param deviceType * @return * @throws PolicyManagementException */ - boolean isPolicyUsed(String deviceId, String deviceType) throws PolicyManagementException; + boolean isPolicyApplied(String deviceId, String deviceType) throws PolicyManagementException; /** - * * @param deviceId * @param deviceType * @param policy * @throws PolicyManagementException */ void setPolicyUsed(String deviceId, String deviceType, Policy policy) throws PolicyManagementException; + + /** + * This method will add the profile to database, + * @param profile + * @throws PolicyManagementException + */ + Profile addProfile(Profile profile) throws PolicyManagementException; + + boolean deleteProfile(int profileId) throws PolicyManagementException; + + Profile updateProfile(Profile profile) throws PolicyManagementException; + + Feature addFeature(Feature feature) throws FeatureManagementException; + + Feature updateFeature(Feature feature) throws FeatureManagementException; + + void deleteFeature(int featureId) throws FeatureManagementException; + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java similarity index 75% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationService.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java index b5082c578d..944e3aa620 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java @@ -19,26 +19,28 @@ package org.wso2.carbon.policy.mgt.common; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + import java.util.List; /** * This is the interface which will be used to create plug-able policy decision points. */ -public interface PolicyEvaluationService { +public interface PolicyEvaluationPoint { /** * This method returns the effective policy from the list. - * @param pipDeviceData device information. + * @param deviceIdentifier device information. * @return returns the effective policy. */ - Policy getEffectivePolicy(PIPDeviceData pipDeviceData); + Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier); /** * This class will return the effective feature set from the list. - * @param pipDeviceData device information. + * @param deviceIdentifier device information. * @return returns the effective feature set. */ - List getEffectiveFeatures(PIPDeviceData pipDeviceData); + List getEffectiveFeatures(DeviceIdentifier deviceIdentifier); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyInformationService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyInformationPoint.java similarity index 77% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyInformationService.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyInformationPoint.java index ea9509506a..0ba6f912ff 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyInformationService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyInformationPoint.java @@ -19,26 +19,30 @@ package org.wso2.carbon.policy.mgt.common; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.core.dto.Device; + import java.util.List; /** * This will be used retrieve data database. This interface has to be implemented by PIP. PIP will be plug-able. */ -public interface PolicyInformationService { +public interface PolicyInformationPoint { /** * This method will return the data related Device, Some of the device data will provided in the initial pipDeviceData object such as * device id, device time and location, Other data such as roles, owned users and ownership type will be filled by this method. - * @param pipDeviceData device data. + * @param deviceIdentifier device data. + * @return PIPDeviceData */ - void getDeviceData(PIPDeviceData pipDeviceData); + PIPDeviceData getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; /** * This method will retrieve the policies related given device Data. * @param pipDeviceData * @return */ - List getRelatedPolicies(PIPDeviceData pipDeviceData); + List getRelatedPolicies(PIPDeviceData pipDeviceData) throws PolicyManagementException; /** * This is will retrieve the features related to device type. This feature list will be used for dynamically @@ -46,5 +50,5 @@ public interface PolicyInformationService { * @param deviceType * @return */ - List getRelatedFeatures(String deviceType); + List getRelatedFeatures(String deviceType) throws FeatureManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java new file mode 100644 index 0000000000..9df30554a0 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java @@ -0,0 +1,93 @@ +/* +* Copyright (c) 2015 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.policy.mgt.common; + + + +import org.wso2.carbon.device.mgt.core.dto.DeviceType; + +import java.sql.Timestamp; +import java.util.List; + +public class Profile { + + private int profileId; + private String profileName; + private int tenantId; + private DeviceType deviceType; + private Timestamp createdDate; + private Timestamp updatedDate; + private List featuresList; // Features included in the policies. + + public DeviceType getDeviceType() { + return deviceType; + } + + public void setDeviceType(DeviceType deviceType) { + this.deviceType = deviceType; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } + + public List getFeaturesList() { + return featuresList; + } + + public void setFeaturesList(List featuresList) { + this.featuresList = featuresList; + } + + public int getProfileId() { + return profileId; + } + + public void setProfileId(int profileId) { + this.profileId = profileId; + } + + public String getProfileName() { + return profileName; + } + + public void setProfileName(String profileName) { + this.profileName = profileName; + } + + public Timestamp getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(Timestamp createdDate) { + this.createdDate = createdDate; + } + + public Timestamp getUpdatedDate() { + return updatedDate; + } + + public void setUpdatedDate(Timestamp updatedDate) { + this.updatedDate = updatedDate; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java deleted file mode 100644 index 3ec34a90f2..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/impl/PolicyManagement.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2014, 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.policy.mgt.common.impl; - -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorService; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; - -public class PolicyManagement implements PolicyAdministratorService { - @Override - public int addPolicy(Policy policy) { - return 0; - } - - @Override - public int addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException { - return 0; - } - - @Override - public int addPolicyToDeviceType(String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException { - return 0; - } - - @Override - public int addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException { - return 0; - } - - @Override - public Policy getPolicy() { - return null; - } - - @Override - public Policy getPolicyOfDevice(String deviceId, String deviceType) throws FeatureManagementException, PolicyManagementException { - return null; - } - - @Override - public Policy getPolicyOfDeviceType(String deviceType) throws FeatureManagementException, PolicyManagementException { - return null; - } - - @Override - public Policy getPolicyOfRole(String roleName) throws FeatureManagementException, PolicyManagementException { - return null; - } - - @Override - public boolean isPolicyAvailableForDevice(String deviceId, String deviceType) throws PolicyManagementException { - return false; - } - - @Override - public boolean isPolicyUsed(String deviceId, String deviceType) throws PolicyManagementException { - return false; - } - - @Override - public void setPolicyUsed(String deviceId, String deviceType, Policy policy) throws PolicyManagementException { - - } -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql new file mode 100644 index 0000000000..9102ed00c1 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql @@ -0,0 +1,211 @@ +SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; +SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; +SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; + +CREATE SCHEMA IF NOT EXISTS `WSO2CDM` DEFAULT CHARACTER SET latin1 ; +USE `WSO2CDM` ; + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE_TYPE` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ( + `ID` INT(11) NOT NULL , + `NAME` VARCHAR(300) NULL DEFAULT NULL , + PRIMARY KEY (`ID`) ) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE` ( + `ID` VARCHAR(20) NOT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `NAME` VARCHAR(100) NULL DEFAULT NULL , + `DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL , + `DATE_OF_LAST_UPDATE` BIGINT(20) NULL DEFAULT NULL , + `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL , + `STATUS` VARCHAR(15) NULL DEFAULT NULL , + `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , + `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , + `OWNER` VARCHAR(45) NULL DEFAULT NULL , + `TENANT_ID` INT(11) NULL DEFAULT '0' , + PRIMARY KEY (`ID`) , + INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_PROFILE` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE` ( + `ID` INT NOT NULL AUTO_INCREMENT , + `PROFILE_NAME` VARCHAR(45) NOT NULL , + `TENANT_ID` INT NOT NULL , + `CREATED_TIME` DATETIME NOT NULL , + `UPDATED_TIME` DATETIME NOT NULL , + PRIMARY KEY (`ID`) ) +ENGINE = InnoDB; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_POLICY` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `NAME` VARCHAR(45) NULL DEFAULT NULL , + `TENANT_ID` INT(11) NOT NULL , + `PROFILE_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC) , + CONSTRAINT `FK_DM_PROFILE_DM_POLICY` + FOREIGN KEY (`PROFILE_ID` ) + REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE_POLICY` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `DEVICE_ID` INT(11) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_POLICY_DEVICE_POLICY` (`POLICY_ID` ASC) , + CONSTRAINT `FK_POLICY_DEVICE_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` ( + `ID` INT(11) NOT NULL , + `DEVICE_TYPE_ID` INT(11) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_DEVICE_TYPE_POLICY` (`POLICY_ID` ASC) , + INDEX `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `FK_DEVICE_TYPE_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_FEATURES` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `NAME` VARCHAR(256) NOT NULL , + `CODE` VARCHAR(45) NULL DEFAULT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `EVALUVATION_RULE` VARCHAR(60) NOT NULL , + PRIMARY KEY (`ID`) ) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_POLICY_FEATURES` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `PROFILE_ID` INT(11) NOT NULL , + `FEATURE_ID` INT(11) NOT NULL , + `CONTENT` BLOB NULL DEFAULT NULL , + PRIMARY KEY (`ID`) , + INDEX `fk_DM_POLICY_FEATURES_DM_FEATURES1` (`FEATURE_ID` ASC) , + INDEX `FK_DM_PROFILE_DM_POLICY_FEATURES` (`PROFILE_ID` ASC) , + CONSTRAINT `fk_DM_POLICY_FEATURES_DM_FEATURES1` + FOREIGN KEY (`FEATURE_ID` ) + REFERENCES `WSO2CDM`.`DM_FEATURES` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DM_PROFILE_DM_POLICY_FEATURES` + FOREIGN KEY (`PROFILE_ID` ) + REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_ROLE_POLICY` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ( + `ID` INT(11) NOT NULL , + `ROLE_NAME` VARCHAR(45) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_ROLE_POLICY_POLICY` (`POLICY_ID` ASC) , + CONSTRAINT `FK_ROLE_POLICY_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_LOCATION` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_LOCATION` ( + `LAT` VARCHAR(45) NOT NULL , + `LONG` VARCHAR(45) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + INDEX `FK_DM_POLICY_DM_LOCATION` (`POLICY_ID` ASC) , + CONSTRAINT `FK_DM_POLICY_DM_LOCATION` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_TIME` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_TIME` ( + `STARTING_TIME` DATETIME NOT NULL , + `ENDING_TIME` DATETIME NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + INDEX `FK_DM_POLICY_DM_TIME` (`POLICY_ID` ASC) , + CONSTRAINT `FK_DM_POLICY_DM_TIME` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + + +SET SQL_MODE=@OLD_SQL_MODE; +SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; +SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java deleted file mode 100644 index 568c59684e..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/PolicyManagementTestCase.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2014, 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.wos2.carbon.policy.mgt.common; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.testng.Assert; -import org.testng.annotations.Test; -import org.wos2.carbon.policy.mgt.common.utils.PolicyCreator; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.impl.PolicyManagement; - -public class PolicyManagementTestCase { - - private static final Log log = LogFactory.getLog(PolicyManagementTestCase.class); - - Policy policy = PolicyCreator.createPolicy(); - - private PolicyManagement policyManagement = new PolicyManagement(); - - @Test(groups = "policy.mgt.test", description = "Testing the adding policy to a device") - public void testAddPolicy() throws FeatureManagementException, PolicyManagementException { - Assert.assertEquals(policyManagement.addPolicyToDevice("1212-ESDD-12ER-7890", "MD", policy), 0); - } - - @Test(groups = "policy.mgt.test", description = "Testing the adding policy to a device type") - public void testAddPolicyToDeviceType() throws FeatureManagementException, PolicyManagementException { - Assert.assertEquals(policyManagement.addPolicyToDeviceType("MD", policy), 0); - } - - @Test(groups = "policy.mgt.test", description = "Testing the adding policy to a user Role") - public void testAddPolicyToRole() throws FeatureManagementException, PolicyManagementException { - Assert.assertEquals(policyManagement.addPolicyToRole("Admin", policy), 0); - } - -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java index 0f276076e4..6ea9e58422 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/test/java/org/wos2/carbon/policy/mgt/common/utils/PolicyCreator.java @@ -20,6 +20,7 @@ package org.wos2.carbon.policy.mgt.common.utils; import org.wso2.carbon.policy.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.Profile; import java.util.ArrayList; import java.util.List; @@ -38,7 +39,14 @@ public class PolicyCreator { List featureList = new ArrayList(); featureList.add(feature); - policy.setFeaturesList(featureList); + Profile profile = new Profile(); + profile.setProfileId(1); + profile.setProfileName("Test-01"); + profile.setTenantId(-1234); + + policy.setProfile(profile); + profile.setFeaturesList(featureList); + policy.setPolicyName("Camera_related_policy"); return policy; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 5095cca991..9f4a752714 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -18,7 +18,8 @@ ~ under the License. --> - + org.wso2.carbon.devicemgt policy-mgt @@ -66,7 +67,9 @@ org.w3c.dom, org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.user.core.*, - org.wso2.carbon.utils + org.wso2.carbon.utils.*, + org.wso2.carbon.device.mgt.core.*, + org.wso2.carbon.device.mgt.common.* !org.wso2.carbon.policy.mgt.core.internal, @@ -75,6 +78,21 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18 + + + file:src/test/resources/log4j.properties + + + src/test/resources/testng.xml + + + + @@ -113,6 +131,30 @@ org.wso2.carbon.devicemgt org.wso2.carbon.policy.mgt.common + + + + + org.testng + testng + + + org.apache.tomcat.wso2 + jdbc-pool + + + + mysql + mysql-connector-java + 5.1.34 + test + + + com.h2database.wso2 + h2-database-engine + test + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManager.java new file mode 100644 index 0000000000..4aa1a1b828 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManager.java @@ -0,0 +1,50 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.*; + +import java.util.List; + +public interface PolicyManager { + + Feature addFeature(Feature feature) throws FeatureManagementException; + + Feature updateFeature(Feature feature) throws FeatureManagementException; + + Profile addProfile(Profile profile) throws PolicyManagementException; + + Profile updateProfile(Profile profile) throws PolicyManagementException; + + Policy addPolicy(Policy policy) throws PolicyManagementException; + + Policy updatePolicy(Policy policy) throws PolicyManagementException; + + Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + + Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException; + + List getPolicies(String deviceType) throws PolicyManagementException; + + List getFeatures() throws FeatureManagementException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerImpl.java new file mode 100644 index 0000000000..7b86d252e3 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerImpl.java @@ -0,0 +1,93 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; + +import java.util.List; + +public class PolicyManagerImpl implements PolicyManager { + + private static final Log log = LogFactory.getLog(PolicyManagerImpl.class); + + PolicyAdministratorPointImpl policyAdministratorPoint; + + public PolicyManagerImpl() { + policyAdministratorPoint = new PolicyAdministratorPointImpl(); + } + + @Override + public Feature addFeature(Feature feature) throws FeatureManagementException { + return policyAdministratorPoint.addFeature(feature); + } + + @Override + public Feature updateFeature(Feature feature) throws FeatureManagementException { + return policyAdministratorPoint.updateFeature(feature); + } + + @Override + public Profile addProfile(Profile profile) throws PolicyManagementException { + return policyAdministratorPoint.addProfile(profile); + } + + @Override + public Profile updateProfile(Profile profile) throws PolicyManagementException { + return policyAdministratorPoint.updateProfile(profile); + } + + @Override + public Policy addPolicy(Policy policy) throws PolicyManagementException { + return policyAdministratorPoint.addPolicy(policy); + } + + @Override + public Policy updatePolicy(Policy policy) throws PolicyManagementException { + return policyAdministratorPoint.updatePolicy(policy); + } + + @Override + public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + return null; + } + + @Override + public Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException { + return null; + } + + @Override + public List getPolicies(String deviceType) throws PolicyManagementException { + return null; + } + + @Override + public List getFeatures() throws FeatureManagementException { + return null; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java new file mode 100644 index 0000000000..1014756202 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.dao; + +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Profile; + +import java.util.List; + +public interface FeatureDAO { + + Feature addFeature(Feature feature) throws FeatureManagerDAOException; + + Feature updateFeature(Feature feature) throws FeatureManagerDAOException; + + List getAllFeatures() throws FeatureManagerDAOException; + + List getFeaturesForProfile(int ProfileId) throws FeatureManagerDAOException; + + void deleteFeature(int featureId) throws FeatureManagerDAOException; + + void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java new file mode 100644 index 0000000000..02ff4a00d6 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureManagerDAOException.java @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.dao; + +public class FeatureManagerDAOException extends Exception { + + private String featureDAOErrorMessage; + + public String getFeatureDAOErrorMessage() { + return featureDAOErrorMessage; + } + + public void setFeatureDAOErrorMessage(String featureDAOErrorMessage) { + this.featureDAOErrorMessage = featureDAOErrorMessage; + } + + public FeatureManagerDAOException(String message) { + super(message); + setFeatureDAOErrorMessage(message); + } + + public FeatureManagerDAOException(String message, Exception ex) { + super(message, ex); + setFeatureDAOErrorMessage(message); + } + + public FeatureManagerDAOException(String message, Throwable cause) { + super(message, cause); + setFeatureDAOErrorMessage(message); + } + + public FeatureManagerDAOException() { + super(); + } + + public FeatureManagerDAOException(Throwable cause) { + super(cause); + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index f0bf968700..8915e234ec 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -18,21 +18,61 @@ package org.wso2.carbon.policy.mgt.core.dao; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.Profile; + +import java.util.List; public interface PolicyDAO { - int addPolicy(Policy policy) throws PolicyManagerDAOException; + Policy addPolicy(Policy policy) throws PolicyManagerDAOException; - int addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException; + Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException; - int addPolicy(String deviceID, String deviceType, Policy policy) throws PolicyManagerDAOException; + Policy addPolicyToRole(String roleName, Policy policy) throws PolicyManagerDAOException; - void updatePolicy(int id, Policy policy) throws PolicyManagerDAOException; + Policy addPolicy(String deviceID, String deviceType, Policy policy) throws PolicyManagerDAOException; + + Policy updatePolicy(Policy policy) throws PolicyManagerDAOException; Policy getPolicy() throws PolicyManagerDAOException; Policy getPolicy(String deviceType) throws PolicyManagerDAOException; Policy getPolicy(String deviceID, String deviceType) throws PolicyManagerDAOException; + + void deletePolicy(Policy policy) throws PolicyManagerDAOException; + + + + + + + + /* Profile addProfile(Profile profile) throws PolicyManagerDAOException; + + Profile updateProfile(Profile profile) throws PolicyManagerDAOException; + + void deleteProfile(Profile profile) throws PolicyManagerDAOException; + + List getAllProfiles() throws PolicyManagerDAOException; + + List getProfilesOfDeviceType(String deviceType) throws PolicyManagerDAOException; + + + + + List getAllFeatures() throws PolicyManagerDAOException; + + List getFeaturesForProfile(int ProfileId) throws PolicyManagerDAOException; + + void deleteFeature(int featureId) throws PolicyManagerDAOException; + + void deleteFeaturesOfProfile(Profile profile) throws PolicyManagerDAOException; + + Feature addFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException; + + Feature updateFeature(Feature feature) throws PolicyManagerDAOException, FeatureManagementException;*/ } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index e1512f34b7..2df823b03c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -35,14 +35,22 @@ public class PolicyManagementDAOFactory { private static final Log log = LogFactory.getLog(PolicyManagementDAOFactory.class); - public static PolicyDAO getDeviceTypeDAO() { - return new PolicyDAOImpl(dataSource); - } public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); } + public static void init(DataSource dtSource) { + dataSource = dtSource; + } + + public static DataSource getDataSource() { + if (dataSource != null) { + return dataSource; + } + throw new RuntimeException("Data source is not yet configured."); + } + /** * Resolve data source from the data source definition * diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java new file mode 100644 index 0000000000..b35354d8ad --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java @@ -0,0 +1,38 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.dao; + +import org.wso2.carbon.policy.mgt.common.Profile; + +import java.util.List; + +public interface ProfileDAO { + + Profile addProfile(Profile profile) throws ProfileManagerDAOException; + + Profile updateProfile(Profile profile) throws ProfileManagerDAOException; + + void deleteProfile(Profile profile) throws ProfileManagerDAOException; + + List getAllProfiles() throws ProfileManagerDAOException; + + List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java new file mode 100644 index 0000000000..ba859ed784 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileManagerDAOException.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.dao; + +public class ProfileManagerDAOException extends Exception{ + + private String profileDAOErrorMessage; + + public String getProfileDAOErrorMessage() { + return profileDAOErrorMessage; + } + + public void setProfileDAOErrorMessage(String profileDAOErrorMessage) { + this.profileDAOErrorMessage = profileDAOErrorMessage; + } + + public ProfileManagerDAOException(String message) { + super(message); + setProfileDAOErrorMessage(message); + } + + public ProfileManagerDAOException(String message, Exception ex) { + super(message, ex); + setProfileDAOErrorMessage(message); + } + + public ProfileManagerDAOException(String message, Throwable cause) { + super(message, cause); + setProfileDAOErrorMessage(message); + } + + public ProfileManagerDAOException() { + super(); + } + + public ProfileManagerDAOException(Throwable cause) { + super(cause); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java new file mode 100644 index 0000000000..42913cc7d6 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -0,0 +1,196 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.dao.*; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class FeatureDAOImpl implements FeatureDAO { + + private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); + + + @Override + public Feature addFeature(Feature feature) throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE) VALUES (?, ?, ?, ?)"; + stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); + stmt.setString(1, feature.getName()); + stmt.setString(2, feature.getCode()); + stmt.setString(3, feature.getDescription()); + stmt.setString(4, feature.getRuleValue()); + + int affectedRows = stmt.executeUpdate(); + + if (log.isDebugEnabled()) { + log.debug(affectedRows + " Features are added."); + } + generatedKeys = stmt.getGeneratedKeys(); + while (generatedKeys.next()) { + feature.setId(generatedKeys.getInt(1)); + } + } catch (SQLException e) { + String msg = "Error occurred while adding feature to the database."; + log.error(msg, e); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + return feature; + } + + + @Override + public Feature updateFeature(Feature feature) throws FeatureManagerDAOException { + return feature; + } + + @Override + public void deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profile.getProfileId()); + stmt.executeUpdate(); + } catch (SQLException e) { + String msg = "Error occurred while deleting the feature related to a profile."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + + @Override + public List getAllFeatures() throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + List featureList = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT ID, NAME, CODE, EVALUVATION_RULE FROM DM_FEATURES"; + stmt = conn.prepareStatement(query); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Feature feature = new Feature(); + feature.setId(resultSet.getInt("ID")); + feature.setCode(resultSet.getString("CODE")); + feature.setName(resultSet.getString("NAME")); + feature.setRuleValue(resultSet.getString("EVALUVATION_RULE")); + featureList.add(feature); + } + + } catch (SQLException e) { + String msg = "Unable to get the list of the features from database."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return featureList; + } + + @Override + public List getFeaturesForProfile(int profileId) throws FeatureManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + List featureList = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " + + "F.EVALUVATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " + + "JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID WHERE PROFILE_ID=?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profileId); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Feature feature = new Feature(); + feature.setId(resultSet.getInt("FEATURE_ID")); + feature.setCode(resultSet.getString("CODE")); + feature.setName(resultSet.getString("NAME")); + feature.setAttribute(resultSet.getObject("CONTENT")); + feature.setRuleValue(resultSet.getString("RULE")); + featureList.add(feature); + } + + } catch (SQLException e) { + String msg = "Unable to get the list of the features from database."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return featureList; + } + + @Override + public void deleteFeature(int featureId) throws FeatureManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } + } + + private Connection getConnection() throws FeatureManagerDAOException { + try { + return PolicyManagementDAOFactory.getDataSource().getConnection(); + } catch (SQLException e) { + throw new FeatureManagerDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", e); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 7b8769ad86..c28a5ca087 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -20,64 +20,282 @@ package org.wso2.carbon.policy.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.Profile; import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; -import javax.sql.DataSource; import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class PolicyDAOImpl implements PolicyDAO { - private static DataSource dataSource; private static final Log log = LogFactory.getLog(PolicyDAOImpl.class); - public PolicyDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; + + @Override + public Policy addPolicy(Policy policy) throws PolicyManagerDAOException { + persistPolicy(policy); + return policy; } @Override - public int addPolicy(Policy policy) throws PolicyManagerDAOException { - return 0; + public Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException { + + // First persist the policy to the data base. + persistPolicy(policy); + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, getDeviceTypeId(deviceType)); + stmt.setInt(2, policy.getId()); + + stmt.executeQuery(); + + } catch (SQLException e) { + String msg = "Error occurred while adding the device type policy to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + + return policy; + } @Override - public int addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException { - return 0; + public Policy addPolicyToRole(String roleName, Policy policy) throws PolicyManagerDAOException { + return null; } @Override - public int addPolicy(String deviceID, String deviceType, Policy policy) throws PolicyManagerDAOException { - return 0; + public Policy addPolicy(String deviceID, String deviceType, Policy policy) throws PolicyManagerDAOException { + + // First persist the policy to the data base. + persistPolicy(policy); + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + + return policy; } @Override - public void updatePolicy(int id, Policy policy) throws PolicyManagerDAOException { + public Policy updatePolicy(Policy policy) throws PolicyManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (Exception e) { + + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + + return policy; } @Override public Policy getPolicy() throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (Exception e) { + + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + return null; } @Override public Policy getPolicy(String deviceType) throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (Exception e) { + + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + return null; } @Override public Policy getPolicy(String deviceID, String deviceType) throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (Exception e) { + + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + return null; } + @Override + public void deletePolicy(Policy policy) throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY WHERE ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, policy.getId()); + stmt.executeUpdate(); + + if (log.isDebugEnabled()) { + log.debug("Policy (" + policy.getPolicyName() + ") delete from database."); + } + } catch (Exception e) { + String msg = "Unable to delete the policy (" + policy.getPolicyName() + ") from database."; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + + } + private Connection getConnection() throws PolicyManagerDAOException { try { - return dataSource.getConnection(); + return PolicyManagementDAOFactory.getDataSource().getConnection(); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while obtaining a connection from the policy " + "management metadata repository datasource", e); } } + + + private void persistPolicy(Policy policy) throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + // TODO : find a way to get the tenant Id. + int tenantId = -1234; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); + + stmt.setString(1, policy.getPolicyName()); + stmt.setInt(2, policy.getProfile().getProfileId()); + stmt.setInt(3, tenantId); + + int affectedRows = stmt.executeUpdate(); + + if (affectedRows == 0 && log.isDebugEnabled()) { + String msg = "No rows are updated on the policy table."; + log.debug(msg); + } + generatedKeys = stmt.getGeneratedKeys(); + + if (generatedKeys.next()) { + policy.setId(generatedKeys.getInt(1)); + } + // checking policy id here, because it object could have passed with id from the calling method. + if (policy.getId() == 0) { + throw new RuntimeException("No rows were inserted, policy id cannot be null."); + } + } catch (SQLException e) { + String msg = "Error occurred while adding policy to the database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + } + + + + + + + + + /** + * This method returns the device type id when supplied with device type name. + * + * @param deviceType + * @return + * @throws PolicyManagerDAOException + */ + private int getDeviceTypeId(String deviceType) throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + int deviceTypeId = -1; + try { + conn = this.getConnection(); + String query = "SELECT ID FROM DM_DEVICE_TYPE WHERE NAME = ?"; + stmt = conn.prepareStatement(query); + stmt.setString(1, deviceType); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + deviceTypeId = resultSet.getInt("ID"); + } + } catch (SQLException e) { + String msg = "Error occurred while selecting the device type id."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return deviceTypeId; + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java new file mode 100644 index 0000000000..0c09c36eb9 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -0,0 +1,195 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.dao.*; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class ProfileDAOImpl implements ProfileDAO { + + private static final Log log = LogFactory.getLog(ProfileDAOImpl.class); + + + public Profile addProfile(Profile profile) throws ProfileManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + // TODO : find a way to get the tenant Id. + int tenantId = -1234; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE (PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); + + stmt.setString(1, profile.getProfileName()); + stmt.setInt(2, tenantId); + stmt.setLong(3, profile.getDeviceType().getId()); + stmt.setTimestamp(4, profile.getCreatedDate()); + stmt.setTimestamp(5, profile.getUpdatedDate()); + + int affectedRows = stmt.executeUpdate(); + + if (affectedRows == 0 && log.isDebugEnabled()) { + String msg = "No rows are updated on the profile table."; + log.debug(msg); + } + generatedKeys = stmt.getGeneratedKeys(); + + if (generatedKeys.next()) { + profile.setProfileId(generatedKeys.getInt(1)); + } + // Checking the profile id here, because profile id could have been passed from the calling method. + if (profile.getProfileId() == 0) { + throw new RuntimeException("Profile id is 0, this could be an issue."); + } + + persistFeatures(profile); + + } catch (SQLException e) { + String msg = "Error occurred while adding the profile to database."; + log.error(msg, e); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, generatedKeys); + } + + return profile; + } + + + public Profile updateProfile(Profile profile) throws ProfileManagerDAOException { + return profile; + } + + @Override + public void deleteProfile(Profile profile) throws ProfileManagerDAOException { + + // First delete the features related to the profile + FeatureDAOImpl featureDAO = new FeatureDAOImpl(); + try { + featureDAO.deleteFeaturesOfProfile(profile); + } catch (FeatureManagerDAOException e) { + String msg = "Error occurred while deleting features."; + log.error(msg, e); + throw new ProfileManagerDAOException(msg, e); + } + + + Connection conn = null; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profile.getProfileId()); + stmt.executeUpdate(); + } catch (SQLException e) { + String msg = "Error occurred while deleting the profile from the data base."; + log.error(msg); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + private void persistFeatures(Profile profile) throws ProfileManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_ID, CONTENT) VALUES (?, ?, ?)"; + + stmt = conn.prepareStatement(query); + for (Feature feature : profile.getFeaturesList()) { + stmt.setInt(1, profile.getProfileId()); + stmt.setInt(2, feature.getId()); + stmt.setObject(3, feature.getAttribute()); + stmt.addBatch(); + //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 + } + stmt.executeBatch(); + } catch (SQLException e) { + String msg = "Error occurred while adding the feature list to the database."; + log.error(msg, e); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + + @Override + public List getAllProfiles() throws ProfileManagerDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } + + return null; + } + + @Override + public List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException { + + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + + try { + conn = this.getConnection(); + String query = ""; + stmt = conn.prepareStatement(query); + } catch (SQLException e) { + + } + + return null; + } + + + private Connection getConnection() throws ProfileManagerDAOException { + try { + return PolicyManagementDAOFactory.getDataSource().getConnection(); + } catch (SQLException e) { + throw new ProfileManagerDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", e); + } + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java new file mode 100644 index 0000000000..c21bf86d69 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -0,0 +1,197 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.Profile; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; + +public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { + + private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class); + + PolicyDAOImpl policyDAO; + FeatureDAOImpl featureDAO; + ProfileDAOImpl profileDAO; + + public PolicyAdministratorPointImpl() { + policyDAO = new PolicyDAOImpl(); + featureDAO = new FeatureDAOImpl(); + profileDAO = new ProfileDAOImpl(); + } + + @Override + public Policy addPolicy(Policy policy) throws PolicyManagementException { + try { + policy = policyDAO.addPolicy(policy); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while persisting the policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return policy; + } + + public Policy updatePolicy(Policy policy) throws PolicyManagementException { + try { + policy = policyDAO.updatePolicy(policy); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while updating the policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return policy; + } + + @Override + public Policy addPolicyToDevice(String deviceId, String deviceType, Policy policy) + throws FeatureManagementException, PolicyManagementException { + + try { + policy = policyDAO.addPolicy(deviceId, deviceType, policy); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while persisting the policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return policy; + } + + @Override + public Policy addPolicyToRole(String roleName, Policy policy) + throws FeatureManagementException, PolicyManagementException { + try { + policy = policyDAO.addPolicyToRole(roleName, policy); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while persisting the policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return policy; + } + + @Override + public Policy getPolicy() { + return null; + } + + @Override + public Policy getPolicyOfDevice(String deviceId, String deviceType) + throws FeatureManagementException, PolicyManagementException { + return null; + } + + @Override + public Policy getPolicyOfDeviceType(String deviceType) + throws FeatureManagementException, PolicyManagementException { + return null; + } + + @Override + public Policy getPolicyOfRole(String roleName) throws FeatureManagementException, PolicyManagementException { + return null; + } + + @Override + public boolean isPolicyAvailableForDevice(String deviceId, String deviceType) throws PolicyManagementException { + return false; + } + + @Override + public boolean isPolicyApplied(String deviceId, String deviceType) throws PolicyManagementException { + return false; + } + + @Override + public void setPolicyUsed(String deviceId, String deviceType, Policy policy) throws PolicyManagementException { + + } + + @Override + public Profile addProfile(Profile profile) throws PolicyManagementException { + try { + profile = profileDAO.addProfile(profile); + } catch (ProfileManagerDAOException e) { + String msg = "Error occurred while persisting the policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + + return profile; + } + + @Override + public boolean deleteProfile(int profileId) throws PolicyManagementException { + return false; + } + + @Override + public Profile updateProfile(Profile profile) throws PolicyManagementException { + try { + profile = profileDAO.updateProfile(profile); + } catch (ProfileManagerDAOException e) { + String msg = "Error occurred while persisting the profile."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + + return profile; + } + + @Override + public Feature addFeature(Feature feature) throws FeatureManagementException { + try { + feature = featureDAO.addFeature(feature); + } catch (FeatureManagerDAOException e) { + String msg = "Error occurred while persisting the feature."; + log.error(msg, e); + throw new FeatureManagementException(msg, e); + } + + return feature; + } + + @Override + public Feature updateFeature(Feature feature) throws FeatureManagementException { + try { + feature = featureDAO.updateFeature(feature); + } catch (FeatureManagerDAOException e) { + String msg = "Error occurred while persisting the feature."; + log.error(msg, e); + throw new FeatureManagementException(msg, e); + } + return feature; + } + + @Override + public void deleteFeature(int featureId) throws FeatureManagementException { + + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java new file mode 100644 index 0000000000..4bec7d6980 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -0,0 +1,67 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +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.impl.DeviceDAOImpl; +import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.policy.mgt.common.*; + +import java.util.List; + +public class PolicyInformationPointImpl implements PolicyInformationPoint { + + private static final Log log = LogFactory.getLog(PolicyInformationPointImpl.class); + DeviceDAOImpl deviceDAO; + + public PolicyInformationPointImpl() { + deviceDAO = new DeviceDAOImpl(DeviceManagementDAOFactory.getDataSource()); + } + + @Override + public PIPDeviceData getDeviceData(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + PIPDeviceData pipDeviceData = new PIPDeviceData(); + Device device; + try { + device = deviceDAO.getDevice(deviceIdentifier); + pipDeviceData.setDevice(device); + + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred when retrieving the data related to device from the database."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + + return pipDeviceData; + } + + @Override + public List getRelatedPolicies(PIPDeviceData pipDeviceData) throws PolicyManagementException { + return null; + } + + @Override + public List getRelatedFeatures(String deviceType) throws FeatureManagementException { + return null; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index 16255d95dc..f7fce484b1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -21,11 +21,13 @@ package org.wso2.carbon.policy.mgt.core.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; -import org.wso2.carbon.policy.mgt.common.PolicyInformationService; +import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; +import org.wso2.carbon.policy.mgt.core.PolicyManager; import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; import org.wso2.carbon.policy.mgt.core.config.PolicyManagementConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.service.PolicyManagementService; import org.wso2.carbon.user.core.service.RealmService; /** @@ -37,7 +39,7 @@ import org.wso2.carbon.user.core.service.RealmService; * bind="setRealmService" * unbind="unsetRealmService" * @scr.reference name="org.wso2.carbon.devicemgt.policy.information.point.default" - * interface="org.wso2.carbon.policy.mgt.common.PolicyInformationService" + * interface="org.wso2.carbon.policy.mgt.common.PolicyInformationPoint" * cardinality="1..1" * policy="dynamic" * bind="setPIPService" @@ -56,6 +58,9 @@ public class PolicyManagementServiceComponent { DataSourceConfig dsConfig = config.getPolicyManagementRepository().getDataSourceConfig(); PolicyManagementDAOFactory.init(dsConfig); + componentContext.getBundleContext().registerService( + PolicyManager.class.getName(), new PolicyManagementService(), null); + } catch (Throwable t) { String msg = "Error occurred while initializing the Policy management core."; log.error(msg, t); @@ -89,13 +94,13 @@ public class PolicyManagementServiceComponent { } - protected void setPIPService(PolicyInformationService policyInformationService) { + protected void setPIPService(PolicyInformationPoint policyInformationService) { if (log.isDebugEnabled()) { log.debug("Setting Policy Information Service"); } } - protected void unsetPIPService(PolicyInformationService policyInformationService) { + protected void unsetPIPService(PolicyInformationPoint policyInformationService) { if (log.isDebugEnabled()) { log.debug("Unsetting Policy Information Service"); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java new file mode 100644 index 0000000000..4154c2bc84 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -0,0 +1,87 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.service; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.core.PolicyManager; +import org.wso2.carbon.policy.mgt.core.PolicyManagerImpl; + +import java.util.List; + +public class PolicyManagementService implements PolicyManager { + + + PolicyManager policyManager; + + public PolicyManagementService() { + policyManager = new PolicyManagerImpl(); + } + + @Override + public Feature addFeature(Feature feature) throws FeatureManagementException { + return policyManager.addFeature(feature); + } + + @Override + public Feature updateFeature(Feature feature) throws FeatureManagementException { + return policyManager.updateFeature(feature); + } + + @Override + public Profile addProfile(Profile profile) throws PolicyManagementException { + return policyManager.addProfile(profile); + } + + @Override + public Profile updateProfile(Profile profile) throws PolicyManagementException { + return policyManager.updateProfile(profile); + } + + @Override + public Policy addPolicy(Policy policy) throws PolicyManagementException { + return policyManager.addPolicy(policy); + } + + @Override + public Policy updatePolicy(Policy policy) throws PolicyManagementException { + return policyManager.updatePolicy(policy); + } + + @Override + public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + return policyManager.getEffectivePolicy(deviceIdentifier); + } + + @Override + public Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException { + return policyManager.getEffectiveFeatures(deviceIdentifier); + } + + @Override + public List getPolicies(String deviceType) throws PolicyManagementException { + return policyManager.getPolicies(deviceType); + } + + @Override + public List getFeatures() throws FeatureManagementException { + return policyManager.getFeatures(); + } +} 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 new file mode 100644 index 0000000000..2c4d670cca --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -0,0 +1,159 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.tomcat.jdbc.pool.PoolProperties; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.w3c.dom.Document; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.common.DBTypes; +import org.wso2.carbon.policy.mgt.core.common.TestDBConfiguration; +import org.wso2.carbon.policy.mgt.core.common.TestDBConfigurations; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; +import org.wso2.carbon.policy.mgt.core.util.FeatureCreator; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import javax.sql.DataSource; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.sql.Connection; +import java.sql.Statement; +import java.util.List; + +public class PolicyDAOTestCase { + + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(PolicyDAOTestCase.class); + + @BeforeClass + @Parameters("dbType") + public void setUpDB(String dbTypeStr) throws Exception { + DBTypes dbType = DBTypes.valueOf(dbTypeStr); + TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); + PoolProperties properties = new PoolProperties(); + + log.info("Database Type : " + dbTypeStr); + + switch (dbType) { + + case MySql: + + log.info("Mysql Called..................................................." + dbTypeStr); + + properties.setUrl(dbConfig.getConnectionUrl()); + properties.setDriverClassName(dbConfig.getDriverClass()); + properties.setUsername(dbConfig.getUserName()); + properties.setPassword(dbConfig.getPwd()); + dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties); + PolicyManagementDAOFactory.init(dataSource); + break; + + case H2: + + properties.setUrl(dbConfig.getConnectionUrl()); + properties.setDriverClassName(dbConfig.getDriverClass()); + properties.setUsername(dbConfig.getUserName()); + properties.setPassword(dbConfig.getPwd()); + dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties); + this.initH2SQLScript(); + PolicyManagementDAOFactory.init(dataSource); + break; + + default: + } + } + + private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws PolicyManagerDAOException, + PolicyManagementException { + File deviceMgtConfig = new File("src/test/resources/testdbconfig.xml"); + Document doc; + TestDBConfigurations dbConfigs; + + doc = PolicyManagerUtil.convertToDocument(deviceMgtConfig); + JAXBContext testDBContext; + + try { + testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); + Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); + dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new PolicyManagerDAOException("Error parsing test db configurations", e); + } + for (TestDBConfiguration config : dbConfigs.getDbTypesList()) { + if (config.getDbType().equals(dbType.toString())) { + return config; + } + } + return null; + } + + private void initH2SQLScript() throws Exception { + Connection conn = null; + Statement stmt = null; + try { + conn = this.getDataSource().getConnection(); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + + } + } + + private void initMySQlSQLScript() throws Exception { + Connection conn = null; + Statement stmt = null; + try { + conn = this.getDataSource().getConnection(); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateMySqlTestDB.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + } + } + + private DataSource getDataSource() { + return dataSource; + } + + @Test + public void addFeatures() throws FeatureManagerDAOException { + + FeatureDAOImpl policyDAO = new FeatureDAOImpl(); + List featureList = FeatureCreator.getFeatureList(); + for (Feature feature : featureList) { + policyDAO.addFeature(feature); + } + + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/TestUtils.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/TestUtils.java new file mode 100644 index 0000000000..3061f38fc3 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/TestUtils.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2014, 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.policy.mgt.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class TestUtils { + + private static final Log log = LogFactory.getLog(TestUtils.class); + + public static void cleanupResources(Connection conn, Statement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DBTypes.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DBTypes.java new file mode 100644 index 0000000000..7362d80451 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DBTypes.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2014, 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.policy.mgt.core.common; + + +public enum DBTypes { + Oracle("Oracle"),H2("H2"),MySql("MySql"); + + String dbName ; + DBTypes(String dbStrName) { + dbName = dbStrName; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfiguration.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfiguration.java new file mode 100644 index 0000000000..633041fdd2 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfiguration.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2014, 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.policy.mgt.core.common; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DBType") +public class TestDBConfiguration { + + private String connectionUrl; + private String driverClass; + private String userName; + private String pwd; + + @Override public String toString() { + return "TestDBConfiguration{" + + "connectionUrl='" + connectionUrl + '\'' + + ", driverClass='" + driverClass + '\'' + + ", userName='" + userName + '\'' + + ", pwd='" + pwd + '\'' + + ", dbType='" + dbType + '\'' + + '}'; + } + + private String dbType; + + @XmlElement(name = "connectionurl", nillable = false) + public String getConnectionUrl() { + return connectionUrl; + } + + public void setConnectionUrl(String connectionUrl) { + this.connectionUrl = connectionUrl; + } + + @XmlElement(name = "driverclass", nillable = false) + public String getDriverClass() { + return driverClass; + } + + public void setDriverClass(String driverClass) { + this.driverClass = driverClass; + } + + @XmlElement(name = "userName", nillable = false) + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @XmlElement(name = "pwd", nillable = false) + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + @XmlAttribute(name = "typeName") + public String getDbType() { + return dbType; + } + + public void setDbType(String dbType) { + this.dbType = dbType; + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfigurations.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfigurations.java new file mode 100644 index 0000000000..629b8106a1 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfigurations.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014, 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.policy.mgt.core.common; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "DeviceMgtTestDBConfigurations") +public class TestDBConfigurations { + + private List dbTypesList; + + @XmlElement(name = "DBType") + public List getDbTypesList() { + return dbTypesList; + } + + public void setDbTypesList(List dbTypesList) { + this.dbTypesList = dbTypesList; + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java new file mode 100644 index 0000000000..ea06c80c87 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/FeatureCreator.java @@ -0,0 +1,115 @@ +/* +* Copyright (c) 2015 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.policy.mgt.core.util; + +import org.wso2.carbon.policy.mgt.common.Feature; + +import java.util.ArrayList; +import java.util.List; + +public class FeatureCreator { + + public static List getFeatureList() { + + Feature feature1 = new Feature(); + feature1.setName("Camera"); + feature1.setCode("C001"); + feature1.setDescription("Camera"); + feature1.setRuleValue("permit_override"); + + + Feature feature2 = new Feature(); + feature2.setName("LOCK"); + feature2.setCode("L001"); + feature2.setDescription("Lock the phone"); + feature2.setRuleValue("deny_override"); + + + Feature feature3 = new Feature(); + feature3.setName("WIFI"); + feature3.setCode("W001"); + feature3.setDescription("Wifi configuration for the device"); + feature3.setRuleValue("all_available"); + + Feature feature4 = new Feature(); + feature4.setName("RING"); + feature4.setCode("R001"); + feature4.setDescription("Ring the mobile"); + feature4.setRuleValue("first_applicable"); + + Feature feature5 = new Feature(); + feature5.setName("LDAP"); + feature5.setCode("L002"); + feature5.setDescription("LDAP Configurations"); + feature5.setRuleValue("all_available"); + + + Feature feature6 = new Feature(); + feature6.setName("VPN"); + feature6.setCode("V001"); + feature6.setDescription("VPN config for accessing the company network from out side"); + feature6.setRuleValue("all_available"); + + + Feature feature7 = new Feature(); + feature7.setName("PASSWORD"); + feature7.setCode("P001"); + feature7.setDescription("Setting the password for the mobile"); + feature7.setRuleValue("first_applicable"); + + Feature feature8 = new Feature(); + feature8.setName("WIPE"); + feature8.setCode("W002"); + feature8.setDescription("Wiping the company profile created to access the company secure data"); + feature8.setRuleValue("permit_override"); + + Feature feature9 = new Feature(); + feature9.setName("ENCRYPTION"); + feature9.setCode("E001"); + feature9.setDescription("Adding the encryption for the phone and SD card."); + feature9.setRuleValue("permit_override"); + + Feature feature10 = new Feature(); + feature10.setName("APP"); + feature10.setCode("A001"); + feature10.setDescription("Installing an application to the phone"); + feature10.setRuleValue("permit_override"); + + Feature feature11 = new Feature(); + feature11.setName("EMAIL"); + feature11.setCode("E002"); + feature11.setDescription("Email configurations of the phone."); + feature11.setRuleValue("all_applicable"); + + List featureList = new ArrayList(); + featureList.add(feature1); + featureList.add(feature2); + featureList.add(feature3); + featureList.add(feature4); + featureList.add(feature5); + featureList.add(feature6); + featureList.add(feature7); + featureList.add(feature8); + featureList.add(feature9); + featureList.add(feature10); + featureList.add(feature11); + + return featureList; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/log4j.properties b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/log4j.properties new file mode 100644 index 0000000000..7da6d6c9e1 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/log4j.properties @@ -0,0 +1,32 @@ +# +# Copyright 2009 WSO2, Inc. (http://wso2.com) +# +# Licensed 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. +# + +# +# This is the log4j configuration file used by WSO2 Carbon +# +# IMPORTANT : Please do not remove or change the names of any +# of the Appenders defined here. The layout pattern & log file +# can be changed using the WSO2 Carbon Management Console, and those +# settings will override the settings in this file. +# + +log4j.rootLogger=DEBUG, STD_OUT + +# Redirect log messages to console +log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender +log4j.appender.STD_OUT.Target=System.out +log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout +log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 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 new file mode 100644 index 0000000000..0fd55c652d --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -0,0 +1,213 @@ + +-- ----------------------------------------------------- +-- Table `DM_DEVICE_TYPE` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE` ( + `ID` INT(11) NOT NULL , + `NAME` VARCHAR(300) NULL DEFAULT NULL , + PRIMARY KEY (`ID`) ) + +; + + +-- ----------------------------------------------------- +-- Table `DM_DEVICE` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_DEVICE` ( + `ID` VARCHAR(20) NOT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `NAME` VARCHAR(100) NULL DEFAULT NULL , + `DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL , + `DATE_OF_LAST_UPDATE` BIGINT(20) NULL DEFAULT NULL , + `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL , + `STATUS` VARCHAR(15) NULL DEFAULT NULL , + `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , + `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , + `OWNER` VARCHAR(45) NULL DEFAULT NULL , + `TENANT_ID` INT(11) NULL DEFAULT '0' , + PRIMARY KEY (`ID`) , + CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) + +; + + +-- ----------------------------------------------------- +-- Table `DM_PROFILE` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_PROFILE` ( + `ID` INT NOT NULL AUTO_INCREMENT , + `PROFILE_NAME` VARCHAR(45) NOT NULL , + `TENANT_ID` INT NOT NULL , + `DEVICE_TYPE_ID` INT NOT NULL , + `CREATED_TIME` DATETIME NOT NULL , + `UPDATED_TIME` DATETIME NOT NULL , + PRIMARY KEY (`ID`) , + CONSTRAINT `DM_PROFILE_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +; + + +-- ----------------------------------------------------- +-- Table `DM_POLICY` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `NAME` VARCHAR(45) NULL DEFAULT NULL , + `TENANT_ID` INT(11) NOT NULL , + `PROFILE_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + CONSTRAINT `FK_DM_PROFILE_DM_POLICY` + FOREIGN KEY (`PROFILE_ID` ) + REFERENCES `DM_PROFILE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) + +; + + +-- ----------------------------------------------------- +-- Table `DM_DEVICE_POLICY` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_DEVICE_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `DEVICE_ID` VARCHAR(20) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + CONSTRAINT `FK_POLICY_DEVICE_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DEVICE_DEVICE_POLICY` + FOREIGN KEY (`DEVICE_ID` ) + REFERENCES `DM_DEVICE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION); + +; + + +-- ----------------------------------------------------- +-- Table `DM_DEVICE_TYPE_POLICY` +-- ----------------------------------------------------- + + +CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE_POLICY` ( + `ID` INT(11) NOT NULL , + `DEVICE_TYPE_ID` INT(11) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + CONSTRAINT `FK_DEVICE_TYPE_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION); + +; + + +-- ----------------------------------------------------- +-- Table `DM_FEATURES` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `NAME` VARCHAR(256) NOT NULL , + `CODE` VARCHAR(45) NULL DEFAULT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `EVALUVATION_RULE` VARCHAR(60) NOT NULL , + PRIMARY KEY (`ID`) ); + +; + + +-- ----------------------------------------------------- +-- Table `DM_PROFILE_FEATURES` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS `DM_PROFILE_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `PROFILE_ID` INT(11) NOT NULL , + `FEATURE_ID` INT(11) NOT NULL , + `CONTENT` BLOB NULL DEFAULT NULL , + PRIMARY KEY (`ID`) , + CONSTRAINT `fk_DM_POLICY_FEATURES_DM_FEATURES1` + FOREIGN KEY (`FEATURE_ID` ) + REFERENCES `DM_FEATURES` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DM_PROFILE_DM_POLICY_FEATURES` + FOREIGN KEY (`PROFILE_ID` ) + REFERENCES `DM_PROFILE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION); + +; + + +-- ----------------------------------------------------- +-- Table `DM_ROLE_POLICY` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( + `ID` INT(11) NOT NULL , + `ROLE_NAME` VARCHAR(45) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + CONSTRAINT `FK_ROLE_POLICY_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION); + +; + + +-- ----------------------------------------------------- +-- Table `DM_LOCATION` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS DM_LOCATION ( + `LAT` VARCHAR(45) NOT NULL , + `LONG` VARCHAR(45) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + CONSTRAINT `FK_DM_POLICY_DM_LOCATION` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION); +; + + +-- ----------------------------------------------------- +-- Table `DM_TIME` +-- ----------------------------------------------------- + +CREATE TABLE IF NOT EXISTS DM_TIME ( + `STARTING_TIME` DATETIME NOT NULL , + `ENDING_TIME` DATETIME NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + CONSTRAINT `FK_DM_POLICY_DM_TIME` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION); +; + 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 new file mode 100644 index 0000000000..554847059b --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -0,0 +1,247 @@ +SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; +SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; +SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; + +DROP SCHEMA IF EXISTS `WSO2CDM` ; +CREATE SCHEMA IF NOT EXISTS `WSO2CDM` DEFAULT CHARACTER SET latin1 ; +USE `WSO2CDM` ; + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE_TYPE` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ( + `ID` INT(11) NOT NULL , + `NAME` VARCHAR(300) NULL DEFAULT NULL , + PRIMARY KEY (`ID`) ) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE` ( + `ID` VARCHAR(20) NOT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `NAME` VARCHAR(100) NULL DEFAULT NULL , + `DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL , + `DATE_OF_LAST_UPDATE` BIGINT(20) NULL DEFAULT NULL , + `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL , + `STATUS` VARCHAR(15) NULL DEFAULT NULL , + `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , + `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , + `OWNER` VARCHAR(45) NULL DEFAULT NULL , + `TENANT_ID` INT(11) NULL DEFAULT '0' , + PRIMARY KEY (`ID`) , + INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_PROFILE` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_PROFILE` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE` ( + `ID` INT NOT NULL AUTO_INCREMENT , + `PROFILE_NAME` VARCHAR(45) NOT NULL , + `TENANT_ID` INT NOT NULL , + `DEVICE_TYPE_ID` INT NOT NULL , + `CREATED_TIME` DATETIME NOT NULL , + `UPDATED_TIME` DATETIME NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `DM_PROFILE_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `DM_PROFILE_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_POLICY` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_POLICY` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `NAME` VARCHAR(45) NULL DEFAULT NULL , + `TENANT_ID` INT(11) NOT NULL , + `PROFILE_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC) , + CONSTRAINT `FK_DM_PROFILE_DM_POLICY` + FOREIGN KEY (`PROFILE_ID` ) + REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE_POLICY` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `DEVICE_ID` VARCHAR(20) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_POLICY_DEVICE_POLICY` (`POLICY_ID` ASC) , + INDEX `FK_DEVICE_DEVICE_POLICY` (`DEVICE_ID` ASC) , + CONSTRAINT `FK_POLICY_DEVICE_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DEVICE_DEVICE_POLICY` + FOREIGN KEY (`DEVICE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` ( + `ID` INT(11) NOT NULL , + `DEVICE_TYPE_ID` INT(11) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_DEVICE_TYPE_POLICY` (`POLICY_ID` ASC) , + INDEX `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `FK_DEVICE_TYPE_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_FEATURES` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_FEATURES` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `NAME` VARCHAR(256) NOT NULL , + `CODE` VARCHAR(45) NULL DEFAULT NULL , + `DESCRIPTION` TEXT NULL DEFAULT NULL , + `EVALUVATION_RULE` VARCHAR(60) NOT NULL , + PRIMARY KEY (`ID`) ) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_PROFILE_FEATURES` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_PROFILE_FEATURES` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT , + `PROFILE_ID` INT(11) NOT NULL , + `FEATURE_ID` INT(11) NOT NULL , + `CONTENT` BLOB NULL DEFAULT NULL , + PRIMARY KEY (`ID`) , + INDEX `fk_DM_POLICY_FEATURES_DM_FEATURES1` (`FEATURE_ID` ASC) , + INDEX `FK_DM_PROFILE_DM_POLICY_FEATURES` (`PROFILE_ID` ASC) , + CONSTRAINT `fk_DM_POLICY_FEATURES_DM_FEATURES1` + FOREIGN KEY (`FEATURE_ID` ) + REFERENCES `WSO2CDM`.`DM_FEATURES` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DM_PROFILE_DM_POLICY_FEATURES` + FOREIGN KEY (`PROFILE_ID` ) + REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_ROLE_POLICY` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ( + `ID` INT(11) NOT NULL , + `ROLE_NAME` VARCHAR(45) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + PRIMARY KEY (`ID`) , + INDEX `FK_ROLE_POLICY_POLICY` (`POLICY_ID` ASC) , + CONSTRAINT `FK_ROLE_POLICY_POLICY` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_LOCATION` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_LOCATION` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_LOCATION` ( + `LAT` VARCHAR(45) NOT NULL , + `LONG` VARCHAR(45) NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + INDEX `FK_DM_POLICY_DM_LOCATION` (`POLICY_ID` ASC) , + CONSTRAINT `FK_DM_POLICY_DM_LOCATION` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_TIME` +-- ----------------------------------------------------- +DROP TABLE IF EXISTS `WSO2CDM`.`DM_TIME` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_TIME` ( + `STARTING_TIME` DATETIME NOT NULL , + `ENDING_TIME` DATETIME NOT NULL , + `POLICY_ID` INT(11) NOT NULL , + INDEX `FK_DM_POLICY_DM_TIME` (`POLICY_ID` ASC) , + CONSTRAINT `FK_DM_POLICY_DM_TIME` + FOREIGN KEY (`POLICY_ID` ) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + + +SET SQL_MODE=@OLD_SQL_MODE; +SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; +SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml new file mode 100644 index 0000000000..ee2d323f32 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml @@ -0,0 +1,33 @@ + + + + + + jdbc:mysql://10.100.0.47:3306/WSO2CDM + com.mysql.jdbc.Driver + root + root + + + jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 + org.h2.Driver + wso2carbon + wso2carbon + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml new file mode 100644 index 0000000000..02107984d5 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml index f6fe137b9b..58a7644503 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml @@ -59,5 +59,9 @@ org.wso2.carbon.devicemgt org.wso2.carbon.policy.mgt.common + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.core + diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java new file mode 100644 index 0000000000..b779b6ab6c --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2015 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.simple.policy.decision.point; + +public class PolicyEvaluationException extends Exception { + + private String policyEvaluationErrorMessage; + + public String getPolicyEvaluationErrorMessage() { + return policyEvaluationErrorMessage; + } + + public void setPolicyEvaluationErrorMessage(String policyEvaluationErrorMessage) { + this.policyEvaluationErrorMessage = policyEvaluationErrorMessage; + } + + public PolicyEvaluationException(String message) { + super(message); + setPolicyEvaluationErrorMessage(message); + } + + public PolicyEvaluationException(String message, Exception ex) { + super(message, ex); + setPolicyEvaluationErrorMessage(message); + } + + public PolicyEvaluationException(String message, Throwable cause) { + super(message, cause); + setPolicyEvaluationErrorMessage(message); + } + + public PolicyEvaluationException() { + super(); + } + + public PolicyEvaluationException(Throwable cause) { + super(cause); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java index fbeb04c7a4..21e3888e80 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java @@ -18,21 +18,22 @@ package org.wso2.carbon.simple.policy.decision.point; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.PIPDeviceData; import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationService; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import java.util.List; -public class PolicyEvaluationServiceImpl implements PolicyEvaluationService { +public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint { @Override - public Policy getEffectivePolicy(PIPDeviceData pipDeviceData) { + public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) { return null; } @Override - public List getEffectiveFeatures(PIPDeviceData pipDeviceData) { + public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) { return null; } } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java new file mode 100644 index 0000000000..f1d076d0fd --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2015 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.simple.policy.decision.point; + +import org.wso2.carbon.policy.mgt.common.PIPDeviceData; +import org.wso2.carbon.policy.mgt.common.Policy; + +public interface SimpleEvaluation { + + void sortPolicy(Policy policy) throws PolicyEvaluationException; + + Policy getEffectivePolicy(PIPDeviceData pipDeviceData) throws PolicyEvaluationException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java new file mode 100644 index 0000000000..95f902a985 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -0,0 +1,47 @@ +/* +* Copyright (c) 2015 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.simple.policy.decision.point; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.PIPDeviceData; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; + +public class SimpleEvaluationImpl implements SimpleEvaluation { + + private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class); + + PolicyDAOImpl policyDAO; + + + public SimpleEvaluationImpl() { + policyDAO = new PolicyDAOImpl(); + } + + @Override + public void sortPolicy(Policy policy) throws PolicyEvaluationException { + + } + + @Override + public Policy getEffectivePolicy(PIPDeviceData pipDeviceData) throws PolicyEvaluationException { + return null; + } +} diff --git a/components/policy-mgt/pom.xml b/components/policy-mgt/pom.xml index c658229e18..7a9524a9ce 100644 --- a/components/policy-mgt/pom.xml +++ b/components/policy-mgt/pom.xml @@ -36,8 +36,8 @@ http://wso2.org - org.wso2.carbon.policy.mgt.core org.wso2.carbon.policy.mgt.common + org.wso2.carbon.policy.mgt.core org.wso2.carbon.policy.information.point org.wso2.carbon.simple.policy.decision.point org.wso2.carbon.complex.policy.decision.point diff --git a/pom.xml b/pom.xml index 3814ec8daf..71ebab3f97 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,7 @@ org.wso2.carbon.base ${carbon.kernel.version} - +