Search in sources :

Example 51 with TenantProfile

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);
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) TenantProfile(org.thingsboard.server.common.data.TenantProfile) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) Test(org.junit.Test)

Example 52 with TenantProfile

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;
}
Also used : TbTransportComponent(org.thingsboard.server.queue.util.TbTransportComponent) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) TransportTenantProfileCache(org.thingsboard.server.common.transport.TransportTenantProfileCache) Set(java.util.Set) TransportService(org.thingsboard.server.common.transport.TransportService) TenantId(org.thingsboard.server.common.data.id.TenantId) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) DataDecodingEncodingService(org.thingsboard.server.common.transport.util.DataDecodingEncodingService) ConcurrentMap(java.util.concurrent.ConcurrentMap) ByteString(com.google.protobuf.ByteString) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) TenantProfile(org.thingsboard.server.common.data.TenantProfile) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) TransportRateLimitService(org.thingsboard.server.common.transport.limits.TransportRateLimitService) EntityType(org.thingsboard.server.common.data.EntityType) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Optional(java.util.Optional) Lazy(org.springframework.context.annotation.Lazy) TenantProfileUpdateResult(org.thingsboard.server.common.transport.profile.TenantProfileUpdateResult) Collections(java.util.Collections) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Example 53 with TenantProfile

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);
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Example 54 with TenantProfile

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;
}
Also used : TenantProfile(org.thingsboard.server.common.data.TenantProfile) Cache(org.springframework.cache.Cache)

Example 55 with TenantProfile

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));
        }
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) Consumer(java.util.function.Consumer) TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Aggregations

TenantProfile (org.thingsboard.server.common.data.TenantProfile)56 Test (org.junit.Test)33 Tenant (org.thingsboard.server.common.data.Tenant)10 DefaultTenantProfileConfiguration (org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration)10 TenantId (org.thingsboard.server.common.data.id.TenantId)9 TenantProfileData (org.thingsboard.server.common.data.tenant.profile.TenantProfileData)9 ArrayList (java.util.ArrayList)8 EntityInfo (org.thingsboard.server.common.data.EntityInfo)7 List (java.util.List)6 TenantProfileId (org.thingsboard.server.common.data.id.TenantProfileId)6 ApiUsageState (org.thingsboard.server.common.data.ApiUsageState)5 EntityType (org.thingsboard.server.common.data.EntityType)5 Collections (java.util.Collections)4 Collectors (java.util.stream.Collectors)4 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)4 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)4 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)4 PageLink (org.thingsboard.server.common.data.page.PageLink)4 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)4 Map (java.util.Map)3