Search in sources :

Example 1 with ReferenceMap

use of org.graylog2.contentpacks.model.entities.references.ReferenceMap in project graylog2-server by Graylog2.

the class InputFacade method exportNativeEntity.

@VisibleForTesting
Entity exportNativeEntity(InputWithExtractors inputWithExtractors, EntityDescriptorIds entityDescriptorIds) {
    final Input input = inputWithExtractors.input();
    // TODO: Create independent representation of entity?
    final Map<String, ValueReference> staticFields = input.getStaticFields().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, kv -> ValueReference.of(kv.getValue())));
    final ReferenceMap configuration = toReferenceMap(input.getConfiguration());
    final List<ExtractorEntity> extractors = inputWithExtractors.extractors().stream().map(this::encodeExtractor).collect(Collectors.toList());
    final InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), configuration, staticFields, ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractors);
    final JsonNode data = objectMapper.convertValue(inputEntity, JsonNode.class);
    final Set<Constraint> constraints = versionConstraints(input);
    return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(input.getId(), ModelTypes.INPUT_V1))).type(ModelTypes.INPUT_V1).data(data).constraints(ImmutableSet.copyOf(constraints)).build();
}
Also used : Extractor(org.graylog2.plugin.inputs.Extractor) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Tools(org.graylog2.plugin.Tools) InputService(org.graylog2.inputs.InputService) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) LoggerFactory(org.slf4j.LoggerFactory) ModelType(org.graylog2.contentpacks.model.ModelType) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) InputRegistry(org.graylog2.shared.inputs.InputRegistry) MessageInputFactory(org.graylog2.shared.inputs.MessageInputFactory) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ConverterFactory(org.graylog2.inputs.converters.ConverterFactory) ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) GrokPatternService(org.graylog2.grok.GrokPatternService) ImmutableMap(com.google.common.collect.ImmutableMap) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) Set(java.util.Set) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) ServerStatus(org.graylog2.plugin.ServerStatus) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) List(java.util.List) Stream(java.util.stream.Stream) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) PluginMetaData(org.graylog2.plugin.PluginMetaData) Optional(java.util.Optional) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) Configuration(org.graylog2.plugin.configuration.Configuration) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Converter(org.graylog2.plugin.inputs.Converter) Entity(org.graylog2.contentpacks.model.entities.Entity) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) DBLookupTableService(org.graylog2.lookup.db.DBLookupTableService) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) Input(org.graylog2.inputs.Input) ExtractorFactory(org.graylog2.inputs.extractors.ExtractorFactory) NotFoundException(org.graylog2.database.NotFoundException) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) PluginVersionConstraint(org.graylog2.contentpacks.model.constraints.PluginVersionConstraint) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) Ints(com.google.common.primitives.Ints) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Message(org.graylog2.plugin.Message) ReferenceMapUtils.toValueMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap) Graph(com.google.common.graph.Graph) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) PluginVersionConstraint(org.graylog2.contentpacks.model.constraints.PluginVersionConstraint) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) Input(org.graylog2.inputs.Input) MessageInput(org.graylog2.plugin.inputs.MessageInput) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) ReferenceMapUtils.toValueMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ReferenceMap

use of org.graylog2.contentpacks.model.entities.references.ReferenceMap in project graylog2-server by Graylog2.

the class ReferenceConverter method convert.

@Override
public Reference convert(JsonNode jsonNode) {
    if (jsonNode.isObject()) {
        final ImmutableSet<String> fieldNames = ImmutableSet.copyOf(jsonNode.fieldNames());
        if (fieldNames.equals(EXPECTED_FIELD_NAMES)) {
            // TODO: Possible to use ValueTypeDeserializer to avoid duplication?
            final String valueTypeText = jsonNode.path(ValueReference.FIELD_TYPE).asText();
            final ValueType valueType = ValueType.valueOf(valueTypeText.toUpperCase(Locale.ROOT));
            final JsonNode value = jsonNode.path(ValueReference.FIELD_VALUE);
            if (valueType == ValueType.BOOLEAN && value.isBoolean()) {
                return ValueReference.of(value.booleanValue());
            } else if (valueType == ValueType.DOUBLE && value.isDouble()) {
                return ValueReference.of(value.doubleValue());
            } else if (valueType == ValueType.FLOAT && value.isFloat()) {
                return ValueReference.of(value.floatValue());
            } else if (valueType == ValueType.INTEGER && value.isInt()) {
                return ValueReference.of(value.intValue());
            } else if (valueType == ValueType.LONG && (value.isLong() || value.isInt())) {
                // Jackson actually creates an int value for a small number so we check for both (long and int value) here
                return ValueReference.of(value.longValue());
            } else if (valueType == ValueType.STRING && value.isTextual()) {
                return ValueReference.of(value.textValue());
            } else if (valueType == ValueType.PARAMETER && value.isTextual()) {
                return ValueReference.createParameter(value.textValue());
            } else {
                return null;
            }
        } else {
            final ImmutableMap.Builder<String, Reference> map = ImmutableMap.builder();
            final Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
            while (fields.hasNext()) {
                final Map.Entry<String, JsonNode> entry = fields.next();
                map.put(entry.getKey(), convert(entry.getValue()));
            }
            return new ReferenceMap(map.build());
        }
    } else if (jsonNode.isArray()) {
        final ImmutableList.Builder<Reference> list = ImmutableList.builder();
        for (JsonNode value : jsonNode) {
            list.add(convert(value));
        }
        return new ReferenceList(list.build());
    }
    return null;
}
Also used : ValueType(org.graylog2.contentpacks.model.entities.references.ValueType) Reference(org.graylog2.contentpacks.model.entities.references.Reference) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) ImmutableMap(com.google.common.collect.ImmutableMap) ReferenceList(org.graylog2.contentpacks.model.entities.references.ReferenceList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 ReferenceMap (org.graylog2.contentpacks.model.entities.references.ReferenceMap)2 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Graph (com.google.common.graph.Graph)1 GraphBuilder (com.google.common.graph.GraphBuilder)1 ImmutableGraph (com.google.common.graph.ImmutableGraph)1 MutableGraph (com.google.common.graph.MutableGraph)1 Ints (com.google.common.primitives.Ints)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1