KafkaTopology.kt 4.31 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
/*
2
 * XML Data Import Service
Jonas Waeber's avatar
Jonas Waeber committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.memobase

import com.beust.klaxon.Klaxon
import org.apache.kafka.streams.KeyValue
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.Topology
import org.apache.kafka.streams.kstream.KStream
import org.apache.kafka.streams.kstream.Predicate
import org.memobase.settings.SettingsLoader
import org.memobase.sftp.SftpClient
import java.io.File
import java.io.InputStream
import java.io.StringReader


class KafkaTopology(private val settings: SettingsLoader) {
    private val sftpClient: SftpClient = SftpClient(settings.sftpSettings)
36
    private val xmlTransformer = XMLTransformer(settings.appSettings)
Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    private val reportingTopic = settings.outputTopic + "-reporting"

    fun build(): Topology {
        val builder = StreamsBuilder()

        val errorFilter = builder
            .stream<String, String>(settings.inputTopic)
            .flatMapValues { _, value -> parseMessage(value) }
            .branch(
                Predicate { _, value -> value.format == Formats.error },
                Predicate { _, _ -> true }
            )

        // report filtered error message from previous job.
        errorFilter[0]
            .mapValues { key, _ ->
Jonas Waeber's avatar
Jonas Waeber committed
53
54
55
56
57
                Report(
                    key,
                    ReportStatus.failure,
                    ReportMessages.processFailure(key, "The input file is invalid.")
                ).toJson()
Jonas Waeber's avatar
Jonas Waeber committed
58
59
60
61
62
63
64
65
66
67
            }
            .to("${settings.outputTopic}-reporting")

        // filtered result simply sends ERROR along!
        errorFilter[0]
            .mapValues { _ -> "ERROR" }
            .to(settings.outputTopic)

        // report full process as failure
        errorFilter[0]
Jonas Waeber's avatar
Jonas Waeber committed
68
69
70
71
72
73
74
            .mapValues { _ ->
                ProcessReport(
                    "xml-data-transform",
                    ReportStatus.failure,
                    1,
                    0,
                    1
Jonas Waeber's avatar
Jonas Waeber committed
75
                ).toJson()
Jonas Waeber's avatar
Jonas Waeber committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
            }
            .to(settings.processReportTopic)

        val transformedValue = errorFilter[1]
            .mapValues { value -> sftpClient.open(File(value.path)) }
            .map { key, value -> transformXml(key, value) }

        streamOutput(transformedValue)

        return builder.build()
    }

    private fun streamOutput(stream: KStream<String, SAXContentHandler>) {
        stream
            .mapValues { value -> value.output.toString() }
            .to(settings.outputTopic)

        stream
Jonas Waeber's avatar
Jonas Waeber committed
94
            .mapValues { value -> value.getReport().toJson() }
Jonas Waeber's avatar
Jonas Waeber committed
95
96
97
            .to(reportingTopic)

        stream
Jonas Waeber's avatar
Jonas Waeber committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
            .mapValues { value ->
                val report = value.getReport()
                if (report.status == ReportStatus.success) {
                    ProcessReport(
                        report.id,
                        ReportStatus.success,
                        1,
                        1,
                        0
                    )
                } else {
                    ProcessReport(
                        report.id,
                        ReportStatus.failure,
                        1,
                        0,
                        1
                    )
                }
            }
Jonas Waeber's avatar
Jonas Waeber committed
118
            .mapValues { value -> value.toJson() }
Jonas Waeber's avatar
Jonas Waeber committed
119
120
121
122
123
124
125
126
127
128
129
            .to(settings.processReportTopic)
    }

    private fun parseMessage(value: String): List<Message> {
        return Klaxon().parse<Message>(StringReader(value)).let {
            if (it == null) emptyList()
            else listOf(it)
        }
    }

    private fun transformXml(key: String, data: InputStream): KeyValue<String, SAXContentHandler> {
130
        return xmlTransformer.applyXSLT(key, data)
Jonas Waeber's avatar
Jonas Waeber committed
131
132
133
134
    }


}