Search in sources :

Example 1 with TenantProfile

use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.

the class DefaultTransportApiService method handle.

private ListenableFuture<TransportApiResponseMsg> handle(GetEntityProfileRequestMsg requestMsg) {
    EntityType entityType = EntityType.valueOf(requestMsg.getEntityType());
    UUID entityUuid = new UUID(requestMsg.getEntityIdMSB(), requestMsg.getEntityIdLSB());
    GetEntityProfileResponseMsg.Builder builder = GetEntityProfileResponseMsg.newBuilder();
    if (entityType.equals(EntityType.DEVICE_PROFILE)) {
        DeviceProfileId deviceProfileId = new DeviceProfileId(entityUuid);
        DeviceProfile deviceProfile = deviceProfileCache.find(deviceProfileId);
        builder.setData(ByteString.copyFrom(dataDecodingEncodingService.encode(deviceProfile)));
    } else if (entityType.equals(EntityType.TENANT)) {
        TenantId tenantId = TenantId.fromUUID(entityUuid);
        TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
        ApiUsageState state = apiUsageStateService.getApiUsageState(tenantId);
        builder.setData(ByteString.copyFrom(dataDecodingEncodingService.encode(tenantProfile)));
        builder.setApiState(ByteString.copyFrom(dataDecodingEncodingService.encode(state)));
    } else {
        throw new RuntimeException("Invalid entity profile request: " + entityType);
    }
    return Futures.immediateFuture(TransportApiResponseMsg.newBuilder().setEntityProfileResponseMsg(builder).build());
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) TenantProfile(org.thingsboard.server.common.data.TenantProfile) UUID(java.util.UUID) GetEntityProfileResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileResponseMsg)

Example 2 with TenantProfile

use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.

the class TenantProfileController method setDefaultTenantProfile.

@ApiOperation(value = "Make tenant profile default (setDefaultTenantProfile)", notes = "Makes specified tenant profile to be default. Referencing non-existing tenant profile Id will cause an error. " + SYSTEM_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
@RequestMapping(value = "/tenantProfile/{tenantProfileId}/default", method = RequestMethod.POST)
@ResponseBody
public TenantProfile setDefaultTenantProfile(@ApiParam(value = TENANT_PROFILE_ID_PARAM_DESCRIPTION) @PathVariable("tenantProfileId") String strTenantProfileId) throws ThingsboardException {
    checkParameter("tenantProfileId", strTenantProfileId);
    try {
        TenantProfileId tenantProfileId = new TenantProfileId(toUUID(strTenantProfileId));
        TenantProfile tenantProfile = checkTenantProfileId(tenantProfileId, Operation.WRITE);
        tenantProfileService.setDefaultTenantProfile(getTenantId(), tenantProfileId);
        return tenantProfile;
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with TenantProfile

use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.

the class DefaultTbApiUsageStateService method onTenantUpdate.

@Override
public void onTenantUpdate(TenantId tenantId) {
    log.info("[{}] On Tenant Update.", tenantId);
    TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
    updateLock.lock();
    try {
        TenantApiUsageState state = (TenantApiUsageState) myUsageStates.get(tenantId);
        if (state != null && !state.getTenantProfileId().equals(tenantProfile.getId())) {
            updateTenantState(state, tenantProfile);
        }
    } finally {
        updateLock.unlock();
    }
}
Also used : TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Example 4 with TenantProfile

use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.

the class DefaultTbApiUsageStateService method onTenantProfileUpdate.

@Override
public void onTenantProfileUpdate(TenantProfileId tenantProfileId) {
    log.info("[{}] On Tenant Profile Update", tenantProfileId);
    TenantProfile tenantProfile = tenantProfileCache.get(tenantProfileId);
    updateLock.lock();
    try {
        myUsageStates.values().stream().filter(state -> state.getEntityType() == EntityType.TENANT).map(state -> (TenantApiUsageState) state).forEach(state -> {
            if (tenantProfile.getId().equals(state.getTenantProfileId())) {
                updateTenantState(state, tenantProfile);
            }
        });
    } finally {
        updateLock.unlock();
    }
}
Also used : UsageStatsKVProto(org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto) Arrays(java.util.Arrays) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) Autowired(org.springframework.beans.factory.annotation.Autowired) MailService(org.thingsboard.rule.engine.api.MailService) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) PreDestroy(javax.annotation.PreDestroy) TenantService(org.thingsboard.server.dao.tenant.TenantService) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) ApiUsageStateService(org.thingsboard.server.dao.usagerecord.ApiUsageStateService) TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UUID(java.util.UUID) ApiUsageStateId(org.thingsboard.server.common.data.id.ApiUsageStateId) Collectors(java.util.stream.Collectors) InternalTelemetryService(org.thingsboard.server.service.telemetry.InternalTelemetryService) Executors(java.util.concurrent.Executors) SchedulerUtils(org.thingsboard.server.common.msg.tools.SchedulerUtils) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TbClusterService(org.thingsboard.server.cluster.TbClusterService) TenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.TenantProfileConfiguration) Lazy(org.springframework.context.annotation.Lazy) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ApiUsageStateMailMessage(org.thingsboard.server.common.data.ApiUsageStateMailMessage) PartitionService(org.thingsboard.server.queue.discovery.PartitionService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Tenant(org.thingsboard.server.common.data.Tenant) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) AbstractPartitionBasedService(org.thingsboard.server.service.partition.AbstractPartitionBasedService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) ApiUsageStateValue(org.thingsboard.server.common.data.ApiUsageStateValue) FutureCallback(com.google.common.util.concurrent.FutureCallback) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Lock(java.util.concurrent.locks.Lock) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) ApiFeature(org.thingsboard.server.common.data.ApiFeature) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) Collections(java.util.Collections) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Example 5 with TenantProfile

use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.

the class BaseTbResourceServiceTest method testSaveResourceWithMaxSumDataSizeOutOfLimit.

@Test
public void testSaveResourceWithMaxSumDataSizeOutOfLimit() throws Exception {
    loginSysAdmin();
    long limit = 1;
    EntityInfo defaultTenantProfileInfo = doGet("/api/tenantProfileInfo/default", EntityInfo.class);
    TenantProfile defaultTenantProfile = doGet("/api/tenantProfile/" + defaultTenantProfileInfo.getId().getId().toString(), TenantProfile.class);
    defaultTenantProfile.getProfileData().setConfiguration(DefaultTenantProfileConfiguration.builder().maxResourcesInBytes(limit).build());
    doPost("/api/tenantProfile", defaultTenantProfile, TenantProfile.class);
    loginTenantAdmin();
    Assert.assertEquals(0, resourceService.sumDataSizeByTenantId(tenantId));
    createResource("test", DEFAULT_FILE_NAME);
    Assert.assertEquals(1, resourceService.sumDataSizeByTenantId(tenantId));
    try {
        thrown.expect(DataValidationException.class);
        thrown.expectMessage(String.format("Failed to create the tb resource, files size limit is exhausted %d bytes!", limit));
        createResource("test1", 1 + DEFAULT_FILE_NAME);
    } finally {
        defaultTenantProfile.getProfileData().setConfiguration(DefaultTenantProfileConfiguration.builder().maxResourcesInBytes(0).build());
        loginSysAdmin();
        doPost("/api/tenantProfile", defaultTenantProfile, TenantProfile.class);
    }
}
Also used : EntityInfo(org.thingsboard.server.common.data.EntityInfo) TenantProfile(org.thingsboard.server.common.data.TenantProfile) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test) DaoSqlTest(org.thingsboard.server.dao.service.DaoSqlTest)

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