From 086dae045ae11293214e707d08e45b3c8aeaad92 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Wed, 11 May 2022 14:45:07 +0300 Subject: [PATCH 1/5] refactoring: - Customer controller - preparation --- .../DefaultTbNotificationEntityService.java | 43 ++++++------- .../entitiy/TbNotificationEntityService.java | 20 +++--- .../entitiy/alarm/DefaultTbAlarmService.java | 3 +- .../entitiy/asset/DefaultTbAssetService.java | 4 +- .../customer/DefaultTbCustomerService.java | 63 +++++++++++++++++++ .../entitiy/customer/TbCustomerService.java | 26 ++++++++ 6 files changed, 123 insertions(+), 36 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/service/entitiy/customer/DefaultTbCustomerService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java index b6ab974988..b5595ec552 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java @@ -22,17 +22,16 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; import org.thingsboard.server.cluster.TbClusterService; +import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.HasName; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.alarm.Alarm; -import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeEventActionType; import org.thingsboard.server.common.data.edge.EdgeEventType; -import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.EdgeId; @@ -70,10 +69,11 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS @Override public void notifyDeleteEntity(TenantId tenantId, I entityId, E entity, - CustomerId customerId, List relatedEdgeIds, - SecurityUser user, Object... additionalInfo) { - logEntityAction(tenantId, entityId, entity, customerId, ActionType.DELETED, user, additionalInfo); - sendDeleteNotificationMsg(tenantId, entityId, relatedEdgeIds); + CustomerId customerId, ActionType actionType, + List relatedEdgeIds, + SecurityUser user, boolean isBody, Object... additionalInfo) { + logEntityAction(tenantId, entityId, entity, customerId, actionType, user, additionalInfo); + sendDeleteNotificationMsg(tenantId, entityId, entity, relatedEdgeIds, isBody); } @Override @@ -126,7 +126,7 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS gatewayNotificationsService.onDeviceDeleted(device); tbClusterService.onDeviceDeleted(device, null); - notifyDeleteEntity(tenantId, deviceId, device, customerId, relatedEdgeIds, user, additionalInfo); + notifyDeleteEntity(tenantId, deviceId, device, customerId, ActionType.DELETED, relatedEdgeIds, user, false, additionalInfo); } @Override @@ -145,12 +145,10 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS } @Override - public void notifyCreateOrUpdateAsset(TenantId tenantId, AssetId assetId, CustomerId customerId, Asset asset, - ActionType actionType, SecurityUser user, Object... additionalInfo) { - logEntityAction(tenantId, assetId, asset, customerId, actionType, user, additionalInfo); - - if (actionType == ActionType.UPDATED) { - sendEntityNotificationMsg(asset.getTenantId(), asset.getId(), EdgeEventActionType.UPDATED); + public void notifyCreateOrUpdateEntity(TenantId tenantId, I entityId, E entity, CustomerId customerId, ActionType actionType, SecurityUser user, Object... additionalInfo) { + logEntityAction(tenantId, entityId, entity, customerId, actionType, user, additionalInfo); + if (actionType == ActionType.UPDATED) { + sendEntityNotificationMsg(tenantId, entityId, EdgeEventActionType.UPDATED); } } @@ -197,9 +195,10 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS } @Override - public void notifyDeleteAlarm(Alarm alarm, SecurityUser user, List relatedEdgeIds) { - logEntityAction(alarm.getTenantId(), alarm.getOriginator(), alarm, alarm.getCustomerId(), ActionType.ALARM_DELETE, user, null); - sendAlarmDeleteNotificationMsg(alarm, relatedEdgeIds); + public void notifyDeleteCustomer(Customer customer, SecurityUser user, List edgeIds) { + logEntityAction(customer.getTenantId(), customer.getId(), customer, customer.getId(), ActionType.DELETED, user, null); + sendDeleteNotificationMsg(customer.getTenantId(), customer.getId(), customer, edgeIds, false); + tbClusterService.broadcastEntityStateChangeEvent(customer.getTenantId(), customer.getId(), ComponentLifecycleEvent.DELETED); } private void logEntityAction(TenantId tenantId, I entityId, E entity, CustomerId customerId, @@ -228,17 +227,15 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS } } - private void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List edgeIds) { - sendDeleteNotificationMsg(tenantId, entityId, edgeIds, null); - } - - protected void sendAlarmDeleteNotificationMsg(Alarm alarm, List relatedEdgeIds) { + protected void sendDeleteNotificationMsg(TenantId tenantId, I entityId, E entity, List edgeIds, boolean isBody) { try { - sendDeleteNotificationMsg(alarm.getTenantId(), alarm.getId(), relatedEdgeIds, json.writeValueAsString(alarm)); + String body = isBody ? json.writeValueAsString(entity) : null; + sendDeleteNotificationMsg(tenantId, entityId, edgeIds, body); } catch (Exception e) { - log.warn("Failed to push delete alarm msg to core: {}", alarm, e); + log.warn("Failed to push delete " + entity.getClass().getName() + " msg to core: {}", entity, e); } } + private void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List edgeIds, String body) { if (edgeIds != null && !edgeIds.isEmpty()) { for (EdgeId edgeId : edgeIds) { diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java index 181a1f41eb..ed416ca051 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java @@ -15,15 +15,14 @@ */ package org.thingsboard.server.service.entitiy; +import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.HasName; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.alarm.Alarm; -import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeEventActionType; -import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.EdgeId; @@ -41,9 +40,13 @@ public interface TbNotificationEntityService { ActionType actionType, SecurityUser user, Exception e, Object... additionalInfo); + void notifyCreateOrUpdateEntity(TenantId tenantId, I entityId, E entity, + CustomerId customerId, ActionType actionType, + SecurityUser user, Object... additionalInfo); + void notifyDeleteEntity(TenantId tenantId, I entityId, E entity, CustomerId customerId, - List relatedEdgeIds, SecurityUser user, - Object... additionalInfo); + ActionType actionType, List relatedEdgeIds, SecurityUser user, + boolean isBody, Object... additionalInfo); void notifyAssignOrUnassignEntityToCustomer(TenantId tenantId, I entityId, CustomerId customerId, E entity, @@ -74,13 +77,10 @@ public interface TbNotificationEntityService { void notifyAssignDeviceToTenant(TenantId tenantId, TenantId newTenantId, DeviceId deviceId, CustomerId customerId, Device device, Tenant tenant, SecurityUser user, Object... additionalInfo); - void notifyCreateOrUpdateAsset(TenantId tenantId, AssetId assetId, CustomerId customerId, Asset asset, - ActionType actionType, SecurityUser user, Object... additionalInfo); - void notifyEdge(TenantId tenantId, EdgeId edgeId, CustomerId customerId, Edge edge, ActionType actionType, SecurityUser user, Object... additionalInfo); - void notifyCreateOrUpdateAlarm(Alarm alarm, - ActionType actionType, SecurityUser user, Object... additionalInfo); + void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo); - void notifyDeleteAlarm(Alarm alarm, SecurityUser user, List relatedEdgeIds); + + void notifyDeleteCustomer(Customer customer, SecurityUser user, List relatedEdgeIds); } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java index f0ac516393..22196c4984 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java @@ -79,7 +79,8 @@ public class DefaultTbAlarmService extends AbstractTbEntityService implements Tb @Override public Boolean delete(Alarm alarm, SecurityUser user) throws ThingsboardException { List relatedEdgeIds = findRelatedEdgeIds(alarm.getTenantId(), alarm.getOriginator()); - notificationEntityService.notifyDeleteAlarm(alarm, user, relatedEdgeIds); + notificationEntityService.notifyDeleteEntity(alarm.getTenantId(), alarm.getOriginator(), alarm, alarm.getCustomerId(), + ActionType.ALARM_DELETE, relatedEdgeIds, user, true); return alarmService.deleteAlarm(alarm.getTenantId(), alarm.getId()).isSuccessful(); } } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/asset/DefaultTbAssetService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/asset/DefaultTbAssetService.java index 84b9c707d0..3f4ce83318 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/asset/DefaultTbAssetService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/asset/DefaultTbAssetService.java @@ -45,7 +45,7 @@ public class DefaultTbAssetService extends AbstractTbEntityService implements Tb TenantId tenantId = asset.getTenantId(); try { Asset savedAsset = checkNotNull(assetService.saveAsset(asset)); - notificationEntityService.notifyCreateOrUpdateAsset(tenantId, savedAsset.getId(), savedAsset.getCustomerId(), asset, actionType, user); + notificationEntityService.notifyCreateOrUpdateEntity(tenantId, savedAsset.getId(), asset, savedAsset.getCustomerId(), actionType, user); return savedAsset; } catch (Exception e) { notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.ASSET), asset, null, actionType, user, e); @@ -60,7 +60,7 @@ public class DefaultTbAssetService extends AbstractTbEntityService implements Tb try { List relatedEdgeIds = findRelatedEdgeIds(tenantId, assetId); assetService.deleteAsset(tenantId, assetId); - notificationEntityService.notifyDeleteEntity(tenantId, assetId, asset, asset.getCustomerId(), relatedEdgeIds, user, asset.toString()); + notificationEntityService.notifyDeleteEntity(tenantId, assetId, asset, asset.getCustomerId(), ActionType.DELETED, relatedEdgeIds, user, false, asset.toString()); return removeAlarmsByEntityId(tenantId, assetId); } catch (Exception e) { diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/customer/DefaultTbCustomerService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/customer/DefaultTbCustomerService.java new file mode 100644 index 0000000000..452c71b51a --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/customer/DefaultTbCustomerService.java @@ -0,0 +1,63 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.entitiy.customer; + +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.Customer; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.audit.ActionType; +import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.common.data.id.EdgeId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.queue.util.TbCoreComponent; +import org.thingsboard.server.service.entitiy.AbstractTbEntityService; +import org.thingsboard.server.service.security.model.SecurityUser; + +import java.util.List; + +@Service +@TbCoreComponent +@AllArgsConstructor +public class DefaultTbCustomerService extends AbstractTbEntityService implements TbCustomerService { + + @Override + public Customer save(Customer customer, SecurityUser user) throws ThingsboardException { + ActionType actionType = customer.getId() == null ? ActionType.ADDED : ActionType.UPDATED; + TenantId tenantId = customer.getTenantId(); + try { + Customer savedCustomer = checkNotNull(customerService.saveCustomer(customer)); + notificationEntityService.notifyCreateOrUpdateEntity(tenantId, savedCustomer.getId(), savedCustomer, savedCustomer.getId(), actionType, user); + return savedCustomer; + } catch (Exception e) { + notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.CUSTOMER), customer, null, actionType, user, e); + throw handleException(e); + } + } + + @Override + public void delete(Customer customer, SecurityUser user) throws ThingsboardException { + TenantId tenantId = customer.getTenantId(); + try { + List relatedEdgeIds = findRelatedEdgeIds(tenantId, customer.getId()); + customerService.deleteCustomer(tenantId, customer.getId()); + notificationEntityService.notifyDeleteCustomer(customer, user, relatedEdgeIds); + } catch (Exception e) { + notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.CUSTOMER), null, null, ActionType.DELETED, user, e); + throw handleException(e); + } + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java new file mode 100644 index 0000000000..36c5a0c0ed --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java @@ -0,0 +1,26 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.entitiy.customer; + +import org.thingsboard.server.common.data.Customer; +import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.service.security.model.SecurityUser; + +public interface TbCustomerService { + Customer save (Customer customer, SecurityUser user) throws ThingsboardException; + + void delete (Customer customer, SecurityUser user) throws ThingsboardException; +} From fa9f464810056ac1ddb0865555ec0033312d06fb Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Wed, 11 May 2022 17:53:26 +0300 Subject: [PATCH 2/5] refactoring: - Customer controller - finish --- .../server/controller/CustomerController.java | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/CustomerController.java b/application/src/main/java/org/thingsboard/server/controller/CustomerController.java index b419121459..8d67fe02de 100644 --- a/application/src/main/java/org/thingsboard/server/controller/CustomerController.java +++ b/application/src/main/java/org/thingsboard/server/controller/CustomerController.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PathVariable; @@ -33,7 +34,6 @@ import org.springframework.web.bind.annotation.RestController; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.audit.ActionType; -import org.thingsboard.server.common.data.edge.EdgeEventActionType; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EdgeId; @@ -42,6 +42,7 @@ import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; import org.thingsboard.server.queue.util.TbCoreComponent; +import org.thingsboard.server.service.entitiy.customer.TbCustomerService; import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Resource; @@ -64,9 +65,12 @@ import static org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LI @RestController @TbCoreComponent +@RequiredArgsConstructor @RequestMapping("/api") public class CustomerController extends BaseController { + private final TbCustomerService tbCustomerService; + public static final String IS_PUBLIC = "isPublic"; public static final String CUSTOMER_SECURITY_CHECK = "If the user has the authority of 'Tenant Administrator', the server checks that the customer is owned by the same tenant. " + "If the user has the authority of 'Customer User', the server checks that the user belongs to the customer."; @@ -145,30 +149,10 @@ public class CustomerController extends BaseController { @RequestMapping(value = "/customer", method = RequestMethod.POST) @ResponseBody public Customer saveCustomer(@ApiParam(value = "A JSON value representing the customer.") @RequestBody Customer customer) throws ThingsboardException { - try { customer.setTenantId(getCurrentUser().getTenantId()); - checkEntity(customer.getId(), customer, Resource.CUSTOMER); - - Customer savedCustomer = checkNotNull(customerService.saveCustomer(customer)); - - logEntityAction(savedCustomer.getId(), savedCustomer, - savedCustomer.getId(), - customer.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); - - if (customer.getId() != null) { - sendEntityNotificationMsg(savedCustomer.getTenantId(), savedCustomer.getId(), EdgeEventActionType.UPDATED); - } - - return savedCustomer; - } catch (Exception e) { - - logEntityAction(emptyId(EntityType.CUSTOMER), customer, - null, customer.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e); - - throw handleException(e); - } - } + return tbCustomerService.save(customer, getCurrentUser()); + } @ApiOperation(value = "Delete Customer (deleteCustomer)", notes = "Deletes the Customer and all customer Users. " + From bc71c22e6216c0b43c388dbb8454bed2bb5f4103 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Thu, 12 May 2022 09:00:30 +0300 Subject: [PATCH 3/5] refactoring: - Customer controller - fix bug: org.thingsboard.server.edge.sql.EdgeSqlTest.testAlarms(org.thingsboard.server.edge.sql.EdgeSqlTest) --- .../DefaultTbNotificationEntityService.java | 25 ++++++++++++++----- .../entitiy/TbNotificationEntityService.java | 3 ++- .../entitiy/alarm/DefaultTbAlarmService.java | 4 +-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java index b5595ec552..c18e175fe2 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java @@ -71,9 +71,9 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS public void notifyDeleteEntity(TenantId tenantId, I entityId, E entity, CustomerId customerId, ActionType actionType, List relatedEdgeIds, - SecurityUser user, boolean isBody, Object... additionalInfo) { + SecurityUser user, Object... additionalInfo) { logEntityAction(tenantId, entityId, entity, customerId, actionType, user, additionalInfo); - sendDeleteNotificationMsg(tenantId, entityId, entity, relatedEdgeIds, isBody); + sendDeleteNotificationMsg(tenantId, entityId, entity, relatedEdgeIds); } @Override @@ -194,10 +194,16 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS sendEntityNotificationMsg(alarm.getTenantId(), alarm.getId(), edgeTypeByActionType (actionType)); } + @Override + public void notifyDeleteAlarm(Alarm alarm, SecurityUser user, List relatedEdgeIds) { + logEntityAction(alarm.getTenantId(), alarm.getOriginator(), alarm, alarm.getCustomerId(), ActionType.ALARM_DELETE, user, null); + sendAlarmDeleteNotificationMsg(alarm, relatedEdgeIds); + } + @Override public void notifyDeleteCustomer(Customer customer, SecurityUser user, List edgeIds) { logEntityAction(customer.getTenantId(), customer.getId(), customer, customer.getId(), ActionType.DELETED, user, null); - sendDeleteNotificationMsg(customer.getTenantId(), customer.getId(), customer, edgeIds, false); + sendDeleteNotificationMsg(customer.getTenantId(), customer.getId(), customer, edgeIds); tbClusterService.broadcastEntityStateChangeEvent(customer.getTenantId(), customer.getId(), ComponentLifecycleEvent.DELETED); } @@ -227,15 +233,22 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS } } - protected void sendDeleteNotificationMsg(TenantId tenantId, I entityId, E entity, List edgeIds, boolean isBody) { + protected void sendDeleteNotificationMsg(TenantId tenantId, I entityId, E entity, List edgeIds) { try { - String body = isBody ? json.writeValueAsString(entity) : null; - sendDeleteNotificationMsg(tenantId, entityId, edgeIds, body); + sendDeleteNotificationMsg(tenantId, entityId, edgeIds, null); } catch (Exception e) { log.warn("Failed to push delete " + entity.getClass().getName() + " msg to core: {}", entity, e); } } + protected void sendAlarmDeleteNotificationMsg(Alarm alarm, List relatedEdgeIds) { + try { + sendDeleteNotificationMsg(alarm.getTenantId(), alarm.getId(), relatedEdgeIds, json.writeValueAsString(alarm)); + } catch (Exception e) { + log.warn("Failed to push delete alarm msg to core: {}", alarm, e); + } + } + private void sendDeleteNotificationMsg(TenantId tenantId, EntityId entityId, List edgeIds, String body) { if (edgeIds != null && !edgeIds.isEmpty()) { for (EdgeId edgeId : edgeIds) { diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java index ed416ca051..43c0cc4c91 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java @@ -46,7 +46,7 @@ public interface TbNotificationEntityService { void notifyDeleteEntity(TenantId tenantId, I entityId, E entity, CustomerId customerId, ActionType actionType, List relatedEdgeIds, SecurityUser user, - boolean isBody, Object... additionalInfo); + Object... additionalInfo); void notifyAssignOrUnassignEntityToCustomer(TenantId tenantId, I entityId, CustomerId customerId, E entity, @@ -81,6 +81,7 @@ public interface TbNotificationEntityService { void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo); + void notifyDeleteAlarm(Alarm alarm, SecurityUser user, List relatedEdgeIds); void notifyDeleteCustomer(Customer customer, SecurityUser user, List relatedEdgeIds); } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java index 22196c4984..1fb19e3c41 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/DefaultTbAlarmService.java @@ -15,7 +15,6 @@ */ package org.thingsboard.server.service.entitiy.alarm; - import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.EntityType; @@ -79,8 +78,7 @@ public class DefaultTbAlarmService extends AbstractTbEntityService implements Tb @Override public Boolean delete(Alarm alarm, SecurityUser user) throws ThingsboardException { List relatedEdgeIds = findRelatedEdgeIds(alarm.getTenantId(), alarm.getOriginator()); - notificationEntityService.notifyDeleteEntity(alarm.getTenantId(), alarm.getOriginator(), alarm, alarm.getCustomerId(), - ActionType.ALARM_DELETE, relatedEdgeIds, user, true); + notificationEntityService.notifyDeleteAlarm(alarm, user, relatedEdgeIds); return alarmService.deleteAlarm(alarm.getTenantId(), alarm.getId()).isSuccessful(); } } From 23485d43b0add13a2704f984006d8f13e8f7db1d Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Thu, 12 May 2022 15:23:04 +0300 Subject: [PATCH 4/5] Improvements to TbCustomerService --- .../server/controller/CustomerController.java | 30 +++++-------------- .../entitiy/SimpleTbEntityService.java | 25 ++++++++++++++++ .../service/entitiy/alarm/TbAlarmService.java | 12 ++++---- .../service/entitiy/asset/TbAssetService.java | 4 +-- .../entitiy/customer/TbCustomerService.java | 7 +++-- .../entitiy/tenant/TbTenantService.java | 2 ++ 6 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/service/entitiy/SimpleTbEntityService.java diff --git a/application/src/main/java/org/thingsboard/server/controller/CustomerController.java b/application/src/main/java/org/thingsboard/server/controller/CustomerController.java index 8d67fe02de..2fc4b7c748 100644 --- a/application/src/main/java/org/thingsboard/server/controller/CustomerController.java +++ b/application/src/main/java/org/thingsboard/server/controller/CustomerController.java @@ -149,10 +149,10 @@ public class CustomerController extends BaseController { @RequestMapping(value = "/customer", method = RequestMethod.POST) @ResponseBody public Customer saveCustomer(@ApiParam(value = "A JSON value representing the customer.") @RequestBody Customer customer) throws ThingsboardException { - customer.setTenantId(getCurrentUser().getTenantId()); - checkEntity(customer.getId(), customer, Resource.CUSTOMER); - return tbCustomerService.save(customer, getCurrentUser()); - } + customer.setTenantId(getCurrentUser().getTenantId()); + checkEntity(customer.getId(), customer, Resource.CUSTOMER); + return tbCustomerService.save(customer, getCurrentUser()); + } @ApiOperation(value = "Delete Customer (deleteCustomer)", notes = "Deletes the Customer and all customer Users. " + @@ -164,27 +164,11 @@ public class CustomerController extends BaseController { public void deleteCustomer(@ApiParam(value = CUSTOMER_ID_PARAM_DESCRIPTION) @PathVariable(CUSTOMER_ID) String strCustomerId) throws ThingsboardException { checkParameter(CUSTOMER_ID, strCustomerId); + CustomerId customerId = new CustomerId(toUUID(strCustomerId)); + Customer customer = checkCustomerId(customerId, Operation.DELETE); try { - CustomerId customerId = new CustomerId(toUUID(strCustomerId)); - Customer customer = checkCustomerId(customerId, Operation.DELETE); - - List relatedEdgeIds = findRelatedEdgeIds(getTenantId(), customerId); - - customerService.deleteCustomer(getTenantId(), customerId); - - logEntityAction(customerId, customer, - customer.getId(), - ActionType.DELETED, null, strCustomerId); - - sendDeleteNotificationMsg(getTenantId(), customerId, relatedEdgeIds); - tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), customerId, ComponentLifecycleEvent.DELETED); + tbCustomerService.delete(customer, getCurrentUser()); } catch (Exception e) { - - logEntityAction(emptyId(EntityType.CUSTOMER), - null, - null, - ActionType.DELETED, e, strCustomerId); - throw handleException(e); } } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/SimpleTbEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/SimpleTbEntityService.java new file mode 100644 index 0000000000..84f5658015 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/SimpleTbEntityService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.entitiy; + +import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.service.security.model.SecurityUser; + +public interface SimpleTbEntityService { + + T save(T entity, SecurityUser user) throws ThingsboardException; + +} diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/TbAlarmService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/TbAlarmService.java index 7c3613d730..f73e44bc69 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/TbAlarmService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/alarm/TbAlarmService.java @@ -16,16 +16,16 @@ package org.thingsboard.server.service.entitiy.alarm; import org.thingsboard.server.common.data.alarm.Alarm; +import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.service.entitiy.SimpleTbEntityService; import org.thingsboard.server.service.security.model.SecurityUser; -public interface TbAlarmService { +public interface TbAlarmService extends SimpleTbEntityService { - Alarm save (Alarm alarm, SecurityUser user) throws ThingsboardException; + void ack(Alarm alarm, SecurityUser user) throws ThingsboardException; - void ack (Alarm alarm, SecurityUser user) throws ThingsboardException; + void clear(Alarm alarm, SecurityUser user) throws ThingsboardException; - void clear (Alarm alarm, SecurityUser user) throws ThingsboardException; - - Boolean delete (Alarm alarm, SecurityUser user) throws ThingsboardException; + Boolean delete(Alarm alarm, SecurityUser user) throws ThingsboardException; } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/asset/TbAssetService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/asset/TbAssetService.java index c6e1c29940..3bc6dadf3d 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/asset/TbAssetService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/asset/TbAssetService.java @@ -22,10 +22,10 @@ import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.service.entitiy.SimpleTbEntityService; import org.thingsboard.server.service.security.model.SecurityUser; -public interface TbAssetService { - Asset save(Asset asset, SecurityUser user) throws ThingsboardException; +public interface TbAssetService extends SimpleTbEntityService { ListenableFuture delete(Asset asset, SecurityUser user) throws ThingsboardException; diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java index 36c5a0c0ed..f34d796db1 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/customer/TbCustomerService.java @@ -17,10 +17,11 @@ package org.thingsboard.server.service.entitiy.customer; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.service.entitiy.SimpleTbEntityService; import org.thingsboard.server.service.security.model.SecurityUser; -public interface TbCustomerService { - Customer save (Customer customer, SecurityUser user) throws ThingsboardException; +public interface TbCustomerService extends SimpleTbEntityService { + + void delete(Customer customer, SecurityUser user) throws ThingsboardException; - void delete (Customer customer, SecurityUser user) throws ThingsboardException; } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/tenant/TbTenantService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/tenant/TbTenantService.java index 0ff7bc749c..c265b568cb 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/tenant/TbTenantService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/tenant/TbTenantService.java @@ -19,7 +19,9 @@ import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.exception.ThingsboardException; public interface TbTenantService { + Tenant save(Tenant tenant) throws ThingsboardException; void delete(Tenant tenant) throws ThingsboardException; + } From adc675822e3dc14d8f0f9fae03886303182588f0 Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Thu, 12 May 2022 16:40:38 +0300 Subject: [PATCH 5/5] Remove redundant test that breaks every time someone adds new service --- .../server/actors/ActorSystemContextTest.java | 257 ------------------ 1 file changed, 257 deletions(-) delete mode 100644 application/src/test/java/org/thingsboard/server/actors/ActorSystemContextTest.java diff --git a/application/src/test/java/org/thingsboard/server/actors/ActorSystemContextTest.java b/application/src/test/java/org/thingsboard/server/actors/ActorSystemContextTest.java deleted file mode 100644 index ae1398f3a3..0000000000 --- a/application/src/test/java/org/thingsboard/server/actors/ActorSystemContextTest.java +++ /dev/null @@ -1,257 +0,0 @@ -/** - * Copyright © 2016-2022 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.thingsboard.server.actors; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.thingsboard.rule.engine.api.MailService; -import org.thingsboard.rule.engine.api.SmsService; -import org.thingsboard.rule.engine.api.sms.SmsSenderFactory; -import org.thingsboard.server.actors.service.ActorService; -import org.thingsboard.server.cluster.TbClusterService; -import org.thingsboard.server.common.transport.util.DataDecodingEncodingService; -import org.thingsboard.server.dao.asset.AssetService; -import org.thingsboard.server.dao.attributes.AttributesService; -import org.thingsboard.server.dao.audit.AuditLogService; -import org.thingsboard.server.dao.cassandra.CassandraCluster; -import org.thingsboard.server.dao.customer.CustomerService; -import org.thingsboard.server.dao.dashboard.DashboardService; -import org.thingsboard.server.dao.device.ClaimDevicesService; -import org.thingsboard.server.dao.device.DeviceService; -import org.thingsboard.server.dao.edge.EdgeEventService; -import org.thingsboard.server.dao.edge.EdgeService; -import org.thingsboard.server.dao.entityview.EntityViewService; -import org.thingsboard.server.dao.event.EventService; -import org.thingsboard.server.dao.nosql.CassandraBufferedRateReadExecutor; -import org.thingsboard.server.dao.nosql.CassandraBufferedRateWriteExecutor; -import org.thingsboard.server.dao.ota.OtaPackageService; -import org.thingsboard.server.dao.relation.RelationService; -import org.thingsboard.server.dao.resource.ResourceService; -import org.thingsboard.server.dao.rule.RuleChainService; -import org.thingsboard.server.dao.rule.RuleNodeStateService; -import org.thingsboard.server.dao.tenant.TbTenantProfileCache; -import org.thingsboard.server.dao.tenant.TenantProfileService; -import org.thingsboard.server.dao.tenant.TenantService; -import org.thingsboard.server.dao.timeseries.TimeseriesService; -import org.thingsboard.server.dao.user.UserService; -import org.thingsboard.server.queue.discovery.PartitionService; -import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; -import org.thingsboard.server.queue.usagestats.TbApiUsageClient; -import org.thingsboard.server.service.apiusage.TbApiUsageStateService; -import org.thingsboard.server.service.component.ComponentDiscoveryService; -import org.thingsboard.server.service.edge.rpc.EdgeRpcService; -import org.thingsboard.server.service.executors.DbCallbackExecutorService; -import org.thingsboard.server.service.executors.ExternalCallExecutorService; -import org.thingsboard.server.service.executors.SharedEventLoopGroupService; -import org.thingsboard.server.service.mail.MailExecutorService; -import org.thingsboard.server.service.profile.TbDeviceProfileCache; -import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService; -import org.thingsboard.server.service.rpc.TbRpcService; -import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService; -import org.thingsboard.server.service.script.JsInvokeService; -import org.thingsboard.server.service.session.DeviceSessionCacheService; -import org.thingsboard.server.service.sms.SmsExecutorService; -import org.thingsboard.server.service.state.DeviceStateService; -import org.thingsboard.server.service.telemetry.AlarmSubscriptionService; -import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; -import org.thingsboard.server.service.transport.TbCoreToTransportService; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = ActorSystemContext.class) -@EnableConfigurationProperties -@TestPropertySource(properties = { - "cache.type=caffeine", -}) -public class ActorSystemContextTest { - - @Autowired - ActorSystemContext ctx; - - @MockBean - private TbApiUsageStateService apiUsageStateService; - - @MockBean - private TbApiUsageClient apiUsageClient; - - @MockBean - private TbServiceInfoProvider serviceInfoProvider; - - @MockBean - private ActorService actorService; - - @MockBean - private ComponentDiscoveryService componentService; - - @MockBean - private DataDecodingEncodingService encodingService; - - @MockBean - private DeviceService deviceService; - - @MockBean - private TbTenantProfileCache tenantProfileCache; - - @MockBean - private TbDeviceProfileCache deviceProfileCache; - - @MockBean - private AssetService assetService; - - @MockBean - private DashboardService dashboardService; - - @MockBean - private TenantService tenantService; - - @MockBean - private TenantProfileService tenantProfileService; - - @MockBean - private CustomerService customerService; - - @MockBean - private UserService userService; - - @MockBean - private RuleChainService ruleChainService; - - @MockBean - private RuleNodeStateService ruleNodeStateService; - - @MockBean - private PartitionService partitionService; - - @MockBean - private TbClusterService clusterService; - - @MockBean - private TimeseriesService tsService; - - @MockBean - private AttributesService attributesService; - - @MockBean - private EventService eventService; - - @MockBean - private RelationService relationService; - - @MockBean - private AuditLogService auditLogService; - - @MockBean - private EntityViewService entityViewService; - - @MockBean - private TelemetrySubscriptionService tsSubService; - - @MockBean - private AlarmSubscriptionService alarmService; - - @MockBean - private JsInvokeService jsSandbox; - - @MockBean - private MailExecutorService mailExecutor; - - @MockBean - private SmsExecutorService smsExecutor; - - @MockBean - private DbCallbackExecutorService dbCallbackExecutor; - - @MockBean - private ExternalCallExecutorService externalCallExecutorService; - - @MockBean - private SharedEventLoopGroupService sharedEventLoopGroupService; - - @MockBean - private MailService mailService; - - @MockBean - private SmsService smsService; - - @MockBean - private SmsSenderFactory smsSenderFactory; - - @MockBean - private ClaimDevicesService claimDevicesService; - - @MockBean - private JsInvokeStats jsInvokeStats; - - @MockBean - private DeviceStateService deviceStateService; - - @MockBean - private DeviceSessionCacheService deviceSessionCacheService; - - @MockBean - private TbCoreToTransportService tbCoreToTransportService; - - @MockBean - private TbRuleEngineDeviceRpcService tbRuleEngineDeviceRpcService; - - @MockBean - private TbCoreDeviceRpcService tbCoreDeviceRpcService; - - @MockBean - private EdgeService edgeService; - - @MockBean - private EdgeEventService edgeEventService; - - @MockBean - private EdgeRpcService edgeRpcService; - - @MockBean - private ResourceService resourceService; - - @MockBean - private OtaPackageService otaPackageService; - - @MockBean - private TbRpcService tbRpcService; - - @MockBean - private CassandraCluster cassandraCluster; - - @MockBean - private CassandraBufferedRateReadExecutor cassandraBufferedRateReadExecutor; - - @MockBean - private CassandraBufferedRateWriteExecutor cassandraBufferedRateWriteExecutor; - - @MockBean - private RedisTemplate redisTemplate; - - @Test - void givenCaffeineCache_whenInit_thenIsLocalCacheTrue() { - assertThat(ctx.getCacheType()).isEqualTo("caffeine"); - assertThat(ctx.isLocalCacheType()).as("caffeine is the local cache type").isTrue(); - } - -}