KafkaTopology.kt 4.3 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
 * 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
Jonas Waeber's avatar
Jonas Waeber committed
22
import net.schmizz.sshj.sftp.RemoteFile
Jonas Waeber's avatar
Jonas Waeber committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
37
    private val xmlTransformer = XMLTransformer(settings.appSettings)
Jonas Waeber's avatar
Jonas Waeber committed
38
39
    private val reportingTopic = settings.outputTopic + "-reporting"

Jonas Waeber's avatar
Jonas Waeber committed
40
    fun prepare(): StreamsBuilder {
Jonas Waeber's avatar
Jonas Waeber committed
41
42
43
44
45
46
47
48
49
50
51
52
53
        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
54
55
56
57
58
                Report(
                    key,
                    ReportStatus.failure,
                    ReportMessages.processFailure(key, "The input file is invalid.")
                ).toJson()
Jonas Waeber's avatar
Jonas Waeber committed
59
60
61
62
63
64
65
66
67
68
            }
            .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
69
70
71
72
73
74
75
            .mapValues { _ ->
                ProcessReport(
                    "xml-data-transform",
                    ReportStatus.failure,
                    1,
                    0,
                    1
Jonas Waeber's avatar
Jonas Waeber committed
76
                ).toJson()
Jonas Waeber's avatar
Jonas Waeber committed
77
78
79
80
81
            }
            .to(settings.processReportTopic)

        val transformedValue = errorFilter[1]
            .mapValues { value -> sftpClient.open(File(value.path)) }
Jonas Waeber's avatar
Jonas Waeber committed
82
83
            .map { key, value ->
                value.use {
Jonas Waeber's avatar
Jonas Waeber committed
84
                    xmlTransformer.applyXSLT(key, value)
Jonas Waeber's avatar
Jonas Waeber committed
85
86
                }
            }
Jonas Waeber's avatar
Jonas Waeber committed
87
        streamOutput(transformedValue)
Jonas Waeber's avatar
Jonas Waeber committed
88
        return builder
Jonas Waeber's avatar
Jonas Waeber committed
89
90
91
92
93
94
95
96
    }

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

        stream
Jonas Waeber's avatar
Jonas Waeber committed
97
            .mapValues { value -> value.getReport().toJson() }
Jonas Waeber's avatar
Jonas Waeber committed
98
99
100
            .to(reportingTopic)

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

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