In order to mitigate against the brute force attacks against Gitlab accounts, we are moving to all edu-ID Logins. We would like to remind you to link your account with your edu-id. Login will be possible only by edu-ID after November 30, 2021. Here you can find the instructions for linking your account.

If you don't have a SWITCH edu-ID, you can create one with this guide here

kind regards

KafkaTopology.kt 2.59 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
/*
Jonas Waeber's avatar
Jonas Waeber committed
2
 * Drupal Sync 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
 * 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 com.beust.klaxon.KlaxonException
23
import java.io.StringReader
Jonas Waeber's avatar
Jonas Waeber committed
24
import org.apache.jena.rdf.model.Model
Jonas Waeber's avatar
Jonas Waeber committed
25
26
27
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.Topology
import org.apache.logging.log4j.LogManager
28
29
30
import org.memobase.model.Input
import org.memobase.model.Institution
import org.memobase.model.RecordSet
Jonas Waeber's avatar
Jonas Waeber committed
31
32
33
import org.memobase.settings.SettingsLoader

class KafkaTopology(private val settings: SettingsLoader) {
34
    private val log = LogManager.getLogger("DrupalSyncProcessor")
Jonas Waeber's avatar
Jonas Waeber committed
35
    private val transformer = Transform()
Jonas Waeber's avatar
Jonas Waeber committed
36

Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
41
    fun build(): Topology {
        val builder = StreamsBuilder()
        val stream = builder.stream<String, String>(settings.inputTopic)
        stream
            .flatMapValues { value -> parseJson(value) }
42
            .flatMapValues { value -> transformJson(value) }
Jonas Waeber's avatar
Jonas Waeber committed
43
            .map { _, value -> transformer.write(value.first, value.second) }
Jonas Waeber's avatar
Jonas Waeber committed
44
45
46
47
48
            .to(settings.outputTopic)

        return builder.build()
    }

Jonas Waeber's avatar
Jonas Waeber committed
49
    private fun parseJson(data: String): List<Input> {
Jonas Waeber's avatar
Jonas Waeber committed
50
        return try {
Jonas Waeber's avatar
Jonas Waeber committed
51
            val int = Klaxon().parse<Input>(StringReader(data))
52
53
            return if (int == null) emptyList()
            else listOf(int)
Jonas Waeber's avatar
Jonas Waeber committed
54
        } catch (ex: KlaxonException) {
55
            log.error(ex.localizedMessage)
Jonas Waeber's avatar
Jonas Waeber committed
56
            emptyList()
Jonas Waeber's avatar
Jonas Waeber committed
57
58
59
        } catch (ex: ClassCastException) {
            log.error(ex.localizedMessage)
            emptyList()
Jonas Waeber's avatar
Jonas Waeber committed
60
61
62
        }
    }

63
    private fun transformJson(input: Input): List<Pair<String, Model>> {
Jonas Waeber's avatar
Jonas Waeber committed
64
        return when (input) {
65
66
67
68
69
70
            is Institution -> listOf(transformer.createInstitution(input))
            is RecordSet -> listOf(transformer.createRecordSet(input))
            else -> {
                log.error("Could not cast input to institution or record set: $input.")
                return emptyList()
            }
Jonas Waeber's avatar
Jonas Waeber committed
71
        }
Jonas Waeber's avatar
Jonas Waeber committed
72
73
    }
}