-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Łukasz Dywicki <luke@code-house.org>
- Loading branch information
Showing
13 changed files
with
588 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
.../src/main/java/org/opennms/miniongateway/grpc/server/kafka/SinkMessageKafkaPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/******************************************************************************* | ||
* This file is part of OpenNMS(R). | ||
* | ||
* Copyright (C) 2022 The OpenNMS Group, Inc. | ||
* OpenNMS(R) is Copyright (C) 1999-2022 The OpenNMS Group, Inc. | ||
* | ||
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. | ||
* | ||
* OpenNMS(R) 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. | ||
* | ||
* OpenNMS(R) 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 OpenNMS(R). If not, see: | ||
* http://www.gnu.org/licenses/ | ||
* | ||
* For more information contact: | ||
* OpenNMS(R) Licensing <license@opennms.org> | ||
* http://www.opennms.org/ | ||
* http://www.opennms.com/ | ||
*******************************************************************************/ | ||
|
||
package org.opennms.miniongateway.grpc.server.kafka; | ||
|
||
import com.google.protobuf.Message; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.opennms.horizon.shared.grpc.common.LocationServerInterceptor; | ||
import org.opennms.horizon.shared.grpc.common.TenantIDGrpcServerInterceptor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
|
||
/** | ||
* A helper class which produces kafka messages. | ||
* | ||
* It additionally retrieves tenant information from present context. | ||
* @param <I> Input message (grpc side) kind | ||
* @param <O> Output message (kafka side) type | ||
*/ | ||
public class SinkMessageKafkaPublisher<I extends Message, O extends Message> { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(SinkMessageKafkaPublisher.class); | ||
private final KafkaTemplate<String, byte[]> kafkaTemplate; | ||
private final TenantIDGrpcServerInterceptor tenantInterceptor; | ||
private final LocationServerInterceptor locationInterceptor; | ||
private final SinkMessageMapper<I, O> mapper; | ||
private final String topic; | ||
|
||
public SinkMessageKafkaPublisher(KafkaTemplate<String, byte[]> kafkaTemplate, | ||
TenantIDGrpcServerInterceptor tenantInterceptor, LocationServerInterceptor locationInterceptor, | ||
SinkMessageMapper<I, O> mapper, String topic) { | ||
this.kafkaTemplate = kafkaTemplate; | ||
this.tenantInterceptor = tenantInterceptor; | ||
this.locationInterceptor = locationInterceptor; | ||
this.mapper = mapper; | ||
this.topic = topic; | ||
} | ||
|
||
/** | ||
* Map passed In message to a backend message which is then used as a payload for record sent to Kafka. | ||
* | ||
* @param message content to include as the message payload. | ||
*/ | ||
public void send(I message) { | ||
String tenantId = tenantInterceptor.readCurrentContextTenantId(); | ||
String locationId = locationInterceptor.readCurrentContextLocationId(); | ||
|
||
O mapped = mapper.map(tenantId, locationId, message); | ||
logger.trace("Received {}; sending a {} to kafka topic {}; tenantId: {}; locationId={}; message={}", | ||
message.getDescriptorForType().getName(), mapped.getDescriptorForType().getName(), topic, tenantId, locationId, mapped); | ||
|
||
kafkaTemplate.send(new ProducerRecord<>(topic, mapped.toByteArray())); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...in/java/org/opennms/miniongateway/grpc/server/kafka/SinkMessageKafkaPublisherFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************* | ||
* This file is part of OpenNMS(R). | ||
* | ||
* Copyright (C) 2023 The OpenNMS Group, Inc. | ||
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc. | ||
* | ||
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. | ||
* | ||
* OpenNMS(R) 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. | ||
* | ||
* OpenNMS(R) 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 OpenNMS(R). If not, see: | ||
* http://www.gnu.org/licenses/ | ||
* | ||
* For more information contact: | ||
* OpenNMS(R) Licensing <license@opennms.org> | ||
* http://www.opennms.org/ | ||
* http://www.opennms.com/ | ||
*******************************************************************************/ | ||
|
||
package org.opennms.miniongateway.grpc.server.kafka; | ||
|
||
import com.google.protobuf.Message; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opennms.horizon.shared.grpc.common.LocationServerInterceptor; | ||
import org.opennms.horizon.shared.grpc.common.TenantIDGrpcServerInterceptor; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SinkMessageKafkaPublisherFactory { | ||
|
||
private final TenantIDGrpcServerInterceptor tenantInterceptor; | ||
private final LocationServerInterceptor locationInterceptor; | ||
private final KafkaTemplate<String, byte[]> kafkaTemplate; | ||
|
||
public <I extends Message, O extends Message> SinkMessageKafkaPublisher<I, O> create(SinkMessageMapper<I, O> mapper, String topic) { | ||
return new SinkMessageKafkaPublisher<>(kafkaTemplate, tenantInterceptor, locationInterceptor, mapper, topic); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...way/main/src/main/java/org/opennms/miniongateway/grpc/server/kafka/SinkMessageMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/******************************************************************************* | ||
* This file is part of OpenNMS(R). | ||
* | ||
* Copyright (C) 2023 The OpenNMS Group, Inc. | ||
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc. | ||
* | ||
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. | ||
* | ||
* OpenNMS(R) 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. | ||
* | ||
* OpenNMS(R) 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 OpenNMS(R). If not, see: | ||
* http://www.gnu.org/licenses/ | ||
* | ||
* For more information contact: | ||
* OpenNMS(R) Licensing <license@opennms.org> | ||
* http://www.opennms.org/ | ||
* http://www.opennms.com/ | ||
*******************************************************************************/ | ||
|
||
package org.opennms.miniongateway.grpc.server.kafka; | ||
|
||
import com.google.protobuf.Message; | ||
|
||
/** | ||
* Mapper for messages sent by minion via sink channels. | ||
* | ||
* @param <I> Minion message. | ||
* @param <O> Backend message. | ||
*/ | ||
public interface SinkMessageMapper<I extends Message, O extends Message> { | ||
|
||
/** | ||
* Maps given message into backend message. | ||
* | ||
* @param tenantId Tenant for which message was generated. | ||
* @param locationId Location for which message was generated. | ||
* @param message Message sent by minion. | ||
* @return Mapped message which should embed both tenantId and locationId. | ||
*/ | ||
O map(String tenantId, String locationId, I message); | ||
|
||
} |
Oops, something went wrong.