Commit b0eb57bba24ddf2056fa876ddf1554dc334d2779

Authored by Igor Kulikov
1 parent 2f89b6d0

Add Rule Chain Controller

@@ -38,6 +38,7 @@ import org.thingsboard.server.common.data.page.TimePageLink; @@ -38,6 +38,7 @@ import org.thingsboard.server.common.data.page.TimePageLink;
38 import org.thingsboard.server.common.data.plugin.ComponentDescriptor; 38 import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
39 import org.thingsboard.server.common.data.plugin.ComponentType; 39 import org.thingsboard.server.common.data.plugin.ComponentType;
40 import org.thingsboard.server.common.data.plugin.PluginMetaData; 40 import org.thingsboard.server.common.data.plugin.PluginMetaData;
  41 +import org.thingsboard.server.common.data.rule.RuleChain;
41 import org.thingsboard.server.common.data.rule.RuleMetaData; 42 import org.thingsboard.server.common.data.rule.RuleMetaData;
42 import org.thingsboard.server.common.data.security.Authority; 43 import org.thingsboard.server.common.data.security.Authority;
43 import org.thingsboard.server.common.data.widget.WidgetType; 44 import org.thingsboard.server.common.data.widget.WidgetType;
@@ -54,6 +55,7 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException; @@ -54,6 +55,7 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException;
54 import org.thingsboard.server.dao.model.ModelConstants; 55 import org.thingsboard.server.dao.model.ModelConstants;
55 import org.thingsboard.server.dao.plugin.PluginService; 56 import org.thingsboard.server.dao.plugin.PluginService;
56 import org.thingsboard.server.dao.relation.RelationService; 57 import org.thingsboard.server.dao.relation.RelationService;
  58 +import org.thingsboard.server.dao.rule.RuleChainService;
57 import org.thingsboard.server.dao.rule.RuleService; 59 import org.thingsboard.server.dao.rule.RuleService;
58 import org.thingsboard.server.dao.user.UserService; 60 import org.thingsboard.server.dao.user.UserService;
59 import org.thingsboard.server.dao.widget.WidgetTypeService; 61 import org.thingsboard.server.dao.widget.WidgetTypeService;
@@ -119,6 +121,9 @@ public abstract class BaseController { @@ -119,6 +121,9 @@ public abstract class BaseController {
119 protected PluginService pluginService; 121 protected PluginService pluginService;
120 122
121 @Autowired 123 @Autowired
  124 + protected RuleChainService ruleChainService;
  125 +
  126 + @Autowired
122 protected ActorService actorService; 127 protected ActorService actorService;
123 128
124 @Autowired 129 @Autowired
@@ -295,6 +300,9 @@ public abstract class BaseController { @@ -295,6 +300,9 @@ public abstract class BaseController {
295 case RULE: 300 case RULE:
296 checkRule(new RuleId(entityId.getId())); 301 checkRule(new RuleId(entityId.getId()));
297 return; 302 return;
  303 + case RULE_CHAIN:
  304 + checkRuleChain(new RuleChainId(entityId.getId()));
  305 + return;
298 case ASSET: 306 case ASSET:
299 checkAsset(assetService.findAssetById(new AssetId(entityId.getId()))); 307 checkAsset(assetService.findAssetById(new AssetId(entityId.getId())));
300 return; 308 return;
@@ -526,6 +534,28 @@ public abstract class BaseController { @@ -526,6 +534,28 @@ public abstract class BaseController {
526 return rule; 534 return rule;
527 } 535 }
528 536
  537 + protected RuleChain checkRuleChain(RuleChainId ruleChainId) throws ThingsboardException {
  538 + checkNotNull(ruleChainId);
  539 + return checkRuleChain(ruleChainService.findRuleChainById(ruleChainId));
  540 + }
  541 +
  542 + protected RuleChain checkRuleChain(RuleChain ruleChain) throws ThingsboardException {
  543 + checkNotNull(ruleChain);
  544 + SecurityUser authUser = getCurrentUser();
  545 + TenantId tenantId = ruleChain.getTenantId();
  546 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  547 + if (authUser.getAuthority() != Authority.SYS_ADMIN) {
  548 + if (authUser.getTenantId() == null ||
  549 + !tenantId.getId().equals(ModelConstants.NULL_UUID) && !authUser.getTenantId().equals(tenantId)) {
  550 + throw new ThingsboardException(YOU_DON_T_HAVE_PERMISSION_TO_PERFORM_THIS_OPERATION,
  551 + ThingsboardErrorCode.PERMISSION_DENIED);
  552 +
  553 + }
  554 + }
  555 + return ruleChain;
  556 + }
  557 +
  558 +
529 protected String constructBaseUrl(HttpServletRequest request) { 559 protected String constructBaseUrl(HttpServletRequest request) {
530 String scheme = request.getScheme(); 560 String scheme = request.getScheme();
531 if (request.getHeader("x-forwarded-proto") != null) { 561 if (request.getHeader("x-forwarded-proto") != null) {
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.controller;
  17 +
  18 +import org.springframework.http.HttpStatus;
  19 +import org.springframework.security.access.prepost.PreAuthorize;
  20 +import org.springframework.web.bind.annotation.*;
  21 +import org.thingsboard.server.common.data.EntityType;
  22 +import org.thingsboard.server.common.data.audit.ActionType;
  23 +import org.thingsboard.server.common.data.id.PluginId;
  24 +import org.thingsboard.server.common.data.id.RuleChainId;
  25 +import org.thingsboard.server.common.data.id.TenantId;
  26 +import org.thingsboard.server.common.data.page.TextPageData;
  27 +import org.thingsboard.server.common.data.page.TextPageLink;
  28 +import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
  29 +import org.thingsboard.server.common.data.plugin.PluginMetaData;
  30 +import org.thingsboard.server.common.data.rule.RuleChain;
  31 +import org.thingsboard.server.common.data.security.Authority;
  32 +import org.thingsboard.server.dao.model.ModelConstants;
  33 +import org.thingsboard.server.exception.ThingsboardException;
  34 +
  35 +import java.util.List;
  36 +
  37 +@RestController
  38 +@RequestMapping("/api")
  39 +public class RuleChainController extends BaseController {
  40 +
  41 + public static final String RULE_CHAIN_ID = "ruleChainId";
  42 +
  43 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
  44 + @RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.GET)
  45 + @ResponseBody
  46 + public RuleChain getRuleChainById(@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
  47 + checkParameter(RULE_CHAIN_ID, strRuleChainId);
  48 + try {
  49 + RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
  50 + return checkRuleChain(ruleChainId);
  51 + } catch (Exception e) {
  52 + throw handleException(e);
  53 + }
  54 + }
  55 +
  56 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
  57 + @RequestMapping(value = "/ruleChain", method = RequestMethod.POST)
  58 + @ResponseBody
  59 + public RuleChain saveRuleChain(@RequestBody RuleChain ruleChain) throws ThingsboardException {
  60 + try {
  61 + boolean created = ruleChain.getId() == null;
  62 + ruleChain.setTenantId(getCurrentUser().getTenantId());
  63 + RuleChain savedRuleChain = checkNotNull(ruleChainService.saveRuleChain(ruleChain));
  64 +
  65 + logEntityAction(savedRuleChain.getId(), savedRuleChain,
  66 + null,
  67 + created ? ActionType.ADDED : ActionType.UPDATED, null);
  68 +
  69 + return savedRuleChain;
  70 + } catch (Exception e) {
  71 +
  72 + logEntityAction(emptyId(EntityType.RULE_CHAIN), ruleChain,
  73 + null, ruleChain.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
  74 +
  75 + throw handleException(e);
  76 + }
  77 + }
  78 +
  79 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
  80 + @RequestMapping(value = "/ruleChains", params = {"limit"}, method = RequestMethod.GET)
  81 + @ResponseBody
  82 + public TextPageData<RuleChain> getRuleChains(
  83 + @RequestParam int limit,
  84 + @RequestParam(required = false) String textSearch,
  85 + @RequestParam(required = false) String idOffset,
  86 + @RequestParam(required = false) String textOffset) throws ThingsboardException {
  87 + try {
  88 + TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
  89 + if (getCurrentUser().getAuthority() == Authority.SYS_ADMIN) {
  90 + return checkNotNull(ruleChainService.findSystemRuleChains(pageLink));
  91 + } else {
  92 + TenantId tenantId = getCurrentUser().getTenantId();
  93 + TextPageData<RuleChain> ruleChainsData = checkNotNull(ruleChainService.findAllTenantRuleChainsByTenantIdAndPageLink(tenantId, pageLink));
  94 + List<RuleChain> ruleChains = ruleChainsData.getData();
  95 + ruleChains.stream()
  96 + .filter(ruleChain -> ruleChain.getTenantId().getId().equals(ModelConstants.NULL_UUID))
  97 + .forEach(ruleChain -> ruleChain.setConfiguration(null));
  98 + return ruleChainsData;
  99 + }
  100 + } catch (Exception e) {
  101 + throw handleException(e);
  102 + }
  103 + }
  104 +
  105 + @PreAuthorize("hasAuthority('SYS_ADMIN')")
  106 + @RequestMapping(value = "/system/ruleChains", params = {"limit"}, method = RequestMethod.GET)
  107 + @ResponseBody
  108 + public TextPageData<RuleChain> getSystemRuleChains(
  109 + @RequestParam int limit,
  110 + @RequestParam(required = false) String textSearch,
  111 + @RequestParam(required = false) String idOffset,
  112 + @RequestParam(required = false) String textOffset) throws ThingsboardException {
  113 + try {
  114 + TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
  115 + return checkNotNull(ruleChainService.findSystemRuleChains(pageLink));
  116 + } catch (Exception e) {
  117 + throw handleException(e);
  118 + }
  119 + }
  120 +
  121 + @PreAuthorize("hasAuthority('TENANT_ADMIN')")
  122 + @RequestMapping(value = "/tenant/ruleChains", params = {"limit"}, method = RequestMethod.GET)
  123 + @ResponseBody
  124 + public TextPageData<RuleChain> getTenantRuleChains(
  125 + @RequestParam int limit,
  126 + @RequestParam(required = false) String textSearch,
  127 + @RequestParam(required = false) String idOffset,
  128 + @RequestParam(required = false) String textOffset) throws ThingsboardException {
  129 + try {
  130 + TenantId tenantId = getCurrentUser().getTenantId();
  131 + TextPageLink pageLink = createPageLink(limit, textSearch, idOffset, textOffset);
  132 + return checkNotNull(ruleChainService.findTenantRuleChains(tenantId, pageLink));
  133 + } catch (Exception e) {
  134 + throw handleException(e);
  135 + }
  136 + }
  137 +
  138 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
  139 + @RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
  140 + @ResponseStatus(value = HttpStatus.OK)
  141 + public void deleteRuleChain(@PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
  142 + checkParameter(RULE_CHAIN_ID, strRuleChainId);
  143 + try {
  144 + RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
  145 + RuleChain ruleChain = checkRuleChain(ruleChainId);
  146 + ruleChainService.deleteRuleChainById(ruleChainId);
  147 +
  148 + logEntityAction(ruleChainId, ruleChain,
  149 + null,
  150 + ActionType.DELETED, null, strRuleChainId);
  151 +
  152 + } catch (Exception e) {
  153 + logEntityAction(emptyId(EntityType.RULE_CHAIN),
  154 + null,
  155 + null,
  156 + ActionType.DELETED, e, strRuleChainId);
  157 + throw handleException(e);
  158 + }
  159 + }
  160 +
  161 +}
@@ -327,6 +327,7 @@ audit_log: @@ -327,6 +327,7 @@ audit_log:
327 "user": "${AUDIT_LOG_MASK_USER:W}" 327 "user": "${AUDIT_LOG_MASK_USER:W}"
328 "rule": "${AUDIT_LOG_MASK_RULE:W}" 328 "rule": "${AUDIT_LOG_MASK_RULE:W}"
329 "plugin": "${AUDIT_LOG_MASK_PLUGIN:W}" 329 "plugin": "${AUDIT_LOG_MASK_PLUGIN:W}"
  330 + "rule_chain": "${AUDIT_LOG_MASK_RULE_CHAIN:W}"
330 sink: 331 sink:
331 # Type of external sink. possible options: none, elasticsearch 332 # Type of external sink. possible options: none, elasticsearch
332 type: "${AUDIT_LOG_SINK_TYPE:none}" 333 type: "${AUDIT_LOG_SINK_TYPE:none}"
@@ -16,13 +16,11 @@ @@ -16,13 +16,11 @@
16 16
17 package org.thingsboard.server.dao.rule; 17 package org.thingsboard.server.dao.rule;
18 18
19 -import org.thingsboard.server.common.data.id.PluginId;  
20 import org.thingsboard.server.common.data.id.RuleChainId; 19 import org.thingsboard.server.common.data.id.RuleChainId;
21 import org.thingsboard.server.common.data.id.RuleNodeId; 20 import org.thingsboard.server.common.data.id.RuleNodeId;
22 import org.thingsboard.server.common.data.id.TenantId; 21 import org.thingsboard.server.common.data.id.TenantId;
23 import org.thingsboard.server.common.data.page.TextPageData; 22 import org.thingsboard.server.common.data.page.TextPageData;
24 import org.thingsboard.server.common.data.page.TextPageLink; 23 import org.thingsboard.server.common.data.page.TextPageLink;
25 -import org.thingsboard.server.common.data.plugin.PluginMetaData;  
26 import org.thingsboard.server.common.data.relation.EntityRelation; 24 import org.thingsboard.server.common.data.relation.EntityRelation;
27 import org.thingsboard.server.common.data.rule.RuleChain; 25 import org.thingsboard.server.common.data.rule.RuleChain;
28 import org.thingsboard.server.common.data.rule.RuleChainMetaData; 26 import org.thingsboard.server.common.data.rule.RuleChainMetaData;