All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.streams.StreamRuleImpl Maven / Gradle / Ivy

There is a newer version: 6.0.5
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.streams;

import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.bson.types.ObjectId;
import org.graylog2.database.CollectionName;
import org.graylog2.database.PersistedImpl;
import org.graylog2.database.validators.FilledStringValidator;
import org.graylog2.database.validators.IntegerValidator;
import org.graylog2.database.validators.ObjectIdValidator;
import org.graylog2.database.validators.OptionalStringValidator;
import org.graylog2.plugin.database.validators.Validator;
import org.graylog2.plugin.streams.StreamRule;
import org.graylog2.plugin.streams.StreamRuleType;

import java.util.Collections;
import java.util.Map;

import static com.google.common.base.MoreObjects.firstNonNull;

/**
 * Representing the rules of a single stream.
 */
@CollectionName("streamrules")
public class StreamRuleImpl extends PersistedImpl implements StreamRule {
    public static final String FIELD_TYPE = "type";
    public static final String FIELD_VALUE = "value";
    public static final String FIELD_FIELD = "field";
    public static final String FIELD_INVERTED = "inverted";
    public static final String FIELD_STREAM_ID = "stream_id";
    public static final String FIELD_CONTENT_PACK = "content_pack";
    public static final String FIELD_DESCRIPTION = "description";

    public StreamRuleImpl(Map fields) {
        super(fields);
    }

    protected StreamRuleImpl(ObjectId id, Map fields) {
        super(id, fields);
    }

    @Override
    public StreamRuleType getType() {
        return StreamRuleType.fromInteger((Integer) fields.get(FIELD_TYPE));
    }

    @Override
    public void setType(StreamRuleType type) {
        fields.put(FIELD_TYPE, type.toInteger());
    }

    @Override
    public String getValue() {
        return (String) fields.get(FIELD_VALUE);
    }

    @Override
    public void setValue(String value) {
        fields.put(FIELD_VALUE, value);
    }

    @Override
    public String getField() {
        return (String) fields.get(FIELD_FIELD);
    }

    @Override
    public void setField(String field) {
        fields.put(FIELD_FIELD, field);
    }

    @Override
    public Boolean getInverted() {
        return (Boolean) firstNonNull(fields.get(FIELD_INVERTED), false);
    }

    @Override
    public void setInverted(Boolean inverted) {
        fields.put(FIELD_INVERTED, inverted);
    }

    @Override
    public String getStreamId() {
        return ((ObjectId) fields.get(FIELD_STREAM_ID)).toHexString();
    }

    @Override
    public String getContentPack() {
        return (String) fields.get(FIELD_CONTENT_PACK);
    }

    @Override
    public void setContentPack(String contentPack) {
        fields.put(FIELD_CONTENT_PACK, contentPack);
    }

    @Override
    public String getDescription() {
        return (String) fields.get(FIELD_DESCRIPTION);
    }

    @Override
    public void setDescription(String description) {
        fields.put(FIELD_DESCRIPTION, description);
    }

    @Override
    public Map getValidations() {
        final ImmutableMap.Builder validators = ImmutableMap.builder();
        validators.put(FIELD_TYPE, new IntegerValidator());
        validators.put(FIELD_FIELD, new FilledStringValidator());
        validators.put(FIELD_STREAM_ID, new ObjectIdValidator());
        validators.put(FIELD_CONTENT_PACK, new OptionalStringValidator());

        if (!this.getType().equals(StreamRuleType.PRESENCE)) {
            validators.put(FIELD_VALUE, new FilledStringValidator());
        }

        return validators.build();
    }

    @Override
    public Map getEmbeddedValidations(String key) {
        return Collections.emptyMap();
    }

    @JsonValue
    @Override
    public Map asMap() {
        // We work on the result a bit to allow correct JSON serializing.
        Map result = Maps.newHashMap(fields);
        result.remove("_id");
        result.put("id", getId());
        result.put(FIELD_STREAM_ID, getStreamId());

        return result;
    }

    @Override
    public String toString() {
        return ("StreamRuleImpl: <" + this.fields.toString() + ">");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy