use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class BaseTenantServiceTest method testSaveTenantWithIsolatedProfileInMonolithSetup.
@Test(expected = DataValidationException.class)
public void testSaveTenantWithIsolatedProfileInMonolithSetup() {
TenantProfile tenantProfile = new TenantProfile();
tenantProfile.setName("Isolated Tenant Profile");
TenantProfileData profileData = new TenantProfileData();
profileData.setConfiguration(new DefaultTenantProfileConfiguration());
tenantProfile.setProfileData(profileData);
tenantProfile.setDefault(false);
tenantProfile.setIsolatedTbCore(true);
tenantProfile.setIsolatedTbRuleEngine(true);
TenantProfile isolatedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
Tenant tenant = new Tenant();
tenant.setTitle("Tenant");
tenant.setTenantProfileId(isolatedTenantProfile.getId());
tenantService.saveTenant(tenant);
}
use of org.thingsboard.server.common.data.TenantProfile 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.TenantProfile in project thingsboard by thingsboard.
the class TenantProfileServiceImpl method deleteTenantProfile.
@Override
public void deleteTenantProfile(TenantId tenantId, TenantProfileId tenantProfileId) {
log.trace("Executing deleteTenantProfile [{}]", tenantProfileId);
validateId(tenantId, INCORRECT_TENANT_PROFILE_ID + tenantProfileId);
TenantProfile tenantProfile = tenantProfileDao.findById(tenantId, tenantProfileId.getId());
if (tenantProfile != null && tenantProfile.isDefault()) {
throw new DataValidationException("Deletion of Default Tenant Profile is prohibited!");
}
this.removeTenantProfile(tenantId, tenantProfileId, false);
}
use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class TenantProfileServiceImpl method setDefaultTenantProfile.
@Override
public boolean setDefaultTenantProfile(TenantId tenantId, TenantProfileId tenantProfileId) {
log.trace("Executing setDefaultTenantProfile [{}]", tenantProfileId);
validateId(tenantId, INCORRECT_TENANT_PROFILE_ID + tenantProfileId);
TenantProfile tenantProfile = tenantProfileDao.findById(tenantId, tenantProfileId.getId());
if (!tenantProfile.isDefault()) {
Cache cache = cacheManager.getCache(TENANT_PROFILE_CACHE);
tenantProfile.setDefault(true);
TenantProfile previousDefaultTenantProfile = findDefaultTenantProfile(tenantId);
boolean changed = false;
if (previousDefaultTenantProfile == null) {
tenantProfileDao.save(tenantId, tenantProfile);
changed = true;
} else if (!previousDefaultTenantProfile.getId().equals(tenantProfile.getId())) {
previousDefaultTenantProfile.setDefault(false);
tenantProfileDao.save(tenantId, previousDefaultTenantProfile);
tenantProfileDao.save(tenantId, tenantProfile);
cache.evict(Collections.singletonList(previousDefaultTenantProfile.getId().getId()));
cache.evict(Arrays.asList("info", previousDefaultTenantProfile.getId().getId()));
changed = true;
}
if (changed) {
cache.evict(Collections.singletonList(tenantProfile.getId().getId()));
cache.evict(Arrays.asList("info", tenantProfile.getId().getId()));
cache.evict(Collections.singletonList("default"));
cache.evict(Arrays.asList("default", "info"));
}
return changed;
}
return false;
}
use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class DefaultTbTenantProfileCache method evict.
@Override
public void evict(TenantId tenantId) {
tenantsMap.remove(tenantId);
TenantProfile tenantProfile = get(tenantId);
if (tenantProfile != null) {
ConcurrentMap<EntityId, Consumer<TenantProfile>> tenantListeners = profileListeners.get(tenantId);
if (tenantListeners != null) {
tenantListeners.forEach((id, listener) -> listener.accept(tenantProfile));
}
}
}
Aggregations