KafkaTopology.kt 6.45 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
 * 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

Jonas Waeber's avatar
Jonas Waeber committed
21
22
import ch.memobase.kafka.utils.ConfigJoiner
import ch.memobase.kafka.utils.models.ImportService
23
import ch.memobase.kafka.utils.models.JoinedValues
Jonas Waeber's avatar
Jonas Waeber committed
24
25
26
27
28
29
import ch.memobase.reporting.Report
import ch.memobase.reporting.ReportStatus
import ch.memobase.settings.HeaderExtractionTransformSupplier
import ch.memobase.settings.SettingsLoader
import ch.memobase.sftp.SftpClient
import java.io.File
Jonas Waeber's avatar
Jonas Waeber committed
30
import net.sf.saxon.s9api.SaxonApiException
Jonas Waeber's avatar
Jonas Waeber committed
31
import org.apache.kafka.common.serialization.Serdes
Jonas Waeber's avatar
Jonas Waeber committed
32
33
34
35
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
36
import org.apache.kafka.streams.kstream.Predicate
Jonas Waeber's avatar
Jonas Waeber committed
37
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
41
import org.memobase.models.Content
import org.memobase.models.Formats
import org.memobase.models.Input
import org.memobase.models.Message
Jonas Waeber's avatar
Jonas Waeber committed
42
43
44
45
import org.memobase.utils.MissingIdentifierException
import org.memobase.utils.SerdeMessage
import org.memobase.utils.XsltException
import org.memobase.xml.XMLTransformer
Jonas Waeber's avatar
Jonas Waeber committed
46
47
48


class KafkaTopology(private val settings: SettingsLoader) {
Jonas Waeber's avatar
Jonas Waeber committed
49
    private val log = LogManager.getLogger("XmlDataTopology")
Jonas Waeber's avatar
Jonas Waeber committed
50
    private val sftpClient: SftpClient = SftpClient(settings.sftpSettings)
Jonas Waeber's avatar
Jonas Waeber committed
51
52
53
54
55
56
57
58
    private val xmlTransformer = XMLTransformer()
    private val configJoiner = ConfigJoiner<Message, ByteArray>(
        ImportService.Transform,
        SerdeMessage(),
        Serdes.ByteArray(),
        xmlTransformer::xsltFunction
    )
    private val reportingTopic = settings.processReportTopic
Jonas Waeber's avatar
Jonas Waeber committed
59
60
61
62

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

Jonas Waeber's avatar
Jonas Waeber committed
63
64
        val configStream = builder.stream<String, String>("import-process-config")
            .map { key, value -> KeyValue(key.toByteArray(), value.toByteArray()) }
Jonas Waeber's avatar
Jonas Waeber committed
65
66
67


        val dataStream = builder
Jonas Waeber's avatar
Jonas Waeber committed
68
69
            .stream<String, String>(settings.inputTopic)
            .flatMapValues { _, value -> parseMessage(value) }
Jonas Waeber's avatar
Jonas Waeber committed
70
71
72
            .filter { _, value ->
                value.format == Formats.xml
            }
Jonas Waeber's avatar
Jonas Waeber committed
73
74
75

        val joinedStream = configJoiner.join(dataStream, configStream)

76
77
78
79
80
81
82
83
84
85
86
        val configNullBranch = joinedStream
            .branch(
                Predicate { _, value -> value.right == null },
                Predicate { _, _ -> true }
            )

        configNullBranch[0]
            .transformValues(HeaderExtractionTransformSupplier<JoinedValues<Message, ByteArray>>())
            .mapValues { key, value ->
                Report(
                    key,
Jonas Waeber's avatar
Jonas Waeber committed
87
88
89
                    ReportStatus.fatal,
                    "Could not find a matching xslt configuration for record set ${value.second.recordSetId}.",
                    Service.name
90
                ).toJson()
91
            }
92
            .to(reportingTopic)
93
94

        val saxHandlerStream = configNullBranch[1]
Jonas Waeber's avatar
Jonas Waeber committed
95
            .mapValues { value ->
96
97
                Input(value.left, value.right)
            }
Jonas Waeber's avatar
Jonas Waeber committed
98
99
100
101
102
103
104
105
            .transformValues(HeaderExtractionTransformSupplier<Input>())
            .mapValues { value ->
                Content(
                    value.first.message,
                    value.second,
                    value.first.xsltData,
                    sftpClient.open(File(value.first.message.path))
                )
Jonas Waeber's avatar
Jonas Waeber committed
106
            }
Jonas Waeber's avatar
Jonas Waeber committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
            .map { key, value ->
                value.inputStream.use {
                    try {
                        val output =
                            xmlTransformer.applyXSLT(key, value.headerMetadata, value.inputStream, value.xsltData)
                        KeyValue(
                            output.first,
                            Pair(output.second.output.toString(), output.second.getReport())
                        )
                    } catch (ex: XsltException) {
                        log.error(ex.message)
                        KeyValue(
                            key, Pair(
                                null, Report(
                                    key,
Jonas Waeber's avatar
Jonas Waeber committed
122
123
124
                                    ReportStatus.fatal,
                                    ex.localizedMessage,
                                    Service.name
Jonas Waeber's avatar
Jonas Waeber committed
125
126
127
128
129
130
131
132
133
                                )
                            )
                        )
                    } catch (ex: MissingIdentifierException) {
                        log.error(ex.message)
                        KeyValue(
                            key, Pair(
                                null, Report(
                                    key,
Jonas Waeber's avatar
Jonas Waeber committed
134
135
136
                                    ReportStatus.fatal,
                                    ex.localizedMessage,
                                    Service.name
Jonas Waeber's avatar
Jonas Waeber committed
137
138
139
                                )
                            )
                        )
Jonas Waeber's avatar
Jonas Waeber committed
140
141
142
143
144
145
                    } catch (ex: SaxonApiException) {
                        log.error(ex.message)
                        KeyValue(
                            key, Pair(
                                null, Report(
                                    key,
Jonas Waeber's avatar
Jonas Waeber committed
146
147
148
                                    ReportStatus.fatal,
                                    ex.localizedMessage,
                                    Service.name
Jonas Waeber's avatar
Jonas Waeber committed
149
150
151
                                )
                            )
                        )
Jonas Waeber's avatar
Jonas Waeber committed
152
153
154
155
                    }
                }
            }
        streamOutput(saxHandlerStream)
Jonas Waeber's avatar
Jonas Waeber committed
156
157
158
        return builder.build()
    }

Jonas Waeber's avatar
Jonas Waeber committed
159
    private fun streamOutput(stream: KStream<String, Pair<String?, Report>>) {
Jonas Waeber's avatar
Jonas Waeber committed
160
        stream
Jonas Waeber's avatar
Jonas Waeber committed
161
162
            .filter { _, value -> value != null }
            .mapValues { value -> value.first }
Jonas Waeber's avatar
Jonas Waeber committed
163
164
165
            .to(settings.outputTopic)

        stream
Jonas Waeber's avatar
Jonas Waeber committed
166
            .mapValues { value -> value.second.toJson() }
Jonas Waeber's avatar
Jonas Waeber committed
167
168
169
170
            .to(reportingTopic)
    }

    private fun parseMessage(value: String): List<Message> {
Jonas Waeber's avatar
Jonas Waeber committed
171
        return Message.fromJson(value)
Jonas Waeber's avatar
Jonas Waeber committed
172
173
    }
}