<commit_before>/**
 * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * 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 org.commonjava.indy.promote.validate;

import groovy.lang.Closure;
import org.commonjava.cdi.util.weft.ExecutorConfig;
import org.commonjava.cdi.util.weft.WeftManaged;
import org.commonjava.indy.IndyWorkflowException;
import org.commonjava.indy.content.ContentDigester;
import org.commonjava.indy.content.ContentManager;
import org.commonjava.indy.content.StoreResource;
import org.commonjava.indy.data.IndyDataException;
import org.commonjava.indy.data.StoreDataManager;
import org.commonjava.indy.data.ArtifactStoreQuery;
import org.commonjava.indy.model.core.ArtifactStore;
import org.commonjava.indy.model.core.StoreKey;
import org.commonjava.indy.promote.conf.PromoteConfig;
import org.commonjava.indy.promote.validate.model.ValidationRequest;
import org.commonjava.indy.util.LocationUtils;
import org.commonjava.atlas.maven.graph.rel.ProjectRelationship;
import org.commonjava.atlas.maven.ident.ref.ArtifactRef;
import org.commonjava.atlas.maven.ident.ref.ProjectRef;
import org.commonjava.atlas.maven.ident.ref.ProjectVersionRef;
import org.commonjava.atlas.maven.ident.util.ArtifactPathInfo;
import org.commonjava.maven.galley.TransferException;
import org.commonjava.maven.galley.TransferManager;
import org.commonjava.maven.galley.event.EventMetadata;
import org.commonjava.maven.galley.io.checksum.ContentDigest;
import org.commonjava.maven.galley.maven.GalleyMavenException;
import org.commonjava.maven.galley.maven.model.view.MavenPomView;
import org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView;
import org.commonjava.maven.galley.maven.parse.MavenMetadataReader;
import org.commonjava.maven.galley.maven.parse.MavenPomReader;
import org.commonjava.maven.galley.maven.rel.MavenModelProcessor;
import org.commonjava.maven.galley.maven.rel.ModelProcessorConfig;
import org.commonjava.maven.galley.maven.spi.type.TypeMapper;
import org.commonjava.maven.galley.maven.util.ArtifactPathUtils;
import org.commonjava.maven.galley.model.Location;
import org.commonjava.maven.galley.model.Transfer;
import org.commonjava.maven.galley.model.TransferOperation;
import org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.commonjava.indy.promote.util.Batcher.batch;
import static org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrapper;
import static org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrappers;
import static org.commonjava.maven.galley.io.ChecksummingTransferDecorator.FORCE_CHECKSUM;

/**
 * Created by jdcasey on 9/11/15.
 */
public class PromotionValidationTools
{
    final Logger logger = LoggerFactory.getLogger( this.getClass() );

    public static final String AVAILABLE_IN_STORES = "availableInStores";

    @Deprecated
    public static final String AVAILABLE_IN_STORE_KEY = "availableInStoreKey";

    private static final int DEFAULT_RULE_PARALLEL_WAIT_TIME_MINS = 30;

    private static final String ITERATION_DEPTH = "promotion-validation-parallel-depth";

    private static final String ITERATION_ITEM = "promotion-validation-parallel-item";

    @Inject
    private ContentManager contentManager;

    @Inject
    private StoreDataManager storeDataManager;

    @Inject
    private MavenPomReader pomReader;

    @Inject
    private MavenMetadataReader metadataReader;

    @Inject
    private MavenModelProcessor modelProcessor;

    @Inject
    private TypeMapper typeMapper;

    @Inject
    private TransferManager transferManager;

    @Inject
    private ContentDigester contentDigester;

    @Inject
    private PromoteConfig promoteConfig;

    @Inject
    @WeftManaged
    @ExecutorConfig( named = "promote-validation-rules-executor", threads = 8 )
    private Executor ruleParallelExecutor;

    protected PromotionValidationTools()
    {
    }

    public PromotionValidationTools( final ContentManager manager, final StoreDataManager storeDataManager,
                                     final MavenPomReader pomReader, final MavenMetadataReader metadataReader,
                                     final MavenModelProcessor modelProcessor, final TypeMapper typeMapper,
                                     final TransferManager transferManager, final ContentDigester contentDigester,
                                     final Executor ruleParallelExecutor, final PromoteConfig config )
    {
        contentManager = manager;
        this.storeDataManager = storeDataManager;
        this.pomReader = pomReader;
        this.metadataReader = metadataReader;
        this.modelProcessor = modelProcessor;
        this.typeMapper = typeMapper;
        this.transferManager = transferManager;
        this.contentDigester = contentDigester;
        this.ruleParallelExecutor = ruleParallelExecutor;
        this.promoteConfig = config;
    }

    public StoreKey[] getValidationStoreKeys( final ValidationRequest request, final boolean includeSource )
            throws PromotionValidationException
    {
        return getValidationStoreKeys( request, includeSource, true );
    }

    public StoreKey[] getValidationStoreKeys( final ValidationRequest request, final boolean includeSource,
                                              final boolean includeTarget )
            throws PromotionValidationException
    {
        String verifyStores = request.getValidationParameter( PromotionValidationTools.AVAILABLE_IN_STORES );
        if ( verifyStores == null )
        {
            verifyStores = request.getValidationParameter( PromotionValidationTools.AVAILABLE_IN_STORE_KEY );
        }

        Logger logger = LoggerFactory.getLogger( getClass() );
        logger.debug( "Got extra validation keys string: '{}'", verifyStores );

        Set<StoreKey> verifyStoreKeys = new HashSet<>();
        if ( includeSource )
        {
            verifyStoreKeys.add( request.getSourceRepository().getKey() );
        }


        if ( verifyStores == null )
        {
            logger.warn(
                    "No external store (availableInStoreKey parameter) specified for validating path availability in rule-set: {}. Using target: {} instead.",
                    request.getRuleSet().getName(), request.getTarget() );
        }
        else
        {
            List<StoreKey> extras = Stream.of( verifyStores.split( "\\s*,\\s*" ) )
                                          .map( StoreKey::fromString )
                                          .filter( item -> item != null )<commit_msg>Change method signature and add deprecated to old promote getValidationStoreKeys<commit_after>/**
 * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * 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 org.commonjava.indy.promote.validate;

import groovy.lang.Closure;
import org.commonjava.cdi.util.weft.ExecutorConfig;
import org.commonjava.cdi.util.weft.WeftManaged;
import org.commonjava.indy.IndyWorkflowException;
import org.commonjava.indy.content.ContentDigester;
import org.commonjava.indy.content.ContentManager;
import org.commonjava.indy.content.StoreResource;
import org.commonjava.indy.data.IndyDataException;
import org.commonjava.indy.data.StoreDataManager;
import org.commonjava.indy.data.ArtifactStoreQuery;
import org.commonjava.indy.model.core.ArtifactStore;
import org.commonjava.indy.model.core.StoreKey;
import org.commonjava.indy.promote.conf.PromoteConfig;
import org.commonjava.indy.promote.validate.model.ValidationRequest;
import org.commonjava.indy.util.LocationUtils;
import org.commonjava.atlas.maven.graph.rel.ProjectRelationship;
import org.commonjava.atlas.maven.ident.ref.ArtifactRef;
import org.commonjava.atlas.maven.ident.ref.ProjectRef;
import org.commonjava.atlas.maven.ident.ref.ProjectVersionRef;
import org.commonjava.atlas.maven.ident.util.ArtifactPathInfo;
import org.commonjava.maven.galley.TransferException;
import org.commonjava.maven.galley.TransferManager;
import org.commonjava.maven.galley.event.EventMetadata;
import org.commonjava.maven.galley.io.checksum.ContentDigest;
import org.commonjava.maven.galley.maven.GalleyMavenException;
import org.commonjava.maven.galley.maven.model.view.MavenPomView;
import org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView;
import org.commonjava.maven.galley.maven.parse.MavenMetadataReader;
import org.commonjava.maven.galley.maven.parse.MavenPomReader;
import org.commonjava.maven.galley.maven.rel.MavenModelProcessor;
import org.commonjava.maven.galley.maven.rel.ModelProcessorConfig;
import org.commonjava.maven.galley.maven.spi.type.TypeMapper;
import org.commonjava.maven.galley.maven.util.ArtifactPathUtils;
import org.commonjava.maven.galley.model.Location;
import org.commonjava.maven.galley.model.Transfer;
import org.commonjava.maven.galley.model.TransferOperation;
import org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.commonjava.indy.promote.util.Batcher.batch;
import static org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrapper;
import static org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrappers;
import static org.commonjava.maven.galley.io.ChecksummingTransferDecorator.FORCE_CHECKSUM;

/**
 * Created by jdcasey on 9/11/15.
 */
public class PromotionValidationTools
{
    final Logger logger = LoggerFactory.getLogger( this.getClass() );

    public static final String AVAILABLE_IN_STORES = "availableInStores";

    @Deprecated
    public static final String AVAILABLE_IN_STORE_KEY = "availableInStoreKey";

    private static final int DEFAULT_RULE_PARALLEL_WAIT_TIME_MINS = 30;

    private static final String ITERATION_DEPTH = "promotion-validation-parallel-depth";

    private static final String ITERATION_ITEM = "promotion-validation-parallel-item";

    @Inject
    private ContentManager contentManager;

    @Inject
    private StoreDataManager storeDataManager;

    @Inject
    private MavenPomReader pomReader;

    @Inject
    private MavenMetadataReader metadataReader;

    @Inject
    private MavenModelProcessor modelProcessor;

    @Inject
    private TypeMapper typeMapper;

    @Inject
    private TransferManager transferManager;

    @Inject
    private ContentDigester contentDigester;

    @Inject
    private PromoteConfig promoteConfig;

    @Inject
    @WeftManaged
    @ExecutorConfig( named = "promote-validation-rules-executor", threads = 8 )
    private Executor ruleParallelExecutor;

    protected PromotionValidationTools()
    {
    }

    public PromotionValidationTools( final ContentManager manager, final StoreDataManager storeDataManager,
                                     final MavenPomReader pomReader, final MavenMetadataReader metadataReader,
                                     final MavenModelProcessor modelProcessor, final TypeMapper typeMapper,
                                     final TransferManager transferManager, final ContentDigester contentDigester,
                                     final Executor ruleParallelExecutor, final PromoteConfig config )
    {
        contentManager = manager;
        this.storeDataManager = storeDataManager;
        this.pomReader = pomReader;
        this.metadataReader = metadataReader;
        this.modelProcessor = modelProcessor;
        this.typeMapper = typeMapper;
        this.transferManager = transferManager;
        this.contentDigester = contentDigester;
        this.ruleParallelExecutor = ruleParallelExecutor;
        this.promoteConfig = config;
    }

    public StoreKey[] getValidationStoreKeys( final ValidationRequest request )
            throws PromotionValidationException
    {
        return getValidationStoreKeys( request, false );
    }

    /**
     * @deprecated This method now is only used for api backward compatible
     */
    @Deprecated
    public StoreKey[] getValidationStoreKeys( final ValidationRequest request, final boolean includeSource,
                                              final boolean includeTarget )
            throws PromotionValidationException
    {
        return getValidationStoreKeys( request, includeSource );
    }

    public StoreKey[] getValidationStoreKeys( final ValidationRequest request, final boolean includeSource )
            throws PromotionValidationException
    {
        String verifyStores = request.getValidationParameter( PromotionValidationTools.AVAILABLE_IN_STORES );
        if ( verifyStores == null )
        {
            verifyStores = request.getValidationParameter( PromotionValidationTools.AVAILABLE_IN_STORE_KEY );
        }

        Logger logger = LoggerFactory.getLogger( getClass() );
        logger.debug( "Got extra validation keys string: '{}'", verifyStores );

        Set<StoreKey> verifyStoreKeys = new HashSet<>();
        if ( includeSource )
        {
            verifyStoreKeys.add( request.getSourceRepository().getKey() );
        }


        if ( verifyStores == null )
        {
            logger.warn(
                    "No external store (availableInStoreKey parameter) specified for validating path availability in rule-set: {}. Using target: {} instead.",
                    request.getRuleSet().getName(), request.getTarget() );
        }
        else
        {
            List<StoreKey> extras = Stream.of( verifyStores.split( "\\s*,\\s*" ) )
                                          .map( StoreKey::fromString )
                                          .filter( item -> item != null )
{'fraction_non_alphanumeric': 0.08348770360907058, 'fraction_numerical': 0.002938358351964229, 'mean_word_length': 3.555135292406168, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2775671', 'n_tokens_mistral': 4363, 'n_tokens_neox': 4187, 'n_words': 924}