com.ottogroup.bi.asap.resman.pipeline.PipelineManager.java Source code

Java tutorial

Introduction

Here is the source code for com.ottogroup.bi.asap.resman.pipeline.PipelineManager.java

Source

/**
 * Copyright 2014 Otto (GmbH & Co KG)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ottogroup.bi.asap.resman.pipeline;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ottogroup.bi.asap.exception.PipelineAlreadyRegisteredException;
import com.ottogroup.bi.asap.exception.RemoteClientConnectionFailedException;
import com.ottogroup.bi.asap.exception.RequiredInputMissingException;
import com.ottogroup.bi.asap.exception.UnknownPipelineException;
import com.ottogroup.bi.asap.pipeline.MicroPipelineConfiguration;
import com.ottogroup.bi.asap.resman.node.ProcessingNodeManager;

/**
 * Registers and manages all deployed pipelines. It receives notification from the {@link ProcessingNodeManager}
 * where each pipeline is deployed and uses the manager to access it for further access. 
 * @author mnxfst
 * @since Nov 28, 2014
 * TODO validate pipeline configuration
 * TODO testing
 */
public class PipelineManager {

    private final Map<String, PipelineDeploymentProfile> pipelineDeployments = new HashMap<>();
    private final ProcessingNodeManager processingNodeManager;

    /**
     * Initializes the manager instance using the provided input
     * @param processingNodeManager provides access to processing nodes (required)
     * @throws RequiredInputMissingException
     */
    public PipelineManager(final ProcessingNodeManager processingNodeManager) throws RequiredInputMissingException {

        ///////////////////////////////////////////////////////////
        // validate input
        if (processingNodeManager == null)
            throw new RequiredInputMissingException("Missing required reference to processing node manager");
        //
        ///////////////////////////////////////////////////////////

        this.processingNodeManager = processingNodeManager;
    }

    /**
     * Instantiates the provided pipeline on a set of processing nodes
     * @param pipelineConfiguration
     * @return
     * @throws RequiredInputMissingException
     * @throws RemoteClientConnectionFailedException 
     * @throws IOException 
     */
    public PipelineDeploymentProfile instantiatePipeline(final MicroPipelineConfiguration pipelineConfiguration)
            throws RequiredInputMissingException, PipelineAlreadyRegisteredException {

        ///////////////////////////////////////////////////////////
        // validate input
        if (pipelineConfiguration == null)
            throw new RequiredInputMissingException("Missing required pipeline configuration");
        String pid = StringUtils.lowerCase(StringUtils.trim(pipelineConfiguration.getId()));
        if (this.pipelineDeployments.containsKey(pid))
            throw new PipelineAlreadyRegisteredException("A pipeline already exists for identifier '"
                    + pipelineConfiguration.getId() + "'. To replace an existing pipeline use updateOrInstantiate");
        // TODO validate pipeline configuration
        //
        ///////////////////////////////////////////////////////////

        final PipelineDeploymentProfile profile = processingNodeManager.instantiatePipeline(
                new PipelineDeploymentProfile(pipelineConfiguration.getId(), pipelineConfiguration));
        this.pipelineDeployments.put(StringUtils.lowerCase(StringUtils.trim(pipelineConfiguration.getId())),
                profile);

        return profile;
    }

    /**
     * Updates or instantiates the provided pipeline on a set of processing nodes
     * @param pipelineConfiguration
     * @return
     * @throws RequiredInputMissingException
     * @throws RemoteClientConnectionFailedException 
     * @throws IOException 
     */
    public PipelineDeploymentProfile updatesOrInstantiatePipeline(
            final MicroPipelineConfiguration pipelineConfiguration)
            throws RequiredInputMissingException, PipelineAlreadyRegisteredException {

        ///////////////////////////////////////////////////////////
        // validate input
        if (pipelineConfiguration == null)
            throw new RequiredInputMissingException("Missing required pipeline configuration");
        // TODO validate pipeline configuration
        //
        ///////////////////////////////////////////////////////////

        String pid = StringUtils.lowerCase(StringUtils.trim(pipelineConfiguration.getId()));
        PipelineDeploymentProfile profile = this.pipelineDeployments.get(pid);
        if (profile != null) {
            profile.getProcessingNodes().clear();
            profile.setConfiguration(pipelineConfiguration);
        } else {
            profile = new PipelineDeploymentProfile(pipelineConfiguration.getId(), pipelineConfiguration);
        }

        profile = processingNodeManager.updateOrInstantiatePipeline(
                new PipelineDeploymentProfile(pipelineConfiguration.getId(), pipelineConfiguration));
        this.pipelineDeployments.put(StringUtils.lowerCase(StringUtils.trim(pipelineConfiguration.getId())),
                profile);

        return profile;
    }

    /**
     * Shuts down the pipeline on all processing node it has been deployed to
     * @param pipelineId
     * @return
     * @throws RequiredInputMissingException
     * @throws UnknownPipelineException
     */
    public PipelineDeploymentProfile shutdownPipeline(final String pipelineId)
            throws RequiredInputMissingException, UnknownPipelineException {

        ///////////////////////////////////////////////////////////
        // validate input
        if (StringUtils.isBlank(pipelineId))
            throw new RequiredInputMissingException("Missing required pipeline identifier");
        String pid = StringUtils.lowerCase(StringUtils.trim(pipelineId));
        if (!this.pipelineDeployments.containsKey(pid))
            throw new UnknownPipelineException("Unknown pipeline '" + pipelineId + "'");
        //
        ///////////////////////////////////////////////////////////

        final PipelineDeploymentProfile profile = this.pipelineDeployments.remove(pid);
        return this.processingNodeManager.shutdownPipeline(profile);
    }

}