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