Search in sources :

Example 1 with ProvisionRequest

use of org.thingsboard.server.dao.device.provision.ProvisionRequest in project thingsboard by thingsboard.

the class AuditLogServiceImpl method constructActionData.

private <E extends HasName, I extends EntityId> JsonNode constructActionData(I entityId, E entity, ActionType actionType, Object... additionalInfo) {
    ObjectNode actionData = JacksonUtil.newObjectNode();
    switch(actionType) {
        case ADDED:
        case UPDATED:
        case ALARM_ACK:
        case ALARM_CLEAR:
        case RELATIONS_DELETED:
        case ASSIGNED_TO_TENANT:
            if (entity != null) {
                ObjectNode entityNode = (ObjectNode) JacksonUtil.valueToTree(entity);
                if (entityId.getEntityType() == EntityType.DASHBOARD) {
                    entityNode.put("configuration", "");
                }
                actionData.set("entity", entityNode);
            }
            if (entityId.getEntityType() == EntityType.RULE_CHAIN) {
                RuleChainMetaData ruleChainMetaData = extractParameter(RuleChainMetaData.class, additionalInfo);
                if (ruleChainMetaData != null) {
                    ObjectNode ruleChainMetaDataNode = (ObjectNode) JacksonUtil.valueToTree(ruleChainMetaData);
                    actionData.set("metadata", ruleChainMetaDataNode);
                }
            }
            break;
        case DELETED:
        case ACTIVATED:
        case SUSPENDED:
        case CREDENTIALS_READ:
            String strEntityId = extractParameter(String.class, additionalInfo);
            actionData.put("entityId", strEntityId);
            break;
        case ATTRIBUTES_UPDATED:
            actionData.put("entityId", entityId.toString());
            String scope = extractParameter(String.class, 0, additionalInfo);
            @SuppressWarnings("unchecked") List<AttributeKvEntry> attributes = extractParameter(List.class, 1, additionalInfo);
            actionData.put("scope", scope);
            ObjectNode attrsNode = JacksonUtil.newObjectNode();
            if (attributes != null) {
                for (AttributeKvEntry attr : attributes) {
                    attrsNode.put(attr.getKey(), attr.getValueAsString());
                }
            }
            actionData.set("attributes", attrsNode);
            break;
        case ATTRIBUTES_DELETED:
        case ATTRIBUTES_READ:
            actionData.put("entityId", entityId.toString());
            scope = extractParameter(String.class, 0, additionalInfo);
            actionData.put("scope", scope);
            @SuppressWarnings("unchecked") List<String> keys = extractParameter(List.class, 1, additionalInfo);
            ArrayNode attrsArrayNode = actionData.putArray("attributes");
            if (keys != null) {
                keys.forEach(attrsArrayNode::add);
            }
            break;
        case RPC_CALL:
            actionData.put("entityId", entityId.toString());
            Boolean oneWay = extractParameter(Boolean.class, 1, additionalInfo);
            String method = extractParameter(String.class, 2, additionalInfo);
            String params = extractParameter(String.class, 3, additionalInfo);
            actionData.put("oneWay", oneWay);
            actionData.put("method", method);
            actionData.put("params", params);
            break;
        case CREDENTIALS_UPDATED:
            actionData.put("entityId", entityId.toString());
            DeviceCredentials deviceCredentials = extractParameter(DeviceCredentials.class, additionalInfo);
            actionData.set("credentials", JacksonUtil.valueToTree(deviceCredentials));
            break;
        case ASSIGNED_TO_CUSTOMER:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            String strCustomerId = extractParameter(String.class, 1, additionalInfo);
            String strCustomerName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("assignedCustomerId", strCustomerId);
            actionData.put("assignedCustomerName", strCustomerName);
            break;
        case UNASSIGNED_FROM_CUSTOMER:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            strCustomerId = extractParameter(String.class, 1, additionalInfo);
            strCustomerName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("unassignedCustomerId", strCustomerId);
            actionData.put("unassignedCustomerName", strCustomerName);
            break;
        case RELATION_ADD_OR_UPDATE:
        case RELATION_DELETED:
            EntityRelation relation = extractParameter(EntityRelation.class, 0, additionalInfo);
            actionData.set("relation", JacksonUtil.valueToTree(relation));
            break;
        case LOGIN:
        case LOGOUT:
        case LOCKOUT:
            String clientAddress = extractParameter(String.class, 0, additionalInfo);
            String browser = extractParameter(String.class, 1, additionalInfo);
            String os = extractParameter(String.class, 2, additionalInfo);
            String device = extractParameter(String.class, 3, additionalInfo);
            actionData.put("clientAddress", clientAddress);
            actionData.put("browser", browser);
            actionData.put("os", os);
            actionData.put("device", device);
            break;
        case PROVISION_SUCCESS:
        case PROVISION_FAILURE:
            ProvisionRequest request = extractParameter(ProvisionRequest.class, additionalInfo);
            if (request != null) {
                actionData.set("provisionRequest", JacksonUtil.valueToTree(request));
            }
            break;
        case TIMESERIES_UPDATED:
            actionData.put("entityId", entityId.toString());
            @SuppressWarnings("unchecked") List<TsKvEntry> updatedTimeseries = extractParameter(List.class, 0, additionalInfo);
            if (updatedTimeseries != null) {
                ArrayNode result = actionData.putArray("timeseries");
                updatedTimeseries.stream().collect(Collectors.groupingBy(TsKvEntry::getTs)).forEach((k, v) -> {
                    ObjectNode element = JacksonUtil.newObjectNode();
                    element.put("ts", k);
                    ObjectNode values = element.putObject("values");
                    v.forEach(kvEntry -> values.put(kvEntry.getKey(), kvEntry.getValueAsString()));
                    result.add(element);
                });
            }
            break;
        case TIMESERIES_DELETED:
            actionData.put("entityId", entityId.toString());
            @SuppressWarnings("unchecked") List<String> timeseriesKeys = extractParameter(List.class, 0, additionalInfo);
            if (timeseriesKeys != null) {
                ArrayNode timeseriesArrayNode = actionData.putArray("timeseries");
                timeseriesKeys.forEach(timeseriesArrayNode::add);
            }
            actionData.put("startTs", extractParameter(Long.class, 1, additionalInfo));
            actionData.put("endTs", extractParameter(Long.class, 2, additionalInfo));
            break;
        case ASSIGNED_TO_EDGE:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            String strEdgeId = extractParameter(String.class, 1, additionalInfo);
            String strEdgeName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("assignedEdgeId", strEdgeId);
            actionData.put("assignedEdgeName", strEdgeName);
            break;
        case UNASSIGNED_FROM_EDGE:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            strEdgeId = extractParameter(String.class, 1, additionalInfo);
            strEdgeName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("unassignedEdgeId", strEdgeId);
            actionData.put("unassignedEdgeName", strEdgeName);
            break;
    }
    return actionData;
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ProvisionRequest(org.thingsboard.server.dao.device.provision.ProvisionRequest)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)1 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)1 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)1 ProvisionRequest (org.thingsboard.server.dao.device.provision.ProvisionRequest)1