001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 *
017 * Contributors:
018 *     bdelbosc
019 */
020package org.nuxeo.runtime.stream;
021
022import java.lang.reflect.InvocationTargetException;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XNodeMap;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.lib.stream.computation.Settings;
033import org.nuxeo.lib.stream.computation.Topology;
034
035@SuppressWarnings("CanBeFinal")
036@XObject("streamProcessor")
037public class StreamProcessorDescriptor {
038    public static final Integer DEFAULT_CONCURRENCY = 4;
039
040    @XNode("@name")
041    public String name;
042
043    @XNode("@logConfig")
044    public String config;
045
046    @XNode("@class")
047    public Class<? extends StreamProcessorTopology> klass;
048
049    @XNode("@defaultConcurrency")
050    public Integer defaultConcurrency = DEFAULT_CONCURRENCY;
051
052    @XNode("@defaultPartitions")
053    public Integer defaultPartitions = DEFAULT_CONCURRENCY;
054
055    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
056    public Map<String, String> options = new HashMap<>();
057
058    @XNodeList(value = "computation", type = ArrayList.class, componentType = ComputationDescriptor.class)
059    public List<ComputationDescriptor> computations = new ArrayList<>(0);
060
061    @XNodeList(value = "stream", type = ArrayList.class, componentType = StreamDescriptor.class)
062    public List<StreamDescriptor> streams = new ArrayList<>(0);
063
064    public String getName() {
065        return name;
066    }
067
068    public Settings getSettings() {
069        Settings settings = new Settings(defaultConcurrency, defaultPartitions);
070        computations.forEach(comp -> settings.setConcurrency(comp.name, comp.concurrency));
071        streams.forEach(stream -> settings.setPartitions(stream.name, stream.partitions));
072        return settings;
073    }
074
075    public Topology getTopology() {
076        try {
077            return klass.getDeclaredConstructor().newInstance().getTopology(options);
078        } catch (InstantiationException | IllegalAccessException | InvocationTargetException
079                | NoSuchMethodException e) {
080            throw new RuntimeException("Can not create topology for processor: " + name, e);
081        }
082
083    }
084
085    @SuppressWarnings("CanBeFinal")
086    @XObject(value = "computation")
087    public static class ComputationDescriptor {
088        @XNode("@name")
089        public String name;
090
091        @XNode("@concurrency")
092        public Integer concurrency = DEFAULT_CONCURRENCY;
093
094        public ComputationDescriptor() {
095        }
096    }
097
098    @SuppressWarnings("CanBeFinal")
099    @XObject(value = "stream")
100    public static class StreamDescriptor {
101
102        @XNode("@name")
103        public String name;
104
105        @XNode("@partitions")
106        public Integer partitions = DEFAULT_CONCURRENCY;
107
108        public StreamDescriptor() {
109        }
110    }
111}