Browse Source

Revert particular methods

pull/2436/head
Volodymyr Babak 6 years ago
parent
commit
f5ab5d7a25
  1. 22
      application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java
  2. 18
      application/src/main/java/org/thingsboard/server/controller/BaseController.java
  3. 43
      application/src/main/java/org/thingsboard/server/controller/ComponentDescriptorController.java
  4. 32
      application/src/main/java/org/thingsboard/server/service/component/AnnotationComponentDiscoveryService.java
  5. 4
      application/src/main/java/org/thingsboard/server/service/component/ComponentDiscoveryService.java
  6. 4
      rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java
  7. 8
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbAbstractAlarmNode.java

22
application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java

@ -249,28 +249,8 @@ class DefaultTbContext implements TbContext {
}
public <E, I extends EntityId> TbMsg entityCreatedMsg(E entity, I id, RuleNodeId ruleNodeId) {
return entityCRUDMsg(entity, id, ruleNodeId, DataConstants.ENTITY_CREATED);
}
public <E, I extends EntityId> TbMsg entityCRUDMsg(E entity, I id, RuleNodeId ruleNodeId, String actionType) {
try {
return TbMsg.newMsg(actionType, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity)));
} catch (JsonProcessingException | IllegalArgumentException e) {
throw new RuntimeException("Failed to process " + id.getEntityType().name().toLowerCase() + " " + actionType + " msg: " + e);
}
}
public TbMsg alarmUpdatedMsg(Alarm alarm, RuleNodeId ruleNodeId) {
return entityCRUDMsg(alarm, alarm.getId(), ruleNodeId, DataConstants.ENTITY_UPDATED);
}
public TbMsg alarmClearedMsg(Alarm alarm, RuleNodeId ruleNodeId) {
return entityCRUDMsg(alarm, alarm.getId(), ruleNodeId, DataConstants.ALARM_CLEAR);
}
public <E, I extends EntityId> TbMsg alarmMsg(E entity, I id, RuleNodeId ruleNodeId, String actionType) {
try {
return TbMsg.newMsg(actionType, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity)));
return TbMsg.newMsg(DataConstants.ENTITY_CREATED, id, getActionMetaData(ruleNodeId), mapper.writeValueAsString(mapper.valueToTree(entity)));
} catch (JsonProcessingException | IllegalArgumentException e) {
throw new RuntimeException("Failed to process " + id.getEntityType().name().toLowerCase() + " created msg: " + e);
}

18
application/src/main/java/org/thingsboard/server/controller/BaseController.java

@ -530,6 +530,24 @@ public abstract class BaseController {
}
}
ComponentDescriptor checkComponentDescriptorByClazz(String clazz) throws ThingsboardException {
try {
log.debug("[{}] Lookup component descriptor", clazz);
return checkNotNull(componentDescriptorService.getComponent(clazz));
} catch (Exception e) {
throw handleException(e, false);
}
}
List<ComponentDescriptor> checkComponentDescriptorsByType(ComponentType type, RuleChainType ruleChainType) throws ThingsboardException {
try {
log.debug("[{}] Lookup component descriptors", type);
return componentDescriptorService.getComponents(type, ruleChainType);
} catch (Exception e) {
throw handleException(e, false);
}
}
List<ComponentDescriptor> checkComponentDescriptorsByTypes(Set<ComponentType> types, RuleChainType ruleChainType) throws ThingsboardException {
try {
log.debug("[{}] Lookup component descriptors", types);

43
application/src/main/java/org/thingsboard/server/controller/ComponentDescriptorController.java

@ -15,6 +15,7 @@
*/
package org.thingsboard.server.controller;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@ -25,8 +26,8 @@ import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.common.data.rule.RuleChainType;
import org.thingsboard.server.queue.util.TbCoreComponent;
import java.util.HashSet;
import java.util.List;
@ -37,24 +38,56 @@ import java.util.Set;
@RequestMapping("/api")
public class ComponentDescriptorController extends BaseController {
@PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN')")
@RequestMapping(value = "/component/{componentDescriptorClazz:.+}", method = RequestMethod.GET)
@ResponseBody
public ComponentDescriptor getComponentDescriptorByClazz(@PathVariable("componentDescriptorClazz") String strComponentDescriptorClazz) throws ThingsboardException {
checkParameter("strComponentDescriptorClazz", strComponentDescriptorClazz);
try {
return checkComponentDescriptorByClazz(strComponentDescriptorClazz);
} catch (Exception e) {
throw handleException(e);
}
}
@PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN')")
@RequestMapping(value = "/components/{componentType}/{ruleChainType}", method = RequestMethod.GET)
@ResponseBody
public List<ComponentDescriptor> getComponentDescriptorsByType(@PathVariable(value = "ruleChainType", required = false) String strRuleChainType,
@PathVariable("componentType") String strComponentType) throws ThingsboardException {
checkParameter("componentType", strComponentType);
try {
return checkComponentDescriptorsByType(ComponentType.valueOf(strComponentType), getRuleChainType(strRuleChainType));
} catch (Exception e) {
throw handleException(e);
}
}
@PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN')")
@RequestMapping(value = "/components/{ruleChainType}", params = {"componentTypes"}, method = RequestMethod.GET)
@ResponseBody
public List<ComponentDescriptor> getComponentDescriptorsByTypes(@PathVariable("ruleChainType") String strRuleChainType,
public List<ComponentDescriptor> getComponentDescriptorsByTypes(@PathVariable(value = "ruleChainType", required = false) String strRuleChainType,
@RequestParam("componentTypes") String[] strComponentTypes) throws ThingsboardException {
checkArrayParameter("componentTypes", strComponentTypes);
checkParameter("ruleChainType", strRuleChainType);
try {
RuleChainType ruleChainType = RuleChainType.valueOf(strRuleChainType);
Set<ComponentType> componentTypes = new HashSet<>();
for (String strComponentType : strComponentTypes) {
componentTypes.add(ComponentType.valueOf(strComponentType));
}
return checkComponentDescriptorsByTypes(componentTypes, ruleChainType);
return checkComponentDescriptorsByTypes(componentTypes, getRuleChainType(strRuleChainType));
} catch (Exception e) {
throw handleException(e);
}
}
private RuleChainType getRuleChainType(String strRuleChainType) {
RuleChainType ruleChainType;
if (StringUtils.isEmpty(strRuleChainType)) {
ruleChainType = RuleChainType.CORE;
} else {
ruleChainType = RuleChainType.valueOf(strRuleChainType);
}
return ruleChainType;
}
}

32
application/src/main/java/org/thingsboard/server/service/component/AnnotationComponentDiscoveryService.java

@ -40,7 +40,6 @@ import javax.annotation.PostConstruct;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -65,7 +64,7 @@ public class AnnotationComponentDiscoveryService implements ComponentDiscoverySe
private Map<String, ComponentDescriptor> components = new HashMap<>();
private Map<ComponentType, List<ComponentDescriptor>> systemComponentsMap = new HashMap<>();
private Map<ComponentType, List<ComponentDescriptor>> coreComponentsMap = new HashMap<>();
private Map<ComponentType, List<ComponentDescriptor>> edgeComponentsMap = new HashMap<>();
@ -117,7 +116,7 @@ public class AnnotationComponentDiscoveryService implements ComponentDiscoverySe
private void putComponentIntoMaps(ComponentType type, RuleNode ruleNodeAnnotation, ComponentDescriptor component) {
if (ruleChainTypeContainsArray(RuleChainType.CORE, ruleNodeAnnotation.ruleChainTypes())) {
systemComponentsMap.computeIfAbsent(type, k -> new ArrayList<>()).add(component);
coreComponentsMap.computeIfAbsent(type, k -> new ArrayList<>()).add(component);
}
if (ruleChainTypeContainsArray(RuleChainType.EDGE, ruleNodeAnnotation.ruleChainTypes())) {
edgeComponentsMap.computeIfAbsent(type, k -> new ArrayList<>()).add(component);
@ -223,10 +222,30 @@ public class AnnotationComponentDiscoveryService implements ComponentDiscoverySe
log.info("Found following definitions: {}", components.values());
}
@Override
public List<ComponentDescriptor> getComponents(ComponentType type, RuleChainType ruleChainType) {
if (RuleChainType.CORE.equals(ruleChainType)) {
if (coreComponentsMap.containsKey(type)) {
return Collections.unmodifiableList(coreComponentsMap.get(type));
} else {
return Collections.emptyList();
}
} else if (RuleChainType.EDGE.equals(ruleChainType)) {
if (edgeComponentsMap.containsKey(type)) {
return Collections.unmodifiableList(edgeComponentsMap.get(type));
} else {
return Collections.emptyList();
}
} else {
log.error("Unsupported rule chain type {}", ruleChainType);
throw new RuntimeException("Unsupported rule chain type " + ruleChainType);
}
}
@Override
public List<ComponentDescriptor> getComponents(Set<ComponentType> types, RuleChainType ruleChainType) {
if (RuleChainType.CORE.equals(ruleChainType)) {
return getComponents(types, systemComponentsMap);
return getComponents(types, coreComponentsMap);
} else if (RuleChainType.EDGE.equals(ruleChainType)) {
return getComponents(types, edgeComponentsMap);
} else {
@ -235,6 +254,11 @@ public class AnnotationComponentDiscoveryService implements ComponentDiscoverySe
}
}
@Override
public Optional<ComponentDescriptor> getComponent(String clazz) {
return Optional.ofNullable(components.get(clazz));
}
private List<ComponentDescriptor> getComponents(Set<ComponentType> types, Map<ComponentType, List<ComponentDescriptor>> componentsMap) {
List<ComponentDescriptor> result = new ArrayList<>();
types.stream().filter(componentsMap::containsKey).forEach(type -> {

4
application/src/main/java/org/thingsboard/server/service/component/ComponentDiscoveryService.java

@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.data.rule.RuleChainType;
import java.util.List;
import java.util.Optional;
import java.util.Set;
/**
@ -29,6 +30,9 @@ public interface ComponentDiscoveryService {
void discoverComponents();
List<ComponentDescriptor> getComponents(ComponentType type, RuleChainType ruleChainType);
List<ComponentDescriptor> getComponents(Set<ComponentType> types, RuleChainType ruleChainType);
Optional<ComponentDescriptor> getComponent(String clazz);
}

4
rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java

@ -145,10 +145,6 @@ public interface TbContext {
// TODO: Does this changes the message?
TbMsg alarmCreatedMsg(Alarm alarm, RuleNodeId ruleNodeId);
TbMsg alarmUpdatedMsg(Alarm alarm, RuleNodeId ruleNodeId);
TbMsg alarmClearedMsg(Alarm alarm, RuleNodeId ruleNodeId);
/*
*
* METHODS TO PROCESS THE MESSAGES

8
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbAbstractAlarmNode.java

@ -65,13 +65,9 @@ public abstract class TbAbstractAlarmNode<C extends TbAbstractAlarmNodeConfigura
() -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Created"),
throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable));
} else if (alarmResult.isUpdated) {
ctx.enqueue(ctx.alarmUpdatedMsg(alarmResult.alarm, ctx.getSelfId()),
() -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Updated"),
throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable));
ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Updated");
} else if (alarmResult.isCleared) {
ctx.enqueue(ctx.alarmClearedMsg(alarmResult.alarm, ctx.getSelfId()),
() -> ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Cleared"),
throwable -> ctx.tellFailure(toAlarmMsg(ctx, alarmResult, msg), throwable));
ctx.tellNext(toAlarmMsg(ctx, alarmResult, msg), "Cleared");
} else {
ctx.tellSuccess(msg);
}

Loading…
Cancel
Save