Commit e5753c036076fbc73ce74d8f1bf4333491b66b20

Authored by Volodymyr Babak
1 parent 1bb77e2e

Refactored edge rule chains storage

application/src/main/data/json/demo/edge_management/rule_chains/edge_root_rule_chain.json renamed from application/src/main/data/json/demo/rule_chains/edge_root_rule_chain.json
application/src/main/data/json/tenant/edge_management/rule_chains/edge_root_rule_chain.json renamed from application/src/main/data/json/tenant/rule_chains/edge_root_rule_chain.json
... ... @@ -77,6 +77,9 @@ public class TenantController extends BaseController {
77 77 tenant = checkNotNull(tenantService.saveTenant(tenant));
78 78 if (newTenant) {
79 79 installScripts.createDefaultRuleChains(tenant.getId());
  80 + if (edgesEnabled) {
  81 + installScripts.createDefaultEdgeRuleChains(tenant.getId());
  82 + }
80 83 }
81 84 return tenant;
82 85 } catch (Exception e) {
... ...
... ... @@ -28,7 +28,6 @@ import org.thingsboard.server.common.data.id.RuleChainId;
28 28 import org.thingsboard.server.common.data.id.TenantId;
29 29 import org.thingsboard.server.common.data.rule.RuleChain;
30 30 import org.thingsboard.server.common.data.rule.RuleChainMetaData;
31   -import org.thingsboard.server.common.data.rule.RuleChainType;
32 31 import org.thingsboard.server.common.data.widget.WidgetType;
33 32 import org.thingsboard.server.common.data.widget.WidgetsBundle;
34 33 import org.thingsboard.server.dao.dashboard.DashboardService;
... ... @@ -36,7 +35,6 @@ import org.thingsboard.server.dao.rule.RuleChainService;
36 35 import org.thingsboard.server.dao.widget.WidgetTypeService;
37 36 import org.thingsboard.server.dao.widget.WidgetsBundleService;
38 37
39   -import java.io.File;
40 38 import java.io.IOException;
41 39 import java.nio.file.DirectoryStream;
42 40 import java.nio.file.Files;
... ... @@ -64,6 +62,8 @@ public class InstallScripts {
64 62 public static final String WIDGET_BUNDLES_DIR = "widget_bundles";
65 63 public static final String DASHBOARDS_DIR = "dashboards";
66 64
  65 + public static final String EDGE_MANAGEMENT = "edge_management";
  66 +
67 67 public static final String JSON_EXT = ".json";
68 68
69 69 @Value("${install.data_dir:}")
... ... @@ -81,10 +81,14 @@ public class InstallScripts {
81 81 @Autowired
82 82 private WidgetsBundleService widgetsBundleService;
83 83
84   - public Path getTenantRuleChainsDir() {
  84 + private Path getTenantRuleChainsDir() {
85 85 return Paths.get(getDataDir(), JSON_DIR, TENANT_DIR, RULE_CHAINS_DIR);
86 86 }
87 87
  88 + private Path getEdgeRuleChainsDir() {
  89 + return Paths.get(getDataDir(), JSON_DIR, TENANT_DIR, EDGE_MANAGEMENT, RULE_CHAINS_DIR);
  90 + }
  91 +
88 92 public String getDataDir() {
89 93 if (!StringUtils.isEmpty(dataDir)) {
90 94 if (!Paths.get(this.dataDir).toFile().isDirectory()) {
... ... @@ -108,18 +112,22 @@ public class InstallScripts {
108 112
109 113 public void createDefaultRuleChains(TenantId tenantId) throws IOException {
110 114 Path tenantChainsDir = getTenantRuleChainsDir();
111   - try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(tenantChainsDir, path -> path.toString().endsWith(InstallScripts.JSON_EXT))) {
  115 + loadRuleChainsFromPath(tenantId, tenantChainsDir);
  116 + }
  117 +
  118 + public void createDefaultEdgeRuleChains(TenantId tenantId) throws IOException {
  119 + Path edgeChainsDir = getEdgeRuleChainsDir();
  120 + loadRuleChainsFromPath(tenantId, edgeChainsDir);
  121 + }
  122 +
  123 + private void loadRuleChainsFromPath(TenantId tenantId, Path ruleChainsPath) throws IOException {
  124 + try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(ruleChainsPath, path -> path.toString().endsWith(InstallScripts.JSON_EXT))) {
112 125 dirStream.forEach(
113 126 path -> loadRuleChainFromFile(tenantId, path)
114 127 );
115 128 }
116 129 }
117 130
118   - public void createDefaultEdgeRuleChains(TenantId tenantId) {
119   - Path tenantChainsDir = getTenantRuleChainsDir();
120   - loadRuleChainFromFile(tenantId, tenantChainsDir.resolve("edge_root_rule_chain.json"));
121   - }
122   -
123 131 public void loadSystemWidgets() throws Exception {
124 132 Path widgetBundlesDir = Paths.get(getDataDir(), JSON_DIR, SYSTEM_DIR, WIDGET_BUNDLES_DIR);
125 133 try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(widgetBundlesDir, path -> path.toString().endsWith(JSON_EXT))) {
... ... @@ -174,7 +182,6 @@ public class InstallScripts {
174 182 }
175 183 }
176 184
177   -
178 185 public void loadDemoRuleChains(TenantId tenantId) throws Exception {
179 186 Path ruleChainsDir = Paths.get(getDataDir(), JSON_DIR, DEMO_DIR, RULE_CHAINS_DIR);
180 187 try {
... ... @@ -196,8 +203,17 @@ public class InstallScripts {
196 203 rootChain = ruleChainService.saveRuleChain(rootChain);
197 204 rootChainMetaData.setRuleChainId(rootChain.getId());
198 205 ruleChainService.saveRuleChainMetaData(new TenantId(EntityId.NULL_UUID), rootChainMetaData);
  206 + } catch (Exception e) {
  207 + log.error("Unable to load dashboard from json", e);
  208 + throw new RuntimeException("Unable to load dashboard from json", e);
  209 + }
  210 + loadEdgeDemoRuleChains(tenantId);
  211 + }
199 212
200   - loadRuleChainFromFile(tenantId, ruleChainsDir.resolve("edge_root_rule_chain.json"));
  213 + private void loadEdgeDemoRuleChains(TenantId tenantId) throws Exception {
  214 + Path edgeRuleChainsDir = Paths.get(getDataDir(), JSON_DIR, DEMO_DIR, EDGE_MANAGEMENT, RULE_CHAINS_DIR);
  215 + try {
  216 + loadRuleChainFromFile(tenantId, edgeRuleChainsDir.resolve("edge_root_rule_chain.json"));
201 217 } catch (Exception e) {
202 218 log.error("Unable to load dashboard from json", e);
203 219 throw new RuntimeException("Unable to load dashboard from json", e);
... ...
... ... @@ -203,7 +203,7 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
203 203 break;
204 204 case "2.5.5":
205 205 try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
206   - log.info("Load TTL functions ...");
  206 + log.info("Load Edge TTL functions ...");
207 207 loadSql(conn, "2.6.0", LOAD_TTL_FUNCTIONS_SQL);
208 208 }
209 209 break;
... ...
... ... @@ -91,7 +91,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
91 91 EntityRelation.CONTAINS_TYPE, RelationTypeGroup.RULE_CHAIN));
92 92 } catch (Exception e) {
93 93 log.warn("[{}] Failed to create tenant to root rule chain relation. from: [{}], to: [{}]",
94   - savedRuleChain.getTenantId(), savedRuleChain.getId());
  94 + savedRuleChain.getTenantId(), savedRuleChain.getTenantId(), savedRuleChain.getId(), e);
95 95 throw new RuntimeException(e);
96 96 }
97 97 }
... ...