001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model;
018
019import java.io.InputStream;
020import java.util.Collection;
021import java.util.List;
022import java.util.Map;
023import java.util.function.Function;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
027import org.apache.camel.model.rest.RestDefinition;
028import org.apache.camel.model.transformer.TransformerDefinition;
029import org.apache.camel.model.validator.ValidatorDefinition;
030import org.apache.camel.support.PatternHelper;
031
032/**
033 * Model interface
034 */
035public interface Model {
036
037    /**
038     * Returns a list of the current route definitions
039     *
040     * @return list of the current route definitions
041     */
042    List<RouteDefinition> getRouteDefinitions();
043
044    /**
045     * Gets the route definition with the given id
046     *
047     * @param id id of the route
048     * @return the route definition or <tt>null</tt> if not found
049     */
050    RouteDefinition getRouteDefinition(String id);
051
052    /**
053     * Adds a collection of route definitions to the context
054     * <p/>
055     * <b>Important: </b> Each route in the same {@link CamelContext} must have
056     * an <b>unique</b> route id. If you use the API from {@link CamelContext}
057     * or {@link Model} to add routes, then any new routes which has a route id
058     * that matches an old route, then the old route is replaced by the new
059     * route.
060     *
061     * @param is input stream with the route(s) definition to add
062     * @throws Exception if the route definitions could not be added for
063     *             whatever reason
064     */
065    void addRouteDefinitions(InputStream is) throws Exception;
066
067    /**
068     * Adds a collection of route definitions to the context
069     * <p/>
070     * <b>Important: </b> Each route in the same {@link CamelContext} must have
071     * an <b>unique</b> route id. If you use the API from {@link CamelContext}
072     * or {@link Model} to add routes, then any new routes which has a route id
073     * that matches an old route, then the old route is replaced by the new
074     * route.
075     *
076     * @param routeDefinitions the route(s) definition to add
077     * @throws Exception if the route definitions could not be added for
078     *             whatever reason
079     */
080    void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception;
081
082    /**
083     * Add a route definition to the context
084     * <p/>
085     * <b>Important: </b> Each route in the same {@link CamelContext} must have
086     * an <b>unique</b> route id. If you use the API from {@link CamelContext}
087     * or {@link Model} to add routes, then any new routes which has a route id
088     * that matches an old route, then the old route is replaced by the new
089     * route.
090     *
091     * @param routeDefinition the route definition to add
092     * @throws Exception if the route definition could not be added for whatever
093     *             reason
094     */
095    void addRouteDefinition(RouteDefinition routeDefinition) throws Exception;
096
097    /**
098     * Removes a collection of route definitions from the context - stopping any
099     * previously running routes if any of them are actively running
100     *
101     * @param routeDefinitions route(s) definitions to remove
102     * @throws Exception if the route definitions could not be removed for
103     *             whatever reason
104     */
105    void removeRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception;
106
107    /**
108     * Removes a route definition from the context - stopping any previously
109     * running routes if any of them are actively running
110     *
111     * @param routeDefinition route definition to remove
112     * @throws Exception if the route definition could not be removed for
113     *             whatever reason
114     */
115    void removeRouteDefinition(RouteDefinition routeDefinition) throws Exception;
116
117    /**
118     * Returns a list of the current REST definitions
119     *
120     * @return list of the current REST definitions
121     */
122    List<RestDefinition> getRestDefinitions();
123
124    /**
125     * Adds a collection of rest definitions to the context
126     *
127     * @param is input stream with the rest(s) definition to add
128     * @param addToRoutes whether the rests should also automatically be added
129     *            as routes
130     * @throws Exception if the rest definitions could not be created for
131     *             whatever reason
132     */
133    void addRestDefinitions(InputStream is, boolean addToRoutes) throws Exception;
134
135    /**
136     * Adds a collection of rest definitions to the context
137     *
138     * @param restDefinitions the rest(s) definition to add
139     * @param addToRoutes whether the rests should also automatically be added
140     *            as routes
141     * @throws Exception if the rest definitions could not be created for
142     *             whatever reason
143     */
144    void addRestDefinitions(Collection<RestDefinition> restDefinitions, boolean addToRoutes) throws Exception;
145
146    /**
147     * Sets the data formats that can be referenced in the routes.
148     *
149     * @param dataFormats the data formats
150     */
151    void setDataFormats(Map<String, DataFormatDefinition> dataFormats);
152
153    /**
154     * Gets the data formats that can be referenced in the routes.
155     *
156     * @return the data formats available
157     */
158    Map<String, DataFormatDefinition> getDataFormats();
159
160    /**
161     * Resolve a data format definition given its name
162     *
163     * @param name the data format definition name or a reference to it in the
164     *            {@link org.apache.camel.spi.Registry}
165     * @return the resolved data format definition, or <tt>null</tt> if not
166     *         found
167     */
168    DataFormatDefinition resolveDataFormatDefinition(String name);
169
170    /**
171     * Gets the processor definition from any of the routes which with the given
172     * id
173     *
174     * @param id id of the processor definition
175     * @return the processor definition or <tt>null</tt> if not found
176     */
177    ProcessorDefinition getProcessorDefinition(String id);
178
179    /**
180     * Gets the processor definition from any of the routes which with the given
181     * id
182     *
183     * @param id id of the processor definition
184     * @param type the processor definition type
185     * @return the processor definition or <tt>null</tt> if not found
186     * @throws ClassCastException is thrown if the type is not correct type
187     */
188    <T extends ProcessorDefinition> T getProcessorDefinition(String id, Class<T> type);
189
190    /**
191     * Sets the validators that can be referenced in the routes.
192     *
193     * @param validators the validators
194     */
195    void setValidators(List<ValidatorDefinition> validators);
196
197    /**
198     * Gets the Hystrix configuration by the given name. If no name is given the
199     * default configuration is returned, see <tt>setHystrixConfiguration</tt>
200     *
201     * @param id id of the configuration, or <tt>null</tt> to return the default
202     *            configuration
203     * @return the configuration, or <tt>null</tt> if no configuration has been
204     *         registered
205     */
206    HystrixConfigurationDefinition getHystrixConfiguration(String id);
207
208    /**
209     * Sets the default Hystrix configuration
210     *
211     * @param configuration the configuration
212     */
213    void setHystrixConfiguration(HystrixConfigurationDefinition configuration);
214
215    /**
216     * Sets the Hystrix configurations
217     *
218     * @param configurations the configuration list
219     */
220    void setHystrixConfigurations(List<HystrixConfigurationDefinition> configurations);
221
222    /**
223     * Adds the Hystrix configuration
224     *
225     * @param id name of the configuration
226     * @param configuration the configuration
227     */
228    void addHystrixConfiguration(String id, HystrixConfigurationDefinition configuration);
229
230    /**
231     * Gets the validators that can be referenced in the routes.
232     *
233     * @return the validators available
234     */
235    List<ValidatorDefinition> getValidators();
236
237    /**
238     * Sets the transformers that can be referenced in the routes.
239     *
240     * @param transformers the transformers
241     */
242    void setTransformers(List<TransformerDefinition> transformers);
243
244    /**
245     * Gets the transformers that can be referenced in the routes.
246     *
247     * @return the transformers available
248     */
249    List<TransformerDefinition> getTransformers();
250
251    /**
252     * Gets the service call configuration by the given name. If no name is
253     * given the default configuration is returned, see
254     * <tt>setServiceCallConfiguration</tt>
255     *
256     * @param serviceName name of service, or <tt>null</tt> to return the
257     *            default configuration
258     * @return the configuration, or <tt>null</tt> if no configuration has been
259     *         registered
260     */
261    ServiceCallConfigurationDefinition getServiceCallConfiguration(String serviceName);
262
263    /**
264     * Sets the default service call configuration
265     *
266     * @param configuration the configuration
267     */
268    void setServiceCallConfiguration(ServiceCallConfigurationDefinition configuration);
269
270    /**
271     * Sets the service call configurations
272     *
273     * @param configurations the configuration list
274     */
275    void setServiceCallConfigurations(List<ServiceCallConfigurationDefinition> configurations);
276
277    /**
278     * Adds the service call configuration
279     *
280     * @param serviceName name of the service
281     * @param configuration the configuration
282     */
283    void addServiceCallConfiguration(String serviceName, ServiceCallConfigurationDefinition configuration);
284
285    /**
286     * Start all routes from this model.
287     */
288    void startRouteDefinitions() throws Exception;
289
290    /**
291     * Used for filtering routes routes matching the given pattern, which
292     * follows the following rules: - Match by route id - Match by route input
293     * endpoint uri The matching is using exact match, by wildcard and regular
294     * expression as documented by
295     * {@link PatternHelper#matchPattern(String, String)}. For example to only
296     * include routes which starts with foo in their route id's, use:
297     * include=foo&#42; And to exclude routes which starts from JMS endpoints,
298     * use: exclude=jms:&#42; Exclude takes precedence over include.
299     *
300     * @param include the include pattern
301     * @param exclude the exclude pattern
302     */
303    void setRouteFilterPattern(String include, String exclude);
304
305    /**
306     * Sets a custom route filter to use for filtering unwanted routes when
307     * routes are added.
308     *
309     * @param filter the filter
310     */
311    void setRouteFilter(Function<RouteDefinition, Boolean> filter);
312
313    /**
314     * Gets the current route filter
315     *
316     * @return the filter, or <tt>null</tt> if no custom filter has been
317     *         configured.
318     */
319    Function<RouteDefinition, Boolean> getRouteFilter();
320
321}