Merge pull request #845 from charithag/master

Expose device management core APIs as siddhi extensions
revert-dabc3590
Ruwan 7 years ago committed by GitHub
commit 0b7f2364ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,24 +19,24 @@
<eventPublisher name="WSO2IoT-DeviceOperation-Publisher" xmlns="http://wso2.org/carbon/eventpublisher"> <eventPublisher name="WSO2IoT-DeviceOperation-Publisher" xmlns="http://wso2.org/carbon/eventpublisher">
<from streamName="org.wso2.iot.operation" version="1.0.0"/> <from streamName="org.wso2.iot.operation" version="1.0.0"/>
<mapping customMapping="enable" type="json"> <mapping customMapping="enable" type="text">
<inline> <inline>
{ {
"deviceIdentifiers": [ "deviceIdentifiers": {{meta_deviceIdentifiers}},
{{meta_deviceIdentifier}} "deviceType": "{{meta_deviceType}}",
],
"operation": { "operation": {
"code": {{code}}, "code": "{{code}}",
"type": {{type}}, "type": "{{type}}",
"status": "PENDING", "status": "PENDING",
"isEnabled": {{isEnabled}}, "isEnabled": "{{isEnabled}}",
"payLoad": {{payLoad}} "payLoad": "{{payLoad}}"
} }
} }
</inline> </inline>
</mapping> </mapping>
<to eventAdapterType="oauth-http"> <to eventAdapterType="oauth-http">
<property name="http.client.method">HttpPost</property> <property name="http.client.method">HttpPost</property>
<property name="http.url">https://localhost:9443/api/device-mgt/v1.0/devices/android/operations</property> <property name="http.url">https://localhost:9443/api/device-mgt/v1.0/devices/{deviceType}/operations</property>
<property name="http.url.templated">true</property>
</to> </to>
</eventPublisher> </eventPublisher>

@ -1,11 +1,15 @@
{ {
"name": "org.wso2.iot.operation", "name": "org.wso2.iot.operation",
"version": "1.0.0", "version": "1.0.0",
"nickName": "", "nickName": "Operation Stream",
"description": "Operation stream for WSO2 IoT Devices", "description": "Operation stream for WSO2 IoT Devices",
"metaData": [ "metaData": [
{ {
"name": "deviceIdentifier", "name": "deviceIdentifiers",
"type": "STRING"
},
{
"name": "deviceType",
"type": "STRING" "type": "STRING"
} }
], ],

@ -59,13 +59,17 @@ import java.net.UnknownHostException;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTTPEventAdapter implements OutputEventAdapter { public class HTTPEventAdapter implements OutputEventAdapter {
@ -181,6 +185,32 @@ public class HTTPEventAdapter implements OutputEventAdapter {
.extractHeaders(dynamicProperties.get(HTTPEventAdapterConstants.ADAPTER_HEADERS)); .extractHeaders(dynamicProperties.get(HTTPEventAdapterConstants.ADAPTER_HEADERS));
String payload = message.toString(); String payload = message.toString();
if ("true".equals(dynamicProperties.get(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED))) {
try {
JSONParser jsonParser = new JSONParser();
JSONObject jsonPayload = (JSONObject) jsonParser.parse(payload);
List<String> matchList = new ArrayList<>();
Pattern regex = Pattern.compile("\\{(.*?)\\}");
Matcher regexMatcher = regex.matcher(url);
while (regexMatcher.find()) {//Finds Matching Pattern in String
matchList.add(regexMatcher.group(1));//Fetching Group from String
}
for(String str:matchList) {
if (jsonPayload.containsKey(str)) {
url = url.replace("{" + str + "}", jsonPayload.get(str).toString());
}
}
if (log.isDebugEnabled()) {
log.debug("Modified url: " + url);
}
} catch (ParseException e) {
log.error("Unable to parse request body to Json.", e);
}
}
try { try {
executorService.submit(new HTTPSender(url, payload, headers, httpClient)); executorService.submit(new HTTPSender(url, payload, headers, httpClient));
} catch (RejectedExecutionException e) { } catch (RejectedExecutionException e) {

@ -76,6 +76,12 @@ public class HTTPEventAdapterFactory extends OutputEventAdapterFactory {
urlProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_HINT)); urlProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_HINT));
urlProp.setRequired(true); urlProp.setRequired(true);
Property urlTemplateProp = new Property(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED);
urlTemplateProp.setDisplayName(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED));
urlTemplateProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_MESSAGE_URL_TEMPLATED_HINT));
urlTemplateProp.setRequired(true);
urlTemplateProp.setOptions(new String[]{"true", "false"});
Property usernameProp = new Property(HTTPEventAdapterConstants.ADAPTER_USERNAME); Property usernameProp = new Property(HTTPEventAdapterConstants.ADAPTER_USERNAME);
usernameProp.setDisplayName(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_USERNAME)); usernameProp.setDisplayName(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_USERNAME));
usernameProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_USERNAME_HINT)); usernameProp.setHint(resourceBundle.getString(HTTPEventAdapterConstants.ADAPTER_USERNAME_HINT));
@ -94,6 +100,7 @@ public class HTTPEventAdapterFactory extends OutputEventAdapterFactory {
headersProp.setRequired(false); headersProp.setRequired(false);
dynamicPropertyList.add(urlProp); dynamicPropertyList.add(urlProp);
dynamicPropertyList.add(urlTemplateProp);
dynamicPropertyList.add(usernameProp); dynamicPropertyList.add(usernameProp);
dynamicPropertyList.add(passwordProp); dynamicPropertyList.add(passwordProp);
dynamicPropertyList.add(headersProp); dynamicPropertyList.add(headersProp);

@ -21,6 +21,8 @@ public class HTTPEventAdapterConstants {
public static final String ADAPTER_TYPE_HTTP = "oauth-http"; public static final String ADAPTER_TYPE_HTTP = "oauth-http";
public static final String ADAPTER_MESSAGE_URL = "http.url"; public static final String ADAPTER_MESSAGE_URL = "http.url";
public static final String ADAPTER_MESSAGE_URL_TEMPLATED = "http.url.templated";
public static final String ADAPTER_MESSAGE_URL_TEMPLATED_HINT = "http.url.templated.hint";
public static final String ADAPTER_MESSAGE_URL_HINT = "http.url.hint"; public static final String ADAPTER_MESSAGE_URL_HINT = "http.url.hint";
public static final String ADAPTER_CONF_DCR_URL = "dcrUrl"; public static final String ADAPTER_CONF_DCR_URL = "dcrUrl";
public static final String ADAPTER_CONF_TOKEN_URL = "tokenUrl"; public static final String ADAPTER_CONF_TOKEN_URL = "tokenUrl";

@ -18,6 +18,8 @@
http.url=URL http.url=URL
http.url.hint=The target HTTP/HTTPS URL, e.g. "http://yourhost:8080/service (URL will auto format for tenants)" http.url.hint=The target HTTP/HTTPS URL, e.g. "http://yourhost:8080/service (URL will auto format for tenants)"
http.url.templated=URL Templated
http.url.templated.hint=The target HTTP/HTTPS URL has template value(s) to fill from event. e.g. "http://yourhost:8080/service/{variable1}/{variable2}" variable1 & variable2 should be available in the event and event should be in json format
username=Username username=Username
http.username.hint=Username to obtain oauth token. Leave empty to use default. http.username.hint=Username to obtain oauth token. Leave empty to use default.
password=Password password=Password

@ -25,9 +25,9 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.extension.siddhi.devicegroup</artifactId> <artifactId>org.wso2.extension.siddhi.device</artifactId>
<packaging>bundle</packaging> <packaging>bundle</packaging>
<name>WSO2 Siddhi Execution Extension - Check device belongs to a group</name> <name>WSO2 Siddhi Execution Extension - Device management Core functionality as Siddhi extension</name>
<url>http://wso2.org</url> <url>http://wso2.org</url>
<dependencies> <dependencies>
@ -51,6 +51,10 @@
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.json.wso2</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.h2database.wso2</groupId> <groupId>com.h2database.wso2</groupId>
<artifactId>h2-database-engine</artifactId> <artifactId>h2-database-engine</artifactId>
@ -91,16 +95,6 @@
<artifactId>commons-logging-api</artifactId> <artifactId>commons-logging-api</artifactId>
<version>RELEASE</version> <version>RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.appmgt.mdm.restconnector</artifactId>
<version>4.0.94-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>asm</groupId> <groupId>asm</groupId>
<artifactId>asm</artifactId> <artifactId>asm</artifactId>
@ -127,10 +121,11 @@
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name> <Bundle-Name>${project.artifactId}</Bundle-Name>
<Export-Package> <Export-Package>
org.wso2.extension.siddhi.devicegroup, org.wso2.extension.siddhi.device,
org.wso2.extension.siddhi.devicegroup.* org.wso2.extension.siddhi.device.*
</Export-Package> </Export-Package>
<Import-Package> <Import-Package>
org.json;version="${orbit.version.json.range}",
org.wso2.siddhi.core.*, org.wso2.siddhi.core.*,
org.wso2.siddhi.query.api.*, org.wso2.siddhi.query.api.*,
org.wso2.carbon.device.mgt.core.*, org.wso2.carbon.device.mgt.core.*,

@ -0,0 +1,132 @@
/*
* Copyright (c) 2017, 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.extension.siddhi.device;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor;
import org.wso2.siddhi.core.executor.function.FunctionExecutor;
import org.wso2.siddhi.query.api.definition.Attribute;
import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
import java.util.List;
/**
* getDevicesOfStatus(status [, deviceType])
* Returns devices with specified status
* Accept Type(s): (STRING, STRING)
* Return Type(s): (STRING)
*/
public class GetDevicesOfStatusFunctionExecutor extends FunctionExecutor {
private static Log log = LogFactory.getLog(GetDevicesOfStatusFunctionExecutor.class);
private Attribute.Type returnType = Attribute.Type.STRING;
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors,
ExecutionPlanContext executionPlanContext) {
if (attributeExpressionExecutors.length != 1 && attributeExpressionExecutors.length != 2) {
throw new ExecutionPlanValidationException(
"Invalid no of arguments passed to device:getDevicesOfStatus() function, required 2 but found " +
attributeExpressionExecutors.length);
}
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the first argument (status) of device:getDevicesOfStatus() " +
"function, required " + Attribute.Type.STRING + " as status, but found " +
attributeExpressionExecutors[0].getReturnType().toString());
}
if (attributeExpressionExecutors.length == 2
&& attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument (device type) of device:getDevicesOfStatus() " +
"function, required " + Attribute.Type.STRING + " as device type, but found " +
attributeExpressionExecutors[1].getReturnType().toString());
}
}
@Override
protected Object execute(Object[] data) {
if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfStatus() function. " +
"First argument cannot be null");
}
if (data.length == 2 && data[1] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfStatus() function. " +
"Second argument cannot be null");
}
String status = (String) data[0];
String deviceType = null;
if (data.length == 2) {
deviceType = (String) data[1];
}
JSONArray deviceIds = new JSONArray();
try {
DeviceManagementProviderService deviceManagementProviderService = DeviceUtils.getDeviceManagementProviderService();
List<Device> devices = deviceManagementProviderService.getDevicesByStatus(EnrolmentInfo.Status.valueOf(status), false);
for (Device device : devices) {
if (deviceType == null || deviceType.equalsIgnoreCase(device.getType())) {
deviceIds.put(device.getDeviceIdentifier());
}
}
} catch (DeviceManagementException e) {
log.error("Error occurred while getting devices with status " + status, e);
}
return deviceIds.toString();
}
@Override
protected Object execute(Object data) {
return execute(new Object[]{data});
}
@Override
public void start() {
//Nothing to start
}
@Override
public void stop() {
//Nothing to stop
}
@Override
public Attribute.Type getReturnType() {
return returnType;
}
@Override
public Object[] currentState() {
return null; //No need to maintain a state.
}
@Override
public void restoreState(Object[] state) {
//Since there's no need to maintain a state, nothing needs to be done here.
}
}

@ -0,0 +1,143 @@
/*
* Copyright (c) 2017, 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.extension.siddhi.device;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor;
import org.wso2.siddhi.core.executor.function.FunctionExecutor;
import org.wso2.siddhi.query.api.definition.Attribute;
import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
import java.util.List;
/**
* getDevicesOfUser(user , deviceType [, status])
* Returns list of ids of devices belongs to a user
* Accept Type(s): (STRING, STRING, STRING)
* Return Type(s): (STRING)
*/
public class GetDevicesOfUserFunctionExecutor extends FunctionExecutor {
private static Log log = LogFactory.getLog(GetDevicesOfUserFunctionExecutor.class);
private Attribute.Type returnType = Attribute.Type.STRING;
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors,
ExecutionPlanContext executionPlanContext) {
if (attributeExpressionExecutors.length != 2 && attributeExpressionExecutors.length != 3) {
throw new ExecutionPlanValidationException(
"Invalid no of arguments passed to device:getDevicesOfUser() function, minimum 2, or 3 with " +
"optional. but found " + attributeExpressionExecutors.length);
}
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the first argument (user) of device:getDevicesOfUser() " +
"function, required " + Attribute.Type.STRING + " as user, but found " +
attributeExpressionExecutors[0].getReturnType().toString());
}
if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument (device type) of device:getDevicesOfUser() " +
"function, required " + Attribute.Type.STRING + " as device type, but found " +
attributeExpressionExecutors[1].getReturnType().toString());
}
if (attributeExpressionExecutors.length == 3
&& attributeExpressionExecutors[2].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid optional parameter type found for the third argument (status) of " +
"device:getDevicesOfUser() function, required " + Attribute.Type.STRING + " as status, but found " +
attributeExpressionExecutors[2].getReturnType().toString());
}
}
@Override
protected Object execute(Object[] data) {
if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfUser() function. " +
"First argument cannot be null");
}
if (data[1] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfUser() function. " +
"Second argument cannot be null");
}
if (data.length == 3 && data[2] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfUser() function. " +
"Third argument cannot be null");
}
String user = (String) data[0];
String deviceType = (String) data[1];
String status = null;
if (data.length == 3) {
status = (String) data[2];
}
JSONArray deviceIds = new JSONArray();
try {
DeviceManagementProviderService deviceManagementProviderService = DeviceUtils.getDeviceManagementProviderService();
List<Device> devices = deviceManagementProviderService.getDevicesOfUser(user, deviceType, false);
for (Device device : devices) {
if (status == null || status.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) {
deviceIds.put(device.getDeviceIdentifier());
}
}
} catch (DeviceManagementException e) {
log.error("Error occurred while getting " + deviceType + " devices of user " + user +
", with status " + status, e);
}
return deviceIds.toString();
}
@Override
protected Object execute(Object data) {
return null; //Since the getDevicesOfUser function takes in 2 or 3 parameters, this method does not get called. Hence,not implemented.
}
@Override
public void start() {
//Nothing to start
}
@Override
public void stop() {
//Nothing to stop
}
@Override
public Attribute.Type getReturnType() {
return returnType;
}
@Override
public Object[] currentState() {
return null; //No need to maintain a state.
}
@Override
public void restoreState(Object[] state) {
//Since there's no need to maintain a state, nothing needs to be done here.
}
}

@ -0,0 +1,134 @@
/*
* Copyright (c) 2017, 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.extension.siddhi.device;
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.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor;
import org.wso2.siddhi.core.executor.function.FunctionExecutor;
import org.wso2.siddhi.query.api.definition.Attribute;
import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
import java.util.List;
/**
* hasDevicesOfStatus(status [, deviceType])
* Returns true if there are devices with specified status
* Accept Type(s): (STRING, STRING)
* Return Type(s): (BOOL)
*/
public class HasDevicesOfStatusFunctionExecutor extends FunctionExecutor {
private static Log log = LogFactory.getLog(HasDevicesOfStatusFunctionExecutor.class);
private Attribute.Type returnType = Attribute.Type.BOOL;
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors,
ExecutionPlanContext executionPlanContext) {
if (attributeExpressionExecutors.length != 1 && attributeExpressionExecutors.length != 2) {
throw new ExecutionPlanValidationException(
"Invalid no of arguments passed to device:hasDevicesOfStatus() function, required 2 but found " +
attributeExpressionExecutors.length);
}
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the first argument (status) of device:hasDevicesOfStatus() " +
"function, required " + Attribute.Type.STRING + " as status, but found " +
attributeExpressionExecutors[0].getReturnType().toString());
}
if (attributeExpressionExecutors.length == 2
&& attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument (device type) of device:hasDevicesOfStatus() " +
"function, required " + Attribute.Type.STRING + " as device type, but found " +
attributeExpressionExecutors[1].getReturnType().toString());
}
}
@Override
protected Object execute(Object[] data) {
if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:hasDevicesOfStatus() function. " +
"First argument cannot be null");
}
if (data.length == 2 && data[1] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:hasDevicesOfStatus() function. " +
"Second argument cannot be null");
}
String status = (String) data[0];
String deviceType = null;
if (data.length == 2) {
deviceType = (String) data[1];
}
try {
DeviceManagementProviderService deviceManagementProviderService = DeviceUtils.getDeviceManagementProviderService();
List<Device> devices = deviceManagementProviderService.getDevicesByStatus(EnrolmentInfo.Status.valueOf(status), false);
if (deviceType == null) {
return !devices.isEmpty();
} else {
for (Device device : devices) {
if (deviceType.equalsIgnoreCase(device.getType())) {
return true;
}
}
return false;
}
} catch (DeviceManagementException e) {
log.error("Error occurred while getting devices with status " + status, e);
}
return false;
}
@Override
protected Object execute(Object data) {
return execute(new Object[]{data});
}
@Override
public void start() {
//Nothing to start
}
@Override
public void stop() {
//Nothing to stop
}
@Override
public Attribute.Type getReturnType() {
return returnType;
}
@Override
public Object[] currentState() {
return null; //No need to maintain a state.
}
@Override
public void restoreState(Object[] state) {
//Since there's no need to maintain a state, nothing needs to be done here.
}
}

@ -0,0 +1,147 @@
/*
* Copyright (c) 2017, 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.extension.siddhi.device;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor;
import org.wso2.siddhi.core.executor.function.FunctionExecutor;
import org.wso2.siddhi.query.api.definition.Attribute;
import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
import java.util.List;
/**
* hasDevicesOfUser(user , deviceType [, status])
* Returns true if there are devices belonging to user
* Accept Type(s): (STRING, STRING, STRING)
* Return Type(s): (BOOL)
*/
public class HasDevicesOfUserFunctionExecutor extends FunctionExecutor {
private static Log log = LogFactory.getLog(HasDevicesOfUserFunctionExecutor.class);
private Attribute.Type returnType = Attribute.Type.BOOL;
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors,
ExecutionPlanContext executionPlanContext) {
if (attributeExpressionExecutors.length != 2 && attributeExpressionExecutors.length != 3) {
throw new ExecutionPlanValidationException(
"Invalid no of arguments passed to device:getDevicesOfUser() function, minimum 2, or 3 with " +
"optional. but found " + attributeExpressionExecutors.length);
}
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the first argument (user) of device:getDevicesOfUser() " +
"function, required " + Attribute.Type.STRING + " as user, but found " +
attributeExpressionExecutors[0].getReturnType().toString());
}
if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument (device type) of device:getDevicesOfUser() " +
"function, required " + Attribute.Type.STRING + " as device type, but found " +
attributeExpressionExecutors[1].getReturnType().toString());
}
if (attributeExpressionExecutors.length == 3
&& attributeExpressionExecutors[2].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid optional parameter type found for the third argument (status) of " +
"device:getDevicesOfUser() function, required " + Attribute.Type.STRING + " as status, but found " +
attributeExpressionExecutors[2].getReturnType().toString());
}
}
@Override
protected Object execute(Object[] data) {
if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfUser() function. " +
"First argument cannot be null");
}
if (data[1] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfUser() function. " +
"Second argument cannot be null");
}
if (data.length == 3 && data[2] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:getDevicesOfUser() function. " +
"Third argument cannot be null");
}
String user = (String) data[0];
String deviceType = (String) data[1];
String status = null;
if (data.length == 3) {
status = (String) data[2];
}
try {
DeviceManagementProviderService deviceManagementProviderService = DeviceUtils.getDeviceManagementProviderService();
List<Device> devices = deviceManagementProviderService.getDevicesOfUser(user, deviceType, false);
if (status == null) {
return !devices.isEmpty();
} else {
for (Device device : devices) {
if (status.equalsIgnoreCase(device.getEnrolmentInfo().getStatus().toString())) {
return true;
}
}
return false;
}
} catch (DeviceManagementException e) {
log.error("Error occurred while getting " + deviceType + " devices of user " + user +
", with status " + status, e);
}
return false;
}
@Override
protected Object execute(Object data) {
return null; //Since the getDevicesOfUser function takes in 2 or 3 parameters, this method does not get called. Hence,not implemented.
}
@Override
public void start() {
//Nothing to start
}
@Override
public void stop() {
//Nothing to stop
}
@Override
public Attribute.Type getReturnType() {
return returnType;
}
@Override
public Object[] currentState() {
return null; //No need to maintain a state.
}
@Override
public void restoreState(Object[] state) {
//Since there's no need to maintain a state, nothing needs to be done here.
}
}

@ -0,0 +1,122 @@
/*
* Copyright (c) 2017, 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.extension.siddhi.device;
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.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor;
import org.wso2.siddhi.core.executor.function.FunctionExecutor;
import org.wso2.siddhi.query.api.definition.Attribute;
import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
/**
* isEnrolled(deviceId, deviceType)
* Returns true if device enrolled.
* Accept Type(s): (STRING, STRING)
* Return Type(s): (BOOL)
*/
public class IsEnrolledFunctionExecutor extends FunctionExecutor {
private static Log log = LogFactory.getLog(IsEnrolledFunctionExecutor.class);
private Attribute.Type returnType = Attribute.Type.BOOL;
@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors,
ExecutionPlanContext executionPlanContext) {
if (attributeExpressionExecutors.length != 2) {
throw new ExecutionPlanValidationException(
"Invalid no of arguments passed to device:isEnrolled() function, required 2, but found "
+ attributeExpressionExecutors.length);
}
if (attributeExpressionExecutors[0].getReturnType()!= Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the first argument (deviceId) of device:isEnrolled() " +
"function, required " + Attribute.Type.STRING + ", but found " +
attributeExpressionExecutors[0].getReturnType().toString());
}
if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument (deviceType) of device:isEnrolled() " +
"function, required " + Attribute.Type.STRING + ", but found " +
attributeExpressionExecutors[1].getReturnType().toString());
}
}
@Override
protected Object execute(Object[] data) {
if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:isEnrolled() function. " +
"First argument cannot be null");
}
if (data[1] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to device:isEnrolled() function. " +
"Second argument cannot be null");
}
String deviceId = (String) data[0];
String deviceType = (String) data[1];
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
try {
DeviceManagementProviderService deviceManagementService = DeviceUtils.getDeviceManagementProviderService();
return deviceManagementService.isEnrolled(deviceIdentifier);
} catch (DeviceManagementException e) {
log.error("Error occurred while checking device is enrolled.", e);
}
return false;
}
@Override
protected Object execute(Object data) {
return null; //Since the getDevicesOfUser function takes in 2 parameters, this method does not get called. Hence,not implemented.
}
@Override
public void start() {
//Nothing to start
}
@Override
public void stop() {
//Nothing to stop
}
@Override
public Attribute.Type getReturnType() {
return returnType;
}
@Override
public Object[] currentState() {
return null; //No need to maintain a state.
}
@Override
public void restoreState(Object[] state) {
//Since there's no need to maintain a state, nothing needs to be done here.
}
}

@ -16,14 +16,14 @@
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup; package org.wso2.extension.siddhi.device;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.extension.siddhi.devicegroup.utils.DeviceGroupUtils; import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.config.ExecutionPlanContext; import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException; import org.wso2.siddhi.core.exception.ExecutionPlanRuntimeException;
import org.wso2.siddhi.core.executor.ExpressionExecutor; import org.wso2.siddhi.core.executor.ExpressionExecutor;
@ -33,14 +33,14 @@ import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
/** /**
* isDeviceInGroup(deviceId , groupId) * isInGroup(groupId, deviceId, deviceType)
* Returns true if device belongs to group, otherwise false. * Returns true if device belongs to group, otherwise false.
* Accept Type(s): (STRING, INTEGER) * Accept Type(s): (INTEGER, STRING, STRING)
* Return Type(s): (BOOL) * Return Type(s): (BOOL)
*/ */
public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor { public class IsInGroupFunctionExecutor extends FunctionExecutor {
private static Log log = LogFactory.getLog(IsDeviceInGroupFunctionExecutor.class); private static Log log = LogFactory.getLog(IsInGroupFunctionExecutor.class);
private Attribute.Type returnType = Attribute.Type.BOOL; private Attribute.Type returnType = Attribute.Type.BOOL;
@Override @Override
@ -48,24 +48,24 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
ExecutionPlanContext executionPlanContext) { ExecutionPlanContext executionPlanContext) {
if (attributeExpressionExecutors.length != 3) { if (attributeExpressionExecutors.length != 3) {
throw new ExecutionPlanValidationException( throw new ExecutionPlanValidationException(
"Invalid no of arguments passed to group:isDeviceInGroup() function, required 3, but found " "Invalid no of arguments passed to device:isInGroup() function, required 3, but found "
+ attributeExpressionExecutors.length); + attributeExpressionExecutors.length);
} }
if (attributeExpressionExecutors[0].getReturnType()!= Attribute.Type.INT) { if (attributeExpressionExecutors[0].getReturnType()!= Attribute.Type.INT) {
throw new ExecutionPlanValidationException( throw new ExecutionPlanValidationException(
"Invalid parameter type found for the first argument (group id) of group:isDeviceInGroup() " + "Invalid parameter type found for the first argument (group id) of device:isInGroup() " +
"function, required " + Attribute.Type.INT + ", but found " + "function, required " + Attribute.Type.INT + ", but found " +
attributeExpressionExecutors[0].getReturnType().toString()); attributeExpressionExecutors[0].getReturnType().toString());
} }
if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) { if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException( throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument (device id) of group:isDeviceInGroup() " + "Invalid parameter type found for the second argument (device id) of device:isInGroup() " +
"function, required " + Attribute.Type.STRING + ", but found " + "function, required " + Attribute.Type.STRING + ", but found " +
attributeExpressionExecutors[1].getReturnType().toString()); attributeExpressionExecutors[1].getReturnType().toString());
} }
if (attributeExpressionExecutors[2].getReturnType() != Attribute.Type.STRING) { if (attributeExpressionExecutors[2].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException( throw new ExecutionPlanValidationException(
"Invalid parameter type found for the third argument (device type) of group:isDeviceInGroup() " + "Invalid parameter type found for the third argument (device type) of device:isInGroup() " +
"function, required " + Attribute.Type.STRING + ", but found " + "function, required " + Attribute.Type.STRING + ", but found " +
attributeExpressionExecutors[2].getReturnType().toString()); attributeExpressionExecutors[2].getReturnType().toString());
} }
@ -74,15 +74,15 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
@Override @Override
protected Object execute(Object[] data) { protected Object execute(Object[] data) {
if (data[0] == null) { if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to group:isDeviceInGroup() function. " + throw new ExecutionPlanRuntimeException("Invalid input given to device:isInGroup() function. " +
"First argument cannot be null"); "First argument cannot be null");
} }
if (data[1] == null) { if (data[1] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to group:isDeviceInGroup() function. " + throw new ExecutionPlanRuntimeException("Invalid input given to device:isInGroup() function. " +
"Second argument cannot be null"); "Second argument cannot be null");
} }
if (data[2] == null) { if (data[2] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to group:isDeviceInGroup() function. " + throw new ExecutionPlanRuntimeException("Invalid input given to device:isInGroup() function. " +
"Third argument cannot be null"); "Third argument cannot be null");
} }
Integer groupId = (Integer) data[0]; Integer groupId = (Integer) data[0];
@ -90,7 +90,7 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
String deviceType = (String) data[2]; String deviceType = (String) data[2];
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
GroupManagementProviderService groupManagementService = DeviceGroupUtils.getGroupManagementProviderService(); GroupManagementProviderService groupManagementService = DeviceUtils.getGroupManagementProviderService();
try { try {
return groupManagementService.isDeviceMappedToGroup(groupId, deviceIdentifier); return groupManagementService.isDeviceMappedToGroup(groupId, deviceIdentifier);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
@ -101,7 +101,7 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
@Override @Override
protected Object execute(Object data) { protected Object execute(Object data) {
return null; //Since the getProperty function takes in 2 parameters, this method does not get called. Hence,not implemented. return null; //Since the isInGroup function takes in 3 parameters, this method does not get called. Hence,not implemented.
} }
@Override @Override
@ -129,5 +129,3 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
//Since there's no need to maintain a state, nothing needs to be done here. //Since there's no need to maintain a state, nothing needs to be done here.
} }
} }

@ -15,48 +15,53 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.utils; package org.wso2.extension.siddhi.device.utils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
/** /**
* This class holds utility methods to retrieve data. * This class holds utility methods to retrieve data.
*/ */
public class DeviceGroupUtils { public class DeviceUtils {
private static Log log = LogFactory.getLog(DeviceGroupUtils.class); private static Log log = LogFactory.getLog(DeviceUtils.class);
private static GroupManagementProviderService groupManagementProviderServiceForTest; private static DeviceManagementProviderService deviceManagementProviderService;
private static GroupManagementProviderService groupManagementProviderService;
private DeviceGroupUtils(){ private DeviceUtils(){
}
public static DeviceManagementProviderService getDeviceManagementProviderService() {
if (deviceManagementProviderService != null) {
return deviceManagementProviderService;
}
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
deviceManagementProviderService =
(DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
if (deviceManagementProviderService == null) {
String msg = "Device Management service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
return deviceManagementProviderService;
} }
public static GroupManagementProviderService getGroupManagementProviderService() { public static GroupManagementProviderService getGroupManagementProviderService() {
if (groupManagementProviderServiceForTest != null) { if (groupManagementProviderService != null) {
return groupManagementProviderServiceForTest; return groupManagementProviderService;
} }
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
GroupManagementProviderService groupManagementProviderService = groupManagementProviderService =
(GroupManagementProviderService) ctx.getOSGiService(GroupManagementProviderService.class, null); (GroupManagementProviderService) ctx.getOSGiService(GroupManagementProviderService.class, null);
if (groupManagementProviderService == null) { if (groupManagementProviderService == null) {
String msg = "GroupImpl Management service has not initialized."; String msg = "Group Management service has not initialized.";
log.error(msg); log.error(msg);
throw new IllegalStateException(msg); throw new IllegalStateException(msg);
} }
return groupManagementProviderService; return groupManagementProviderService;
} }
/**
* This method is only to set groupManagementProviderService locally for testing as OSGi framework cannot start
* with testng to register the groupManagementProviderService. Hence setting groupManagementProviderService from
* CheckDeviceInGroupExtensionTestCase
* @param groupManagementProviderServiceForTest to be set.
*/
public static void setGroupManagementProviderServiceForTest(
GroupManagementProviderService groupManagementProviderServiceForTest) {
DeviceGroupUtils.groupManagementProviderServiceForTest = groupManagementProviderServiceForTest;
}
} }

@ -16,4 +16,9 @@
# under the License. # under the License.
# #
isDeviceInGroup=org.wso2.extension.siddhi.devicegroup.IsDeviceInGroupFunctionExecutor isInGroup=org.wso2.extension.siddhi.device.IsInGroupFunctionExecutor
getDevicesOfUser=org.wso2.extension.siddhi.device.GetDevicesOfUserFunctionExecutor
hasDevicesOfUser=org.wso2.extension.siddhi.device.HasDevicesOfUserFunctionExecutor
getDevicesOfStatus=org.wso2.extension.siddhi.device.GetDevicesOfStatusFunctionExecutor
hasDevicesOfStatus=org.wso2.extension.siddhi.device.HasDevicesOfStatusFunctionExecutor
isEnrolled=org.wso2.extension.siddhi.device.IsEnrolledFunctionExecutor

@ -16,7 +16,7 @@
* under the License. * under the License.
* *
*/ */
package org.wso2.extension.siddhi.devicegroup; package org.wso2.extension.siddhi.device;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -32,8 +32,8 @@ import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.extension.siddhi.devicegroup.test.util.DataSourceConfig; import org.wso2.extension.siddhi.device.test.util.DataSourceConfig;
import org.wso2.extension.siddhi.devicegroup.test.util.TestUtils; import org.wso2.extension.siddhi.device.test.util.TestUtils;
import javax.sql.DataSource; import javax.sql.DataSource;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;

@ -16,13 +16,12 @@
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup; package org.wso2.extension.siddhi.device;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -47,11 +46,11 @@ import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.UserStoreException; import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.extension.siddhi.devicegroup.test.util.SiddhiTestHelper; import org.wso2.extension.siddhi.device.test.util.SiddhiTestHelper;
import org.wso2.extension.siddhi.devicegroup.test.util.TestDataHolder; import org.wso2.extension.siddhi.device.test.util.TestDataHolder;
import org.wso2.extension.siddhi.devicegroup.test.util.TestDeviceManagementService; import org.wso2.extension.siddhi.device.test.util.TestDeviceManagementService;
import org.wso2.extension.siddhi.devicegroup.test.util.TestUtils; import org.wso2.extension.siddhi.device.test.util.TestUtils;
import org.wso2.extension.siddhi.devicegroup.utils.DeviceGroupUtils; import org.wso2.extension.siddhi.device.utils.DeviceUtils;
import org.wso2.siddhi.core.ExecutionPlanRuntime; import org.wso2.siddhi.core.ExecutionPlanRuntime;
import org.wso2.siddhi.core.SiddhiManager; import org.wso2.siddhi.core.SiddhiManager;
import org.wso2.siddhi.core.event.Event; import org.wso2.siddhi.core.event.Event;
@ -60,39 +59,56 @@ import org.wso2.siddhi.core.stream.input.InputHandler;
import org.wso2.siddhi.core.util.EventPrinter; import org.wso2.siddhi.core.util.EventPrinter;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS; import static org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS;
import static org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE; import static org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE;
public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTest { public class ExtensionTestCase extends BaseDeviceManagementTest {
private static final Logger log = Logger.getLogger(CheckDeviceInGroupExtensionTestCase.class); private static final Logger log = Logger.getLogger(ExtensionTestCase.class);
private AtomicInteger count = new AtomicInteger(0); private AtomicInteger count = new AtomicInteger(0);
private volatile boolean eventArrived; private volatile boolean eventArrived;
private GroupManagementProviderService groupManagementProviderService; private GroupManagementProviderService groupManagementProviderService;
private DeviceManagementProviderService deviceMgtService; private DeviceManagementProviderService deviceManagementProviderService;
private static String DEVICE_TYPE = "Test"; private static String DEVICE_TYPE = "Test";
private QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
for (Event event : inEvents) {
count.incrementAndGet();
eventArrived = true;
}
}
};
@BeforeClass @BeforeClass
@Override @Override
public void init() throws Exception { public void init() throws Exception {
log.info("Initializing"); log.info("Initializing");
count.set(0);
eventArrived = false;
groupManagementProviderService = new GroupManagementProviderServiceImpl(); groupManagementProviderService = new GroupManagementProviderServiceImpl();
deviceMgtService = new DeviceManagementProviderServiceImpl(); deviceManagementProviderService = new DeviceManagementProviderServiceImpl();
DeviceGroupUtils.setGroupManagementProviderServiceForTest(groupManagementProviderService);
DeviceManagementServiceComponent.notifyStartupListeners(); DeviceManagementServiceComponent.notifyStartupListeners();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService);
DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService()); DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService());
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl());
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(groupManagementProviderService); DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(groupManagementProviderService);
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
deviceMgtService.registerDeviceType( deviceManagementProviderService.registerDeviceType(
new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));
Field deviceManagementProviderServiceField = DeviceUtils.class.getDeclaredField("deviceManagementProviderService");
deviceManagementProviderServiceField.setAccessible(true);
deviceManagementProviderServiceField.set(null, deviceManagementProviderService);
Field groupManagementProviderServiceField = DeviceUtils.class.getDeclaredField("groupManagementProviderService");
groupManagementProviderServiceField.setAccessible(true);
groupManagementProviderServiceField.set(null, groupManagementProviderService);
} }
private RegistryService getRegistryService() throws RegistryException, UserStoreException, private RegistryService getRegistryService() throws RegistryException, UserStoreException,
@ -119,7 +135,7 @@ public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTes
public void enrollDevice() { public void enrollDevice() {
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE); Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
try { try {
boolean enrollmentStatus = deviceMgtService.enrollDevice(device); boolean enrollmentStatus = deviceManagementProviderService.enrollDevice(device);
Assert.assertTrue(enrollmentStatus); Assert.assertTrue(enrollmentStatus);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error Occurred while enrolling device"; String msg = "Error Occurred while enrolling device";
@ -140,25 +156,18 @@ public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTes
} }
@Test(dependsOnMethods = {"addDevices"}) @Test(dependsOnMethods = {"addDevices"})
public void testIsDeviceInGroupExtension() throws InterruptedException, GroupManagementException { public void testIsInGroupExtension() throws InterruptedException, GroupManagementException {
log.info("IsDeviceInGroup TestCase"); log.info("IsInGroup TestCase");
SiddhiManager siddhiManager = new SiddhiManager(); SiddhiManager siddhiManager = new SiddhiManager();
count.set(0);
eventArrived = false;
String inStreamDefinition = "define stream inputStream (groupId int, deviceId string, deviceType string);"; String inStreamDefinition = "define stream inputStream (groupId int, deviceId string, deviceType string);";
String query = ("@info(name = 'query1') from inputStream[devicegroup:isDeviceInGroup(groupId, deviceId, deviceType) == true] " + String query = ("@info(name = 'query1') from inputStream[device:isInGroup(groupId, deviceId, deviceType)] " +
"select deviceId insert into outputStream;"); "select deviceId insert into outputStream;");
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query); ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);
executionPlanRuntime.addCallback("query1", queryCallback);
executionPlanRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
for (Event event : inEvents) {
count.incrementAndGet();
eventArrived = true;
}
}
});
InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream"); InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
executionPlanRuntime.start(); executionPlanRuntime.start();
@ -172,4 +181,101 @@ public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTes
Assert.assertEquals(1, count.get()); Assert.assertEquals(1, count.get());
executionPlanRuntime.shutdown(); executionPlanRuntime.shutdown();
} }
@Test(dependsOnMethods = {"testIsInGroupExtension"})
public void testGetDevicesOfUserFunctionExecutor() throws InterruptedException, GroupManagementException {
log.info("GetDevicesOfUser without status TestCase");
SiddhiManager siddhiManager = new SiddhiManager();
count.set(0);
eventArrived = false;
String inStreamDefinition = "define stream inputStream (user string, deviceType string);";
String query = ("@info(name = 'query1') from inputStream[device:hasDevicesOfUser(user, deviceType)] " +
"select device:getDevicesOfUser(user, deviceType) as devices insert into outputStream;");
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);
executionPlanRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
executionPlanRuntime.start();
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
inputHandler.send(new Object[]{device.getEnrolmentInfo().getOwner(), device.getType()});
SiddhiTestHelper.waitForEvents(100, 1, count, 10000);
Assert.assertTrue(eventArrived);
Assert.assertEquals(1, count.get());
executionPlanRuntime.shutdown();
}
@Test(dependsOnMethods = {"testGetDevicesOfUserFunctionExecutor"})
public void testGetDevicesOfUserWithStatusFunctionExecutor() throws InterruptedException, GroupManagementException {
log.info("GetDevicesOfUser with status TestCase");
SiddhiManager siddhiManager = new SiddhiManager();
count.set(0);
eventArrived = false;
String inStreamDefinition = "define stream inputStream (user string, deviceType string, status string);";
String query = ("@info(name = 'query1') from inputStream[device:hasDevicesOfUser(user, deviceType, status)] " +
"select device:getDevicesOfUser(user, deviceType, status) as devices insert into outputStream;");
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);
executionPlanRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
executionPlanRuntime.start();
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
inputHandler.send(new Object[]{device.getEnrolmentInfo().getOwner(), device.getType(),
device.getEnrolmentInfo().getStatus().toString()});
SiddhiTestHelper.waitForEvents(100, 1, count, 10000);
Assert.assertTrue(eventArrived);
Assert.assertEquals(1, count.get());
executionPlanRuntime.shutdown();
}
@Test(dependsOnMethods = {"testGetDevicesOfUserWithStatusFunctionExecutor"})
public void testGetDevicesOfStatusFunctionExecutor() throws InterruptedException, GroupManagementException {
log.info("GetDevicesOfStatus without deviceType TestCase");
SiddhiManager siddhiManager = new SiddhiManager();
count.set(0);
eventArrived = false;
String inStreamDefinition = "define stream inputStream (status string);";
String query = ("@info(name = 'query1') from inputStream[device:hasDevicesOfStatus(status)] " +
"select device:getDevicesOfStatus(status) as devices insert into outputStream;");
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);
executionPlanRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
executionPlanRuntime.start();
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
inputHandler.send(new Object[]{device.getEnrolmentInfo().getStatus().toString()});
SiddhiTestHelper.waitForEvents(100, 1, count, 10000);
Assert.assertTrue(eventArrived);
Assert.assertEquals(1, count.get());
executionPlanRuntime.shutdown();
}
@Test(dependsOnMethods = {"testGetDevicesOfStatusFunctionExecutor"})
public void testGetDevicesOfStatusWithTypeFunctionExecutor() throws InterruptedException, GroupManagementException {
log.info("GetDevicesOfStatus with deviceType TestCase");
SiddhiManager siddhiManager = new SiddhiManager();
count.set(0);
eventArrived = false;
String inStreamDefinition = "define stream inputStream (status string, deviceType string);";
String query = ("@info(name = 'query1') from inputStream[device:hasDevicesOfStatus(status, deviceType)] " +
"select device:getDevicesOfStatus(status, deviceType) as devices insert into outputStream;");
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);
executionPlanRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
executionPlanRuntime.start();
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
inputHandler.send(new Object[]{device.getEnrolmentInfo().getStatus().toString(), device.getType()});
SiddhiTestHelper.waitForEvents(100, 1, count, 10000);
Assert.assertTrue(eventArrived);
Assert.assertEquals(1, count.get());
executionPlanRuntime.shutdown();
}
} }

@ -15,7 +15,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.test.util; package org.wso2.extension.siddhi.device.test.util;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;

@ -16,7 +16,7 @@
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.test.util; package org.wso2.extension.siddhi.device.test.util;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.test.util; package org.wso2.extension.siddhi.device.test.util;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
@ -45,7 +45,7 @@ public class TestDataHolder {
enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
enrolmentInfo.setOwner(OWNER); enrolmentInfo.setOwner(OWNER);
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED); enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
device.setEnrolmentInfo(enrolmentInfo); device.setEnrolmentInfo(enrolmentInfo);
device.setDescription("Test Description"); device.setDescription("Test Description");
device.setDeviceIdentifier(initialDeviceIdentifier); device.setDeviceIdentifier(initialDeviceIdentifier);

@ -15,7 +15,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.test.util; package org.wso2.extension.siddhi.device.test.util;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceManager;

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.test.util; package org.wso2.extension.siddhi.device.test.util;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;

@ -15,7 +15,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.extension.siddhi.devicegroup.test.util; package org.wso2.extension.siddhi.device.test.util;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;

@ -24,8 +24,8 @@
<test name="Extension Unit Tests" preserve-order="true"> <test name="Extension Unit Tests" preserve-order="true">
<classes> <classes>
<class name="org.wso2.extension.siddhi.devicegroup.BaseDeviceManagementTest"/> <class name="org.wso2.extension.siddhi.device.BaseDeviceManagementTest"/>
<class name="org.wso2.extension.siddhi.devicegroup.CheckDeviceInGroupExtensionTestCase"/> <class name="org.wso2.extension.siddhi.device.ExtensionTestCase"/>
</classes> </classes>
</test> </test>
</suite> </suite>

@ -33,7 +33,7 @@
<url>http://wso2.org</url> <url>http://wso2.org</url>
<modules> <modules>
<module>org.wso2.extension.siddhi.devicegroup</module> <module>org.wso2.extension.siddhi.device</module>
<module>org.wso2.extension.siddhi.execution.json</module> <module>org.wso2.extension.siddhi.execution.json</module>
<module>org.wso2.gpl.siddhi.extension.geo.script</module> <module>org.wso2.gpl.siddhi.extension.geo.script</module>
</modules> </modules>

@ -24,7 +24,7 @@
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>org.wso2.extension.siddhi.devicegroup.feature</artifactId> <artifactId>org.wso2.extension.siddhi.device.feature</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>WSO2 Siddhi Execution Extension - Device Group Feature</name> <name>WSO2 Siddhi Execution Extension - Device Group Feature</name>
<url>http://wso2.org</url> <url>http://wso2.org</url>
@ -33,7 +33,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.extension.siddhi.devicegroup</artifactId> <artifactId>org.wso2.extension.siddhi.device</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
@ -51,7 +51,7 @@
<goal>p2-feature-gen</goal> <goal>p2-feature-gen</goal>
</goals> </goals>
<configuration> <configuration>
<id>org.wso2.extension.siddhi.devicegroup</id> <id>org.wso2.extension.siddhi.device</id>
<propertiesFile>../../etc/feature.properties</propertiesFile> <propertiesFile>../../etc/feature.properties</propertiesFile>
<adviceFile> <adviceFile>
<properties> <properties>
@ -61,7 +61,7 @@
</adviceFile> </adviceFile>
<bundles> <bundles>
<bundleDef> <bundleDef>
org.wso2.carbon.devicemgt-plugins:org.wso2.extension.siddhi.devicegroup:${carbon.devicemgt.plugins.version} org.wso2.carbon.devicemgt-plugins:org.wso2.extension.siddhi.device:${carbon.devicemgt.plugins.version}
</bundleDef> </bundleDef>
</bundles> </bundles>
</configuration> </configuration>

@ -39,7 +39,7 @@
<module>org.wso2.carbon.device.mgt.adapter.feature</module> <module>org.wso2.carbon.device.mgt.adapter.feature</module>
<module>org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization.feature</module> <module>org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization.feature</module>
<module>org.wso2.carbon.andes.extensions.device.mgt.api.feature</module> <module>org.wso2.carbon.andes.extensions.device.mgt.api.feature</module>
<module>org.wso2.extension.siddhi.devicegroup.feature</module> <module>org.wso2.extension.siddhi.device.feature</module>
<module>org.wso2.extension.siddhi.execution.json.feature</module> <module>org.wso2.extension.siddhi.execution.json.feature</module>
<module>org.wso2.carbon.device.mgt.notification.listener.feature</module> <module>org.wso2.carbon.device.mgt.notification.listener.feature</module>
<module>org.wso2.gpl.siddhi.extension.geo.script.feature</module> <module>org.wso2.gpl.siddhi.extension.geo.script.feature</module>

@ -412,7 +412,7 @@
<!--IoT Server specific dependencies--> <!--IoT Server specific dependencies-->
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.extension.siddhi.devicegroup</artifactId> <artifactId>org.wso2.extension.siddhi.device</artifactId>
<version>${carbon.devicemgt.plugins.version}</version> <version>${carbon.devicemgt.plugins.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -1210,7 +1210,7 @@
<javax.ws.rs.version>1.1.1</javax.ws.rs.version> <javax.ws.rs.version>1.1.1</javax.ws.rs.version>
<!-- Carbon Device Management --> <!-- Carbon Device Management -->
<carbon.devicemgt.version>3.0.172</carbon.devicemgt.version> <carbon.devicemgt.version>3.0.175</carbon.devicemgt.version>
<carbon.devicemgt.version.range>[3.0.0, 4.0.0)</carbon.devicemgt.version.range> <carbon.devicemgt.version.range>[3.0.0, 4.0.0)</carbon.devicemgt.version.range>
<!-- Carbon App Management --> <!-- Carbon App Management -->

Loading…
Cancel
Save