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

com.boozallen.aiops.mda.pattern.IngestAlertingDisabledSyncBase Maven / Gradle / Ivy

package com.boozallen.aiops.mda.pattern;

/*-
 * #%L
 * aiSSEMBLE::Test::MDA::Data Delivery Spark
 * %%
 * Copyright (C) 2021 Booz Allen
 * %%
 * This software package is licensed under the Booz Allen Public License. All Rights Reserved.
 * #L%
 */

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import com.boozallen.aissemble.core.metadata.MetadataAPI;
import com.boozallen.aissemble.core.metadata.MetadataModel;
import com.boozallen.aissemble.core.metadata.producer.MetadataProducer;

import com.boozallen.aissemble.core.filestore.AbstractFileStore;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.spark.sql.api.java.UDF1;
import org.apache.spark.sql.api.java.UDF2;
import com.boozallen.aissemble.data.encryption.policy.config.EncryptAlgorithm;
import org.apache.spark.sql.types.DataTypes;

import javax.inject.Inject;
import java.util.Map;
import java.util.HashMap;
import com.boozallen.aiops.mda.pattern.pipeline.PipelineBase;
import java.util.UUID;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;

import com.boozallen.aissemble.data.lineage.util.LineageUtil.LineageEventData;
import static com.boozallen.aissemble.data.lineage.util.LineageUtil.recordLineage;
import io.openlineage.client.OpenLineage.ParentRunFacet;
import com.boozallen.aissemble.security.client.PolicyDecisionPointClient;
import com.boozallen.aissemble.security.authorization.policy.PolicyDecision;
import com.boozallen.aissemble.data.encryption.policy.EncryptionPolicy;
import com.boozallen.aissemble.data.encryption.policy.EncryptionPolicyManager;
import com.boozallen.aissemble.security.exception.AissembleSecurityException;
import com.boozallen.aissemble.security.authorization.models.PDPRequest;
import com.boozallen.aissemble.data.encryption.AiopsEncrypt;
import com.boozallen.aissemble.data.encryption.SimpleAesEncrypt;
import com.boozallen.aissemble.data.encryption.VaultEncrypt;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.functions;
import org.apache.spark.sql.types.StructType;
import org.apache.commons.lang.NotImplementedException;

import java.util.stream.Collectors;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;

import org.apache.spark.sql.Encoder;
import org.apache.spark.sql.Encoders;

import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.lit;

import org.aeonbits.owner.KrauseningConfigFactory;

/**
 * Performs scaffolding synchronous processing for IngestAlertingDisabledSync. Business logic is delegated to the
 * subclass.
 * 
 * GENERATED CODE - DO NOT MODIFY (add your customizations in IngestAlertingDisabledSync).
 * 
 * Generated from: templates/data-delivery-spark/synchronous.processor.base.java.vm
 */
public abstract class IngestAlertingDisabledSyncBase extends AbstractPipelineStep {

    private static final Logger logger = LoggerFactory.getLogger(IngestAlertingDisabledSyncBase.class);

    protected static final String stepPhase = "IngestAlertingDisabledSync";

    private MetadataProducer metadataProducer;

    protected IngestAlertingDisabledSyncBase(String subject, String action) {
        super(subject, action);

        // Register the encryption UDF
        sparkSession.sqlContext().udf().register("encryptUDF", encryptUDF(), DataTypes.StringType);
    }

    public Dataset executeStep() {
        long start = System.currentTimeMillis();
        logger.debug("START: step execution...");

        // TODO: add authorization check here
        Dataset outboundPayload;
        UUID runId = UUID.randomUUID();
        ParentRunFacet parentRunFacet = PipelineBase.getInstance().getPipelineRunAsParentRunFacet();
        String jobName = getJobName();
        String defaultNamespace = getDefaultNamespace();
        LineageEventData eventData = createBaseEventData();
        ZonedDateTime eventStartTime = ZonedDateTime.now(ZoneOffset.UTC);
        Map eventParams = new HashMap();
        eventParams.put("startTime", eventStartTime);
        recordLineage(createLineageStartEvent(runId, jobName, defaultNamespace, parentRunFacet, eventData, eventParams));
        try {

            outboundPayload = executeStepImpl();

            long stop = System.currentTimeMillis();
            long ms = stop - start;

            logger.debug("COMPLETE: step execution completed in {}ms", (stop - start));

            recordProvenance();
            ZonedDateTime eventEndTime = ZonedDateTime.now(ZoneOffset.UTC);
            eventParams.put("endTime", eventEndTime);
            recordLineage(createLineageCompleteEvent(runId, jobName, defaultNamespace, parentRunFacet, eventData,
                    eventParams));
        } catch (Exception e) {
            logger.error("Step failed to complete", e);
            ZonedDateTime eventEndTime = ZonedDateTime.now(ZoneOffset.UTC);
            eventParams.put("error", e);
            eventParams.put("endTime", eventEndTime);
            recordLineage(createLineageFailEvent(runId, jobName, defaultNamespace, parentRunFacet, eventData,
                    eventParams));
            PipelineBase.getInstance().recordPipelineLineageFailEvent();
            throw e;
        }
        return outboundPayload;

    }

    /**
     * Returns the resource identifier that will be used to track this record set via provenance.
     * 
     * @return resource identifier
     */
    protected String getProvenanceResourceIdentifier() {
        return "Unspecified IngestAlertingDisabledSync resource";
    }

    /**
     * Controls creating the metadata that will be recorded for provenance purposes.
     * 
     * @param resource
     *            the identifier of the data
     * @param subject
     *            the thing acting on the resource
     * @param action
     *            the name of the activity being performed on the resource
     * @return the provenance metadata to create
     */
    protected abstract MetadataModel createProvenanceMetadata(String resource, String subject, String action);

    /**
     * Records provenance for this step.
     */
    protected void recordProvenance() {
        logger.info("Sending provenance...");

        String resource = getProvenanceResourceIdentifier();
        MetadataModel model = createProvenanceMetadata(resource, subject, action);
        metadataProducer.send(model);

        logger.info("Provenance record sent");
    }

    @Inject
    public void setMetadataProducer(MetadataProducer metadataProducer) {
        this.metadataProducer = metadataProducer;
    }

    /**
     * This method performs the business logic of this step.
     * 
     * @return outbound payload
     */
    protected abstract Dataset executeStepImpl();

    /***
     * Calls the Policy Decision Point with the jwt
     * 
     * @param jwt
     *            the authenticated token
     * @return a policy decision
     */
    protected String getAuthorization(String jwt) {
        PolicyDecisionPointClient policyDecisionPointClient = new PolicyDecisionPointClient();

        PDPRequest pdpRequest = new PDPRequest();
        pdpRequest.setJwt(jwt);
        pdpRequest.setResource("");
        pdpRequest.setAction("data-access");

        return policyDecisionPointClient.getPolicyDecision(pdpRequest);
    }

    /**
     * Spark User Defined Function for running encryption on columns. Note: must be registered with the spark session.
     * 
     * @return The cipher text
     */
    protected UDF2 encryptUDF () {
        return (plainText, encryptAlgorithm) -> {
            if (plainText != null) {
                // Default algorithm is AES
                AiopsEncrypt aiopsEncrypt = new SimpleAesEncrypt();

                if (encryptAlgorithm.equals("VAULT_ENCRYPT")) {
                    aiopsEncrypt = new VaultEncrypt();
                }

                return aiopsEncrypt.encryptValue(plainText);
            } else {
                return "";
            }
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy