Refactoring after final review
This commit is contained in:
parent
caf2f29693
commit
ec4200d735
@ -56,9 +56,9 @@ public abstract class BaseAlarmProcessor extends BaseEdgeProcessor {
|
||||
if (alarm == null) {
|
||||
throw new RuntimeException("[{" + tenantId + "}] alarmUpdateMsg {" + alarmUpdateMsg + "} cannot be converted to alarm");
|
||||
}
|
||||
EntityType entityType = isEdgeProtoDeprecated ? EntityType.valueOf(alarmUpdateMsg.getOriginatorType())
|
||||
: alarm.getOriginator().getEntityType();
|
||||
EntityId originatorId = getAlarmOriginator(tenantId, alarmUpdateMsg.getOriginatorName(), entityType);
|
||||
EntityId originatorId = isEdgeProtoDeprecated
|
||||
? getAlarmOriginator(tenantId, alarmUpdateMsg.getOriginatorName(), EntityType.valueOf(alarmUpdateMsg.getOriginatorType()))
|
||||
: alarm.getOriginator();
|
||||
if (originatorId == null) {
|
||||
log.warn("[{}] Originator not found for the alarm msg {}", tenantId, alarmUpdateMsg);
|
||||
return Futures.immediateFuture(null);
|
||||
|
||||
@ -129,17 +129,16 @@ public class AssetProfileEdgeProcessor extends BaseAssetProfileProcessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setDefaultRuleChainId(TenantId tenantId, AssetProfile assetProfile) {
|
||||
// do nothing on cloud
|
||||
protected void setDefaultRuleChainId(TenantId tenantId, AssetProfile assetProfile, RuleChainId ruleChainId) {
|
||||
assetProfile.setDefaultRuleChainId(ruleChainId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setDefaultEdgeRuleChainId(AssetProfile assetProfile, RuleChainId ruleChainId, AssetProfileUpdateMsg assetProfileUpdateMsg, boolean isEdgeVersionDeprecated) {
|
||||
UUID defaultEdgeRuleChainUUID = isEdgeVersionDeprecated
|
||||
? safeGetUUID(assetProfileUpdateMsg.getDefaultRuleChainIdMSB(), assetProfileUpdateMsg.getDefaultRuleChainIdLSB())
|
||||
: assetProfile.getDefaultRuleChainId() != null ? assetProfile.getDefaultRuleChainId().getId() : null;
|
||||
: ruleChainId != null ? ruleChainId.getId() : null;
|
||||
assetProfile.setDefaultEdgeRuleChainId(defaultEdgeRuleChainUUID != null ? new RuleChainId(defaultEdgeRuleChainUUID) : null);
|
||||
assetProfile.setDefaultRuleChainId(ruleChainId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -61,8 +61,9 @@ public abstract class BaseAssetProfileProcessor extends BaseEdgeProcessor {
|
||||
}
|
||||
assetProfile.setName(assetProfileName);
|
||||
|
||||
setDefaultRuleChainId(tenantId, assetProfile);
|
||||
setDefaultEdgeRuleChainId(assetProfile, created ? null : assetProfileById.getDefaultRuleChainId(), assetProfileUpdateMsg, isEdgeProtoDeprecated);
|
||||
RuleChainId ruleChainId = assetProfile.getDefaultRuleChainId();
|
||||
setDefaultRuleChainId(tenantId, assetProfile, created ? null : assetProfileById.getDefaultRuleChainId());
|
||||
setDefaultEdgeRuleChainId(assetProfile, ruleChainId, assetProfileUpdateMsg, isEdgeProtoDeprecated);
|
||||
setDefaultDashboardId(tenantId, created ? null : assetProfileById.getDefaultDashboardId(), assetProfile, assetProfileUpdateMsg, isEdgeProtoDeprecated);
|
||||
|
||||
assetProfileValidator.validate(assetProfile, AssetProfile::getTenantId);
|
||||
@ -92,7 +93,7 @@ public abstract class BaseAssetProfileProcessor extends BaseEdgeProcessor {
|
||||
return assetProfile;
|
||||
}
|
||||
|
||||
protected abstract void setDefaultRuleChainId(TenantId tenantId, AssetProfile assetProfile);
|
||||
protected abstract void setDefaultRuleChainId(TenantId tenantId, AssetProfile assetProfile, RuleChainId ruleChainId);
|
||||
|
||||
protected abstract void setDefaultEdgeRuleChainId(AssetProfile assetProfile, RuleChainId ruleChainId, AssetProfileUpdateMsg assetProfileUpdateMsg, boolean isEdgeVersionDeprecated);
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.common.data.Dashboard;
|
||||
import org.thingsboard.server.common.data.ShortCustomerInfo;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
import org.thingsboard.server.common.data.id.DashboardId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.gen.edge.v1.DashboardUpdateMsg;
|
||||
@ -31,7 +32,7 @@ import java.util.Set;
|
||||
@Slf4j
|
||||
public abstract class BaseDashboardProcessor extends BaseEdgeProcessor {
|
||||
|
||||
protected boolean saveOrUpdateDashboard(TenantId tenantId, DashboardId dashboardId, DashboardUpdateMsg dashboardUpdateMsg, boolean isEdgeProtoDeprecated) {
|
||||
protected boolean saveOrUpdateDashboard(TenantId tenantId, DashboardId dashboardId, DashboardUpdateMsg dashboardUpdateMsg, boolean isEdgeProtoDeprecated, CustomerId customerId) {
|
||||
boolean created = false;
|
||||
Dashboard dashboard = isEdgeProtoDeprecated
|
||||
? createDashboard(tenantId, dashboardId, dashboardUpdateMsg)
|
||||
@ -39,24 +40,39 @@ public abstract class BaseDashboardProcessor extends BaseEdgeProcessor {
|
||||
if (dashboard == null) {
|
||||
throw new RuntimeException("[{" + tenantId + "}] dashboardUpdateMsg {" + dashboardUpdateMsg + "} cannot be converted to dashboard");
|
||||
}
|
||||
Set<ShortCustomerInfo> assignedCustomers = null;
|
||||
Dashboard dashboardById = dashboardService.findDashboardById(tenantId, dashboardId);
|
||||
if (dashboardById == null) {
|
||||
created = true;
|
||||
dashboard.setId(null);
|
||||
} else {
|
||||
dashboard.setId(dashboardId);
|
||||
assignedCustomers = filterNonExistingCustomers(tenantId, dashboardById.getAssignedCustomers());
|
||||
}
|
||||
|
||||
dashboardValidator.validate(dashboard, Dashboard::getTenantId);
|
||||
if (created) {
|
||||
dashboard.setId(dashboardId);
|
||||
}
|
||||
Dashboard savedDashboard = dashboardService.saveDashboard(dashboard, false);
|
||||
if (savedDashboard.getAssignedCustomers() != null && savedDashboard.getAssignedCustomers().isEmpty()) {
|
||||
for (ShortCustomerInfo assignedCustomer : savedDashboard.getAssignedCustomers()) {
|
||||
dashboardService.unassignDashboardFromCustomer(tenantId, savedDashboard.getId(), assignedCustomer.getCustomerId());
|
||||
Set<ShortCustomerInfo> msgAssignedCustomers = filterNonExistingCustomers(tenantId, dashboard.getAssignedCustomers());
|
||||
if (msgAssignedCustomers != null) {
|
||||
if (assignedCustomers == null) {
|
||||
assignedCustomers = msgAssignedCustomers;
|
||||
} else {
|
||||
assignedCustomers.addAll(msgAssignedCustomers);
|
||||
}
|
||||
}
|
||||
dashboard.setAssignedCustomers(assignedCustomers);
|
||||
Dashboard savedDashboard = dashboardService.saveDashboard(dashboard, false);
|
||||
if (msgAssignedCustomers != null && !msgAssignedCustomers.isEmpty()) {
|
||||
for (ShortCustomerInfo assignedCustomer : msgAssignedCustomers) {
|
||||
if (assignedCustomer.getCustomerId().equals(customerId)) {
|
||||
dashboardService.assignDashboardToCustomer(tenantId, savedDashboard.getId(), assignedCustomer.getCustomerId());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
unassignCustomersFromDashboard(tenantId, savedDashboard, customerId);
|
||||
}
|
||||
return created;
|
||||
}
|
||||
|
||||
@ -80,6 +96,15 @@ public abstract class BaseDashboardProcessor extends BaseEdgeProcessor {
|
||||
return dashboard;
|
||||
}
|
||||
|
||||
private void unassignCustomersFromDashboard(TenantId tenantId, Dashboard dashboard, CustomerId customerId) {
|
||||
if (dashboard.getAssignedCustomers() != null && !dashboard.getAssignedCustomers().isEmpty()) {
|
||||
for (ShortCustomerInfo assignedCustomer : dashboard.getAssignedCustomers()) {
|
||||
if (assignedCustomer.getCustomerId().equals(customerId)) {
|
||||
dashboardService.unassignDashboardFromCustomer(tenantId, dashboard.getId(), assignedCustomer.getCustomerId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Set<ShortCustomerInfo> filterNonExistingCustomers(TenantId tenantId, Set<ShortCustomerInfo> assignedCustomers);
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ public class DashboardEdgeProcessor extends BaseDashboardProcessor {
|
||||
|
||||
private void saveOrUpdateDashboard(TenantId tenantId, DashboardId dashboardId, DashboardUpdateMsg dashboardUpdateMsg, Edge edge, EdgeVersion edgeVersion) {
|
||||
boolean created = super.saveOrUpdateDashboard(tenantId, dashboardId, dashboardUpdateMsg,
|
||||
EdgeVersionUtils.isEdgeVersionOlderThan_3_6_2(edgeVersion));
|
||||
EdgeVersionUtils.isEdgeVersionOlderThan_3_6_2(edgeVersion), edge.getCustomerId());
|
||||
if (created) {
|
||||
createRelationFromEdge(tenantId, edge.getId(), dashboardId);
|
||||
pushDashboardCreatedEventToRuleEngine(tenantId, edge, dashboardId);
|
||||
|
||||
@ -73,8 +73,9 @@ public abstract class BaseDeviceProfileProcessor extends BaseEdgeProcessor {
|
||||
}
|
||||
deviceProfile.setName(deviceProfileName);
|
||||
|
||||
setDefaultRuleChainId(tenantId, deviceProfile);
|
||||
setDefaultEdgeRuleChainId(deviceProfile, created ? null : deviceProfileById.getDefaultRuleChainId(), deviceProfileUpdateMsg, isEdgeVersionProtoDeprecated);
|
||||
RuleChainId ruleChainId = deviceProfile.getDefaultRuleChainId();
|
||||
setDefaultRuleChainId(tenantId, deviceProfile, created ? null : deviceProfileById.getDefaultRuleChainId());
|
||||
setDefaultEdgeRuleChainId(deviceProfile, ruleChainId, deviceProfileUpdateMsg, isEdgeVersionProtoDeprecated);
|
||||
setDefaultDashboardId(tenantId, created ? null : deviceProfileById.getDefaultDashboardId(), deviceProfile, deviceProfileUpdateMsg, isEdgeVersionProtoDeprecated);
|
||||
|
||||
deviceProfileValidator.validate(deviceProfile, DeviceProfile::getTenantId);
|
||||
@ -124,7 +125,7 @@ public abstract class BaseDeviceProfileProcessor extends BaseEdgeProcessor {
|
||||
return deviceProfile;
|
||||
}
|
||||
|
||||
protected abstract void setDefaultRuleChainId(TenantId tenantId, DeviceProfile deviceProfile);
|
||||
protected abstract void setDefaultRuleChainId(TenantId tenantId, DeviceProfile deviceProfile, RuleChainId ruleChainId);
|
||||
|
||||
protected abstract void setDefaultEdgeRuleChainId(DeviceProfile deviceProfile, RuleChainId ruleChainId, DeviceProfileUpdateMsg deviceProfileUpdateMsg, boolean isEdgeVersionDeprecated);
|
||||
|
||||
|
||||
@ -129,17 +129,16 @@ public class DeviceProfileEdgeProcessor extends BaseDeviceProfileProcessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setDefaultRuleChainId(TenantId tenantId, DeviceProfile deviceProfile) {
|
||||
// do nothing on cloud
|
||||
protected void setDefaultRuleChainId(TenantId tenantId, DeviceProfile deviceProfile, RuleChainId ruleChainId) {
|
||||
deviceProfile.setDefaultRuleChainId(ruleChainId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setDefaultEdgeRuleChainId(DeviceProfile deviceProfile, RuleChainId ruleChainId, DeviceProfileUpdateMsg deviceProfileUpdateMsg, boolean isEdgeVersionDeprecated) {
|
||||
UUID defaultEdgeRuleChainUUID = isEdgeVersionDeprecated
|
||||
? safeGetUUID(deviceProfileUpdateMsg.getDefaultRuleChainIdMSB(), deviceProfileUpdateMsg.getDefaultRuleChainIdLSB())
|
||||
: deviceProfile.getDefaultRuleChainId() != null ? deviceProfile.getDefaultRuleChainId().getId() : null;
|
||||
: ruleChainId != null ? ruleChainId.getId() : null;
|
||||
deviceProfile.setDefaultEdgeRuleChainId(defaultEdgeRuleChainUUID != null ? new RuleChainId(defaultEdgeRuleChainUUID) : null);
|
||||
deviceProfile.setDefaultRuleChainId(ruleChainId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -449,6 +449,9 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest {
|
||||
if (adminSettings.getKey().equals("connectivity")) {
|
||||
validateConnectivityAdminSettings(adminSettings);
|
||||
}
|
||||
if (adminSettings.getKey().equals("jwt")) {
|
||||
validateJwtAdminSettings(adminSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -467,12 +470,14 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest {
|
||||
|
||||
private void validateConnectivityAdminSettings(AdminSettings adminSettings) {
|
||||
JsonNode jsonNode = adminSettings.getJsonValue();
|
||||
Assert.assertNotNull(jsonNode.get("http"));
|
||||
Assert.assertNotNull(jsonNode.get("https"));
|
||||
Assert.assertNotNull(jsonNode.get("mqtt"));
|
||||
Assert.assertNotNull(jsonNode.get("mqtts"));
|
||||
Assert.assertNotNull(jsonNode.get("coap"));
|
||||
Assert.assertNotNull(jsonNode.get("coaps"));
|
||||
Assert.assertNotNull(jsonNode.get("tokenExpirationTime"));
|
||||
Assert.assertNotNull(jsonNode.get("refreshTokenExpTime"));
|
||||
Assert.assertNotNull(jsonNode.get("tokenIssuer"));
|
||||
Assert.assertNotNull(jsonNode.get("tokenSigningKey"));
|
||||
}
|
||||
|
||||
private void validateJwtAdminSettings(AdminSettings adminSettings) {
|
||||
Assert.assertNotNull(adminSettings.getJsonValue().get("baseUrl"));
|
||||
}
|
||||
|
||||
private void validateAssetProfiles(int expectedMsgCnt) throws Exception {
|
||||
|
||||
@ -16,7 +16,6 @@
|
||||
package org.thingsboard.server.edge;
|
||||
|
||||
import com.google.protobuf.AbstractMessage;
|
||||
import com.google.protobuf.ByteString;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
@ -31,7 +30,6 @@ import org.thingsboard.server.gen.edge.v1.UpdateMsgType;
|
||||
import org.thingsboard.server.gen.edge.v1.UplinkMsg;
|
||||
import org.thingsboard.server.gen.edge.v1.UplinkResponseMsg;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@ -311,7 +311,7 @@ message AlarmUpdateMsg {
|
||||
string name = 4 [deprecated = true];
|
||||
string type = 5 [deprecated = true];
|
||||
string originatorType = 6 [deprecated = true];
|
||||
string originatorName = 7;
|
||||
string originatorName = 7 [deprecated = true];
|
||||
string severity = 8 [deprecated = true];
|
||||
string status = 9 [deprecated = true];
|
||||
int64 startTs = 10 [deprecated = true];
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.thingsboard.common.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.Assert;
|
||||
@ -28,7 +27,7 @@ import java.util.UUID;
|
||||
public class JacksonUtilTest {
|
||||
|
||||
@Test
|
||||
public void allow_unquoted_field_mapper_test() {
|
||||
public void allowUnquotedFieldMapperTest() {
|
||||
String data = "{data: 123}";
|
||||
JsonNode actualResult = JacksonUtil.toJsonNode(data, JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER); // should be: {"data": 123}
|
||||
ObjectNode expectedResult = JacksonUtil.newObjectNode();
|
||||
@ -38,14 +37,15 @@ public class JacksonUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fail_on_unknown_properties_edge_mapper_test() throws JsonProcessingException {
|
||||
public void failOnUnknownPropertiesMapperTest() {
|
||||
Asset asset = new Asset();
|
||||
asset.setId(new AssetId(UUID.randomUUID()));
|
||||
asset.setName("Test");
|
||||
asset.setType("type");
|
||||
String serializedAsset = JacksonUtil.toString(asset);
|
||||
JsonNode jsonNode = JacksonUtil.IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER.readTree(serializedAsset);
|
||||
JsonNode jsonNode = JacksonUtil.toJsonNode(serializedAsset);
|
||||
// case: add new field to serialized Asset string and check for backward compatibility with original Asset object
|
||||
Assert.assertNotNull(jsonNode);
|
||||
((ObjectNode) jsonNode).put("test", (String) null);
|
||||
serializedAsset = JacksonUtil.toString(jsonNode);
|
||||
// deserialize with FAIL_ON_UNKNOWN_PROPERTIES = false
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user