Search in sources :

Example 1 with TenantProfileId

use of org.thingsboard.server.common.data.id.TenantProfileId 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 2 with TenantProfileId

use of org.thingsboard.server.common.data.id.TenantProfileId 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 3 with TenantProfileId

use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.

the class AbstractConsumerService method handleComponentLifecycleMsg.

protected void handleComponentLifecycleMsg(UUID id, ByteString nfMsg) {
    Optional<TbActorMsg> actorMsgOpt = encodingService.decode(nfMsg.toByteArray());
    if (actorMsgOpt.isPresent()) {
        TbActorMsg actorMsg = actorMsgOpt.get();
        if (actorMsg instanceof ComponentLifecycleMsg) {
            ComponentLifecycleMsg componentLifecycleMsg = (ComponentLifecycleMsg) actorMsg;
            log.info("[{}][{}][{}] Received Lifecycle event: {}", componentLifecycleMsg.getTenantId(), componentLifecycleMsg.getEntityId().getEntityType(), componentLifecycleMsg.getEntityId(), componentLifecycleMsg.getEvent());
            if (EntityType.TENANT_PROFILE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                TenantProfileId tenantProfileId = new TenantProfileId(componentLifecycleMsg.getEntityId().getId());
                tenantProfileCache.evict(tenantProfileId);
                if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.UPDATED)) {
                    apiUsageStateService.onTenantProfileUpdate(tenantProfileId);
                }
            } else if (EntityType.TENANT.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                tenantProfileCache.evict(componentLifecycleMsg.getTenantId());
                if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.UPDATED)) {
                    apiUsageStateService.onTenantUpdate(componentLifecycleMsg.getTenantId());
                } else if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.DELETED)) {
                    apiUsageStateService.onTenantDelete((TenantId) componentLifecycleMsg.getEntityId());
                }
            } else if (EntityType.DEVICE_PROFILE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                deviceProfileCache.evict(componentLifecycleMsg.getTenantId(), new DeviceProfileId(componentLifecycleMsg.getEntityId().getId()));
            } else if (EntityType.DEVICE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                deviceProfileCache.evict(componentLifecycleMsg.getTenantId(), new DeviceId(componentLifecycleMsg.getEntityId().getId()));
            } else if (EntityType.API_USAGE_STATE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                apiUsageStateService.onApiUsageStateUpdate(componentLifecycleMsg.getTenantId());
            } else if (EntityType.CUSTOMER.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                if (componentLifecycleMsg.getEvent() == ComponentLifecycleEvent.DELETED) {
                    apiUsageStateService.onCustomerDelete((CustomerId) componentLifecycleMsg.getEntityId());
                }
            }
        }
        log.trace("[{}] Forwarding message to App Actor {}", id, actorMsg);
        actorContext.tellWithHighPriority(actorMsg);
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg)

Example 4 with TenantProfileId

use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.

the class BaseTenantProfileServiceTest method testFindTenantProfileInfos.

@Test
public void testFindTenantProfileInfos() {
    List<TenantProfile> tenantProfiles = new ArrayList<>();
    for (int i = 0; i < 28; i++) {
        TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile" + i);
        tenantProfiles.add(tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile));
    }
    List<EntityInfo> loadedTenantProfileInfos = new ArrayList<>();
    PageLink pageLink = new PageLink(17);
    PageData<EntityInfo> pageData;
    do {
        pageData = tenantProfileService.findTenantProfileInfos(TenantId.SYS_TENANT_ID, pageLink);
        loadedTenantProfileInfos.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantProfiles, idComparator);
    Collections.sort(loadedTenantProfileInfos, tenantProfileInfoIdComparator);
    List<EntityInfo> tenantProfileInfos = tenantProfiles.stream().map(tenantProfile -> new EntityInfo(tenantProfile.getId(), tenantProfile.getName())).collect(Collectors.toList());
    Assert.assertEquals(tenantProfileInfos, loadedTenantProfileInfos);
    for (EntityInfo tenantProfile : loadedTenantProfileInfos) {
        tenantProfileService.deleteTenantProfile(TenantId.SYS_TENANT_ID, new TenantProfileId(tenantProfile.getId().getId()));
    }
    pageLink = new PageLink(17);
    pageData = tenantProfileService.findTenantProfileInfos(TenantId.SYS_TENANT_ID, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
}
Also used : EntityInfo(org.thingsboard.server.common.data.EntityInfo) PageLink(org.thingsboard.server.common.data.page.PageLink) TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) Tenant(org.thingsboard.server.common.data.Tenant) Test(org.junit.Test) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) Collectors(java.util.stream.Collectors) TenantId(org.thingsboard.server.common.data.id.TenantId) ArrayList(java.util.ArrayList) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) List(java.util.List) TenantProfile(org.thingsboard.server.common.data.TenantProfile) After(org.junit.After) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) PageData(org.thingsboard.server.common.data.page.PageData) Assert(org.junit.Assert) Collections(java.util.Collections) EntityInfo(org.thingsboard.server.common.data.EntityInfo) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TenantProfile(org.thingsboard.server.common.data.TenantProfile) Test(org.junit.Test)

Example 5 with TenantProfileId

use of org.thingsboard.server.common.data.id.TenantProfileId in project thingsboard by thingsboard.

the class TenantProfileController method deleteTenantProfile.

@ApiOperation(value = "Delete Tenant Profile (deleteTenantProfile)", notes = "Deletes the tenant profile. Referencing non-existing tenant profile Id will cause an error. Referencing profile that is used by the tenants will cause an error. " + SYSTEM_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('SYS_ADMIN')")
@RequestMapping(value = "/tenantProfile/{tenantProfileId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteTenantProfile(@ApiParam(value = TENANT_PROFILE_ID_PARAM_DESCRIPTION) @PathVariable("tenantProfileId") String strTenantProfileId) throws ThingsboardException {
    checkParameter("tenantProfileId", strTenantProfileId);
    try {
        TenantProfileId tenantProfileId = new TenantProfileId(toUUID(strTenantProfileId));
        TenantProfile profile = checkTenantProfileId(tenantProfileId, Operation.DELETE);
        tenantProfileService.deleteTenantProfile(getTenantId(), tenantProfileId);
        tbClusterService.onTenantProfileDelete(profile, null);
    } 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) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TenantProfileId (org.thingsboard.server.common.data.id.TenantProfileId)11 TenantProfile (org.thingsboard.server.common.data.TenantProfile)7 Collections (java.util.Collections)5 Tenant (org.thingsboard.server.common.data.Tenant)5 TenantId (org.thingsboard.server.common.data.id.TenantId)5 Set (java.util.Set)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Slf4j (lombok.extern.slf4j.Slf4j)4 ByteString (com.google.protobuf.ByteString)3 List (java.util.List)3 Optional (java.util.Optional)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Lazy (org.springframework.context.annotation.Lazy)3 ApiUsageState (org.thingsboard.server.common.data.ApiUsageState)3 EntityType (org.thingsboard.server.common.data.EntityType)3 TransportService (org.thingsboard.server.common.transport.TransportService)3 TransportTenantProfileCache (org.thingsboard.server.common.transport.TransportTenantProfileCache)3