use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.
the class AbstractTenantEntity method toTenant.
protected Tenant toTenant() {
Tenant tenant = new Tenant(TenantId.fromUUID(this.getUuid()));
tenant.setCreatedTime(createdTime);
tenant.setTitle(title);
tenant.setRegion(region);
tenant.setCountry(country);
tenant.setState(state);
tenant.setCity(city);
tenant.setAddress(address);
tenant.setAddress2(address2);
tenant.setZip(zip);
tenant.setPhone(phone);
tenant.setEmail(email);
tenant.setAdditionalInfo(additionalInfo);
if (tenantProfileId != null) {
tenant.setTenantProfileId(new TenantProfileId(tenantProfileId));
}
return tenant;
}
use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.
the class DefaultTransportService method processToTransportMsg.
protected void processToTransportMsg(TransportProtos.ToTransportMsg toSessionMsg) {
UUID sessionId = new UUID(toSessionMsg.getSessionIdMSB(), toSessionMsg.getSessionIdLSB());
SessionMetaData md = sessions.get(sessionId);
if (md != null) {
log.trace("[{}] Processing notification: {}", sessionId, toSessionMsg);
SessionMsgListener listener = md.getListener();
transportCallbackExecutor.submit(() -> {
if (toSessionMsg.hasGetAttributesResponse()) {
listener.onGetAttributesResponse(toSessionMsg.getGetAttributesResponse());
}
if (toSessionMsg.hasAttributeUpdateNotification()) {
listener.onAttributeUpdate(sessionId, toSessionMsg.getAttributeUpdateNotification());
}
if (toSessionMsg.hasSessionCloseNotification()) {
listener.onRemoteSessionCloseCommand(sessionId, toSessionMsg.getSessionCloseNotification());
}
if (toSessionMsg.hasToTransportUpdateCredentialsNotification()) {
listener.onToTransportUpdateCredentials(toSessionMsg.getToTransportUpdateCredentialsNotification());
}
if (toSessionMsg.hasToDeviceRequest()) {
listener.onToDeviceRpcRequest(sessionId, toSessionMsg.getToDeviceRequest());
}
if (toSessionMsg.hasToServerResponse()) {
String requestId = sessionId + "-" + toSessionMsg.getToServerResponse().getRequestId();
toServerRpcPendingMap.remove(requestId);
listener.onToServerRpcResponse(toSessionMsg.getToServerResponse());
}
});
if (md.getSessionType() == TransportProtos.SessionType.SYNC) {
deregisterSession(md.getSessionInfo());
}
} else {
log.trace("Processing broadcast notification: {}", toSessionMsg);
if (toSessionMsg.hasEntityUpdateMsg()) {
TransportProtos.EntityUpdateMsg msg = toSessionMsg.getEntityUpdateMsg();
EntityType entityType = EntityType.valueOf(msg.getEntityType());
if (EntityType.DEVICE_PROFILE.equals(entityType)) {
DeviceProfile deviceProfile = deviceProfileCache.put(msg.getData());
if (deviceProfile != null) {
log.info("On device profile update: {}", deviceProfile);
onProfileUpdate(deviceProfile);
}
} else if (EntityType.TENANT_PROFILE.equals(entityType)) {
rateLimitService.update(tenantProfileCache.put(msg.getData()));
} else if (EntityType.TENANT.equals(entityType)) {
Optional<Tenant> profileOpt = dataDecodingEncodingService.decode(msg.getData().toByteArray());
if (profileOpt.isPresent()) {
Tenant tenant = profileOpt.get();
boolean updated = tenantProfileCache.put(tenant.getId(), tenant.getTenantProfileId());
if (updated) {
rateLimitService.update(tenant.getId());
}
}
} else if (EntityType.API_USAGE_STATE.equals(entityType)) {
Optional<ApiUsageState> stateOpt = dataDecodingEncodingService.decode(msg.getData().toByteArray());
if (stateOpt.isPresent()) {
ApiUsageState apiUsageState = stateOpt.get();
rateLimitService.update(apiUsageState.getTenantId(), apiUsageState.isTransportEnabled());
// TODO: if transport is disabled, we should close all sessions and not to check credentials.
}
} else if (EntityType.DEVICE.equals(entityType)) {
Optional<Device> deviceOpt = dataDecodingEncodingService.decode(msg.getData().toByteArray());
deviceOpt.ifPresent(device -> {
onDeviceUpdate(device);
eventPublisher.publishEvent(new DeviceUpdatedEvent(device));
});
}
} else if (toSessionMsg.hasEntityDeleteMsg()) {
TransportProtos.EntityDeleteMsg msg = toSessionMsg.getEntityDeleteMsg();
EntityType entityType = EntityType.valueOf(msg.getEntityType());
UUID entityUuid = new UUID(msg.getEntityIdMSB(), msg.getEntityIdLSB());
if (EntityType.DEVICE_PROFILE.equals(entityType)) {
deviceProfileCache.evict(new DeviceProfileId(new UUID(msg.getEntityIdMSB(), msg.getEntityIdLSB())));
} else if (EntityType.TENANT_PROFILE.equals(entityType)) {
tenantProfileCache.remove(new TenantProfileId(entityUuid));
} else if (EntityType.TENANT.equals(entityType)) {
rateLimitService.remove(TenantId.fromUUID(entityUuid));
} else if (EntityType.DEVICE.equals(entityType)) {
rateLimitService.remove(new DeviceId(entityUuid));
onDeviceDeleted(new DeviceId(entityUuid));
}
} else if (toSessionMsg.hasResourceUpdateMsg()) {
TransportProtos.ResourceUpdateMsg msg = toSessionMsg.getResourceUpdateMsg();
TenantId tenantId = TenantId.fromUUID(new UUID(msg.getTenantIdMSB(), msg.getTenantIdLSB()));
ResourceType resourceType = ResourceType.valueOf(msg.getResourceType());
String resourceId = msg.getResourceKey();
transportResourceCache.update(tenantId, resourceType, resourceId);
sessions.forEach((id, mdRez) -> {
log.trace("ResourceUpdate - [{}] [{}]", id, mdRez);
transportCallbackExecutor.submit(() -> mdRez.getListener().onResourceUpdate(msg));
});
} else if (toSessionMsg.hasResourceDeleteMsg()) {
TransportProtos.ResourceDeleteMsg msg = toSessionMsg.getResourceDeleteMsg();
TenantId tenantId = TenantId.fromUUID(new UUID(msg.getTenantIdMSB(), msg.getTenantIdLSB()));
ResourceType resourceType = ResourceType.valueOf(msg.getResourceType());
String resourceId = msg.getResourceKey();
transportResourceCache.evict(tenantId, resourceType, resourceId);
sessions.forEach((id, mdRez) -> {
log.warn("ResourceDelete - [{}] [{}]", id, mdRez);
transportCallbackExecutor.submit(() -> mdRez.getListener().onResourceDelete(msg));
});
} else {
// TODO: should we notify the device actor about missed session?
log.debug("[{}] Missing session.", sessionId);
}
}
}
use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.
the class DefaultTransportTenantProfileCache method getTenantProfile.
private TenantProfile getTenantProfile(TenantId tenantId) {
TenantProfile profile = null;
TenantProfileId tenantProfileId = tenantIds.get(tenantId);
if (tenantProfileId != null) {
profile = profiles.get(tenantProfileId);
}
if (profile == null) {
tenantProfileFetchLock.lock();
try {
tenantProfileId = tenantIds.get(tenantId);
if (tenantProfileId != null) {
profile = profiles.get(tenantProfileId);
}
if (profile == null) {
TransportProtos.GetEntityProfileRequestMsg msg = TransportProtos.GetEntityProfileRequestMsg.newBuilder().setEntityType(EntityType.TENANT.name()).setEntityIdMSB(tenantId.getId().getMostSignificantBits()).setEntityIdLSB(tenantId.getId().getLeastSignificantBits()).build();
TransportProtos.GetEntityProfileResponseMsg entityProfileMsg = transportService.getEntityProfile(msg);
Optional<TenantProfile> profileOpt = dataDecodingEncodingService.decode(entityProfileMsg.getData().toByteArray());
if (profileOpt.isPresent()) {
profile = profileOpt.get();
TenantProfile existingProfile = profiles.get(profile.getId());
if (existingProfile != null) {
profile = existingProfile;
} else {
profiles.put(profile.getId(), profile);
}
tenantProfileIds.computeIfAbsent(profile.getId(), id -> ConcurrentHashMap.newKeySet()).add(tenantId);
tenantIds.put(tenantId, profile.getId());
} else {
log.warn("[{}] Can't decode tenant profile: {}", tenantId, entityProfileMsg.getData());
throw new RuntimeException("Can't decode tenant profile!");
}
Optional<ApiUsageState> apiStateOpt = dataDecodingEncodingService.decode(entityProfileMsg.getApiState().toByteArray());
apiStateOpt.ifPresent(apiUsageState -> rateLimitService.update(tenantId, apiUsageState.isTransportEnabled()));
}
} finally {
tenantProfileFetchLock.unlock();
}
}
return profile;
}
use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.
the class DefaultTransportTenantProfileCache method put.
@Override
public boolean put(TenantId tenantId, TenantProfileId profileId) {
log.trace("[{}] put: {}", tenantId, profileId);
TenantProfileId oldProfileId = tenantIds.get(tenantId);
if (oldProfileId != null && !oldProfileId.equals(profileId)) {
tenantProfileIds.computeIfAbsent(oldProfileId, id -> ConcurrentHashMap.newKeySet()).remove(tenantId);
tenantIds.put(tenantId, profileId);
tenantProfileIds.computeIfAbsent(profileId, id -> ConcurrentHashMap.newKeySet()).add(tenantId);
return true;
} else {
return false;
}
}
use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.
the class DefaultTbTenantProfileCache method get.
@Override
public TenantProfile get(TenantId tenantId) {
TenantProfileId profileId = tenantsMap.get(tenantId);
if (profileId == null) {
Tenant tenant = tenantService.findTenantById(tenantId);
if (tenant != null) {
profileId = tenant.getTenantProfileId();
tenantsMap.put(tenantId, profileId);
} else {
return null;
}
}
return get(profileId);
}
Aggregations