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

org.graylog.events.contentpack.entities.EventNotificationHandlerConfigEntity Maven / Gradle / Ivy

There is a newer version: 5.2.7
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.graylog.events.contentpack.entities;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import org.graylog.events.notifications.EventNotificationHandler;
import org.graylog.events.notifications.NotificationDto;
import org.graylog.events.notifications.NotificationParameters;
import org.graylog2.contentpacks.NativeEntityConverter;
import org.graylog2.contentpacks.exceptions.ContentPackException;
import org.graylog2.contentpacks.model.ModelTypes;
import org.graylog2.contentpacks.model.entities.EntityDescriptor;
import org.graylog2.contentpacks.model.entities.references.ValueReference;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Optional;

@AutoValue
@JsonDeserialize(builder = EventNotificationHandlerConfigEntity.Builder.class)
public abstract class EventNotificationHandlerConfigEntity implements NativeEntityConverter {

    private static final String FIELD_NOTIFICATION_ID = "notification_id";
    private static final String FIELD_NOTIFICATION_PARAMETERS = "notification_parameters";

    @JsonProperty(FIELD_NOTIFICATION_ID)
    public abstract ValueReference notificationId();

    @JsonProperty(FIELD_NOTIFICATION_PARAMETERS)
    public abstract Optional notificationParameters();

    public static Builder builder() {
        return Builder.create();
    }

    public abstract Builder toBuilder();

    @AutoValue.Builder
    public static abstract class Builder {

        @JsonCreator
        public static Builder create() {
            return new AutoValue_EventNotificationHandlerConfigEntity.Builder();
        }

        @JsonProperty(FIELD_NOTIFICATION_ID)
        public abstract Builder notificationId(ValueReference notificationId);

        @JsonProperty(FIELD_NOTIFICATION_PARAMETERS)
        public abstract Builder notificationParameters(@Nullable NotificationParameters notificationParameters);

        public abstract EventNotificationHandlerConfigEntity build();
    }

    @Override
    public EventNotificationHandler.Config toNativeEntity(Map parameters, Map natvieEntities) {
        String notificationId = notificationId().asString(parameters);
        final EntityDescriptor notificationDescriptor = EntityDescriptor.create(notificationId, ModelTypes.NOTIFICATION_V1);
        final Object notification = natvieEntities.get(notificationDescriptor);

        final EventNotificationHandler.Config.Builder configBuilder = EventNotificationHandler.Config.builder();

        if (notification == null) {
            throw new ContentPackException("Missing notification (" + notificationId + ") for event definition");
        } else if (notification instanceof NotificationDto) {
            NotificationDto notificationDto = (NotificationDto) notification;
            configBuilder.notificationId(notificationDto.id());
        } else {
            throw new ContentPackException("Invalid type for notification (" + notificationId + ") of event definition: " + notification.getClass());
        }

        return configBuilder.notificationParameters(notificationParameters().orElse(null))
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy