Merge pull request #13738 from dskarzh/improve-script-errors
Improve rule engine and calculated fields test script errors
This commit is contained in:
commit
aed4af9477
@ -17,19 +17,21 @@ package org.thingsboard.server.controller;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.thingsboard.common.util.JacksonUtil;
|
import org.thingsboard.common.util.JacksonUtil;
|
||||||
@ -41,7 +43,6 @@ import org.thingsboard.script.api.tbel.TbelCfTsRollingArg;
|
|||||||
import org.thingsboard.script.api.tbel.TbelInvokeService;
|
import org.thingsboard.script.api.tbel.TbelInvokeService;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.EventInfo;
|
import org.thingsboard.server.common.data.EventInfo;
|
||||||
import org.thingsboard.server.common.data.HasTenantId;
|
|
||||||
import org.thingsboard.server.common.data.cf.CalculatedField;
|
import org.thingsboard.server.common.data.cf.CalculatedField;
|
||||||
import org.thingsboard.server.common.data.cf.configuration.CalculatedFieldConfiguration;
|
import org.thingsboard.server.common.data.cf.configuration.CalculatedFieldConfiguration;
|
||||||
import org.thingsboard.server.common.data.event.EventType;
|
import org.thingsboard.server.common.data.event.EventType;
|
||||||
@ -49,17 +50,14 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
|
|||||||
import org.thingsboard.server.common.data.id.CalculatedFieldId;
|
import org.thingsboard.server.common.data.id.CalculatedFieldId;
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.EntityIdFactory;
|
import org.thingsboard.server.common.data.id.EntityIdFactory;
|
||||||
import org.thingsboard.server.common.data.id.HasId;
|
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.page.PageData;
|
import org.thingsboard.server.common.data.page.PageData;
|
||||||
import org.thingsboard.server.common.data.page.PageLink;
|
import org.thingsboard.server.common.data.page.PageLink;
|
||||||
import org.thingsboard.server.config.annotations.ApiOperation;
|
import org.thingsboard.server.config.annotations.ApiOperation;
|
||||||
import org.thingsboard.server.dao.event.EventService;
|
import org.thingsboard.server.dao.event.EventService;
|
||||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||||
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldScriptEngine;
|
|
||||||
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldTbelScriptEngine;
|
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldTbelScriptEngine;
|
||||||
import org.thingsboard.server.service.entitiy.cf.TbCalculatedFieldService;
|
import org.thingsboard.server.service.entitiy.cf.TbCalculatedFieldService;
|
||||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
|
||||||
import org.thingsboard.server.service.security.permission.Operation;
|
import org.thingsboard.server.service.security.permission.Operation;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -133,13 +131,12 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
"Remove 'id', 'tenantId' from the request body example (below) to create new Calculated Field entity. "
|
"Remove 'id', 'tenantId' from the request body example (below) to create new Calculated Field entity. "
|
||||||
+ TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
|
+ TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/calculatedField", method = RequestMethod.POST)
|
@PostMapping("/calculatedField")
|
||||||
@ResponseBody
|
|
||||||
public CalculatedField saveCalculatedField(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "A JSON value representing the calculated field.")
|
public CalculatedField saveCalculatedField(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "A JSON value representing the calculated field.")
|
||||||
@RequestBody CalculatedField calculatedField) throws Exception {
|
@RequestBody CalculatedField calculatedField) throws Exception {
|
||||||
calculatedField.setTenantId(getTenantId());
|
calculatedField.setTenantId(getTenantId());
|
||||||
checkEntityId(calculatedField.getEntityId(), Operation.WRITE_CALCULATED_FIELD);
|
checkEntityId(calculatedField.getEntityId(), Operation.WRITE_CALCULATED_FIELD);
|
||||||
checkReferencedEntities(calculatedField.getConfiguration(), getCurrentUser());
|
checkReferencedEntities(calculatedField.getConfiguration());
|
||||||
return tbCalculatedFieldService.save(calculatedField, getCurrentUser());
|
return tbCalculatedFieldService.save(calculatedField, getCurrentUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,8 +144,7 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
notes = "Fetch the Calculated Field object based on the provided Calculated Field Id."
|
notes = "Fetch the Calculated Field object based on the provided Calculated Field Id."
|
||||||
)
|
)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/calculatedField/{calculatedFieldId}", method = RequestMethod.GET)
|
@GetMapping("/calculatedField/{calculatedFieldId}")
|
||||||
@ResponseBody
|
|
||||||
public CalculatedField getCalculatedFieldById(@Parameter @PathVariable(CALCULATED_FIELD_ID) String strCalculatedFieldId) throws ThingsboardException {
|
public CalculatedField getCalculatedFieldById(@Parameter @PathVariable(CALCULATED_FIELD_ID) String strCalculatedFieldId) throws ThingsboardException {
|
||||||
checkParameter(CALCULATED_FIELD_ID, strCalculatedFieldId);
|
checkParameter(CALCULATED_FIELD_ID, strCalculatedFieldId);
|
||||||
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(toUUID(strCalculatedFieldId));
|
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(toUUID(strCalculatedFieldId));
|
||||||
@ -162,8 +158,7 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
notes = "Fetch the Calculated Fields based on the provided Entity Id."
|
notes = "Fetch the Calculated Fields based on the provided Entity Id."
|
||||||
)
|
)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/{entityType}/{entityId}/calculatedFields", params = {"pageSize", "page"}, method = RequestMethod.GET)
|
@GetMapping(value = "/{entityType}/{entityId}/calculatedFields", params = {"pageSize", "page"})
|
||||||
@ResponseBody
|
|
||||||
public PageData<CalculatedField> getCalculatedFieldsByEntityId(
|
public PageData<CalculatedField> getCalculatedFieldsByEntityId(
|
||||||
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true, schema = @Schema(defaultValue = "DEVICE")) @PathVariable("entityType") String entityType,
|
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true, schema = @Schema(defaultValue = "DEVICE")) @PathVariable("entityType") String entityType,
|
||||||
@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @PathVariable("entityId") String entityIdStr,
|
@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @PathVariable("entityId") String entityIdStr,
|
||||||
@ -182,8 +177,8 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
@ApiOperation(value = "Delete Calculated Field (deleteCalculatedField)",
|
@ApiOperation(value = "Delete Calculated Field (deleteCalculatedField)",
|
||||||
notes = "Deletes the calculated field. Referencing non-existing Calculated Field Id will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
|
notes = "Deletes the calculated field. Referencing non-existing Calculated Field Id will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/calculatedField/{calculatedFieldId}", method = RequestMethod.DELETE)
|
@DeleteMapping("/calculatedField/{calculatedFieldId}")
|
||||||
@ResponseStatus(value = HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public void deleteCalculatedField(@PathVariable(CALCULATED_FIELD_ID) String strCalculatedFieldId) throws Exception {
|
public void deleteCalculatedField(@PathVariable(CALCULATED_FIELD_ID) String strCalculatedFieldId) throws Exception {
|
||||||
checkParameter(CALCULATED_FIELD_ID, strCalculatedFieldId);
|
checkParameter(CALCULATED_FIELD_ID, strCalculatedFieldId);
|
||||||
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(toUUID(strCalculatedFieldId));
|
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(toUUID(strCalculatedFieldId));
|
||||||
@ -196,8 +191,7 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
notes = "Gets latest calculated field debug event for specified calculated field id. " +
|
notes = "Gets latest calculated field debug event for specified calculated field id. " +
|
||||||
"Referencing non-existing calculated field id will cause an error. " + TENANT_AUTHORITY_PARAGRAPH)
|
"Referencing non-existing calculated field id will cause an error. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/calculatedField/{calculatedFieldId}/debug", method = RequestMethod.GET)
|
@GetMapping("/calculatedField/{calculatedFieldId}/debug")
|
||||||
@ResponseBody
|
|
||||||
public JsonNode getLatestCalculatedFieldDebugEvent(@Parameter @PathVariable(CALCULATED_FIELD_ID) String strCalculatedFieldId) throws ThingsboardException {
|
public JsonNode getLatestCalculatedFieldDebugEvent(@Parameter @PathVariable(CALCULATED_FIELD_ID) String strCalculatedFieldId) throws ThingsboardException {
|
||||||
checkParameter(CALCULATED_FIELD_ID, strCalculatedFieldId);
|
checkParameter(CALCULATED_FIELD_ID, strCalculatedFieldId);
|
||||||
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(toUUID(strCalculatedFieldId));
|
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(toUUID(strCalculatedFieldId));
|
||||||
@ -212,15 +206,13 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
@ApiOperation(value = "Test Script expression",
|
@ApiOperation(value = "Test Script expression",
|
||||||
notes = TEST_SCRIPT_EXPRESSION + TENANT_AUTHORITY_PARAGRAPH)
|
notes = TEST_SCRIPT_EXPRESSION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/calculatedField/testScript", method = RequestMethod.POST)
|
@PostMapping("/calculatedField/testScript")
|
||||||
@ResponseBody
|
|
||||||
public JsonNode testScript(
|
public JsonNode testScript(
|
||||||
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Test calculated field TBEL expression.")
|
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Test calculated field TBEL expression.")
|
||||||
@RequestBody JsonNode inputParams) {
|
@RequestBody JsonNode inputParams) {
|
||||||
String expression = inputParams.get("expression").asText();
|
String expression = inputParams.get("expression").asText();
|
||||||
Map<String, TbelCfArg> arguments = Objects.requireNonNullElse(
|
Map<String, TbelCfArg> arguments = Objects.requireNonNullElse(
|
||||||
JacksonUtil.convertValue(inputParams.get("arguments"), new TypeReference<>() {
|
JacksonUtil.convertValue(inputParams.get("arguments"), new TypeReference<>() {}),
|
||||||
}),
|
|
||||||
Collections.emptyMap()
|
Collections.emptyMap()
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -231,12 +223,13 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
String output = "";
|
String output = "";
|
||||||
String errorText = "";
|
String errorText = "";
|
||||||
|
|
||||||
|
CalculatedFieldTbelScriptEngine engine = null;
|
||||||
try {
|
try {
|
||||||
if (tbelInvokeService == null) {
|
if (tbelInvokeService == null) {
|
||||||
throw new IllegalArgumentException("TBEL script engine is disabled!");
|
throw new IllegalArgumentException("TBEL script engine is disabled!");
|
||||||
}
|
}
|
||||||
|
|
||||||
CalculatedFieldScriptEngine calculatedFieldScriptEngine = new CalculatedFieldTbelScriptEngine(
|
engine = new CalculatedFieldTbelScriptEngine(
|
||||||
getTenantId(),
|
getTenantId(),
|
||||||
tbelInvokeService,
|
tbelInvokeService,
|
||||||
expression,
|
expression,
|
||||||
@ -254,17 +247,20 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode json = calculatedFieldScriptEngine.executeJsonAsync(args).get(TIMEOUT, TimeUnit.SECONDS);
|
JsonNode json = engine.executeJsonAsync(args).get(TIMEOUT, TimeUnit.SECONDS);
|
||||||
output = JacksonUtil.toString(json);
|
output = JacksonUtil.toString(json);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error evaluating expression", e);
|
log.error("Error evaluating expression", e);
|
||||||
errorText = e.getMessage();
|
Throwable rootCause = ExceptionUtils.getRootCause(e);
|
||||||
|
errorText = ObjectUtils.firstNonNull(rootCause.getMessage(), e.getMessage(), e.getClass().getSimpleName());
|
||||||
|
} finally {
|
||||||
|
if (engine != null) {
|
||||||
|
engine.destroy();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ObjectNode result = JacksonUtil.newObjectNode();
|
return JacksonUtil.newObjectNode()
|
||||||
result.put("output", output);
|
.put("output", output)
|
||||||
result.put("error", errorText);
|
.put("error", errorText);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getLatestTimestamp(Map<String, TbelCfArg> arguments) {
|
private long getLatestTimestamp(Map<String, TbelCfArg> arguments) {
|
||||||
@ -281,7 +277,7 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
return lastUpdateTimestamp == -1 ? System.currentTimeMillis() : lastUpdateTimestamp;
|
return lastUpdateTimestamp == -1 ? System.currentTimeMillis() : lastUpdateTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <E extends HasId<I> & HasTenantId, I extends EntityId> void checkReferencedEntities(CalculatedFieldConfiguration calculatedFieldConfig, SecurityUser user) throws ThingsboardException {
|
private void checkReferencedEntities(CalculatedFieldConfiguration calculatedFieldConfig) throws ThingsboardException {
|
||||||
List<EntityId> referencedEntityIds = calculatedFieldConfig.getReferencedEntities();
|
List<EntityId> referencedEntityIds = calculatedFieldConfig.getReferencedEntities();
|
||||||
for (EntityId referencedEntityId : referencedEntityIds) {
|
for (EntityId referencedEntityId : referencedEntityIds) {
|
||||||
EntityType entityType = referencedEntityId.getEntityType();
|
EntityType entityType = referencedEntityId.getEntityType();
|
||||||
@ -290,8 +286,7 @@ public class CalculatedFieldController extends BaseController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case CUSTOMER, ASSET, DEVICE -> checkEntityId(referencedEntityId, Operation.READ);
|
case CUSTOMER, ASSET, DEVICE -> checkEntityId(referencedEntityId, Operation.READ);
|
||||||
default ->
|
default -> throw new IllegalArgumentException("Calculated fields do not support '" + entityType + "' for referenced entities.");
|
||||||
throw new IllegalArgumentException("Calculated fields do not support '" + entityType + "' for referenced entities.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.controller;
|
package org.thingsboard.server.controller;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
@ -23,16 +22,19 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
|||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.thingsboard.common.util.JacksonUtil;
|
import org.thingsboard.common.util.JacksonUtil;
|
||||||
@ -155,9 +157,8 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "Get Rule Chain (getRuleChainById)",
|
@ApiOperation(value = "Get Rule Chain (getRuleChainById)",
|
||||||
notes = "Fetch the Rule Chain object based on the provided Rule Chain Id. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Fetch the Rule Chain object based on the provided Rule Chain Id. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.GET)
|
@GetMapping("/ruleChain/{ruleChainId}")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain getRuleChainById(
|
public RuleChain getRuleChainById(
|
||||||
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
@ -169,9 +170,8 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Get Rule Chain output labels (getRuleChainOutputLabels)",
|
@ApiOperation(value = "Get Rule Chain output labels (getRuleChainOutputLabels)",
|
||||||
notes = "Fetch the unique labels for the \"output\" Rule Nodes that belong to the Rule Chain based on the provided Rule Chain Id. "
|
notes = "Fetch the unique labels for the \"output\" Rule Nodes that belong to the Rule Chain based on the provided Rule Chain Id. "
|
||||||
+ RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
+ RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/output/labels", method = RequestMethod.GET)
|
@GetMapping("/ruleChain/{ruleChainId}/output/labels")
|
||||||
@ResponseBody
|
|
||||||
public Set<String> getRuleChainOutputLabels(
|
public Set<String> getRuleChainOutputLabels(
|
||||||
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
@ -184,9 +184,8 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Get output labels usage (getRuleChainOutputLabelsUsage)",
|
@ApiOperation(value = "Get output labels usage (getRuleChainOutputLabelsUsage)",
|
||||||
notes = "Fetch the list of rule chains and the relation types (labels) they use to process output of the current rule chain based on the provided Rule Chain Id. "
|
notes = "Fetch the list of rule chains and the relation types (labels) they use to process output of the current rule chain based on the provided Rule Chain Id. "
|
||||||
+ RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
+ RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/output/labels/usage", method = RequestMethod.GET)
|
@GetMapping("/ruleChain/{ruleChainId}/output/labels/usage")
|
||||||
@ResponseBody
|
|
||||||
public List<RuleChainOutputLabelsUsage> getRuleChainOutputLabelsUsage(
|
public List<RuleChainOutputLabelsUsage> getRuleChainOutputLabelsUsage(
|
||||||
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
@ -198,9 +197,8 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "Get Rule Chain (getRuleChainById)",
|
@ApiOperation(value = "Get Rule Chain (getRuleChainById)",
|
||||||
notes = "Fetch the Rule Chain Metadata object based on the provided Rule Chain Id. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Fetch the Rule Chain Metadata object based on the provided Rule Chain Id. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/metadata", method = RequestMethod.GET)
|
@GetMapping("/ruleChain/{ruleChainId}/metadata")
|
||||||
@ResponseBody
|
|
||||||
public RuleChainMetaData getRuleChainMetaData(
|
public RuleChainMetaData getRuleChainMetaData(
|
||||||
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
@ -218,9 +216,8 @@ public class RuleChainController extends BaseController {
|
|||||||
"\n\n" + RULE_CHAIN_DESCRIPTION +
|
"\n\n" + RULE_CHAIN_DESCRIPTION +
|
||||||
"Remove 'id', 'tenantId' from the request body example (below) to create new Rule Chain entity." +
|
"Remove 'id', 'tenantId' from the request body example (below) to create new Rule Chain entity." +
|
||||||
TENANT_AUTHORITY_PARAGRAPH)
|
TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain", method = RequestMethod.POST)
|
@PostMapping("/ruleChain")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain saveRuleChain(
|
public RuleChain saveRuleChain(
|
||||||
@Parameter(description = "A JSON value representing the rule chain.")
|
@Parameter(description = "A JSON value representing the rule chain.")
|
||||||
@RequestBody RuleChain ruleChain) throws Exception {
|
@RequestBody RuleChain ruleChain) throws Exception {
|
||||||
@ -232,9 +229,8 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Create Default Rule Chain",
|
@ApiOperation(value = "Create Default Rule Chain",
|
||||||
notes = "Create rule chain from template, based on the specified name in the request. " +
|
notes = "Create rule chain from template, based on the specified name in the request. " +
|
||||||
"Creates the rule chain based on the template that is used to create root rule chain. " + TENANT_AUTHORITY_PARAGRAPH)
|
"Creates the rule chain based on the template that is used to create root rule chain. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/device/default", method = RequestMethod.POST)
|
@PostMapping("/ruleChain/device/default")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain saveRuleChain(
|
public RuleChain saveRuleChain(
|
||||||
@Parameter(description = "A JSON value representing the request.")
|
@Parameter(description = "A JSON value representing the request.")
|
||||||
@RequestBody DefaultRuleChainCreateRequest request) throws Exception {
|
@RequestBody DefaultRuleChainCreateRequest request) throws Exception {
|
||||||
@ -245,9 +241,8 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "Set Root Rule Chain (setRootRuleChain)",
|
@ApiOperation(value = "Set Root Rule Chain (setRootRuleChain)",
|
||||||
notes = "Makes the rule chain to be root rule chain. Updates previous root rule chain as well. " + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Makes the rule chain to be root rule chain. Updates previous root rule chain as well. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/root", method = RequestMethod.POST)
|
@PostMapping("/ruleChain/{ruleChainId}/root")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain setRootRuleChain(
|
public RuleChain setRootRuleChain(
|
||||||
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
@ -259,9 +254,8 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "Update Rule Chain Metadata",
|
@ApiOperation(value = "Update Rule Chain Metadata",
|
||||||
notes = "Updates the rule chain metadata. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Updates the rule chain metadata. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/metadata", method = RequestMethod.POST)
|
@PostMapping("/ruleChain/metadata")
|
||||||
@ResponseBody
|
|
||||||
public RuleChainMetaData saveRuleChainMetaData(
|
public RuleChainMetaData saveRuleChainMetaData(
|
||||||
@Parameter(description = "A JSON value representing the rule chain metadata.")
|
@Parameter(description = "A JSON value representing the rule chain metadata.")
|
||||||
@RequestBody RuleChainMetaData ruleChainMetaData,
|
@RequestBody RuleChainMetaData ruleChainMetaData,
|
||||||
@ -284,8 +278,7 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Get Rule Chains (getRuleChains)",
|
@ApiOperation(value = "Get Rule Chains (getRuleChains)",
|
||||||
notes = "Returns a page of Rule Chains owned by tenant. " + RULE_CHAIN_DESCRIPTION + PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Returns a page of Rule Chains owned by tenant. " + RULE_CHAIN_DESCRIPTION + PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChains", params = {"pageSize", "page"}, method = RequestMethod.GET)
|
@GetMapping(value = "/ruleChains", params = {"pageSize", "page"})
|
||||||
@ResponseBody
|
|
||||||
public PageData<RuleChain> getRuleChains(
|
public PageData<RuleChain> getRuleChains(
|
||||||
@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true)
|
@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true)
|
||||||
@RequestParam int pageSize,
|
@RequestParam int pageSize,
|
||||||
@ -302,7 +295,7 @@ public class RuleChainController extends BaseController {
|
|||||||
TenantId tenantId = getCurrentUser().getTenantId();
|
TenantId tenantId = getCurrentUser().getTenantId();
|
||||||
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
|
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
|
||||||
RuleChainType type = RuleChainType.CORE;
|
RuleChainType type = RuleChainType.CORE;
|
||||||
if (typeStr != null && typeStr.trim().length() > 0) {
|
if (StringUtils.isNotBlank(typeStr)) {
|
||||||
type = RuleChainType.valueOf(typeStr);
|
type = RuleChainType.valueOf(typeStr);
|
||||||
}
|
}
|
||||||
return checkNotNull(ruleChainService.findTenantRuleChainsByType(tenantId, type, pageLink));
|
return checkNotNull(ruleChainService.findTenantRuleChainsByType(tenantId, type, pageLink));
|
||||||
@ -311,9 +304,9 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Delete rule chain (deleteRuleChain)",
|
@ApiOperation(value = "Delete rule chain (deleteRuleChain)",
|
||||||
notes = "Deletes the rule chain. Referencing non-existing rule chain Id will cause an error. " +
|
notes = "Deletes the rule chain. Referencing non-existing rule chain Id will cause an error. " +
|
||||||
"Referencing rule chain that is used in the device profiles will cause an error." + TENANT_AUTHORITY_PARAGRAPH)
|
"Referencing rule chain that is used in the device profiles will cause an error." + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
|
@DeleteMapping("/ruleChain/{ruleChainId}")
|
||||||
@ResponseStatus(value = HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public void deleteRuleChain(
|
public void deleteRuleChain(
|
||||||
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
@ -326,9 +319,8 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Get latest input message (getLatestRuleNodeDebugInput)",
|
@ApiOperation(value = "Get latest input message (getLatestRuleNodeDebugInput)",
|
||||||
notes = "Gets the input message from the debug events for specified Rule Chain Id. " +
|
notes = "Gets the input message from the debug events for specified Rule Chain Id. " +
|
||||||
"Referencing non-existing rule chain Id will cause an error. " + TENANT_AUTHORITY_PARAGRAPH)
|
"Referencing non-existing rule chain Id will cause an error. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleNode/{ruleNodeId}/debugIn", method = RequestMethod.GET)
|
@GetMapping("/ruleNode/{ruleNodeId}/debugIn")
|
||||||
@ResponseBody
|
|
||||||
public JsonNode getLatestRuleNodeDebugInput(
|
public JsonNode getLatestRuleNodeDebugInput(
|
||||||
@Parameter(description = RULE_NODE_ID_PARAM_DESCRIPTION)
|
@Parameter(description = RULE_NODE_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_NODE_ID) String strRuleNodeId) throws ThingsboardException {
|
@PathVariable(RULE_NODE_ID) String strRuleNodeId) throws ThingsboardException {
|
||||||
@ -343,8 +335,7 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Is TBEL script executor enabled",
|
@ApiOperation(value = "Is TBEL script executor enabled",
|
||||||
notes = "Returns 'True' if the TBEL script execution is enabled" + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Returns 'True' if the TBEL script execution is enabled" + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/tbelEnabled", method = RequestMethod.GET)
|
@GetMapping("/ruleChain/tbelEnabled")
|
||||||
@ResponseBody
|
|
||||||
public Boolean isTbelEnabled() {
|
public Boolean isTbelEnabled() {
|
||||||
return tbelEnabled;
|
return tbelEnabled;
|
||||||
}
|
}
|
||||||
@ -352,13 +343,12 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Test Script function",
|
@ApiOperation(value = "Test Script function",
|
||||||
notes = TEST_SCRIPT_FUNCTION + TENANT_AUTHORITY_PARAGRAPH)
|
notes = TEST_SCRIPT_FUNCTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/testScript", method = RequestMethod.POST)
|
@PostMapping("/ruleChain/testScript")
|
||||||
@ResponseBody
|
|
||||||
public JsonNode testScript(
|
public JsonNode testScript(
|
||||||
@Parameter(description = "Script language: JS or TBEL")
|
@Parameter(description = "Script language: JS or TBEL")
|
||||||
@RequestParam(required = false) ScriptLanguage scriptLang,
|
@RequestParam(required = false) ScriptLanguage scriptLang,
|
||||||
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Test JS request. See API call description above.")
|
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Test JS request. See API call description above.")
|
||||||
@RequestBody JsonNode inputParams) throws ThingsboardException, JsonProcessingException {
|
@RequestBody JsonNode inputParams) {
|
||||||
String script = inputParams.get("script").asText();
|
String script = inputParams.get("script").asText();
|
||||||
String scriptType = inputParams.get("scriptType").asText();
|
String scriptType = inputParams.get("scriptType").asText();
|
||||||
JsonNode argNamesJson = inputParams.get("argNames");
|
JsonNode argNamesJson = inputParams.get("argNames");
|
||||||
@ -366,8 +356,7 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
String data = inputParams.get("msg").asText();
|
String data = inputParams.get("msg").asText();
|
||||||
JsonNode metadataJson = inputParams.get("metadata");
|
JsonNode metadataJson = inputParams.get("metadata");
|
||||||
Map<String, String> metadata = JacksonUtil.convertValue(metadataJson, new TypeReference<Map<String, String>>() {
|
Map<String, String> metadata = JacksonUtil.convertValue(metadataJson, new TypeReference<>() {});
|
||||||
});
|
|
||||||
String msgType = inputParams.get("msgType").asText();
|
String msgType = inputParams.get("msgType").asText();
|
||||||
String output = "";
|
String output = "";
|
||||||
String errorText = "";
|
String errorText = "";
|
||||||
@ -384,55 +373,40 @@ public class RuleChainController extends BaseController {
|
|||||||
}
|
}
|
||||||
engine = new RuleNodeTbelScriptEngine(getTenantId(), tbelInvokeService, script, argNames);
|
engine = new RuleNodeTbelScriptEngine(getTenantId(), tbelInvokeService, script, argNames);
|
||||||
}
|
}
|
||||||
TbMsg inMsg = TbMsg.newMsg()
|
|
||||||
|
var inMsg = TbMsg.newMsg()
|
||||||
.type(msgType)
|
.type(msgType)
|
||||||
.copyMetaData(new TbMsgMetaData(metadata))
|
.copyMetaData(new TbMsgMetaData(metadata))
|
||||||
.dataType(TbMsgDataType.JSON)
|
.dataType(TbMsgDataType.JSON)
|
||||||
.data(data)
|
.data(data)
|
||||||
.build();
|
.build();
|
||||||
switch (scriptType) {
|
|
||||||
case "update":
|
output = switch (scriptType) {
|
||||||
output = msgToOutput(engine.executeUpdateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
case "update" -> msgToOutput(engine.executeUpdateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
||||||
break;
|
case "generate" -> msgToOutput(engine.executeGenerateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
||||||
case "generate":
|
case "filter" -> Boolean.toString(engine.executeFilterAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
||||||
output = msgToOutput(engine.executeGenerateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
case "switch" -> JacksonUtil.toString(engine.executeSwitchAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
||||||
break;
|
case "json" -> JacksonUtil.toString(engine.executeJsonAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
|
||||||
case "filter":
|
case "string" -> engine.executeToStringAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
|
||||||
boolean result = engine.executeFilterAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
|
default -> throw new IllegalArgumentException("Unsupported script type: " + scriptType);
|
||||||
output = Boolean.toString(result);
|
};
|
||||||
break;
|
|
||||||
case "switch":
|
|
||||||
Set<String> states = engine.executeSwitchAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
|
|
||||||
output = JacksonUtil.toString(states);
|
|
||||||
break;
|
|
||||||
case "json":
|
|
||||||
JsonNode json = engine.executeJsonAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
|
|
||||||
output = JacksonUtil.toString(json);
|
|
||||||
break;
|
|
||||||
case "string":
|
|
||||||
output = engine.executeToStringAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unsupported script type: " + scriptType);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error evaluating JS function", e);
|
log.error("Error evaluating JS function", e);
|
||||||
errorText = e.getMessage();
|
Throwable rootCause = ExceptionUtils.getRootCause(e);
|
||||||
|
errorText = ObjectUtils.firstNonNull(rootCause.getMessage(), e.getMessage(), e.getClass().getSimpleName());
|
||||||
} finally {
|
} finally {
|
||||||
if (engine != null) {
|
if (engine != null) {
|
||||||
engine.destroy();
|
engine.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ObjectNode result = JacksonUtil.newObjectNode();
|
return JacksonUtil.newObjectNode()
|
||||||
result.put("output", output);
|
.put("output", output)
|
||||||
result.put("error", errorText);
|
.put("error", errorText);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "Export Rule Chains", notes = "Exports all tenant rule chains as one JSON." + TENANT_AUTHORITY_PARAGRAPH)
|
@ApiOperation(value = "Export Rule Chains", notes = "Exports all tenant rule chains as one JSON." + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChains/export", params = {"limit"}, method = RequestMethod.GET)
|
@GetMapping(value = "/ruleChains/export", params = {"limit"})
|
||||||
@ResponseBody
|
|
||||||
public RuleChainData exportRuleChains(
|
public RuleChainData exportRuleChains(
|
||||||
@Parameter(description = "A limit of rule chains to export.", required = true)
|
@Parameter(description = "A limit of rule chains to export.", required = true)
|
||||||
@RequestParam("limit") int limit) throws ThingsboardException {
|
@RequestParam("limit") int limit) throws ThingsboardException {
|
||||||
@ -443,8 +417,7 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "Import Rule Chains", notes = "Imports all tenant rule chains as one JSON." + TENANT_AUTHORITY_PARAGRAPH)
|
@ApiOperation(value = "Import Rule Chains", notes = "Imports all tenant rule chains as one JSON." + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChains/import", method = RequestMethod.POST)
|
@PostMapping("/ruleChains/import")
|
||||||
@ResponseBody
|
|
||||||
public List<RuleChainImportResult> importRuleChains(
|
public List<RuleChainImportResult> importRuleChains(
|
||||||
@Parameter(description = "A JSON value representing the rule chains.")
|
@Parameter(description = "A JSON value representing the rule chains.")
|
||||||
@RequestBody RuleChainData ruleChainData,
|
@RequestBody RuleChainData ruleChainData,
|
||||||
@ -454,12 +427,12 @@ public class RuleChainController extends BaseController {
|
|||||||
return ruleChainService.importTenantRuleChains(tenantId, ruleChainData, overwrite, tbRuleChainService::updateRuleNodeConfiguration);
|
return ruleChainService.importTenantRuleChains(tenantId, ruleChainData, overwrite, tbRuleChainService::updateRuleNodeConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String msgToOutput(TbMsg msg) throws Exception {
|
private String msgToOutput(TbMsg msg) {
|
||||||
JsonNode resultNode = convertMsgToOut(msg);
|
JsonNode resultNode = convertMsgToOut(msg);
|
||||||
return JacksonUtil.toString(resultNode);
|
return JacksonUtil.toString(resultNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String msgToOutput(List<TbMsg> msgs) throws Exception {
|
private String msgToOutput(List<TbMsg> msgs) {
|
||||||
JsonNode resultNode;
|
JsonNode resultNode;
|
||||||
if (msgs.size() > 1) {
|
if (msgs.size() > 1) {
|
||||||
resultNode = JacksonUtil.newArrayNode();
|
resultNode = JacksonUtil.newArrayNode();
|
||||||
@ -473,7 +446,7 @@ public class RuleChainController extends BaseController {
|
|||||||
return JacksonUtil.toString(resultNode);
|
return JacksonUtil.toString(resultNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonNode convertMsgToOut(TbMsg msg) throws Exception {
|
private JsonNode convertMsgToOut(TbMsg msg) {
|
||||||
ObjectNode msgData = JacksonUtil.newObjectNode();
|
ObjectNode msgData = JacksonUtil.newObjectNode();
|
||||||
if (!StringUtils.isEmpty(msg.getData())) {
|
if (!StringUtils.isEmpty(msg.getData())) {
|
||||||
msgData.set("msg", JacksonUtil.toJsonNode(msg.getData()));
|
msgData.set("msg", JacksonUtil.toJsonNode(msg.getData()));
|
||||||
@ -492,8 +465,7 @@ public class RuleChainController extends BaseController {
|
|||||||
"Third, once rule chain will be delivered to edge service, it's going to start processing messages locally. " +
|
"Third, once rule chain will be delivered to edge service, it's going to start processing messages locally. " +
|
||||||
"\n\nOnly rule chain with type 'EDGE' can be assigned to edge." + TENANT_AUTHORITY_PARAGRAPH)
|
"\n\nOnly rule chain with type 'EDGE' can be assigned to edge." + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/edge/{edgeId}/ruleChain/{ruleChainId}", method = RequestMethod.POST)
|
@PostMapping("/edge/{edgeId}/ruleChain/{ruleChainId}")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain assignRuleChainToEdge(@PathVariable("edgeId") String strEdgeId,
|
public RuleChain assignRuleChainToEdge(@PathVariable("edgeId") String strEdgeId,
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
checkParameter("edgeId", strEdgeId);
|
checkParameter("edgeId", strEdgeId);
|
||||||
@ -514,8 +486,7 @@ public class RuleChainController extends BaseController {
|
|||||||
EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION +
|
EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION +
|
||||||
"Third, once 'unassign' command will be delivered to edge service, it's going to remove rule chain locally." + TENANT_AUTHORITY_PARAGRAPH)
|
"Third, once 'unassign' command will be delivered to edge service, it's going to remove rule chain locally." + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/edge/{edgeId}/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
|
@DeleteMapping("/edge/{edgeId}/ruleChain/{ruleChainId}")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain unassignRuleChainFromEdge(@PathVariable("edgeId") String strEdgeId,
|
public RuleChain unassignRuleChainFromEdge(@PathVariable("edgeId") String strEdgeId,
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
checkParameter("edgeId", strEdgeId);
|
checkParameter("edgeId", strEdgeId);
|
||||||
@ -530,9 +501,8 @@ public class RuleChainController extends BaseController {
|
|||||||
|
|
||||||
@ApiOperation(value = "Get Edge Rule Chains (getEdgeRuleChains)",
|
@ApiOperation(value = "Get Edge Rule Chains (getEdgeRuleChains)",
|
||||||
notes = "Returns a page of Rule Chains assigned to the specified edge. " + RULE_CHAIN_DESCRIPTION + PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Returns a page of Rule Chains assigned to the specified edge. " + RULE_CHAIN_DESCRIPTION + PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/edge/{edgeId}/ruleChains", params = {"pageSize", "page"}, method = RequestMethod.GET)
|
@GetMapping(value = "/edge/{edgeId}/ruleChains", params = {"pageSize", "page"})
|
||||||
@ResponseBody
|
|
||||||
public PageData<RuleChain> getEdgeRuleChains(
|
public PageData<RuleChain> getEdgeRuleChains(
|
||||||
@Parameter(description = EDGE_ID_PARAM_DESCRIPTION, required = true)
|
@Parameter(description = EDGE_ID_PARAM_DESCRIPTION, required = true)
|
||||||
@PathVariable(EDGE_ID) String strEdgeId,
|
@PathVariable(EDGE_ID) String strEdgeId,
|
||||||
@ -557,9 +527,8 @@ public class RuleChainController extends BaseController {
|
|||||||
@ApiOperation(value = "Set Edge Template Root Rule Chain (setEdgeTemplateRootRuleChain)",
|
@ApiOperation(value = "Set Edge Template Root Rule Chain (setEdgeTemplateRootRuleChain)",
|
||||||
notes = "Makes the rule chain to be root rule chain for any new edge that will be created. " +
|
notes = "Makes the rule chain to be root rule chain for any new edge that will be created. " +
|
||||||
"Does not update root rule chain for already created edges. " + TENANT_AUTHORITY_PARAGRAPH)
|
"Does not update root rule chain for already created edges. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/edgeTemplateRoot", method = RequestMethod.POST)
|
@PostMapping("/ruleChain/{ruleChainId}/edgeTemplateRoot")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain setEdgeTemplateRootRuleChain(@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
public RuleChain setEdgeTemplateRootRuleChain(@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
||||||
@ -572,8 +541,7 @@ public class RuleChainController extends BaseController {
|
|||||||
notes = "Makes the rule chain to be automatically assigned for any new edge that will be created. " +
|
notes = "Makes the rule chain to be automatically assigned for any new edge that will be created. " +
|
||||||
"Does not assign this rule chain for already created edges. " + TENANT_AUTHORITY_PARAGRAPH)
|
"Does not assign this rule chain for already created edges. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/autoAssignToEdge", method = RequestMethod.POST)
|
@PostMapping("/ruleChain/{ruleChainId}/autoAssignToEdge")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain setAutoAssignToEdgeRuleChain(@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
public RuleChain setAutoAssignToEdgeRuleChain(@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
||||||
@ -586,8 +554,7 @@ public class RuleChainController extends BaseController {
|
|||||||
notes = "Removes the rule chain from the list of rule chains that are going to be automatically assigned for any new edge that will be created. " +
|
notes = "Removes the rule chain from the list of rule chains that are going to be automatically assigned for any new edge that will be created. " +
|
||||||
"Does not unassign this rule chain for already assigned edges. " + TENANT_AUTHORITY_PARAGRAPH)
|
"Does not unassign this rule chain for already assigned edges. " + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/{ruleChainId}/autoAssignToEdge", method = RequestMethod.DELETE)
|
@DeleteMapping("/ruleChain/{ruleChainId}/autoAssignToEdge")
|
||||||
@ResponseBody
|
|
||||||
public RuleChain unsetAutoAssignToEdgeRuleChain(@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
public RuleChain unsetAutoAssignToEdgeRuleChain(@Parameter(description = RULE_CHAIN_ID_PARAM_DESCRIPTION)
|
||||||
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
|
||||||
checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
checkParameter(RULE_CHAIN_ID, strRuleChainId);
|
||||||
@ -599,9 +566,8 @@ public class RuleChainController extends BaseController {
|
|||||||
// TODO: @voba refactor this - add new config to edge rule chain to set it as auto-assign
|
// TODO: @voba refactor this - add new config to edge rule chain to set it as auto-assign
|
||||||
@ApiOperation(value = "Get Auto Assign To Edge Rule Chains (getAutoAssignToEdgeRuleChains)",
|
@ApiOperation(value = "Get Auto Assign To Edge Rule Chains (getAutoAssignToEdgeRuleChains)",
|
||||||
notes = "Returns a list of Rule Chains that will be assigned to a newly created edge. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
notes = "Returns a list of Rule Chains that will be assigned to a newly created edge. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
|
||||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
|
||||||
@RequestMapping(value = "/ruleChain/autoAssignToEdgeRuleChains", method = RequestMethod.GET)
|
@GetMapping("/ruleChain/autoAssignToEdgeRuleChains")
|
||||||
@ResponseBody
|
|
||||||
public List<RuleChain> getAutoAssignToEdgeRuleChains() throws ThingsboardException {
|
public List<RuleChain> getAutoAssignToEdgeRuleChains() throws ThingsboardException {
|
||||||
TenantId tenantId = getCurrentUser().getTenantId();
|
TenantId tenantId = getCurrentUser().getTenantId();
|
||||||
List<RuleChain> result = new ArrayList<>();
|
List<RuleChain> result = new ArrayList<>();
|
||||||
@ -612,4 +578,5 @@ public class RuleChainController extends BaseController {
|
|||||||
}
|
}
|
||||||
return checkNotNull(result);
|
return checkNotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user