Search in sources :

Example 11 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project walkmod-core by walkmod.

the class YAMLConfigurationProvider method addPluginConfig.

@Override
public void addPluginConfig(final PluginConfig pluginConfig, boolean recursive) throws TransformerException {
    File cfg = new File(fileName);
    ArrayNode pluginList = null;
    JsonNode node = null;
    try {
        node = mapper.readTree(cfg);
    } catch (Exception e) {
    }
    if (node == null) {
        node = new ObjectNode(mapper.getNodeFactory());
    }
    if (recursive && node.has("modules")) {
        JsonNode aux = node.get("modules");
        if (aux.isArray()) {
            ArrayNode modules = (ArrayNode) aux;
            int max = modules.size();
            for (int i = 0; i < max; i++) {
                JsonNode module = modules.get(i);
                if (module.isTextual()) {
                    String moduleDir = module.asText();
                    try {
                        File auxFile = new File(fileName).getCanonicalFile().getParentFile();
                        YAMLConfigurationProvider child = new YAMLConfigurationProvider(auxFile.getAbsolutePath() + File.separator + moduleDir + File.separator + "walkmod.yml");
                        child.createConfig();
                        child.addPluginConfig(pluginConfig, recursive);
                    } catch (IOException e) {
                        throw new TransformerException(e);
                    }
                }
            }
        }
    } else {
        if (!node.has("plugins")) {
            pluginList = new ArrayNode(mapper.getNodeFactory());
            if (node.isObject()) {
                ObjectNode aux = (ObjectNode) node;
                aux.set("plugins", pluginList);
            } else {
                throw new TransformerException("The root element is not a JSON node");
            }
        } else {
            JsonNode aux = node.get("plugins");
            if (aux.isArray()) {
                pluginList = (ArrayNode) node.get("plugins");
            } else {
                throw new TransformerException("The plugins element is not a valid array");
            }
        }
        pluginList.add(new TextNode(pluginConfig.getGroupId() + ":" + pluginConfig.getArtifactId() + ":" + pluginConfig.getVersion()));
        write(node);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConfigurationException(org.walkmod.conf.ConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 12 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project walkmod-core by walkmod.

the class AbstractYMLConfigurationAction method createTransformation.

public void createTransformation(ObjectNode transformationNode, TransformationConfig transCfg) {
    String name = transCfg.getName();
    if (name != null) {
        transformationNode.set("name", new TextNode(name));
    }
    String typeName = transCfg.getType();
    if (typeName != null) {
        transformationNode.set("type", new TextNode(typeName));
    }
    String mergePolicy = transCfg.getMergePolicy();
    if (mergePolicy != null) {
        transformationNode.set("merge-policy", new TextNode(mergePolicy));
    }
    if (transCfg.isMergeable()) {
        transformationNode.set("isMergeable", BooleanNode.TRUE);
    }
    Map<String, Object> params = transCfg.getParameters();
    if (params != null && !params.isEmpty()) {
        populateParams(transformationNode, params);
    }
}
Also used : TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 13 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project walkmod-core by walkmod.

the class AddChainYMLAction method doAction.

@Override
public void doAction(JsonNode chainsNode) throws Exception {
    ArrayNode chainsList = null;
    ObjectMapper mapper = provider.getObjectMapper();
    if (chainsNode != null) {
        if (!chainsNode.has("chains")) {
            chainsList = new ArrayNode(mapper.getNodeFactory());
            if (chainsNode.isObject()) {
                ObjectNode aux = (ObjectNode) chainsNode;
                aux.set("chains", chainsList);
            } else {
                throw new TransformerException("The root element is not a JSON node");
            }
        } else {
            JsonNode aux = chainsNode.get("chains");
            if (aux.isArray()) {
                chainsList = (ArrayNode) chainsNode.get("chains");
            } else {
                throw new TransformerException("The plugins element is not a valid array");
            }
        }
    }
    ObjectNode chainNode = new ObjectNode(mapper.getNodeFactory());
    ReaderConfig readerCfg = chainCfg.getReaderConfig();
    if (readerCfg != null) {
        if (chainsNode == null) {
            chainsNode = new ObjectNode(mapper.getNodeFactory());
            ObjectNode aux = (ObjectNode) chainsNode;
            chainsList = new ArrayNode(mapper.getNodeFactory());
            aux.set("chains", chainsList);
        }
        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
        chainNode.set("reader", readerNode);
        populateWriterReader(readerNode, readerCfg.getPath(), readerCfg.getType(), readerCfg.getIncludes(), readerCfg.getExcludes(), readerCfg.getParameters());
    } else {
        provider.addDefaultReaderConfig(chainCfg);
    }
    WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
    if (walkerCfg != null) {
        ObjectNode walkerNode = null;
        String type = walkerCfg.getType();
        if (type != null) {
            if (chainsNode == null) {
                chainsNode = new ObjectNode(mapper.getNodeFactory());
                ObjectNode aux = (ObjectNode) chainsNode;
                chainsList = new ArrayNode(mapper.getNodeFactory());
                aux.set("chains", chainsList);
            }
            walkerNode = new ObjectNode(mapper.getNodeFactory());
            chainNode.set("walker", walkerNode);
            walkerNode.set("type", new TextNode(type));
        }
        Map<String, Object> wparams = walkerCfg.getParams();
        if (wparams != null && !wparams.isEmpty()) {
            if (walkerNode == null) {
                if (chainsNode == null) {
                    chainsNode = new ObjectNode(mapper.getNodeFactory());
                    ObjectNode aux = (ObjectNode) chainsNode;
                    chainsList = new ArrayNode(mapper.getNodeFactory());
                    aux.set("chains", chainsList);
                }
                walkerNode = new ObjectNode(mapper.getNodeFactory());
                chainNode.set("walker", walkerNode);
            }
            populateParams(walkerNode, wparams);
        }
        String rootNamespace = walkerCfg.getRootNamespace();
        if (rootNamespace != null) {
            if (walkerNode == null) {
                if (chainsNode == null) {
                    chainsNode = new ObjectNode(mapper.getNodeFactory());
                    ObjectNode aux = (ObjectNode) chainsNode;
                    chainsList = new ArrayNode(mapper.getNodeFactory());
                    aux.set("chains", chainsList);
                }
                walkerNode = new ObjectNode(mapper.getNodeFactory());
                chainNode.set("walker", walkerNode);
            }
            walkerNode.set("root-namespace", new TextNode(rootNamespace));
        }
        List<TransformationConfig> transformationList = walkerCfg.getTransformations();
        if (transformationList != null && !transformationList.isEmpty()) {
            ArrayNode transformationListNode = new ArrayNode(mapper.getNodeFactory());
            if (walkerNode == null) {
                if (chainsNode == null) {
                    ObjectNode aux = new ObjectNode(mapper.getNodeFactory());
                    aux.set("transformations", transformationListNode);
                    chainsNode = aux;
                } else {
                    chainNode.set("transformations", transformationListNode);
                }
            } else {
                walkerNode.set("transformations", transformationListNode);
            }
            for (TransformationConfig transCfg : transformationList) {
                ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
                transformationListNode.add(transformationNode);
                createTransformation(transformationNode, transCfg);
            }
        }
    }
    WriterConfig writerCfg = chainCfg.getWriterConfig();
    if (writerCfg != null) {
        if (chainsNode == null) {
            chainsNode = new ObjectNode(mapper.getNodeFactory());
            ObjectNode aux = (ObjectNode) chainsNode;
            chainsList = new ArrayNode(mapper.getNodeFactory());
            aux.set("chains", chainsList);
        }
        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
        chainNode.set("writer", writerNode);
        populateWriterReader(writerNode, writerCfg.getPath(), writerCfg.getType(), writerCfg.getIncludes(), writerCfg.getExcludes(), writerCfg.getParams());
    } else {
        provider.addDefaultWriterConfig(chainCfg);
    }
    if (chainsList != null) {
        int beforePos = -1;
        if (before != null) {
            Iterator<JsonNode> it = chainsList.iterator();
            int i = 0;
            while (it.hasNext() && beforePos == -1) {
                JsonNode next = it.next();
                if (next.get("name").equals(before)) {
                    beforePos = i;
                }
                i++;
            }
        }
        if (beforePos == -1) {
            chainsList.add(chainNode);
        } else {
            chainsList.insert(beforePos, chainNode);
        }
    }
    if (readerCfg != null || walkerCfg != null || writerCfg != null) {
        provider.write(chainsNode);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WriterConfig(org.walkmod.conf.entities.WriterConfig) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TransformerException(javax.xml.transform.TransformerException)

Example 14 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project walkmod-core by walkmod.

the class AddIncludesOrExcludesYMLAction method doAction.

@Override
public void doAction(JsonNode node) throws Exception {
    if (chain == null) {
        chain = "default";
    }
    ObjectMapper mapper = provider.getObjectMapper();
    if (node.has("chains")) {
        JsonNode chains = node.get("chains");
        if (chains.isArray()) {
            ArrayNode chainsArray = (ArrayNode) chains;
            int limit = chainsArray.size();
            ObjectNode selectedChain = null;
            for (int i = 0; i < limit && selectedChain == null; i++) {
                JsonNode chainNode = chainsArray.get(i);
                if (chainNode.has("name")) {
                    if (chainNode.get("name").asText().equals(chain)) {
                        selectedChain = (ObjectNode) chainNode;
                    }
                }
            }
            if (selectedChain == null) {
                selectedChain = new ObjectNode(mapper.getNodeFactory());
                selectedChain.set("name", new TextNode(chain));
                chainsArray.add(selectedChain);
            }
            if (setToReader) {
                JsonNode reader = null;
                if (selectedChain.has("reader")) {
                    reader = selectedChain.get("reader");
                } else {
                    reader = new ObjectNode(mapper.getNodeFactory());
                    selectedChain.set("reader", reader);
                }
                setIncludesOrExcludesList((ObjectNode) reader);
            }
            if (setToWriter) {
                JsonNode reader = null;
                if (selectedChain.has("writer")) {
                    reader = selectedChain.get("writer");
                } else {
                    reader = new ObjectNode(mapper.getNodeFactory());
                    selectedChain.set("writer", reader);
                }
                setIncludesOrExcludesList((ObjectNode) reader);
            }
        }
    } else {
        ObjectNode root = (ObjectNode) node;
        if (node.has("transformations")) {
            JsonNode transformations = node.get("transformations");
            root.remove("transformations");
            ObjectNode chainNode = new ObjectNode(mapper.getNodeFactory());
            chainNode.set("name", new TextNode("default"));
            chainNode.set("transformations", transformations);
            ArrayNode chains = new ArrayNode(mapper.getNodeFactory());
            chains.add(chainNode);
            if (!chain.equals("default")) {
                chainNode = new ObjectNode(mapper.getNodeFactory());
                chainNode.set("name", new TextNode(chain));
                chains.add(chainNode);
            }
            ObjectNode reader = new ObjectNode(mapper.getNodeFactory());
            setIncludesOrExcludesList(reader);
            chainNode.set("reader", reader);
            ObjectNode writer = new ObjectNode(mapper.getNodeFactory());
            setIncludesOrExcludesList(writer);
            chainNode.set("writer", writer);
            root.set("chains", chains);
        } else if (!node.has("modules")) {
            ObjectNode chainNode = new ObjectNode(mapper.getNodeFactory());
            chainNode.set("name", new TextNode("default"));
            ArrayNode chains = new ArrayNode(mapper.getNodeFactory());
            chains.add(chainNode);
            ObjectNode reader = new ObjectNode(mapper.getNodeFactory());
            setIncludesOrExcludesList(reader);
            chainNode.set("reader", reader);
            ObjectNode writer = new ObjectNode(mapper.getNodeFactory());
            setIncludesOrExcludesList(writer);
            chainNode.set("writer", writer);
            root.set("chains", chains);
        }
    }
    provider.write(node);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project walkmod-core by walkmod.

the class AddTransformationYMLAction method doAction.

@Override
public void doAction(JsonNode chainsNode) throws Exception {
    ArrayNode transformationsNode = null;
    boolean isMultiModule = chainsNode.has("modules");
    ObjectMapper mapper = provider.getObjectMapper();
    if (!isMultiModule) {
        boolean validChainName = chain != null && !"".equals(chain) && !"default".equals(chain);
        if (!chainsNode.has("chains")) {
            if (chainsNode.has("transformations")) {
                JsonNode aux = chainsNode.get("transformations");
                if (aux.isArray()) {
                    transformationsNode = (ArrayNode) aux;
                }
                if (!validChainName) {
                    ObjectNode auxRoot = (ObjectNode) chainsNode;
                    if (transformationsNode == null) {
                        transformationsNode = new ArrayNode(mapper.getNodeFactory());
                    }
                    auxRoot.set("transformations", transformationsNode);
                } else {
                    // reset the root
                    chainsNode = new ObjectNode(mapper.getNodeFactory());
                    ObjectNode auxRoot = (ObjectNode) chainsNode;
                    // the default chain list added
                    ObjectNode chainObject = new ObjectNode(mapper.getNodeFactory());
                    chainObject.set("name", new TextNode("default"));
                    chainObject.set("transformations", transformationsNode);
                    ArrayNode chainsListNode = new ArrayNode(mapper.getNodeFactory());
                    // the requested chain added
                    ObjectNode newChain = new ObjectNode(mapper.getNodeFactory());
                    newChain.set("name", new TextNode(chain));
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
                        newChain.set("reader", readerNode);
                        populateWriterReader(readerNode, path, null, null, null, null);
                        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
                        newChain.set("writer", writerNode);
                        populateWriterReader(writerNode, path, null, null, null, null);
                    }
                    transformationsNode = new ArrayNode(mapper.getNodeFactory());
                    newChain.set("transformations", transformationsNode);
                    if (before == null || !"default".equals(before)) {
                        chainsListNode.add(chainObject);
                    }
                    chainsListNode.add(newChain);
                    if (before != null && "default".equals(before)) {
                        chainsListNode.add(chainObject);
                    }
                    auxRoot.set("chains", chainsListNode);
                }
            } else {
                ObjectNode auxRoot = (ObjectNode) chainsNode;
                transformationsNode = new ArrayNode(mapper.getNodeFactory());
                boolean writeChainInfo = validChainName;
                if (!writeChainInfo) {
                    writeChainInfo = path != null && !"".equals(path.trim());
                    chain = "default";
                }
                if (writeChainInfo) {
                    ArrayNode auxChainsList = new ArrayNode(mapper.getNodeFactory());
                    ObjectNode aux = new ObjectNode(mapper.getNodeFactory());
                    auxChainsList.add(aux);
                    aux.set("name", new TextNode(chain));
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
                        aux.set("reader", readerNode);
                        populateWriterReader(readerNode, path, null, null, null, null);
                    }
                    auxRoot.set("chains", auxChainsList);
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
                        aux.set("writer", writerNode);
                        populateWriterReader(writerNode, path, null, null, null, null);
                    }
                    auxRoot = aux;
                }
                auxRoot.set("transformations", transformationsNode);
            }
        } else {
            if (validChainName) {
                JsonNode aux = chainsNode.get("chains");
                boolean found = false;
                if (aux.isArray()) {
                    Iterator<JsonNode> it = aux.elements();
                    while (it.hasNext()) {
                        JsonNode next = it.next();
                        if (next.has("name")) {
                            String id = next.get("name").asText();
                            if (chain.equals(id)) {
                                found = true;
                                if (next.has("transformations")) {
                                    JsonNode auxTrans = next.get("transformations");
                                    if (auxTrans.isArray()) {
                                        transformationsNode = (ArrayNode) auxTrans;
                                    } else {
                                        throw new Exception("The chain [" + chain + "] does not have a valid transformations node");
                                    }
                                } else if (next.isObject()) {
                                    ObjectNode auxNext = (ObjectNode) next;
                                    transformationsNode = new ArrayNode(mapper.getNodeFactory());
                                    auxNext.set("transformations", transformationsNode);
                                } else {
                                    throw new Exception("The chain [" + chain + "] does not have a valid structure");
                                }
                            }
                        }
                    }
                    if (!found) {
                        ChainConfig chainCfg = new ChainConfigImpl();
                        chainCfg.setName(chain);
                        WalkerConfig walkerCfg = new WalkerConfigImpl();
                        List<TransformationConfig> transfs = new LinkedList<TransformationConfig>();
                        transfs.add(transformationCfg);
                        walkerCfg.setTransformations(transfs);
                        chainCfg.setWalkerConfig(walkerCfg);
                        provider.addChainConfig(chainCfg, false, before);
                        return;
                    }
                }
            } else {
                ObjectNode node = new ObjectNode(mapper.getNodeFactory());
                node.set("name", new TextNode(chain));
                ArrayNode transNodes = new ArrayNode(mapper.getNodeFactory());
                node.set("transformations", transNodes);
                ArrayNode array = (ArrayNode) chainsNode.get("chains");
                array.add(node);
                ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
                transNodes.add(transformationNode);
                createTransformation(transformationNode, transformationCfg);
                return;
            }
        }
        if (transformationsNode != null) {
            ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
            if (order != null && order < transformationsNode.size()) {
                transformationsNode.insert(order, transformationNode);
            } else {
                transformationsNode.add(transformationNode);
            }
            createTransformation(transformationNode, transformationCfg);
            provider.write(chainsNode);
            return;
        } else if (chain != null) {
            throw new Exception("The chain [" + chain + "] does not exists");
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

TextNode (com.fasterxml.jackson.databind.node.TextNode)96 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)35 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)29 JsonNode (com.fasterxml.jackson.databind.JsonNode)28 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)14 IntNode (com.fasterxml.jackson.databind.node.IntNode)14 MockScheduler (org.apache.kafka.common.utils.MockScheduler)14 MockTime (org.apache.kafka.common.utils.MockTime)14 ExpectedTaskBuilder (org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder)14 ExpectedTasks (org.apache.kafka.trogdor.common.ExpectedTasks)13 WorkerRunning (org.apache.kafka.trogdor.rest.WorkerRunning)12 NoOpTaskSpec (org.apache.kafka.trogdor.task.NoOpTaskSpec)11 ArrayList (java.util.ArrayList)9 Scheduler (org.apache.kafka.common.utils.Scheduler)9 MiniTrogdorCluster (org.apache.kafka.trogdor.common.MiniTrogdorCluster)9 CreateTaskRequest (org.apache.kafka.trogdor.rest.CreateTaskRequest)9 TaskRunning (org.apache.kafka.trogdor.rest.TaskRunning)9 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)8