# ---------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ---------------------------------------------------------------------------
apiVersion: camel.apache.org/v1
kind: Kamelet
metadata:
  name: cassandra-sink
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "4.10.2"
    camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,[encoded data, 7036 characters]"
    camel.apache.org/provider: "Apache Software Foundation"
    camel.apache.org/kamelet.group: "Cassandra"
    camel.apache.org/kamelet.namespace: "Nosql"
  labels:
    camel.apache.org/kamelet.type: "sink"
spec:
  definition:
    title: "Cassandra Sink"
    description: |-
      Send data to an Apache Cassandra cluster.

      This Kamelet expects JSON Array formatted data. The content of the JSON Array is used as input for the CQL Prepared Statement set in the query parameter.
    required:
      - connectionHost
      - connectionPort
      - keyspace
      - query
    type: object
    properties:
      connectionHost:
        title: Connection Host
        description: The hostname(s) for the Cassandra server(s). Use a comma to separate multiple hostnames.
        type: string
        example: localhost
      connectionPort:
        title: Connection Port
        description: The port number(s) of the cassandra server(s). Use a comma to separate multiple port numbers.
        type: string
        example: 9042
      keyspace:
        title: Keyspace 
        description: The keyspace to use.
        type: string
        example: customers
      username:
        title: Username
        description: The username for accessing a secured Cassandra cluster.
        type: string
        x-descriptors:
        - urn:camel:group:credentials
      password:
        title: Password
        description: The password for accessing a secured Cassandra cluster.
        type: string
        format: password
        x-descriptors:
        - urn:camel:group:credentials
      consistencyLevel:
        title: Consistency Level
        description: The consistency level to use.
        type: string
        default: ANY
        enum: ["ANY", "ONE", "TWO", "THREE", "QUORUM", "ALL", "LOCAL_QUORUM", "EACH_QUORUM", "LOCAL_ONE"]
      prepareStatements:
        title: Prepare Statements
        description: If true, specifies to use PreparedStatements as the query. If false, specifies to use regular Statements as the query.
        type: boolean
        default: true
      query:
        title: Query
        description: The query to execute against the Cassandra cluster table.
        type: string
      extraTypeCodecs:
        title: Extra Type Codecs
        description: To use a specific comma separated list of Extra Type codecs. 
        type: string
        enum: ["BLOB_TO_ARRAY", "BOOLEAN_LIST_TO_ARRAY", "BYTE_LIST_TO_ARRAY", "SHORT_LIST_TO_ARRAY", "INT_LIST_TO_ARRAY", "LONG_LIST_TO_ARRAY", "FLOAT_LIST_TO_ARRAY", "DOUBLE_LIST_TO_ARRAY", "TIMESTAMP_UTC", "TIMESTAMP_MILLIS_SYSTEM", "TIMESTAMP_MILLIS_UTC", "ZONED_TIMESTAMP_SYSTEM", "ZONED_TIMESTAMP_UTC", "ZONED_TIMESTAMP_PERSISTED", "LOCAL_TIMESTAMP_SYSTEM", "LOCAL_TIMESTAMP_UTC"]
      jsonPayload:
        title: JSON Payload
        description: If we want to transform the payload in json or not
        type: boolean
        default: true
  types:
    in:
      mediaType: application/json
  dependencies:
  - "camel:jackson"
  - "camel:kamelet"
  - "camel:cassandraql"
  - "camel:core"
  template:
    beans:
      - name: local-sql-cassandraql-sink
        type: "#class:org.apache.camel.component.cassandra.CassandraComponent"
        properties:
          autowiredEnabled: "false"
    from:
      uri: "kamelet:source"
      steps:
      - choice:
          precondition: true
          when:
            - simple: '${properties:jsonPayload:true}'
              steps:
                - unmarshal:
                    json: 
                      library: Jackson
                      useList: true
      - to: 
          uri: "{{local-sql-cassandraql-sink}}://{{connectionHost}}:{{connectionPort}}/{{keyspace}}"
          parameters:
            username: "{{?username}}"
            password: "{{?password}}"
            prepareStatements: "{{prepareStatements}}"
            consistencyLevel: "{{consistencyLevel}}"
            cql: "{{query}}"
            extraTypeCodecs: "{{?extraTypeCodecs}}"