forked from community/device-mgt-plugins
Merge pull request #845 from charithag/master
Expose device management core APIs as siddhi extensionsrevert-dabc3590
commit
0b7f2364ed
@ -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.
|
||||
}
|
||||
}
|
32
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/main/java/org/wso2/extension/siddhi/devicegroup/IsDeviceInGroupFunctionExecutor.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/main/java/org/wso2/extension/siddhi/device/IsInGroupFunctionExecutor.java
32
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/main/java/org/wso2/extension/siddhi/devicegroup/IsDeviceInGroupFunctionExecutor.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/main/java/org/wso2/extension/siddhi/device/IsInGroupFunctionExecutor.java
45
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/main/java/org/wso2/extension/siddhi/devicegroup/utils/DeviceGroupUtils.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/main/java/org/wso2/extension/siddhi/device/utils/DeviceUtils.java
45
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/main/java/org/wso2/extension/siddhi/devicegroup/utils/DeviceGroupUtils.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/main/java/org/wso2/extension/siddhi/device/utils/DeviceUtils.java
6
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/BaseDeviceManagementTest.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/BaseDeviceManagementTest.java
6
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/BaseDeviceManagementTest.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/BaseDeviceManagementTest.java
168
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/CheckDeviceInGroupExtensionTestCase.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/ExtensionTestCase.java
168
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/CheckDeviceInGroupExtensionTestCase.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/ExtensionTestCase.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/DataSourceConfig.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/DataSourceConfig.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/DataSourceConfig.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/DataSourceConfig.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/SiddhiTestHelper.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/SiddhiTestHelper.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/SiddhiTestHelper.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/SiddhiTestHelper.java
4
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestDataHolder.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestDataHolder.java
4
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestDataHolder.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestDataHolder.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestDeviceManagementService.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestDeviceManagementService.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestDeviceManagementService.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestDeviceManagementService.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestDeviceManager.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestDeviceManager.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestDeviceManager.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestDeviceManager.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestUtils.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestUtils.java
2
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/java/org/wso2/extension/siddhi/devicegroup/test/util/TestUtils.java → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/java/org/wso2/extension/siddhi/device/test/util/TestUtils.java
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/datasources/master-datasources.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/datasources/master-datasources.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/datasources/master-datasources.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/datasources/master-datasources.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/bundle-config/README.txt → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/bundle-config/README.txt
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/bundle-config/README.txt → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/bundle-config/README.txt
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/carboncontext-osgi-services.properties → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/carboncontext-osgi-services.properties
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/carboncontext-osgi-services.properties → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/carboncontext-osgi-services.properties
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/config-validation.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/config-validation.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/config-validation.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/config-validation.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/logging-bridge.properties → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/logging-bridge.properties
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/etc/logging-bridge.properties → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/etc/logging-bridge.properties
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/security/authenticators.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/security/authenticators.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/security/authenticators.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/security/authenticators.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/tomcat/carbon/META-INF/context.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/tomcat/carbon/META-INF/context.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/tomcat/carbon/META-INF/context.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/tomcat/carbon/META-INF/context.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/tomcat/carbon/WEB-INF/web.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/tomcat/carbon/WEB-INF/web.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/tomcat/carbon/WEB-INF/web.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/tomcat/carbon/WEB-INF/web.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/tomcat/catalina-server.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/tomcat/catalina-server.xml
0
components/extensions/siddhi-extensions/org.wso2.extension.siddhi.devicegroup/src/test/resources/carbon-home/repository/conf/tomcat/catalina-server.xml → components/extensions/siddhi-extensions/org.wso2.extension.siddhi.device/src/test/resources/carbon-home/repository/conf/tomcat/catalina-server.xml
Loading…
Reference in new issue