<commit_before>package org.alien4cloud.alm.deployment.configuration.flow.modifiers;

import static alien4cloud.utils.AlienUtils.safe;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;

import alien4cloud.common.MetaPropertiesService;
import alien4cloud.model.common.MetaPropertyTarget;
import org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext;
import org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier;
import org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration;
import org.alien4cloud.tosca.model.templates.Topology;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import alien4cloud.deployment.matching.services.location.LocationMatchingService;
import alien4cloud.model.application.ApplicationEnvironment;
import alien4cloud.model.deployment.matching.ILocationMatch;
import alien4cloud.model.orchestrators.locations.Location;
import alien4cloud.orchestrators.locations.services.LocationService;
import alien4cloud.plugin.exception.MissingPluginException;
import alien4cloud.topology.validation.LocationPolicyValidationService;
import alien4cloud.tosca.context.ToscaContext;

/**
 * This processor actually does not change the topology but check that location settings are defined and up-to-date in order to allow other processors to
 * continue.
 */
@Component
public class LocationMatchingModifier implements ITopologyModifier {
    @Inject
    private LocationMatchingService locationMatchingService;
    @Inject
    private LocationService locationService;
    @Inject
    private PluginModifierRegistry pluginModifierRegistry;
    @Inject
    private LocationPolicyValidationService locationPolicyValidationService;
    @Inject
    private MetaPropertiesService metaPropertiesService;

    @Override
    public void process(Topology topology, FlowExecutionContext context) {

        // first process
        processLocationMatching(topology, context);

        Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class,
                LocationMatchingModifier.class.getSimpleName());

        // perform validation
        locationPolicyValidationService.validateLocationPolicies(configurationOptional.orElse(new DeploymentMatchingConfiguration()))
                .forEach(locationPolicyTask -> context.log().error(locationPolicyTask));

        // No errors from validation let's inject location topology modifiers if any.
        if (context.log().isValid()) {
            // FIXME this code is a temporary demo implementation for 2.0.0 SM2, it has to be removed and improved in favor of a clear definition of locaiton
            // modifiers.
            List<ILocationMatch> locationMatches = (List<ILocationMatch>) context.getExecutionCache().get(FlowExecutionContext.LOCATION_MATCH_CACHE_KEY);
            String metaPropKey = metaPropertiesService.getMetapropertykeyByName("loc_modifiers", MetaPropertyTarget.LOCATION);
            if (metaPropKey == null) {
                return;
            }
            String locationModifierDefinitionsStr = safe(locationMatches.get(0).getLocation().getMetaProperties()).get(metaPropKey);
            if (locationModifierDefinitionsStr == null) {
                return;
            }
            String[] locationModifierDefinitions = locationModifierDefinitionsStr.split(",");
            for (String locationModifierDefinition : locationModifierDefinitions) {
                injectLocationTopologyModfier(context, locationMatches.get(0).getLocation().getName(), locationModifierDefinition);
            }
        }
    }

    private void injectLocationTopologyModfier(FlowExecutionContext context, String locationName, String locationModifierDefinition) {
        String[] locationModifierImpl = locationModifierDefinition.split(":");<commit_msg>location matching modifier should search for the configured location modifier plugin associated only to the selected locations for deployment<commit_after>package org.alien4cloud.alm.deployment.configuration.flow.modifiers;

import static alien4cloud.utils.AlienUtils.safe;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;

import org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext;
import org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier;
import org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration;
import org.alien4cloud.tosca.model.templates.Topology;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import alien4cloud.common.MetaPropertiesService;
import alien4cloud.deployment.matching.services.location.LocationMatchingService;
import alien4cloud.model.application.ApplicationEnvironment;
import alien4cloud.model.common.MetaPropertyTarget;
import alien4cloud.model.deployment.matching.ILocationMatch;
import alien4cloud.model.orchestrators.locations.Location;
import alien4cloud.orchestrators.locations.services.LocationService;
import alien4cloud.plugin.exception.MissingPluginException;
import alien4cloud.topology.validation.LocationPolicyValidationService;
import alien4cloud.tosca.context.ToscaContext;

/**
 * This processor actually does not change the topology but check that location settings are defined and up-to-date in order to allow other processors to
 * continue.
 */
@Component
public class LocationMatchingModifier implements ITopologyModifier {
    @Inject
    private LocationMatchingService locationMatchingService;
    @Inject
    private LocationService locationService;
    @Inject
    private PluginModifierRegistry pluginModifierRegistry;
    @Inject
    private LocationPolicyValidationService locationPolicyValidationService;
    @Inject
    private MetaPropertiesService metaPropertiesService;

    @Override
    public void process(Topology topology, FlowExecutionContext context) {

        // first process
        processLocationMatching(topology, context);

        Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class,
                LocationMatchingModifier.class.getSimpleName());

        // perform validation
        locationPolicyValidationService.validateLocationPolicies(configurationOptional.orElse(new DeploymentMatchingConfiguration()))
                .forEach(locationPolicyTask -> context.log().error(locationPolicyTask));

        // No errors from validation let's inject location topology modifiers if any.
        if (context.log().isValid()) {
            // FIXME this code is a temporary demo implementation for 2.0.0 SM2, it has to be removed and improved in favor of a clear definition of locaiton
            // modifiers.
            Map<String, Location> selectedLocations = (Map<String, Location>) context.getExecutionCache()
                    .get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
            String metaPropKey = metaPropertiesService.getMetapropertykeyByName("loc_modifiers", MetaPropertyTarget.LOCATION);
            if (metaPropKey == null) {
                return;
            }
            String locationModifierDefinitionsStr = safe(selectedLocations.values().iterator().next().getMetaProperties()).get(metaPropKey);
            if (locationModifierDefinitionsStr == null) {
                return;
            }
            String[] locationModifierDefinitions = locationModifierDefinitionsStr.split(",");
            for (String locationModifierDefinition : locationModifierDefinitions) {
                injectLocationTopologyModfier(context, selectedLocations.values().iterator().next().getName(), locationModifierDefinition);
            }
        }
    }

    private void injectLocationTopologyModfier(FlowExecutionContext context, String locationName, String locationModifierDefinition) {
        String[] locationModifierImpl = locationModifierDefinition.split(":");
{'fraction_non_alphanumeric': 0.07495451788963008, 'fraction_numerical': 0.005336567616737417, 'mean_word_length': 4.460927152317881, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3963039', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1984, 'n_words': 422}