Search in sources :

Example 16 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BaseRuleServiceTest method findTenantRules.

@Test
public void findTenantRules() throws Exception {
    TenantId tenantIdA = new TenantId(UUIDs.timeBased());
    TenantId tenantIdB = new TenantId(UUIDs.timeBased());
    PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt());
    PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt());
    pluginService.savePlugin(pluginA);
    pluginService.savePlugin(pluginB);
    ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
    TextPageData<RuleMetaData> foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100));
    Assert.assertEquals(3, foundA.getData().size());
    TextPageData<RuleMetaData> foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100));
    Assert.assertEquals(2, foundB.getData().size());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 17 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BaseRuleServiceTest method deleteRulesByTenantId.

@Test
public void deleteRulesByTenantId() throws Exception {
    TenantId tenantIdA = new TenantId(UUIDs.timeBased());
    TenantId tenantIdB = new TenantId(UUIDs.timeBased());
    PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt());
    PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt());
    pluginService.savePlugin(pluginA);
    pluginService.savePlugin(pluginB);
    ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
    ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
    TextPageData<RuleMetaData> foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100));
    Assert.assertEquals(3, foundA.getData().size());
    TextPageData<RuleMetaData> foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100));
    Assert.assertEquals(2, foundB.getData().size());
    ruleService.deleteRulesByTenantId(tenantIdA);
    foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100));
    Assert.assertEquals(0, foundA.getData().size());
    foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100));
    Assert.assertEquals(2, foundB.getData().size());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 18 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class BaseRuleServiceTest method findSystemRules.

@Test
public void findSystemRules() throws Exception {
    // system tenant id
    TenantId systemTenant = new TenantId(ModelConstants.NULL_UUID);
    PluginMetaData plugin = generatePlugin(systemTenant, "testPluginToken" + ThreadLocalRandom.current().nextInt());
    pluginService.savePlugin(plugin);
    ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
    ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
    ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
    TextPageData<RuleMetaData> found = ruleService.findSystemRules(new TextPageLink(100));
    Assert.assertEquals(3, found.getData().size());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 19 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class DefaultSystemDataLoaderService method loadPlugins.

private void loadPlugins(Path pluginsDir, TenantId tenantId) throws Exception {
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(pluginsDir, path -> path.toString().endsWith(JSON_EXT))) {
        dirStream.forEach(path -> {
            try {
                JsonNode pluginJson = objectMapper.readTree(path.toFile());
                PluginMetaData plugin = objectMapper.treeToValue(pluginJson, PluginMetaData.class);
                plugin.setTenantId(tenantId);
                if (plugin.getState() == ComponentLifecycleState.ACTIVE) {
                    plugin.setState(ComponentLifecycleState.SUSPENDED);
                    PluginMetaData savedPlugin = pluginService.savePlugin(plugin);
                    pluginService.activatePluginById(savedPlugin.getId());
                } else {
                    pluginService.savePlugin(plugin);
                }
            } catch (Exception e) {
                log.error("Unable to load plugin from json: [{}]", path.toString());
                throw new RuntimeException("Unable to load plugin from json", e);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 20 with PluginMetaData

use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.

the class PluginApiController method processRequest.

@SuppressWarnings("rawtypes")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/{pluginToken}/**")
@ResponseStatus(value = HttpStatus.OK)
public DeferredResult<ResponseEntity> processRequest(@PathVariable("pluginToken") String pluginToken, RequestEntity<byte[]> requestEntity, HttpServletRequest request) throws ThingsboardException {
    log.debug("[{}] Going to process requst uri: {}", pluginToken, requestEntity.getUrl());
    DeferredResult<ResponseEntity> result = new DeferredResult<ResponseEntity>();
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(pluginToken);
    if (pluginMd == null) {
        result.setErrorResult(new PluginNotFoundException("Plugin with token: " + pluginToken + " not found!"));
    } else {
        TenantId tenantId = getCurrentUser().getTenantId();
        CustomerId customerId = getCurrentUser().getCustomerId();
        if (validatePluginAccess(pluginMd, tenantId, customerId)) {
            if (tenantId != null && ModelConstants.NULL_UUID.equals(tenantId.getId())) {
                tenantId = null;
            }
            UserId userId = getCurrentUser().getId();
            String userName = getCurrentUser().getName();
            PluginApiCallSecurityContext securityCtx = new PluginApiCallSecurityContext(pluginMd.getTenantId(), pluginMd.getId(), tenantId, customerId, userId, userName);
            actorService.process(new BasicPluginRestMsg(securityCtx, new RestRequest(requestEntity, request), result));
        } else {
            result.setResult(new ResponseEntity<>(HttpStatus.FORBIDDEN));
        }
    }
    return result;
}
Also used : BasicPluginRestMsg(org.thingsboard.server.extensions.api.plugins.rest.BasicPluginRestMsg) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PluginApiCallSecurityContext(org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext) ResponseEntity(org.springframework.http.ResponseEntity) TenantId(org.thingsboard.server.common.data.id.TenantId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) UserId(org.thingsboard.server.common.data.id.UserId) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)50 Test (org.junit.Test)24 TenantId (org.thingsboard.server.common.data.id.TenantId)17 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)14 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)13 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)13 PluginId (org.thingsboard.server.common.data.id.PluginId)8 TelemetryStoragePlugin (org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 ArrayList (java.util.ArrayList)4 TextPageData (org.thingsboard.server.common.data.page.TextPageData)4 List (java.util.List)3 UUID (java.util.UUID)3 Before (org.junit.Before)3 Tenant (org.thingsboard.server.common.data.Tenant)3 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)3 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)2