Search in sources :

Example 6 with TenantProfile

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

the class TenantProfileServiceImpl method findOrCreateDefaultTenantProfile.

@Override
public TenantProfile findOrCreateDefaultTenantProfile(TenantId tenantId) {
    log.trace("Executing findOrCreateDefaultTenantProfile");
    TenantProfile defaultTenantProfile = findDefaultTenantProfile(tenantId);
    if (defaultTenantProfile == null) {
        defaultTenantProfile = new TenantProfile();
        defaultTenantProfile.setDefault(true);
        defaultTenantProfile.setName("Default");
        TenantProfileData profileData = new TenantProfileData();
        profileData.setConfiguration(new DefaultTenantProfileConfiguration());
        defaultTenantProfile.setProfileData(profileData);
        defaultTenantProfile.setDescription("Default tenant profile");
        defaultTenantProfile.setIsolatedTbCore(false);
        defaultTenantProfile.setIsolatedTbRuleEngine(false);
        defaultTenantProfile = saveTenantProfile(tenantId, defaultTenantProfile);
    }
    return defaultTenantProfile;
}
Also used : 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)

Example 7 with TenantProfile

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

the class TenantProfileServiceImpl method saveTenantProfile.

@Override
public TenantProfile saveTenantProfile(TenantId tenantId, TenantProfile tenantProfile) {
    log.trace("Executing saveTenantProfile [{}]", tenantProfile);
    tenantProfileValidator.validate(tenantProfile, (tenantProfile1) -> TenantId.SYS_TENANT_ID);
    TenantProfile savedTenantProfile;
    try {
        savedTenantProfile = tenantProfileDao.save(tenantId, tenantProfile);
    } catch (Exception t) {
        ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
        if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("tenant_profile_name_unq_key")) {
            throw new DataValidationException("Tenant profile with such name already exists!");
        } else {
            throw t;
        }
    }
    Cache cache = cacheManager.getCache(TENANT_PROFILE_CACHE);
    cache.evict(Collections.singletonList(savedTenantProfile.getId().getId()));
    cache.evict(Arrays.asList("info", savedTenantProfile.getId().getId()));
    if (savedTenantProfile.isDefault()) {
        cache.evict(Collections.singletonList("default"));
        cache.evict(Arrays.asList("default", "info"));
    }
    return savedTenantProfile;
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Cache(org.springframework.cache.Cache)

Example 8 with TenantProfile

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

the class TenantServiceImpl method saveTenant.

@Override
public Tenant saveTenant(Tenant tenant) {
    log.trace("Executing saveTenant [{}]", tenant);
    tenant.setRegion(DEFAULT_TENANT_REGION);
    if (tenant.getTenantProfileId() == null) {
        TenantProfile tenantProfile = this.tenantProfileService.findOrCreateDefaultTenantProfile(TenantId.SYS_TENANT_ID);
        tenant.setTenantProfileId(tenantProfile.getId());
    }
    tenantValidator.validate(tenant, Tenant::getId);
    Tenant savedTenant = tenantDao.save(tenant.getId(), tenant);
    if (tenant.getId() == null) {
        deviceProfileService.createDefaultDeviceProfile(savedTenant.getId());
        apiUsageStateService.createDefaultApiUsageState(savedTenant.getId(), null);
    }
    return savedTenant;
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Example 9 with TenantProfile

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

the class DefaultSystemDataLoaderService method createDefaultTenantProfiles.

@Override
public void createDefaultTenantProfiles() throws Exception {
    tenantProfileService.findOrCreateDefaultTenantProfile(TenantId.SYS_TENANT_ID);
    TenantProfileData tenantProfileData = new TenantProfileData();
    tenantProfileData.setConfiguration(new DefaultTenantProfileConfiguration());
    TenantProfile isolatedTbCoreProfile = new TenantProfile();
    isolatedTbCoreProfile.setDefault(false);
    isolatedTbCoreProfile.setName("Isolated TB Core");
    isolatedTbCoreProfile.setDescription("Isolated TB Core tenant profile");
    isolatedTbCoreProfile.setIsolatedTbCore(true);
    isolatedTbCoreProfile.setIsolatedTbRuleEngine(false);
    isolatedTbCoreProfile.setProfileData(tenantProfileData);
    try {
        tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, isolatedTbCoreProfile);
    } catch (DataValidationException e) {
        log.warn(e.getMessage());
    }
    TenantProfile isolatedTbRuleEngineProfile = new TenantProfile();
    isolatedTbRuleEngineProfile.setDefault(false);
    isolatedTbRuleEngineProfile.setName("Isolated TB Rule Engine");
    isolatedTbRuleEngineProfile.setDescription("Isolated TB Rule Engine tenant profile");
    isolatedTbRuleEngineProfile.setIsolatedTbCore(false);
    isolatedTbRuleEngineProfile.setIsolatedTbRuleEngine(true);
    isolatedTbRuleEngineProfile.setProfileData(tenantProfileData);
    try {
        tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, isolatedTbRuleEngineProfile);
    } catch (DataValidationException e) {
        log.warn(e.getMessage());
    }
    TenantProfile isolatedTbCoreAndTbRuleEngineProfile = new TenantProfile();
    isolatedTbCoreAndTbRuleEngineProfile.setDefault(false);
    isolatedTbCoreAndTbRuleEngineProfile.setName("Isolated TB Core and TB Rule Engine");
    isolatedTbCoreAndTbRuleEngineProfile.setDescription("Isolated TB Core and TB Rule Engine tenant profile");
    isolatedTbCoreAndTbRuleEngineProfile.setIsolatedTbCore(true);
    isolatedTbCoreAndTbRuleEngineProfile.setIsolatedTbRuleEngine(true);
    isolatedTbCoreAndTbRuleEngineProfile.setProfileData(tenantProfileData);
    try {
        tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, isolatedTbCoreAndTbRuleEngineProfile);
    } catch (DataValidationException e) {
        log.warn(e.getMessage());
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) 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)

Example 10 with TenantProfile

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

the class BaseTenantProfileControllerTest method testSaveTenantProfile.

@Test
public void testSaveTenantProfile() throws Exception {
    loginSysAdmin();
    TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
    TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class);
    Assert.assertNotNull(savedTenantProfile);
    Assert.assertNotNull(savedTenantProfile.getId());
    Assert.assertTrue(savedTenantProfile.getCreatedTime() > 0);
    Assert.assertEquals(tenantProfile.getName(), savedTenantProfile.getName());
    Assert.assertEquals(tenantProfile.getDescription(), savedTenantProfile.getDescription());
    Assert.assertEquals(tenantProfile.getProfileData(), savedTenantProfile.getProfileData());
    Assert.assertEquals(tenantProfile.isDefault(), savedTenantProfile.isDefault());
    Assert.assertEquals(tenantProfile.isIsolatedTbCore(), savedTenantProfile.isIsolatedTbCore());
    Assert.assertEquals(tenantProfile.isIsolatedTbRuleEngine(), savedTenantProfile.isIsolatedTbRuleEngine());
    savedTenantProfile.setName("New tenant profile");
    doPost("/api/tenantProfile", savedTenantProfile, TenantProfile.class);
    TenantProfile foundTenantProfile = doGet("/api/tenantProfile/" + savedTenantProfile.getId().getId().toString(), TenantProfile.class);
    Assert.assertEquals(foundTenantProfile.getName(), savedTenantProfile.getName());
}
Also used : TenantProfile(org.thingsboard.server.common.data.TenantProfile) Test(org.junit.Test)

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