|
| 1 | +# |
| 2 | +# Copyright IBM Corporation 2021 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +from rayvens.core.common import get_run_mode, await_start, brokers |
| 18 | +from rayvens.core.common import kafka_send_to, kafka_recv_from |
| 19 | +from rayvens.core.catalog import construct_source, construct_sink |
| 20 | +from rayvens.core.integration import Integration |
| 21 | + |
| 22 | + |
| 23 | +def start(camel_mode, check_port): |
| 24 | + return Camel(get_run_mode(camel_mode, check_port)) |
| 25 | + |
| 26 | + |
| 27 | +class Camel: |
| 28 | + def __init__(self, mode): |
| 29 | + self.mode = mode |
| 30 | + self.mode.transport = 'kafka' |
| 31 | + |
| 32 | + def add_source(self, stream, source, source_name): |
| 33 | + # Construct integration |
| 34 | + integration = Integration(stream.name, source_name, source) |
| 35 | + |
| 36 | + # Prepare env: |
| 37 | + integration.prepare_environment(self.mode) |
| 38 | + |
| 39 | + # Determine the `to` endpoint value made up of a base address and |
| 40 | + # a custom route provided by the user. Use this to construct the |
| 41 | + # integration source code. |
| 42 | + integration_content = construct_source( |
| 43 | + source, |
| 44 | + f'kafka:{integration.kafka_transport_topic}?brokers={brokers()}') |
| 45 | + |
| 46 | + # Start running the source integration. |
| 47 | + integration.invoke_run(self.mode, integration_content) |
| 48 | + |
| 49 | + # Set up source for the HTTP connector case. |
| 50 | + kafka_send_to(integration.kafka_transport_topic, |
| 51 | + integration.kafka_transport_partitions, stream.actor) |
| 52 | + |
| 53 | + if not await_start(self.mode, integration): |
| 54 | + raise RuntimeError('Could not start source') |
| 55 | + return integration |
| 56 | + |
| 57 | + def add_sink(self, stream, sink, sink_name): |
| 58 | + # Construct integration |
| 59 | + integration = Integration(stream.name, sink_name, sink) |
| 60 | + |
| 61 | + # Prepare env: |
| 62 | + integration.prepare_environment(self.mode) |
| 63 | + |
| 64 | + # Get integration source code. |
| 65 | + integration_content = construct_sink( |
| 66 | + sink, |
| 67 | + f'kafka:{integration.kafka_transport_topic}?brokers={brokers()}') |
| 68 | + |
| 69 | + # Start running the integration. |
| 70 | + integration.invoke_run(self.mode, integration_content) |
| 71 | + |
| 72 | + kafka_recv_from(sink_name, integration.kafka_transport_topic, |
| 73 | + stream.actor) |
| 74 | + |
| 75 | + # Wait for integration to finish. |
| 76 | + if not await_start(self.mode, integration): |
| 77 | + raise RuntimeError('Could not start sink') |
| 78 | + |
| 79 | + return integration |
| 80 | + |
| 81 | + def disconnect(self, integration): |
| 82 | + integration.disconnect(self.mode) |
0 commit comments