Search in sources :

Example 31 with Yaml

use of org.yaml.snakeyaml.Yaml in project pipeline-aws-plugin by jenkinsci.

the class YAMLParameterFileParser method parseParams.

@Override
public Collection<Parameter> parseParams(InputStream fileContent) throws IOException {
    Yaml yaml = new Yaml();
    @SuppressWarnings("unchecked") Map<String, Object> parse = yaml.load(new InputStreamReader(fileContent, Charsets.UTF_8));
    Collection<Parameter> parameters = new ArrayList<>();
    for (Map.Entry<String, Object> entry : parse.entrySet()) {
        Object value = entry.getValue();
        if (value instanceof Collection) {
            String val = Joiner.on(",").join((Collection) value);
            parameters.add(new Parameter().withParameterKey(entry.getKey()).withParameterValue(val));
        } else {
            parameters.add(new Parameter().withParameterKey(entry.getKey()).withParameterValue(value.toString()));
        }
    }
    return parameters;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Parameter(com.amazonaws.services.cloudformation.model.Parameter) Collection(java.util.Collection) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml)

Example 32 with Yaml

use of org.yaml.snakeyaml.Yaml in project java by wavefrontHQ.

the class AgentPreprocessorConfiguration method loadFromStream.

public void loadFromStream(InputStream stream) {
    totalValidRules = 0;
    totalInvalidRules = 0;
    Yaml yaml = new Yaml();
    try {
        // noinspection unchecked
        Map<String, Object> rulesByPort = (Map<String, Object>) yaml.load(stream);
        for (String strPort : rulesByPort.keySet()) {
            int validRules = 0;
            // noinspection unchecked
            List<Map<String, String>> rules = (List<Map<String, String>>) rulesByPort.get(strPort);
            for (Map<String, String> rule : rules) {
                try {
                    requireArguments(rule, "rule", "action");
                    allowArguments(rule, "rule", "action", "scope", "search", "replace", "match", "tag", "newtag", "value", "source", "iterations");
                    String ruleName = rule.get("rule").replaceAll("[^a-z0-9_-]", "");
                    PreprocessorRuleMetrics ruleMetrics = new PreprocessorRuleMetrics(Metrics.newCounter(new TaggedMetricName("preprocessor." + ruleName, "count", "port", strPort)), Metrics.newCounter(new TaggedMetricName("preprocessor." + ruleName, "cpu_nanos", "port", strPort)));
                    if (rule.get("scope") != null && rule.get("scope").equals("pointLine")) {
                        switch(rule.get("action")) {
                            case "replaceRegex":
                                allowArguments(rule, "rule", "action", "scope", "search", "replace", "match", "iterations");
                                this.forPort(strPort).forPointLine().addTransformer(new PointLineReplaceRegexTransformer(rule.get("search"), rule.get("replace"), rule.get("match"), Integer.parseInt(rule.getOrDefault("iterations", "1")), ruleMetrics));
                                break;
                            case "blacklistRegex":
                                allowArguments(rule, "rule", "action", "scope", "match");
                                this.forPort(strPort).forPointLine().addFilter(new PointLineBlacklistRegexFilter(rule.get("match"), ruleMetrics));
                                break;
                            case "whitelistRegex":
                                allowArguments(rule, "rule", "action", "scope", "match");
                                this.forPort(strPort).forPointLine().addFilter(new PointLineWhitelistRegexFilter(rule.get("match"), ruleMetrics));
                                break;
                            default:
                                throw new IllegalArgumentException("Action '" + rule.get("action") + "' is not valid or cannot be applied to pointLine");
                        }
                    } else {
                        switch(rule.get("action")) {
                            case "replaceRegex":
                                allowArguments(rule, "rule", "action", "scope", "search", "replace", "match", "iterations");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointReplaceRegexTransformer(rule.get("scope"), rule.get("search"), rule.get("replace"), rule.get("match"), Integer.parseInt(rule.getOrDefault("iterations", "1")), ruleMetrics));
                                break;
                            case "forceLowercase":
                                allowArguments(rule, "rule", "action", "scope", "match");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointForceLowercaseTransformer(rule.get("scope"), rule.get("match"), ruleMetrics));
                                break;
                            case "addTag":
                                allowArguments(rule, "rule", "action", "tag", "value");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointAddTagTransformer(rule.get("tag"), rule.get("value"), ruleMetrics));
                                break;
                            case "addTagIfNotExists":
                                allowArguments(rule, "rule", "action", "tag", "value");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointAddTagIfNotExistsTransformer(rule.get("tag"), rule.get("value"), ruleMetrics));
                                break;
                            case "dropTag":
                                allowArguments(rule, "rule", "action", "tag", "match");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointDropTagTransformer(rule.get("tag"), rule.get("match"), ruleMetrics));
                                break;
                            case "extractTag":
                                allowArguments(rule, "rule", "action", "tag", "source", "search", "replace", "match");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointExtractTagTransformer(rule.get("tag"), rule.get("source"), rule.get("search"), rule.get("replace"), rule.get("match"), ruleMetrics));
                                break;
                            case "renameTag":
                                allowArguments(rule, "rule", "action", "tag", "newtag", "match");
                                this.forPort(strPort).forReportPoint().addTransformer(new ReportPointRenameTagTransformer(rule.get("tag"), rule.get("newtag"), rule.get("match"), ruleMetrics));
                                break;
                            case "blacklistRegex":
                                allowArguments(rule, "rule", "action", "scope", "match");
                                this.forPort(strPort).forReportPoint().addFilter(new ReportPointBlacklistRegexFilter(rule.get("scope"), rule.get("match"), ruleMetrics));
                                break;
                            case "whitelistRegex":
                                allowArguments(rule, "rule", "action", "scope", "match");
                                this.forPort(strPort).forReportPoint().addFilter(new ReportPointWhitelistRegexFilter(rule.get("scope"), rule.get("match"), ruleMetrics));
                                break;
                            default:
                                throw new IllegalArgumentException("Action '" + rule.get("action") + "' is not valid");
                        }
                    }
                    validRules++;
                } catch (IllegalArgumentException | NullPointerException ex) {
                    logger.warning("Invalid rule " + (rule == null || rule.get("rule") == null ? "" : rule.get("rule")) + " (port " + strPort + "): " + ex);
                    totalInvalidRules++;
                }
            }
            logger.info("Loaded " + validRules + " rules for port " + strPort);
            totalValidRules += validRules;
        }
        logger.info("Total " + totalValidRules + " rules loaded");
        if (totalInvalidRules > 0) {
            throw new RuntimeException("Total " + totalInvalidRules + " invalid rules detected, aborting start-up");
        }
    } catch (ClassCastException e) {
        throw new RuntimeException("Can't parse preprocessor configuration - aborting start-up");
    }
}
Also used : TaggedMetricName(com.wavefront.common.TaggedMetricName) List(java.util.List) Yaml(org.yaml.snakeyaml.Yaml) Map(java.util.Map) HashMap(java.util.HashMap)

Example 33 with Yaml

use of org.yaml.snakeyaml.Yaml in project twister2 by DSC-SPIDAL.

the class ConfigReader method loadStream.

/**
 * Load config from the given YAML stream
 *
 * @param inputStream the name of YAML stream to read
 *
 * @return Map, contains the key value pairs of config
 */
// yaml.load API returns raw Map
@SuppressWarnings("unchecked")
public static Map<String, Object> loadStream(InputStream inputStream) {
    LOG.fine("Reading config stream");
    Yaml yaml = new Yaml();
    Map<Object, Object> propsYaml = (Map<Object, Object>) yaml.load(inputStream);
    LOG.fine("Successfully read config");
    Map<String, Object> typedMap = new HashMap<>();
    for (Object key : propsYaml.keySet()) {
        typedMap.put(key.toString(), propsYaml.get(key));
    }
    return typedMap;
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Yaml(org.yaml.snakeyaml.Yaml)

Example 34 with Yaml

use of org.yaml.snakeyaml.Yaml in project jvm-profiler by uber-common.

the class YamlConfigProvider method getConfig.

private static Map<String, Map<String, List<String>>> getConfig(String configFilePathOrUrl) {
    if (configFilePathOrUrl == null || configFilePathOrUrl.isEmpty()) {
        logger.warn("Empty YAML config file path");
        return new HashMap<>();
    }
    byte[] bytes;
    try {
        bytes = new ExponentialBackoffRetryPolicy<byte[]>(3, 100).attempt(() -> {
            String filePathLowerCase = configFilePathOrUrl.toLowerCase();
            if (filePathLowerCase.startsWith("http://") || filePathLowerCase.startsWith("https://")) {
                return getHttp(configFilePathOrUrl);
            } else {
                return Files.readAllBytes(Paths.get(configFilePathOrUrl));
            }
        });
        logger.info("Read YAML config from: " + configFilePathOrUrl);
    } catch (Throwable e) {
        logger.warn("Failed to read file: " + configFilePathOrUrl, e);
        return new HashMap<>();
    }
    if (bytes == null || bytes.length == 0) {
        return new HashMap<>();
    }
    Map<String, Map<String, List<String>>> result = new HashMap<>();
    try {
        try (ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
            Yaml yaml = new Yaml();
            Object yamlObj = yaml.load(stream);
            if (!(yamlObj instanceof Map)) {
                logger.warn("Invalid YAML config content: " + yamlObj);
                return result;
            }
            Map yamlMap = (Map) yamlObj;
            Map overrideMap = null;
            for (Object key : yamlMap.keySet()) {
                String configKey = key.toString();
                Object valueObj = yamlMap.get(key);
                if (valueObj == null) {
                    continue;
                }
                if (configKey.equals(OVERRIDE_KEY)) {
                    if (valueObj instanceof Map) {
                        overrideMap = (Map) valueObj;
                    } else {
                        logger.warn("Invalid override property: " + valueObj);
                    }
                } else {
                    addConfig(result, "", configKey, valueObj);
                }
            }
            if (overrideMap != null) {
                for (Object key : overrideMap.keySet()) {
                    String overrideKey = key.toString();
                    Object valueObj = overrideMap.get(key);
                    if (valueObj == null) {
                        continue;
                    }
                    if (!(valueObj instanceof Map)) {
                        logger.warn("Invalid override section: " + key + ": " + valueObj);
                        continue;
                    }
                    Map<Object, Object> overrideValues = (Map<Object, Object>) valueObj;
                    for (Map.Entry<Object, Object> entry : overrideValues.entrySet()) {
                        if (entry.getValue() == null) {
                            continue;
                        }
                        String configKey = entry.getKey().toString();
                        addConfig(result, overrideKey, configKey, entry.getValue());
                    }
                }
            }
            return result;
        }
    } catch (Throwable e) {
        logger.warn("Failed to read config file: " + configFilePathOrUrl, e);
        return new HashMap<>();
    }
}
Also used : ExponentialBackoffRetryPolicy(com.uber.profiling.util.ExponentialBackoffRetryPolicy) HashMap(java.util.HashMap) Yaml(org.yaml.snakeyaml.Yaml) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with Yaml

use of org.yaml.snakeyaml.Yaml in project bboxdb by jnidzwetzki.

the class TupleStoreConfiguration method exportToYamlFile.

/**
 * Export the data to YAML File
 * @return
 * @throws IOException
 */
public void exportToYamlFile(final File outputFile) throws IOException {
    final Map<String, Object> data = getPropertyMap();
    final FileWriter writer = new FileWriter(outputFile);
    logger.debug("Output data to: " + outputFile);
    final Yaml yaml = new Yaml();
    yaml.dump(data, writer);
    writer.close();
}
Also used : FileWriter(java.io.FileWriter) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)253 Map (java.util.Map)92 HashMap (java.util.HashMap)73 IOException (java.io.IOException)55 FileInputStream (java.io.FileInputStream)47 InputStream (java.io.InputStream)47 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)39 Constructor (org.yaml.snakeyaml.constructor.Constructor)27 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)21 List (java.util.List)21 Test (org.junit.Test)21 LinkedHashMap (java.util.LinkedHashMap)17 Writer (java.io.Writer)13 FileWriter (java.io.FileWriter)12 InputStreamReader (java.io.InputStreamReader)12 Path (java.nio.file.Path)12 FileReader (java.io.FileReader)10 Reader (java.io.Reader)9