mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
Add siddhi extensions to reflect get methods in device management core service
This commit is contained in:
parent
8c27f9a3ad
commit
a2a46a3439
@ -25,9 +25,9 @@
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>org.wso2.extension.siddhi.devicegroup</artifactId>
|
||||
<artifactId>org.wso2.extension.siddhi.device</artifactId>
|
||||
<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>
|
||||
|
||||
<dependencies>
|
||||
@ -51,6 +51,10 @@
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json.wso2</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database.wso2</groupId>
|
||||
<artifactId>h2-database-engine</artifactId>
|
||||
@ -91,16 +95,6 @@
|
||||
<artifactId>commons-logging-api</artifactId>
|
||||
<version>RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||
<artifactId>org.wso2.carbon.appmgt.mdm.restconnector</artifactId>
|
||||
<version>4.0.91-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>asm</groupId>
|
||||
<artifactId>asm</artifactId>
|
||||
<version>RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>asm</groupId>
|
||||
<artifactId>asm</artifactId>
|
||||
@ -127,10 +121,11 @@
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Export-Package>
|
||||
org.wso2.extension.siddhi.devicegroup,
|
||||
org.wso2.extension.siddhi.devicegroup.*
|
||||
org.wso2.extension.siddhi.device,
|
||||
org.wso2.extension.siddhi.device.*
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
org.json;version="${orbit.version.json.range}",
|
||||
org.wso2.siddhi.core.*,
|
||||
org.wso2.siddhi.query.api.*,
|
||||
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.
|
||||
*/
|
||||
|
||||
package org.wso2.extension.siddhi.devicegroup;
|
||||
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.group.mgt.GroupManagementException;
|
||||
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.exception.ExecutionPlanRuntimeException;
|
||||
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.
|
||||
* Accept Type(s): (STRING, INTEGER)
|
||||
* Accept Type(s): (INTEGER, STRING, STRING)
|
||||
* 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;
|
||||
|
||||
@Override
|
||||
@ -48,24 +48,24 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
|
||||
ExecutionPlanContext executionPlanContext) {
|
||||
if (attributeExpressionExecutors.length != 3) {
|
||||
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);
|
||||
}
|
||||
if (attributeExpressionExecutors[0].getReturnType()!= Attribute.Type.INT) {
|
||||
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 " +
|
||||
attributeExpressionExecutors[0].getReturnType().toString());
|
||||
}
|
||||
if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
|
||||
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 " +
|
||||
attributeExpressionExecutors[1].getReturnType().toString());
|
||||
}
|
||||
if (attributeExpressionExecutors[2].getReturnType() != Attribute.Type.STRING) {
|
||||
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 " +
|
||||
attributeExpressionExecutors[2].getReturnType().toString());
|
||||
}
|
||||
@ -74,15 +74,15 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
|
||||
@Override
|
||||
protected Object execute(Object[] data) {
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
Integer groupId = (Integer) data[0];
|
||||
@ -90,7 +90,7 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
|
||||
String deviceType = (String) data[2];
|
||||
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
|
||||
GroupManagementProviderService groupManagementService = DeviceGroupUtils.getGroupManagementProviderService();
|
||||
GroupManagementProviderService groupManagementService = DeviceUtils.getGroupManagementProviderService();
|
||||
try {
|
||||
return groupManagementService.isDeviceMappedToGroup(groupId, deviceIdentifier);
|
||||
} catch (GroupManagementException e) {
|
||||
@ -101,7 +101,7 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
|
||||
|
||||
@Override
|
||||
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
|
||||
@ -129,5 +129,3 @@ public class IsDeviceInGroupFunctionExecutor extends FunctionExecutor {
|
||||
//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
|
||||
* 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.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
|
||||
|
||||
/**
|
||||
* This class holds utility methods to retrieve data.
|
||||
*/
|
||||
public class DeviceGroupUtils {
|
||||
public class DeviceUtils {
|
||||
|
||||
private static Log log = LogFactory.getLog(DeviceGroupUtils.class);
|
||||
private static GroupManagementProviderService groupManagementProviderServiceForTest;
|
||||
private static Log log = LogFactory.getLog(DeviceUtils.class);
|
||||
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() {
|
||||
if (groupManagementProviderServiceForTest != null) {
|
||||
return groupManagementProviderServiceForTest;
|
||||
if (groupManagementProviderService != null) {
|
||||
return groupManagementProviderService;
|
||||
}
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
GroupManagementProviderService groupManagementProviderService =
|
||||
groupManagementProviderService =
|
||||
(GroupManagementProviderService) ctx.getOSGiService(GroupManagementProviderService.class, null);
|
||||
if (groupManagementProviderService == null) {
|
||||
String msg = "GroupImpl Management service has not initialized.";
|
||||
String msg = "Group Management service has not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
# in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
@ -16,4 +16,9 @@
|
||||
# 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.
|
||||
*
|
||||
*/
|
||||
package org.wso2.extension.siddhi.devicegroup;
|
||||
package org.wso2.extension.siddhi.device;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
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.operation.mgt.dao.OperationManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
||||
import org.wso2.extension.siddhi.devicegroup.test.util.DataSourceConfig;
|
||||
import org.wso2.extension.siddhi.devicegroup.test.util.TestUtils;
|
||||
import org.wso2.extension.siddhi.device.test.util.DataSourceConfig;
|
||||
import org.wso2.extension.siddhi.device.test.util.TestUtils;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
@ -16,13 +16,12 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.extension.siddhi.devicegroup;
|
||||
package org.wso2.extension.siddhi.device;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
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.DeviceIdentifier;
|
||||
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.service.RealmService;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||
import org.wso2.extension.siddhi.devicegroup.test.util.SiddhiTestHelper;
|
||||
import org.wso2.extension.siddhi.devicegroup.test.util.TestDataHolder;
|
||||
import org.wso2.extension.siddhi.devicegroup.test.util.TestDeviceManagementService;
|
||||
import org.wso2.extension.siddhi.devicegroup.test.util.TestUtils;
|
||||
import org.wso2.extension.siddhi.devicegroup.utils.DeviceGroupUtils;
|
||||
import org.wso2.extension.siddhi.device.test.util.SiddhiTestHelper;
|
||||
import org.wso2.extension.siddhi.device.test.util.TestDataHolder;
|
||||
import org.wso2.extension.siddhi.device.test.util.TestDeviceManagementService;
|
||||
import org.wso2.extension.siddhi.device.test.util.TestUtils;
|
||||
import org.wso2.extension.siddhi.device.utils.DeviceUtils;
|
||||
import org.wso2.siddhi.core.ExecutionPlanRuntime;
|
||||
import org.wso2.siddhi.core.SiddhiManager;
|
||||
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 java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
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.Roles.DEFAULT_ADMIN_ROLE;
|
||||
|
||||
public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTest {
|
||||
private static final Logger log = Logger.getLogger(CheckDeviceInGroupExtensionTestCase.class);
|
||||
public class ExtensionTestCase extends BaseDeviceManagementTest {
|
||||
private static final Logger log = Logger.getLogger(ExtensionTestCase.class);
|
||||
|
||||
private AtomicInteger count = new AtomicInteger(0);
|
||||
private volatile boolean eventArrived;
|
||||
private GroupManagementProviderService groupManagementProviderService;
|
||||
private DeviceManagementProviderService deviceMgtService;
|
||||
private DeviceManagementProviderService deviceManagementProviderService;
|
||||
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
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
log.info("Initializing");
|
||||
count.set(0);
|
||||
eventArrived = false;
|
||||
groupManagementProviderService = new GroupManagementProviderServiceImpl();
|
||||
deviceMgtService = new DeviceManagementProviderServiceImpl();
|
||||
DeviceGroupUtils.setGroupManagementProviderServiceForTest(groupManagementProviderService);
|
||||
deviceManagementProviderService = new DeviceManagementProviderServiceImpl();
|
||||
|
||||
DeviceManagementServiceComponent.notifyStartupListeners();
|
||||
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService);
|
||||
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService);
|
||||
DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService());
|
||||
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl());
|
||||
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(groupManagementProviderService);
|
||||
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
|
||||
deviceMgtService.registerDeviceType(
|
||||
deviceManagementProviderService.registerDeviceType(
|
||||
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,
|
||||
@ -119,7 +135,7 @@ public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTes
|
||||
public void enrollDevice() {
|
||||
Device device = TestDataHolder.generateDummyDeviceData(DEVICE_TYPE);
|
||||
try {
|
||||
boolean enrollmentStatus = deviceMgtService.enrollDevice(device);
|
||||
boolean enrollmentStatus = deviceManagementProviderService.enrollDevice(device);
|
||||
Assert.assertTrue(enrollmentStatus);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error Occurred while enrolling device";
|
||||
@ -140,25 +156,18 @@ public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTes
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = {"addDevices"})
|
||||
public void testIsDeviceInGroupExtension() throws InterruptedException, GroupManagementException {
|
||||
log.info("IsDeviceInGroup TestCase");
|
||||
public void testIsInGroupExtension() throws InterruptedException, GroupManagementException {
|
||||
log.info("IsInGroup TestCase");
|
||||
SiddhiManager siddhiManager = new SiddhiManager();
|
||||
|
||||
count.set(0);
|
||||
eventArrived = false;
|
||||
|
||||
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;");
|
||||
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
executionPlanRuntime.addCallback("query1", queryCallback);
|
||||
|
||||
InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
|
||||
executionPlanRuntime.start();
|
||||
@ -172,4 +181,101 @@ public class CheckDeviceInGroupExtensionTestCase extends BaseDeviceManagementTes
|
||||
Assert.assertEquals(1, count.get());
|
||||
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
|
||||
* 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.XmlRootElement;
|
||||
@ -16,7 +16,7 @@
|
||||
* 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;
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* 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.DeviceIdentifier;
|
||||
@ -45,7 +45,7 @@ public class TestDataHolder {
|
||||
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||
enrolmentInfo.setOwner(OWNER);
|
||||
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||
enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED);
|
||||
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||
device.setEnrolmentInfo(enrolmentInfo);
|
||||
device.setDescription("Test Description");
|
||||
device.setDeviceIdentifier(initialDeviceIdentifier);
|
||||
@ -15,7 +15,7 @@
|
||||
* specific language governing permissions and 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.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* 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.DeviceIdentifier;
|
||||
@ -15,7 +15,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* 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.LogFactory;
|
||||
@ -24,8 +24,8 @@
|
||||
|
||||
<test name="Extension Unit Tests" preserve-order="true">
|
||||
<classes>
|
||||
<class name="org.wso2.extension.siddhi.devicegroup.BaseDeviceManagementTest"/>
|
||||
<class name="org.wso2.extension.siddhi.devicegroup.CheckDeviceInGroupExtensionTestCase"/>
|
||||
<class name="org.wso2.extension.siddhi.device.BaseDeviceManagementTest"/>
|
||||
<class name="org.wso2.extension.siddhi.device.ExtensionTestCase"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
@ -33,7 +33,7 @@
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<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.gpl.siddhi.extension.geo.script</module>
|
||||
</modules>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user