Commit fea2f8799ed2a86b062e038c3013aa1ebc3b57b3

Authored by Dima Landiak
Committed by Andrew Shvayka
1 parent 287e4bbb

improvements

... ... @@ -595,11 +595,11 @@ public abstract class BaseController {
595 595 case ALARM_CLEAR:
596 596 msgType = DataConstants.ALARM_CLEAR;
597 597 break;
598   - case SWAPPED_FROM_TENANT:
599   - msgType = DataConstants.ENTITY_SWAPPED_FROM;
  598 + case ASSIGNED_FROM_TENANT:
  599 + msgType = DataConstants.ENTITY_ASSIGNED_FROM_TENANT;
600 600 break;
601   - case SWAPPED_TO_TENANT:
602   - msgType = DataConstants.ENTITY_SWAPPED_TO;
  601 + case ASSIGNED_TO_TENANT:
  602 + msgType = DataConstants.ENTITY_ASSIGNED_TO_TENANT;
603 603 break;
604 604 }
605 605 if (!StringUtils.isEmpty(msgType)) {
... ... @@ -620,16 +620,16 @@ public abstract class BaseController {
620 620 String strCustomerName = extractParameter(String.class, 2, additionalInfo);
621 621 metaData.putValue("unassignedCustomerId", strCustomerId);
622 622 metaData.putValue("unassignedCustomerName", strCustomerName);
623   - } else if (actionType == ActionType.SWAPPED_FROM_TENANT) {
  623 + } else if (actionType == ActionType.ASSIGNED_FROM_TENANT) {
624 624 String strTenantId = extractParameter(String.class, 0, additionalInfo);
625 625 String strTenantName = extractParameter(String.class, 1, additionalInfo);
626   - metaData.putValue("swappedFromTenantId", strTenantId);
627   - metaData.putValue("swappedFromTenantName", strTenantName);
628   - } else if (actionType == ActionType.SWAPPED_TO_TENANT) {
  626 + metaData.putValue("assignedFromTenantId", strTenantId);
  627 + metaData.putValue("assignedFromTenantName", strTenantName);
  628 + } else if (actionType == ActionType.ASSIGNED_TO_TENANT) {
629 629 String strTenantId = extractParameter(String.class, 0, additionalInfo);
630 630 String strTenantName = extractParameter(String.class, 1, additionalInfo);
631   - metaData.putValue("swappedToTenantId", strTenantId);
632   - metaData.putValue("swappedToTenantName", strTenantName);
  631 + metaData.putValue("assignedToTenantId", strTenantId);
  632 + metaData.putValue("assignedToTenantName", strTenantName);
633 633 }
634 634 ObjectNode entityNode;
635 635 if (entity != null) {
... ...
... ... @@ -491,13 +491,13 @@ public class DeviceController extends BaseController {
491 491 @PreAuthorize("hasAuthority('TENANT_ADMIN')")
492 492 @RequestMapping(value = "/tenant/{tenantId}/device/{deviceId}", method = RequestMethod.POST)
493 493 @ResponseBody
494   - public Device swapDevice(@PathVariable(TENANT_ID) String strTenantId,
495   - @PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
  494 + public Device assignDeviceToTenant(@PathVariable(TENANT_ID) String strTenantId,
  495 + @PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
496 496 checkParameter(TENANT_ID, strTenantId);
497 497 checkParameter(DEVICE_ID, strDeviceId);
498 498 try {
499 499 DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
500   - Device device = checkDeviceId(deviceId, Operation.WRITE);
  500 + Device device = checkDeviceId(deviceId, Operation.ASSIGN_TO_TENANT);
501 501
502 502 TenantId newTenantId = new TenantId(toUUID(strTenantId));
503 503 Tenant newTenant = tenantService.findTenantById(newTenantId);
... ... @@ -505,36 +505,36 @@ public class DeviceController extends BaseController {
505 505 throw new ThingsboardException("Could not find the specified Tenant!", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
506 506 }
507 507
508   - Device swappedDevice = deviceService.swapDevice(newTenantId, device);
  508 + Device assignedDevice = deviceService.assignDeviceToTenant(newTenantId, device);
509 509
510   - logEntityAction(getCurrentUser(), deviceId, swappedDevice,
511   - swappedDevice.getCustomerId(),
512   - ActionType.SWAPPED_TO_TENANT, null, strTenantId, newTenant.getName());
  510 + logEntityAction(getCurrentUser(), deviceId, assignedDevice,
  511 + assignedDevice.getCustomerId(),
  512 + ActionType.ASSIGNED_TO_TENANT, null, strTenantId, newTenant.getName());
513 513
514 514 Tenant currentTenant = tenantService.findTenantById(getTenantId());
515   - pushSwappedFromNotification(currentTenant, newTenantId, swappedDevice);
  515 + pushAssignedFromNotification(currentTenant, newTenantId, assignedDevice);
516 516
517   - return swappedDevice;
  517 + return assignedDevice;
518 518 } catch (Exception e) {
519 519 logEntityAction(getCurrentUser(), emptyId(EntityType.DEVICE), null,
520 520 null,
521   - ActionType.SWAPPED_TO_TENANT, e, strTenantId);
  521 + ActionType.ASSIGNED_TO_TENANT, e, strTenantId);
522 522 throw handleException(e);
523 523 }
524 524 }
525 525
526   - private void pushSwappedFromNotification(Tenant currentTenant, TenantId newTenantId, Device swappedDevice) {
527   - String data = entityToStr(swappedDevice);
  526 + private void pushAssignedFromNotification(Tenant currentTenant, TenantId newTenantId, Device assignedDevice) {
  527 + String data = entityToStr(assignedDevice);
528 528 if (data != null) {
529   - TbMsg tbMsg = TbMsg.newMsg(DataConstants.ENTITY_SWAPPED_FROM, swappedDevice.getId(), getMetaDataForSwappedFrom(currentTenant), TbMsgDataType.JSON, data);
530   - tbClusterService.pushMsgToRuleEngine(newTenantId, swappedDevice.getId(), tbMsg, null);
  529 + TbMsg tbMsg = TbMsg.newMsg(DataConstants.ENTITY_ASSIGNED_FROM_TENANT, assignedDevice.getId(), getMetaDataForAssignedFrom(currentTenant), TbMsgDataType.JSON, data);
  530 + tbClusterService.pushMsgToRuleEngine(newTenantId, assignedDevice.getId(), tbMsg, null);
531 531 }
532 532 }
533 533
534   - private TbMsgMetaData getMetaDataForSwappedFrom(Tenant tenant) {
  534 + private TbMsgMetaData getMetaDataForAssignedFrom(Tenant tenant) {
535 535 TbMsgMetaData metaData = new TbMsgMetaData();
536   - metaData.putValue("swappedFromTenantId", tenant.getId().getId().toString());
537   - metaData.putValue("swappedFromTenantName", tenant.getName());
  536 + metaData.putValue("assignedFromTenantId", tenant.getId().getId().toString());
  537 + metaData.putValue("assignedFromTenantName", tenant.getName());
538 538 return metaData;
539 539 }
540 540 }
... ...
... ... @@ -18,6 +18,6 @@ package org.thingsboard.server.service.security.permission;
18 18 public enum Operation {
19 19
20 20 ALL, CREATE, READ, WRITE, DELETE, ASSIGN_TO_CUSTOMER, UNASSIGN_FROM_CUSTOMER, RPC_CALL,
21   - READ_CREDENTIALS, WRITE_CREDENTIALS, READ_ATTRIBUTES, WRITE_ATTRIBUTES, READ_TELEMETRY, WRITE_TELEMETRY, CLAIM_DEVICES
  21 + READ_CREDENTIALS, WRITE_CREDENTIALS, READ_ATTRIBUTES, WRITE_ATTRIBUTES, READ_TELEMETRY, WRITE_TELEMETRY, CLAIM_DEVICES, ASSIGN_TO_TENANT
22 22
23 23 }
... ...
... ... @@ -800,7 +800,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
800 800 }
801 801
802 802 @Test
803   - public void testSwapDeviceFromOneTenantToAnother() throws Exception {
  803 + public void testAssignDeviceToTenant() throws Exception {
804 804 Device device = new Device();
805 805 device.setName("My device");
806 806 device.setType("default");
... ... @@ -816,7 +816,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
816 816 relation.setTo(savedAnotherDevice.getId());
817 817 relation.setTypeGroup(RelationTypeGroup.COMMON);
818 818 relation.setType("Contains");
819   - doPost("/api/relation", relation);
  819 + doPost("/api/relation", relation).andExpect(status().isOk());
820 820
821 821 loginSysAdmin();
822 822 Tenant tenant = new Tenant();
... ... @@ -834,13 +834,13 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
834 834 createUserAndLogin(user, "testPassword1");
835 835
836 836 login("tenant2@thingsboard.org", "testPassword1");
837   - Device swappedDevice = doPost("/api/tenant/" + savedDifferentTenant.getId().getId() + "/device/" + savedDevice.getId().getId(), Device.class);
  837 + Device assignedDevice = doPost("/api/tenant/" + savedDifferentTenant.getId().getId() + "/device/" + savedDevice.getId().getId(), Device.class);
838 838
839   - doGet("/api/device/" + swappedDevice.getId().getId().toString(), Device.class, status().isNotFound());
  839 + doGet("/api/device/" + assignedDevice.getId().getId().toString(), Device.class, status().isNotFound());
840 840
841 841 login("tenant9@thingsboard.org", "testPassword1");
842 842
843   - Device foundDevice1 = doGet("/api/device/" + swappedDevice.getId().getId().toString(), Device.class);
  843 + Device foundDevice1 = doGet("/api/device/" + assignedDevice.getId().getId().toString(), Device.class);
844 844 Assert.assertNotNull(foundDevice1);
845 845
846 846 doGet("/api/relation?fromId=" + savedDevice.getId().getId() + "&fromType=DEVICE&relationType=Contains&toId=" + savedAnotherDevice.getId().getId() + "&toType=DEVICE", EntityRelation.class, status().isNotFound());
... ...
... ... @@ -47,6 +47,4 @@ public interface AuditLogService {
47 47 E entity,
48 48 ActionType actionType,
49 49 Exception e, Object... additionalInfo);
50   -
51   - void removeAuditLogs(TenantId tenantId, EntityId entityId);
52 50 }
... ...
... ... @@ -65,6 +65,6 @@ public interface DeviceService {
65 65
66 66 ListenableFuture<List<EntitySubtype>> findDeviceTypesByTenantId(TenantId tenantId);
67 67
68   - Device swapDevice(TenantId tenantId, Device device);
  68 + Device assignDeviceToTenant(TenantId tenantId, Device device);
69 69
70 70 }
... ...
... ... @@ -57,8 +57,8 @@ public class DataConstants {
57 57 public static final String ATTRIBUTES_DELETED = "ATTRIBUTES_DELETED";
58 58 public static final String ALARM_ACK = "ALARM_ACK";
59 59 public static final String ALARM_CLEAR = "ALARM_CLEAR";
60   - public static final String ENTITY_SWAPPED_FROM = "ENTITY_SWAPPED_FROM";
61   - public static final String ENTITY_SWAPPED_TO = "ENTITY_SWAPPED_TO";
  60 + public static final String ENTITY_ASSIGNED_FROM_TENANT = "ENTITY_ASSIGNED_FROM_TENANT";
  61 + public static final String ENTITY_ASSIGNED_TO_TENANT = "ENTITY_ASSIGNED_TO_TENANT";
62 62
63 63 public static final String RPC_CALL_FROM_SERVER_TO_DEVICE = "RPC_CALL_FROM_SERVER_TO_DEVICE";
64 64
... ...
... ... @@ -41,8 +41,8 @@ public enum ActionType {
41 41 LOGIN(false),
42 42 LOGOUT(false),
43 43 LOCKOUT(false),
44   - SWAPPED_FROM_TENANT(false),
45   - SWAPPED_TO_TENANT(false);
  44 + ASSIGNED_FROM_TENANT(false),
  45 + ASSIGNED_TO_TENANT(false);
46 46
47 47 private final boolean isRead;
48 48
... ...
... ... @@ -159,7 +159,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
159 159 try {
160 160 List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(asset.getTenantId(), assetId).get();
161 161 if (entityViews != null && !entityViews.isEmpty()) {
162   - throw new DataValidationException("Can't delete asset that is assigned to entity views!");
  162 + throw new DataValidationException("Can't delete asset that has entity views!");
163 163 }
164 164 } catch (ExecutionException | InterruptedException e) {
165 165 log.error("Exception while finding entity views for assetId [{}]", assetId, e);
... ...
... ... @@ -51,8 +51,6 @@ import org.thingsboard.server.dao.service.DataValidator;
51 51
52 52 import java.io.PrintWriter;
53 53 import java.io.StringWriter;
54   -import java.util.ArrayList;
55   -import java.util.Arrays;
56 54 import java.util.List;
57 55
58 56 import static org.thingsboard.server.dao.service.Validator.validateEntityId;
... ... @@ -158,25 +156,6 @@ public class AuditLogServiceImpl implements AuditLogService {
158 156 }
159 157 }
160 158
161   - @Override
162   - public void removeAuditLogs(TenantId tenantId, EntityId entityId) {
163   - List<AuditLog> auditLogs = new ArrayList<>();
164   - TimePageData<AuditLog> auditLogPageData;
165   - TimePageLink auditLogPageLink = new TimePageLink(1000);
166   - do {
167   - auditLogPageData = findAuditLogsByTenantIdAndEntityId(tenantId, entityId,
168   - new ArrayList<>(Arrays.asList(ActionType.values())), auditLogPageLink);
169   - auditLogs.addAll(auditLogPageData.getData());
170   - if (auditLogPageData.hasNext()) {
171   - auditLogPageLink = auditLogPageData.getNextPageLink();
172   - }
173   - } while (auditLogPageData.hasNext());
174   -
175   - for (AuditLog auditLog : auditLogs) {
176   - auditLogDao.removeById(tenantId, auditLog.getUuidId());
177   - }
178   - }
179   -
180 159 private <E extends HasName, I extends EntityId> JsonNode constructActionData(I entityId, E entity,
181 160 ActionType actionType,
182 161 Object... additionalInfo) {
... ... @@ -187,7 +166,7 @@ public class AuditLogServiceImpl implements AuditLogService {
187 166 case ALARM_ACK:
188 167 case ALARM_CLEAR:
189 168 case RELATIONS_DELETED:
190   - case SWAPPED_TO_TENANT:
  169 + case ASSIGNED_TO_TENANT:
191 170 if (entity != null) {
192 171 ObjectNode entityNode = objectMapper.valueToTree(entity);
193 172 if (entityId.getEntityType() == EntityType.DASHBOARD) {
... ...
... ... @@ -58,8 +58,4 @@ public class DummyAuditLogServiceImpl implements AuditLogService {
58 58 public <E extends HasName, I extends EntityId> ListenableFuture<List<Void>> logEntityAction(TenantId tenantId, CustomerId customerId, UserId userId, String userName, I entityId, E entity, ActionType actionType, Exception e, Object... additionalInfo) {
59 59 return null;
60 60 }
61   -
62   - @Override
63   - public void removeAuditLogs(TenantId tenantId, EntityId entityId) {
64   - }
65 61 }
... ...
... ... @@ -48,7 +48,6 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
48 48 import org.thingsboard.server.common.data.relation.EntitySearchDirection;
49 49 import org.thingsboard.server.common.data.security.DeviceCredentials;
50 50 import org.thingsboard.server.common.data.security.DeviceCredentialsType;
51   -import org.thingsboard.server.dao.audit.AuditLogService;
52 51 import org.thingsboard.server.dao.customer.CustomerDao;
53 52 import org.thingsboard.server.dao.entity.AbstractEntityService;
54 53 import org.thingsboard.server.dao.entityview.EntityViewService;
... ... @@ -104,9 +103,6 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
104 103 @Autowired
105 104 private EventService eventService;
106 105
107   - @Autowired
108   - private AuditLogService auditLogService;
109   -
110 106 @Override
111 107 public Device findDeviceById(TenantId tenantId, DeviceId deviceId) {
112 108 log.trace("Executing findDeviceById [{}]", deviceId);
... ... @@ -201,7 +197,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
201 197 try {
202 198 List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), deviceId).get();
203 199 if (entityViews != null && !entityViews.isEmpty()) {
204   - throw new DataValidationException("Can't delete device that is assigned to entity views!");
  200 + throw new DataValidationException("Can't delete device that has entity views!");
205 201 }
206 202 } catch (ExecutionException | InterruptedException e) {
207 203 log.error("Exception while finding entity views for deviceId [{}]", deviceId, e);
... ... @@ -338,13 +334,13 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
338 334 @Transactional
339 335 @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}")
340 336 @Override
341   - public Device swapDevice(TenantId tenantId, Device device) {
342   - log.trace("Executing swapDevice [{}]", device);
  337 + public Device assignDeviceToTenant(TenantId tenantId, Device device) {
  338 + log.trace("Executing assignDeviceToTenant [{}][{}]", tenantId, device);
343 339
344 340 try {
345 341 List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), device.getId()).get();
346 342 if (!CollectionUtils.isEmpty(entityViews)) {
347   - throw new DataValidationException("Can't swap device that is assigned to entity views!");
  343 + throw new DataValidationException("Can't assign device that has entity views to another tenant!");
348 344 }
349 345 } catch (ExecutionException | InterruptedException e) {
350 346 log.error("Exception while finding entity views for deviceId [{}]", device.getId(), e);
... ... @@ -355,11 +351,6 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
355 351
356 352 relationService.removeRelations(device.getTenantId(), device.getId());
357 353
358   - // TODO: 30/07/2020 implement for Cassandra
359   - if (sqlDatabaseUsed) {
360   - auditLogService.removeAuditLogs(device.getTenantId(), device.getId());
361   - }
362   -
363 354 device.setTenantId(tenantId);
364 355 device.setCustomerId(null);
365 356 return doSaveDevice(device, null);
... ...
... ... @@ -28,7 +28,6 @@ import org.thingsboard.server.common.data.page.TimePageLink;
28 28 import org.thingsboard.server.dao.exception.DataValidationException;
29 29 import org.thingsboard.server.dao.service.DataValidator;
30 30
31   -import java.util.ArrayList;
32 31 import java.util.List;
33 32 import java.util.Optional;
34 33
... ... @@ -97,20 +96,17 @@ public class BaseEventService implements EventService {
97 96
98 97 @Override
99 98 public void removeEvents(TenantId tenantId, EntityId entityId) {
100   - List<Event> events = new ArrayList<>();
101 99 TimePageData<Event> eventPageData;
102 100 TimePageLink eventPageLink = new TimePageLink(1000);
103 101 do {
104 102 eventPageData = findEvents(tenantId, entityId, eventPageLink);
105   - events.addAll(eventPageData.getData());
  103 + for (Event event : eventPageData.getData()) {
  104 + eventDao.removeById(tenantId, event.getUuidId());
  105 + }
106 106 if (eventPageData.hasNext()) {
107 107 eventPageLink = eventPageData.getNextPageLink();
108 108 }
109 109 } while (eventPageData.hasNext());
110   -
111   - for (Event event : events) {
112   - eventDao.removeById(tenantId, event.getUuidId());
113   - }
114 110 }
115 111
116 112 private DataValidator<Event> eventValidator =
... ...
... ... @@ -35,7 +35,7 @@ import org.thingsboard.server.common.msg.session.SessionMsgType;
35 35 configClazz = EmptyNodeConfiguration.class,
36 36 relationTypes = {"Post attributes", "Post telemetry", "RPC Request from Device", "RPC Request to Device", "Activity Event", "Inactivity Event",
37 37 "Connect Event", "Disconnect Event", "Entity Created", "Entity Updated", "Entity Deleted", "Entity Assigned",
38   - "Entity Unassigned", "Attributes Updated", "Attributes Deleted", "Alarm Acknowledged", "Alarm Cleared", "Other", "Entity Swapped From", "Entity Swapped To"},
  38 + "Entity Unassigned", "Attributes Updated", "Attributes Deleted", "Alarm Acknowledged", "Alarm Cleared", "Other", "Entity Assigned From Tenant", "Entity Assigned To Tenant"},
39 39 nodeDescription = "Route incoming messages by Message Type",
40 40 nodeDetails = "Sends messages with message types <b>\"Post attributes\", \"Post telemetry\", \"RPC Request\"</b> etc. via corresponding chain, otherwise <b>Other</b> chain is used.",
41 41 uiResources = {"static/rulenode/rulenode-core-config.js"},
... ... @@ -86,10 +86,10 @@ public class TbMsgTypeSwitchNode implements TbNode {
86 86 relationType = "Alarm Cleared";
87 87 } else if (msg.getType().equals(DataConstants.RPC_CALL_FROM_SERVER_TO_DEVICE)) {
88 88 relationType = "RPC Request to Device";
89   - } else if (msg.getType().equals(DataConstants.ENTITY_SWAPPED_FROM)) {
90   - relationType = "Entity Swapped From";
91   - } else if (msg.getType().equals(DataConstants.ENTITY_SWAPPED_TO)) {
92   - relationType = "Entity Swapped To";
  89 + } else if (msg.getType().equals(DataConstants.ENTITY_ASSIGNED_FROM_TENANT)) {
  90 + relationType = "Entity Assigned From Tenant";
  91 + } else if (msg.getType().equals(DataConstants.ENTITY_ASSIGNED_TO_TENANT)) {
  92 + relationType = "Entity Assigned To Tenant";
93 93 } else {
94 94 relationType = "Other";
95 95 }
... ...
... ... @@ -223,11 +223,11 @@ export default angular.module('thingsboard.types', [])
223 223 "LOCKOUT": {
224 224 name: "audit-log.type-lockout"
225 225 },
226   - "SWAPPED_FROM_TENANT": {
227   - name: "audit-log.type-swapped-from-tenant"
  226 + "ASSIGNED_FROM_TENANT": {
  227 + name: "audit-log.type-assigned-from-tenant"
228 228 },
229   - "SWAPPED_TO_TENANT": {
230   - name: "audit-log.type-swapped-to-tenant"
  229 + "ASSIGNED_TO_TENANT": {
  230 + name: "audit-log.type-assigned-to-tenant"
231 231 }
232 232 },
233 233 auditLogActionStatus: {
... ...
... ... @@ -357,8 +357,8 @@
357 357 "failure-details": "Failure details",
358 358 "search": "Search audit logs",
359 359 "clear-search": "Clear search",
360   - "type-swapped-from-tenant": "Swapped from Tenant",
361   - "type-swapped-to-tenant": "Swapped to Tenant"
  360 + "type-assigned-from-tenant": "Assigned from Tenant",
  361 + "type-assigned-to-tenant": "Assigned to Tenant"
362 362 },
363 363 "confirm-on-exit": {
364 364 "message": "You have unsaved changes. Are you sure you want to leave this page?",
... ...