Add/delete users to edge on assign/unassign to/from customer
This commit is contained in:
parent
bf21ff3c09
commit
f29d15d8b7
@ -718,6 +718,14 @@ public abstract class BaseController {
|
||||
return result;
|
||||
}
|
||||
|
||||
protected void sendNotificationMsgToEdgeService(TenantId tenantId, EdgeId edgeId, CustomerId customerId, ActionType edgeEventAction) {
|
||||
try {
|
||||
sendNotificationMsgToEdgeService(tenantId, edgeId, null, json.writeValueAsString(customerId), EdgeEventType.EDGE, edgeEventAction);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to push assign/unassign to/from customer to core: {}", customerId, e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void sendNotificationMsgToEdgeService(TenantId tenantId, EntityRelation relation, ActionType edgeEventAction) {
|
||||
try {
|
||||
if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) &&
|
||||
|
||||
@ -178,6 +178,9 @@ public class EdgeController extends BaseController {
|
||||
savedEdge.getCustomerId(),
|
||||
ActionType.ASSIGNED_TO_CUSTOMER, null, strEdgeId, strCustomerId, customer.getName());
|
||||
|
||||
sendNotificationMsgToEdgeService(savedEdge.getTenantId(), savedEdge.getId(),
|
||||
customerId, ActionType.ASSIGNED_TO_CUSTOMER);
|
||||
|
||||
return savedEdge;
|
||||
} catch (Exception e) {
|
||||
logEntityAction(emptyId(EntityType.EDGE), null,
|
||||
@ -206,6 +209,9 @@ public class EdgeController extends BaseController {
|
||||
edge.getCustomerId(),
|
||||
ActionType.UNASSIGNED_FROM_CUSTOMER, null, strEdgeId, customer.getId().toString(), customer.getName());
|
||||
|
||||
sendNotificationMsgToEdgeService(savedEdge.getTenantId(), savedEdge.getId(),
|
||||
edge.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER);
|
||||
|
||||
return savedEdge;
|
||||
} catch (Exception e) {
|
||||
logEntityAction(emptyId(EntityType.EDGE), null,
|
||||
|
||||
@ -157,8 +157,9 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
|
||||
TenantId tenantId = new TenantId(new UUID(edgeNotificationMsg.getTenantIdMSB(), edgeNotificationMsg.getTenantIdLSB()));
|
||||
EdgeEventType edgeEventType = EdgeEventType.valueOf(edgeNotificationMsg.getEdgeEventType());
|
||||
switch (edgeEventType) {
|
||||
// TODO: voba - handle edge updates
|
||||
// case EDGE:
|
||||
case EDGE:
|
||||
processEdge(tenantId, edgeNotificationMsg);
|
||||
break;
|
||||
case USER:
|
||||
case ASSET:
|
||||
case DEVICE:
|
||||
@ -186,6 +187,44 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
private void processEdge(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
|
||||
// TODO: voba - handle edge updates
|
||||
try {
|
||||
ActionType edgeEventActionType = ActionType.valueOf(edgeNotificationMsg.getEdgeEventAction());
|
||||
EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
|
||||
switch (edgeEventActionType) {
|
||||
case ASSIGNED_TO_CUSTOMER:
|
||||
case UNASSIGNED_FROM_CUSTOMER:
|
||||
CustomerId customerId = mapper.readValue(edgeNotificationMsg.getEntityBody(), CustomerId.class);
|
||||
ListenableFuture<Edge> edgeFuture = edgeService.findEdgeByIdAsync(tenantId, edgeId);
|
||||
Futures.addCallback(edgeFuture, new FutureCallback<Edge>() {
|
||||
@Override
|
||||
public void onSuccess(@Nullable Edge edge) {
|
||||
if (edge != null && customerId != null && !EntityId.NULL_UUID.equals(customerId.getId())) {
|
||||
ActionType actionType = ActionType.ASSIGNED_TO_CUSTOMER.equals(edgeEventActionType) ? ActionType.ADDED : ActionType.DELETED;
|
||||
saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.CUSTOMER, actionType, customerId, null);
|
||||
TextPageData<User> pageData = userService.findCustomerUsers(tenantId, customerId, new TextPageLink(Integer.MAX_VALUE));
|
||||
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
|
||||
log.trace("[{}] [{}] user(s) are going to be {} to edge.", edge.getId(), pageData.getData().size(), actionType.name());
|
||||
for (User user : pageData.getData()) {
|
||||
saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.USER, actionType, user.getId(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable t) {
|
||||
log.error("Can't find edge by id [{}]", edgeNotificationMsg, t);
|
||||
}
|
||||
}, dbCallbackExecutorService);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Exception during processing edge event", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void processEntity(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
|
||||
ActionType edgeEventActionType = ActionType.valueOf(edgeNotificationMsg.getEdgeEventAction());
|
||||
EdgeEventType edgeEventType = EdgeEventType.valueOf(edgeNotificationMsg.getEdgeEventType());
|
||||
|
||||
@ -41,6 +41,7 @@ import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.edge.rpc.EdgeEventStorageSettings;
|
||||
import org.thingsboard.server.service.edge.rpc.constructor.AlarmUpdateMsgConstructor;
|
||||
import org.thingsboard.server.service.edge.rpc.constructor.AssetUpdateMsgConstructor;
|
||||
import org.thingsboard.server.service.edge.rpc.constructor.CustomerUpdateMsgConstructor;
|
||||
import org.thingsboard.server.service.edge.rpc.constructor.DashboardUpdateMsgConstructor;
|
||||
import org.thingsboard.server.service.edge.rpc.constructor.DeviceUpdateMsgConstructor;
|
||||
import org.thingsboard.server.service.edge.rpc.constructor.EntityDataMsgConstructor;
|
||||
@ -167,6 +168,10 @@ public class EdgeContextComponent {
|
||||
@Autowired
|
||||
private DashboardUpdateMsgConstructor dashboardUpdateMsgConstructor;
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CustomerUpdateMsgConstructor customerUpdateMsgConstructor;
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private UserUpdateMsgConstructor userUpdateMsgConstructor;
|
||||
|
||||
@ -30,6 +30,7 @@ import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.RandomStringUtils;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.thingsboard.server.common.data.Customer;
|
||||
import org.thingsboard.server.common.data.Dashboard;
|
||||
import org.thingsboard.server.common.data.DataConstants;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
@ -82,6 +83,7 @@ import org.thingsboard.server.gen.edge.AttributesRequestMsg;
|
||||
import org.thingsboard.server.gen.edge.ConnectRequestMsg;
|
||||
import org.thingsboard.server.gen.edge.ConnectResponseCode;
|
||||
import org.thingsboard.server.gen.edge.ConnectResponseMsg;
|
||||
import org.thingsboard.server.gen.edge.CustomerUpdateMsg;
|
||||
import org.thingsboard.server.gen.edge.DashboardUpdateMsg;
|
||||
import org.thingsboard.server.gen.edge.DeviceCredentialsRequestMsg;
|
||||
import org.thingsboard.server.gen.edge.DeviceCredentialsUpdateMsg;
|
||||
@ -348,6 +350,9 @@ public final class EdgeGrpcSession implements Closeable {
|
||||
case DASHBOARD:
|
||||
processDashboard(edgeEvent, msgType, edgeEventAction);
|
||||
break;
|
||||
case CUSTOMER:
|
||||
processCustomer(edgeEvent, msgType, edgeEventAction);
|
||||
break;
|
||||
case RULE_CHAIN:
|
||||
processRuleChain(edgeEvent, msgType, edgeEventAction);
|
||||
break;
|
||||
@ -510,6 +515,36 @@ public final class EdgeGrpcSession implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
private void processCustomer(EdgeEvent edgeEvent, UpdateMsgType msgType, ActionType edgeEventAction) {
|
||||
CustomerId customerId = new CustomerId(edgeEvent.getEntityId());
|
||||
EntityUpdateMsg entityUpdateMsg = null;
|
||||
switch (edgeEventAction) {
|
||||
case ADDED:
|
||||
case UPDATED:
|
||||
Customer customer = ctx.getCustomerService().findCustomerById(edgeEvent.getTenantId(), customerId);
|
||||
if (customer != null) {
|
||||
CustomerUpdateMsg customerUpdateMsg =
|
||||
ctx.getCustomerUpdateMsgConstructor().constructCustomerUpdatedMsg(msgType, customer);
|
||||
entityUpdateMsg = EntityUpdateMsg.newBuilder()
|
||||
.setCustomerUpdateMsg(customerUpdateMsg)
|
||||
.build();
|
||||
}
|
||||
break;
|
||||
case DELETED:
|
||||
CustomerUpdateMsg customerUpdateMsg =
|
||||
ctx.getCustomerUpdateMsgConstructor().constructCustomerDeleteMsg(customerId);
|
||||
entityUpdateMsg = EntityUpdateMsg.newBuilder()
|
||||
.setCustomerUpdateMsg(customerUpdateMsg)
|
||||
.build();
|
||||
break;
|
||||
}
|
||||
if (entityUpdateMsg != null) {
|
||||
outputStream.onNext(ResponseMsg.newBuilder()
|
||||
.setEntityUpdateMsg(entityUpdateMsg)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
private void processRuleChain(EdgeEvent edgeEvent, UpdateMsgType msgType, ActionType edgeEventAction) {
|
||||
RuleChainId ruleChainId = new RuleChainId(edgeEvent.getEntityId());
|
||||
EntityUpdateMsg entityUpdateMsg = null;
|
||||
|
||||
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright © 2016-2020 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.edge.rpc.constructor;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.thingsboard.server.common.data.Customer;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
import org.thingsboard.server.dao.util.mapping.JacksonUtil;
|
||||
import org.thingsboard.server.gen.edge.CustomerUpdateMsg;
|
||||
import org.thingsboard.server.gen.edge.UpdateMsgType;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CustomerUpdateMsgConstructor {
|
||||
|
||||
public CustomerUpdateMsg constructCustomerUpdatedMsg(UpdateMsgType msgType, Customer customer) {
|
||||
CustomerUpdateMsg.Builder builder = CustomerUpdateMsg.newBuilder()
|
||||
.setMsgType(msgType)
|
||||
.setIdMSB(customer.getId().getId().getMostSignificantBits())
|
||||
.setIdLSB(customer.getId().getId().getLeastSignificantBits())
|
||||
.setTitle(customer.getTitle());
|
||||
if (customer.getCountry() != null) {
|
||||
builder.setCountry(customer.getCountry());
|
||||
}
|
||||
if (customer.getState() != null) {
|
||||
builder.setState(customer.getState());
|
||||
}
|
||||
if (customer.getCity() != null) {
|
||||
builder.setCity(customer.getCity());
|
||||
}
|
||||
if (customer.getAddress() != null) {
|
||||
builder.setAddress(customer.getAddress());
|
||||
}
|
||||
if (customer.getAddress2() != null) {
|
||||
builder.setAddress2(customer.getAddress2());
|
||||
}
|
||||
if (customer.getZip() != null) {
|
||||
builder.setZip(customer.getZip());
|
||||
}
|
||||
if (customer.getPhone() != null) {
|
||||
builder.setPhone(customer.getPhone());
|
||||
}
|
||||
if (customer.getEmail() != null) {
|
||||
builder.setEmail(customer.getEmail());
|
||||
}
|
||||
if (customer.getAdditionalInfo() != null) {
|
||||
builder.setAdditionalInfo(JacksonUtil.toString(customer.getAdditionalInfo()));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public CustomerUpdateMsg constructCustomerDeleteMsg(CustomerId customerId) {
|
||||
return CustomerUpdateMsg.newBuilder()
|
||||
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE)
|
||||
.setIdMSB(customerId.getId().getMostSignificantBits())
|
||||
.setIdLSB(customerId.getId().getLeastSignificantBits()).build();
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,7 @@ package org.thingsboard.server.service.edge.rpc.constructor;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -42,7 +43,10 @@ public class EntityDataMsgConstructor {
|
||||
case TIMESERIES_UPDATED:
|
||||
try {
|
||||
JsonObject data = entityData.getAsJsonObject();
|
||||
long ts = data.getAsJsonPrimitive("ts").getAsLong();
|
||||
long ts = System.currentTimeMillis();
|
||||
if (data.get("ts") != null && !data.get("ts").isJsonNull()) {
|
||||
ts = data.getAsJsonObject("ts").getAsLong();
|
||||
}
|
||||
builder.setPostTelemetryMsg(JsonConverter.convertToTelemetryProto(data.getAsJsonObject("data"), ts));
|
||||
} catch (Exception e) {
|
||||
log.warn("Can't convert to telemetry proto, entityData [{}]", entityData, e);
|
||||
|
||||
@ -36,6 +36,10 @@ public class UserUpdateMsgConstructor {
|
||||
.setIdLSB(user.getId().getId().getLeastSignificantBits())
|
||||
.setEmail(user.getEmail())
|
||||
.setAuthority(user.getAuthority().name());
|
||||
if (user.getCustomerId() != null) {
|
||||
builder.setCustomerIdMSB(user.getCustomerId().getId().getMostSignificantBits());
|
||||
builder.setCustomerIdLSB(user.getCustomerId().getId().getLeastSignificantBits());
|
||||
}
|
||||
if (user.getFirstName() != null) {
|
||||
builder.setFirstName(user.getFirstName());
|
||||
}
|
||||
|
||||
@ -264,6 +264,7 @@ public class DefaultSyncEdgeService implements SyncEdgeService {
|
||||
TextPageData<User> pageData = userService.findTenantAdmins(edge.getTenantId(), new TextPageLink(Integer.MAX_VALUE));
|
||||
pushUsersToEdge(pageData, edge);
|
||||
if (edge.getCustomerId() != null && !EntityId.NULL_UUID.equals(edge.getCustomerId().getId())) {
|
||||
saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.CUSTOMER, ActionType.ADDED, edge.getCustomerId(), null);
|
||||
pageData = userService.findCustomerUsers(edge.getTenantId(), edge.getCustomerId(), new TextPageLink(Integer.MAX_VALUE));
|
||||
pushUsersToEdge(pageData, edge);
|
||||
}
|
||||
|
||||
@ -261,11 +261,13 @@ message UserUpdateMsg {
|
||||
UpdateMsgType msgType = 1;
|
||||
int64 idMSB = 2;
|
||||
int64 idLSB = 3;
|
||||
string email = 4;
|
||||
string authority = 5;
|
||||
string firstName = 6;
|
||||
string lastName = 7;
|
||||
string additionalInfo = 8;
|
||||
int64 customerIdMSB = 4;
|
||||
int64 customerIdLSB = 5;
|
||||
string email = 6;
|
||||
string authority = 7;
|
||||
string firstName = 8;
|
||||
string lastName = 9;
|
||||
string additionalInfo = 10;
|
||||
}
|
||||
|
||||
message WidgetsBundleUpdateMsg {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user