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

org.graylog2.restclient.models.InputService Maven / Gradle / Ivy

The 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.restclient.models;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.lib.ExclusiveInputException;
import org.graylog2.restclient.lib.ServerNodes;
import org.graylog2.restclient.models.api.responses.system.InputLaunchResponse;
import org.graylog2.restclient.models.api.responses.system.InputStateSummaryResponse;
import org.graylog2.restclient.models.api.responses.system.InputTypeSummaryResponse;
import org.graylog2.restclient.models.api.responses.system.InputTypesResponse;
import org.graylog2.restclient.models.api.responses.system.InputsResponse;
import org.graylog2.restroutes.generated.InputsResource;
import org.graylog2.restroutes.generated.routes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dennis Oelkers 
 */
public class InputService {
    private static final Logger log = LoggerFactory.getLogger(NodeService.class);

    private final ApiClient api;
    private final Input.Factory inputFactory;
    private final InputState.Factory inputStateFactory;
    private final NodeService nodeService;
    private final ServerNodes serverNodes;
    private final InputsResource resource = routes.InputsResource();


    @Inject
    public InputService(ApiClient api,
                        Input.Factory inputFactory,
                        InputState.Factory inputStateFactory,
                        NodeService nodeService,
                        ServerNodes serverNodes) {
        this.api = api;
        this.inputFactory = inputFactory;
        this.inputStateFactory = inputStateFactory;
        this.nodeService = nodeService;
        this.serverNodes = serverNodes;
    }

    protected Map getInputsFromAllEntities() {
        Map result = Maps.newHashMap();
        try {
            result.putAll(api.path(resource.list(), InputsResponse.class).fromAllNodes().executeOnAll());
        } catch (APIException e) {
            log.error("Unable to fetch server input list", e);
            return result;
        }
        try {
            for(Radio radio : nodeService.radios().values()) {
                result.put(radio,
                        api.path(routes.radio().InputsResource().list(), InputsResponse.class).radio(radio).execute());
            }
        } catch (APIException e) {
            log.error("Unable to fetch radio list: " + e);
        } catch (IOException e) {
            log.error("Unable to fetch radio list: " + e);
        }
        return result;
    }

    protected List getInputsFromNode(ClusterEntity node) {
        List result = Lists.newArrayList();
        try {
            result = api.path(resource.list(), InputsResponse.class).clusterEntity(node).execute().inputs;
        } catch (APIException e) {
            log.error("Unable to fetch input list: " + e);
        } catch (IOException e) {
            log.error("Unable to fetch input list: " + e);
        }

        return result;
    }

    protected Map getInputsFromAllNodes() {
        Map responseMap = Maps.newHashMap();
        try {
            responseMap.putAll(api.path(resource.list(), InputsResponse.class).fromAllNodes().executeOnAll());
        } catch (APIException e) {
            log.error("Unable to fetch input list: ", e);
        }
        return responseMap;
    }

    public List loadAllInputStates(ClusterEntity node) {
        List inputStates = Lists.newArrayList();

        for (InputStateSummaryResponse inputsResponse : getInputsFromNode(node)) {
            inputStates.add(inputStateFactory.fromSummaryResponse(inputsResponse, node));
        }

        return inputStates;
    }

    public List loadAllInputStates() {
        List inputStates = Lists.newArrayList();

        for (Map.Entry> entry : loadAllInputStatesByEntity().entrySet()) {
            inputStates.addAll(entry.getValue());
        }

        return inputStates;
    }

    public Map> loadAllInputStatesByEntity() {
        Map> result = Maps.newHashMap();
        Map inputsResponseMap = getInputsFromAllEntities();

        for (Map.Entry entry : inputsResponseMap.entrySet()) {
            List nodeList = Lists.newArrayList();
            result.put(entry.getKey(), nodeList);

            for (InputStateSummaryResponse issr : entry.getValue().inputs) {
                nodeList.add(inputStateFactory.fromSummaryResponse(issr, entry.getKey()));
            }
        }

        return result;
    }

    public Map> loadAllInputStatesByInput() {
        Map> inputStatesByNode = loadAllInputStatesByEntity();
        Map> result = Maps.newHashMap();

        for (Map.Entry> nodeEntry : inputStatesByNode.entrySet()) {
            for (InputState inputState : nodeEntry.getValue()) {
                Input input = inputState.getInput();
                if (result.get(input) == null) {
                    Map inputStateMap = Maps.newHashMap();
                    result.put(input, inputStateMap);
                }

                result.get(input).put(nodeEntry.getKey(), inputState);
            }
        }

        return result;
    }

    public Map> getAllInputTypes() throws IOException, APIException {
        Map> result = Maps.newHashMap();
        Map inputTypesResponseMap = api.path(routes.InputTypesResource().types(), InputTypesResponse.class)
                .fromAllNodes().executeOnAll();

        for (Map.Entry entry : inputTypesResponseMap.entrySet())
            result.put(entry.getKey(), entry.getValue().types);

        return result;
    }

    public InputTypeSummaryResponse getInputTypeInformation(Node node, String type) throws IOException, APIException {
        return api.path(routes.InputTypesResource().info(type), InputTypeSummaryResponse.class).node(node).execute();
    }

    public Map getAllInputTypeInformation() throws IOException, APIException {
        Map types = Maps.newHashMap();

        for (Map.Entry> entry : getAllInputTypes().entrySet()) {
            for (String type : entry.getValue().keySet()) {
                try {
                    InputTypeSummaryResponse itr = getInputTypeInformation(entry.getKey(), type);
                    if (itr != null && itr.type != null)
                        types.put(itr.type, itr);
                } catch (IOException | APIException e) {
                    log.error("Unable to get details for input <" + type + ">: ", e);
                }
            }
        }

        return types;
    }

    public String launchGlobal(String title, String type, Map configuration, boolean isExclusive) throws ExclusiveInputException {
        Node master = serverNodes.master();
        InputLaunchResponse ilr = master.launchInput(title, type, true, configuration, isExclusive);

        if (ilr == null) {
            throw new RuntimeException("Unable to launch global input!");
        }

        for (Node serverNode: serverNodes.all()) {
            if (!serverNode.isMaster())
                serverNode.launchExistingInput(ilr.id);
        }
        try {
            for (Radio radio : nodeService.radios().values()) {
                radio.launchExistingInput(ilr.id);
            }
        } catch (APIException | IOException e) {
            log.error("Unable to fetch list of radios: " + e);
        }

        return ilr.id;
    }

    public Map terminateGlobal(String inputId) {
        Map results = Maps.newHashMap();

        for (Node serverNode : serverNodes.all())
            if (!serverNode.isMaster())
                results.put(serverNode, serverNode.terminateInput(inputId));

        try {
            for (Radio radio : nodeService.radios().values())
                results.put(radio, radio.terminateInput(inputId));
        } catch (APIException e) {
            log.error("Unable to fetch list of radios: " + e);
        } catch (IOException e) {
            log.error("Unable to fetch list of radios: " + e);
        }

        Node master = serverNodes.master();
        results.put(master, master.terminateInput(inputId));

        return results;
    }

    public void start(String inputId) throws IOException, APIException {
        Map.Entry target = findNodeAndInputStateForInput(inputId);

        List targetNodes = targetNodesForInput(target);

        for (ClusterEntity targetNode : targetNodes)
            targetNode.startInput(inputId);
    }

    public void stop(String inputId) throws IOException, APIException {
        final Map.Entry target = findNodeAndInputStateForInput(inputId);

        List targetNodes = targetNodesForInput(target);

        for (ClusterEntity targetNode : targetNodes)
            targetNode.stopInput(inputId);
    }

    public void restart(String inputId) throws IOException, APIException {
        final Map.Entry target = findNodeAndInputStateForInput(inputId);

        List targetNodes = targetNodesForInput(target);

        for (ClusterEntity targetNode : targetNodes)
            targetNode.restartInput(inputId);
    }

    protected List targetNodesForInput(Map.Entry target) {
        final List targetNodes = Lists.newArrayList();
        if (target.getValue().getInput().getGlobal()) {
            targetNodes.addAll(serverNodes.all());
            try {
                targetNodes.addAll(nodeService.radios().values());
            } catch (APIException | IOException e) {
                log.error("Unable to fetch list of radios: " + e);
            }
        } else {
            targetNodes.add(target.getKey());
        }

        return targetNodes;
    }

    protected Map.Entry findNodeAndInputStateForInput(String inputId) {
        ClusterEntity targetNode = null;
        InputState targetInputState = null;

        for (final Map.Entry> entry : loadAllInputStatesByEntity().entrySet()) {
            for (final InputState inputState : entry.getValue()) {
                if (inputState.getInput().getPersistId().equals(inputId)) {
                    return new AbstractMap.SimpleImmutableEntry(entry.getKey(), inputState);
                }
            }
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy