Search in sources :

Example 16 with TenantProfile

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

the class BaseTenantProfileControllerTest method testSaveSameTenantProfileWithDifferentIsolatedTbCore.

@Test
public void testSaveSameTenantProfileWithDifferentIsolatedTbCore() throws Exception {
    loginSysAdmin();
    TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
    TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class);
    savedTenantProfile.setIsolatedTbCore(true);
    doPost("/api/tenantProfile", savedTenantProfile).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Can't update isolatedTbCore property")));
}
Also used : TenantProfile(org.thingsboard.server.common.data.TenantProfile) Test(org.junit.Test)

Example 17 with TenantProfile

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

the class BaseTenantProfileControllerTest method testSaveSameTenantProfileWithDifferentIsolatedTbRuleEngine.

@Test
public void testSaveSameTenantProfileWithDifferentIsolatedTbRuleEngine() throws Exception {
    loginSysAdmin();
    TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
    TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class);
    savedTenantProfile.setIsolatedTbRuleEngine(true);
    doPost("/api/tenantProfile", savedTenantProfile).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Can't update isolatedTbRuleEngine property")));
}
Also used : TenantProfile(org.thingsboard.server.common.data.TenantProfile) Test(org.junit.Test)

Example 18 with TenantProfile

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

the class DefaultTransportTenantProfileCache method put.

@Override
public TenantProfileUpdateResult put(ByteString profileBody) {
    Optional<TenantProfile> profileOpt = dataDecodingEncodingService.decode(profileBody.toByteArray());
    if (profileOpt.isPresent()) {
        TenantProfile newProfile = profileOpt.get();
        log.trace("[{}] put: {}", newProfile.getId(), newProfile);
        Set<TenantId> affectedTenants = tenantProfileIds.get(newProfile.getId());
        return new TenantProfileUpdateResult(newProfile, affectedTenants != null ? affectedTenants : Collections.emptySet());
    } else {
        log.warn("Failed to decode profile: {}", profileBody.toString());
        return new TenantProfileUpdateResult(null, Collections.emptySet());
    }
}
Also used : TenantProfileUpdateResult(org.thingsboard.server.common.transport.profile.TenantProfileUpdateResult) TenantId(org.thingsboard.server.common.data.id.TenantId) TenantProfile(org.thingsboard.server.common.data.TenantProfile)

Example 19 with TenantProfile

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

the class BaseDeviceServiceTest method testSaveDevicesWithMaxDeviceOutOfLimit.

@Test(expected = DataValidationException.class)
public void testSaveDevicesWithMaxDeviceOutOfLimit() {
    TenantProfile defaultTenantProfile = tenantProfileService.findDefaultTenantProfile(tenantId);
    defaultTenantProfile.getProfileData().setConfiguration(DefaultTenantProfileConfiguration.builder().maxDevices(1).build());
    tenantProfileService.saveTenantProfile(tenantId, defaultTenantProfile);
    Assert.assertEquals(0, deviceService.countByTenantId(tenantId));
    this.saveDevice(tenantId, "My first device");
    Assert.assertEquals(1, deviceService.countByTenantId(tenantId));
    this.saveDevice(tenantId, "My second device that out of maxDeviceCount limit");
}
Also used : TenantProfile(org.thingsboard.server.common.data.TenantProfile) Test(org.junit.Test)

Example 20 with TenantProfile

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

the class TelemetryController method saveTelemetry.

private DeferredResult<ResponseEntity> saveTelemetry(TenantId curTenantId, EntityId entityIdSrc, String requestBody, long ttl) throws ThingsboardException {
    Map<Long, List<KvEntry>> telemetryRequest;
    JsonElement telemetryJson;
    try {
        telemetryJson = new JsonParser().parse(requestBody);
    } catch (Exception e) {
        return getImmediateDeferredResult("Unable to parse timeseries payload: Invalid JSON body!", HttpStatus.BAD_REQUEST);
    }
    try {
        telemetryRequest = JsonConverter.convertToTelemetry(telemetryJson, System.currentTimeMillis());
    } catch (Exception e) {
        return getImmediateDeferredResult("Unable to parse timeseries payload. Invalid JSON body: " + e.getMessage(), HttpStatus.BAD_REQUEST);
    }
    List<TsKvEntry> entries = new ArrayList<>();
    for (Map.Entry<Long, List<KvEntry>> entry : telemetryRequest.entrySet()) {
        for (KvEntry kv : entry.getValue()) {
            entries.add(new BasicTsKvEntry(entry.getKey(), kv));
        }
    }
    if (entries.isEmpty()) {
        return getImmediateDeferredResult("No timeseries data found in request body!", HttpStatus.BAD_REQUEST);
    }
    SecurityUser user = getCurrentUser();
    return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.WRITE_TELEMETRY, entityIdSrc, (result, tenantId, entityId) -> {
        long tenantTtl = ttl;
        if (!TenantId.SYS_TENANT_ID.equals(tenantId) && tenantTtl == 0) {
            TenantProfile tenantProfile = tenantProfileCache.get(tenantId);
            tenantTtl = TimeUnit.DAYS.toSeconds(((DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration()).getDefaultStorageTtlDays());
        }
        tsSubService.saveAndNotify(tenantId, user.getCustomerId(), entityId, entries, tenantTtl, new FutureCallback<Void>() {

            @Override
            public void onSuccess(@Nullable Void tmp) {
                logTelemetryUpdated(user, entityId, entries, null);
                result.setResult(new ResponseEntity(HttpStatus.OK));
            }

            @Override
            public void onFailure(Throwable t) {
                logTelemetryUpdated(user, entityId, entries, t);
                AccessValidator.handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        });
    });
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) ArrayList(java.util.ArrayList) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) UncheckedApiException(org.thingsboard.server.service.telemetry.exception.UncheckedApiException) JsonParseException(com.google.gson.JsonParseException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) InvalidParametersException(org.thingsboard.server.service.telemetry.exception.InvalidParametersException) ResponseEntity(org.springframework.http.ResponseEntity) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) TenantProfile(org.thingsboard.server.common.data.TenantProfile) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) JsonParser(com.google.gson.JsonParser)

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