From fda1d4ddf7992989cf5fcd0cf53f9df105bfac9b Mon Sep 17 00:00:00 2001 From: GPrathap Date: Mon, 6 Jun 2016 21:19:50 +0530 Subject: [PATCH] adding plugin implementation --- .../droneanalyzer/component/api/pom.xml | 173 ++++++++++++ .../droneanalyzer/component/plugin/pom.xml | 133 +++++++++ .../constants/DroneAnalyzerConstants.java | 70 +++++ .../exception/DeviceMgtPluginException.java | 59 ++++ .../plugin/impl/DroneAnalyzerManager.java | 254 ++++++++++++++++++ .../impl/DroneAnalyzerManagerService.java | 68 +++++ .../plugin/impl/dao/DroneAnalyzerDAO.java | 132 +++++++++ .../impl/dao/impl/DroneAnalyzerDAOImpl.java | 206 ++++++++++++++ .../feature/DroneAnalyzerFeatureManager.java | 61 +++++ .../impl/util/DeviceSchemaInitializer.java | 49 ++++ .../util/DroneAnalyzerStartupListener.java | 46 ++++ .../plugin/impl/util/DroneAnalyzerUtils.java | 190 +++++++++++++ .../DroneAnalyzerManagementDataHolder.java | 47 ++++ .../plugin/internal/ServiceComponent.java | 113 ++++++++ .../droneanalyzer/plugin/mqtt/MqttConfig.java | 63 +++++ .../samples/droneanalyzer/component/pom.xml | 54 ++++ .../droneanalyzer/feature/feature/pom.xml | 5 + modules/samples/droneanalyzer/pom.xml | 2 +- 18 files changed, 1724 insertions(+), 1 deletion(-) create mode 100644 modules/samples/droneanalyzer/component/api/pom.xml create mode 100644 modules/samples/droneanalyzer/component/plugin/pom.xml create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/constants/DroneAnalyzerConstants.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/exception/DeviceMgtPluginException.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManager.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManagerService.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/DroneAnalyzerDAO.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/impl/DroneAnalyzerDAOImpl.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/feature/DroneAnalyzerFeatureManager.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DeviceSchemaInitializer.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerStartupListener.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerUtils.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/DroneAnalyzerManagementDataHolder.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/ServiceComponent.java create mode 100644 modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/mqtt/MqttConfig.java create mode 100644 modules/samples/droneanalyzer/component/pom.xml diff --git a/modules/samples/droneanalyzer/component/api/pom.xml b/modules/samples/droneanalyzer/component/api/pom.xml new file mode 100644 index 00000000..3bb9abee --- /dev/null +++ b/modules/samples/droneanalyzer/component/api/pom.xml @@ -0,0 +1,173 @@ + + + + + + org.homeautomation + droneanalyzer-component + 1.0.0 + ../pom.xml + + 4.0.0 + ${project-base-package}.api + 1.0.0 + war + ${project-base-package}.api + http://wso2.com + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + UTF-8 + ${maven.compiler.source} + ${maven.compiler.target} + + + + maven-war-plugin + ${maven-war-plugin.version} + + ${project-base-package}.api + + + + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + provided + + + org.apache.axis2.wso2 + axis2-client + + + + + + + org.apache.cxf + cxf-rt-frontend-jaxws + provided + + + org.apache.cxf + cxf-rt-frontend-jaxrs + provided + + + org.apache.cxf + cxf-rt-transports-http + provided + + + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + provided + + + + + org.apache.httpcomponents + httpasyncclient + 4.1 + provided + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot + provided + + + org.homeautomation + ${project-base-package}.plugin + provided + + + + + org.codehaus.jackson + jackson-core-asl + + + org.codehaus.jackson + jackson-jaxrs + + + javax + javaee-web-api + provided + + + javax.ws.rs + jsr311-api + provided + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.analytics.data.publisher + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.annotations + provided + + + org.wso2.carbon.analytics + org.wso2.carbon.analytics.api + provided + + + org.wso2.carbon.analytics + org.wso2.carbon.analytics.api + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.extensions + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.identity.jwt.client.extension + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.application.extension + provided + + + \ No newline at end of file diff --git a/modules/samples/droneanalyzer/component/plugin/pom.xml b/modules/samples/droneanalyzer/component/plugin/pom.xml new file mode 100644 index 00000000..9b58e2db --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/pom.xml @@ -0,0 +1,133 @@ + + + + + + org.homeautomation + droneanalyzer-component + 1.0.0 + ../pom.xml + + 4.0.0 + 1.0.0 + ${project-base-package}.plugin + bundle + ${project-base-package}.plugin + http://wso2.org + + + + org.apache.felix + maven-scr-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + UTF-8 + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.apache.felix + maven-bundle-plugin + ${org.apache.felix.version} + true + + + ${project-base-package}.plugin + ${project-base-package}.plugin + 1.0.0 + IoT Server Impl Bundle + ${project-base-package}.plugin.internal + + org.osgi.framework, + org.osgi.service.component, + org.apache.commons.logging, + javax.naming;resolution:=optional, + javax.sql;resolution:=optional, + org.wso2.carbon.device.mgt.common.*, + org.wso2.carbon.device.mgt.common, + org.wso2.carbon.device.mgt.iot.*, + org.wso2.carbon.device.mgt.extensions.feature.mgt.*, + org.wso2.carbon.utils.*, + org.wso2.carbon.event.output.adapter.core, + org.wso2.carbon.event.output.adapter.core.exception, + org.wso2.carbon.base, + org.wso2.carbon.core.util, + org.wso2.carbon.context, + org.wso2.carbon.core, + org.apache.commons.codec.binary + + + !${project-base-package}.plugin.internal, + ${project-base-package}.plugin.* + + + + + + + + + commons-codec.wso2 + commons-codec + + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.osgi + org.eclipse.osgi.services + + + org.wso2.carbon + org.wso2.carbon.logging + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + + + org.wso2.carbon + org.wso2.carbon.ndatasource.core + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.extensions + + + org.wso2.carbon + org.wso2.carbon.utils + + + org.wso2.carbon.analytics-common + org.wso2.carbon.event.output.adapter.core + + + \ No newline at end of file diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/constants/DroneAnalyzerConstants.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/constants/DroneAnalyzerConstants.java new file mode 100644 index 00000000..a82897dc --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/constants/DroneAnalyzerConstants.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.constants; + +import org.wso2.carbon.utils.CarbonUtils; +import java.io.File; + +/** + * Device type specific constants which includes all transport protocols configurations, + * stream definition and device specific dome constants + */ +public class DroneAnalyzerConstants { + public final static String DEVICE_TYPE = "drone"; + public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME"; + public final static String DEVICE_PLUGIN_DEVICE_ID = "drone_DEVICE_ID"; + public final static String STATE_ON = "ON"; + public final static String STATE_OFF = "OFF"; + + //sensor events summerized table name + public static final String SENSOR_EVENT_TABLE = "ORG_WSO2_IOT_DEVICES_droneStats"; + public static final String DATA_SOURCE_NAME = "jdbc/droneDM_DB"; + public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; + + //mqtt tranport related constants + public static final String MQTT_ADAPTER_NAME = "droneStats_mqtt"; + public static final String MQTT_ADAPTER_TYPE = "oauth-mqtt"; + public static final String ADAPTER_TOPIC_PROPERTY = "topic"; + public static final String MQTT_PORT = "\\{mqtt.broker.port\\}"; + public static final String MQTT_BROKER_HOST = "\\{mqtt.broker.host\\}"; + public static final String CARBON_CONFIG_PORT_OFFSET = "Ports.Offset"; + public static final String DEFAULT_CARBON_LOCAL_IP_PROPERTY = "carbon.local.ip"; + public static final int CARBON_DEFAULT_PORT_OFFSET = 0; + public static final int DEFAULT_MQTT_PORT = 1883; + public static final String SUBSCRIBED_TOPIC = "carbon.super/drone/+/publisher"; + public static final String CONTENT_TRANSFORMATION = "contentTransformer"; + public static final String CONTENT_VALIDATION = "contentValidator"; + public static final String RESOURCE = "resource"; + + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String DCR_PROPERTY_KEY = "dcrUrl"; + public static final String BROKER_URL_PROPERTY_KEY = "url"; + public static final String SCOPES_PROPERTY_KEY = "scopes"; + public static final String QOS_PROPERTY_KEY = "qos"; + public static final String CLIENT_ID_PROPERTY_KEY = "qos"; + public static final String CLEAR_SESSION_PROPERTY_KEY = "clearSession"; + public static final String TOPIC = "topic"; + + public final static String SENSOR_STREAM_DEFINITION = "org.wso2.iot.devices.droneStats"; + public final static String SENSOR_STREAM_DEFINITION_VERSION = "1.0.0"; + + public static final String MQTT_CONFIG_LOCATION = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + + "mqtt.properties"; +} + diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/exception/DeviceMgtPluginException.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/exception/DeviceMgtPluginException.java new file mode 100644 index 00000000..e0c03a72 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/exception/DeviceMgtPluginException.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.exception; + +/** + * Device type plugin exception handler + */ +public class DeviceMgtPluginException extends Exception { + + private String errorMessage; + + public DeviceMgtPluginException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public DeviceMgtPluginException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public DeviceMgtPluginException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public DeviceMgtPluginException() { + super(); + } + + public DeviceMgtPluginException(Throwable cause) { + super(cause); + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} \ No newline at end of file diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManager.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManager.java new file mode 100644 index 00000000..f5aee4c1 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManager.java @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.homeautomation.droneanalyzer.plugin.impl.dao.DroneAnalyzerDAO; +import org.homeautomation.droneanalyzer.plugin.exception.DeviceMgtPluginException; +import org.homeautomation.droneanalyzer.plugin.impl.feature.DroneAnalyzerFeatureManager; +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.DeviceManager; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; + +import java.util.List; + + +/** + * This represents the ggg implementation of DeviceManagerService. + */ +public class DroneAnalyzerManager implements DeviceManager { + + private static final Log log = LogFactory.getLog(DroneAnalyzerManager.class); + private static final DroneAnalyzerDAO deviceTypeDAO = new DroneAnalyzerDAO(); + private FeatureManager featureManager = new DroneAnalyzerFeatureManager(); + + @Override + public FeatureManager getFeatureManager() { + return featureManager; + } + + @Override + public boolean saveConfiguration(PlatformConfiguration platformConfiguration) throws DeviceManagementException { + return false; + } + + @Override + public PlatformConfiguration getConfiguration() throws DeviceManagementException { + return null; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("Enrolling a new droneanalyzer device : " + device.getDeviceIdentifier()); + } + DroneAnalyzerDAO.beginTransaction(); + status = deviceTypeDAO.getDroneAnalyzerDAO().addDevice(device); + DroneAnalyzerDAO.commitTransaction(); + } catch (DeviceMgtPluginException e) { + try { + DroneAnalyzerDAO.rollbackTransaction(); + } catch (DeviceMgtPluginException iotDAOEx) { + log.warn("Error occurred while roll back the device enrol transaction :" + device.toString(), iotDAOEx); + } + String msg = "Error while enrolling the droneanalyzer device : " + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("Modifying the droneanalyzer device enrollment data"); + } + DroneAnalyzerDAO.beginTransaction(); + status = deviceTypeDAO.getDroneAnalyzerDAO().updateDevice(device); + DroneAnalyzerDAO.commitTransaction(); + } catch (DeviceMgtPluginException e) { + try { + DroneAnalyzerDAO.rollbackTransaction(); + } catch (DeviceMgtPluginException iotDAOEx) { + String msg = "Error occurred while roll back the update device transaction :" + device.toString(); + log.warn(msg, iotDAOEx); + } + String msg = "Error while updating the enrollment of the droneanalyzer device : " + + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("Dis-enrolling droneanalyzer device : " + deviceId); + } + DroneAnalyzerDAO.beginTransaction(); + status = deviceTypeDAO.getDroneAnalyzerDAO().deleteDevice(deviceId.getId()); + DroneAnalyzerDAO.commitTransaction(); + } catch (DeviceMgtPluginException e) { + try { + DroneAnalyzerDAO.rollbackTransaction(); + } catch (DeviceMgtPluginException iotDAOEx) { + String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); + log.warn(msg, iotDAOEx); + } + String msg = "Error while removing the droneanalyzer device : " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + boolean isEnrolled = false; + try { + if (log.isDebugEnabled()) { + log.debug("Checking the enrollment of droneanalyzer device : " + deviceId.getId()); + } + Device iotDevice = + deviceTypeDAO.getDroneAnalyzerDAO().getDevice(deviceId.getId()); + if (iotDevice != null) { + isEnrolled = true; + } + } catch (DeviceMgtPluginException e) { + String msg = "Error while checking the enrollment status of droneanalyzer device : " + + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return isEnrolled; + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return true; + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) + throws DeviceManagementException { + return true; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + Device device; + try { + if (log.isDebugEnabled()) { + log.debug("Getting the details of droneanalyzer device : " + deviceId.getId()); + } + device = deviceTypeDAO.getDroneAnalyzerDAO().getDevice(deviceId.getId()); + } catch (DeviceMgtPluginException e) { + String msg = "Error while fetching the droneanalyzer device : " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return device; + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + return true; + } + + public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + return false; + } + + @Override + public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, + EnrolmentInfo.Status status) throws DeviceManagementException { + return false; + } + + @Override + public License getLicense(String s) throws LicenseManagementException { + return null; + } + + @Override + public void addLicense(License license) throws LicenseManagementException { + + } + + @Override + public boolean requireDeviceAuthorization() { + return true; + } + + @Override + public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("updating the details of droneanalyzer device : " + deviceIdentifier); + } + DroneAnalyzerDAO.beginTransaction(); + status = deviceTypeDAO.getDroneAnalyzerDAO().updateDevice(device); + DroneAnalyzerDAO.commitTransaction(); + } catch (DeviceMgtPluginException e) { + try { + DroneAnalyzerDAO.rollbackTransaction(); + } catch (DeviceMgtPluginException iotDAOEx) { + String msg = "Error occurred while roll back the update device info transaction :" + device.toString(); + log.warn(msg, iotDAOEx); + } + String msg = + "Error while updating the droneanalyzer device : " + deviceIdentifier; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public List getAllDevices() throws DeviceManagementException { + List devices; + try { + if (log.isDebugEnabled()) { + log.debug("Fetching the details of all droneanalyzer devices"); + } + devices = deviceTypeDAO.getDroneAnalyzerDAO().getAllDevices(); + } catch (DeviceMgtPluginException e) { + String msg = "Error while fetching all droneanalyzer devices."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return devices; + } +} \ No newline at end of file diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManagerService.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManagerService.java new file mode 100644 index 00000000..3b0c02e0 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/DroneAnalyzerManagerService.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.impl; + +import org.homeautomation.droneanalyzer.plugin.constants.DroneAnalyzerConstants; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.ProvisioningConfig; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManager; +import org.wso2.carbon.device.mgt.common.app.mgt.Application; +import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; +import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; + +import java.util.List; + +public class DroneAnalyzerManagerService implements DeviceManagementService { + private DeviceManager deviceManager; + + @Override + public String getType() { + return DroneAnalyzerConstants.DEVICE_TYPE; + } + + @Override + public void init() throws DeviceManagementException { + this.deviceManager = new DroneAnalyzerManager(); + } + + @Override + public DeviceManager getDeviceManager() { + return deviceManager; + } + + @Override + public ApplicationManager getApplicationManager() { + return null; + } + + @Override + public ProvisioningConfig getProvisioningConfig() { + return new ProvisioningConfig(DroneAnalyzerConstants.DEVICE_TYPE_PROVIDER_DOMAIN, false); + } + + @Override + public PushNotificationConfig getPushNotificationConfig() { + return null; + } + +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/DroneAnalyzerDAO.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/DroneAnalyzerDAO.java new file mode 100644 index 00000000..3cde74d1 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/DroneAnalyzerDAO.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.impl.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.homeautomation.droneanalyzer.plugin.constants.DroneAnalyzerConstants; +import org.homeautomation.droneanalyzer.plugin.impl.dao.impl.DroneAnalyzerDAOImpl; +import org.homeautomation.droneanalyzer.plugin.exception.DeviceMgtPluginException; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * Database handler which is specified for gg type + */ +public class DroneAnalyzerDAO { + + private static final Log log = LogFactory.getLog(DroneAnalyzerDAO.class); + static DataSource dataSource; // package local variable + private static ThreadLocal currentConnection = new ThreadLocal<>(); + + public DroneAnalyzerDAO() { + initDroneAnalyzerDAO(); + } + + public static void initDroneAnalyzerDAO() { + try { + Context ctx = new InitialContext(); + dataSource = (DataSource) ctx.lookup(DroneAnalyzerConstants.DATA_SOURCE_NAME); + } catch (NamingException e) { + log.error("Error while looking up the data source: " + + DroneAnalyzerConstants.DATA_SOURCE_NAME); + } + } + + public static void beginTransaction() throws DeviceMgtPluginException { + try { + Connection conn = dataSource.getConnection(); + conn.setAutoCommit(false); + currentConnection.set(conn); + } catch (SQLException e) { + throw new DeviceMgtPluginException("Error occurred while retrieving datasource connection", e); + } + } + + public static Connection getConnection() throws DeviceMgtPluginException { + if (currentConnection.get() == null) { + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new DeviceMgtPluginException("Error occurred while retrieving data source connection", + e); + } + } + return currentConnection.get(); + } + + public static void commitTransaction() throws DeviceMgtPluginException { + 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 DeviceMgtPluginException("Error occurred while committing the transaction", e); + } finally { + closeConnection(); + } + } + + public static void closeConnection() throws DeviceMgtPluginException { + + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } + currentConnection.remove(); + } + + public static void rollbackTransaction() throws DeviceMgtPluginException { + 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 DeviceMgtPluginException("Error occurred while rollback the transaction", e); + } finally { + closeConnection(); + } + } + + public DroneAnalyzerDAOImpl getDroneAnalyzerDAO() { + return new DroneAnalyzerDAOImpl(); + } +} \ No newline at end of file diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/impl/DroneAnalyzerDAOImpl.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/impl/DroneAnalyzerDAOImpl.java new file mode 100644 index 00000000..a212b0b6 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/dao/impl/DroneAnalyzerDAOImpl.java @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.impl.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.homeautomation.droneanalyzer.plugin.constants.DroneAnalyzerConstants; +import org.homeautomation.droneanalyzer.plugin.exception.DeviceMgtPluginException; +import org.homeautomation.droneanalyzer.plugin.impl.dao.DroneAnalyzerDAO; +import org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerUtils; + +import org.wso2.carbon.device.mgt.common.Device; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Implements IotDeviceDAO for gg Devices. + */ +public class DroneAnalyzerDAOImpl { + + + private static final Log log = LogFactory.getLog(DroneAnalyzerDAOImpl.class); + + public Device getDevice(String deviceId) throws DeviceMgtPluginException { + Connection conn = null; + PreparedStatement stmt = null; + Device iotDevice = null; + ResultSet resultSet = null; + try { + conn = DroneAnalyzerDAO.getConnection(); + String selectDBQuery = + "SELECT drone_DEVICE_ID, DEVICE_NAME" + + " FROM drone_DEVICE WHERE drone_DEVICE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, deviceId); + resultSet = stmt.executeQuery(); + + if (resultSet.next()) { + iotDevice = new Device(); + iotDevice.setName(resultSet.getString( + DroneAnalyzerConstants.DEVICE_PLUGIN_DEVICE_NAME)); + if (log.isDebugEnabled()) { + log.debug("drone device " + deviceId + " data has been fetched from " + + "drone database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while fetching drone device : '" + deviceId + "'"; + log.error(msg, e); + throw new DeviceMgtPluginException(msg, e); + } finally { + DroneAnalyzerUtils.cleanupResources(stmt, resultSet); + DroneAnalyzerDAO.closeConnection(); + } + return iotDevice; + } + + public boolean addDevice(Device device) throws DeviceMgtPluginException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = DroneAnalyzerDAO.getConnection(); + String createDBQuery = + "INSERT INTO drone_DEVICE(drone_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)"; + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, device.getDeviceIdentifier()); + stmt.setString(2, device.getName()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("drone device " + device.getDeviceIdentifier() + " data has been" + + " added to the drone database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while adding the drone device '" + + device.getDeviceIdentifier() + "' to the drone db."; + log.error(msg, e); + throw new DeviceMgtPluginException(msg, e); + } finally { + DroneAnalyzerUtils.cleanupResources(stmt, null); + } + return status; + } + + public boolean updateDevice(Device device) throws DeviceMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = DroneAnalyzerDAO.getConnection(); + String updateDBQuery = + "UPDATE drone_DEVICE SET DEVICE_NAME = ? WHERE drone_DEVICE_ID = ?"; + stmt = conn.prepareStatement(updateDBQuery); + if (device.getProperties() == null) { + device.setProperties(new ArrayList()); + } + stmt.setString(1, device.getName()); + stmt.setString(2, device.getDeviceIdentifier()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("drone device " + device.getDeviceIdentifier() + " data has been" + + " modified."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while modifying the drone device '" + + device.getDeviceIdentifier() + "' data."; + log.error(msg, e); + throw new DeviceMgtPluginException(msg, e); + } finally { + DroneAnalyzerUtils.cleanupResources(stmt, null); + } + return status; + } + + public boolean deleteDevice(String deviceId) throws DeviceMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = DroneAnalyzerDAO.getConnection(); + String deleteDBQuery = + "DELETE FROM drone_DEVICE WHERE drone_DEVICE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, deviceId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("drone device " + deviceId + " data has deleted" + + " from the drone database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while deleting drone device " + deviceId; + log.error(msg, e); + throw new DeviceMgtPluginException(msg, e); + } finally { + DroneAnalyzerUtils.cleanupResources(stmt, null); + } + return status; + } + + public List getAllDevices() throws DeviceMgtPluginException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + Device device; + List iotDevices = new ArrayList<>(); + try { + conn = DroneAnalyzerDAO.getConnection(); + String selectDBQuery = + "SELECT drone_DEVICE_ID, DEVICE_NAME " + + "FROM drone_DEVICE"; + stmt = conn.prepareStatement(selectDBQuery); + resultSet = stmt.executeQuery(); + while (resultSet.next()) { + device = new Device(); + device.setDeviceIdentifier(resultSet.getString(DroneAnalyzerConstants.DEVICE_PLUGIN_DEVICE_ID)); + device.setName(resultSet.getString(DroneAnalyzerConstants.DEVICE_PLUGIN_DEVICE_NAME)); + List propertyList = new ArrayList<>(); + device.setProperties(propertyList); + } + if (log.isDebugEnabled()) { + log.debug("All drone device details have fetched from drone database."); + } + return iotDevices; + } catch (SQLException e) { + String msg = "Error occurred while fetching all drone device data'"; + log.error(msg, e); + throw new DeviceMgtPluginException(msg, e); + } finally { + DroneAnalyzerUtils.cleanupResources(stmt, resultSet); + DroneAnalyzerDAO.closeConnection(); + } + } +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/feature/DroneAnalyzerFeatureManager.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/feature/DroneAnalyzerFeatureManager.java new file mode 100644 index 00000000..a8d3547c --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/feature/DroneAnalyzerFeatureManager.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * 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. + */ +package org.homeautomation.droneanalyzer.plugin.impl.feature; + +import org.homeautomation.droneanalyzer.plugin.constants.DroneAnalyzerConstants; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.extensions.feature.mgt.GenericFeatureManager; + +import java.util.List; + +/** + * Device type specific feature management server + */ +public class DroneAnalyzerFeatureManager implements FeatureManager { + @Override + public boolean addFeature(Feature feature) throws DeviceManagementException { + return false; + } + + @Override + public boolean addFeatures(List features) throws DeviceManagementException { + return false; + } + + @Override + public Feature getFeature(String name) throws DeviceManagementException { + GenericFeatureManager genericFeatureManager = GenericFeatureManager.getInstance(); + return genericFeatureManager.getFeature(DroneAnalyzerConstants.DEVICE_TYPE, name); + } + + @Override + public List getFeatures() throws DeviceManagementException { + GenericFeatureManager genericFeatureManager = GenericFeatureManager.getInstance(); + return genericFeatureManager.getFeatures(DroneAnalyzerConstants.DEVICE_TYPE); + } + + @Override + public boolean removeFeature(String name) throws DeviceManagementException { + return false; + } + + @Override + public boolean addSupportedFeaturesToDB() throws DeviceManagementException { + return false; + } +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DeviceSchemaInitializer.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DeviceSchemaInitializer.java new file mode 100644 index 00000000..be86eab4 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DeviceSchemaInitializer.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.homeautomation.droneanalyzer.plugin.impl.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.dbcreator.DatabaseCreator; + +import javax.sql.DataSource; +import java.io.File; + +/** + * Provides methods for initializing the database script. + */ +public class DeviceSchemaInitializer extends DatabaseCreator{ + + private static final Log log = LogFactory.getLog(DeviceSchemaInitializer.class); + private static final String setupSQLScriptBaseLocation = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + + File.separator + "cdm" + File.separator + "plugins" + File.separator; + + public DeviceSchemaInitializer(DataSource dataSource) { + super(dataSource); + } + + @Override + protected String getDbScriptLocation(String databaseType) { + String scriptName = databaseType + ".sql"; + if (log.isDebugEnabled()) { + log.debug("Loading database script from :" + scriptName); + } + return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; + } +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerStartupListener.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerStartupListener.java new file mode 100644 index 00000000..a57fabd9 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerStartupListener.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.impl.util; + +import org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.core.ServerStartupObserver; + +import java.io.IOException; + +/** + * When device type related plugin is depleting it will initiate mqtt connection with mqtt broker + */ +public class DroneAnalyzerStartupListener implements ServerStartupObserver { + private static final Log log = LogFactory.getLog(org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerStartupListener.class); + + @Override + public void completingServerStartup() { + } + + @Override + public void completedServerStartup() { + try { + DroneAnalyzerUtils.setupMqttOutputAdapter(); + } catch (IOException e) { + log.error("Failed to initialize the drone output adapter", e); + } + } +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerUtils.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerUtils.java new file mode 100644 index 00000000..09bba541 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/impl/util/DroneAnalyzerUtils.java @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.impl.util; + +import org.homeautomation.droneanalyzer.plugin.constants.DroneAnalyzerConstants; +import org.homeautomation.droneanalyzer.plugin.exception.DeviceMgtPluginException; +import org.homeautomation.droneanalyzer.plugin.internal.DroneAnalyzerManagementDataHolder; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.ServerConfiguration; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.core.util.Utils; +import org.wso2.carbon.event.output.adapter.core.MessageType; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration; +import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Contains utility methods used by gg plugin. + */ +public class DroneAnalyzerUtils { + + private static Log log = LogFactory.getLog(org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerUtils.class); + + public static void cleanupResources(Connection conn, PreparedStatement 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); + } + } + } + + public static void cleanupResources(PreparedStatement stmt, ResultSet rs) { + cleanupResources(null, stmt, rs); + } + + /** + * Creates the device management schema. + */ + public static void setupDeviceManagementSchema() throws DeviceMgtPluginException { + try { + Context ctx = new InitialContext(); + DataSource dataSource = (DataSource) ctx.lookup(DroneAnalyzerConstants.DATA_SOURCE_NAME); + DeviceSchemaInitializer initializer = + new DeviceSchemaInitializer(dataSource); + log.info("Initializing device management repository database schema"); + initializer.createRegistryDatabase(); + } catch (NamingException e) { + log.error("Error while looking up the data source: " + DroneAnalyzerConstants.DATA_SOURCE_NAME); + } catch (Exception e) { + throw new DeviceMgtPluginException("Error occurred while initializing Iot Device " + + "Management database schema", e); + } + } + + public static String replaceMqttProperty(String urlWithPlaceholders) { + String MQTT_BROKER_HOST = null; + String MQTT_PORT = null; + if(!DroneAnalyzerConstants.MQTT_BROKER_HOST.startsWith("$")){ + MQTT_BROKER_HOST = "\\$".concat(DroneAnalyzerConstants.MQTT_BROKER_HOST); + } + if(!DroneAnalyzerConstants.MQTT_PORT.startsWith("$")){ + MQTT_PORT = "\\$".concat(DroneAnalyzerConstants.MQTT_PORT); + } + urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders); + urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_PORT, "" + + (DroneAnalyzerConstants.DEFAULT_MQTT_PORT + getPortOffset())); + urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_BROKER_HOST, + System.getProperty(DroneAnalyzerConstants.DEFAULT_CARBON_LOCAL_IP_PROPERTY, "localhost")); + return urlWithPlaceholders; + } + + private static int getPortOffset() { + ServerConfiguration carbonConfig = ServerConfiguration.getInstance(); + String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty( + DroneAnalyzerConstants.CARBON_CONFIG_PORT_OFFSET)); + try { + if ((portOffset != null)) { + return Integer.parseInt(portOffset.trim()); + } else { + return DroneAnalyzerConstants.CARBON_DEFAULT_PORT_OFFSET; + } + } catch (NumberFormatException e) { + return DroneAnalyzerConstants.CARBON_DEFAULT_PORT_OFFSET; + } + } + + public static void setupMqttOutputAdapter() throws IOException { + OutputEventAdapterConfiguration outputEventAdapterConfiguration = + createMqttOutputEventAdapterConfiguration(DroneAnalyzerConstants.MQTT_ADAPTER_NAME, + DroneAnalyzerConstants.MQTT_ADAPTER_TYPE, MessageType.TEXT); + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + DroneAnalyzerConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true); + DroneAnalyzerManagementDataHolder.getInstance().getOutputEventAdapterService() + .create(outputEventAdapterConfiguration); + } catch (OutputEventAdapterException e) { + log.error("Unable to create Output Event Adapter : " + DroneAnalyzerConstants.MQTT_ADAPTER_NAME, e); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + /** + * Create Output Event Adapter Configuration for given configuration. + * + * @param name Output Event Adapter name + * @param type Output Event Adapter type + * @param msgFormat Output Event Adapter message format + * @return OutputEventAdapterConfiguration instance for given configuration + */ + private static OutputEventAdapterConfiguration createMqttOutputEventAdapterConfiguration(String name, String type, + String msgFormat) throws IOException { + OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration(); + outputEventAdapterConfiguration.setName(name); + outputEventAdapterConfiguration.setType(type); + outputEventAdapterConfiguration.setMessageFormat(msgFormat); + File configFile = new File(DroneAnalyzerConstants.MQTT_CONFIG_LOCATION); + if (configFile.exists()) { + Map mqttAdapterProperties = new HashMap<>(); + InputStream propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + mqttAdapterProperties.put(DroneAnalyzerConstants.USERNAME_PROPERTY_KEY, properties.getProperty( + DroneAnalyzerConstants.USERNAME_PROPERTY_KEY)); + mqttAdapterProperties.put(DroneAnalyzerConstants.DCR_PROPERTY_KEY, Utils.replaceSystemProperty( + properties.getProperty(DroneAnalyzerConstants.DCR_PROPERTY_KEY))); + mqttAdapterProperties.put(DroneAnalyzerConstants.BROKER_URL_PROPERTY_KEY, replaceMqttProperty( + properties.getProperty(DroneAnalyzerConstants.BROKER_URL_PROPERTY_KEY))); + mqttAdapterProperties.put(DroneAnalyzerConstants.SCOPES_PROPERTY_KEY, properties.getProperty( + DroneAnalyzerConstants.SCOPES_PROPERTY_KEY)); + mqttAdapterProperties.put(DroneAnalyzerConstants.CLEAR_SESSION_PROPERTY_KEY, properties.getProperty( + DroneAnalyzerConstants.CLEAR_SESSION_PROPERTY_KEY)); + mqttAdapterProperties.put(DroneAnalyzerConstants.QOS_PROPERTY_KEY, properties.getProperty( + DroneAnalyzerConstants.QOS_PROPERTY_KEY)); + mqttAdapterProperties.put(DroneAnalyzerConstants.CLIENT_ID_PROPERTY_KEY, ""); + mqttAdapterProperties.put(DroneAnalyzerConstants.RESOURCE, "output-event"); + outputEventAdapterConfiguration.setStaticProperties(mqttAdapterProperties); + } + return outputEventAdapterConfiguration; + } +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/DroneAnalyzerManagementDataHolder.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/DroneAnalyzerManagementDataHolder.java new file mode 100644 index 00000000..5a6847ef --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/DroneAnalyzerManagementDataHolder.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.homeautomation.droneanalyzer.plugin.internal; + +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; + +/** + * DataHolder class of gg plugins component. + */ +public class DroneAnalyzerManagementDataHolder { + + private static DroneAnalyzerManagementDataHolder thisInstance = new DroneAnalyzerManagementDataHolder(); + private OutputEventAdapterService outputEventAdapterService; + + private DroneAnalyzerManagementDataHolder() { + } + + public static DroneAnalyzerManagementDataHolder getInstance() { + return thisInstance; + } + + public OutputEventAdapterService getOutputEventAdapterService() { + return outputEventAdapterService; + } + + public void setOutputEventAdapterService( + OutputEventAdapterService outputEventAdapterService) { + this.outputEventAdapterService = outputEventAdapterService; + } +} + diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/ServiceComponent.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/ServiceComponent.java new file mode 100644 index 00000000..38b63cf4 --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/internal/ServiceComponent.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.homeautomation.droneanalyzer.plugin.internal; + +import org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerStartupListener; +import org.homeautomation.droneanalyzer.plugin.exception.DeviceMgtPluginException; +import org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerUtils; +import org.homeautomation.droneanalyzer.plugin.impl.DroneAnalyzerManagerService; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.core.ServerStartupObserver; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; + +/** + * @scr.component name="org.homeautomation.droneanalyzer.plugin.internal.ServiceComponent" + * immediate="true" + * @scr.reference name="event.output.adapter.service" + * interface="org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService" + * cardinality="1..1" + * policy="dynamic" + * bind="setOutputEventAdapterService" + * unbind="unsetOutputEventAdapterService" + */ + +public class ServiceComponent { + private static final Log log = LogFactory.getLog(ServiceComponent.class); + private ServiceRegistration serviceRegistration; + + protected void activate(ComponentContext ctx) { + if (log.isDebugEnabled()) { + log.debug("Activating b Management Service Component"); + } + try { + DroneAnalyzerManagerService deviceTypeManagerService = new DroneAnalyzerManagerService(); + BundleContext bundleContext = ctx.getBundleContext(); + serviceRegistration = + bundleContext.registerService(DeviceManagementService.class.getName(), + deviceTypeManagerService, null); + bundleContext.registerService(ServerStartupObserver.class.getName(), new DroneAnalyzerStartupListener(), + null); + String setupOption = System.getProperty("setup"); + if (setupOption != null) { + if (log.isDebugEnabled()) { + log.debug("-Dsetup is enabled. Iot Device management repository schema initialization is about " + + "to begin"); + } + try { + DroneAnalyzerUtils.setupDeviceManagementSchema(); + } catch (DeviceMgtPluginException e) { + log.error("Exception occurred while initializing device management database schema", e); + } + } + if (log.isDebugEnabled()) { + log.debug("b Management Service Component has been successfully activated"); + } + } catch (Throwable e) { + log.error("Error occurred while activating Current Sensor Management Service Component", e); + } + } + + protected void deactivate(ComponentContext ctx) { + if (log.isDebugEnabled()) { + log.debug("De-activating b Management Service Component"); + } + try { + if (serviceRegistration != null) { + serviceRegistration.unregister(); + } + if (log.isDebugEnabled()) { + log.debug("Current Sensor Management Service Component has been successfully de-activated"); + } + } catch (Throwable e) { + log.error("Error occurred while de-activating Iot Device Management bundle", e); + } + } + + /** + * Initialize the Output EventAdapter Service dependency + * + * @param outputEventAdapterService Output EventAdapter Service reference + */ + protected void setOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + DroneAnalyzerManagementDataHolder.getInstance().setOutputEventAdapterService(outputEventAdapterService); + } + + /** + * De-reference the Output EventAdapter Service dependency. + */ + protected void unsetOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + DroneAnalyzerManagementDataHolder.getInstance().setOutputEventAdapterService(null); + } +} diff --git a/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/mqtt/MqttConfig.java b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/mqtt/MqttConfig.java new file mode 100644 index 00000000..fc328b5a --- /dev/null +++ b/modules/samples/droneanalyzer/component/plugin/src/main/java/org.homeautomation/droneanalyzer/plugin/mqtt/MqttConfig.java @@ -0,0 +1,63 @@ +/* + * 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.homeautomation.droneanalyzer.plugin.mqtt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.homeautomation.droneanalyzer.plugin.constants.DroneAnalyzerConstants; +import org.homeautomation.droneanalyzer.plugin.impl.util.DroneAnalyzerUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * MQTT configurations + */ +public class MqttConfig { + + private static String brokerEndpoint; + + private static MqttConfig mqttConfig = new MqttConfig(); + private static final Log log = LogFactory.getLog(MqttConfig.class); + + private MqttConfig() { + File configFile = new File(DroneAnalyzerConstants.MQTT_CONFIG_LOCATION); + if (configFile.exists()) { + try { + InputStream propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + brokerEndpoint = DroneAnalyzerUtils.replaceMqttProperty( + properties.getProperty(DroneAnalyzerConstants.BROKER_URL_PROPERTY_KEY)); + } catch (IOException e) { + log.error("Failed to read the mqtt.properties file" + e); + } + } + } + + public static MqttConfig getInstance() { + return mqttConfig; + } + + public String getBrokerEndpoint() { + return brokerEndpoint; + } +} diff --git a/modules/samples/droneanalyzer/component/pom.xml b/modules/samples/droneanalyzer/component/pom.xml new file mode 100644 index 00000000..7bea418b --- /dev/null +++ b/modules/samples/droneanalyzer/component/pom.xml @@ -0,0 +1,54 @@ + + + + org.homeautomation + droneanalyzer + 1.0.0 + ../pom.xml + + 4.0.0 + 1.0.0 + droneanalyzer-component + pom + + + + + org.apache.felix + maven-scr-plugin + ${maven-scr-plugin.version} + + + generate-scr-scrdescriptor + + scr + + + + + + + + + plugin + api + ui + analytics + + \ No newline at end of file diff --git a/modules/samples/droneanalyzer/feature/feature/pom.xml b/modules/samples/droneanalyzer/feature/feature/pom.xml index 356f0970..9cf4b8a9 100644 --- a/modules/samples/droneanalyzer/feature/feature/pom.xml +++ b/modules/samples/droneanalyzer/feature/feature/pom.xml @@ -198,6 +198,11 @@ org.wso2.carbon.device.mgt.server:${carbon.device.mgt.version} + diff --git a/modules/samples/droneanalyzer/pom.xml b/modules/samples/droneanalyzer/pom.xml index 21d8e498..4d2738d1 100644 --- a/modules/samples/droneanalyzer/pom.xml +++ b/modules/samples/droneanalyzer/pom.xml @@ -16,7 +16,7 @@ ~ specific language governing permissions and limitations ~ under the License. --> - org.wso2