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

com.ibm.cp4waiops.connectors.sdk.ConfigurationEventConsumer Maven / Gradle / Ivy

package com.ibm.cp4waiops.connectors.sdk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ibm.aiops.connectors.bridge.ConnectorBridgeGrpc.ConnectorBridgeStub;

import io.cloudevents.CloudEvent;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

/**
 * Manages GRPCCloudEventConfigurationConsumeChannel instances, creating and destroying them as needed
 */
class ConfigurationEventConsumer {
    private static final Logger logger = Logger.getLogger(ConfigurationEventConsumer.class.getName());

    private static class ChannelConfigurationEntry {
        public CloudEvent requestTemplate;
        public GRPCCloudEventConfigurationChannel channel;
    }

    private ConnectorBridgeStub _stub;
    private AtomicReference _latestReceivedConfiguration;
    private HashMap _configChannels;

    protected MeterRegistry _meterRegistry;

    /**
     * Constructs an instance
     * 
     * @param stub
     *            the interface used communicate with the bridge server
     * @param actionQueue
     *            where consumed events are placed
     * @param meterRegistry
     *            a registry for channel metrics
     */
    ConfigurationEventConsumer(ConnectorBridgeStub stub, AtomicReference latestReceivedConfiguration,
            MeterRegistry meterRegistry) {
        _stub = stub;
        _latestReceivedConfiguration = latestReceivedConfiguration;
        _configChannels = new HashMap<>();
        _meterRegistry = meterRegistry;
    }

    /**
     * 
     * Set up configuration channel Creates the target channel if it does not exist
     * 
     * @param name
     *            the channel name that will be sent to the server, most likely the name of the kafka topic
     * @param requestTemplate
     *            a template for information sent to the server when starting the channel
     */
    void setupConfigurationChannel(String name, CloudEvent requestTemplate, Counter startConfigrationCount) {
        // Reuse existing stream if no change is needed
        ChannelConfigurationEntry existing = _configChannels.get(name);
        if (existing != null && existing.requestTemplate.equals(requestTemplate)) {
            return;
        }
        // Terminate existing channel
        if (existing != null) {
            logger.log(Level.INFO, "channel request template changed, restarting: name=" + name);
            existing.channel.close();
        }
        // Start new channel
        ChannelConfigurationEntry entry = new ChannelConfigurationEntry();
        entry.requestTemplate = requestTemplate;
        entry.channel = new GRPCCloudEventConfigurationChannel(name, _stub, requestTemplate,
                _latestReceivedConfiguration, _meterRegistry, startConfigrationCount);
        entry.channel.startConfigrationChannel();
        _configChannels.put(name, entry);
    }

    /**
     * Closes all active configuration consumer channels except those listed
     * 
     * @param excludedNames
     */
    void closeAllChannelsExcept(List excludedNames) {
        ArrayList toRemove = new ArrayList<>();

        for (var entry : _configChannels.entrySet()) {
            if (excludedNames.contains(entry.getKey())) {
                continue;
            }
            logger.log(Level.INFO, "channel no longer requested, closing: name=" + entry.getKey());
            toRemove.add(entry.getKey());
            entry.getValue().channel.close();
        }
        for (String name : toRemove) {
            _configChannels.remove(name);
        }
    }

    /**
     * Closes all active configuration consumer channels
     */
    void closeAllChannels() {
        closeAllChannelsExcept(new ArrayList<>());
    }

    /**
     * Restarts any channel that have stopped due to connection errors
     */
    void fixChannels() {

        for (var entry : _configChannels.entrySet()) {
            entry.getValue().channel.fixChannelIfStopped();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy