Search in sources :

Example 16 with ComponentDescriptor

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

the class BaseComponentDescriptorControllerTest method testGetByType.

@Test
public void testGetByType() throws Exception {
    List<ComponentDescriptor> descriptors = readResponse(doGet("/api/components/" + ComponentType.PLUGIN).andExpect(status().isOk()), new TypeReference<List<ComponentDescriptor>>() {
    });
    Assert.assertNotNull(descriptors);
    Assert.assertEquals(AMOUNT_OF_DEFAULT_PLUGINS_DESCRIPTORS, descriptors.size());
    for (ComponentType type : ComponentType.values()) {
        doGet("/api/components/" + type).andExpect(status().isOk());
    }
}
Also used : ComponentType(org.thingsboard.server.common.data.plugin.ComponentType) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) List(java.util.List) Test(org.junit.Test)

Example 17 with ComponentDescriptor

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

the class AnnotationComponentDiscoveryService method persist.

private List<ComponentDescriptor> persist(Set<BeanDefinition> filterDefs, ComponentType type) {
    List<ComponentDescriptor> result = new ArrayList<>();
    for (BeanDefinition def : filterDefs) {
        ComponentDescriptor scannedComponent = scanAndPersistComponent(def, type);
        result.add(scannedComponent);
    }
    return result;
}
Also used : ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 18 with ComponentDescriptor

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

the class AnnotationComponentDiscoveryService method scanAndPersistComponent.

private ComponentDescriptor scanAndPersistComponent(BeanDefinition def, ComponentType type) {
    ComponentDescriptor scannedComponent = new ComponentDescriptor();
    String clazzName = def.getBeanClassName();
    try {
        scannedComponent.setType(type);
        Class<?> clazz = Class.forName(clazzName);
        String descriptorResourceName;
        switch(type) {
            case FILTER:
                Filter filterAnnotation = clazz.getAnnotation(Filter.class);
                scannedComponent.setName(filterAnnotation.name());
                scannedComponent.setScope(filterAnnotation.scope());
                descriptorResourceName = filterAnnotation.descriptor();
                break;
            case PROCESSOR:
                Processor processorAnnotation = clazz.getAnnotation(Processor.class);
                scannedComponent.setName(processorAnnotation.name());
                scannedComponent.setScope(processorAnnotation.scope());
                descriptorResourceName = processorAnnotation.descriptor();
                break;
            case ACTION:
                Action actionAnnotation = clazz.getAnnotation(Action.class);
                scannedComponent.setName(actionAnnotation.name());
                scannedComponent.setScope(actionAnnotation.scope());
                descriptorResourceName = actionAnnotation.descriptor();
                break;
            case PLUGIN:
                Plugin pluginAnnotation = clazz.getAnnotation(Plugin.class);
                scannedComponent.setName(pluginAnnotation.name());
                scannedComponent.setScope(pluginAnnotation.scope());
                descriptorResourceName = pluginAnnotation.descriptor();
                for (Class<?> actionClazz : pluginAnnotation.actions()) {
                    ComponentDescriptor actionComponent = getComponent(actionClazz.getName()).orElseThrow(() -> {
                        log.error("Can't initialize plugin {}, due to missing action {}!", def.getBeanClassName(), actionClazz.getName());
                        return new ClassNotFoundException("Action: " + actionClazz.getName() + "is missing!");
                    });
                    if (actionComponent.getType() != ComponentType.ACTION) {
                        log.error("Plugin {} action {} has wrong component type!", def.getBeanClassName(), actionClazz.getName(), actionComponent.getType());
                        throw new RuntimeException("Plugin " + def.getBeanClassName() + "action " + actionClazz.getName() + " has wrong component type!");
                    }
                }
                scannedComponent.setActions(Arrays.stream(pluginAnnotation.actions()).map(action -> action.getName()).collect(Collectors.joining(",")));
                break;
            default:
                throw new RuntimeException(type + " is not supported yet!");
        }
        scannedComponent.setConfigurationDescriptor(mapper.readTree(Resources.toString(Resources.getResource(descriptorResourceName), Charsets.UTF_8)));
        scannedComponent.setClazz(clazzName);
        log.info("Processing scanned component: {}", scannedComponent);
    } catch (Exception e) {
        log.error("Can't initialize component {}, due to {}", def.getBeanClassName(), e.getMessage(), e);
        throw new RuntimeException(e);
    }
    ComponentDescriptor persistedComponent = componentDescriptorService.findByClazz(clazzName);
    if (persistedComponent == null) {
        log.info("Persisting new component: {}", scannedComponent);
        scannedComponent = componentDescriptorService.saveComponent(scannedComponent);
    } else if (scannedComponent.equals(persistedComponent)) {
        log.info("Component is already persisted: {}", persistedComponent);
        scannedComponent = persistedComponent;
    } else {
        log.info("Component {} will be updated to {}", persistedComponent, scannedComponent);
        componentDescriptorService.deleteByClazz(persistedComponent.getClazz());
        scannedComponent.setId(persistedComponent.getId());
        scannedComponent = componentDescriptorService.saveComponent(scannedComponent);
    }
    return scannedComponent;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor)

Example 19 with ComponentDescriptor

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

the class AnnotationComponentDiscoveryService method getPluginActions.

@Override
public List<ComponentDescriptor> getPluginActions(String pluginClazz) {
    Optional<ComponentDescriptor> pluginOpt = getComponent(pluginClazz);
    if (pluginOpt.isPresent()) {
        ComponentDescriptor plugin = pluginOpt.get();
        if (ComponentType.PLUGIN != plugin.getType()) {
            throw new IllegalArgumentException(pluginClazz + " is not a plugin!");
        }
        List<ComponentDescriptor> result = new ArrayList<>();
        for (String action : plugin.getActions().split(",")) {
            getComponent(action).ifPresent(v -> result.add(v));
        }
        return result;
    } else {
        throw new IllegalArgumentException(pluginClazz + " is not a component!");
    }
}
Also used : ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor)

Aggregations

ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)19 Test (org.junit.Test)5 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ComponentDescriptorId (org.thingsboard.server.common.data.id.ComponentDescriptorId)3 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)2 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)2 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1 MessagingException (javax.mail.MessagingException)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)1 Device (org.thingsboard.server.common.data.Device)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)1 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 ComponentType (org.thingsboard.server.common.data.plugin.ComponentType)1 DeviceCredentialsFilter (org.thingsboard.server.common.data.security.DeviceCredentialsFilter)1